Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
gh-117182: Allow modules to modify their own __class__ when lazily lo…
…aded
  • Loading branch information
effigies committed Mar 29, 2024
commit a5e96703d96d78e93df67c748edcf05904bc23bc
12 changes: 8 additions & 4 deletions Lib/importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,17 @@ def __getattribute__(self, attr):
# Only the first thread to get the lock should trigger the load
# and reset the module's class. The rest can now getattr().
if object.__getattribute__(self, '__class__') is _LazyModule:
__class__ = loader_state['__class__']

# Reentrant calls from the same thread must be allowed to proceed without
# triggering the load again.
# exec_module() and self-referential imports are the primary ways this can
# happen, but in any case we must return something to avoid deadlock.
if loader_state['is_loading']:
return object.__getattribute__(self, attr)
return __class__.__getattribute__(self, attr)
loader_state['is_loading'] = True

__dict__ = object.__getattribute__(self, '__dict__')
__dict__ = __class__.__getattribute__(self, '__dict__')

# All module metadata must be gathered from __spec__ in order to avoid
# using mutated values.
Expand Down Expand Up @@ -216,8 +218,10 @@ def __getattribute__(self, attr):
# Update after loading since that's what would happen in an eager
# loading situation.
__dict__.update(attrs_updated)
# Finally, stop triggering this method.
self.__class__ = types.ModuleType
# Finally, stop triggering this method, if the module did not
# already update its own __class__.
if isinstance(self, _LazyModule):
object.__setattr__(self, '__class__', __class__)

return getattr(self, attr)

Expand Down