Issue #18072: Implement get_code() for importlib.abc.InspectLoader and

ExecutionLoader.
This commit is contained in:
Brett Cannon 2013-05-27 21:11:04 -04:00
parent acfa291af9
commit 3b62ca88e4
3 changed files with 121 additions and 17 deletions

View file

@ -147,14 +147,18 @@ class InspectLoader(Loader):
"""
raise ImportError
@abc.abstractmethod
def get_code(self, fullname):
"""Abstract method which when implemented should return the code object
for the module. The fullname is a str. Returns a types.CodeType.
"""Method which returns the code object for the module.
Raises ImportError if the module cannot be found.
The fullname is a str. Returns a types.CodeType if possible, else
returns None if a code object does not make sense
(e.g. built-in module). Raises ImportError if the module cannot be
found.
"""
raise ImportError
source = self.get_source(fullname)
if source is None:
return None
return self.source_to_code(source)
@abc.abstractmethod
def get_source(self, fullname):
@ -194,6 +198,22 @@ class ExecutionLoader(InspectLoader):
"""
raise ImportError
def get_code(self, fullname):
"""Method to return the code object for fullname.
Should return None if not applicable (e.g. built-in module).
Raise ImportError if the module cannot be found.
"""
source = self.get_source(fullname)
if source is None:
return None
try:
path = self.get_filename(fullname)
except ImportError:
return self.source_to_code(source)
else:
return self.source_to_code(source, path)
class FileLoader(_bootstrap.FileLoader, ResourceLoader, ExecutionLoader):