diff options
| author | Jeff Epler <jepler@gmail.com> | 2020-09-12 13:57:31 -0500 |
|---|---|---|
| committer | Jeff Epler <jepler@gmail.com> | 2020-09-12 14:07:23 -0500 |
| commit | 54d97251fe2dd4939652a186bf703885e654b4d1 (patch) | |
| tree | daa39c435669778a4c1b02dbc4c47e087fd80db6 /tests | |
| parent | b24d3b886af1d4fbbe481961a3b3215fcc0ba799 (diff) | |
modstruct: Improve compliance with python3
While checking whether we can enable -Wimplicit-fallthrough, I encountered
a diagnostic in mp_binary_set_val_array_from_int which led to discovering
the following bug:
```
>>> struct.pack("xb", 3)
b'\x03\x03'
```
That is, the next value (3) was used as the value of a padding byte, while
standard Python always fills "x" bytes with zeros. I initially thought
this had to do with the unintentional fallthrough, but it doesn't.
Instead, this code would relate to an array.array with a typecode of
padding ('x'), which is ALSO not desktop Python compliant:
```
>>> array.array('x', (1, 2, 3))
array('x', [1, 0, 0])
```
Possibly this is dead code that used to be shared between struct-setting
and array-setting, but it no longer is.
I also discovered that the argument list length for struct.pack
and struct.pack_into were not checked, and that the length of binary data
passed to array.array was not checked to be a multiple of the element
size.
I have corrected all of these to conform more closely to standard Python
and revised some tests where necessary. Some tests for micropython-specific
behavior that does not conform to standard Python and is not present
in CircuitPython was deleted outright.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/basics/struct1.py | 25 | ||||
| -rw-r--r-- | tests/basics/struct_micropython.py | 41 | ||||
| -rw-r--r-- | tests/basics/struct_micropython.py.exp | 2 | ||||
| -rw-r--r-- | tests/misc/non_compliant.py | 6 | ||||
| -rw-r--r-- | tests/misc/non_compliant.py.exp | 2 |
5 files changed, 25 insertions, 51 deletions
diff --git a/tests/basics/struct1.py b/tests/basics/struct1.py index db34342a1..107006cc3 100644 --- a/tests/basics/struct1.py +++ b/tests/basics/struct1.py @@ -39,6 +39,28 @@ print(v == (10, 100, 200, 300)) # network byte order print(struct.pack('!i', 123)) +# too short / too long arguments +buf = bytearray(b'>>>123<<<') +try: + struct.pack_into('bb', buf, 0, 3) +except: + print('struct.error') + +try: + struct.pack_into('bb', buf, 0, 3, 1, 4) +except: + print('struct.error') + +try: + struct.pack('bb', 3) +except: + print('struct.error') + +try: + struct.pack('bb', 3, 1, 4) +except: + print('struct.error') + # check that we get an error if the buffer is too small try: struct.unpack('I', b'\x00\x00\x00') @@ -96,3 +118,6 @@ try: print(struct.unpack_from('<b', buf, -11)) except: print('struct.error') + +# check padding bytes +print(struct.pack("xb", 3)) diff --git a/tests/basics/struct_micropython.py b/tests/basics/struct_micropython.py deleted file mode 100644 index e72153071..000000000 --- a/tests/basics/struct_micropython.py +++ /dev/null @@ -1,41 +0,0 @@ -# test MicroPython-specific features of struct - -try: - import ustruct as struct -except: - try: - import struct - except ImportError: - print("SKIP") - raise SystemExit - -try: - struct.pack('O', None) -except ValueError: - print("SKIP") - raise SystemExit - -class A(): - pass - -# pack and unpack objects -o = A() -s = struct.pack("<O", o) -o2 = struct.unpack("<O", s) -print(o is o2[0]) - -# pack can accept less arguments than required for the format spec -print(struct.pack('<2I', 1)) - -# pack and unpack pointer to a string -# This requires uctypes to get the address of the string and instead of -# putting this in a dedicated test that can be skipped we simply pass -# if the import fails. -try: - import uctypes - o = uctypes.addressof('abc') - s = struct.pack("<S", o) - o2 = struct.unpack("<S", s) - assert o2[0] == 'abc' -except ImportError: - pass diff --git a/tests/basics/struct_micropython.py.exp b/tests/basics/struct_micropython.py.exp deleted file mode 100644 index 55b7b6623..000000000 --- a/tests/basics/struct_micropython.py.exp +++ /dev/null @@ -1,2 +0,0 @@ -True -b'\x01\x00\x00\x00\x00\x00\x00\x00' diff --git a/tests/misc/non_compliant.py b/tests/misc/non_compliant.py index 99633416a..9a746a8b0 100644 --- a/tests/misc/non_compliant.py +++ b/tests/misc/non_compliant.py @@ -105,12 +105,6 @@ try: except NotImplementedError: print('NotImplementedError') -# struct pack with too many args, not checked by uPy -print(struct.pack('bb', 1, 2, 3)) - -# struct pack with too few args, not checked by uPy -print(struct.pack('bb', 1)) - # array slice assignment with unsupported RHS try: bytearray(4)[0:1] = [1, 2] diff --git a/tests/misc/non_compliant.py.exp b/tests/misc/non_compliant.py.exp index 3f15a1440..8518828ec 100644 --- a/tests/misc/non_compliant.py.exp +++ b/tests/misc/non_compliant.py.exp @@ -14,8 +14,6 @@ NotImplementedError NotImplementedError NotImplementedError NotImplementedError -b'\x01\x02' -b'\x01\x00' NotImplementedError AttributeError TypeError |
