mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 18:59:04 +08:00
Merge pull request #1792 from youknowone/struct
Fix struct module and add test from unittest
This commit is contained in:
4 files changed
+1189
-127
No files matched your search
@@ -0,0 +1,873 @@
|
||||
from collections import abc
|
||||
import array
|
||||
import math
|
||||
import operator
|
||||
import unittest
|
||||
import struct
|
||||
import sys
|
||||
|
||||
from test import support
|
||||
|
||||
ISBIGENDIAN = sys.byteorder == "big"
|
||||
|
||||
integer_codes = 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'n', 'N'
|
||||
byteorders = '', '@', '=', '<', '>', '!'
|
||||
|
||||
def iter_integer_formats(byteorders=byteorders):
|
||||
for code in integer_codes:
|
||||
for byteorder in byteorders:
|
||||
if (byteorder not in ('', '@') and code in ('n', 'N')):
|
||||
continue
|
||||
yield code, byteorder
|
||||
|
||||
def string_reverse(s):
|
||||
return s[::-1]
|
||||
|
||||
def bigendian_to_native(value):
|
||||
if ISBIGENDIAN:
|
||||
return value
|
||||
else:
|
||||
return string_reverse(value)
|
||||
|
||||
class StructTest(unittest.TestCase):
|
||||
def test_isbigendian(self):
|
||||
self.assertEqual((struct.pack('=i', 1)[0] == 0), ISBIGENDIAN)
|
||||
|
||||
def test_consistence(self):
|
||||
self.assertRaises(struct.error, struct.calcsize, 'Z')
|
||||
|
||||
sz = struct.calcsize('i')
|
||||
self.assertEqual(sz * 3, struct.calcsize('iii'))
|
||||
|
||||
fmt = 'cbxxxxxxhhhhiillffd?'
|
||||
fmt3 = '3c3b18x12h6i6l6f3d3?'
|
||||
sz = struct.calcsize(fmt)
|
||||
sz3 = struct.calcsize(fmt3)
|
||||
self.assertEqual(sz * 3, sz3)
|
||||
|
||||
self.assertRaises(struct.error, struct.pack, 'iii', 3)
|
||||
self.assertRaises(struct.error, struct.pack, 'i', 3, 3, 3)
|
||||
self.assertRaises((TypeError, struct.error), struct.pack, 'i', 'foo')
|
||||
self.assertRaises((TypeError, struct.error), struct.pack, 'P', 'foo')
|
||||
self.assertRaises(struct.error, struct.unpack, 'd', b'flap')
|
||||
s = struct.pack('ii', 1, 2)
|
||||
self.assertRaises(struct.error, struct.unpack, 'iii', s)
|
||||
self.assertRaises(struct.error, struct.unpack, 'i', s)
|
||||
|
||||
def test_transitiveness(self):
|
||||
c = b'a'
|
||||
b = 1
|
||||
h = 255
|
||||
i = 65535
|
||||
l = 65536
|
||||
f = 3.1415
|
||||
d = 3.1415
|
||||
t = True
|
||||
|
||||
for prefix in ('', '@', '<', '>', '=', '!'):
|
||||
for format in ('xcbhilfd?', 'xcBHILfd?'):
|
||||
format = prefix + format
|
||||
s = struct.pack(format, c, b, h, i, l, f, d, t)
|
||||
cp, bp, hp, ip, lp, fp, dp, tp = struct.unpack(format, s)
|
||||
self.assertEqual(cp, c)
|
||||
self.assertEqual(bp, b)
|
||||
self.assertEqual(hp, h)
|
||||
self.assertEqual(ip, i)
|
||||
self.assertEqual(lp, l)
|
||||
self.assertEqual(int(100 * fp), int(100 * f))
|
||||
self.assertEqual(int(100 * dp), int(100 * d))
|
||||
self.assertEqual(tp, t)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_new_features(self):
|
||||
# Test some of the new features in detail
|
||||
# (format, argument, big-endian result, little-endian result, asymmetric)
|
||||
tests = [
|
||||
('c', b'a', b'a', b'a', 0),
|
||||
('xc', b'a', b'\0a', b'\0a', 0),
|
||||
('cx', b'a', b'a\0', b'a\0', 0),
|
||||
('s', b'a', b'a', b'a', 0),
|
||||
('0s', b'helloworld', b'', b'', 1),
|
||||
('1s', b'helloworld', b'h', b'h', 1),
|
||||
('9s', b'helloworld', b'helloworl', b'helloworl', 1),
|
||||
('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),
|
||||
('b', 7, b'\7', b'\7', 0),
|
||||
('b', -7, b'\371', b'\371', 0),
|
||||
('B', 7, b'\7', b'\7', 0),
|
||||
('B', 249, b'\371', b'\371', 0),
|
||||
('h', 700, b'\002\274', b'\274\002', 0),
|
||||
('h', -700, b'\375D', b'D\375', 0),
|
||||
('H', 700, b'\002\274', b'\274\002', 0),
|
||||
('H', 0x10000-700, b'\375D', b'D\375', 0),
|
||||
('i', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
|
||||
('i', -70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
|
||||
('I', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
|
||||
('I', 0x100000000-70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
|
||||
('l', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
|
||||
('l', -70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
|
||||
('L', 70000000, b'\004,\035\200', b'\200\035,\004', 0),
|
||||
('L', 0x100000000-70000000, b'\373\323\342\200', b'\200\342\323\373', 0),
|
||||
('f', 2.0, b'@\000\000\000', b'\000\000\000@', 0),
|
||||
('d', 2.0, b'@\000\000\000\000\000\000\000',
|
||||
b'\000\000\000\000\000\000\000@', 0),
|
||||
('f', -2.0, b'\300\000\000\000', b'\000\000\000\300', 0),
|
||||
('d', -2.0, b'\300\000\000\000\000\000\000\000',
|
||||
b'\000\000\000\000\000\000\000\300', 0),
|
||||
('?', 0, b'\0', b'\0', 0),
|
||||
('?', 3, b'\1', b'\1', 1),
|
||||
('?', True, b'\1', b'\1', 0),
|
||||
('?', [], b'\0', b'\0', 1),
|
||||
('?', (1,), b'\1', b'\1', 1),
|
||||
]
|
||||
|
||||
for fmt, arg, big, lil, asy in tests:
|
||||
for (xfmt, exp) in [('>'+fmt, big), ('!'+fmt, big), ('<'+fmt, lil),
|
||||
('='+fmt, ISBIGENDIAN and big or lil)]:
|
||||
res = struct.pack(xfmt, arg)
|
||||
self.assertEqual(res, exp)
|
||||
self.assertEqual(struct.calcsize(xfmt), len(res))
|
||||
rev = struct.unpack(xfmt, res)[0]
|
||||
if rev != arg:
|
||||
self.assertTrue(asy)
|
||||
|
||||
def test_calcsize(self):
|
||||
expected_size = {
|
||||
'b': 1, 'B': 1,
|
||||
'h': 2, 'H': 2,
|
||||
'i': 4, 'I': 4,
|
||||
'l': 4, 'L': 4,
|
||||
'q': 8, 'Q': 8,
|
||||
}
|
||||
|
||||
# standard integer sizes
|
||||
for code, byteorder in iter_integer_formats(('=', '<', '>', '!')):
|
||||
format = byteorder+code
|
||||
size = struct.calcsize(format)
|
||||
self.assertEqual(size, expected_size[code])
|
||||
|
||||
# native integer sizes
|
||||
native_pairs = 'bB', 'hH', 'iI', 'lL', 'nN', 'qQ'
|
||||
for format_pair in native_pairs:
|
||||
for byteorder in '', '@':
|
||||
signed_size = struct.calcsize(byteorder + format_pair[0])
|
||||
unsigned_size = struct.calcsize(byteorder + format_pair[1])
|
||||
self.assertEqual(signed_size, unsigned_size)
|
||||
|
||||
# bounds for native integer sizes
|
||||
self.assertEqual(struct.calcsize('b'), 1)
|
||||
self.assertLessEqual(2, struct.calcsize('h'))
|
||||
self.assertLessEqual(4, struct.calcsize('l'))
|
||||
self.assertLessEqual(struct.calcsize('h'), struct.calcsize('i'))
|
||||
self.assertLessEqual(struct.calcsize('i'), struct.calcsize('l'))
|
||||
self.assertLessEqual(8, struct.calcsize('q'))
|
||||
self.assertLessEqual(struct.calcsize('l'), struct.calcsize('q'))
|
||||
self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('i'))
|
||||
self.assertGreaterEqual(struct.calcsize('n'), struct.calcsize('P'))
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_integers(self):
|
||||
# Integer tests (bBhHiIlLqQnN).
|
||||
import binascii
|
||||
|
||||
class IntTester(unittest.TestCase):
|
||||
def __init__(self, format):
|
||||
super(IntTester, self).__init__(methodName='test_one')
|
||||
self.format = format
|
||||
self.code = format[-1]
|
||||
self.byteorder = format[:-1]
|
||||
if not self.byteorder in byteorders:
|
||||
raise ValueError("unrecognized packing byteorder: %s" %
|
||||
self.byteorder)
|
||||
self.bytesize = struct.calcsize(format)
|
||||
self.bitsize = self.bytesize * 8
|
||||
if self.code in tuple('bhilqn'):
|
||||
self.signed = True
|
||||
self.min_value = -(2**(self.bitsize-1))
|
||||
self.max_value = 2**(self.bitsize-1) - 1
|
||||
elif self.code in tuple('BHILQN'):
|
||||
self.signed = False
|
||||
self.min_value = 0
|
||||
self.max_value = 2**self.bitsize - 1
|
||||
else:
|
||||
raise ValueError("unrecognized format code: %s" %
|
||||
self.code)
|
||||
|
||||
def test_one(self, x, pack=struct.pack,
|
||||
unpack=struct.unpack,
|
||||
unhexlify=binascii.unhexlify):
|
||||
|
||||
format = self.format
|
||||
if self.min_value <= x <= self.max_value:
|
||||
expected = x
|
||||
if self.signed and x < 0:
|
||||
expected += 1 << self.bitsize
|
||||
self.assertGreaterEqual(expected, 0)
|
||||
expected = '%x' % expected
|
||||
if len(expected) & 1:
|
||||
expected = "0" + expected
|
||||
expected = expected.encode('ascii')
|
||||
expected = unhexlify(expected)
|
||||
expected = (b"\x00" * (self.bytesize - len(expected)) +
|
||||
expected)
|
||||
if (self.byteorder == '<' or
|
||||
self.byteorder in ('', '@', '=') and not ISBIGENDIAN):
|
||||
expected = string_reverse(expected)
|
||||
self.assertEqual(len(expected), self.bytesize)
|
||||
|
||||
# Pack work?
|
||||
got = pack(format, x)
|
||||
self.assertEqual(got, expected)
|
||||
|
||||
# Unpack work?
|
||||
retrieved = unpack(format, got)[0]
|
||||
self.assertEqual(x, retrieved)
|
||||
|
||||
# Adding any byte should cause a "too big" error.
|
||||
self.assertRaises((struct.error, TypeError), unpack, format,
|
||||
b'\x01' + got)
|
||||
else:
|
||||
# x is out of range -- verify pack realizes that.
|
||||
self.assertRaises((OverflowError, ValueError, struct.error),
|
||||
pack, format, x)
|
||||
|
||||
def run(self):
|
||||
from random import randrange
|
||||
|
||||
# Create all interesting powers of 2.
|
||||
values = []
|
||||
for exp in range(self.bitsize + 3):
|
||||
values.append(1 << exp)
|
||||
|
||||
# Add some random values.
|
||||
for i in range(self.bitsize):
|
||||
val = 0
|
||||
for j in range(self.bytesize):
|
||||
val = (val << 8) | randrange(256)
|
||||
values.append(val)
|
||||
|
||||
# Values absorbed from other tests
|
||||
values.extend([300, 700000, sys.maxsize*4])
|
||||
|
||||
# Try all those, and their negations, and +-1 from
|
||||
# them. Note that this tests all power-of-2
|
||||
# boundaries in range, and a few out of range, plus
|
||||
# +-(2**n +- 1).
|
||||
for base in values:
|
||||
for val in -base, base:
|
||||
for incr in -1, 0, 1:
|
||||
x = val + incr
|
||||
self.test_one(x)
|
||||
|
||||
# Some error cases.
|
||||
class NotAnInt:
|
||||
def __int__(self):
|
||||
return 42
|
||||
|
||||
# Objects with an '__index__' method should be allowed
|
||||
# to pack as integers. That is assuming the implemented
|
||||
# '__index__' method returns an 'int'.
|
||||
class Indexable(object):
|
||||
def __init__(self, value):
|
||||
self._value = value
|
||||
|
||||
def __index__(self):
|
||||
return self._value
|
||||
|
||||
# If the '__index__' method raises a type error, then
|
||||
# '__int__' should be used with a deprecation warning.
|
||||
class BadIndex(object):
|
||||
def __index__(self):
|
||||
raise TypeError
|
||||
|
||||
def __int__(self):
|
||||
return 42
|
||||
|
||||
self.assertRaises((TypeError, struct.error),
|
||||
struct.pack, self.format,
|
||||
"a string")
|
||||
self.assertRaises((TypeError, struct.error),
|
||||
struct.pack, self.format,
|
||||
randrange)
|
||||
self.assertRaises((TypeError, struct.error),
|
||||
struct.pack, self.format,
|
||||
3+42j)
|
||||
self.assertRaises((TypeError, struct.error),
|
||||
struct.pack, self.format,
|
||||
NotAnInt())
|
||||
self.assertRaises((TypeError, struct.error),
|
||||
struct.pack, self.format,
|
||||
BadIndex())
|
||||
|
||||
# Check for legitimate values from '__index__'.
|
||||
for obj in (Indexable(0), Indexable(10), Indexable(17),
|
||||
Indexable(42), Indexable(100), Indexable(127)):
|
||||
try:
|
||||
struct.pack(format, obj)
|
||||
except:
|
||||
self.fail("integer code pack failed on object "
|
||||
"with '__index__' method")
|
||||
|
||||
# Check for bogus values from '__index__'.
|
||||
for obj in (Indexable(b'a'), Indexable('b'), Indexable(None),
|
||||
Indexable({'a': 1}), Indexable([1, 2, 3])):
|
||||
self.assertRaises((TypeError, struct.error),
|
||||
struct.pack, self.format,
|
||||
obj)
|
||||
|
||||
for code, byteorder in iter_integer_formats():
|
||||
format = byteorder+code
|
||||
t = IntTester(format)
|
||||
t.run()
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_nN_code(self):
|
||||
# n and N don't exist in standard sizes
|
||||
def assertStructError(func, *args, **kwargs):
|
||||
with self.assertRaises(struct.error) as cm:
|
||||
func(*args, **kwargs)
|
||||
self.assertIn("bad char in struct format", str(cm.exception))
|
||||
for code in 'nN':
|
||||
for byteorder in ('=', '<', '>', '!'):
|
||||
format = byteorder+code
|
||||
assertStructError(struct.calcsize, format)
|
||||
assertStructError(struct.pack, format, 0)
|
||||
assertStructError(struct.unpack, format, b"")
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_p_code(self):
|
||||
# Test p ("Pascal string") code.
|
||||
for code, input, expected, expectedback in [
|
||||
('p', b'abc', b'\x00', b''),
|
||||
('1p', b'abc', b'\x00', b''),
|
||||
('2p', b'abc', b'\x01a', b'a'),
|
||||
('3p', b'abc', b'\x02ab', b'ab'),
|
||||
('4p', b'abc', b'\x03abc', b'abc'),
|
||||
('5p', b'abc', b'\x03abc\x00', b'abc'),
|
||||
('6p', b'abc', b'\x03abc\x00\x00', b'abc'),
|
||||
('1000p', b'x'*1000, b'\xff' + b'x'*999, b'x'*255)]:
|
||||
got = struct.pack(code, input)
|
||||
self.assertEqual(got, expected)
|
||||
(got,) = struct.unpack(code, got)
|
||||
self.assertEqual(got, expectedback)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_705836(self):
|
||||
# SF bug 705836. "<f" and ">f" had a severe rounding bug, where a carry
|
||||
# from the low-order discarded bits could propagate into the exponent
|
||||
# field, causing the result to be wrong by a factor of 2.
|
||||
for base in range(1, 33):
|
||||
# smaller <- largest representable float less than base.
|
||||
delta = 0.5
|
||||
while base - delta / 2.0 != base:
|
||||
delta /= 2.0
|
||||
smaller = base - delta
|
||||
# Packing this rounds away a solid string of trailing 1 bits.
|
||||
packed = struct.pack("<f", smaller)
|
||||
unpacked = struct.unpack("<f", packed)[0]
|
||||
# This failed at base = 2, 4, and 32, with unpacked = 1, 2, and
|
||||
# 16, respectively.
|
||||
self.assertEqual(base, unpacked)
|
||||
bigpacked = struct.pack(">f", smaller)
|
||||
self.assertEqual(bigpacked, string_reverse(packed))
|
||||
unpacked = struct.unpack(">f", bigpacked)[0]
|
||||
self.assertEqual(base, unpacked)
|
||||
|
||||
# Largest finite IEEE single.
|
||||
big = (1 << 24) - 1
|
||||
big = math.ldexp(big, 127 - 23)
|
||||
packed = struct.pack(">f", big)
|
||||
unpacked = struct.unpack(">f", packed)[0]
|
||||
self.assertEqual(big, unpacked)
|
||||
|
||||
# The same, but tack on a 1 bit so it rounds up to infinity.
|
||||
big = (1 << 25) - 1
|
||||
big = math.ldexp(big, 127 - 24)
|
||||
self.assertRaises(OverflowError, struct.pack, ">f", big)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_1530559(self):
|
||||
for code, byteorder in iter_integer_formats():
|
||||
format = byteorder + code
|
||||
self.assertRaises(struct.error, struct.pack, format, 1.0)
|
||||
self.assertRaises(struct.error, struct.pack, format, 1.5)
|
||||
self.assertRaises(struct.error, struct.pack, 'P', 1.0)
|
||||
self.assertRaises(struct.error, struct.pack, 'P', 1.5)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_unpack_from(self):
|
||||
test_string = b'abcd01234'
|
||||
fmt = '4s'
|
||||
s = struct.Struct(fmt)
|
||||
for cls in (bytes, bytearray):
|
||||
data = cls(test_string)
|
||||
self.assertEqual(s.unpack_from(data), (b'abcd',))
|
||||
self.assertEqual(s.unpack_from(data, 2), (b'cd01',))
|
||||
self.assertEqual(s.unpack_from(data, 4), (b'0123',))
|
||||
for i in range(6):
|
||||
self.assertEqual(s.unpack_from(data, i), (data[i:i+4],))
|
||||
for i in range(6, len(test_string) + 1):
|
||||
self.assertRaises(struct.error, s.unpack_from, data, i)
|
||||
for cls in (bytes, bytearray):
|
||||
data = cls(test_string)
|
||||
self.assertEqual(struct.unpack_from(fmt, data), (b'abcd',))
|
||||
self.assertEqual(struct.unpack_from(fmt, data, 2), (b'cd01',))
|
||||
self.assertEqual(struct.unpack_from(fmt, data, 4), (b'0123',))
|
||||
for i in range(6):
|
||||
self.assertEqual(struct.unpack_from(fmt, data, i), (data[i:i+4],))
|
||||
for i in range(6, len(test_string) + 1):
|
||||
self.assertRaises(struct.error, struct.unpack_from, fmt, data, i)
|
||||
|
||||
# keyword arguments
|
||||
self.assertEqual(s.unpack_from(buffer=test_string, offset=2),
|
||||
(b'cd01',))
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_pack_into(self):
|
||||
test_string = b'Reykjavik rocks, eow!'
|
||||
writable_buf = array.array('b', b' '*100)
|
||||
fmt = '21s'
|
||||
s = struct.Struct(fmt)
|
||||
|
||||
# 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)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
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)
|
||||
|
||||
# Test with offset.
|
||||
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), pack_into, small_buf, 0,
|
||||
test_string)
|
||||
self.assertRaises((ValueError, struct.error), pack_into, small_buf, 2,
|
||||
test_string)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_unpack_with_buffer(self):
|
||||
# SF bug 1563759: struct.unpack doesn't support buffer protocol objects
|
||||
data1 = array.array('B', b'\x12\x34\x56\x78')
|
||||
data2 = memoryview(b'\x12\x34\x56\x78') # XXX b'......XXXX......', 6, 4
|
||||
for data in [data1, data2]:
|
||||
value, = struct.unpack('>I', data)
|
||||
self.assertEqual(value, 0x12345678)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_bool(self):
|
||||
class ExplodingBool(object):
|
||||
def __bool__(self):
|
||||
raise OSError
|
||||
for prefix in tuple("<>!=")+('',):
|
||||
false = (), [], [], '', 0
|
||||
true = [1], 'test', 5, -1, 0xffffffff+1, 0xffffffff/2
|
||||
|
||||
falseFormat = prefix + '?' * len(false)
|
||||
packedFalse = struct.pack(falseFormat, *false)
|
||||
unpackedFalse = struct.unpack(falseFormat, packedFalse)
|
||||
|
||||
trueFormat = prefix + '?' * len(true)
|
||||
packedTrue = struct.pack(trueFormat, *true)
|
||||
unpackedTrue = struct.unpack(trueFormat, packedTrue)
|
||||
|
||||
self.assertEqual(len(true), len(unpackedTrue))
|
||||
self.assertEqual(len(false), len(unpackedFalse))
|
||||
|
||||
for t in unpackedFalse:
|
||||
self.assertFalse(t)
|
||||
for t in unpackedTrue:
|
||||
self.assertTrue(t)
|
||||
|
||||
packed = struct.pack(prefix+'?', 1)
|
||||
|
||||
self.assertEqual(len(packed), struct.calcsize(prefix+'?'))
|
||||
|
||||
if len(packed) != 1:
|
||||
self.assertFalse(prefix, msg='encoded bool is not one byte: %r'
|
||||
%packed)
|
||||
|
||||
try:
|
||||
struct.pack(prefix + '?', ExplodingBool())
|
||||
except OSError:
|
||||
pass
|
||||
else:
|
||||
self.fail("Expected OSError: struct.pack(%r, "
|
||||
"ExplodingBool())" % (prefix + '?'))
|
||||
|
||||
for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
|
||||
self.assertTrue(struct.unpack('>?', c)[0])
|
||||
|
||||
@unittest.skip("TODO: RUSTPYTHON")
|
||||
def test_count_overflow(self):
|
||||
hugecount = '{}b'.format(sys.maxsize+1)
|
||||
self.assertRaises(struct.error, struct.calcsize, hugecount)
|
||||
|
||||
hugecount2 = '{}b{}H'.format(sys.maxsize//2, sys.maxsize//2)
|
||||
self.assertRaises(struct.error, struct.calcsize, hugecount2)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_trailing_counter(self):
|
||||
store = array.array('b', b' '*100)
|
||||
|
||||
# format lists containing only count spec should result in an error
|
||||
self.assertRaises(struct.error, struct.pack, '12345')
|
||||
self.assertRaises(struct.error, struct.unpack, '12345', b'')
|
||||
self.assertRaises(struct.error, struct.pack_into, '12345', store, 0)
|
||||
self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0)
|
||||
|
||||
# Format lists with trailing count spec should result in an error
|
||||
self.assertRaises(struct.error, struct.pack, 'c12345', 'x')
|
||||
self.assertRaises(struct.error, struct.unpack, 'c12345', b'x')
|
||||
self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0,
|
||||
'x')
|
||||
self.assertRaises(struct.error, struct.unpack_from, 'c12345', store,
|
||||
0)
|
||||
|
||||
# Mixed format tests
|
||||
self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs')
|
||||
self.assertRaises(struct.error, struct.unpack, '14s42',
|
||||
b'spam and eggs')
|
||||
self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0,
|
||||
'spam and eggs')
|
||||
self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)
|
||||
|
||||
def test_Struct_reinitialization(self):
|
||||
# Issue 9422: there was a memory leak when reinitializing a
|
||||
# Struct instance. This test can be used to detect the leak
|
||||
# when running with regrtest -L.
|
||||
s = struct.Struct('i')
|
||||
s.__init__('ii')
|
||||
|
||||
def check_sizeof(self, format_str, number_of_codes):
|
||||
# The size of 'PyStructObject'
|
||||
totalsize = support.calcobjsize('2n3P')
|
||||
# The size taken up by the 'formatcode' dynamic array
|
||||
totalsize += struct.calcsize('P3n0P') * (number_of_codes + 1)
|
||||
support.check_sizeof(self, struct.Struct(format_str), totalsize)
|
||||
|
||||
@support.cpython_only
|
||||
def test__sizeof__(self):
|
||||
for code in integer_codes:
|
||||
self.check_sizeof(code, 1)
|
||||
self.check_sizeof('BHILfdspP', 9)
|
||||
self.check_sizeof('B' * 1234, 1234)
|
||||
self.check_sizeof('fd', 2)
|
||||
self.check_sizeof('xxxxxxxxxxxxxx', 0)
|
||||
self.check_sizeof('100H', 1)
|
||||
self.check_sizeof('187s', 1)
|
||||
self.check_sizeof('20p', 1)
|
||||
self.check_sizeof('0s', 1)
|
||||
self.check_sizeof('0c', 0)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_boundary_error_message(self):
|
||||
regex1 = (
|
||||
r'pack_into requires a buffer of at least 6 '
|
||||
r'bytes for packing 1 bytes at offset 5 '
|
||||
r'\(actual buffer size is 1\)'
|
||||
)
|
||||
with self.assertRaisesRegex(struct.error, regex1):
|
||||
struct.pack_into('b', bytearray(1), 5, 1)
|
||||
|
||||
regex2 = (
|
||||
r'unpack_from requires a buffer of at least 6 '
|
||||
r'bytes for unpacking 1 bytes at offset 5 '
|
||||
r'\(actual buffer size is 1\)'
|
||||
)
|
||||
with self.assertRaisesRegex(struct.error, regex2):
|
||||
struct.unpack_from('b', bytearray(1), 5)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_boundary_error_message_with_negative_offset(self):
|
||||
byte_list = bytearray(10)
|
||||
with self.assertRaisesRegex(
|
||||
struct.error,
|
||||
r'no space to pack 4 bytes at offset -2'):
|
||||
struct.pack_into('<I', byte_list, -2, 123)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
struct.error,
|
||||
'offset -11 out of range for 10-byte buffer'):
|
||||
struct.pack_into('<B', byte_list, -11, 123)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
struct.error,
|
||||
r'not enough data to unpack 4 bytes at offset -2'):
|
||||
struct.unpack_from('<I', byte_list, -2)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
struct.error,
|
||||
"offset -11 out of range for 10-byte buffer"):
|
||||
struct.unpack_from('<B', byte_list, -11)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_boundary_error_message_with_large_offset(self):
|
||||
# Test overflows cause by large offset and value size (issue 30245)
|
||||
regex1 = (
|
||||
r'pack_into requires a buffer of at least ' + str(sys.maxsize + 4) +
|
||||
r' bytes for packing 4 bytes at offset ' + str(sys.maxsize) +
|
||||
r' \(actual buffer size is 10\)'
|
||||
)
|
||||
with self.assertRaisesRegex(struct.error, regex1):
|
||||
struct.pack_into('<I', bytearray(10), sys.maxsize, 1)
|
||||
|
||||
regex2 = (
|
||||
r'unpack_from requires a buffer of at least ' + str(sys.maxsize + 4) +
|
||||
r' bytes for unpacking 4 bytes at offset ' + str(sys.maxsize) +
|
||||
r' \(actual buffer size is 10\)'
|
||||
)
|
||||
with self.assertRaisesRegex(struct.error, regex2):
|
||||
struct.unpack_from('<I', bytearray(10), sys.maxsize)
|
||||
|
||||
def test_issue29802(self):
|
||||
# When the second argument of struct.unpack() was of wrong type
|
||||
# the Struct object was decrefed twice and the reference to
|
||||
# deallocated object was left in a cache.
|
||||
with self.assertRaises(TypeError):
|
||||
struct.unpack('b', 0)
|
||||
# Shouldn't crash.
|
||||
self.assertEqual(struct.unpack('b', b'a'), (b'a'[0],))
|
||||
|
||||
def test_format_attr(self):
|
||||
s = struct.Struct('=i2H')
|
||||
self.assertEqual(s.format, '=i2H')
|
||||
|
||||
# use a bytes string
|
||||
s2 = struct.Struct(s.format.encode())
|
||||
self.assertEqual(s2.format, s.format)
|
||||
|
||||
|
||||
class UnpackIteratorTest(unittest.TestCase):
|
||||
"""
|
||||
Tests for iterative unpacking (struct.Struct.iter_unpack).
|
||||
"""
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_construct(self):
|
||||
def _check_iterator(it):
|
||||
self.assertIsInstance(it, abc.Iterator)
|
||||
self.assertIsInstance(it, abc.Iterable)
|
||||
s = struct.Struct('>ibcp')
|
||||
it = s.iter_unpack(b"")
|
||||
_check_iterator(it)
|
||||
it = s.iter_unpack(b"1234567")
|
||||
_check_iterator(it)
|
||||
# Wrong bytes length
|
||||
with self.assertRaises(struct.error):
|
||||
s.iter_unpack(b"123456")
|
||||
with self.assertRaises(struct.error):
|
||||
s.iter_unpack(b"12345678")
|
||||
# Zero-length struct
|
||||
s = struct.Struct('>')
|
||||
with self.assertRaises(struct.error):
|
||||
s.iter_unpack(b"")
|
||||
with self.assertRaises(struct.error):
|
||||
s.iter_unpack(b"12")
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_iterate(self):
|
||||
s = struct.Struct('>IB')
|
||||
b = bytes(range(1, 16))
|
||||
it = s.iter_unpack(b)
|
||||
self.assertEqual(next(it), (0x01020304, 5))
|
||||
self.assertEqual(next(it), (0x06070809, 10))
|
||||
self.assertEqual(next(it), (0x0b0c0d0e, 15))
|
||||
self.assertRaises(StopIteration, next, it)
|
||||
self.assertRaises(StopIteration, next, it)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_arbitrary_buffer(self):
|
||||
s = struct.Struct('>IB')
|
||||
b = bytes(range(1, 11))
|
||||
it = s.iter_unpack(memoryview(b))
|
||||
self.assertEqual(next(it), (0x01020304, 5))
|
||||
self.assertEqual(next(it), (0x06070809, 10))
|
||||
self.assertRaises(StopIteration, next, it)
|
||||
self.assertRaises(StopIteration, next, it)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_length_hint(self):
|
||||
lh = operator.length_hint
|
||||
s = struct.Struct('>IB')
|
||||
b = bytes(range(1, 16))
|
||||
it = s.iter_unpack(b)
|
||||
self.assertEqual(lh(it), 3)
|
||||
next(it)
|
||||
self.assertEqual(lh(it), 2)
|
||||
next(it)
|
||||
self.assertEqual(lh(it), 1)
|
||||
next(it)
|
||||
self.assertEqual(lh(it), 0)
|
||||
self.assertRaises(StopIteration, next, it)
|
||||
self.assertEqual(lh(it), 0)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_module_func(self):
|
||||
# Sanity check for the global struct.iter_unpack()
|
||||
it = struct.iter_unpack('>IB', bytes(range(1, 11)))
|
||||
self.assertEqual(next(it), (0x01020304, 5))
|
||||
self.assertEqual(next(it), (0x06070809, 10))
|
||||
self.assertRaises(StopIteration, next, it)
|
||||
self.assertRaises(StopIteration, next, it)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_half_float(self):
|
||||
# Little-endian examples from:
|
||||
# http://en.wikipedia.org/wiki/Half_precision_floating-point_format
|
||||
format_bits_float__cleanRoundtrip_list = [
|
||||
(b'\x00\x3c', 1.0),
|
||||
(b'\x00\xc0', -2.0),
|
||||
(b'\xff\x7b', 65504.0), # (max half precision)
|
||||
(b'\x00\x04', 2**-14), # ~= 6.10352 * 10**-5 (min pos normal)
|
||||
(b'\x01\x00', 2**-24), # ~= 5.96046 * 10**-8 (min pos subnormal)
|
||||
(b'\x00\x00', 0.0),
|
||||
(b'\x00\x80', -0.0),
|
||||
(b'\x00\x7c', float('+inf')),
|
||||
(b'\x00\xfc', float('-inf')),
|
||||
(b'\x55\x35', 0.333251953125), # ~= 1/3
|
||||
]
|
||||
|
||||
for le_bits, f in format_bits_float__cleanRoundtrip_list:
|
||||
be_bits = le_bits[::-1]
|
||||
self.assertEqual(f, struct.unpack('<e', le_bits)[0])
|
||||
self.assertEqual(le_bits, struct.pack('<e', f))
|
||||
self.assertEqual(f, struct.unpack('>e', be_bits)[0])
|
||||
self.assertEqual(be_bits, struct.pack('>e', f))
|
||||
if sys.byteorder == 'little':
|
||||
self.assertEqual(f, struct.unpack('e', le_bits)[0])
|
||||
self.assertEqual(le_bits, struct.pack('e', f))
|
||||
else:
|
||||
self.assertEqual(f, struct.unpack('e', be_bits)[0])
|
||||
self.assertEqual(be_bits, struct.pack('e', f))
|
||||
|
||||
# Check for NaN handling:
|
||||
format_bits__nan_list = [
|
||||
('<e', b'\x01\xfc'),
|
||||
('<e', b'\x00\xfe'),
|
||||
('<e', b'\xff\xff'),
|
||||
('<e', b'\x01\x7c'),
|
||||
('<e', b'\x00\x7e'),
|
||||
('<e', b'\xff\x7f'),
|
||||
]
|
||||
|
||||
for formatcode, bits in format_bits__nan_list:
|
||||
self.assertTrue(math.isnan(struct.unpack('<e', bits)[0]))
|
||||
self.assertTrue(math.isnan(struct.unpack('>e', bits[::-1])[0]))
|
||||
|
||||
# Check that packing produces a bit pattern representing a quiet NaN:
|
||||
# all exponent bits and the msb of the fraction should all be 1.
|
||||
packed = struct.pack('<e', math.nan)
|
||||
self.assertEqual(packed[1] & 0x7e, 0x7e)
|
||||
packed = struct.pack('<e', -math.nan)
|
||||
self.assertEqual(packed[1] & 0x7e, 0x7e)
|
||||
|
||||
# Checks for round-to-even behavior
|
||||
format_bits_float__rounding_list = [
|
||||
('>e', b'\x00\x01', 2.0**-25 + 2.0**-35), # Rounds to minimum subnormal
|
||||
('>e', b'\x00\x00', 2.0**-25), # Underflows to zero (nearest even mode)
|
||||
('>e', b'\x00\x00', 2.0**-26), # Underflows to zero
|
||||
('>e', b'\x03\xff', 2.0**-14 - 2.0**-24), # Largest subnormal.
|
||||
('>e', b'\x03\xff', 2.0**-14 - 2.0**-25 - 2.0**-65),
|
||||
('>e', b'\x04\x00', 2.0**-14 - 2.0**-25),
|
||||
('>e', b'\x04\x00', 2.0**-14), # Smallest normal.
|
||||
('>e', b'\x3c\x01', 1.0+2.0**-11 + 2.0**-16), # rounds to 1.0+2**(-10)
|
||||
('>e', b'\x3c\x00', 1.0+2.0**-11), # rounds to 1.0 (nearest even mode)
|
||||
('>e', b'\x3c\x00', 1.0+2.0**-12), # rounds to 1.0
|
||||
('>e', b'\x7b\xff', 65504), # largest normal
|
||||
('>e', b'\x7b\xff', 65519), # rounds to 65504
|
||||
('>e', b'\x80\x01', -2.0**-25 - 2.0**-35), # Rounds to minimum subnormal
|
||||
('>e', b'\x80\x00', -2.0**-25), # Underflows to zero (nearest even mode)
|
||||
('>e', b'\x80\x00', -2.0**-26), # Underflows to zero
|
||||
('>e', b'\xbc\x01', -1.0-2.0**-11 - 2.0**-16), # rounds to 1.0+2**(-10)
|
||||
('>e', b'\xbc\x00', -1.0-2.0**-11), # rounds to 1.0 (nearest even mode)
|
||||
('>e', b'\xbc\x00', -1.0-2.0**-12), # rounds to 1.0
|
||||
('>e', b'\xfb\xff', -65519), # rounds to 65504
|
||||
]
|
||||
|
||||
for formatcode, bits, f in format_bits_float__rounding_list:
|
||||
self.assertEqual(bits, struct.pack(formatcode, f))
|
||||
|
||||
# This overflows, and so raises an error
|
||||
format_bits_float__roundingError_list = [
|
||||
# Values that round to infinity.
|
||||
('>e', 65520.0),
|
||||
('>e', 65536.0),
|
||||
('>e', 1e300),
|
||||
('>e', -65520.0),
|
||||
('>e', -65536.0),
|
||||
('>e', -1e300),
|
||||
('<e', 65520.0),
|
||||
('<e', 65536.0),
|
||||
('<e', 1e300),
|
||||
('<e', -65520.0),
|
||||
('<e', -65536.0),
|
||||
('<e', -1e300),
|
||||
]
|
||||
|
||||
for formatcode, f in format_bits_float__roundingError_list:
|
||||
self.assertRaises(OverflowError, struct.pack, formatcode, f)
|
||||
|
||||
# Double rounding
|
||||
format_bits_float__doubleRoundingError_list = [
|
||||
('>e', b'\x67\xff', 0x1ffdffffff * 2**-26), # should be 2047, if double-rounded 64>32>16, becomes 2048
|
||||
]
|
||||
|
||||
for formatcode, bits, f in format_bits_float__doubleRoundingError_list:
|
||||
self.assertEqual(bits, struct.pack(formatcode, f))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -109,7 +109,7 @@ impl PyBytes {
|
||||
}
|
||||
|
||||
#[pymethod(name = "__len__")]
|
||||
fn len(&self) -> usize {
|
||||
pub(crate) fn len(&self) -> usize {
|
||||
self.inner.len()
|
||||
}
|
||||
|
||||
|
||||
+12
-5
@@ -7,14 +7,13 @@ use statrs::function::erf::{erf, erfc};
|
||||
use statrs::function::gamma::{gamma, ln_gamma};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
use num_traits::cast::ToPrimitive;
|
||||
use num_traits::{One, Zero};
|
||||
|
||||
use crate::function::OptionalArg;
|
||||
use crate::obj::objfloat::{self, IntoPyFloat, PyFloatRef};
|
||||
use crate::obj::objint::PyIntRef;
|
||||
use crate::obj::objint::{self, PyIntRef};
|
||||
use crate::obj::objtype;
|
||||
use crate::pyobject::{PyObjectRef, PyResult, TypeProtocol};
|
||||
use crate::pyobject::{Either, PyObjectRef, PyResult, TypeProtocol};
|
||||
use crate::vm::VirtualMachine;
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
@@ -261,8 +260,16 @@ fn math_frexp(value: IntoPyFloat) -> (f64, i32) {
|
||||
}
|
||||
}
|
||||
|
||||
fn math_ldexp(value: PyFloatRef, i: PyIntRef) -> f64 {
|
||||
value.to_f64() * (2_f64).powf(i.as_bigint().to_f64().unwrap())
|
||||
fn math_ldexp(
|
||||
value: Either<PyFloatRef, PyIntRef>,
|
||||
i: PyIntRef,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<f64> {
|
||||
let value = match value {
|
||||
Either::A(f) => f.to_f64(),
|
||||
Either::B(z) => objint::try_float(z.as_bigint(), vm)?,
|
||||
};
|
||||
Ok(value * (2_f64).powf(objint::try_float(i.as_bigint(), vm)?))
|
||||
}
|
||||
|
||||
fn math_gcd(a: PyIntRef, b: PyIntRef) -> BigInt {
|
||||
|
||||
+303
-121
@@ -9,16 +9,19 @@
|
||||
* https://docs.rs/byteorder/1.2.6/byteorder/
|
||||
*/
|
||||
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||
use num_bigint::BigInt;
|
||||
use num_traits::ToPrimitive;
|
||||
use std::io::{Cursor, Read, Write};
|
||||
use std::iter::Peekable;
|
||||
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt};
|
||||
|
||||
use crate::exceptions::PyBaseExceptionRef;
|
||||
use crate::function::Args;
|
||||
use crate::obj::{
|
||||
objbytes::PyBytesRef, objstr::PyStringRef, objtuple::PyTuple, objtype::PyClassRef,
|
||||
objbytes::PyBytesRef, objstr::PyString, objstr::PyStringRef, objtuple::PyTuple,
|
||||
objtype::PyClassRef,
|
||||
};
|
||||
use crate::pyobject::{PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject};
|
||||
use crate::pyobject::{Either, PyClassImpl, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject};
|
||||
use crate::VirtualMachine;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -31,21 +34,35 @@ enum Endianness {
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FormatCode {
|
||||
repeat: u32,
|
||||
code: char,
|
||||
}
|
||||
|
||||
impl FormatCode {
|
||||
fn size(&self) -> usize {
|
||||
fn unit_size(&self) -> usize {
|
||||
match self.code {
|
||||
'b' | 'B' | '?' => 1,
|
||||
'x' | 'c' | 'b' | 'B' | '?' | 's' | 'p' => 1,
|
||||
'h' | 'H' => 2,
|
||||
'i' | 'l' | 'I' | 'L' | 'f' => 4,
|
||||
'q' | 'Q' | 'd' => 8,
|
||||
'n' | 'N' | 'P' => std::mem::size_of::<usize>(),
|
||||
c => {
|
||||
panic!("Unsupported format code {:?}", c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn size(&self) -> usize {
|
||||
self.unit_size() * self.repeat as usize
|
||||
}
|
||||
|
||||
fn arg_count(&self) -> usize {
|
||||
match self.code {
|
||||
'x' => 0,
|
||||
's' | 'p' => 1,
|
||||
_ => self.repeat as usize,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -68,9 +85,10 @@ impl FormatSpec {
|
||||
}
|
||||
|
||||
fn pack(&self, args: &[PyObjectRef], vm: &VirtualMachine) -> PyResult<Vec<u8>> {
|
||||
if self.codes.len() != args.len() {
|
||||
return Err(vm.new_exception_msg(
|
||||
vm.try_class("_struct", "error")?,
|
||||
let arg_count: usize = self.codes.iter().map(|c| c.arg_count()).sum();
|
||||
if arg_count != args.len() {
|
||||
return Err(new_struct_error(
|
||||
vm,
|
||||
format!(
|
||||
"pack expected {} items for packing (got {})",
|
||||
self.codes.len(),
|
||||
@@ -81,41 +99,48 @@ impl FormatSpec {
|
||||
|
||||
// Create data vector:
|
||||
let mut data = Vec::<u8>::new();
|
||||
let mut arg_idx = 0;
|
||||
// Loop over all opcodes:
|
||||
for (code, arg) in self.codes.iter().zip(args.iter()) {
|
||||
for code in self.codes.iter() {
|
||||
debug!("code: {:?}", code);
|
||||
match self.endianness {
|
||||
Endianness::Little => {
|
||||
pack_item::<byteorder::LittleEndian>(vm, code, arg, &mut data)?
|
||||
}
|
||||
Endianness::Big => pack_item::<byteorder::BigEndian>(vm, code, arg, &mut data)?,
|
||||
Endianness::Network => {
|
||||
pack_item::<byteorder::NetworkEndian>(vm, code, arg, &mut data)?
|
||||
}
|
||||
Endianness::Native => {
|
||||
pack_item::<byteorder::NativeEndian>(vm, code, arg, &mut data)?
|
||||
}
|
||||
}
|
||||
let pack_item = match self.endianness {
|
||||
Endianness::Little => pack_item::<byteorder::LittleEndian>,
|
||||
Endianness::Big => pack_item::<byteorder::BigEndian>,
|
||||
Endianness::Network => pack_item::<byteorder::NetworkEndian>,
|
||||
Endianness::Native => pack_item::<byteorder::NativeEndian>,
|
||||
};
|
||||
arg_idx += pack_item(vm, code, &args[arg_idx..], &mut data)?;
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
fn unpack(&self, data: &[u8], vm: &VirtualMachine) -> PyResult<PyTuple> {
|
||||
let mut rdr = Cursor::new(data);
|
||||
if self.size() != data.len() {
|
||||
return Err(new_struct_error(
|
||||
vm,
|
||||
format!("unpack requires a buffer of {} bytes", self.size()),
|
||||
));
|
||||
}
|
||||
|
||||
let mut rdr = Cursor::new(data);
|
||||
let mut items = vec![];
|
||||
for code in &self.codes {
|
||||
debug!("unpack code: {:?}", code);
|
||||
let item = match self.endianness {
|
||||
Endianness::Little => unpack_code::<byteorder::LittleEndian>(vm, &code, &mut rdr)?,
|
||||
Endianness::Big => unpack_code::<byteorder::BigEndian>(vm, &code, &mut rdr)?,
|
||||
match self.endianness {
|
||||
Endianness::Little => {
|
||||
unpack_code::<byteorder::LittleEndian>(vm, &code, &mut rdr, &mut items)?
|
||||
}
|
||||
Endianness::Big => {
|
||||
unpack_code::<byteorder::BigEndian>(vm, &code, &mut rdr, &mut items)?
|
||||
}
|
||||
Endianness::Network => {
|
||||
unpack_code::<byteorder::NetworkEndian>(vm, &code, &mut rdr)?
|
||||
unpack_code::<byteorder::NetworkEndian>(vm, &code, &mut rdr, &mut items)?
|
||||
}
|
||||
Endianness::Native => {
|
||||
unpack_code::<byteorder::NativeEndian>(vm, &code, &mut rdr, &mut items)?
|
||||
}
|
||||
Endianness::Native => unpack_code::<byteorder::NativeEndian>(vm, &code, &mut rdr)?,
|
||||
};
|
||||
items.push(item);
|
||||
}
|
||||
|
||||
Ok(PyTuple::from(items))
|
||||
@@ -173,22 +198,16 @@ where
|
||||
repeat = repeat * 10 + current_digit;
|
||||
}
|
||||
}
|
||||
Some(repeat)
|
||||
repeat
|
||||
}
|
||||
_ => None,
|
||||
_ => 1,
|
||||
};
|
||||
|
||||
// determine format char:
|
||||
let c = chars.next();
|
||||
match c {
|
||||
Some(c) if is_supported_format_character(c) => {
|
||||
if let Some(repeat) = repeat {
|
||||
for _ in 0..repeat {
|
||||
codes.push(FormatCode { code: c })
|
||||
}
|
||||
} else {
|
||||
codes.push(FormatCode { code: c })
|
||||
}
|
||||
codes.push(FormatCode { repeat, code: c })
|
||||
}
|
||||
_ => return Err(format!("Illegal format code {:?}", c)),
|
||||
}
|
||||
@@ -199,7 +218,8 @@ where
|
||||
|
||||
fn is_supported_format_character(c: char) -> bool {
|
||||
match c {
|
||||
'b' | 'B' | 'h' | 'H' | 'i' | 'I' | 'l' | 'L' | 'q' | 'Q' | 'f' | 'd' => true,
|
||||
'x' | 'c' | 'b' | 'B' | '?' | 'h' | 'H' | 'i' | 'I' | 'l' | 'L' | 'q' | 'Q' | 'n' | 'N'
|
||||
| 'f' | 'd' | 's' | 'p' | 'P' => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -252,117 +272,216 @@ fn pack_bool(vm: &VirtualMachine, arg: &PyObjectRef, data: &mut dyn Write) -> Py
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pack_item<Endianness>(
|
||||
fn pack_isize<Endianness>(
|
||||
vm: &VirtualMachine,
|
||||
code: &FormatCode,
|
||||
arg: &PyObjectRef,
|
||||
data: &mut dyn Write,
|
||||
) -> PyResult<()>
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match code.code {
|
||||
'b' => pack_i8(vm, arg, data)?,
|
||||
'B' => pack_u8(vm, arg, data)?,
|
||||
'?' => pack_bool(vm, arg, data)?,
|
||||
'h' => pack_i16::<Endianness>(vm, arg, data)?,
|
||||
'H' => pack_u16::<Endianness>(vm, arg, data)?,
|
||||
'i' | 'l' => pack_i32::<Endianness>(vm, arg, data)?,
|
||||
'I' | 'L' => pack_u32::<Endianness>(vm, arg, data)?,
|
||||
'q' => pack_i64::<Endianness>(vm, arg, data)?,
|
||||
'Q' => pack_u64::<Endianness>(vm, arg, data)?,
|
||||
'f' => pack_f32::<Endianness>(vm, arg, data)?,
|
||||
'd' => pack_f64::<Endianness>(vm, arg, data)?,
|
||||
c => {
|
||||
panic!("Unsupported format code {:?}", c);
|
||||
}
|
||||
let v = isize::try_from_object(vm, arg.clone())?;
|
||||
match std::mem::size_of::<isize>() {
|
||||
8 => data.write_i64::<Endianness>(v as i64).unwrap(),
|
||||
4 => data.write_i32::<Endianness>(v as i32).unwrap(),
|
||||
_ => unreachable!("unexpected architecture"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pack_usize<Endianness>(
|
||||
vm: &VirtualMachine,
|
||||
arg: &PyObjectRef,
|
||||
data: &mut dyn Write,
|
||||
) -> PyResult<()>
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
let v = isize::try_from_object(vm, arg.clone())?;
|
||||
match std::mem::size_of::<usize>() {
|
||||
8 => data.write_u64::<Endianness>(v as u64).unwrap(),
|
||||
4 => data.write_u32::<Endianness>(v as u32).unwrap(),
|
||||
_ => unreachable!("unexpected architecture"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pack_string(
|
||||
vm: &VirtualMachine,
|
||||
arg: &PyObjectRef,
|
||||
data: &mut dyn Write,
|
||||
length: usize,
|
||||
) -> PyResult<()> {
|
||||
let v = PyBytesRef::try_from_object(vm, arg.clone())?;
|
||||
match data.write_all(&v.get_value()[..length]) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(new_struct_error(vm, format!("{:?}", e))),
|
||||
}
|
||||
}
|
||||
|
||||
fn pack_char(vm: &VirtualMachine, arg: &PyObjectRef, data: &mut dyn Write) -> PyResult<()> {
|
||||
let v = PyBytesRef::try_from_object(vm, arg.clone())?;
|
||||
if v.len() == 1 {
|
||||
data.write_u8(v[0]).unwrap();
|
||||
Ok(())
|
||||
} else {
|
||||
Err(new_struct_error(
|
||||
vm,
|
||||
"char format requires a bytes object of length 1".to_owned(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn pack_item<Endianness>(
|
||||
vm: &VirtualMachine,
|
||||
code: &FormatCode,
|
||||
args: &[PyObjectRef],
|
||||
data: &mut dyn Write,
|
||||
) -> PyResult<usize>
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
let pack = match code.code {
|
||||
'c' => pack_char,
|
||||
'b' => pack_i8,
|
||||
'B' => pack_u8,
|
||||
'?' => pack_bool,
|
||||
'h' => pack_i16::<Endianness>,
|
||||
'H' => pack_u16::<Endianness>,
|
||||
'i' | 'l' => pack_i32::<Endianness>,
|
||||
'I' | 'L' => pack_u32::<Endianness>,
|
||||
'q' => pack_i64::<Endianness>,
|
||||
'Q' => pack_u64::<Endianness>,
|
||||
'n' => pack_isize::<Endianness>,
|
||||
'N' | 'P' => pack_usize::<Endianness>,
|
||||
'f' => pack_f32::<Endianness>,
|
||||
'd' => pack_f64::<Endianness>,
|
||||
's' | 'p' => {
|
||||
pack_string(vm, &args[0], data, code.repeat as usize)?;
|
||||
return Ok(1);
|
||||
}
|
||||
'x' => {
|
||||
for _ in 0..code.repeat as usize {
|
||||
data.write_u8(0).unwrap();
|
||||
}
|
||||
return Ok(0);
|
||||
}
|
||||
c => {
|
||||
panic!("Unsupported format code {:?}", c);
|
||||
}
|
||||
};
|
||||
|
||||
for arg in args.iter().take(code.repeat as usize) {
|
||||
pack(vm, arg, data)?;
|
||||
}
|
||||
Ok(code.repeat as usize)
|
||||
}
|
||||
|
||||
fn struct_pack(fmt: PyStringRef, args: Args, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
|
||||
let format_spec = FormatSpec::parse(fmt.as_str()).map_err(|e| vm.new_value_error(e))?;
|
||||
let format_spec = FormatSpec::parse(fmt.as_str()).map_err(|e| new_struct_error(vm, e))?;
|
||||
format_spec.pack(args.as_ref(), vm)
|
||||
}
|
||||
|
||||
fn unpack_i8(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult {
|
||||
match rdr.read_i8() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_int(v)),
|
||||
#[inline]
|
||||
fn unpack<F, T, G>(vm: &VirtualMachine, rdr: &mut dyn Read, read: F, transform: G) -> PyResult
|
||||
where
|
||||
F: Fn(&mut dyn Read) -> std::io::Result<T>,
|
||||
G: Fn(T) -> PyResult,
|
||||
{
|
||||
match read(rdr) {
|
||||
Ok(v) => transform(v),
|
||||
Err(_) => Err(new_struct_error(
|
||||
vm,
|
||||
format!(
|
||||
"unpack requires a buffer of {} bytes",
|
||||
std::mem::size_of::<T>()
|
||||
),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn unpack_u8(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult {
|
||||
match rdr.read_u8() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_int(v)),
|
||||
}
|
||||
#[inline]
|
||||
fn unpack_int<F, T>(vm: &VirtualMachine, rdr: &mut dyn Read, read: F) -> PyResult
|
||||
where
|
||||
F: Fn(&mut dyn Read) -> std::io::Result<T>,
|
||||
T: Into<BigInt> + ToPrimitive,
|
||||
{
|
||||
unpack(vm, rdr, read, |v| Ok(vm.ctx.new_int(v)))
|
||||
}
|
||||
|
||||
fn unpack_bool(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult {
|
||||
match rdr.read_u8() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_bool(v > 0)),
|
||||
}
|
||||
unpack(vm, rdr, |rdr| rdr.read_u8(), |v| Ok(vm.ctx.new_bool(v > 0)))
|
||||
}
|
||||
|
||||
fn unpack_i8(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult {
|
||||
unpack_int(vm, rdr, |rdr| rdr.read_i8())
|
||||
}
|
||||
|
||||
fn unpack_u8(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult {
|
||||
unpack_int(vm, rdr, |rdr| rdr.read_u8())
|
||||
}
|
||||
|
||||
fn unpack_i16<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match rdr.read_i16::<Endianness>() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_int(v)),
|
||||
}
|
||||
unpack_int(vm, rdr, |rdr| rdr.read_i16::<Endianness>())
|
||||
}
|
||||
|
||||
fn unpack_u16<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match rdr.read_u16::<Endianness>() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_int(v)),
|
||||
}
|
||||
unpack_int(vm, rdr, |rdr| rdr.read_u16::<Endianness>())
|
||||
}
|
||||
|
||||
fn unpack_i32<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match rdr.read_i32::<Endianness>() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_int(v)),
|
||||
}
|
||||
unpack_int(vm, rdr, |rdr| rdr.read_i32::<Endianness>())
|
||||
}
|
||||
|
||||
fn unpack_u32<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match rdr.read_u32::<Endianness>() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_int(v)),
|
||||
}
|
||||
unpack_int(vm, rdr, |rdr| rdr.read_u32::<Endianness>())
|
||||
}
|
||||
|
||||
fn unpack_i64<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match rdr.read_i64::<Endianness>() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_int(v)),
|
||||
}
|
||||
unpack_int(vm, rdr, |rdr| rdr.read_i64::<Endianness>())
|
||||
}
|
||||
|
||||
fn unpack_u64<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match rdr.read_u64::<Endianness>() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_int(v)),
|
||||
unpack_int(vm, rdr, |rdr| rdr.read_u64::<Endianness>())
|
||||
}
|
||||
|
||||
fn unpack_isize<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match std::mem::size_of::<isize>() {
|
||||
8 => unpack_i64::<Endianness>(vm, rdr),
|
||||
4 => unpack_i32::<Endianness>(vm, rdr),
|
||||
_ => unreachable!("unexpected architecture"),
|
||||
}
|
||||
}
|
||||
|
||||
fn unpack_usize<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match std::mem::size_of::<usize>() {
|
||||
8 => unpack_u64::<Endianness>(vm, rdr),
|
||||
4 => unpack_u32::<Endianness>(vm, rdr),
|
||||
_ => unreachable!("unexpected architecture"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -370,53 +489,102 @@ fn unpack_f32<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match rdr.read_f32::<Endianness>() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_float(f64::from(v))),
|
||||
}
|
||||
unpack(
|
||||
vm,
|
||||
rdr,
|
||||
|rdr| rdr.read_f32::<Endianness>(),
|
||||
|v| Ok(vm.ctx.new_float(f64::from(v))),
|
||||
)
|
||||
}
|
||||
|
||||
fn unpack_f64<Endianness>(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match rdr.read_f64::<Endianness>() {
|
||||
Err(err) => panic!("Error in reading {:?}", err),
|
||||
Ok(v) => Ok(vm.ctx.new_float(v)),
|
||||
}
|
||||
unpack(
|
||||
vm,
|
||||
rdr,
|
||||
|rdr| rdr.read_f64::<Endianness>(),
|
||||
|v| Ok(vm.ctx.new_float(v)),
|
||||
)
|
||||
}
|
||||
|
||||
fn unpack_empty(_vm: &VirtualMachine, rdr: &mut dyn Read, length: u32) {
|
||||
let mut handle = rdr.take(length as u64);
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
let _ = handle.read_to_end(&mut buf);
|
||||
}
|
||||
|
||||
fn unpack_char(vm: &VirtualMachine, rdr: &mut dyn Read) -> PyResult {
|
||||
unpack_string(vm, rdr, 1)
|
||||
}
|
||||
|
||||
fn unpack_string(vm: &VirtualMachine, rdr: &mut dyn Read, length: u32) -> PyResult {
|
||||
let mut handle = rdr.take(length as u64);
|
||||
let mut buf: Vec<u8> = Vec::new();
|
||||
handle.read_to_end(&mut buf).map_err(|_| {
|
||||
new_struct_error(vm, format!("unpack requires a buffer of {} bytes", length,))
|
||||
})?;
|
||||
Ok(vm.ctx.new_bytes(buf))
|
||||
}
|
||||
|
||||
fn struct_unpack(fmt: PyStringRef, buffer: PyBytesRef, vm: &VirtualMachine) -> PyResult<PyTuple> {
|
||||
let fmt_str = fmt.as_str();
|
||||
let format_spec = FormatSpec::parse(fmt_str).map_err(|e| vm.new_value_error(e))?;
|
||||
let format_spec = FormatSpec::parse(fmt_str).map_err(|e| new_struct_error(vm, e))?;
|
||||
format_spec.unpack(buffer.get_value(), vm)
|
||||
}
|
||||
|
||||
fn unpack_code<Endianness>(vm: &VirtualMachine, code: &FormatCode, rdr: &mut dyn Read) -> PyResult
|
||||
fn unpack_code<Endianness>(
|
||||
vm: &VirtualMachine,
|
||||
code: &FormatCode,
|
||||
rdr: &mut dyn Read,
|
||||
items: &mut Vec<PyObjectRef>,
|
||||
) -> PyResult<()>
|
||||
where
|
||||
Endianness: byteorder::ByteOrder,
|
||||
{
|
||||
match code.code {
|
||||
'b' => unpack_i8(vm, rdr),
|
||||
'B' => unpack_u8(vm, rdr),
|
||||
'?' => unpack_bool(vm, rdr),
|
||||
'h' => unpack_i16::<Endianness>(vm, rdr),
|
||||
'H' => unpack_u16::<Endianness>(vm, rdr),
|
||||
'i' | 'l' => unpack_i32::<Endianness>(vm, rdr),
|
||||
'I' | 'L' => unpack_u32::<Endianness>(vm, rdr),
|
||||
'q' => unpack_i64::<Endianness>(vm, rdr),
|
||||
'Q' => unpack_u64::<Endianness>(vm, rdr),
|
||||
'f' => unpack_f32::<Endianness>(vm, rdr),
|
||||
'd' => unpack_f64::<Endianness>(vm, rdr),
|
||||
let unpack = match code.code {
|
||||
'b' => unpack_i8,
|
||||
'B' => unpack_u8,
|
||||
'c' => unpack_char,
|
||||
'?' => unpack_bool,
|
||||
'h' => unpack_i16::<Endianness>,
|
||||
'H' => unpack_u16::<Endianness>,
|
||||
'i' | 'l' => unpack_i32::<Endianness>,
|
||||
'I' | 'L' => unpack_u32::<Endianness>,
|
||||
'q' => unpack_i64::<Endianness>,
|
||||
'Q' => unpack_u64::<Endianness>,
|
||||
'n' => unpack_isize::<Endianness>,
|
||||
'N' => unpack_usize::<Endianness>,
|
||||
'P' => unpack_usize::<Endianness>, // FIXME: native-only
|
||||
'f' => unpack_f32::<Endianness>,
|
||||
'd' => unpack_f64::<Endianness>,
|
||||
'x' => {
|
||||
unpack_empty(vm, rdr, code.repeat);
|
||||
return Ok(());
|
||||
}
|
||||
's' | 'p' => {
|
||||
items.push(unpack_string(vm, rdr, code.repeat)?);
|
||||
return Ok(());
|
||||
}
|
||||
c => {
|
||||
panic!("Unsupported format code {:?}", c);
|
||||
}
|
||||
};
|
||||
for _ in 0..code.repeat {
|
||||
items.push(unpack(vm, rdr)?);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn struct_calcsize(fmt: PyStringRef, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
let fmt_str = fmt.as_str();
|
||||
let format_spec = FormatSpec::parse(fmt_str).map_err(|e| vm.new_value_error(e))?;
|
||||
fn struct_calcsize(fmt: Either<PyStringRef, PyBytesRef>, vm: &VirtualMachine) -> PyResult<usize> {
|
||||
// FIXME: the given fmt must be parsed as ascii string
|
||||
// https://github.com/RustPython/RustPython/pull/1792#discussion_r387340905
|
||||
let parsed = match fmt {
|
||||
Either::A(string) => FormatSpec::parse(string.as_str()),
|
||||
Either::B(bytes) => FormatSpec::parse(std::str::from_utf8(&bytes).unwrap()),
|
||||
};
|
||||
let format_spec = parsed.map_err(|e| new_struct_error(vm, e))?;
|
||||
Ok(format_spec.size())
|
||||
}
|
||||
|
||||
@@ -436,9 +604,17 @@ impl PyValue for PyStruct {
|
||||
#[pyimpl]
|
||||
impl PyStruct {
|
||||
#[pyslot]
|
||||
fn tp_new(cls: PyClassRef, fmt_str: PyStringRef, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
|
||||
let spec = FormatSpec::parse(fmt_str.as_str()).map_err(|e| vm.new_value_error(e))?;
|
||||
|
||||
fn tp_new(
|
||||
cls: PyClassRef,
|
||||
fmt: Either<PyStringRef, PyBytesRef>,
|
||||
vm: &VirtualMachine,
|
||||
) -> PyResult<PyRef<Self>> {
|
||||
let fmt_str = match fmt {
|
||||
Either::A(s) => s,
|
||||
Either::B(b) => PyString::from(std::str::from_utf8(b.get_value()).unwrap())
|
||||
.into_ref_with_type(vm, vm.ctx.str_type())?,
|
||||
};
|
||||
let spec = FormatSpec::parse(fmt_str.as_str()).map_err(|e| new_struct_error(vm, e))?;
|
||||
PyStruct { spec, fmt_str }.into_ref_with_type(vm, cls)
|
||||
}
|
||||
|
||||
@@ -479,3 +655,9 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
|
||||
"Struct" => PyStruct::make_class(ctx),
|
||||
})
|
||||
}
|
||||
|
||||
fn new_struct_error(vm: &VirtualMachine, msg: String) -> PyBaseExceptionRef {
|
||||
// _struct.error must exist
|
||||
let class = vm.try_class("_struct", "error").unwrap();
|
||||
vm.new_exception_msg(class, msg)
|
||||
}
|
||||
Reference in new issue
Block a user