Skip to content

Instantly share code, notes, and snippets.

@itsbth
Created February 26, 2015 00:26
Show Gist options
  • Save itsbth/154bd3a32e774f5cf2a5 to your computer and use it in GitHub Desktop.
Save itsbth/154bd3a32e774f5cf2a5 to your computer and use it in GitHub Desktop.
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