44
55#include "Python.h"
66#include "pycore_abstract.h" // _PyIndex_Check()
7- #include "pycore_bytes_methods.h"
8- #include "pycore_object.h"
7+ #include "pycore_bytes_methods.h" // _Py_bytes_startswith()
8+ #include "pycore_initconfig.h" // _PyStatus_OK()
9+ #include "pycore_object.h" // _PyObject_GC_TRACK
910#include "pycore_pymem.h" // PYMEM_CLEANBYTE
1011
1112#include "pystrhex.h"
@@ -41,6 +42,44 @@ get_bytes_state(void)
4142}
4243
4344
45+ // Return a borrowed reference to the empty bytes string singleton.
46+ static inline PyObject * bytes_get_empty (void )
47+ {
48+ struct _Py_bytes_state * state = get_bytes_state ();
49+ // bytes_get_empty() must not be called before _PyBytes_Init()
50+ // or after _PyBytes_Fini()
51+ assert (state -> empty_string != NULL );
52+ return state -> empty_string ;
53+ }
54+
55+
56+ // Return a strong reference to the empty bytes string singleton.
57+ static inline PyObject * bytes_new_empty (void )
58+ {
59+ PyObject * empty = bytes_get_empty ();
60+ Py_INCREF (empty );
61+ return (PyObject * )empty ;
62+ }
63+
64+
65+ static int
66+ bytes_create_empty_string_singleton (struct _Py_bytes_state * state )
67+ {
68+ // Create the empty bytes string singleton
69+ PyBytesObject * op = (PyBytesObject * )PyObject_Malloc (PyBytesObject_SIZE );
70+ if (op == NULL ) {
71+ return -1 ;
72+ }
73+ _PyObject_InitVar ((PyVarObject * )op , & PyBytes_Type , 0 );
74+ op -> ob_shash = -1 ;
75+ op -> ob_sval [0 ] = '\0' ;
76+
77+ assert (state -> empty_string == NULL );
78+ state -> empty_string = (PyObject * )op ;
79+ return 0 ;
80+ }
81+
82+
4483/*
4584 For PyBytes_FromString(), the parameter `str' points to a null-terminated
4685 string containing exactly `size' bytes.
@@ -70,12 +109,7 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
70109 assert (size >= 0 );
71110
72111 if (size == 0 ) {
73- struct _Py_bytes_state * state = get_bytes_state ();
74- op = state -> empty_string ;
75- if (op != NULL ) {
76- Py_INCREF (op );
77- return (PyObject * )op ;
78- }
112+ return bytes_new_empty ();
79113 }
80114
81115 if ((size_t )size > (size_t )PY_SSIZE_T_MAX - PyBytesObject_SIZE ) {
@@ -94,13 +128,8 @@ _PyBytes_FromSize(Py_ssize_t size, int use_calloc)
94128 }
95129 _PyObject_InitVar ((PyVarObject * )op , & PyBytes_Type , size );
96130 op -> ob_shash = -1 ;
97- if (!use_calloc )
131+ if (!use_calloc ) {
98132 op -> ob_sval [size ] = '\0' ;
99- /* empty byte string singleton */
100- if (size == 0 ) {
101- struct _Py_bytes_state * state = get_bytes_state ();
102- Py_INCREF (op );
103- state -> empty_string = op ;
104133 }
105134 return (PyObject * ) op ;
106135}
@@ -122,6 +151,9 @@ PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
122151 return (PyObject * )op ;
123152 }
124153 }
154+ if (size == 0 ) {
155+ return bytes_new_empty ();
156+ }
125157
126158 op = (PyBytesObject * )_PyBytes_FromSize (size , 0 );
127159 if (op == NULL )
@@ -155,11 +187,7 @@ PyBytes_FromString(const char *str)
155187
156188 struct _Py_bytes_state * state = get_bytes_state ();
157189 if (size == 0 ) {
158- op = state -> empty_string ;
159- if (op != NULL ) {
160- Py_INCREF (op );
161- return (PyObject * )op ;
162- }
190+ return bytes_new_empty ();
163191 }
164192 else if (size == 1 ) {
165193 op = state -> characters [* str & UCHAR_MAX ];
@@ -178,11 +206,8 @@ PyBytes_FromString(const char *str)
178206 op -> ob_shash = -1 ;
179207 memcpy (op -> ob_sval , str , size + 1 );
180208 /* share short strings */
181- if (size == 0 ) {
182- Py_INCREF (op );
183- state -> empty_string = op ;
184- }
185- else if (size == 1 ) {
209+ if (size == 1 ) {
210+ assert (state -> characters [* str & UCHAR_MAX ] == NULL );
186211 Py_INCREF (op );
187212 state -> characters [* str & UCHAR_MAX ] = op ;
188213 }
@@ -1272,7 +1297,7 @@ PyBytes_AsStringAndSize(PyObject *obj,
12721297/* -------------------------------------------------------------------- */
12731298/* Methods */
12741299
1275- #define STRINGLIB_GET_EMPTY () get_bytes_state()->empty_string
1300+ #define STRINGLIB_GET_EMPTY () bytes_get_empty()
12761301
12771302#include "stringlib/stringdefs.h"
12781303
@@ -3053,9 +3078,9 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
30533078 goto error ;
30543079 }
30553080 if (newsize == 0 ) {
3056- * pv = _PyBytes_FromSize ( 0 , 0 );
3081+ * pv = bytes_new_empty ( );
30573082 Py_DECREF (v );
3058- return ( * pv == NULL ) ? -1 : 0 ;
3083+ return 0 ;
30593084 }
30603085 /* XXX UNREF/NEWREF interface should be more symmetrical */
30613086#ifdef Py_REF_DEBUG
@@ -3084,6 +3109,18 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
30843109 return -1 ;
30853110}
30863111
3112+
3113+ PyStatus
3114+ _PyBytes_Init (PyThreadState * tstate )
3115+ {
3116+ struct _Py_bytes_state * state = & tstate -> interp -> bytes ;
3117+ if (bytes_create_empty_string_singleton (state ) < 0 ) {
3118+ return _PyStatus_NO_MEMORY ();
3119+ }
3120+ return _PyStatus_OK ();
3121+ }
3122+
3123+
30873124void
30883125_PyBytes_Fini (PyThreadState * tstate )
30893126{
0 commit comments