mirror of
https://github.com/python/cpython.git
synced 2025-08-09 03:19:15 +00:00
[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 commitc8c0afc713
(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 commit9fe60340d7
) Co-authored-by: Mark Dickinson <dickinsm@gmail.com> * Remove unrelated test
This commit is contained in:
parent
d7a7883326
commit
42df73652d
4 changed files with 53 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,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).
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue