cpython/Python
Eddie Elizondo 4590f72259
bpo-38076 Clear the interpreter state only after clearing module globals (GH-18039)
Currently, during runtime destruction, `_PyImport_Cleanup` is clearing the interpreter state before clearing out the modules themselves. This leads to a segfault on modules that rely on the module state to clear themselves up.

For example, let's take the small snippet added in the issue by @DinoV :
```
import _struct

class C:
    def __init__(self):
        self.pack = _struct.pack
    def __del__(self):
        self.pack('I', -42)

_struct.x = C()
```

The module `_struct` uses the module state to run `pack`. Therefore, the module state has to be alive until after the module has been cleared out to successfully run `C.__del__`. This happens at line 606, when `_PyImport_Cleanup` calls `_PyModule_Clear`. In fact, the loop that calls `_PyModule_Clear` has in its comments: 

> Now, if there are any modules left alive, clear their globals to minimize potential leaks.  All C extension modules actually end up here, since they are kept alive in the interpreter state.

That means that we can't clear the module state (which is used by C Extensions) before we run that loop.

Moving `_PyInterpreterState_ClearModules` until after it, fixes the segfault in the code snippet.

Finally, this updates a test in `io` to correctly assert the error that it now throws (since it now finds the io module state). The test that uses this is: `test_create_at_shutdown_without_encoding`. Given this test is now working is a proof that the module state now stays alive even when `__del__` is called at module destruction time. Thus, I didn't add a new tests for this.


https://bugs.python.org/issue38076
2020-02-04 02:29:25 -08:00
..
clinic bpo-39489: Remove COUNT_ALLOCS special build (GH-18259) 2020-02-03 15:17:15 +01:00
Python-ast.c closes bpo-38648: Remove double tp_free slot in Python-ast.c. (GH-17002) 2019-10-30 18:08:06 -07:00
README
_warnings.c bpo-39487: Merge duplicated _Py_IDENTIFIER identifiers in C code (GH-18254) 2020-01-30 15:20:25 -08:00
asdl.c
ast.c bpo-39235: Fix end location for genexp in call args (GH-17925) 2020-01-09 11:18:47 -08:00
ast_opt.c Fix constant folding optimization for positional only arguments (GH-17837) 2020-01-05 17:03:56 +00:00
ast_unparse.c
bltinmodule.c Update sum comment. (#18240) 2020-02-01 11:08:34 +00:00
bootstrap_hash.c
ceval.c bpo-39487: Merge duplicated _Py_IDENTIFIER identifiers in C code (GH-18254) 2020-01-30 15:20:25 -08:00
ceval_gil.h
codecs.c bpo-38631: Avoid Py_FatalError() in _PyCodecRegistry_Init() (GH-18217) 2020-01-27 23:23:12 +01:00
compile.c bpo-39320: Handle unpacking of **values in compiler (GH-18141) 2020-01-27 09:57:45 +00:00
condvar.h Typo fix: "throuhgh" should be "through". (GH-16704) 2019-10-10 20:43:13 -07:00
context.c bpo-38979: fix ContextVar "__class_getitem__" method (GH-17497) 2019-12-08 03:35:59 -08:00
dtoa.c
dup2.c
dynamic_annotations.c
dynload_aix.c bpo-37752: Delete redundant Py_CHARMASK in normalizestring() (GH-15095) 2019-09-10 17:04:08 +01:00
dynload_dl.c
dynload_hpux.c
dynload_shlib.c
dynload_stub.c
dynload_win.c bpo-36266: Add module name in ImportError when DLL not found on Windows (GH-15180) 2019-08-17 13:50:39 -07:00
errors.c bpo-39487: Merge duplicated _Py_IDENTIFIER identifiers in C code (GH-18254) 2020-01-30 15:20:25 -08:00
fileutils.c bpo-38353: Add subfunctions to getpath.c (GH-16572) 2019-10-04 02:22:39 +02:00
formatter_unicode.c Fix typo in formatter_unicode (GH-16831) 2019-10-25 09:44:02 -07:00
frozen.c
frozenmain.c bpo-38304: PyConfig_InitPythonConfig() cannot fail anymore (GH-16509) 2019-10-01 12:06:16 +02:00
future.c
getargs.c bpo-39028: Performance enhancement in keyword extraction (GH-17576) 2019-12-18 15:51:22 +09:00
getcompiler.c
getcopyright.c Bring Python into the next decade. (GH-17801) 2020-01-02 18:56:34 -08:00
getopt.c Delete stale comment in Python/getopt.c. (GH-14719) 2019-08-19 18:35:13 -07:00
getplatform.c
getversion.c
graminit.c bpo-11410: Standardize and use symbol visibility attributes across POSIX and Windows. (GH-16347) 2019-10-15 08:26:12 +01:00
hamt.c closes bpo-37446: resolve undefined behavior in Python/hamt.c (GH-17727) 2019-12-30 18:31:52 -08:00
import.c bpo-38076 Clear the interpreter state only after clearing module globals (GH-18039) 2020-02-04 02:29:25 -08:00
importdl.c
importdl.h
importlib.h bpo-39320: Handle unpacking of **values in compiler (GH-18141) 2020-01-27 09:57:45 +00:00
importlib_external.h bpo-39320: Handle unpacking of **values in compiler (GH-18141) 2020-01-27 09:57:45 +00:00
importlib_zipimport.h bpo-39156: Break up COMPARE_OP into four logically distinct opcodes. (GH-17754) 2020-01-14 10:12:45 +00:00
initconfig.c bpo-39489: Remove COUNT_ALLOCS special build (GH-18259) 2020-02-03 15:17:15 +01:00
makeopcodetargets.py
marshal.c bpo-38823: Fix refleak in marshal init error path (GH-17260) 2019-11-20 01:59:32 +01:00
modsupport.c
mysnprintf.c
mystrtoul.c bpo-37752: Delete redundant Py_CHARMASK in normalizestring() (GH-15095) 2019-09-10 17:04:08 +01:00
opcode_targets.h bpo-39320: Handle unpacking of **values in compiler (GH-18141) 2020-01-27 09:57:45 +00:00
pathconfig.c bpo-39270: Remove dead assignment from config_init_module_search_paths (GH-17914) 2020-01-09 10:14:11 +01:00
peephole.c bpo-39156: Break up COMPARE_OP into four logically distinct opcodes. (GH-17754) 2020-01-14 10:12:45 +00:00
preconfig.c bpo-38304: Remove PyConfig.struct_size (GH-16500) (GH-16508) 2019-10-01 10:56:37 +02:00
pyarena.c
pyctype.c
pyfpe.c
pyhash.c
pylifecycle.c bpo-39542: Move object.h debug functions to internal C API (GH-18331) 2020-02-03 17:28:26 +01:00
pymath.c bpo-31031: Unify duplicate bits_in_digit and bit_length (GH-2866) 2020-01-16 15:09:19 +01:00
pystate.c bpo-39511: PyThreadState_Clear() calls on_delete (GH-18296) 2020-02-01 02:30:25 +01:00
pystrcmp.c
pystrhex.c Fix unused variable and signed/unsigned warnings (GH-15537) 2019-08-27 09:55:13 -07:00
pystrtod.c bpo-38835: Don't use PyFPE_START_PROTECT and PyFPE_END_PROTECT (GH-17231) 2019-11-20 02:51:30 +01:00
pythonrun.c bpo-39542: Move object.h debug functions to internal C API (GH-18331) 2020-02-03 17:28:26 +01:00
pytime.c bpo-38068: Clean up gettimeofday configure logic. (GH-15775) 2019-09-10 03:37:59 -07:00
strdup.c
structmember.c
symtable.c Fix SystemError when nested function has annotation on positional-only argument (GH-17826) 2020-01-05 01:57:21 +00:00
sysmodule.c bpo-39489: Remove COUNT_ALLOCS special build (GH-18259) 2020-02-03 15:17:15 +01:00
thread.c bpo-38096: Clean up the "struct sequence" / "named tuple" docs (GH-15895) 2019-09-11 15:17:32 +01:00
thread_nt.h
thread_pthread.h bpo-38852: Set thread stack size to 8 Mb for debug builds on android platforms (GH-17337) 2019-12-08 08:40:14 +01:00
traceback.c bpo-38070: _Py_DumpTraceback() writes <no Python frame> (GH-16244) 2019-09-17 23:36:16 +02:00
wordcode_helpers.h

README

Miscellaneous source files for the main Python shared library