@@ -214,22 +214,6 @@ extern "C" {
214
214
# define OVERALLOCATE_FACTOR 4
215
215
#endif
216
216
217
- /* bpo-40521: Interned strings are shared by all interpreters. */
218
- #ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
219
- # define INTERNED_STRINGS
220
- #endif
221
-
222
- /* This dictionary holds all interned unicode strings. Note that references
223
- to strings in this dictionary are *not* counted in the string's ob_refcnt.
224
- When the interned string reaches a refcnt of 0 the string deallocation
225
- function will delete the reference from this dictionary.
226
-
227
- Another way to look at this is that to say that the actual reference
228
- count of a string is: s->ob_refcnt + (s->state ? 2 : 0)
229
- */
230
- #ifdef INTERNED_STRINGS
231
- static PyObject * interned = NULL ;
232
- #endif
233
217
234
218
/* Forward declaration */
235
219
static inline int
@@ -1966,21 +1950,20 @@ unicode_dealloc(PyObject *unicode)
1966
1950
1967
1951
case SSTATE_INTERNED_MORTAL :
1968
1952
{
1969
- #ifdef INTERNED_STRINGS
1953
+ struct _Py_unicode_state * state = get_unicode_state ();
1970
1954
/* Revive the dead object temporarily. PyDict_DelItem() removes two
1971
1955
references (key and value) which were ignored by
1972
1956
PyUnicode_InternInPlace(). Use refcnt=3 rather than refcnt=2
1973
1957
to prevent calling unicode_dealloc() again. Adjust refcnt after
1974
1958
PyDict_DelItem(). */
1975
1959
assert (Py_REFCNT (unicode ) == 0 );
1976
1960
Py_SET_REFCNT (unicode , 3 );
1977
- if (PyDict_DelItem (interned , unicode ) != 0 ) {
1961
+ if (PyDict_DelItem (state -> interned , unicode ) != 0 ) {
1978
1962
_PyErr_WriteUnraisableMsg ("deletion of interned string failed" ,
1979
1963
NULL );
1980
1964
}
1981
1965
assert (Py_REFCNT (unicode ) == 1 );
1982
1966
Py_SET_REFCNT (unicode , 0 );
1983
- #endif
1984
1967
break ;
1985
1968
}
1986
1969
@@ -11359,13 +11342,11 @@ _PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right)
11359
11342
if (PyUnicode_CHECK_INTERNED (left ))
11360
11343
return 0 ;
11361
11344
11362
- #ifdef INTERNED_STRINGS
11363
11345
assert (_PyUnicode_HASH (right_uni ) != -1 );
11364
11346
Py_hash_t hash = _PyUnicode_HASH (left );
11365
11347
if (hash != -1 && hash != _PyUnicode_HASH (right_uni )) {
11366
11348
return 0 ;
11367
11349
}
11368
- #endif
11369
11350
11370
11351
return unicode_compare_eq (left , right_uni );
11371
11352
}
@@ -15610,21 +15591,21 @@ PyUnicode_InternInPlace(PyObject **p)
15610
15591
return ;
15611
15592
}
15612
15593
15613
- #ifdef INTERNED_STRINGS
15614
15594
if (PyUnicode_READY (s ) == -1 ) {
15615
15595
PyErr_Clear ();
15616
15596
return ;
15617
15597
}
15618
15598
15619
- if (interned == NULL ) {
15620
- interned = PyDict_New ();
15621
- if (interned == NULL ) {
15599
+ struct _Py_unicode_state * state = get_unicode_state ();
15600
+ if (state -> interned == NULL ) {
15601
+ state -> interned = PyDict_New ();
15602
+ if (state -> interned == NULL ) {
15622
15603
PyErr_Clear (); /* Don't leave an exception */
15623
15604
return ;
15624
15605
}
15625
15606
}
15626
15607
15627
- PyObject * t = PyDict_SetDefault (interned , s , s );
15608
+ PyObject * t = PyDict_SetDefault (state -> interned , s , s );
15628
15609
if (t == NULL ) {
15629
15610
PyErr_Clear ();
15630
15611
return ;
@@ -15641,13 +15622,9 @@ PyUnicode_InternInPlace(PyObject **p)
15641
15622
this. */
15642
15623
Py_SET_REFCNT (s , Py_REFCNT (s ) - 2 );
15643
15624
_PyUnicode_STATE (s ).interned = SSTATE_INTERNED_MORTAL ;
15644
- #else
15645
- // PyDict expects that interned strings have their hash
15646
- // (PyASCIIObject.hash) already computed.
15647
- (void )unicode_hash (s );
15648
- #endif
15649
15625
}
15650
15626
15627
+
15651
15628
void
15652
15629
PyUnicode_InternImmortal (PyObject * * p )
15653
15630
{
@@ -15681,29 +15658,25 @@ PyUnicode_InternFromString(const char *cp)
15681
15658
void
15682
15659
_PyUnicode_ClearInterned (PyInterpreterState * interp )
15683
15660
{
15684
- if (!_Py_IsMainInterpreter (interp )) {
15685
- // interned dict is shared by all interpreters
15686
- return ;
15687
- }
15688
-
15689
- if (interned == NULL ) {
15661
+ struct _Py_unicode_state * state = & interp -> unicode ;
15662
+ if (state -> interned == NULL ) {
15690
15663
return ;
15691
15664
}
15692
- assert (PyDict_CheckExact (interned ));
15665
+ assert (PyDict_CheckExact (state -> interned ));
15693
15666
15694
15667
/* Interned unicode strings are not forcibly deallocated; rather, we give
15695
15668
them their stolen references back, and then clear and DECREF the
15696
15669
interned dict. */
15697
15670
15698
15671
#ifdef INTERNED_STATS
15699
15672
fprintf (stderr , "releasing %zd interned strings\n" ,
15700
- PyDict_GET_SIZE (interned ));
15673
+ PyDict_GET_SIZE (state -> interned ));
15701
15674
15702
15675
Py_ssize_t immortal_size = 0 , mortal_size = 0 ;
15703
15676
#endif
15704
15677
Py_ssize_t pos = 0 ;
15705
15678
PyObject * s , * ignored_value ;
15706
- while (PyDict_Next (interned , & pos , & s , & ignored_value )) {
15679
+ while (PyDict_Next (state -> interned , & pos , & s , & ignored_value )) {
15707
15680
assert (PyUnicode_IS_READY (s ));
15708
15681
15709
15682
switch (PyUnicode_CHECK_INTERNED (s )) {
@@ -15734,8 +15707,8 @@ _PyUnicode_ClearInterned(PyInterpreterState *interp)
15734
15707
mortal_size , immortal_size );
15735
15708
#endif
15736
15709
15737
- PyDict_Clear (interned );
15738
- Py_CLEAR (interned );
15710
+ PyDict_Clear (state -> interned );
15711
+ Py_CLEAR (state -> interned );
15739
15712
}
15740
15713
15741
15714
@@ -16106,7 +16079,8 @@ _PyUnicode_EnableLegacyWindowsFSEncoding(void)
16106
16079
static inline int
16107
16080
unicode_is_finalizing (void )
16108
16081
{
16109
- return (interned == NULL );
16082
+ struct _Py_unicode_state * state = get_unicode_state ();
16083
+ return (state -> interned == NULL );
16110
16084
}
16111
16085
#endif
16112
16086
@@ -16116,10 +16090,8 @@ _PyUnicode_Fini(PyInterpreterState *interp)
16116
16090
{
16117
16091
struct _Py_unicode_state * state = & interp -> unicode ;
16118
16092
16119
- if (_Py_IsMainInterpreter (interp )) {
16120
- // _PyUnicode_ClearInterned() must be called before _PyUnicode_Fini()
16121
- assert (interned == NULL );
16122
- }
16093
+ // _PyUnicode_ClearInterned() must be called before
16094
+ assert (state -> interned == NULL );
16123
16095
16124
16096
_PyUnicode_FiniEncodings (& state -> fs_codec );
16125
16097
0 commit comments