[Python-Dev] Question about the current implementation of str (original) (raw)

Victor Stinner victor.stinner at gmail.com
Sat Apr 9 05:09:37 EDT 2016


2016-04-09 9:52 GMT+02:00 Victor Stinner <victor.stinner at gmail.com>:

But the hash is used as an heuristic to decide if a string is "immutable" or not, the refcount is also used by the heuristic. If the string is immutable, an operation like resize must create a new string.

I'm talking about this private function:

static int unicode_modifiable(PyObject unicode) { assert(_PyUnicode_CHECK(unicode)); if (Py_REFCNT(unicode) != 1) return 0; if (_PyUnicode_HASH(unicode) != -1) return 0; if (PyUnicode_CHECK_INTERNED(unicode)) return 0; if (!PyUnicode_CheckExact(unicode)) return 0; #ifdef Py_DEBUG / singleton refcount is greater than 1 */ assert(!unicode_is_singleton(unicode)); #endif return 1; }

Victor



More information about the Python-Dev mailing list