summaryrefslogtreecommitdiff
path: root/pyximport/pyximport.py
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2011-10-16 13:42:01 +0200
committerStefan Behnel <stefan_ml@behnel.de>2011-10-16 13:42:01 +0200
commit672ac430a22b41bd21ff40a7e37d9d4b01e312ef (patch)
tree1bc3ecff27540d9319ad9b7dd402c0784ed3edde /pyximport/pyximport.py
parente1f45deccacf1db49139568c5a7c2e33cbed6b4c (diff)
downloadcython-672ac430a22b41bd21ff40a7e37d9d4b01e312ef.tar.gz
new pyximport option 'load_py_module_on_import_failure': when .py build succeeds but import fails, retry by loading the plain .py module
--HG-- extra : rebase_source : df6019c5cf3c2b670fb4789bf871ce8b15be2b7f
Diffstat (limited to 'pyximport/pyximport.py')
-rw-r--r--pyximport/pyximport.py24
1 files changed, 20 insertions, 4 deletions
diff --git a/pyximport/pyximport.py b/pyximport/pyximport.py
index 052a9f3bd..264170bb5 100644
--- a/pyximport/pyximport.py
+++ b/pyximport/pyximport.py
@@ -184,9 +184,14 @@ def load_module(name, pyxfilename, pyxbuild_dir=None):
mod = imp.load_dynamic(name, so_path)
assert mod.__file__ == so_path, (mod.__file__, so_path)
except Exception:
- import traceback
- raise ImportError("Building module failed: %s" %
- traceback.format_exception_only(*sys.exc_info()[:2])),None,sys.exc_info()[2]
+ if pyxargs.load_py_module_on_import_failure and pyxfilename.endswith('.py'):
+ # try to fall back to normal import
+ mod = imp.load_source(name, pyxfilename)
+ assert mod.__file__ in (pyxfilename, pyxfilename+'c', pyxfilename+'o'), (mod.__file__, pyxfilename)
+ else:
+ import traceback
+ raise ImportError("Building module failed: %s" %
+ traceback.format_exception_only(*sys.exc_info()[:2])),None,sys.exc_info()[2]
return mod
@@ -345,7 +350,8 @@ class PyxArgs(object):
##pyxargs=None
def install(pyximport=True, pyimport=False, build_dir=None, build_in_temp=True,
- setup_args={}, reload_support=False ):
+ setup_args={}, reload_support=False,
+ load_py_module_on_import_failure=False):
"""Main entry point. Call this to install the .pyx import hook in
your meta-path for a single Python process. If you want it to be
installed whenever you use Python, add it to your sitecustomize
@@ -374,6 +380,15 @@ def install(pyximport=True, pyimport=False, build_dir=None, build_in_temp=True,
reload(<pyxmodulename>), e.g. after a change in the Cython code.
Additional files <so_path>.reloadNN may arise on that account, when
the previously loaded module file cannot be overwritten.
+
+ ``load_py_module_on_import_failure``: If the compilation of a .py
+ file succeeds, but the subsequent import fails for some reason,
+ retry the import with the normal .py module instead of the
+ compiled module. Note that this may lead to unpredictable results
+ for modules that change the system state during their import, as
+ the second import will rerun these modifications in whatever state
+ the system was left after the import of the compiled module
+ failed.
"""
if not build_dir:
build_dir = os.path.expanduser('~/.pyxbld')
@@ -384,6 +399,7 @@ def install(pyximport=True, pyimport=False, build_dir=None, build_in_temp=True,
pyxargs.build_in_temp = build_in_temp
pyxargs.setup_args = (setup_args or {}).copy()
pyxargs.reload_support = reload_support
+ pyxargs.load_py_module_on_import_failure = load_py_module_on_import_failure
has_py_importer = False
has_pyx_importer = False