diff options
| author | Mark Dickinson <dickinsm@gmail.com> | 2023-11-26 14:29:52 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-26 14:29:52 (GMT) |
| commit | 9fe60340d7e8dc22b3aec205c557bc69a1b2d18c (patch) | |
| tree | 9047eeb509b3a097caf58d9168ac719454cbb1fe /Lib/test/test_struct.py | |
| parent | 3faf8e586d36e73faba13d9b61663afed6a24cb4 (diff) | |
| download | cpython-9fe60340d7e8dc22b3aec205c557bc69a1b2d18c.zip cpython-9fe60340d7e8dc22b3aec205c557bc69a1b2d18c.tar.gz cpython-9fe60340d7e8dc22b3aec205c557bc69a1b2d18c.tar.bz2 | |
gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. (#112424)
Revert commit c8c0afc7137ab9f22bf59d591084948ca967c97c (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`.
Diffstat (limited to 'Lib/test/test_struct.py')
| -rw-r--r-- | Lib/test/test_struct.py | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index c76649c..15f6ee0 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -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})') |
