mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Issue #18072: Implement get_code() for importlib.abc.InspectLoader and
ExecutionLoader.
This commit is contained in:
parent
acfa291af9
commit
3b62ca88e4
3 changed files with 121 additions and 17 deletions
|
@ -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):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue