Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
cython
Commits
17bef673
Commit
17bef673
authored
Oct 12, 2016
by
Sergei Lebedev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allowed importing pyx files from ZIP archives
parent
0d27e211
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
33 deletions
+77
-33
pyximport/pyximport.py
pyximport/pyximport.py
+39
-26
pyximport/test/test_pyximport.py
pyximport/test/test_pyximport.py
+38
-7
No files found.
pyximport/pyximport.py
View file @
17bef673
...
...
@@ -47,10 +47,11 @@ the documentation.
This code is based on the Py2.3+ import protocol as described in PEP 302.
"""
import
sys
import
os
import
glob
import
imp
import
os
import
sys
from
zipimport
import
zipimporter
,
ZipImportError
mod_name
=
"pyximport"
...
...
@@ -300,19 +301,31 @@ class PyxImporter(object):
paths
=
package_path
else
:
paths
=
sys
.
path
join_path
=
os
.
path
.
join
is_file
=
os
.
path
.
isfile
is_abs
=
os
.
path
.
isabs
abspath
=
os
.
path
.
abspath
#is_dir = os.path.isdir
sep
=
os
.
path
.
sep
for
path
in
paths
:
if
not
path
:
path
=
os
.
getcwd
()
elif
not
is_abs
(
path
):
path
=
abspath
(
path
)
if
is_file
(
path
+
sep
+
pyx_module_name
):
return
PyxLoader
(
fullname
,
join_path
(
path
,
pyx_module_name
),
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
)
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
)
...
...
pyximport/test/test_pyximport.py
View file @
17bef673
from
__future__
import
absolute_import
,
print_function
from
pyximport
import
pyximport
;
pyximport
.
install
(
reload_support
=
True
)
from
pyximport
import
pyximport
pyximport
.
install
(
reload_support
=
True
)
import
os
,
sys
import
time
,
shutil
import
os
import
shutil
import
sys
import
tempfile
import
time
from
zipfile
import
ZipFile
try
:
from
__builtin__
import
reload
except
ImportError
:
from
importlib
import
reload
def
make_tempdir
():
...
...
@@ -27,7 +36,7 @@ def on_remove_file_error(func, path, excinfo):
print
(
"You may want to delete this yourself when you get a chance."
)
def
test
():
def
test
_with_reload
():
pyximport
.
_test_files
=
[]
tempdir
=
make_tempdir
()
sys
.
path
.
append
(
tempdir
)
...
...
@@ -68,5 +77,27 @@ def make_ext(name, filename):
remove_tempdir
(
tempdir
)
if
__name__
==
"__main__"
:
test
()
def
test_zip
():
try
:
import
test_zip_module
except
ImportError
:
pass
else
:
assert
False
,
"test_zip_module already exists"
fd
,
zip_path
=
tempfile
.
mkstemp
(
suffix
=
".zip"
)
os
.
close
(
fd
)
try
:
with
ZipFile
(
zip_path
,
"w"
)
as
zf
:
zf
.
writestr
(
"test_zip_module.pyx"
,
b"x = 42"
)
sys
.
path
.
insert
(
0
,
zip_path
)
import
test_zip_module
assert
test_zip_module
.
x
==
42
finally
:
os
.
remove
(
zip_path
)
if
__name__
==
"__main__"
:
test_with_reload
()
test_zip
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment