summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorJeremy Kloth <jeremy.kloth@gmail.com>2022-03-22 12:53:51 (GMT)
committerGitHub <noreply@github.com>2022-03-22 12:53:51 (GMT)
commit88872a29f19092d2fde27365af230abd6d301941 (patch)
treec3b3f338395d4f3015cf1e9c37689a30019126b1 /Objects
parent7d810b6a4eab6eba689acc5bb05f85515478d690 (diff)
downloadcpython-88872a29f19092d2fde27365af230abd6d301941.zip
cpython-88872a29f19092d2fde27365af230abd6d301941.tar.gz
cpython-88872a29f19092d2fde27365af230abd6d301941.tar.bz2
bpo-47084: Clear Unicode cached representations on finalization (GH-32032)
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 5dfe6e1..ce3ebce 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -16057,6 +16057,35 @@ _PyUnicode_FiniTypes(PyInterpreterState *interp)
}
+static void unicode_static_dealloc(PyObject *op)
+{
+ PyASCIIObject* ascii = (PyASCIIObject*)op;
+
+ assert(ascii->state.compact);
+
+ if (ascii->state.ascii) {
+ if (ascii->wstr) {
+ PyObject_Free(ascii->wstr);
+ ascii->wstr = NULL;
+ }
+ }
+ else {
+ PyCompactUnicodeObject* compact = (PyCompactUnicodeObject*)op;
+ void* data = (void*)(compact + 1);
+ if (ascii->wstr && ascii->wstr != data) {
+ PyObject_Free(ascii->wstr);
+ ascii->wstr = NULL;
+ compact->wstr_length = 0;
+ }
+ if (compact->utf8) {
+ PyObject_Free(compact->utf8);
+ compact->utf8 = NULL;
+ compact->utf8_length = 0;
+ }
+ }
+}
+
+
void
_PyUnicode_Fini(PyInterpreterState *interp)
{
@@ -16070,6 +16099,21 @@ _PyUnicode_Fini(PyInterpreterState *interp)
_PyUnicode_FiniEncodings(&state->fs_codec);
unicode_clear_identifiers(state);
+
+ // Clear the single character singletons
+ for (int i = 0; i < 128; i++) {
+ unicode_static_dealloc((PyObject*)&_Py_SINGLETON(strings).ascii[i]);
+ }
+ for (int i = 0; i < 128; i++) {
+ unicode_static_dealloc((PyObject*)&_Py_SINGLETON(strings).latin1[i]);
+ }
+}
+
+
+void
+_PyStaticUnicode_Dealloc(PyObject *op)
+{
+ unicode_static_dealloc(op);
}