diff options
author | Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> | 2022-09-25 13:32:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-25 13:32:48 (GMT) |
commit | c8c0afc7137ab9f22bf59d591084948ca967c97c (patch) | |
tree | 60cfb58ebb2efefdc00cd339a4f9b545e35bccea /Modules/_struct.c | |
parent | f5f047aa628caeca680745c55e24519f06aa6724 (diff) | |
download | cpython-c8c0afc7137ab9f22bf59d591084948ca967c97c.zip cpython-c8c0afc7137ab9f22bf59d591084948ca967c97c.tar.gz cpython-c8c0afc7137ab9f22bf59d591084948ca967c97c.tar.bz2 |
GH-78724: Initialize struct.Struct in __new__ (GH-94532)
Closes https://github.com/python/cpython/issues/75960
Closes https://github.com/python/cpython/issues/78724
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 59 |
1 files changed, 26 insertions, 33 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index 09f52a9..f9bac34 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1477,28 +1477,9 @@ prepare_s(PyStructObject *self) return -1; } -static PyObject * -s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) -{ - PyObject *self; - - assert(type != NULL); - allocfunc alloc_func = PyType_GetSlot(type, Py_tp_alloc); - assert(alloc_func != NULL); - - self = alloc_func(type, 0); - if (self != NULL) { - PyStructObject *s = (PyStructObject*)self; - s->s_format = Py_NewRef(Py_None); - s->s_codes = NULL; - s->s_size = -1; - s->s_len = -1; - } - return self; -} - /*[clinic input] -Struct.__init__ +@classmethod +Struct.__new__ format: object @@ -1510,16 +1491,24 @@ the format string. See help(struct) for more on format strings. [clinic start generated code]*/ -static int -Struct___init___impl(PyStructObject *self, PyObject *format) -/*[clinic end generated code: output=b8e80862444e92d0 input=192a4575a3dde802]*/ +static PyObject * +Struct_impl(PyTypeObject *type, PyObject *format) +/*[clinic end generated code: output=49468b044e334308 input=8b91868eb1df0e28]*/ { - int ret = 0; + allocfunc alloc = PyType_GetSlot(type, Py_tp_alloc); + assert(alloc != NULL); + PyStructObject *self = (PyStructObject *)alloc(type, 0); + + if (self == NULL) { + return NULL; + } if (PyUnicode_Check(format)) { format = PyUnicode_AsASCIIString(format); - if (format == NULL) - return -1; + if (format == NULL) { + Py_DECREF(self); + return NULL; + } } else { Py_INCREF(format); @@ -1527,19 +1516,24 @@ Struct___init___impl(PyStructObject *self, PyObject *format) if (!PyBytes_Check(format)) { Py_DECREF(format); + Py_DECREF(self); PyErr_Format(PyExc_TypeError, "Struct() argument 1 must be a str or bytes object, " "not %.200s", _PyType_Name(Py_TYPE(format))); - return -1; + return NULL; } - Py_SETREF(self->s_format, format); + self->s_format = format; - ret = prepare_s(self); - return ret; + if (prepare_s(self) < 0) { + Py_DECREF(self); + return NULL; + } + return (PyObject *)self; } + static int s_clear(PyStructObject *s) { @@ -2144,9 +2138,8 @@ static PyType_Slot PyStructType_slots[] = { {Py_tp_methods, s_methods}, {Py_tp_members, s_members}, {Py_tp_getset, s_getsetlist}, - {Py_tp_init, Struct___init__}, + {Py_tp_new, Struct}, {Py_tp_alloc, PyType_GenericAlloc}, - {Py_tp_new, s_new}, {Py_tp_free, PyObject_GC_Del}, {0, 0}, }; |