[3.12] gh-112358: Fix Python 3.12 regression with subclassing struct.Struct (GH-112424) (#112426)

* [3.12] gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. (GH-112424)

Revert commit c8c0afc713 (PR GH-94532),
which moved `struct.Struct` initialisation from `Struct.__init__` to `Struct.__new__`.
This caused issues with code in the wild that subclasses `struct.Struct`..
(cherry picked from commit 9fe60340d7)

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>

* Remove unrelated test
This commit is contained in:
Mark Dickinson 2023-11-27 08:25:06 +00:00 committed by GitHub
parent d7a7883326
commit 42df73652d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 48 deletions

View file

@ -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,16 @@ 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')
class UnpackIteratorTest(unittest.TestCase):
"""
Tests for iterative unpacking (struct.Struct.iter_unpack).