mirror of
https://github.com/python/cpython.git
synced 2025-07-08 03:45:36 +00:00
gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. (#112424)
Revert commit c8c0afc713
(PR #94532),
which moved `struct.Struct` initialisation from `Struct.__init__` to `Struct.__new__`.
This caused issues with code in the wild that subclasses `struct.Struct`.
This commit is contained in:
parent
3faf8e586d
commit
9fe60340d7
4 changed files with 52 additions and 48 deletions
|
@ -700,20 +700,6 @@ class StructTest(unittest.TestCase):
|
|||
with self.assertRaises(TypeError):
|
||||
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):
|
||||
# Embedded null characters should not be allowed in format strings.
|
||||
|
@ -774,6 +760,15 @@ class StructTest(unittest.TestCase):
|
|||
test_error_propagation('N')
|
||||
test_error_propagation('n')
|
||||
|
||||
def test_struct_subclass_instantiation(self):
|
||||
# Regression test for https://github.com/python/cpython/issues/112358
|
||||
class MyStruct(struct.Struct):
|
||||
def __init__(self):
|
||||
super().__init__('>h')
|
||||
|
||||
my_struct = MyStruct()
|
||||
self.assertEqual(my_struct.pack(12345), b'\x30\x39')
|
||||
|
||||
def test_repr(self):
|
||||
s = struct.Struct('=i2H')
|
||||
self.assertEqual(repr(s), f'Struct({s.format!r})')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue