@@ -191,16 +191,6 @@ extern "C" {
191
191
# define OVERALLOCATE_FACTOR 4
192
192
#endif
193
193
194
- /* This dictionary holds all interned unicode strings. Note that references
195
- to strings in this dictionary are *not* counted in the string's ob_refcnt.
196
- When the interned string reaches a refcnt of 0 the string deallocation
197
- function will delete the reference from this dictionary.
198
-
199
- Another way to look at this is that to say that the actual reference
200
- count of a string is: s->ob_refcnt + (s->state ? 2 : 0)
201
- */
202
- static PyObject * interned = NULL ;
203
-
204
194
/* Forward declaration */
205
195
static inline int
206
196
_PyUnicodeWriter_WriteCharInline (_PyUnicodeWriter * writer , Py_UCS4 ch );
@@ -235,6 +225,23 @@ static inline PyObject* unicode_new_empty(void)
235
225
return empty ;
236
226
}
237
227
228
+ /* This dictionary holds all interned unicode strings. Note that references
229
+ to strings in this dictionary are *not* counted in the string's ob_refcnt.
230
+ When the interned string reaches a refcnt of 0 the string deallocation
231
+ function will delete the reference from this dictionary.
232
+ Another way to look at this is that to say that the actual reference
233
+ count of a string is: s->ob_refcnt + (s->state ? 2 : 0)
234
+ */
235
+ static inline PyObject * get_interned_dict (void )
236
+ {
237
+ return _PyRuntime .global_objects .interned ;
238
+ }
239
+
240
+ static inline void set_interned_dict (PyObject * dict )
241
+ {
242
+ _PyRuntime .global_objects .interned = dict ;
243
+ }
244
+
238
245
#define _Py_RETURN_UNICODE_EMPTY () \
239
246
do { \
240
247
return unicode_new_empty(); \
@@ -1523,7 +1530,7 @@ unicode_dealloc(PyObject *unicode)
1523
1530
_Py_FatalRefcountError ("deallocating an Unicode singleton" );
1524
1531
}
1525
1532
#endif
1526
-
1533
+ PyObject * interned = get_interned_dict ();
1527
1534
if (PyUnicode_CHECK_INTERNED (unicode )) {
1528
1535
/* Revive the dead object temporarily. PyDict_DelItem() removes two
1529
1536
references (key and value) which were ignored by
@@ -14657,12 +14664,14 @@ PyUnicode_InternInPlace(PyObject **p)
14657
14664
return ;
14658
14665
}
14659
14666
14667
+ PyObject * interned = get_interned_dict ();
14660
14668
if (interned == NULL ) {
14661
14669
interned = PyDict_New ();
14662
14670
if (interned == NULL ) {
14663
14671
PyErr_Clear (); /* Don't leave an exception */
14664
14672
return ;
14665
14673
}
14674
+ set_interned_dict (interned );
14666
14675
}
14667
14676
14668
14677
PyObject * t = PyDict_SetDefault (interned , s , s );
@@ -14713,6 +14722,7 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
14713
14722
return ;
14714
14723
}
14715
14724
14725
+ PyObject * interned = get_interned_dict ();
14716
14726
if (interned == NULL ) {
14717
14727
return ;
14718
14728
}
@@ -14748,7 +14758,8 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
14748
14758
#endif
14749
14759
14750
14760
PyDict_Clear (interned );
14751
- Py_CLEAR (interned );
14761
+ Py_DECREF (interned );
14762
+ set_interned_dict (NULL );
14752
14763
}
14753
14764
14754
14765
@@ -15155,7 +15166,7 @@ _PyUnicode_EnableLegacyWindowsFSEncoding(void)
15155
15166
static inline int
15156
15167
unicode_is_finalizing (void )
15157
15168
{
15158
- return (interned == NULL );
15169
+ return (get_interned_dict () == NULL );
15159
15170
}
15160
15171
#endif
15161
15172
@@ -15197,7 +15208,7 @@ _PyUnicode_Fini(PyInterpreterState *interp)
15197
15208
15198
15209
if (_Py_IsMainInterpreter (interp )) {
15199
15210
// _PyUnicode_ClearInterned() must be called before _PyUnicode_Fini()
15200
- assert (interned == NULL );
15211
+ assert (get_interned_dict () == NULL );
15201
15212
// bpo-47182: force a unicodedata CAPI capsule re-import on
15202
15213
// subsequent initialization of main interpreter.
15203
15214
ucnhash_capi = NULL ;
0 commit comments