intern'ed strings and deepcopy()

Tim Peters tim_one at
Sun Apr 13 20:54:27 CEST 2003

[Martin v. Löwis]
> ...
> To make my example more correct, I should write
> type(a) == type(b) == str and b == a 'implies'
>    id(intern(b+" s")) == id(intern(a+" s"))
> so that the strings being interned are temporaries themselves. For some
> reason, this still doesn't trigger the mortality of the interned string
> in 2.3a2, but I haven't investigated why.

obmalloc reuses the freed string space immediately (by design; it knows that
the most recently freed memory is most likely to still be in cache).

To prevent that, you need to trick it into giving the freed memory to
something else that persists.  This should print False in 2.3a2:

def save(s):
    global gunk
    gunk = s
    return 0

a = b = "ab"
print id(intern(a + " s")) == (
    save(a + "xy") or id(intern(b + " s")))

Less obscurely,

a = b = "ab"
print id(intern(a + " s"))
whatever = "cd" + " ef"
print id(intern(b + " s"))

prints two different numbers.

> In any case, there is no *guarantee* (anymore) that the id of an
> interned string stays the same over time; one should store the
> interned string itself, and not its id.


More information about the Python-list mailing list