// Lazily build the dictionary to hold interned Strings if (interned == NULL) { interned = PyDict_New(); if (interned == NULL) { PyErr_Clear(); return; } }
PyObject *t;
// Make an entry to the interned dictionary for the // given object t = PyDict_SetDefault(interned, s, s);
.........
// The two references in interned dict (key and value) are // not counted by refcnt. // unicode_dealloc() and _PyUnicode_ClearInterned() take // care of this. Py_SET_REFCNT(s, Py_REFCNT(s) - 2);
// Set the state of the string to be INTERNED _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL; }
// Get all the keys to the interned dictionary PyObject *keys = PyDict_Keys(interned);
.........
// Interned Unicode strings are not forcibly deallocated; // rather, we give them their stolen references back // and then clear and DECREF the interned dict.
for (Py_ssize_t i = 0; i PyObject *s = PyList_GET_ITEM(keys, i);
.........
switch (PyUnicode_CHECK_INTERNED(s)) { case SSTATE_INTERNED_IMMORTAL: Py_SET_REFCNT(s, Py_REFCNT(s) + 1); break; case SSTATE_INTERNED_MORTAL: // Restore the two references (key and value) ignored // by PyUnicode_InternInPlace(). Py_SET_REFCNT(s, Py_REFCNT(s) + 2); break; case SSTATE_NOT_INTERNED: /* fall through */ default: Py_UNREACHABLE(); }
// marking the string to be NOT_INTERNED _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED; }
// decreasing the reference to the initialized and // access keys object. Py_DECREF(keys);
// clearing the dictionary PyDict_Clear(interned);
// clearing the object interned Py_CLEAR(interned); }