Skip to content

Commit 5a8b4ce

Browse files
get interned strings from another dict with interned strings?
1 parent d9eaaf6 commit 5a8b4ce

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Objects/codeobject.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
#define INITIAL_SPECIALIZED_CODE_SIZE 16
2727

28+
// copypaste from unicodeobject.c
29+
#define INTERNED_STRINGS _PyRuntime.cached_objects.interned_strings
30+
2831
static const char *
2932
code_event_name(PyCodeEvent event) {
3033
switch (event) {
@@ -196,7 +199,6 @@ intern_strings(PyObject *tuple)
196199
return 0;
197200
}
198201

199-
200202
/* Intern constants. In the default build, this interns selected string
201203
constants. In the free-threaded build, this also interns non-string
202204
constants. */
@@ -205,17 +207,29 @@ intern_constants(PyObject *tuple, int *modified)
205207
{
206208
PyInterpreterState *interp = _PyInterpreterState_GET();
207209
PyObject *interned_dict = _Py_INTERP_CACHED_OBJECT(interp, interned_strings);
210+
// copypaste from unicodeobject.c
211+
#ifdef Py_GIL_DISABLED
212+
# define INTERN_MUTEX &_Py_INTERP_CACHED_OBJECT(interp, interned_mutex)
213+
#endif
214+
FT_MUTEX_LOCK(INTERN_MUTEX);
208215
Py_INCREF(interned_dict);
209216
for (Py_ssize_t i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
210217
PyObject *v = PyTuple_GET_ITEM(tuple, i);
211218
if (PyUnicode_CheckExact(v) && PyUnicode_GET_LENGTH(v) > 1) {
212219
if (PyUnicode_CHECK_INTERNED(v) != 0) {
213220
continue;
214221
}
222+
//
215223
PyObject *interned = PyDict_GetItemWithError(interned_dict, v);
216224
if (interned == NULL && PyErr_Occurred()) {
217225
goto error;
218226
}
227+
if (!interned) {
228+
interned = (PyObject *)_Py_hashtable_get(INTERNED_STRINGS, v);
229+
if (interned == NULL && PyErr_Occurred()) {
230+
goto error;
231+
}
232+
}
219233
if (interned != NULL && interned != v) {
220234
Py_INCREF(interned);
221235
PyTuple_SET_ITEM(tuple, i, interned);
@@ -317,10 +331,12 @@ intern_constants(PyObject *tuple, int *modified)
317331
}
318332
#endif
319333
}
334+
FT_MUTEX_UNLOCK(INTERN_MUTEX);
320335
Py_DECREF(interned_dict);
321336
return 0;
322337

323338
error:
339+
FT_MUTEX_UNLOCK(INTERN_MUTEX);
324340
Py_DECREF(interned_dict);
325341
return -1;
326342
}

0 commit comments

Comments
 (0)