| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f54b90c commit 6d24e80
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -700,20 +700,6 @@ def test__struct_types_immutable(self): | |||
| 700 | 700 | with self.assertRaises(TypeError): | |
| 701 | 701 | cls.x = 1 | |
| 702 | 702 | ||
| 703 | - @support.cpython_only | ||
| 704 | - def test__struct_Struct__new__initialized(self): | ||
| 705 | - # See https://github.com/python/cpython/issues/78724 | ||
| 706 | - | ||
| 707 | - s = struct.Struct.__new__(struct.Struct, "b") | ||
| 708 | - s.unpack_from(b"abcd") | ||
| 709 | - | ||
| 710 | - @support.cpython_only | ||
| 711 | - def test__struct_Struct_subclassing(self): | ||
| 712 | - class Bob(struct.Struct): | ||
| 713 | - pass | ||
| 714 | - | ||
| 715 | - s = Bob("b") | ||
| 716 | - s.unpack_from(b"abcd") | ||
| 717 | 703 | ||
| 718 | 704 | def test_issue35714(self): | |
| 719 | 705 | # Embedded null characters should not be allowed in format strings. | |
@@ -774,6 +760,15 @@ def test_error_propagation(fmt_str): | |||
| 774 | 760 | test_error_propagation('N') | |
| 775 | 761 | test_error_propagation('n') | |
| 776 | 762 | ||
| 763 | + def test_struct_subclass_instantiation(self): | ||
| 764 | + # Regression test for https://github.com/python/cpython/issues/112358 | ||
| 765 | + class MyStruct(struct.Struct): | ||
| 766 | + def __init__(self): | ||
| 767 | + super().__init__('>h') | ||
| 768 | + | ||
| 769 | + my_struct = MyStruct() | ||
| 770 | + self.assertEqual(my_struct.pack(12345), b'\x30\x39') | ||
| 771 | + | ||
| 777 | 772 | def test_repr(self): | |
| 778 | 773 | s = struct.Struct('=i2H') | |
| 779 | 774 | self.assertEqual(repr(s), f'Struct({s.format!r})') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Revert change to :class:`struct.Struct` initialization that broke some cases | ||
| 2 | + of subclassing. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1553,9 +1553,28 @@ prepare_s(PyStructObject *self) | |||
| 1553 | 1553 | return -1; | |
| 1554 | 1554 | } | |
| 1555 | 1555 | ||
| 1556 | + static PyObject * | ||
| 1557 | + s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||
| 1558 | + { | ||
| 1559 | + PyObject *self; | ||
| 1560 | + | ||
| 1561 | + assert(type != NULL); | ||
| 1562 | + allocfunc alloc_func = PyType_GetSlot(type, Py_tp_alloc); | ||
| 1563 | + assert(alloc_func != NULL); | ||
| 1564 | + | ||
| 1565 | + self = alloc_func(type, 0); | ||
| 1566 | + if (self != NULL) { | ||
| 1567 | + PyStructObject *s = (PyStructObject*)self; | ||
| 1568 | + s->s_format = Py_NewRef(Py_None); | ||
| 1569 | + s->s_codes = NULL; | ||
| 1570 | + s->s_size = -1; | ||
| 1571 | + s->s_len = -1; | ||
| 1572 | + } | ||
| 1573 | + return self; | ||
| 1574 | + } | ||
| 1575 | + | ||
| 1556 | 1576 | /*[clinic input] | |
| 1557 | - @classmethod | ||
| 1558 | - Struct.__new__ | ||
| 1577 | + Struct.__init__ | ||
| 1559 | 1578 | ||
| 1560 | 1579 | format: object | |
| 1561 | 1580 | ||
@@ -1567,49 +1586,36 @@ the format string. | |||
| 1567 | 1586 | See help(struct) for more on format strings. | |
| 1568 | 1587 | [clinic start generated code]*/ | |
| 1569 | 1588 | ||
| 1570 | - static PyObject * | ||
| 1571 | - Struct_impl(PyTypeObject *type, PyObject *format) | ||
| 1572 | - /*[clinic end generated code: output=49468b044e334308 input=8b91868eb1df0e28]*/ | ||
| 1589 | + static int | ||
| 1590 | + Struct___init___impl(PyStructObject *self, PyObject *format) | ||
| 1591 | + /*[clinic end generated code: output=b8e80862444e92d0 input=192a4575a3dde802]*/ | ||
| 1573 | 1592 | { | |
| 1574 | - allocfunc alloc = PyType_GetSlot(type, Py_tp_alloc); | ||
| 1575 | - assert(alloc != NULL); | ||
| 1576 | - PyStructObject *self = (PyStructObject *)alloc(type, 0); | ||
| 1577 | - | ||
| 1578 | - if (self == NULL) { | ||
| 1579 | - return NULL; | ||
| 1580 | - } | ||
| 1593 | + int ret = 0; | ||
| 1581 | 1594 | ||
| 1582 | 1595 | if (PyUnicode_Check(format)) { | |
| 1583 | 1596 | format = PyUnicode_AsASCIIString(format); | |
| 1584 | - if (format == NULL) { | ||
| 1585 | - Py_DECREF(self); | ||
| 1586 | - return NULL; | ||
| 1587 | - } | ||
| 1597 | + if (format == NULL) | ||
| 1598 | + return -1; | ||
| 1588 | 1599 | } | |
| 1589 | 1600 | else { | |
| 1590 | 1601 | Py_INCREF(format); | |
| 1591 | 1602 | } | |
| 1592 | 1603 | ||
| 1593 | 1604 | if (!PyBytes_Check(format)) { | |
| 1594 | 1605 | Py_DECREF(format); | |
| 1595 | - Py_DECREF(self); | ||
| 1596 | 1606 | PyErr_Format(PyExc_TypeError, | |
| 1597 | 1607 | "Struct() argument 1 must be a str or bytes object, " | |
| 1598 | 1608 | "not %.200s", | |
| 1599 | 1609 | _PyType_Name(Py_TYPE(format))); | |
| 1600 | - return NULL; | ||
| 1610 | + return -1; | ||
| 1601 | 1611 | } | |
| 1602 | 1612 | ||
| 1603 | - self->s_format = format; | ||
| 1613 | + Py_SETREF(self->s_format, format); | ||
| 1604 | 1614 | ||
| 1605 | - if (prepare_s(self) < 0) { | ||
| 1606 | - Py_DECREF(self); | ||
| 1607 | - return NULL; | ||
| 1608 | - } | ||
| 1609 | - return (PyObject *)self; | ||
| 1615 | + ret = prepare_s(self); | ||
| 1616 | + return ret; | ||
| 1610 | 1617 | } | |
| 1611 | 1618 | ||
| 1612 | - | ||
| 1613 | 1619 | static int | |
| 1614 | 1620 | s_clear(PyStructObject *s) | |
| 1615 | 1621 | { | |
@@ -2219,8 +2225,9 @@ static PyType_Slot PyStructType_slots[] = { | |||
| 2219 | 2225 | {Py_tp_methods, s_methods}, | |
| 2220 | 2226 | {Py_tp_members, s_members}, | |
| 2221 | 2227 | {Py_tp_getset, s_getsetlist}, | |
| 2222 | - {Py_tp_new, Struct}, | ||
| 2228 | + {Py_tp_init, Struct___init__}, | ||
| 2223 | 2229 | {Py_tp_alloc, PyType_GenericAlloc}, | |
| 2230 | + {Py_tp_new, s_new}, | ||
| 2224 | 2231 | {Py_tp_free, PyObject_GC_Del}, | |
| 2225 | 2232 | {0, 0}, | |
| 2226 | 2233 | }; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments