Update test_struct.py from 3.14.3

This commit is contained in:
ShaharNaveh authored and Jeong, YunWon committed 2026-02-14 22:02:24 +09:00
1 parent 28b2506b13
commit c43ad469ae
1 file changed
+58 -39
+58 -39
View File
@@ -5,7 +5,6 @@ import gc
import math
import operator
import unittest
import platform
import struct
import sys
import weakref
@@ -435,41 +434,11 @@ class StructTest(ComplexesAreIdenticalMixin, unittest.TestCase):
self.assertEqual(s.unpack_from(buffer=test_string, offset=2),
(b'cd01',))
def test_pack_into(self):
def _test_pack_into(self, pack_into):
test_string = b'Reykjavik rocks, eow!'
writable_buf = array.array('b', b' '*100)
fmt = '21s'
s = struct.Struct(fmt)
writable_buf = memoryview(array.array('b', b' '*100))
# Test without offset
s.pack_into(writable_buf, 0, test_string)
from_buf = writable_buf.tobytes()[:len(test_string)]
self.assertEqual(from_buf, test_string)
# Test with offset.
s.pack_into(writable_buf, 10, test_string)
from_buf = writable_buf.tobytes()[:len(test_string)+10]
self.assertEqual(from_buf, test_string[:10] + test_string)
# Go beyond boundaries.
small_buf = array.array('b', b' '*10)
self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 0,
test_string)
self.assertRaises((ValueError, struct.error), s.pack_into, small_buf, 2,
test_string)
# Test bogus offset (issue 3694)
sb = small_buf
self.assertRaises((TypeError, struct.error), struct.pack_into, b'', sb,
None)
def test_pack_into_fn(self):
test_string = b'Reykjavik rocks, eow!'
writable_buf = array.array('b', b' '*100)
fmt = '21s'
pack_into = lambda *args: struct.pack_into(fmt, *args)
# Test without offset.
pack_into(writable_buf, 0, test_string)
from_buf = writable_buf.tobytes()[:len(test_string)]
self.assertEqual(from_buf, test_string)
@@ -479,12 +448,49 @@ class StructTest(ComplexesAreIdenticalMixin, unittest.TestCase):
from_buf = writable_buf.tobytes()[:len(test_string)+10]
self.assertEqual(from_buf, test_string[:10] + test_string)
# Test with negative offset.
pack_into(writable_buf, -30, test_string)
from_buf = writable_buf.tobytes()[-30:-30+len(test_string)]
self.assertEqual(from_buf, test_string)
# Go beyond boundaries.
small_buf = array.array('b', b' '*10)
self.assertRaises((ValueError, struct.error), pack_into, small_buf, 0,
test_string)
self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(small_buf, 0, test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(writable_buf, 90, test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(writable_buf, -10, test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(writable_buf, 150, test_string)
with self.assertRaises((ValueError, struct.error)):
pack_into(writable_buf, -150, test_string)
# Test invalid buffer.
self.assertRaises(TypeError, pack_into, b' '*100, 0, test_string)
self.assertRaises(TypeError, pack_into, ' '*100, 0, test_string)
self.assertRaises(TypeError, pack_into, [0]*100, 0, test_string)
self.assertRaises(TypeError, pack_into, None, 0, test_string)
self.assertRaises(TypeError, pack_into, writable_buf[::2], 0, test_string)
self.assertRaises(TypeError, pack_into, writable_buf[::-1], 0, test_string)
# Test bogus offset (issue bpo-3694)
with self.assertRaises(TypeError):
pack_into(writable_buf, None, test_string)
with self.assertRaises(TypeError):
pack_into(writable_buf, 0.0, test_string)
with self.assertRaises((IndexError, OverflowError)):
pack_into(writable_buf, 2**1000, test_string)
with self.assertRaises((IndexError, OverflowError)):
pack_into(writable_buf, -2**1000, test_string)
def test_pack_into(self):
s = struct.Struct('21s')
self._test_pack_into(s.pack_into)
def test_pack_into_fn(self):
pack_into = lambda *args: struct.pack_into('21s', *args)
self._test_pack_into(pack_into)
def test_unpack_with_buffer(self):
# SF bug 1563759: struct.unpack doesn't support buffer protocol objects
@@ -821,6 +827,18 @@ class StructTest(ComplexesAreIdenticalMixin, unittest.TestCase):
results = executor.map(exec, [code] * 5)
self.assertListEqual(list(results), [None] * 5)
def test_operations_on_half_initialized_Struct(self):
S = struct.Struct.__new__(struct.Struct)
spam = array.array('b', b' ')
self.assertRaises(RuntimeError, S.iter_unpack, spam)
self.assertRaises(RuntimeError, S.pack, 1)
self.assertRaises(RuntimeError, S.pack_into, spam, 1)
self.assertRaises(RuntimeError, S.unpack, spam)
self.assertRaises(RuntimeError, S.unpack_from, spam)
self.assertRaises(RuntimeError, getattr, S, 'format')
self.assertEqual(S.size, -1)
class UnpackIteratorTest(unittest.TestCase):
"""
@@ -895,6 +913,7 @@ class UnpackIteratorTest(unittest.TestCase):
self.assertRaises(StopIteration, next, it)
def test_half_float(self):
_testcapi = import_helper.import_module('_testcapi')
# Little-endian examples from:
# http://en.wikipedia.org/wiki/Half_precision_floating-point_format
format_bits_float__cleanRoundtrip_list = [
@@ -939,8 +958,8 @@ class UnpackIteratorTest(unittest.TestCase):
# Check that packing produces a bit pattern representing a quiet NaN:
# all exponent bits and the msb of the fraction should all be 1.
if platform.machine().startswith('parisc'):
# HP PA RISC uses 0 for quiet, see:
if _testcapi.nan_msb_is_signaling:
# HP PA RISC and some MIPS CPUs use 0 for quiet, see:
# https://en.wikipedia.org/wiki/NaN#Encoding
expected = 0x7c
else: