Search code examples
pythonpython-3.xstring-interning

Is it possible to inspect the interned string pool directly?


Is it possible in python to inspect and look at the string pool of interned strings? I am familiar with the sys.intern() function, however is there a means to inspect the data there?


Solution

  • There is no mechanism for enumerating the interned string pool. See module Objects/unicodeobject.c and Python/sysmodule.c in the source code. Specifically, the definition of the var that holds interned strings: static PyObject *interned = NULL;.

    In theory that information could be exposed but it isn't because it has very limited usefulness. In part because most interned strings are "mortal". That is, they are removed from the interned string cache when no longer referenced. The value of exposing the set of cached strings is very close to zero which makes it difficult to justify the code to do so. Especially since doing so would require holding the GIL for extended periods or making a copy of the interned string dictionary so it can be iterated over without affecting other, concurrent, manipulations of that cache.