[Python-Dev] Alternative implementation of interning

Martin v. Loewis martin@v.loewis.de
16 Aug 2002 21:17:19 +0200

Guido van Rossum <guido@python.org> writes:

> Maybe we can add even detect the abusing cases by putting a test in
> PyString_InternInPlace() like this:
> if (s->ob_refcnt == 1) {
>     PyErr_Warn(PyExc_DeprecationWarning,
>                "interning won't keep your string alive");
>     PyErr_Clear(); /* In case the warning was an error, ignore it */
>     Py_INCREF(s); /* Make s immortal */
> }

I believe this will trigger very often; the first usage of
InternFromString (for a certain string) will likely trigger it.

If people do

PyObject *__foo__;

int init(){
  __foo__ = PyString_InternFromString("__foo__");

then this is perfectly safe: you get an extra reference back (on top
of ones that the intern dictionary just stops holding); you can keep
this reference as long as you want.

So I would agree with your analysis that this will cause only few
problems. Unfortunately, those will be hard to track down.