Skip to content
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

Closed
wants to merge 10 commits into from
54 changes: 42 additions & 12 deletions attest/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import imp
import inspect
import marshal
import os
import sys

Expand Down Expand Up @@ -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)

Expand All @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be raise, not return.

return info[2] == imp.PKG_DIRECTORY

def get_filename(self, name):
try:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, just return from within each if/elif

(fd, fn, info), path = self._cache[name]
except KeyError:
return ImportError(name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise not return.

Copy link
Author

Choose a reason for hiding this comment

The 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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