gh-135801: Add the module parameter to compile() etc (GH-139652)

Many functions related to compiling or parsing Python code, such as
compile(), ast.parse(), symtable.symtable(),
and importlib.abc.InspectLoader.source_to_code() now allow to pass
the module name used when filtering syntax warnings.
This commit is contained in:
Serhiy Storchaka 2025-11-13 13:21:32 +02:00 committed by GitHub
parent 63548b3699
commit d8e6bdc0d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
47 changed files with 390 additions and 115 deletions

View file

@ -108,7 +108,7 @@ def get_code(self, fullname):
source = self.get_source(fullname)
if source is None:
return None
return self.source_to_code(source)
return self.source_to_code(source, '<string>', fullname)
@abc.abstractmethod
def get_source(self, fullname):
@ -120,12 +120,12 @@ def get_source(self, fullname):
raise ImportError
@staticmethod
def source_to_code(data, path='<string>'):
def source_to_code(data, path='<string>', fullname=None):
"""Compile 'data' into a code object.
The 'data' argument can be anything that compile() can handle. The'path'
argument should be where the data was retrieved (when applicable)."""
return compile(data, path, 'exec', dont_inherit=True)
return compile(data, path, 'exec', dont_inherit=True, module=fullname)
exec_module = _bootstrap_external._LoaderBasics.exec_module
load_module = _bootstrap_external._LoaderBasics.load_module
@ -163,9 +163,8 @@ def get_code(self, fullname):
try:
path = self.get_filename(fullname)
except ImportError:
return self.source_to_code(source)
else:
return self.source_to_code(source, path)
path = '<string>'
return self.source_to_code(source, path, fullname)
_register(
ExecutionLoader,