mirror of https://github.com/python/cpython.git
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
This commit is contained in:
parent
f5f047aa62
commit
c8c0afc713
|
@ -700,6 +700,20 @@ def test__struct_types_immutable(self):
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
cls.x = 1
|
cls.x = 1
|
||||||
|
|
||||||
|
@support.cpython_only
|
||||||
|
def test__struct_Struct__new__initialized(self):
|
||||||
|
# See https://github.com/python/cpython/issues/78724
|
||||||
|
|
||||||
|
s = struct.Struct.__new__(struct.Struct, "b")
|
||||||
|
s.unpack_from(b"abcd")
|
||||||
|
|
||||||
|
@support.cpython_only
|
||||||
|
def test__struct_Struct_subclassing(self):
|
||||||
|
class Bob(struct.Struct):
|
||||||
|
pass
|
||||||
|
|
||||||
|
s = Bob("b")
|
||||||
|
s.unpack_from(b"abcd")
|
||||||
|
|
||||||
def test_issue35714(self):
|
def test_issue35714(self):
|
||||||
# Embedded null characters should not be allowed in format strings.
|
# Embedded null characters should not be allowed in format strings.
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix crash in :class:`struct.Struct` when it was not completely initialized by initializing it in :meth:`~object.__new__``. Patch by Kumar Aditya.
|
|
@ -1477,28 +1477,9 @@ prepare_s(PyStructObject *self)
|
||||||
return -1;
|
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]
|
/*[clinic input]
|
||||||
Struct.__init__
|
@classmethod
|
||||||
|
Struct.__new__
|
||||||
|
|
||||||
format: object
|
format: object
|
||||||
|
|
||||||
|
@ -1510,16 +1491,24 @@ the format string.
|
||||||
See help(struct) for more on format strings.
|
See help(struct) for more on format strings.
|
||||||
[clinic start generated code]*/
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static int
|
static PyObject *
|
||||||
Struct___init___impl(PyStructObject *self, PyObject *format)
|
Struct_impl(PyTypeObject *type, PyObject *format)
|
||||||
/*[clinic end generated code: output=b8e80862444e92d0 input=192a4575a3dde802]*/
|
/*[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)) {
|
if (PyUnicode_Check(format)) {
|
||||||
format = PyUnicode_AsASCIIString(format);
|
format = PyUnicode_AsASCIIString(format);
|
||||||
if (format == NULL)
|
if (format == NULL) {
|
||||||
return -1;
|
Py_DECREF(self);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Py_INCREF(format);
|
Py_INCREF(format);
|
||||||
|
@ -1527,19 +1516,24 @@ Struct___init___impl(PyStructObject *self, PyObject *format)
|
||||||
|
|
||||||
if (!PyBytes_Check(format)) {
|
if (!PyBytes_Check(format)) {
|
||||||
Py_DECREF(format);
|
Py_DECREF(format);
|
||||||
|
Py_DECREF(self);
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"Struct() argument 1 must be a str or bytes object, "
|
"Struct() argument 1 must be a str or bytes object, "
|
||||||
"not %.200s",
|
"not %.200s",
|
||||||
_PyType_Name(Py_TYPE(format)));
|
_PyType_Name(Py_TYPE(format)));
|
||||||
return -1;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_SETREF(self->s_format, format);
|
self->s_format = format;
|
||||||
|
|
||||||
ret = prepare_s(self);
|
if (prepare_s(self) < 0) {
|
||||||
return ret;
|
Py_DECREF(self);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return (PyObject *)self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
s_clear(PyStructObject *s)
|
s_clear(PyStructObject *s)
|
||||||
{
|
{
|
||||||
|
@ -2144,9 +2138,8 @@ static PyType_Slot PyStructType_slots[] = {
|
||||||
{Py_tp_methods, s_methods},
|
{Py_tp_methods, s_methods},
|
||||||
{Py_tp_members, s_members},
|
{Py_tp_members, s_members},
|
||||||
{Py_tp_getset, s_getsetlist},
|
{Py_tp_getset, s_getsetlist},
|
||||||
{Py_tp_init, Struct___init__},
|
{Py_tp_new, Struct},
|
||||||
{Py_tp_alloc, PyType_GenericAlloc},
|
{Py_tp_alloc, PyType_GenericAlloc},
|
||||||
{Py_tp_new, s_new},
|
|
||||||
{Py_tp_free, PyObject_GC_Del},
|
{Py_tp_free, PyObject_GC_Del},
|
||||||
{0, 0},
|
{0, 0},
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,7 @@ preserve
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
PyDoc_STRVAR(Struct___init____doc__,
|
PyDoc_STRVAR(Struct__doc__,
|
||||||
"Struct(format)\n"
|
"Struct(format)\n"
|
||||||
"--\n"
|
"--\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
@ -19,13 +19,13 @@ PyDoc_STRVAR(Struct___init____doc__,
|
||||||
"\n"
|
"\n"
|
||||||
"See help(struct) for more on format strings.");
|
"See help(struct) for more on format strings.");
|
||||||
|
|
||||||
static int
|
static PyObject *
|
||||||
Struct___init___impl(PyStructObject *self, PyObject *format);
|
Struct_impl(PyTypeObject *type, PyObject *format);
|
||||||
|
|
||||||
static int
|
static PyObject *
|
||||||
Struct___init__(PyObject *self, PyObject *args, PyObject *kwargs)
|
Struct(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
int return_value = -1;
|
PyObject *return_value = NULL;
|
||||||
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
|
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
|
||||||
|
|
||||||
#define NUM_KEYWORDS 1
|
#define NUM_KEYWORDS 1
|
||||||
|
@ -61,7 +61,7 @@ Struct___init__(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
format = fastargs[0];
|
format = fastargs[0];
|
||||||
return_value = Struct___init___impl((PyStructObject *)self, format);
|
return_value = Struct_impl(type, format);
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
|
@ -451,4 +451,4 @@ exit:
|
||||||
|
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=eca7df0e75f8919d input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=f3d6e06f80368998 input=a9049054013a1b77]*/
|
||||||
|
|
Loading…
Reference in New Issue