-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AssertImportHook.is_package() method #136
Changes from 7 commits
f68ed2c
60d54af
0d0e1b8
eea0535
e34a28c
bc0a749
0472dc4
a84e7ec
0832fe8
d41b861
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import imp | ||
import inspect | ||
import marshal | ||
import os | ||
import sys | ||
|
||
|
@@ -277,9 +278,15 @@ def load_module(self, name): | |
if name in sys.modules: | ||
return sys.modules[name] | ||
|
||
source, filename, newpath = self.get_source(name) | ||
source = self.get_source(name) | ||
filename = self.get_filename(name) | ||
(fd, fn, info), path = self._cache[name] | ||
|
||
if self.is_package(name): | ||
newpath = [fn] | ||
else: | ||
newpath = None | ||
|
||
if source is None: | ||
return imp.load_module(name, fd, fn, info) | ||
|
||
|
@@ -300,19 +307,42 @@ def get_source(self, name): | |
except KeyError: | ||
raise ImportError(name) | ||
|
||
code = filename = newpath = None | ||
if info[2] == imp.PY_SOURCE: | ||
filename = fn | ||
with fd: | ||
code = fd.read() | ||
return fd.read() | ||
elif info[2] == imp.PKG_DIRECTORY: | ||
with open(self.get_filename(name), 'U') as f: | ||
return f.read() | ||
|
||
def is_package(self, name): | ||
try: | ||
(fd, fn, info), path = self._cache[name] | ||
except KeyError: | ||
return ImportError(name) | ||
return info[2] == imp.PKG_DIRECTORY | ||
|
||
def get_filename(self, name): | ||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, just |
||
(fd, fn, info), path = self._cache[name] | ||
except KeyError: | ||
return ImportError(name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I really was dumb. |
||
|
||
if info[2] == imp.PY_SOURCE: | ||
return fn | ||
elif info[2] == imp.PY_COMPILED: | ||
filename = fn[:-1] | ||
with open(filename, 'U') as f: | ||
code = f.read() | ||
return fn[:-1] | ||
elif info[2] == imp.PKG_DIRECTORY: | ||
filename = os.path.join(fn, '__init__.py') | ||
newpath = [fn] | ||
with open(filename, 'U') as f: | ||
code = f.read() | ||
return os.path.join(fn, '__init__.py') | ||
|
||
def get_code(self, name): | ||
source = self.get_source(name) | ||
filename = self.get_filename(name) | ||
|
||
if source: | ||
code = compile(source, filename) | ||
else: | ||
with open(filename, 'rb') as f: | ||
f.seek(8) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excuse my ignorance, but is this actually right? Do you have a link to documentation that says this is appropriate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the links, I agree with what you did, looks good to me! |
||
code = marshal.load(f) | ||
|
||
return code, filename, newpath | ||
return code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
raise
, notreturn
.