@@ -48,6 +48,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48
48
#include "pycore_interp.h" // PyInterpreterState.fs_codec
49
49
#include "pycore_object.h" // _PyObject_GC_TRACK()
50
50
#include "pycore_pathconfig.h" // _Py_DumpPathConfig()
51
+ #include "pycore_pyerrors.h" // _Py_FatalRefcountError()
51
52
#include "pycore_pylifecycle.h" // _Py_SetFileSystemEncoding()
52
53
#include "pycore_pystate.h" // _PyInterpreterState_GET()
53
54
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
@@ -212,6 +213,24 @@ extern "C" {
212
213
#endif
213
214
214
215
216
+ /* Forward declaration */
217
+ static inline int
218
+ _PyUnicodeWriter_WriteCharInline (_PyUnicodeWriter * writer , Py_UCS4 ch );
219
+ static inline void
220
+ _PyUnicodeWriter_InitWithBuffer (_PyUnicodeWriter * writer , PyObject * buffer );
221
+ static PyObject *
222
+ unicode_encode_utf8 (PyObject * unicode , _Py_error_handler error_handler ,
223
+ const char * errors );
224
+ static PyObject *
225
+ unicode_decode_utf8 (const char * s , Py_ssize_t size ,
226
+ _Py_error_handler error_handler , const char * errors ,
227
+ Py_ssize_t * consumed );
228
+ #ifdef Py_DEBUG
229
+ static inline int unicode_is_finalizing (void );
230
+ static int unicode_is_singleton (PyObject * unicode );
231
+ #endif
232
+
233
+
215
234
static struct _Py_unicode_state *
216
235
get_unicode_state (void )
217
236
{
@@ -279,19 +298,6 @@ unicode_fill(enum PyUnicode_Kind kind, void *data, Py_UCS4 value,
279
298
}
280
299
281
300
282
- /* Forward declaration */
283
- static inline int
284
- _PyUnicodeWriter_WriteCharInline (_PyUnicodeWriter * writer , Py_UCS4 ch );
285
- static inline void
286
- _PyUnicodeWriter_InitWithBuffer (_PyUnicodeWriter * writer , PyObject * buffer );
287
- static PyObject *
288
- unicode_encode_utf8 (PyObject * unicode , _Py_error_handler error_handler ,
289
- const char * errors );
290
- static PyObject *
291
- unicode_decode_utf8 (const char * s , Py_ssize_t size ,
292
- _Py_error_handler error_handler , const char * errors ,
293
- Py_ssize_t * consumed );
294
-
295
301
/* Fast detection of the most frequent whitespace characters */
296
302
const unsigned char _Py_ascii_whitespace [] = {
297
303
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
@@ -1930,6 +1936,12 @@ _PyUnicode_Ready(PyObject *unicode)
1930
1936
static void
1931
1937
unicode_dealloc (PyObject * unicode )
1932
1938
{
1939
+ #ifdef Py_DEBUG
1940
+ if (!unicode_is_finalizing () && unicode_is_singleton (unicode )) {
1941
+ _Py_FatalRefcountError ("deallocating an Unicode singleton" );
1942
+ }
1943
+ #endif
1944
+
1933
1945
switch (PyUnicode_CHECK_INTERNED (unicode )) {
1934
1946
case SSTATE_NOT_INTERNED :
1935
1947
break ;
@@ -1982,11 +1994,8 @@ unicode_is_singleton(PyObject *unicode)
1982
1994
if (unicode == state -> empty_string ) {
1983
1995
return 1 ;
1984
1996
}
1985
- PyASCIIObject * ascii = (PyASCIIObject * )unicode ;
1986
- if (ascii -> state .kind != PyUnicode_WCHAR_KIND && ascii -> length == 1 )
1987
- {
1988
- Py_UCS4 ch = PyUnicode_READ_CHAR (unicode , 0 );
1989
- if (ch < 256 && state -> latin1 [ch ] == unicode ) {
1997
+ for (Py_ssize_t i = 0 ; i < 256 ; i ++ ) {
1998
+ if (unicode == state -> latin1 [i ]) {
1990
1999
return 1 ;
1991
2000
}
1992
2001
}
@@ -15984,6 +15993,16 @@ _PyUnicode_EnableLegacyWindowsFSEncoding(void)
15984
15993
#endif
15985
15994
15986
15995
15996
+ #ifdef Py_DEBUG
15997
+ static inline int
15998
+ unicode_is_finalizing (void )
15999
+ {
16000
+ struct _Py_unicode_state * state = get_unicode_state ();
16001
+ return (state -> interned == NULL );
16002
+ }
16003
+ #endif
16004
+
16005
+
15987
16006
void
15988
16007
_PyUnicode_Fini (PyInterpreterState * interp )
15989
16008
{
0 commit comments