summaryrefslogtreecommitdiff
path: root/c/test_c.py
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2019-06-03 20:11:03 +0200
committerArmin Rigo <arigo@tunes.org>2019-06-03 20:11:03 +0200
commitf6c550466541a029465f076e691bbcfe15251ab1 (patch)
treee15a1f375693f289a57119f22f9bdb0005709ca6 /c/test_c.py
parent32eb057f4dbe37584988b934dd46cbcc535f2397 (diff)
downloadcffi-f6c550466541a029465f076e691bbcfe15251ab1.tar.gz
Simplify the implementation, cut down the explanations, and allow
accessing multiple items instead of just one in from_buffer("type *"), trading off security for the realisation that it makes sense to access multiple items in some cases (unlike after ffi.new()).
Diffstat (limited to 'c/test_c.py')
-rw-r--r--c/test_c.py62
1 files changed, 27 insertions, 35 deletions
diff --git a/c/test_c.py b/c/test_c.py
index 4617ac1..555aafb 100644
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -3864,12 +3864,13 @@ def test_from_buffer_types():
assert p2 == p1
assert typeof(p2) is BIntP
assert p2[0] == lst[0]
- with pytest.raises(IndexError):
- p2[1]
- with pytest.raises(IndexError):
- p2[-1]
- with pytest.raises(ValueError):
- from_buffer(BIntP, b"abc") # not enough data
+ assert p2[1] == lst[1]
+ assert p2[2] == lst[2]
+ # hopefully does not crash, but doesn't raise an exception:
+ p2[3]
+ p2[-1]
+ # not enough data even for one, but this is not enforced:
+ from_buffer(BIntP, b"")
#
BIntA2 = new_array_type(BIntP, 2)
p2 = from_buffer(BIntA2, bytestring) # int[2]
@@ -3911,26 +3912,18 @@ def test_from_buffer_types():
assert p2.a2 == lst2[1]
assert p2[0].a1 == lst2[0]
assert p2[0].a2 == lst2[1]
- with pytest.raises(IndexError):
- p2[1]
- with pytest.raises(IndexError):
- p2[-1]
- with pytest.raises(ValueError):
- from_buffer(BStructP, b"1234567") # not enough data
+ assert p2[1].a1 == lst2[2]
+ assert p2[1].a2 == lst2[3]
+ # does not crash:
+ p2[2]
+ p2[-1]
+ # not enough data even for one, but this is not enforced:
+ from_buffer(BStructP, b"")
+ from_buffer(BStructP, b"1234567")
#
release(p1)
assert repr(p1) == "<cdata 'foo[]' buffer RELEASED>"
#
- # keepalive behaviour similar to newp()
- plst = []
- for i in range(100):
- plst.append(from_buffer(BStructP, buffer(newp(BStructP, [i, -i])))[0])
- if i % 10 == 5:
- import gc; gc.collect()
- for i in range(len(plst)):
- assert plst[i].a1 == i
- assert plst[i].a2 == -i
- #
BEmptyStruct = new_struct_type("empty")
complete_struct_or_union(BEmptyStruct, [], Ellipsis, 0)
assert sizeof(BEmptyStruct) == 0
@@ -3955,25 +3948,24 @@ def test_from_buffer_types():
assert pv.a1 == lst[0]
assert pv.va[0] == lst[1]
assert pv.va[1] == lst[2]
- assert sizeof(pv[0]) == 3 * size_of_int()
- assert len(pv.va) == 2
- with pytest.raises(IndexError):
- pv.va[2]
- with pytest.raises(IndexError):
- pv.va[-1]
- with pytest.raises(ValueError):
- from_buffer(BVarStructP, b"123") # not enough data
+ assert sizeof(pv[0]) == 1 * size_of_int()
+ with pytest.raises(TypeError):
+ len(pv.va)
+ # hopefully does not crash, but doesn't raise an exception:
+ pv.va[2]
+ pv.va[-1]
+ # not enough data even for one, but this is not enforced:
+ from_buffer(BVarStructP, b"")
assert repr(pv) == "<cdata 'varfoo *' buffer from 'bytearray' object>"
- assert repr(pv[0]) == "<cdata 'varfoo' buffer from 'bytearray' object>"
+ assert repr(pv[0]).startswith("<cdata 'varfoo &' ")
#
release(pv)
assert repr(pv) == "<cdata 'varfoo *' buffer RELEASED>"
- assert repr(pv[0]) == "<cdata 'varfoo' buffer RELEASED>"
+ assert repr(pv[0]).startswith("<cdata 'varfoo &' ")
#
pv = from_buffer(BVarStructP, bytestring) # make a fresh one
- release(pv[0])
- assert repr(pv) == "<cdata 'varfoo *' buffer RELEASED>"
- assert repr(pv[0]) == "<cdata 'varfoo' buffer RELEASED>"
+ with pytest.raises(ValueError):
+ release(pv[0])
def test_memmove():
Short = new_primitive_type("short")