[issue40050] importlib: module.__spec__ leaks importlib namespaces (test_importlib leaked xxx references)

Brett Cannon report at bugs.python.org
Tue Mar 24 13:09:11 EDT 2020


Brett Cannon <brett at python.org> added the comment:

>From Victor:
* Maybe the test_importlib should before save/restore the the "Python state" rather than modifying modules

You will have to be more specific than that as there is an import_state() context manager to control import state via the sys module.


* Maybe module.__spec__ should leak less importlib internals: explicitly clear namespaces? Use static methods? I'm not sure how to do that.

Are you saying change everything on __spec__ objects to be static methods? That won't work because the whole point of __spec__ objects is to essentially be data classes to store details of a module.

* Remove module.__spec__? ... that would mean rejecting PEP 451 implemented in Python 3.4, that sounds like a major regression :-(

No. You would break the world and undo years of work to clean up import semantics with no good way to go back to the old way.

Can I ask why you suddenly want to throw __spec__ objects out due to a leak tied back to _weakref switching to multi-phase initialization? Also note that the use of weakrefs by importlib isn't tied to __spec__ objects but to per-module import locks in importlib._bootstrap.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40050>
_______________________________________


More information about the Python-bugs-list mailing list