Author: Ronan Lamy <[email protected]>
Branch: cleanup-test_lib_pypy
Changeset: r95480:da6688b781d8
Date: 2018-12-12 22:52 +0000
http://bitbucket.org/pypy/pypy/changeset/da6688b781d8/
Log: Make ctypes tests more py3-friendly
diff --git a/extra_tests/ctypes_tests/test_array.py
b/extra_tests/ctypes_tests/test_array.py
--- a/extra_tests/ctypes_tests/test_array.py
+++ b/extra_tests/ctypes_tests/test_array.py
@@ -2,7 +2,7 @@
from ctypes import *
def test_slice():
- values = range(5)
+ values = list(range(5))
numarray = c_int * 5
na = numarray(*(c_int(x) for x in values))
@@ -14,7 +14,7 @@
def test_init_again():
sz = (c_char * 3)()
addr1 = addressof(sz)
- sz.__init__(*"foo")
+ sz.__init__(*b"foo")
addr2 = addressof(sz)
assert addr1 == addr2
@@ -33,18 +33,18 @@
A = c_char * 10
TP = POINTER(A)
x = TP(A())
- assert x[0] != ''
+ assert x[0] != b''
A = c_wchar * 10
TP = POINTER(A)
x = TP(A())
- assert x[0] != ''
+ assert x[0] != b''
def test_output_simple_array():
A = c_char * 10
AA = A * 10
aa = AA()
- assert aa[0] != ''
+ assert aa[0] != b''
def test_output_complex_test():
class Car(Structure):
@@ -52,13 +52,13 @@
("speed", c_float),
("owner", c_char * 10)]
- assert isinstance(Car("abcdefghi", 42.0, "12345").brand, bytes)
- assert Car("abcdefghi", 42.0, "12345").brand == "abcdefghi"
- assert Car("abcdefghio", 42.0, "12345").brand == "abcdefghio"
+ assert isinstance(Car(b"abcdefghi", 42.0, b"12345").brand, bytes)
+ assert Car(b"abcdefghi", 42.0, b"12345").brand == b"abcdefghi"
+ assert Car(b"abcdefghio", 42.0, b"12345").brand == b"abcdefghio"
with pytest.raises(ValueError):
- Car("abcdefghiop", 42.0, "12345")
+ Car(b"abcdefghiop", 42.0, b"12345")
A = Car._fields_[2][1]
TP = POINTER(A)
x = TP(A())
- assert x[0] != ''
+ assert x[0] != b''
diff --git a/extra_tests/ctypes_tests/test_buffers.py
b/extra_tests/ctypes_tests/test_buffers.py
--- a/extra_tests/ctypes_tests/test_buffers.py
+++ b/extra_tests/ctypes_tests/test_buffers.py
@@ -11,26 +11,26 @@
assert sizeof(b) == 33 * sizeof(c_char)
assert type(b[0]) is str
- b = create_string_buffer("abc")
+ b = create_string_buffer(b"abc")
assert len(b) == 4 # trailing nul char
assert sizeof(b) == 4 * sizeof(c_char)
assert type(b[0]) is str
- assert b[0] == "a"
- assert b[:] == "abc\0"
+ assert b[0] == b"a"
+ assert b[:] == b"abc\0"
def test_from_buffer():
- b1 = bytearray("abcde")
+ b1 = bytearray(b"abcde")
b = (c_char * 5).from_buffer(b1)
- assert b[2] == "c"
+ assert b[2] == b"c"
#
- b1 = bytearray("abcd")
+ b1 = bytearray(b"abcd")
b = c_int.from_buffer(b1)
assert b.value in (1684234849, # little endian
1633837924) # big endian
def test_from_buffer_keepalive():
# Issue #2878
- b1 = bytearray("ab")
+ b1 = bytearray(b"ab")
array = (c_uint16 * 32)()
array[6] = c_uint16.from_buffer(b1)
# this is also what we get on CPython. I don't think it makes
diff --git a/extra_tests/ctypes_tests/test_callbacks.py
b/extra_tests/ctypes_tests/test_callbacks.py
--- a/extra_tests/ctypes_tests/test_callbacks.py
+++ b/extra_tests/ctypes_tests/test_callbacks.py
@@ -44,8 +44,8 @@
(c_float, -math.e),
(c_double, 3.14),
(c_double, -3.14),
- (c_char, "x"),
- (c_char, "a"),
+ (c_char, b"x"),
+ (c_char, b"a"),
])
@pytest.mark.parametrize('functype', functypes)
def test_types(typ, arg, functype):
diff --git a/extra_tests/ctypes_tests/test_extra.py
b/extra_tests/ctypes_tests/test_extra.py
--- a/extra_tests/ctypes_tests/test_extra.py
+++ b/extra_tests/ctypes_tests/test_extra.py
@@ -54,20 +54,20 @@
def test_char_p():
- x = c_char_p("hello\x00world")
- assert x.value == "hello"
- x.value = "world"
- assert x.value == "world"
+ x = c_char_p(b"hello\x00world")
+ assert x.value == b"hello"
+ x.value = b"world"
+ assert x.value == b"world"
p = pointer(x)
- assert p[0] == x.value == "world"
- p[0] = "other"
- assert x.value == p.contents.value == p[0] == "other"
+ assert p[0] == x.value == b"world"
+ p[0] = b"other"
+ assert x.value == p.contents.value == p[0] == b"other"
myarray = (c_char_p * 10)()
- myarray[7] = "hello"
+ myarray[7] = b"hello"
assert isinstance(myarray[7], str)
- assert myarray[7] == "hello"
+ assert myarray[7] == b"hello"
def test_struct():
class tagpoint(Structure):
@@ -113,33 +113,33 @@
def test_char_array():
a = (c_char * 3)()
- a[0] = 'x'
- a[1] = 'y'
- assert a.value == 'xy'
- a[2] = 'z'
- assert a.value == 'xyz'
+ a[0] = b'x'
+ a[1] = b'y'
+ assert a.value == b'xy'
+ a[2] = b'z'
+ assert a.value == b'xyz'
b = create_string_buffer(3)
assert type(b) is type(a)
assert len(b) == 3
- b.value = "nxw"
- assert b[0] == 'n'
- assert b[1] == 'x'
- assert b[2] == 'w'
+ b.value = b"nxw"
+ assert b[0] == b'n'
+ assert b[1] == b'x'
+ assert b[2] == b'w'
- b.value = "?"
- assert b[0] == '?'
- assert b[1] == '\x00'
- assert b[2] == 'w'
+ b.value = b"?"
+ assert b[0] == b'?'
+ assert b[1] == b'\x00'
+ assert b[2] == b'w'
class S(Structure):
_fields_ = [('p', POINTER(c_char))]
s = S()
s.p = b
- s.p.contents.value = '!'
- assert b.value == '!'
+ s.p.contents.value = b'!'
+ assert b.value == b'!'
assert len(create_string_buffer(0)) == 0
@@ -168,7 +168,7 @@
assert not c_int(0) # a bit strange, if you ask me
assert c_int(-1)
assert not c_byte(0)
- assert not c_char('\x00') # hum
+ assert not c_char(b'\x00') # hum
assert not c_float(0.0)
assert not c_double(0.0)
assert not c_ulonglong(0)
@@ -192,18 +192,18 @@
# automatic conversions to c_char_p
func.argtypes = [c_char_p]
- assert func("hello") == "hello"
- assert func(c_char_p("hello")) == "hello"
- assert func((c_char * 6)(*"hello")) == "hello"
- assert func(create_string_buffer("hello")) == "hello"
+ assert func(b"hello") == b"hello"
+ assert func(c_char_p(b"hello")) == b"hello"
+ assert func((c_char * 6)(*b"hello")) == b"hello"
+ assert func(create_string_buffer(b"hello")) == b"hello"
# automatic conversions to c_void_p
func.argtypes = [c_void_p]
- assert func("hello") == "hello"
- assert func(c_char_p("hello")) == "hello"
- assert func((c_char * 6)(*"hello")) == "hello"
- assert func((c_byte * 6)(104,101,108,108,111)) =="hello"
- assert func(create_string_buffer("hello")) == "hello"
+ assert func(b"hello") == b"hello"
+ assert func(c_char_p(b"hello")) == b"hello"
+ assert func((c_char * 6)(*b"hello")) == b"hello"
+ assert func((c_byte * 6)(104,101,108,108,111)) ==b"hello"
+ assert func(create_string_buffer(b"hello")) == b"hello"
def test_varsize_cast():
import struct
diff --git a/extra_tests/ctypes_tests/test_functions.py
b/extra_tests/ctypes_tests/test_functions.py
--- a/extra_tests/ctypes_tests/test_functions.py
+++ b/extra_tests/ctypes_tests/test_functions.py
@@ -12,7 +12,7 @@
f.argtypes = [c_byte, c_short, c_int, c_long, c_float, c_double]
f.restype = c_char
result = f(0, 0, 0, 0, 0, 0)
- assert result == '\x00'
+ assert result == b'\x00'
def test_boolresult(dll):
f = dll._testfunc_i_bhilfd
@@ -43,10 +43,10 @@
f.restype = c_char
f.argtypes = [POINTER(c_char_p)]
#
- s = c_char_p('hello world')
+ s = c_char_p(b'hello world')
ps = pointer(s)
- assert f(ps) == 'h'
- assert f(s) == 'h' # automatic conversion from char** to char*
+ assert f(ps) == b'h'
+ assert f(s) == b'h' # automatic conversion from char** to char*
################################################################
@@ -54,8 +54,8 @@
f = dll.my_strchr
f.argtypes = [c_char_p]
f.restype = c_char_p
- result = f("abcd", ord("b"))
- assert result == "bcd"
+ result = f(b"abcd", ord("b"))
+ assert result == b"bcd"
@pytest.mark.pypy_only
def test_keepalive_buffers(monkeypatch, dll):
@@ -72,8 +72,8 @@
return orig__call_funcptr(funcptr, *newargs)
monkeypatch.setattr(f, '_call_funcptr', _call_funcptr)
#
- result = f("abcd", ord("b"))
- assert result == "bcd"
+ result = f(b"abcd", ord("b"))
+ assert result == b"bcd"
def test_caching_bug_1(dll):
# the same test as test_call_some_args, with two extra lines
@@ -82,17 +82,17 @@
f = dll.my_strchr
f.argtypes = [c_char_p, c_int]
f.restype = c_char_p
- result = f("abcd", ord("b"))
- assert result == "bcd"
- result = f("abcd", ord("b"), 42)
- assert result == "bcd"
+ result = f(b"abcd", ord("b"))
+ assert result == b"bcd"
+ result = f(b"abcd", ord("b"), 42)
+ assert result == b"bcd"
def test_argument_conversion_and_checks(dll):
#This test is designed to check for segfaults if the wrong type of
argument is passed as parameter
strlen = dll.my_strchr
strlen.argtypes = [c_char_p, c_int]
strlen.restype = c_char_p
- assert strlen("eggs", ord("g")) == "ggs"
+ assert strlen(b"eggs", ord("g")) == b"ggs"
# Should raise ArgumentError, not segfault
with pytest.raises(ArgumentError):
@@ -205,10 +205,10 @@
get_data_signature = ('test_issue1655', dll)
get_data = get_data_prototype(get_data_signature, get_data_paramflag)
- assert get_data('testing!') == 4
+ assert get_data(b'testing!') == 4
get_data.errcheck = ret_list_p(1)
- assert get_data('testing!') == [-1, -2, -3, -4]
+ assert get_data(b'testing!') == [-1, -2, -3, -4]
def test_issue2533(tmpdir):
import cffi
diff --git a/extra_tests/ctypes_tests/test_guess_argtypes.py
b/extra_tests/ctypes_tests/test_guess_argtypes.py
--- a/extra_tests/ctypes_tests/test_guess_argtypes.py
+++ b/extra_tests/ctypes_tests/test_guess_argtypes.py
@@ -18,7 +18,7 @@
assert guess(13) == c_int
assert guess(0) == c_int
- assert guess('xca') == c_char_p
+ assert guess(b'xca') == c_char_p
assert guess(None) == c_void_p
assert guess(c_int(3)) == c_int
assert guess(u'xca') == c_wchar_p
diff --git a/extra_tests/ctypes_tests/test_keepalive.py
b/extra_tests/ctypes_tests/test_keepalive.py
--- a/extra_tests/ctypes_tests/test_keepalive.py
+++ b/extra_tests/ctypes_tests/test_keepalive.py
@@ -101,12 +101,12 @@
p = pointer(x)
assert p._objects == {'1':x}
p[0] = y
- assert p._objects.keys() == ['1']
+ assert list(p._objects.keys()) == ['1']
assert p._objects['1'].value == 3
@pytest.mark.pypy_only
def test_primitive():
- assert c_char_p("abc")._objects._buffer[0] == "a"
+ assert c_char_p(b"abc")._objects._buffer[0] == b"a"
assert c_int(3)._objects is None
def test_pointer_to_pointer():
@@ -222,13 +222,12 @@
def test_c_char_p():
n = 2
- xs = "hello" * n
+ xs = b"hello" * n
x = c_char_p(xs)
del xs
import gc; gc.collect()
- print 'x =', repr(x)
- assert x.value == 'hellohello'
- assert x._objects == 'hellohello'
+ assert x.value == b'hellohello'
+ assert x._objects == b'hellohello'
#
class datum(Structure):
_fields_ = [
@@ -242,7 +241,7 @@
]
for wrap in [False, True]:
n = 2
- xs = "hello" * n
+ xs = b"hello" * n
if wrap:
xs = c_char_p(xs)
dat = datum()
@@ -250,19 +249,15 @@
dat.dsize = 15
del xs
import gc; gc.collect()
- print 'dat.dptr =', repr(dat.dptr)
- print 'dat._objects =', repr(dat._objects)
- assert dat.dptr == "hellohello"
- assert dat._objects.keys() == ['0']
+ assert dat.dptr == b"hellohello"
+ assert list(dat._objects.keys()) == ['0']
- xs = "hello" * n
+ xs = b"hello" * n
if wrap:
xs = c_char_p(xs)
dat = union()
dat.dptr = xs
del xs
import gc; gc.collect()
- print 'dat.dptr =', repr(dat.dptr)
- print 'dat._objects =', repr(dat._objects)
- assert dat.dptr == "hellohello"
- assert dat._objects.keys() == ['0']
+ assert dat.dptr == b"hellohello"
+ assert list(dat._objects.keys()) == ['0']
diff --git a/extra_tests/ctypes_tests/test_prototypes.py
b/extra_tests/ctypes_tests/test_prototypes.py
--- a/extra_tests/ctypes_tests/test_prototypes.py
+++ b/extra_tests/ctypes_tests/test_prototypes.py
@@ -29,31 +29,31 @@
def test_kwargs(dll):
proto = CFUNCTYPE(c_char_p, c_char_p, c_int)
- paramflags = (1, 'text', "tavino"), (1, 'letter', ord('v'))
+ paramflags = (1, 'text', b"tavino"), (1, 'letter', ord('v'))
func = proto(('my_strchr', dll), paramflags)
assert func.argtypes == (c_char_p, c_int)
assert func.restype == c_char_p
- result = func("abcd", ord('b'))
- assert result == "bcd"
+ result = func(b"abcd", ord('b'))
+ assert result == b"bcd"
result = func()
- assert result == "vino"
+ assert result == b"vino"
- result = func("grapevine")
- assert result == "vine"
+ result = func(b"grapevine")
+ assert result == b"vine"
- result = func(text="grapevine")
- assert result == "vine"
+ result = func(text=b"grapevine")
+ assert result == b"vine"
result = func(letter=ord('i'))
- assert result == "ino"
+ assert result == b"ino"
- result = func(letter=ord('p'), text="impossible")
- assert result == "possible"
+ result = func(letter=ord('p'), text=b"impossible")
+ assert result == b"possible"
- result = func(text="impossible", letter=ord('p'))
- assert result == "possible"
+ result = func(text=b"impossible", letter=ord('p'))
+ assert result == b"possible"
def test_array_to_ptr_wrongtype(dll):
ARRAY = c_byte * 8
diff --git a/extra_tests/ctypes_tests/test_structures.py
b/extra_tests/ctypes_tests/test_structures.py
--- a/extra_tests/ctypes_tests/test_structures.py
+++ b/extra_tests/ctypes_tests/test_structures.py
@@ -22,8 +22,8 @@
("age", c_int))
# short enough
- p = Person("123456", 6)
- assert p.name == "123456"
+ p = Person(b"123456", 6)
+ assert p.name == b"123456"
assert p.age == 6
def test___init__():
@@ -32,11 +32,11 @@
("age", c_int))
def __init__(self, name, surname, age):
- self.name = name + ' ' + surname
+ self.name = name + b' ' + surname
self.age = age
- p = Person("John", "Doe", 25)
- assert p.name == "John Doe"
+ p = Person(b"John", b"Doe", 25)
+ assert p.name == b"John Doe"
assert p.age == 25
def test_setattr():
diff --git a/extra_tests/ctypes_tests/test_unions.py
b/extra_tests/ctypes_tests/test_unions.py
--- a/extra_tests/ctypes_tests/test_unions.py
+++ b/extra_tests/ctypes_tests/test_unions.py
@@ -8,9 +8,9 @@
stuff = Stuff()
stuff.y = ord('x') | (ord('z') << 24)
if sys.byteorder == 'little':
- assert stuff.x == 'x'
+ assert stuff.x == b'x'
else:
- assert stuff.x == 'z'
+ assert stuff.x == b'z'
def test_union_of_structures():
class Stuff(Structure):
diff --git a/extra_tests/ctypes_tests/test_values.py
b/extra_tests/ctypes_tests/test_values.py
--- a/extra_tests/ctypes_tests/test_values.py
+++ b/extra_tests/ctypes_tests/test_values.py
@@ -5,6 +5,6 @@
A testcase which accesses *values* in a dll.
"""
a_string = (c_char * 16).in_dll(dll, "a_string")
- assert a_string.raw == "0123456789abcdef"
- a_string[15] = '$'
+ assert a_string.raw == b"0123456789abcdef"
+ a_string[15:16] = b'$'
assert dll.get_a_string_char(15) == ord('$')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit