mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
* Target Python 3.15 using the final upstream reference Update active version and comparison configuration to Python 3.15.0. Preserve the alpha implementation status, actual bytecode format, and existing libraries pending separate migration. Align generated library-upgrade workflow pins without changing permissions or actions. Assisted-by: OpenAI Codex:model identifier unavailable * Adapt essential snippet contracts to Python 3.15 final Preserve existing coverage while following verified final behavior for script metadata, builtin signatures and loading, AST fixture construction, memoryview diagnostics, mmap search bounds, and struct format errors. The unchanged final comparator exposes the earlier expectations; focused native baseline failures establish the required implementation changes. Assisted-by: OpenAI Codex:model identifier unavailable * Keep final-reference regression paths and remove expired sysconfig argument Preserve reverse multiplication syntax and native exact overflow diagnostics. Accept the two source-proven CPython 3.15 specialization messages only in affected reverse str/bytes/tuple cases. Use an actually out-of-range Windows datetime errno fixture. Remove the sysconfig check_home argument at its existing canonical 3.15 removal test boundary. Assisted-by: OpenAI Codex:model identifier unavailable * Keep README wording unchanged in Python 3.15 transition Assisted-by: OpenAI Codex:model identifier unavailable * Match Python 3.15 repetition specialization and keep universal tests Use exact sequence-type guards and preserve generic fallback diagnostics. Validate both cold and warmed expressions without implementation-specific assertions. Reject stale CPython source checkouts and cross-version library caches. Assisted-by: OpenAI Codex:model identifier unavailable * Limit in-place specialization additions to exact sequence repetition Assisted-by: OpenAI Codex:model identifier unavailable * Keep subclass regression scoped to repetition dispatch Assisted-by: OpenAI Codex:model identifier unavailable * Provision final CPython 3.15 with pinned uv builds Assisted-by: OpenAI Codex:model identifier unavailable * Enforce Python 3.15 AST constructor requirements Reject retired missing-field and arbitrary-keyword forms while preserving defaults and explicit placeholders. Sync only matching final CPython constructor test hunks. Assisted-by: OpenAI Codex:model identifier unavailable * Keep seeded Python ahead of its Windows DLL directory Assisted-by: OpenAI Codex:model identifier unavailable * Keep opcode metadata tied to the implemented bytecode format The interpreter target and reference tests use Python 3.15.0, but the current Rust opcode enum retains the 3.14 bytecode format. Generate metadata from its matching source rather than importing unsupported instruction families. Assisted-by: OpenAI Codex:model identifier unavailable * Align CSV, logarithm and decimal formatting with Python 3.15 Propagate dialect lookup errors except AttributeError, retry integer logarithms after float overflow, and update the decimal conversion diagnostic. Exercise the final reference contracts without implementation-specific branches. Assisted-by: OpenAI Codex:model identifier unavailable * Bind the logarithm index result before borrowing its integer Assisted-by: OpenAI Codex:model identifier unavailable * Align required canonical test expectations with Python 3.15 Take only demonstrated contract changes from CPython v3.15.0, d220409a13b77de7a6c30abe16ea90db01528edc: removed module __cached__, Struct format exception classes, and decimal-format diagnostic. Preserve unrelated tests and expectations. Assisted-by: OpenAI Codex:model identifier unavailable * Track pending frozendict pickle support during the 3.15 transition Keep the two existing frozendict pickle tests running as expected failures until #8951 supplies the builtin. Unexpected success will signal removal of these temporary markers. Assisted-by: OpenAI Codex:model identifier unavailable * Support final Python 3.15 Windows negative timestamps Use FILETIME for pre-epoch conversions and preserve precise Windows timestamp errors. Assisted-by: OpenAI Codex:model identifier unavailable * Derive historical Windows timezone offsets from wall time FILETIME reports historical DST as unknown, so calculate the actual offset rather than selecting standard-time bias. Assisted-by: OpenAI Codex:model identifier unavailable * Identify decimal formatting arguments in Python 3.15 errors Track consumed positional arguments, including star width and precision, and preserve mapping key representations in decimal conversion diagnostics. Assisted-by: OpenAI Codex:model identifier unavailable * Reuse mapping format keys for lookup and error context Assisted-by: OpenAI Codex:model identifier unavailable * Borrow mapping diagnostic keys as Python objects Assisted-by: OpenAI Codex:model identifier unavailable * Avoid empty components in the reference Python library path Assisted-by: OpenAI Codex:model identifier unavailable * Isolate RustPython bytecode caches during the 3.15 transition Assisted-by: OpenAI Codex:model identifier unavailable * Sync final Windows timestamp test expectations Import only the final CPython platform-limit guards and mktime roundtrip expectation required by the existing Windows CI suite. Assisted-by: OpenAI Codex:model identifier unavailable * Restore official Python setup for 3.15 final Use the existing pinned setup-python action now that its official catalog supplies CPython 3.15.0. Remove the temporary standalone provisioning action and its sparse-checkout entry while retaining interpreter outputs and requested versions. Assisted-by: OpenAI Codex:model identifier unavailable * Align merged runtime contracts with Python 3.15 final Restrict mappingproxy ordering, use final codec registry normalization, and prefer index conversion before float conversion for sleep. Retain universal Python assertions and import the matching final codec/time canonical test hunks plus the non-ASCII codec-name deprecation warning. Upstream source: CPython d220409a13b77de7a6c30abe16ea90db01528edc. The existing codec normalization implementation and bytecode contract remain unchanged. Assisted-by: OpenAI Codex:model identifier unavailable * Limit Windows launcher version check to its interpreter Exclude only the CPython py.exe version-identity case from RustPython CI. The installed launcher reports its own final version, while RustPython correctly remains alpha. Preserve the other 45 launcher cases and all RustPython version assertions without modifying Lib. Assisted-by: OpenAI Codex:model identifier unavailable --------- Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
205 lines
5.2 KiB
Python
205 lines
5.2 KiB
Python
import struct
|
|
|
|
from testutils import assert_raises
|
|
|
|
data = struct.pack("IH", 14, 12)
|
|
assert data == bytes([14, 0, 0, 0, 12, 0])
|
|
|
|
v1, v2 = struct.unpack("IH", data)
|
|
assert v1 == 14
|
|
assert v2 == 12
|
|
|
|
data = struct.pack("<IH", 14, 12)
|
|
assert data == bytes([14, 0, 0, 0, 12, 0])
|
|
|
|
v1, v2 = struct.unpack("<IH", data)
|
|
assert v1 == 14
|
|
assert v2 == 12
|
|
|
|
data = struct.pack(">IH", 14, 12)
|
|
assert data == bytes([0, 0, 0, 14, 0, 12])
|
|
|
|
v1, v2 = struct.unpack(">IH", data)
|
|
assert v1 == 14
|
|
assert v2 == 12
|
|
|
|
data = struct.pack("3B", 65, 66, 67)
|
|
assert data == bytes([65, 66, 67])
|
|
|
|
v1, v2, v3 = struct.unpack("3B", data)
|
|
assert v1 == 65
|
|
assert v2 == 66
|
|
assert v3 == 67
|
|
|
|
with assert_raises(Exception):
|
|
data = struct.pack("B0B", 65, 66)
|
|
|
|
with assert_raises(Exception):
|
|
data = struct.pack("B2B", 65, 66)
|
|
|
|
data = struct.pack("B1B", 65, 66)
|
|
|
|
with assert_raises(Exception):
|
|
struct.pack("<IH", "14", 12)
|
|
|
|
assert struct.calcsize("B") == 1
|
|
# assert struct.calcsize("<L4B") == 12
|
|
|
|
assert struct.Struct("3B").pack(65, 66, 67) == bytes([65, 66, 67])
|
|
|
|
|
|
class Indexable(object):
|
|
def __init__(self, value):
|
|
self._value = value
|
|
|
|
def __index__(self):
|
|
return self._value
|
|
|
|
|
|
data = struct.pack("B", Indexable(65))
|
|
assert data == bytes([65])
|
|
|
|
data = struct.pack("5s", b"test1")
|
|
assert data == b"test1"
|
|
|
|
data = struct.pack("3s", b"test2")
|
|
assert data == b"tes"
|
|
|
|
data = struct.pack("7s", b"test3")
|
|
assert data == b"test3\0\0"
|
|
|
|
data = struct.pack("?", True)
|
|
assert data == b"\1"
|
|
|
|
data = struct.pack("?", [])
|
|
assert data == b"\0"
|
|
|
|
assert struct.error.__module__ == "struct"
|
|
assert struct.error.__name__ == "error"
|
|
|
|
# Non-ASCII format string: error type matches CPython.
|
|
# str and bytes → ValueError
|
|
try:
|
|
struct.Struct("\udc00")
|
|
except ValueError as e:
|
|
assert str(e) == "non-ASCII character in struct format"
|
|
else:
|
|
raise AssertionError("expected ValueError")
|
|
|
|
with assert_raises(ValueError):
|
|
struct.Struct("한")
|
|
|
|
with assert_raises(ValueError):
|
|
struct.Struct(b"\xff")
|
|
|
|
|
|
# A value the format has no room for names the format and the range it holds.
|
|
for fmt, message in (
|
|
("B", "'B' format requires 0 <= number <= 255"),
|
|
("b", "'b' format requires -128 <= number <= 127"),
|
|
(">H", "'H' format requires 0 <= number <= 65535"),
|
|
(">i", "'i' format requires -2147483648 <= number <= 2147483647"),
|
|
("N", "'N' format requires 0 <= number <= 18446744073709551615"),
|
|
("P", "int too large to convert"),
|
|
):
|
|
try:
|
|
struct.pack(fmt, 10**30)
|
|
except struct.error as e:
|
|
assert str(e) == message, (fmt, str(e))
|
|
else:
|
|
raise AssertionError(f"expected struct.error for {fmt!r}")
|
|
|
|
try:
|
|
struct.pack("B", "x")
|
|
except struct.error as e:
|
|
assert str(e) == "required argument is not an integer", e
|
|
else:
|
|
raise AssertionError("expected struct.error")
|
|
|
|
|
|
# __init__ reads a new format into a Struct that already holds one.
|
|
s = struct.Struct(">h")
|
|
s.__init__(">hh")
|
|
assert s.format == ">hh"
|
|
assert s.size == 4
|
|
assert s.pack(1, 2) == b"\x00\x01\x00\x02"
|
|
assert s.unpack(b"\x00\x01\x00\x02") == (1, 2)
|
|
|
|
# A format that cannot be read leaves the Struct as it was.
|
|
for bad in ("\udc00", "$"):
|
|
with assert_raises((ValueError, struct.error)):
|
|
s.__init__(bad)
|
|
assert s.format == ">hh"
|
|
assert s.pack(1, 2) == b"\x00\x01\x00\x02"
|
|
|
|
|
|
# A subclass may do its own __init__ and pass the format up.
|
|
class BigShort(struct.Struct):
|
|
def __init__(self):
|
|
super().__init__(">h")
|
|
|
|
|
|
assert BigShort().pack(12345) == b"\x30\x39"
|
|
|
|
# Until __init__ runs there is no format to answer with.
|
|
blank = struct.Struct.__new__(struct.Struct)
|
|
assert blank.size == -1
|
|
with assert_raises(AttributeError):
|
|
blank.format
|
|
for call in (
|
|
lambda: blank.pack(1),
|
|
lambda: blank.unpack(b"aa"),
|
|
lambda: blank.unpack_from(b"aaaa"),
|
|
lambda: blank.pack_into(bytearray(4), 0, 1),
|
|
lambda: blank.iter_unpack(b"aa"),
|
|
lambda: repr(blank),
|
|
):
|
|
with assert_raises(RuntimeError):
|
|
call()
|
|
|
|
|
|
# The buffer a format asks for is sized by the format: one too large to
|
|
# allocate must raise instead of aborting.
|
|
with assert_raises(MemoryError):
|
|
struct.pack("%dx" % (2**60))
|
|
|
|
|
|
# Clearing or evicting cached formats must not invalidate a live iterator.
|
|
iterator = struct.iter_unpack("<H", b"\x01\x00\x02\x00")
|
|
struct._clearcache()
|
|
for padding in range(110):
|
|
assert struct.calcsize(f"{padding}x") == padding
|
|
assert list(iterator) == [(1,), (2,)]
|
|
|
|
|
|
class ClearFormats:
|
|
def __index__(self):
|
|
struct._clearcache()
|
|
assert struct.pack("<H", 7) == b"\x07\x00"
|
|
return 42
|
|
|
|
|
|
# Argument conversion may clear and repopulate the cache during an active pack.
|
|
assert struct.pack("<H", ClearFormats()) == b"\x2a\x00"
|
|
buffer = bytearray(2)
|
|
struct.pack_into("<H", buffer, 0, ClearFormats())
|
|
assert buffer == b"\x2a\x00"
|
|
|
|
|
|
# Struct's format is positional-or-keyword, including inherited initialization.
|
|
class KeywordStruct(struct.Struct):
|
|
pass
|
|
|
|
|
|
for cls in (struct.Struct, KeywordStruct):
|
|
for fmt in (">h", b">h"):
|
|
s = cls(format=fmt)
|
|
assert s.format == ">h"
|
|
assert s.pack(7) == b"\x00\x07"
|
|
s.__init__(format=">hh")
|
|
assert s.unpack(b"\x00\x01\x00\x02") == (1, 2)
|
|
with assert_raises(struct.error):
|
|
s.__init__(format="$")
|
|
assert s.format == ">hh"
|
|
assert s.pack(1, 2) == b"\x00\x01\x00\x02"
|