@@ -1766,8 +1766,10 @@ prepare_s(PyStructObject *self)
17661766 return -1 ;
17671767}
17681768
1769+ /* This should be moved to s_new() when Struct___init__() will
1770+ be removed (see gh-143715 and gh-94532). */
17691771static int
1770- s_init (PyStructObject * self , PyObject * format )
1772+ s_create (PyStructObject * self , PyObject * format )
17711773{
17721774 if (PyUnicode_Check (format )) {
17731775 format = PyUnicode_AsASCIIString (format );
@@ -1780,8 +1782,7 @@ s_init(PyStructObject *self, PyObject *format)
17801782 if (!PyBytes_Check (format )) {
17811783 PyErr_Format (PyExc_TypeError ,
17821784 "Struct() argument 1 must be a str or bytes object, "
1783- "not %T" ,
1784- format );
1785+ "not %T" , format );
17851786 Py_DECREF (format );
17861787 return -1 ;
17871788 }
@@ -1797,12 +1798,6 @@ s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
17971798{
17981799 PyStructObject * self ;
17991800
1800- if (PyTuple_GET_SIZE (args ) != 1 ) {
1801- if (PyErr_WarnEx (PyExc_DeprecationWarning ,
1802- "Struct.__new__() has one positional argument" , 1 )) {
1803- return NULL ;
1804- }
1805- }
18061801 assert (type != NULL );
18071802 allocfunc alloc_func = PyType_GetSlot (type , Py_tp_alloc );
18081803 assert (alloc_func != NULL );
@@ -1815,13 +1810,21 @@ s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18151810 self -> s_size = -1 ;
18161811 self -> s_len = -1 ;
18171812 self -> init_called = false;
1818- if (PyTuple_GET_SIZE (args ) == 1 ) {
1819- if (s_init (self , PyTuple_GET_ITEM (args , 0 ))) {
1820- Py_DECREF (self );
1821- return NULL ;
1813+ if (PyTuple_GET_SIZE (args ) != 1 ) {
1814+ if (PyErr_WarnEx (PyExc_DeprecationWarning ,
1815+ "Struct.__new__() has one positional argument" , 1 )) {
1816+ goto err ;
1817+ }
1818+ }
1819+ else {
1820+ if (s_create (self , PyTuple_GET_ITEM (args , 0 ))) {
1821+ goto err ;
18221822 }
18231823 }
18241824 return (PyObject * )self ;
1825+ err :
1826+ Py_DECREF (self );
1827+ return NULL ;
18251828}
18261829
18271830/*[clinic input]
@@ -1845,21 +1848,23 @@ Struct___init___impl(PyStructObject *self, PyObject *format)
18451848 "Struct() missing required argument 'format' (pos 1)" );
18461849 return -1 ;
18471850 }
1848- if (!self -> init_called ) {
1849- if (!self -> s_codes && s_init (self , format )) {
1851+ if (self -> init_called ) {
1852+ if (self -> s_codes
1853+ && PyErr_WarnEx (PyExc_DeprecationWarning ,
1854+ ("Explicit call of __init__() on "
1855+ "initialized Struct() is deprecated" ), 1 ))
1856+ {
1857+ return -1 ;
1858+ }
1859+ return s_create (self , format );
1860+ }
1861+ else {
1862+ if (!self -> s_codes && s_create (self , format )) {
18501863 return -1 ;
18511864 }
18521865 self -> init_called = true;
18531866 return 0 ;
18541867 }
1855- if ((self -> s_codes && self -> init_called )
1856- && PyErr_WarnEx (PyExc_DeprecationWarning ,
1857- ("Explicit call of __init__() on "
1858- "initialized Struct() is deprecated" ), 1 ))
1859- {
1860- return -1 ;
1861- }
1862- return s_init (self , format );
18631868}
18641869
18651870static int
0 commit comments