Files
RustPython/Lib/test/test_structseq.py
T
Jeong, YunWonandLee Dogeon afad92bf1b Add OrderedDict, marshal, inspect, and chflags bitflagset (#8780)
* Implement _collections.OrderedDict

Extracted from #6799: native OrderedDict as a dict subclass,
iterator mutation tracking, and mapping-view set operations.

* Add OrderedDict, warnings source lines, and socket sendmsg

Implement _collections.OrderedDict as a dict subclass with a
linked list of insertion-order nodes.

Pack struct F/D as native float/double complex pairs, and report
unexpected __replace__ field names as a list repr.

Register encodings.latin_1 at startup. Reject NUL in input()
prompts. Copy non-exact type dicts in mapping order.

Fetch warning source lines through _bless_my_loader and unicode
splitlines. Keep the last warnings.filters list after deletion.

Snapshot sendmsg sequences before converting buffers, parse
ancillary ints with __index__, and dispatch AF_CAN by protocol.

Store sys.trace and sys.profile on each ThreadSlot so
_settraceallthreads/_setprofileallthreads are visible to
sys.gettrace/getprofile on other threads.

Use <frozen importlib._bootstrap> as frozen importlib
co_filename. Keep HEAPTYPE socket objects GC-tracked.

Assisted-by: Grok:grok-4.6

* Load marshal via readinto and dump sets in dumps order

marshal.load fills a scratch buffer with readinto on a
contiguous writable memoryview, and rejects a returned length
other than the requested size.

set and frozenset dumps write elements in sorted(v,
key=marshal.dumps) order.

Assisted-by: Grok:grok-4.6

* Add classmethod_descriptor and ABC IS_ABSTRACT flags

Give METH_CLASS methods a distinct classmethod_descriptor type.
Store Py_TPFLAGS_IS_ABSTRACT from __abstractmethods__ and look
up that attribute only in the type's own dict.

Expose defaultdict.default_factory as a member descriptor.
Inherit bool's unused number slots from int. Mark set and
frozenset __contains__ as METH_COEXIST.

Parse classmethod, staticmethod, object.__new__, and heap type
text signatures. Register interactive source in linecache.

Assisted-by: Grok:grok-4.6

* address review: odict reuse, descr call, new wrapper

Reuse odict node slots and walk the live list in next_after.
Require both | operands to be dicts. Drop the odict iterator
on GC clear. Bind classmethod descriptors through descr_get
and update IS_ABSTRACT under the type lock. Give odict keys
and items sq_contains. Walk tp_new_wrapper past new_wrapper
so HEAPTYPE socket subclasses can call socket.__new__.
Split FreeBSD Bluetooth protocol constants from Linux values.

Assisted-by: Grok:grok-4.6

* Represent chflags bits with bitflagset

Assisted-by: Grok:grok-4.6

* Represent access and mode bits with bitflagset

Assisted-by: Grok:grok-4.6

* Continue unfinished REPL suites as incomplete input

Assisted-by: Grok:grok-4.6

* Add pickle and os.stat inspect signatures

Give _pickle.Pickler the clinic constructor text signature and
accept fix_imports and buffer_callback as positional-or-keyword.
Allow #[pyfunction(text_signature)] and use it so os.stat reports
(path, *, dir_fd=None, follow_symlinks=True). Uncomment import
_pickle and drop the pickle inspect expectedFailures.

Assisted-by: Grok:grok-4.6

* Add Unpickler, dump/load, and lstat signatures

Give _pickle Unpickler, dump, dumps, load, and loads clinic
text signatures, and os.lstat (path, *, dir_fd=None).

Assisted-by: Grok:grok-4.6

* Complete OrderedDict with insertion-order links

Move the native OrderedDict into ordered_dict.rs and track order
with a linked list of nodes, keeping mapping-view set operations.

Assisted-by: Grok:grok-4.6

* address CI: stat casts, HCI_DATA_DIR, passing xfails

Compare stat permission bits as Mode so Linux clippy does not
flag a u32-to-u32 cast. Export HCI_DATA_DIR with the Bluetooth
socket option values. Drop expectedFailure from tests that now
pass.

Assisted-by: Grok:grok-4.6

* Fix wasm teardown deadlock and bluetooth constants

Assisted-by: Grok:grok-4.6

* Clear process-wide eval breaker bits in STW test

Assisted-by: Grok:grok-4.6

* Drop exclusive-held panic that tripped wasm clippy

Assisted-by: Grok:grok-4.6

* Fix OrderedDict contains and export Windows BDADDR

Assisted-by: Grok:grok-4.6

* Add cmp_to_key and hash-once LRU cache

_functools.cmp_to_key now returns KeyWrapper. LRU lookups store the
key hash on _lru_list_elem so __hash__ is not called again on hits
or eviction.

Assisted-by: Grok:grok-4.6

* Allow subclass keywords on list, tuple, and float

list.__init__ rejects keywords only when tp_new is still list's.
tuple/float.__new__ reject them only when tp_init is still the base's.

Assisted-by: Grok:grok-4.6

* Fix CI lint, test re-export, and structseq members

Drop the duplicate clear_eval_breaker_for_test re-export.
Format structseq imports. Expose struct sequence fields as
readonly members so assignment raises "readonly attribute".

Assisted-by: Grok:grok-4.6

* Allow subclass keywords on itertools and filter

Positional-only constructors ignore extra keywords when a subclass
has replaced tp_init, matching list/tuple/float.

Assisted-by: Grok:grok-4.6

* Remove text_signature derive parameter

Drop the string override on #[pyfunction] and #[pyclass]. Signatures
are generated from the Rust parameter list only. Restore inspect
expectedFailures that depended on the override.

Assisted-by: Grok:grok-4.6

* Rename set contains_method to __contains__

Keep a borrowed contains helper for slots and C-API. The coexist
pymethod is named __contains__.

Assisted-by: Grok:grok-4.6

* Use MonitoringEvent instead of EVENT_* aliases

Drop the unused MonitoringEvents bitflags and the EVENT_PY_START
style mask constants. Call sites use the enum and .mask().

Assisted-by: Grok:grok-4.6

* Store warnings filters as PyListRef

The list already serializes its elements. Drop the extra mutex around
the cached list pointer.

Assisted-by: Grok:grok-4.6

* Re-export clear_eval_breaker_for_test after rebase

Keep the STW test helper visible without bringing back stop_bit_set.

Assisted-by: Grok:grok-4.6

* Fix groupby reentrancy and islice arguments

Give _grouper its own tgtkey and take currvalue after the key
comparison. Accept __index__ on islice bounds, reject step 0, and
drain the source up to stop.

Assisted-by: Grok:grok-4.6

* SETREF warnings filters, onceregistry, and defaultaction

Keep the last list, dict, or str after the module attributes are
deleted by swapping the interpreter cache pointer. Do not store a
None fallback for _warnings_context.

Assisted-by: Grok:grok-4.6

* Iterate OrderedDict inner dict via next_entry

&PyDict is not an iterator after the borrowed-ref signature change.

Assisted-by: Grok:grok-4.6

* Remove expectedFailure from testBluetoothConstants

Linux now exports BTPROTO_HCI and the rest of the set.

Assisted-by: Grok:grok-4.6

* Borrow get_filter arguments as PyObject

Match the warn_explicit call site that passes borrowed
handles after the internal-helper borrow change.

Assisted-by: Grok:grok-4.6

* Fix bool slots and SemHandle safety docs

Leave unused bool number slots unset so inspect reports
__add__ on int. Document SemHandle::from_raw safety.

Assisted-by: Grok:grok-4.6

* Keep sendmsg errors and drop duplicate linecache

Replace sendmsg iterable TypeError only. Register REPL
source in linecache once per compiled input.

Assisted-by: Grok:grok-4.6

* Nest REPL parse or-patterns for clippy

Assisted-by: Grok:grok-4.6

* Mark pydoc os.stat signature test expectedFailure

Assisted-by: Grok:grok-4.6

---------

Co-authored-by: Lee Dogeon <dev.moreal@gmail.com>
2026-09-24 12:21:26 +09:00

370 lines
14 KiB
Python
Vendored

import copy
import gc
import os
import pickle
import re
import textwrap
import time
import unittest
from test.support import script_helper
class StructSeqTest(unittest.TestCase):
def test_tuple(self):
t = time.gmtime()
self.assertIsInstance(t, tuple)
astuple = tuple(t)
self.assertEqual(len(t), len(astuple))
self.assertEqual(t, astuple)
# Check that slicing works the same way; at one point, slicing t[i:j] with
# 0 < i < j could produce NULLs in the result.
for i in range(-len(t), len(t)):
self.assertEqual(t[i:], astuple[i:])
for j in range(-len(t), len(t)):
self.assertEqual(t[i:j], astuple[i:j])
for j in range(-len(t), len(t)):
self.assertEqual(t[:j], astuple[:j])
self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
self.assertRaises(IndexError, t.__getitem__, len(t))
for i in range(-len(t), len(t)-1):
self.assertEqual(t[i], astuple[i])
def test_repr(self):
t = time.gmtime()
self.assertTrue(repr(t))
t = time.gmtime(0)
self.assertEqual(repr(t),
"time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, "
"tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)")
# os.stat() gives a complicated struct sequence.
st = os.stat(__file__)
rep = repr(st)
self.assertStartsWith(rep, "os.stat_result")
self.assertIn("st_mode=", rep)
self.assertIn("st_ino=", rep)
self.assertIn("st_dev=", rep)
def test_concat(self):
t1 = time.gmtime()
t2 = t1 + tuple(t1)
for i in range(len(t1)):
self.assertEqual(t2[i], t2[i+len(t1)])
def test_repeat(self):
t1 = time.gmtime()
t2 = 3 * t1
for i in range(len(t1)):
self.assertEqual(t2[i], t2[i+len(t1)])
self.assertEqual(t2[i], t2[i+2*len(t1)])
def test_contains(self):
t1 = time.gmtime()
for item in t1:
self.assertIn(item, t1)
self.assertNotIn(-42, t1)
def test_hash(self):
t1 = time.gmtime()
self.assertEqual(hash(t1), hash(tuple(t1)))
def test_cmp(self):
t1 = time.gmtime()
t2 = type(t1)(t1)
self.assertEqual(t1, t2)
self.assertTrue(not (t1 < t2))
self.assertTrue(t1 <= t2)
self.assertTrue(not (t1 > t2))
self.assertTrue(t1 >= t2)
self.assertTrue(not (t1 != t2))
def test_fields(self):
t = time.gmtime()
self.assertEqual(len(t), t.n_sequence_fields)
self.assertEqual(t.n_unnamed_fields, 0)
self.assertEqual(t.n_fields, time._STRUCT_TM_ITEMS)
def test_constructor(self):
t = time.struct_time
self.assertRaises(TypeError, t)
self.assertRaises(TypeError, t, None)
self.assertRaises(TypeError, t, "123")
self.assertRaises(TypeError, t, "123", dict={})
self.assertRaises(TypeError, t, "123456789", dict=None)
self.assertRaises(TypeError, t, seq="123456789", dict={})
self.assertEqual(t("123456789"), tuple("123456789"))
self.assertEqual(t("123456789", {}), tuple("123456789"))
self.assertEqual(t("123456789", dict={}), tuple("123456789"))
self.assertEqual(t(sequence="123456789", dict={}), tuple("123456789"))
self.assertEqual(t("1234567890"), tuple("123456789"))
self.assertEqual(t("1234567890").tm_zone, "0")
self.assertEqual(t("123456789", {"tm_zone": "some zone"}), tuple("123456789"))
self.assertEqual(t("123456789", {"tm_zone": "some zone"}).tm_zone, "some zone")
s = "123456789"
self.assertEqual("".join(t(s)), s)
def test_constructor_with_duplicate_fields(self):
t = time.struct_time
error_message = re.escape("got duplicate or unexpected field name(s)")
with self.assertRaisesRegex(TypeError, error_message):
t("1234567890", dict={"tm_zone": "some zone"})
with self.assertRaisesRegex(TypeError, error_message):
t("1234567890", dict={"tm_zone": "some zone", "tm_mon": 1})
with self.assertRaisesRegex(TypeError, error_message):
t("1234567890", dict={"error": 0, "tm_zone": "some zone"})
with self.assertRaisesRegex(TypeError, error_message):
t("1234567890", dict={"error": 0, "tm_zone": "some zone", "tm_mon": 1})
def test_constructor_with_duplicate_unnamed_fields(self):
assert os.stat_result.n_unnamed_fields > 0
n_visible_fields = os.stat_result.n_sequence_fields
r = os.stat_result(range(n_visible_fields), {'st_atime': -1.0})
self.assertEqual(r.st_atime, -1.0)
self.assertEqual(r, tuple(range(n_visible_fields)))
r = os.stat_result((*range(n_visible_fields), -1.0))
self.assertEqual(r.st_atime, -1.0)
self.assertEqual(r, tuple(range(n_visible_fields)))
with self.assertRaisesRegex(TypeError,
re.escape("got duplicate or unexpected field name(s)")):
os.stat_result((*range(n_visible_fields), -1.0), {'st_atime': -1.0})
def test_constructor_with_unknown_fields(self):
t = time.struct_time
error_message = re.escape("got duplicate or unexpected field name(s)")
with self.assertRaisesRegex(TypeError, error_message):
t("123456789", dict={"tm_year": 0})
with self.assertRaisesRegex(TypeError, error_message):
t("123456789", dict={"tm_year": 0, "tm_mon": 1})
with self.assertRaisesRegex(TypeError, error_message):
t("123456789", dict={"tm_zone": "some zone", "tm_mon": 1})
with self.assertRaisesRegex(TypeError, error_message):
t("123456789", dict={"tm_zone": "some zone", "error": 0})
with self.assertRaisesRegex(TypeError, error_message):
t("123456789", dict={"error": 0, "tm_zone": "some zone", "tm_mon": 1})
with self.assertRaisesRegex(TypeError, error_message):
t("123456789", dict={"error": 0})
with self.assertRaisesRegex(TypeError, error_message):
t("123456789", dict={"tm_zone": "some zone", "error": 0})
def test_eviltuple(self):
class Exc(Exception):
pass
# Devious code could crash structseqs' constructors
class C:
def __getitem__(self, i):
raise Exc
def __len__(self):
return 9
self.assertRaises(Exc, time.struct_time, C())
def test_pickling(self):
t = time.gmtime()
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
p = pickle.dumps(t, proto)
t2 = pickle.loads(p)
self.assertEqual(t2.__class__, t.__class__)
self.assertEqual(t2, t)
self.assertEqual(t2.tm_year, t.tm_year)
self.assertEqual(t2.tm_zone, t.tm_zone)
def test_pickling_with_unnamed_fields(self):
assert os.stat_result.n_unnamed_fields > 0
r = os.stat_result(range(os.stat_result.n_sequence_fields),
{'st_atime': 1.0, 'st_atime_ns': 2.0})
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
p = pickle.dumps(r, proto)
r2 = pickle.loads(p)
self.assertEqual(r2.__class__, r.__class__)
self.assertEqual(r2, r)
self.assertEqual(r2.st_mode, r.st_mode)
self.assertEqual(r2.st_atime, r.st_atime)
self.assertEqual(r2.st_atime_ns, r.st_atime_ns)
def test_copying(self):
n_fields = time.struct_time.n_fields
t = time.struct_time([[i] for i in range(n_fields)])
t2 = copy.copy(t)
self.assertEqual(t2.__class__, t.__class__)
self.assertEqual(t2, t)
self.assertEqual(t2.tm_year, t.tm_year)
self.assertEqual(t2.tm_zone, t.tm_zone)
self.assertIs(t2[0], t[0])
self.assertIs(t2.tm_year, t.tm_year)
t3 = copy.deepcopy(t)
self.assertEqual(t3.__class__, t.__class__)
self.assertEqual(t3, t)
self.assertEqual(t3.tm_year, t.tm_year)
self.assertEqual(t3.tm_zone, t.tm_zone)
self.assertIsNot(t3[0], t[0])
self.assertIsNot(t3.tm_year, t.tm_year)
def test_copying_with_unnamed_fields(self):
assert os.stat_result.n_unnamed_fields > 0
n_sequence_fields = os.stat_result.n_sequence_fields
r = os.stat_result([[i] for i in range(n_sequence_fields)],
{'st_atime': [1.0], 'st_atime_ns': [2.0]})
r2 = copy.copy(r)
self.assertEqual(r2.__class__, r.__class__)
self.assertEqual(r2, r)
self.assertEqual(r2.st_mode, r.st_mode)
self.assertEqual(r2.st_atime, r.st_atime)
self.assertEqual(r2.st_atime_ns, r.st_atime_ns)
self.assertIs(r2[0], r[0])
self.assertIs(r2.st_mode, r.st_mode)
self.assertIs(r2.st_atime, r.st_atime)
self.assertIs(r2.st_atime_ns, r.st_atime_ns)
r3 = copy.deepcopy(r)
self.assertEqual(r3.__class__, r.__class__)
self.assertEqual(r3, r)
self.assertEqual(r3.st_mode, r.st_mode)
self.assertEqual(r3.st_atime, r.st_atime)
self.assertEqual(r3.st_atime_ns, r.st_atime_ns)
self.assertIsNot(r3[0], r[0])
self.assertIsNot(r3.st_mode, r.st_mode)
self.assertIsNot(r3.st_atime, r.st_atime)
self.assertIsNot(r3.st_atime_ns, r.st_atime_ns)
def test_extended_getslice(self):
# Test extended slicing by comparing with list slicing.
t = time.gmtime()
L = list(t)
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
for start in indices:
for stop in indices:
# Skip step 0 (invalid)
for step in indices[1:]:
self.assertEqual(list(t[start:stop:step]),
L[start:stop:step])
def test_match_args(self):
expected_args = ('tm_year', 'tm_mon', 'tm_mday', 'tm_hour', 'tm_min',
'tm_sec', 'tm_wday', 'tm_yday', 'tm_isdst')
self.assertEqual(time.struct_time.__match_args__, expected_args)
def test_match_args_with_unnamed_fields(self):
expected_args = ('st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid',
'st_gid', 'st_size')
self.assertEqual(os.stat_result.n_unnamed_fields, 3)
self.assertEqual(os.stat_result.__match_args__, expected_args)
def test_copy_replace_all_fields_visible(self):
assert os.times_result.n_unnamed_fields == 0
assert os.times_result.n_sequence_fields == os.times_result.n_fields
t = os.times()
# visible fields
self.assertEqual(copy.replace(t), t)
self.assertIsInstance(copy.replace(t), os.times_result)
self.assertEqual(copy.replace(t, user=1.5), (1.5, *t[1:]))
self.assertEqual(copy.replace(t, system=2.5), (t[0], 2.5, *t[2:]))
self.assertEqual(copy.replace(t, user=1.5, system=2.5), (1.5, 2.5, *t[2:]))
# unknown fields
with self.assertRaisesRegex(TypeError, 'unexpected field name'):
copy.replace(t, error=-1)
with self.assertRaisesRegex(TypeError, 'unexpected field name'):
copy.replace(t, user=1, error=-1)
def test_copy_replace_with_invisible_fields(self):
assert time.struct_time.n_unnamed_fields == 0
assert time.struct_time.n_sequence_fields < time.struct_time.n_fields
t = time.gmtime(0)
# visible fields
t2 = copy.replace(t)
self.assertEqual(t2, (1970, 1, 1, 0, 0, 0, 3, 1, 0))
self.assertIsInstance(t2, time.struct_time)
t3 = copy.replace(t, tm_year=2000)
self.assertEqual(t3, (2000, 1, 1, 0, 0, 0, 3, 1, 0))
self.assertEqual(t3.tm_year, 2000)
t4 = copy.replace(t, tm_mon=2)
self.assertEqual(t4, (1970, 2, 1, 0, 0, 0, 3, 1, 0))
self.assertEqual(t4.tm_mon, 2)
t5 = copy.replace(t, tm_year=2000, tm_mon=2)
self.assertEqual(t5, (2000, 2, 1, 0, 0, 0, 3, 1, 0))
self.assertEqual(t5.tm_year, 2000)
self.assertEqual(t5.tm_mon, 2)
# named invisible fields
self.assertHasAttr(t, 'tm_zone')
with self.assertRaisesRegex(AttributeError, 'readonly attribute'):
t.tm_zone = 'some other zone'
self.assertEqual(t2.tm_zone, t.tm_zone)
self.assertEqual(t3.tm_zone, t.tm_zone)
self.assertEqual(t4.tm_zone, t.tm_zone)
t6 = copy.replace(t, tm_zone='some other zone')
self.assertEqual(t, t6)
self.assertEqual(t6.tm_zone, 'some other zone')
t7 = copy.replace(t, tm_year=2000, tm_zone='some other zone')
self.assertEqual(t7, (2000, 1, 1, 0, 0, 0, 3, 1, 0))
self.assertEqual(t7.tm_year, 2000)
self.assertEqual(t7.tm_zone, 'some other zone')
# unknown fields
with self.assertRaisesRegex(TypeError, 'unexpected field name'):
copy.replace(t, error=2)
with self.assertRaisesRegex(TypeError, 'unexpected field name'):
copy.replace(t, tm_year=2000, error=2)
with self.assertRaisesRegex(TypeError, 'unexpected field name'):
copy.replace(t, tm_zone='some other zone', error=2)
def test_copy_replace_with_unnamed_fields(self):
assert os.stat_result.n_unnamed_fields > 0
r = os.stat_result(range(os.stat_result.n_sequence_fields))
error_message = re.escape('__replace__() is not supported')
with self.assertRaisesRegex(TypeError, error_message):
copy.replace(r)
with self.assertRaisesRegex(TypeError, error_message):
copy.replace(r, st_mode=1)
with self.assertRaisesRegex(TypeError, error_message):
copy.replace(r, error=2)
with self.assertRaisesRegex(TypeError, error_message):
copy.replace(r, st_mode=1, error=2)
def test_reference_cycle(self):
# gh-122527: Check that a structseq that's part of a reference cycle
# with its own type doesn't crash. Previously, if the type's dictionary
# was cleared first, the structseq instance would crash in the
# destructor.
script_helper.assert_python_ok("-c", textwrap.dedent(r"""
import time
t = time.gmtime()
type(t).refcyle = t
"""))
def test_replace_gc_tracked(self):
# Verify that __replace__ results are properly GC-tracked
time_struct = time.gmtime(0)
lst = []
replaced_struct = time_struct.__replace__(tm_year=lst)
lst.append(replaced_struct)
self.assertTrue(gc.is_tracked(replaced_struct))
if __name__ == "__main__":
unittest.main()