@@ -2580,24 +2580,27 @@ static PyNumberMethods bytes_as_number = {
25802580};
25812581
25822582static PyObject *
2583- bytes_subtype_new (PyTypeObject * type , PyObject * args , PyObject * kwds );
2583+ bytes_subtype_new (PyTypeObject * , PyObject * );
2584+
2585+ /*[clinic input]
2586+ @classmethod
2587+ bytes.__new__ as bytes_new
2588+
2589+ source as x: object = NULL
2590+ encoding: str = NULL
2591+ errors: str = NULL
2592+
2593+ [clinic start generated code]*/
25842594
25852595static PyObject *
2586- bytes_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
2596+ bytes_new_impl (PyTypeObject * type , PyObject * x , const char * encoding ,
2597+ const char * errors )
2598+ /*[clinic end generated code: output=1e0c471be311a425 input=f0a966d19b7262b4]*/
25872599{
2588- PyObject * x = NULL ;
2589- const char * encoding = NULL ;
2590- const char * errors = NULL ;
2591- PyObject * new = NULL ;
2600+ PyObject * bytes ;
25922601 PyObject * func ;
25932602 Py_ssize_t size ;
2594- static char * kwlist [] = {"source" , "encoding" , "errors" , 0 };
25952603
2596- if (type != & PyBytes_Type )
2597- return bytes_subtype_new (type , args , kwds );
2598- if (!PyArg_ParseTupleAndKeywords (args , kwds , "|Oss:bytes" , kwlist , & x ,
2599- & encoding , & errors ))
2600- return NULL ;
26012604 if (x == NULL ) {
26022605 if (encoding != NULL || errors != NULL ) {
26032606 PyErr_SetString (PyExc_TypeError ,
@@ -2606,78 +2609,73 @@ bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
26062609 "errors without a string argument" );
26072610 return NULL ;
26082611 }
2609- return PyBytes_FromStringAndSize (NULL , 0 );
2612+ bytes = PyBytes_FromStringAndSize (NULL , 0 );
26102613 }
2611-
2612- if (encoding != NULL ) {
2614+ else if (encoding != NULL ) {
26132615 /* Encode via the codec registry */
26142616 if (!PyUnicode_Check (x )) {
26152617 PyErr_SetString (PyExc_TypeError ,
26162618 "encoding without a string argument" );
26172619 return NULL ;
26182620 }
2619- new = PyUnicode_AsEncodedString (x , encoding , errors );
2620- if (new == NULL )
2621- return NULL ;
2622- assert (PyBytes_Check (new ));
2623- return new ;
2621+ bytes = PyUnicode_AsEncodedString (x , encoding , errors );
26242622 }
2625-
2626- if (errors != NULL ) {
2623+ else if (errors != NULL ) {
26272624 PyErr_SetString (PyExc_TypeError ,
26282625 PyUnicode_Check (x ) ?
26292626 "string argument without an encoding" :
26302627 "errors without a string argument" );
26312628 return NULL ;
26322629 }
2633-
26342630 /* We'd like to call PyObject_Bytes here, but we need to check for an
26352631 integer argument before deferring to PyBytes_FromObject, something
26362632 PyObject_Bytes doesn't do. */
2637- func = _PyObject_LookupSpecial (x , & PyId___bytes__ );
2638- if (func != NULL ) {
2639- new = _PyObject_CallNoArg (func );
2633+ else if ((func = _PyObject_LookupSpecial (x , & PyId___bytes__ )) != NULL ) {
2634+ bytes = _PyObject_CallNoArg (func );
26402635 Py_DECREF (func );
2641- if (new == NULL )
2636+ if (bytes == NULL )
26422637 return NULL ;
2643- if (!PyBytes_Check (new )) {
2638+ if (!PyBytes_Check (bytes )) {
26442639 PyErr_Format (PyExc_TypeError ,
2645- "__bytes__ returned non-bytes (type %.200s)" ,
2646- Py_TYPE (new )-> tp_name );
2647- Py_DECREF (new );
2640+ "__bytes__ returned non-bytes (type %.200s)" ,
2641+ Py_TYPE (bytes )-> tp_name );
2642+ Py_DECREF (bytes );
26482643 return NULL ;
26492644 }
2650- return new ;
26512645 }
26522646 else if (PyErr_Occurred ())
26532647 return NULL ;
2654-
2655- if (PyUnicode_Check (x )) {
2648+ else if (PyUnicode_Check (x )) {
26562649 PyErr_SetString (PyExc_TypeError ,
26572650 "string argument without an encoding" );
26582651 return NULL ;
26592652 }
26602653 /* Is it an integer? */
2661- if (_PyIndex_Check (x )) {
2654+ else if (_PyIndex_Check (x )) {
26622655 size = PyNumber_AsSsize_t (x , PyExc_OverflowError );
26632656 if (size == -1 && PyErr_Occurred ()) {
26642657 if (!PyErr_ExceptionMatches (PyExc_TypeError ))
26652658 return NULL ;
26662659 PyErr_Clear (); /* fall through */
2660+ bytes = PyBytes_FromObject (x );
26672661 }
26682662 else {
26692663 if (size < 0 ) {
26702664 PyErr_SetString (PyExc_ValueError , "negative count" );
26712665 return NULL ;
26722666 }
2673- new = _PyBytes_FromSize (size , 1 );
2674- if (new == NULL )
2675- return NULL ;
2676- return new ;
2667+ bytes = _PyBytes_FromSize (size , 1 );
26772668 }
26782669 }
2670+ else {
2671+ bytes = PyBytes_FromObject (x );
2672+ }
2673+
2674+ if (bytes != NULL && type != & PyBytes_Type ) {
2675+ Py_SETREF (bytes , bytes_subtype_new (type , bytes ));
2676+ }
26792677
2680- return PyBytes_FromObject ( x ) ;
2678+ return bytes ;
26812679}
26822680
26832681static PyObject *
@@ -2889,15 +2887,12 @@ PyBytes_FromObject(PyObject *x)
28892887}
28902888
28912889static PyObject *
2892- bytes_subtype_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
2890+ bytes_subtype_new (PyTypeObject * type , PyObject * tmp )
28932891{
2894- PyObject * tmp , * pnew ;
2892+ PyObject * pnew ;
28952893 Py_ssize_t n ;
28962894
28972895 assert (PyType_IsSubtype (type , & PyBytes_Type ));
2898- tmp = bytes_new (& PyBytes_Type , args , kwds );
2899- if (tmp == NULL )
2900- return NULL ;
29012896 assert (PyBytes_Check (tmp ));
29022897 n = PyBytes_GET_SIZE (tmp );
29032898 pnew = type -> tp_alloc (type , n );
@@ -2907,7 +2902,6 @@ bytes_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
29072902 ((PyBytesObject * )pnew )-> ob_shash =
29082903 ((PyBytesObject * )tmp )-> ob_shash ;
29092904 }
2910- Py_DECREF (tmp );
29112905 return pnew ;
29122906}
29132907
0 commit comments