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 from CPython 3.10.6
  • Loading branch information
CPython Developers authored and youknowone committed Aug 14, 2022
commit 193fa88ea91abcf64c1e944d080e6dcacabf339a
60 changes: 60 additions & 0 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from collections import abc
import array
import gc
import math
import operator
import unittest
import struct
import sys
import weakref

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

ISBIGENDIAN = sys.byteorder == "big"

Expand Down Expand Up @@ -654,6 +658,58 @@ def test_format_attr(self):
s2 = struct.Struct(s.format.encode())
self.assertEqual(s2.format, s.format)

def test_struct_cleans_up_at_runtime_shutdown(self):
code = """if 1:
import struct

class C:
def __init__(self):
self.pack = struct.pack
def __del__(self):
self.pack('I', -42)

struct.x = C()
"""
rc, stdout, stderr = assert_python_ok("-c", code)
self.assertEqual(rc, 0)
self.assertEqual(stdout.rstrip(), b"")
self.assertIn(b"Exception ignored in:", stderr)
self.assertIn(b"C.__del__", stderr)

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

# When we create a new struct module, trigger use of its cache,
# and then delete it ...
_struct_module = import_helper.import_fresh_module("_struct")
module_ref = weakref.ref(_struct_module)
_struct_module.calcsize("b")
del _struct_module

# Then the module should have been garbage collected.
gc.collect()
self.assertIsNone(
module_ref(), "_struct module was not garbage collected")

@support.cpython_only
def test__struct_types_immutable(self):
# See https://github.com/python/cpython/issues/94254

Struct = struct.Struct
unpack_iterator = type(struct.iter_unpack("b", b'x'))
for cls in (Struct, unpack_iterator):
with self.subTest(cls=cls):
with self.assertRaises(TypeError):
cls.x = 1


def test_issue35714(self):
# Embedded null characters should not be allowed in format strings.
for s in '\0', '2\0i', b'\0':
with self.assertRaisesRegex(struct.error,
'embedded null character'):
struct.calcsize(s)


class UnpackIteratorTest(unittest.TestCase):
"""
Expand Down Expand Up @@ -681,6 +737,10 @@ def _check_iterator(it):
with self.assertRaises(struct.error):
s.iter_unpack(b"12")

def test_uninstantiable(self):
iter_unpack_type = type(struct.Struct(">ibcp").iter_unpack(b""))
self.assertRaises(TypeError, iter_unpack_type)

def test_iterate(self):
s = struct.Struct('>IB')
b = bytes(range(1, 16))
Expand Down