Skip to content

Commit 3d48334

Browse files
authored
bpo-38858: Call _PyUnicode_Fini() in Py_EndInterpreter() (GH-17330)
Py_EndInterpreter() now clears the filesystem codec.
1 parent 310e2d2 commit 3d48334

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

‎Include/internal/pycore_pylifecycle.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ extern void _PyImport_Fini2(void);
7777
extern void _PyGC_Fini(PyThreadState *tstate);
7878
extern void _PyType_Fini(void);
7979
extern void _Py_HashRandomization_Fini(void);
80-
extern void _PyUnicode_Fini(void);
80+
extern void _PyUnicode_Fini(PyThreadState *tstate);
8181
extern void _PyLong_Fini(void);
8282
extern void _PyFaulthandler_Fini(void);
8383
extern void _PyHash_Fini(void);

‎Objects/unicodeobject.c‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15929,34 +15929,37 @@ _PyUnicode_EnableLegacyWindowsFSEncoding(void)
1592915929

1593015930

1593115931
void
15932-
_PyUnicode_Fini(void)
15932+
_PyUnicode_Fini(PyThreadState *tstate)
1593315933
{
15934+
if (_Py_IsMainInterpreter(tstate)) {
1593415935
#if defined(WITH_VALGRIND) || defined(__INSURE__)
15935-
/* Insure++ is a memory analysis tool that aids in discovering
15936-
* memory leaks and other memory problems. On Python exit, the
15937-
* interned string dictionaries are flagged as being in use at exit
15938-
* (which it is). Under normal circumstances, this is fine because
15939-
* the memory will be automatically reclaimed by the system. Under
15940-
* memory debugging, it's a huge source of useless noise, so we
15941-
* trade off slower shutdown for less distraction in the memory
15942-
* reports. -baw
15943-
*/
15944-
unicode_release_interned();
15936+
/* Insure++ is a memory analysis tool that aids in discovering
15937+
* memory leaks and other memory problems. On Python exit, the
15938+
* interned string dictionaries are flagged as being in use at exit
15939+
* (which it is). Under normal circumstances, this is fine because
15940+
* the memory will be automatically reclaimed by the system. Under
15941+
* memory debugging, it's a huge source of useless noise, so we
15942+
* trade off slower shutdown for less distraction in the memory
15943+
* reports. -baw
15944+
*/
15945+
unicode_release_interned();
1594515946
#endif /* __INSURE__ */
1594615947

15947-
Py_CLEAR(unicode_empty);
15948+
Py_CLEAR(unicode_empty);
1594815949

15949-
for (Py_ssize_t i = 0; i < 256; i++) {
15950-
Py_CLEAR(unicode_latin1[i]);
15950+
for (Py_ssize_t i = 0; i < 256; i++) {
15951+
Py_CLEAR(unicode_latin1[i]);
15952+
}
15953+
_PyUnicode_ClearStaticStrings();
15954+
(void)PyUnicode_ClearFreeList();
1595115955
}
15952-
_PyUnicode_ClearStaticStrings();
15953-
(void)PyUnicode_ClearFreeList();
1595415956

1595515957
PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
1595615958
PyMem_RawFree(interp->fs_codec.encoding);
1595715959
interp->fs_codec.encoding = NULL;
1595815960
PyMem_RawFree(interp->fs_codec.errors);
1595915961
interp->fs_codec.errors = NULL;
15962+
interp->config.filesystem_errors = _Py_ERROR_UNKNOWN;
1596015963
}
1596115964

1596215965

‎Python/pylifecycle.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,9 +1182,6 @@ finalize_interp_types(PyThreadState *tstate, int is_main_interp)
11821182
_PySet_Fini();
11831183
_PyBytes_Fini();
11841184
_PyLong_Fini();
1185-
}
1186-
1187-
if (is_main_interp) {
11881185
_PyFloat_Fini();
11891186
_PyDict_Fini();
11901187
_PySlice_Fini();
@@ -1197,9 +1194,12 @@ finalize_interp_types(PyThreadState *tstate, int is_main_interp)
11971194
_PyArg_Fini();
11981195
_PyAsyncGen_Fini();
11991196
_PyContext_Fini();
1197+
}
1198+
1199+
/* Cleanup Unicode implementation */
1200+
_PyUnicode_Fini(tstate);
12001201

1201-
/* Cleanup Unicode implementation */
1202-
_PyUnicode_Fini();
1202+
if (is_main_interp) {
12031203
_Py_ClearFileSystemEncoding();
12041204
}
12051205
}

0 commit comments

Comments
 (0)