@@ -211,21 +211,13 @@ static int unicode_is_singleton(PyObject *unicode);
211
211
#endif
212
212
213
213
214
- // Return a borrowed reference to the empty string singleton.
214
+ // Return a reference to the immortal empty string singleton.
215
215
static inline PyObject * unicode_get_empty (void )
216
216
{
217
217
_Py_DECLARE_STR (empty , "" );
218
218
return & _Py_STR (empty );
219
219
}
220
220
221
-
222
- // Return a strong reference to the empty string singleton.
223
- static inline PyObject * unicode_new_empty (void )
224
- {
225
- PyObject * empty = unicode_get_empty ();
226
- return Py_NewRef (empty );
227
- }
228
-
229
221
/* This dictionary holds all interned unicode strings. Note that references
230
222
to strings in this dictionary are *not* counted in the string's ob_refcnt.
231
223
When the interned string reaches a refcnt of 0 the string deallocation
@@ -310,7 +302,7 @@ clear_interned_dict(PyInterpreterState *interp)
310
302
311
303
#define _Py_RETURN_UNICODE_EMPTY () \
312
304
do { \
313
- return unicode_new_empty (); \
305
+ return unicode_get_empty (); \
314
306
} while (0)
315
307
316
308
static inline void
@@ -650,7 +642,6 @@ unicode_result(PyObject *unicode)
650
642
PyObject * empty = unicode_get_empty ();
651
643
if (unicode != empty ) {
652
644
Py_DECREF (unicode );
653
- Py_INCREF (empty );
654
645
}
655
646
return empty ;
656
647
}
@@ -662,7 +653,6 @@ unicode_result(PyObject *unicode)
662
653
Py_UCS1 ch = data [0 ];
663
654
PyObject * latin1_char = LATIN1 (ch );
664
655
if (unicode != latin1_char ) {
665
- Py_INCREF (latin1_char );
666
656
Py_DECREF (unicode );
667
657
}
668
658
return latin1_char ;
@@ -1199,7 +1189,7 @@ PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
1199
1189
{
1200
1190
/* Optimization for empty strings */
1201
1191
if (size == 0 ) {
1202
- return unicode_new_empty ();
1192
+ return unicode_get_empty ();
1203
1193
}
1204
1194
1205
1195
PyObject * obj ;
@@ -1669,7 +1659,7 @@ unicode_resize(PyObject **p_unicode, Py_ssize_t length)
1669
1659
return 0 ;
1670
1660
1671
1661
if (length == 0 ) {
1672
- PyObject * empty = unicode_new_empty ();
1662
+ PyObject * empty = unicode_get_empty ();
1673
1663
Py_SETREF (* p_unicode , empty );
1674
1664
return 0 ;
1675
1665
}
@@ -1764,7 +1754,9 @@ unicode_write_cstr(PyObject *unicode, Py_ssize_t index,
1764
1754
static PyObject *
1765
1755
get_latin1_char (Py_UCS1 ch )
1766
1756
{
1767
- return Py_NewRef (LATIN1 (ch ));
1757
+ PyObject * o = LATIN1 (ch );
1758
+ assert (_Py_IsImmortal (o ));
1759
+ return o ;
1768
1760
}
1769
1761
1770
1762
static PyObject *
@@ -1891,7 +1883,7 @@ PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
1891
1883
"NULL string with positive size with NULL passed to PyUnicode_FromStringAndSize" );
1892
1884
return NULL ;
1893
1885
}
1894
- return unicode_new_empty ();
1886
+ return unicode_get_empty ();
1895
1887
}
1896
1888
1897
1889
PyObject *
@@ -10261,7 +10253,7 @@ replace(PyObject *self, PyObject *str1,
10261
10253
}
10262
10254
new_size = slen + n * (len2 - len1 );
10263
10255
if (new_size == 0 ) {
10264
- u = unicode_new_empty ();
10256
+ u = unicode_get_empty ();
10265
10257
goto done ;
10266
10258
}
10267
10259
if (new_size > (PY_SSIZE_T_MAX / rkind )) {
@@ -14505,7 +14497,7 @@ unicode_new_impl(PyTypeObject *type, PyObject *x, const char *encoding,
14505
14497
{
14506
14498
PyObject * unicode ;
14507
14499
if (x == NULL ) {
14508
- unicode = unicode_new_empty ();
14500
+ unicode = unicode_get_empty ();
14509
14501
}
14510
14502
else if (encoding == NULL && errors == NULL ) {
14511
14503
unicode = PyObject_Str (x );
@@ -14994,8 +14986,7 @@ unicode_ascii_iter_next(unicodeiterobject *it)
14994
14986
Py_UCS1 chr = (Py_UCS1 )PyUnicode_READ (PyUnicode_1BYTE_KIND ,
14995
14987
data , it -> it_index );
14996
14988
it -> it_index ++ ;
14997
- PyObject * item = (PyObject * )& _Py_SINGLETON (strings ).ascii [chr ];
14998
- return Py_NewRef (item );
14989
+ return (PyObject * )& _Py_SINGLETON (strings ).ascii [chr ];
14999
14990
}
15000
14991
it -> it_seq = NULL ;
15001
14992
Py_DECREF (seq );
@@ -15025,7 +15016,7 @@ unicodeiter_reduce(unicodeiterobject *it, PyObject *Py_UNUSED(ignored))
15025
15016
if (it -> it_seq != NULL ) {
15026
15017
return Py_BuildValue ("N(O)n" , iter , it -> it_seq , it -> it_index );
15027
15018
} else {
15028
- PyObject * u = unicode_new_empty ();
15019
+ PyObject * u = unicode_get_empty ();
15029
15020
if (u == NULL ) {
15030
15021
Py_XDECREF (iter );
15031
15022
return NULL ;
0 commit comments