Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update test_struct to 3.13.3
  • Loading branch information
arihant2math committed Apr 15, 2025
commit 844090b0b8413d25d94a80be0c683e23d347f33a
85 changes: 83 additions & 2 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import weakref

from test import support
from test.support import import_helper
from test.support import import_helper, suppress_immortalization
from test.support.script_helper import assert_python_ok

ISBIGENDIAN = sys.byteorder == "big"
Expand Down Expand Up @@ -96,6 +96,13 @@ def test_new_features(self):
('10s', b'helloworld', b'helloworld', b'helloworld', 0),
('11s', b'helloworld', b'helloworld\0', b'helloworld\0', 1),
('20s', b'helloworld', b'helloworld'+10*b'\0', b'helloworld'+10*b'\0', 1),
('0p', b'helloworld', b'', b'', 1),
('1p', b'helloworld', b'\x00', b'\x00', 1),
('2p', b'helloworld', b'\x01h', b'\x01h', 1),
('10p', b'helloworld', b'\x09helloworl', b'\x09helloworl', 1),
('11p', b'helloworld', b'\x0Ahelloworld', b'\x0Ahelloworld', 0),
('12p', b'helloworld', b'\x0Ahelloworld\0', b'\x0Ahelloworld\0', 1),
('20p', b'helloworld', b'\x0Ahelloworld'+9*b'\0', b'\x0Ahelloworld'+9*b'\0', 1),
('b', 7, b'\7', b'\7', 0),
('b', -7, b'\371', b'\371', 0),
('B', 7, b'\7', b'\7', 0),
Expand Down Expand Up @@ -339,6 +346,7 @@ def assertStructError(func, *args, **kwargs):
def test_p_code(self):
# Test p ("Pascal string") code.
for code, input, expected, expectedback in [
('0p', b'abc', b'', b''),
('p', b'abc', b'\x00', b''),
('1p', b'abc', b'\x00', b''),
('2p', b'abc', b'\x01a', b'a'),
Expand Down Expand Up @@ -523,6 +531,9 @@ def __bool__(self):

for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
self.assertTrue(struct.unpack('>?', c)[0])
self.assertTrue(struct.unpack('<?', c)[0])
self.assertTrue(struct.unpack('=?', c)[0])
self.assertTrue(struct.unpack('@?', c)[0])

def test_count_overflow(self):
hugecount = '{}b'.format(sys.maxsize+1)
Expand Down Expand Up @@ -582,6 +593,7 @@ def test__sizeof__(self):
self.check_sizeof('187s', 1)
self.check_sizeof('20p', 1)
self.check_sizeof('0s', 1)
self.check_sizeof('0p', 1)
self.check_sizeof('0c', 0)

def test_boundary_error_message(self):
Expand Down Expand Up @@ -678,6 +690,7 @@ def __del__(self):
self.assertIn(b"Exception ignored in:", stderr)
self.assertIn(b"C.__del__", stderr)

@suppress_immortalization()
def test__struct_reference_cycle_cleaned_up(self):
# Regression test for python/cpython#94207.

Expand Down Expand Up @@ -714,6 +727,74 @@ def test_issue35714(self):
'embedded null character'):
struct.calcsize(s)

@support.cpython_only
def test_issue98248(self):
def test_error_msg(prefix, int_type, is_unsigned):
fmt_str = prefix + int_type
size = struct.calcsize(fmt_str)
if is_unsigned:
max_ = 2 ** (size * 8) - 1
min_ = 0
else:
max_ = 2 ** (size * 8 - 1) - 1
min_ = -2 ** (size * 8 - 1)
error_msg = f"'{int_type}' format requires {min_} <= number <= {max_}"
for number in [int(-1e50), min_ - 1, max_ + 1, int(1e50)]:
with self.subTest(format_str=fmt_str, number=number):
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack(fmt_str, number)
error_msg = "required argument is not an integer"
not_number = ""
with self.subTest(format_str=fmt_str, number=not_number):
with self.assertRaisesRegex(struct.error, error_msg):
struct.pack(fmt_str, not_number)

for prefix in '@=<>':
for int_type in 'BHILQ':
test_error_msg(prefix, int_type, True)
for int_type in 'bhilq':
test_error_msg(prefix, int_type, False)

int_type = 'N'
test_error_msg('@', int_type, True)

int_type = 'n'
test_error_msg('@', int_type, False)

@support.cpython_only
def test_issue98248_error_propagation(self):
class Div0:
def __index__(self):
1 / 0

def test_error_propagation(fmt_str):
with self.subTest(format_str=fmt_str, exception="ZeroDivisionError"):
with self.assertRaises(ZeroDivisionError):
struct.pack(fmt_str, Div0())

for prefix in '@=<>':
for int_type in 'BHILQbhilq':
test_error_propagation(prefix + int_type)

test_error_propagation('N')
test_error_propagation('n')

# TODO: RUSTPYTHON
@unittest.expectedFailure
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')

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_repr(self):
s = struct.Struct('=i2H')
self.assertEqual(repr(s), f'Struct({s.format!r})')

class UnpackIteratorTest(unittest.TestCase):
"""
Expand Down Expand Up @@ -895,4 +976,4 @@ def test_half_float(self):


if __name__ == '__main__':
unittest.main()
unittest.main()
Loading