mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
Access _struct attributes directly instead of mucking with getattr.
string_reverse(): Simplify. assertRaises(): Raise TestFailed on failure. test_unpack_from(), test_pack_into(), test_pack_into_fn(): never use `assert` to test for an expected result (it doesn't test anything when Python is run with -O).
This commit is contained in:
parent
5332989bda
commit
852eae1bc1
1 changed files with 18 additions and 20 deletions
|
@ -1,4 +1,4 @@
|
||||||
from test.test_support import TestFailed, verbose, verify
|
from test.test_support import TestFailed, verbose, verify, vereq
|
||||||
import test.test_support
|
import test.test_support
|
||||||
import struct
|
import struct
|
||||||
import array
|
import array
|
||||||
|
@ -16,13 +16,11 @@ except ImportError:
|
||||||
PY_STRUCT_RANGE_CHECKING = 0
|
PY_STRUCT_RANGE_CHECKING = 0
|
||||||
PY_STRUCT_OVERFLOW_MASKING = 1
|
PY_STRUCT_OVERFLOW_MASKING = 1
|
||||||
else:
|
else:
|
||||||
PY_STRUCT_RANGE_CHECKING = getattr(_struct, '_PY_STRUCT_RANGE_CHECKING', 0)
|
PY_STRUCT_RANGE_CHECKING = _struct._PY_STRUCT_RANGE_CHECKING
|
||||||
PY_STRUCT_OVERFLOW_MASKING = getattr(_struct, '_PY_STRUCT_OVERFLOW_MASKING', 0)
|
PY_STRUCT_OVERFLOW_MASKING = _struct._PY_STRUCT_OVERFLOW_MASKING
|
||||||
|
|
||||||
def string_reverse(s):
|
def string_reverse(s):
|
||||||
chars = list(s)
|
return "".join(reversed(s))
|
||||||
chars.reverse()
|
|
||||||
return "".join(chars)
|
|
||||||
|
|
||||||
def bigendian_to_native(value):
|
def bigendian_to_native(value):
|
||||||
if ISBIGENDIAN:
|
if ISBIGENDIAN:
|
||||||
|
@ -504,7 +502,7 @@ def assertRaises(excClass, callableObj, *args, **kwargs):
|
||||||
except excClass:
|
except excClass:
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
raise RuntimeError("%s not raised." % excClass)
|
raise TestFailed("%s not raised." % excClass)
|
||||||
|
|
||||||
def test_unpack_from():
|
def test_unpack_from():
|
||||||
test_string = 'abcd01234'
|
test_string = 'abcd01234'
|
||||||
|
@ -512,20 +510,20 @@ def test_unpack_from():
|
||||||
s = struct.Struct(fmt)
|
s = struct.Struct(fmt)
|
||||||
for cls in (str, buffer):
|
for cls in (str, buffer):
|
||||||
data = cls(test_string)
|
data = cls(test_string)
|
||||||
assert s.unpack_from(data) == ('abcd',)
|
vereq(s.unpack_from(data), ('abcd',))
|
||||||
assert s.unpack_from(data, 2) == ('cd01',)
|
vereq(s.unpack_from(data, 2), ('cd01',))
|
||||||
assert s.unpack_from(data, 4) == ('0123',)
|
vereq(s.unpack_from(data, 4), ('0123',))
|
||||||
for i in xrange(6):
|
for i in xrange(6):
|
||||||
assert s.unpack_from(data, i) == (data[i:i+4],)
|
vereq(s.unpack_from(data, i), (data[i:i+4],))
|
||||||
for i in xrange(6, len(test_string) + 1):
|
for i in xrange(6, len(test_string) + 1):
|
||||||
simple_err(s.unpack_from, data, i)
|
simple_err(s.unpack_from, data, i)
|
||||||
for cls in (str, buffer):
|
for cls in (str, buffer):
|
||||||
data = cls(test_string)
|
data = cls(test_string)
|
||||||
assert struct.unpack_from(fmt, data) == ('abcd',)
|
vereq(struct.unpack_from(fmt, data), ('abcd',))
|
||||||
assert struct.unpack_from(fmt, data, 2) == ('cd01',)
|
vereq(struct.unpack_from(fmt, data, 2), ('cd01',))
|
||||||
assert struct.unpack_from(fmt, data, 4) == ('0123',)
|
vereq(struct.unpack_from(fmt, data, 4), ('0123',))
|
||||||
for i in xrange(6):
|
for i in xrange(6):
|
||||||
assert (struct.unpack_from(fmt, data, i) == (data[i:i+4],))
|
vereq(struct.unpack_from(fmt, data, i), (data[i:i+4],))
|
||||||
for i in xrange(6, len(test_string) + 1):
|
for i in xrange(6, len(test_string) + 1):
|
||||||
simple_err(struct.unpack_from, fmt, data, i)
|
simple_err(struct.unpack_from, fmt, data, i)
|
||||||
|
|
||||||
|
@ -538,12 +536,12 @@ def test_pack_into():
|
||||||
# Test without offset
|
# Test without offset
|
||||||
s.pack_into(writable_buf, 0, test_string)
|
s.pack_into(writable_buf, 0, test_string)
|
||||||
from_buf = writable_buf.tostring()[:len(test_string)]
|
from_buf = writable_buf.tostring()[:len(test_string)]
|
||||||
assert from_buf == test_string
|
vereq(from_buf, test_string)
|
||||||
|
|
||||||
# Test with offset.
|
# Test with offset.
|
||||||
s.pack_into(writable_buf, 10, test_string)
|
s.pack_into(writable_buf, 10, test_string)
|
||||||
from_buf = writable_buf.tostring()[:len(test_string)+10]
|
from_buf = writable_buf.tostring()[:len(test_string)+10]
|
||||||
assert from_buf == (test_string[:10] + test_string)
|
vereq(from_buf, test_string[:10] + test_string)
|
||||||
|
|
||||||
# Go beyond boundaries.
|
# Go beyond boundaries.
|
||||||
small_buf = array.array('c', ' '*10)
|
small_buf = array.array('c', ' '*10)
|
||||||
|
@ -556,15 +554,15 @@ def test_pack_into_fn():
|
||||||
fmt = '21s'
|
fmt = '21s'
|
||||||
pack_into = lambda *args: struct.pack_into(fmt, *args)
|
pack_into = lambda *args: struct.pack_into(fmt, *args)
|
||||||
|
|
||||||
# Test without offset
|
# Test without offset.
|
||||||
pack_into(writable_buf, 0, test_string)
|
pack_into(writable_buf, 0, test_string)
|
||||||
from_buf = writable_buf.tostring()[:len(test_string)]
|
from_buf = writable_buf.tostring()[:len(test_string)]
|
||||||
assert from_buf == test_string
|
vereq(from_buf, test_string)
|
||||||
|
|
||||||
# Test with offset.
|
# Test with offset.
|
||||||
pack_into(writable_buf, 10, test_string)
|
pack_into(writable_buf, 10, test_string)
|
||||||
from_buf = writable_buf.tostring()[:len(test_string)+10]
|
from_buf = writable_buf.tostring()[:len(test_string)+10]
|
||||||
assert from_buf == (test_string[:10] + test_string)
|
vereq(from_buf, test_string[:10] + test_string)
|
||||||
|
|
||||||
# Go beyond boundaries.
|
# Go beyond boundaries.
|
||||||
small_buf = array.array('c', ' '*10)
|
small_buf = array.array('c', ' '*10)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue