Commit 0bc88c6c authored by Mark Florisson's avatar Mark Florisson

Compile imported module conditionally for faster development based on version

parent 7ecf30ab
......@@ -11,48 +11,7 @@ import imp
import pyximport
# set up the PyxArgs global variable in pyximport (why is that a global :)
importers = pyximport.install(pyimport=True)
pyximport.uninstall(*importers)
def _import_normal(modulename):
# __import__ does not take keyword arguments under 2.4
return __import__(modulename, None, None, [''])
def _import_compile(modulename):
if '.' in modulename:
packagename, modulename = modulename.rsplit('.', 1)
__path__ = _import_normal(packagename).__path__
else:
__path__ = None
file, filename, description = imp.find_module(modulename, __path__)
if file:
file.close()
else:
raise ImportError(modulename)
return pyximport.load_module(modulename, filename)
def importer(modulename, compile=False, version=None):
"""
Import a module. If compile is true, always try to compile the .py file.
Otherwise, try a regular import and if that fails (i.e. there is a
syntax error, try to compile it.
"""
if version is not None and sys.version_info[:2] >= version and not compile:
return _import_normal(modulename)
if compile:
return _import_compile(modulename)
else:
try:
return _import_normal(modulename)
except SyntaxError:
return _import_compile(modulename)
def importer(modulename):
def importer(modulename, version=None):
try:
# Check for an already compiled module
return __import__(modulename, None, None, [''])
......@@ -62,4 +21,11 @@ def importer(modulename):
dirname = os.path.dirname
root = dirname(dirname(dirname(os.path.abspath(__file__))))
filename = os.path.join(root, *modulename.split('.')) + ".pyx"
return pyximport.load_module(modulename, filename)
\ No newline at end of file
if version and version < sys.version_info[:2]:
return pyximport.load_module(modulename, filename)
else:
mod = imp.new_module(modulename)
exec open(filename).read() in mod.__dict__, mod.__dict__
sys.modules[modulename] = mod
return mod
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment