Created
August 5, 2021 14:58
-
-
Save TheMatt2/7d3646db897ed61da39ed3b566d51f91 to your computer and use it in GitHub Desktop.
Demonstration script for Jython bug with array module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import array | |
def test_signed(typecode): | |
try: | |
a = array.array(typecode) | |
# Create byte representation of exactly one item | |
byte_item = bytes() | |
for i in range(a.itemsize): | |
# Add a byte | |
byte_item += str(i).encode() | |
try: | |
a.frombytes(byte_item) | |
except AttributeError: | |
# Python 2 uses fromstring | |
a.fromstring(byte_item) | |
if len(a) != 1: | |
print("Test %s: itemsize does not match grouping" % typecode) | |
return | |
# Byteswapping twice should be the same as not byteswapping at all, test that | |
a.byteswap() | |
list_a = a.tolist() | |
a.byteswap() | |
a.byteswap() | |
list_b = a.tolist() | |
except: | |
exc_type, exc_value, exc_traceback = sys.exc_info() | |
print("Test %s: error %r" % (typecode, exc_value)) | |
return | |
if list_a != list_b: | |
print("Test %c: byteswapping twice returned different results" % typecode) | |
return | |
print("Test %s: passed" % typecode) | |
def test_unsigned(typecode): | |
try: | |
a = array.array(typecode) | |
# Create byte representation of exactly one item | |
byte_item = bytes() | |
for i in range(a.itemsize): | |
# Add a byte | |
byte_item += str(i).encode() | |
try: | |
a.frombytes(byte_item) | |
except AttributeError: | |
# Python 2 uses fromstring | |
a.fromstring(byte_item) | |
if len(a) != 1: | |
print("Test %s: itemsize does not match grouping" % typecode) | |
return | |
# Byteswapping twice should be the same as not byteswapping at all, test that | |
a.byteswap() | |
list_a = a.tolist() | |
a.byteswap() | |
a.byteswap() | |
list_b = a.tolist() | |
except: | |
exc_type, exc_value, exc_traceback = sys.exc_info() | |
print("Test %s: error %r" % (typecode, exc_value)) | |
return | |
if list_a != list_b: | |
print("Test %s: byteswapping twice returned different results" % typecode) | |
return | |
# Make sure all are positive | |
for item in list_a: | |
if item < 0: | |
print("Test %s: unsigned type returned negative byteswap value" % typecode) | |
return | |
print("Test %s: passed" % typecode) | |
def test_string(typecode): | |
# c is char type, u is unicode type | |
if typecode == "u": | |
literal = u"hello" | |
else: | |
literal = "hello" | |
try: | |
a = array.array(typecode) | |
a.fromlist(list(literal)) | |
# Byteswapping twice should be the same as not byteswapping at all, test that | |
a.byteswap() | |
list_a = a.tolist() | |
a.byteswap() | |
a.byteswap() | |
list_b = a.tolist() | |
except: | |
exc_type, exc_value, exc_traceback = sys.exc_info() | |
print("Test %s: error %r" % (typecode, exc_value)) | |
return | |
if list_a != list_b: | |
print("Test %s: byteswapping twice returned different results" % typecode) | |
return | |
print("Test %s: passed" % typecode) | |
# Try all typecodes | |
typecodes = [] | |
for c in range(256): | |
c = chr(c) | |
try: | |
a = array.array(c) | |
except ValueError: | |
pass | |
else: | |
typecodes.append(c) | |
typecodes = "".join(sorted(typecodes)) | |
print("Typecodes: %s" % typecodes) | |
for typecode in typecodes: | |
if typecode == "u" or typecode == "c": | |
test_string(typecode) | |
elif typecode.isupper(): | |
test_unsigned(typecode) | |
else: | |
test_signed(typecode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment