Created
February 26, 2015 00:26
-
-
Save itsbth/154bd3a32e774f5cf2a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import subprocess | |
import imp | |
from os import path | |
class CppFinder(object): | |
def find_module(self, fullname, ignored=None): | |
print("find_module(self, {}, {})".format(fullname, path)) | |
if path.exists(fullname + ".cpp"): | |
return CppLoader() | |
raise ImportError | |
def __call__(self, path): | |
print("__call__(self, {})".format(path)) | |
return self | |
class CppLoader(object): | |
def load_module(self, fullname): | |
print("load_module(self, {})".format(fullname)) | |
if fullname in sys.modules: | |
return sys.modules[fullname] | |
runnable = "./{}.cpp.program".format(fullname) | |
subprocess.call(["g++", "-o", runnable, fullname + ".cpp"]) | |
mod = imp.new_module(fullname) | |
mod.__file__ = fullname | |
mod.__path__ = [] | |
mod.__loader__ = self | |
def run(*args): | |
return subprocess.call((runnable,) + args) | |
mod.run = run | |
sys.modules.setdefault(fullname, mod) | |
return mod | |
def register(): | |
subprocess.call(("true",)) | |
sys.meta_path.append(CppFinder()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment