Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions Lib/bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,9 @@ def trace_dispatch(self, frame, event, arg):
is entered.
return: A function or other code block is about to return.
exception: An exception has occurred.
c_call: A C function is about to be called.
c_return: A C function has returned.
c_exception: A C function has raised an exception.

For the Python events, specialized functions (see the dispatch_*()
methods) are called. For the C events, no action is taken.
For all the events, specialized functions (see the dispatch_*()
methods) are called.

The arg parameter depends on the previous event.
"""
Expand All @@ -290,12 +287,6 @@ def trace_dispatch(self, frame, event, arg):
return self.dispatch_return(frame, arg)
if event == 'exception':
return self.dispatch_exception(frame, arg)
if event == 'c_call':
return self.trace_dispatch
if event == 'c_exception':
return self.trace_dispatch
if event == 'c_return':
return self.trace_dispatch
if event == 'opcode':
return self.dispatch_opcode(frame, arg)
print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
Expand Down
4 changes: 2 additions & 2 deletions Lib/codeop.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ def _maybe_compile(compiler, source, filename, symbol, flags):
try:
compiler(source + "\n", filename, symbol, flags=flags)
return None
except _IncompleteInputError as e:
except _IncompleteInputError:
return None
except SyntaxError as e:
except SyntaxError:
pass
# fallthrough

Expand Down
15 changes: 12 additions & 3 deletions Lib/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ def _find_module(name, path=None):

file_path = spec.origin

if spec.loader.is_package(name):
# On namespace packages, spec.loader might be None, but
# spec.submodule_search_locations should always be set — check it instead.
if isinstance(spec.submodule_search_locations, importlib.machinery.NamespacePath):
return None, spec.submodule_search_locations, ("", "", _PKG_DIRECTORY)

if spec.loader.is_package(name): # non-namespace package
return None, os.path.dirname(file_path), ("", "", _PKG_DIRECTORY)

if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
Expand Down Expand Up @@ -334,7 +339,7 @@ def load_module(self, fqname, fp, pathname, file_info):
self.msgout(2, "load_module ->", m)
return m
if type == _PY_SOURCE:
co = compile(fp.read(), pathname, 'exec')
co = compile(fp.read(), pathname, 'exec', module=fqname)
elif type == _PY_COMPILED:
try:
data = fp.read()
Expand Down Expand Up @@ -400,7 +405,6 @@ def scan_opcodes(self, co):
yield "relative_import", (level, fromlist, name)

def scan_code(self, co, m):
code = co.co_code
scanner = self.scan_opcodes
for what, args in scanner(co):
if what == "store":
Expand Down Expand Up @@ -454,6 +458,11 @@ def load_package(self, fqname, pathname):
if newname:
fqname = newname
m = self.add_module(fqname)

if isinstance(pathname, importlib.machinery.NamespacePath):
m.__path__ = pathname
Comment on lines +462 to +463

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Snapshot namespace paths in ModuleFinder

When analyzing a nested namespace such as a.b, ModuleFinder.find_module() passes only the part name (b) into _find_module(), so retaining this object gives it an internal name of b rather than a.b. At the next PathFinder.invalidate_caches(), it recalculates from global sys.path; if an unrelated top-level namespace b is present, a.b.__path__ changes from .../a/b to .../b and import a.b.c is incorrectly reported missing. Snapshot the discovered locations, or otherwise preserve ModuleFinder's fully qualified parent context, before assigning __path__.

Useful? React with 👍 / 👎.

return m

m.__file__ = pathname
m.__path__ = [pathname]

Expand Down
Loading
Loading