Skip to content

Commit b1651ea

Browse files
committed
Test _imp._set_lazy_attributes() at shutdown, add a comment and reword NEWS
1 parent a13e2d5 commit b1651ea

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎Lib/test/test_lazy_import/__init__.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,6 +2846,21 @@ def __del__(self, write=os.write, type=type, ns=ns):
28462846
""")
28472847
self.assertEqual(assert_python_ok("-c", code).out, b"module")
28482848

2849+
def test_set_lazy_attributes(self):
2850+
code = textwrap.dedent("""
2851+
import _imp, os, sys
2852+
2853+
class Canary:
2854+
def __del__(self, write=os.write,
2855+
set_lazy=_imp._set_lazy_attributes):
2856+
set_lazy(None, "mod")
2857+
write(1, b"ok")
2858+
2859+
# Freed while sys.lazy_modules is being cleared.
2860+
sys.lazy_modules.add(Canary())
2861+
""")
2862+
self.assertEqual(assert_python_ok("-c", code).out, b"ok")
2863+
28492864

28502865
if __name__ == '__main__':
28512866
unittest.main()
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Fix a segfault triggered by declaring or resolving a lazy import within a finalizer during shutdown.
1+
Fix a crash when a :ref:`lazy import <lazy-imports>` is declared or resolved
2+
by a finalizer running during interpreter shutdown.

‎Python/import.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4219,6 +4219,7 @@ register_lazy_on_parent(PyThreadState *tstate, PyObject *name, PyObject *source)
42194219
{
42204220
PyDictObject *pending =
42214221
(PyDictObject *)LAZY_PENDING_SUBMODULES(tstate->interp);
4222+
// Finalizers can still run after finalize_modules() cleared the dict.
42224223
if (pending == NULL) {
42234224
return 0;
42244225
}

0 commit comments

Comments
 (0)