@@ -236,14 +236,6 @@ bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size)
236236 return -1 ;
237237 }
238238
239- if (requested_size == 0 ) {
240- /* Resizing to zero just resets to the empty constant (#153419) */
241- Py_XSETREF (obj -> ob_bytes_object ,
242- Py_GetConstant (Py_CONSTANT_EMPTY_BYTES ));
243- bytearray_reinit_from_bytes (obj , 0 , 0 );
244- return 0 ;
245- }
246-
247239 if (size + logical_offset <= alloc ) {
248240 /* Current buffer is large enough to host the requested size,
249241 decide on a strategy. */
@@ -910,19 +902,6 @@ bytearray_ass_subscript(PyObject *op, PyObject *index, PyObject *values)
910902 return ret ;
911903}
912904
913- static PyObject *
914- bytearray_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
915- {
916- PyObject * self = PyType_GenericNew (type , args , kwds );
917- if (self == NULL ) {
918- return NULL ;
919- }
920- PyByteArrayObject * obj = _PyByteArray_CAST (self );
921- obj -> ob_bytes_object = Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
922- bytearray_reinit_from_bytes (obj , 0 , 0 );
923- return self ;
924- }
925-
926905/*[clinic input]
927906bytearray.__init__
928907
@@ -941,13 +920,22 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
941920 PyObject * it ;
942921 PyObject * (* iternext )(PyObject * );
943922
923+ /* First __init__; set ob_bytes_object so ob_bytes is always non-null. */
924+ if (self -> ob_bytes_object == NULL ) {
925+ self -> ob_bytes_object = Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
926+ bytearray_reinit_from_bytes (self , 0 , 0 );
927+ self -> ob_exports = 0 ;
928+ }
929+
944930 if (Py_SIZE (self ) != 0 ) {
945- /* Empty previous contents if possible (yes, do this first of all!). */
931+ /* Empty previous contents (yes, do this first of all!) */
946932 if (PyByteArray_Resize ((PyObject * )self , 0 ) < 0 )
947933 return -1 ;
948934 }
949935
936+ /* Should be caused by first init or the resize to 0. */
950937 assert (self -> ob_bytes_object == Py_GetConstantBorrowed (Py_CONSTANT_EMPTY_BYTES ));
938+ assert (self -> ob_exports == 0 );
951939
952940 /* Make a quick exit if no first argument */
953941 if (arg == NULL ) {
@@ -975,11 +963,8 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
975963 }
976964 assert (PyBytes_Check (encoded ));
977965
978- /* Most encodes return a new unique bytes, just use it as buffer.
979- If there are active exports, let `iconcat` handle raising the
980- error when encoded is not empty */
981- if (self -> ob_exports == 0
982- && _PyObject_IsUniquelyReferenced (encoded )
966+ /* Most encodes return a new unique bytes, just use it as buffer. */
967+ if (_PyObject_IsUniquelyReferenced (encoded )
983968 && PyBytes_CheckExact (encoded ))
984969 {
985970 Py_ssize_t size = Py_SIZE (encoded );
@@ -2952,7 +2937,7 @@ PyTypeObject PyByteArray_Type = {
29522937 0 , /* tp_dictoffset */
29532938 bytearray___init__ , /* tp_init */
29542939 PyType_GenericAlloc , /* tp_alloc */
2955- bytearray_new , /* tp_new */
2940+ PyType_GenericNew , /* tp_new */
29562941 PyObject_Free , /* tp_free */
29572942 .tp_version_tag = _Py_TYPE_VERSION_BYTEARRAY ,
29582943};
0 commit comments