@@ -49,9 +49,13 @@ class fs_unicode_converter(CConverter):
4949void
5050_PyImport_Init (void )
5151{
52+ PyInterpreterState * interp = PyThreadState_Get ()-> interp ;
5253 initstr = PyUnicode_InternFromString ("__init__" );
5354 if (initstr == NULL )
5455 Py_FatalError ("Can't initialize import variables" );
56+ interp -> builtins_copy = PyDict_Copy (interp -> builtins );
57+ if (interp -> builtins_copy == NULL )
58+ Py_FatalError ("Can't backup builtins dict" );
5559}
5660
5761void
@@ -397,8 +401,10 @@ PyImport_Cleanup(void)
397401 PyObject * key , * value , * dict ;
398402 PyInterpreterState * interp = PyThreadState_GET ()-> interp ;
399403 PyObject * modules = interp -> modules ;
400- PyObject * builtins = interp -> builtins ;
404+ PyObject * builtins_mod = NULL ;
405+ PyObject * sys_mod = NULL ;
401406 PyObject * weaklist = NULL ;
407+ char * * p ;
402408
403409 if (modules == NULL )
404410 return ; /* Already done */
@@ -411,31 +417,22 @@ PyImport_Cleanup(void)
411417
412418 /* XXX Perhaps these precautions are obsolete. Who knows? */
413419
414- value = PyDict_GetItemString (modules , "builtins" );
415- if (value != NULL && PyModule_Check (value )) {
416- dict = PyModule_GetDict (value );
420+ if (Py_VerboseFlag )
421+ PySys_WriteStderr ("# clear builtins._\n" );
422+ PyDict_SetItemString (interp -> builtins , "_" , Py_None );
423+
424+ for (p = sys_deletes ; * p != NULL ; p ++ ) {
417425 if (Py_VerboseFlag )
418- PySys_WriteStderr ("# clear builtins._\n" );
419- PyDict_SetItemString (dict , "_" , Py_None );
420- }
421- value = PyDict_GetItemString (modules , "sys" );
422- if (value != NULL && PyModule_Check (value )) {
423- char * * p ;
424- PyObject * v ;
425- dict = PyModule_GetDict (value );
426- for (p = sys_deletes ; * p != NULL ; p ++ ) {
427- if (Py_VerboseFlag )
428- PySys_WriteStderr ("# clear sys.%s\n" , * p );
429- PyDict_SetItemString (dict , * p , Py_None );
430- }
431- for (p = sys_files ; * p != NULL ; p += 2 ) {
432- if (Py_VerboseFlag )
433- PySys_WriteStderr ("# restore sys.%s\n" , * p );
434- v = PyDict_GetItemString (dict , * (p + 1 ));
435- if (v == NULL )
436- v = Py_None ;
437- PyDict_SetItemString (dict , * p , v );
438- }
426+ PySys_WriteStderr ("# clear sys.%s\n" , * p );
427+ PyDict_SetItemString (interp -> sysdict , * p , Py_None );
428+ }
429+ for (p = sys_files ; * p != NULL ; p += 2 ) {
430+ if (Py_VerboseFlag )
431+ PySys_WriteStderr ("# restore sys.%s\n" , * p );
432+ value = PyDict_GetItemString (interp -> sysdict , * (p + 1 ));
433+ if (value == NULL )
434+ value = Py_None ;
435+ PyDict_SetItemString (interp -> sysdict , * p , value );
439436 }
440437
441438 /* We prepare a list which will receive (name, weakref) tuples of
@@ -473,11 +470,15 @@ PyImport_Cleanup(void)
473470
474471 /* Clear the modules dict. */
475472 PyDict_Clear (modules );
476- /* Replace the interpreter's reference to builtins with an empty dict
477- (module globals still have a reference to the original builtins). */
478- builtins = interp -> builtins ;
479- interp -> builtins = PyDict_New ();
480- Py_DECREF (builtins );
473+ /* Restore the original builtins dict, to ensure that any
474+ user data gets cleared. */
475+ dict = PyDict_Copy (interp -> builtins );
476+ if (dict == NULL )
477+ PyErr_Clear ();
478+ PyDict_Clear (interp -> builtins );
479+ if (PyDict_Update (interp -> builtins , interp -> builtins_copy ))
480+ PyErr_Clear ();
481+ Py_XDECREF (dict );
481482 /* Clear module dict copies stored in the interpreter state */
482483 _PyState_ClearModules ();
483484 /* Collect references */
@@ -488,7 +489,15 @@ PyImport_Cleanup(void)
488489
489490 /* Now, if there are any modules left alive, clear their globals to
490491 minimize potential leaks. All C extension modules actually end
491- up here, since they are kept alive in the interpreter state. */
492+ up here, since they are kept alive in the interpreter state.
493+
494+ The special treatment of "builtins" here is because even
495+ when it's not referenced as a module, its dictionary is
496+ referenced by almost every module's __builtins__. Since
497+ deleting a module clears its dictionary (even if there are
498+ references left to it), we need to delete the "builtins"
499+ module last. Likewise, we don't delete sys until the very
500+ end because it is implicitly referenced (e.g. by print). */
492501 if (weaklist != NULL ) {
493502 Py_ssize_t i , n ;
494503 n = PyList_GET_SIZE (weaklist );
@@ -498,17 +507,27 @@ PyImport_Cleanup(void)
498507 PyObject * mod = PyWeakref_GET_OBJECT (PyTuple_GET_ITEM (tup , 1 ));
499508 if (mod == Py_None )
500509 continue ;
501- Py_INCREF (mod );
502510 assert (PyModule_Check (mod ));
511+ dict = PyModule_GetDict (mod );
512+ if (dict == interp -> builtins || dict == interp -> sysdict )
513+ continue ;
514+ Py_INCREF (mod );
503515 if (Py_VerboseFlag && PyUnicode_Check (name ))
504- PySys_FormatStderr ("# cleanup[3] wiping %U\n" ,
505- name , mod );
516+ PySys_FormatStderr ("# cleanup[3] wiping %U\n" , name );
506517 _PyModule_Clear (mod );
507518 Py_DECREF (mod );
508519 }
509520 Py_DECREF (weaklist );
510521 }
511522
523+ /* Next, delete sys and builtins (in that order) */
524+ if (Py_VerboseFlag )
525+ PySys_FormatStderr ("# cleanup[3] wiping sys\n" );
526+ _PyModule_ClearDict (interp -> sysdict );
527+ if (Py_VerboseFlag )
528+ PySys_FormatStderr ("# cleanup[3] wiping builtins\n" );
529+ _PyModule_ClearDict (interp -> builtins );
530+
512531 /* Clear and delete the modules directory. Actual modules will
513532 still be there only if imported during the execution of some
514533 destructor. */
0 commit comments