[3.13] gh-113993: For string interning, do not rely on (or assert) _P… · python/cpython@281ffb6

GitHub

@@ -247,7 +247,8 @@ _PyUnicode_InternedSize_Immortal(void)

247247// value, to help detect bugs in optimizations.

248248249249while (PyDict_Next(dict, &pos, &key, &value)) {

250-if (_Py_IsImmortal(key)) {

250+assert(PyUnicode_CHECK_INTERNED(key) !=SSTATE_INTERNED_IMMORTAL_STATIC);

251+if (PyUnicode_CHECK_INTERNED(key) ==SSTATE_INTERNED_IMMORTAL) {

251252count++;

252253 }

253254 }

@@ -683,10 +684,14 @@ _PyUnicode_CheckConsistency(PyObject *op, int check_content)

683684684685/* Check interning state */

685686#ifdefPy_DEBUG

687+// Note that we do not check `_Py_IsImmortal(op)`, since stable ABI

688+// extensions can make immortal strings mortal (but with a high enough

689+// refcount).

690+// The other way is extremely unlikely (worth a potential failed assertion

691+// in a debug build), so we do check `!_Py_IsImmortal(op)`.

686692switch (PyUnicode_CHECK_INTERNED(op)) {

687693caseSSTATE_NOT_INTERNED:

688694if (ascii->state.statically_allocated) {

689-CHECK(_Py_IsImmortal(op));

690695// This state is for two exceptions:

691696// - strings are currently checked before they're interned

692697// - the 256 one-latin1-character strings

@@ -702,11 +707,9 @@ _PyUnicode_CheckConsistency(PyObject *op, int check_content)

702707break;

703708caseSSTATE_INTERNED_IMMORTAL:

704709CHECK(!ascii->state.statically_allocated);

705-CHECK(_Py_IsImmortal(op));

706710break;

707711caseSSTATE_INTERNED_IMMORTAL_STATIC:

708712CHECK(ascii->state.statically_allocated);

709-CHECK(_Py_IsImmortal(op));

710713break;

711714default:

712715Py_UNREACHABLE();

@@ -1899,7 +1902,6 @@ static PyObject*

18991902get_latin1_char(Py_UCS1ch)

19001903{

19011904PyObject*o=LATIN1(ch);

1902-assert(_Py_IsImmortal(o));

19031905returno;

19041906}

19051907@@ -15049,7 +15051,6 @@ intern_static(PyInterpreterState *interp, PyObject *s /* stolen */)

1504915051assert(s!=NULL);

1505015052assert(_PyUnicode_CHECK(s));

1505115053assert(_PyUnicode_STATE(s).statically_allocated);

15052-assert(_Py_IsImmortal(s));

15053150541505415055switch (PyUnicode_CHECK_INTERNED(s)) {

1505515056caseSSTATE_NOT_INTERNED:

@@ -15190,7 +15191,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,

1519015191 {

1519115192PyObject*r= (PyObject*)_Py_hashtable_get(INTERNED_STRINGS, s);

1519215193if (r!=NULL) {

15193-assert(_Py_IsImmortal(r));

15194+assert(_PyUnicode_STATE(r).statically_allocated);

1519415195assert(r!=s); // r must be statically_allocated; s is not

1519515196Py_DECREF(s);

1519615197returnPy_NewRef(r);