diff options
author | Sergei Lebedev <superbobry@gmail.com> | 2017-02-10 22:33:00 +0100 |
---|---|---|
committer | Sergei Lebedev <superbobry@gmail.com> | 2017-02-11 13:23:46 +0100 |
commit | deef9b9fe048ee7a312fd8a3edf33060d5c75c22 (patch) | |
tree | 29987cbad4fb7eddcd1962a2c2f880a35204e089 /pyximport | |
parent | 17bef6731460f078a68dc56914763b23ed528121 (diff) | |
download | cython-deef9b9fe048ee7a312fd8a3edf33060d5c75c22.tar.gz |
Addressed review comments
Diffstat (limited to 'pyximport')
-rw-r--r-- | pyximport/pyximport.py | 44 | ||||
-rw-r--r-- | pyximport/test/test_pyximport.py | 9 |
2 files changed, 33 insertions, 20 deletions
diff --git a/pyximport/pyximport.py b/pyximport/pyximport.py index 84a18d7d9..5572d1c86 100644 --- a/pyximport/pyximport.py +++ b/pyximport/pyximport.py @@ -308,27 +308,31 @@ class PyxImporter(object): elif not os.path.isabs(path): path = os.path.abspath(path) - try: - zi = zipimporter(path) - data = zi.get_data(pyx_module_name) - except (ZipImportError, IOError): - pyx_module_path = os.path.join(path, pyx_module_name) + if os.path.isfile(path): + try: + zi = zipimporter(path) + data = zi.get_data(pyx_module_name) + except (ZipImportError, IOError): + continue # Module not found. + else: + # XXX unzip the imported file into the build dir. A bit + # hacky, but it works! + if not os.path.exists(self.pyxbuild_dir): + os.makedirs(self.pyxbuild_dir) + + pyx_module_path = os.path.join(self.pyxbuild_dir, + pyx_module_name) + with open(pyx_module_path, "wb") as f: + f.write(data) else: - # XXX unzip the imported file into the build dir. A bit - # hacky, but it works! - if not os.path.exists(self.pyxbuild_dir): - os.makedirs(self.pyxbuild_dir) - - pyx_module_path = os.path.join(self.pyxbuild_dir, - pyx_module_name) - with open(pyx_module_path, "wb") as handle: - handle.write(data) - - if os.path.isfile(pyx_module_path): - return PyxLoader(fullname, pyx_module_path, - pyxbuild_dir=self.pyxbuild_dir, - inplace=self.inplace, - language_level=self.language_level) + pyx_module_path = os.path.join(path, pyx_module_name) + if not os.path.isfile(pyx_module_path): + continue # Module not found. + + return PyxLoader(fullname, pyx_module_path, + pyxbuild_dir=self.pyxbuild_dir, + inplace=self.inplace, + language_level=self.language_level) # not found, normal package, not a .pyx file, none of our business _debug("%s not found" % fullname) diff --git a/pyximport/test/test_pyximport.py b/pyximport/test/test_pyximport.py index 1882c3397..1825b6cc1 100644 --- a/pyximport/test/test_pyximport.py +++ b/pyximport/test/test_pyximport.py @@ -96,6 +96,15 @@ def test_zip(): assert test_zip_module.x == 42 finally: os.remove(zip_path) + sys.path.remove(zip_path) + + +def test_zip_nonexisting(): + sys.path.append("nonexisting_zip_module.zip") + try: + import nonexisting_zip_module + except ImportError: + pass if __name__== "__main__": |