Skip to content

Commit ee3070f

Browse files
Remove outdated Python pre-3.9 code
These are manual fixes
1 parent f2d4d69 commit ee3070f

16 files changed

Lines changed: 196 additions & 271 deletions

‎src/c/test_c.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,9 @@ def find_and_load_library(name, flags=RTLD_NOW):
101101
path = None
102102
else:
103103
path = ctypes.util.find_library(name)
104-
if path is None and sys.platform == 'darwin' and sys.version_info[:2] == (3, 8):
105-
pytest.xfail("find_library usually broken on MacOS Python 3.8")
106104
if path is None and name == 'c':
107105
assert sys.platform == 'win32'
108-
assert (sys.version_info >= (3,) or
109-
'__pypy__' in sys.builtin_module_names)
110-
pytest.skip("dlopen(None) cannot work on Windows "
111-
"with PyPy or Python 3")
106+
pytest.skip("dlopen(None) cannot work on Windows")
112107
return load_library(path, flags)
113108

114109
def test_load_library():

‎src/cffi/verifier.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def _write_source(self, file=None):
161161
self._write_source_to(file)
162162
else:
163163
# Write our source file to an in memory file.
164-
f = NativeIO()
164+
f = io.StringIO()
165165
self._write_source_to(f)
166166
source_data = f.getvalue()
167167

‎testing/cffi0/backend_tests.py‎

Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import platform
33
import sys, ctypes, ctypes.util
44
from cffi import FFI, CDefError, FFIError, VerificationMissing
5-
from testing.support import *
65

76
SIZE_OF_INT = ctypes.sizeof(ctypes.c_int)
87
SIZE_OF_LONG = ctypes.sizeof(ctypes.c_long)
@@ -311,7 +310,7 @@ def test_char(self):
311310
assert bool(ffi.cast("char", 1))
312311
assert bool(ffi.cast("char", 255))
313312
pytest.raises(TypeError, ffi.new, "char*", 32)
314-
pytest.raises(TypeError, ffi.new, "char*", u+"x")
313+
pytest.raises(TypeError, ffi.new, "char*", "x")
315314
pytest.raises(TypeError, ffi.new, "char*", b"foo")
316315
#
317316
p = ffi.new("char[]", [b'a', b'b', b'\x9c'])
@@ -342,13 +341,13 @@ def check_wchar_t(self, ffi):
342341
def test_wchar_t(self):
343342
ffi = FFI(backend=self.Backend())
344343
self.check_wchar_t(ffi)
345-
assert ffi.new("wchar_t*", u+'x')[0] == u+'x'
346-
assert ffi.new("wchar_t*", u+'\u1234')[0] == u+'\u1234'
344+
assert ffi.new("wchar_t*", 'x')[0] == 'x'
345+
assert ffi.new("wchar_t*", '\u1234')[0] == '\u1234'
347346
if SIZE_OF_WCHAR > 2:
348-
assert ffi.new("wchar_t*", u+'\U00012345')[0] == u+'\U00012345'
347+
assert ffi.new("wchar_t*", '\U00012345')[0] == '\U00012345'
349348
else:
350-
pytest.raises(TypeError, ffi.new, "wchar_t*", u+'\U00012345')
351-
assert ffi.new("wchar_t*")[0] == u+'\x00'
349+
pytest.raises(TypeError, ffi.new, "wchar_t*", '\U00012345')
350+
assert ffi.new("wchar_t*")[0] == '\x00'
352351
assert int(ffi.cast("wchar_t", 300)) == 300
353352
assert not bool(ffi.cast("wchar_t", 0))
354353
assert bool(ffi.cast("wchar_t", 1))
@@ -358,40 +357,40 @@ def test_wchar_t(self):
358357
pytest.raises(TypeError, ffi.new, "wchar_t*", 32)
359358
pytest.raises(TypeError, ffi.new, "wchar_t*", "foo")
360359
#
361-
p = ffi.new("wchar_t[]", [u+'a', u+'b', u+'\u1234'])
360+
p = ffi.new("wchar_t[]", ['a', 'b', '\u1234'])
362361
assert len(p) == 3
363-
assert p[0] == u+'a'
364-
assert p[1] == u+'b' and type(p[1]) is unicode
365-
assert p[2] == u+'\u1234'
366-
p[0] = u+'x'
367-
assert p[0] == u+'x' and type(p[0]) is unicode
368-
p[1] = u+'\u1357'
369-
assert p[1] == u+'\u1357'
370-
p = ffi.new("wchar_t[]", u+"abcd")
362+
assert p[0] == 'a'
363+
assert p[1] == 'b' and type(p[1]) is str
364+
assert p[2] == '\u1234'
365+
p[0] = 'x'
366+
assert p[0] == 'x' and type(p[0]) is str
367+
p[1] = '\u1357'
368+
assert p[1] == '\u1357'
369+
p = ffi.new("wchar_t[]", "abcd")
371370
assert len(p) == 5
372-
assert p[4] == u+'\x00'
373-
p = ffi.new("wchar_t[]", u+"a\u1234b")
371+
assert p[4] == '\x00'
372+
p = ffi.new("wchar_t[]", "a\u1234b")
374373
assert len(p) == 4
375-
assert p[1] == u+'\u1234'
374+
assert p[1] == '\u1234'
376375
#
377-
p = ffi.new("wchar_t[]", u+'\U00023456')
376+
p = ffi.new("wchar_t[]", '\U00023456')
378377
if SIZE_OF_WCHAR == 2:
379378
assert len(p) == 3
380-
assert p[0] == u+'\ud84d'
381-
assert p[1] == u+'\udc56'
382-
assert p[2] == u+'\x00'
379+
assert p[0] == '\ud84d'
380+
assert p[1] == '\udc56'
381+
assert p[2] == '\x00'
383382
else:
384383
assert len(p) == 2
385-
assert p[0] == u+'\U00023456'
386-
assert p[1] == u+'\x00'
384+
assert p[0] == '\U00023456'
385+
assert p[1] == '\x00'
387386
#
388-
p = ffi.new("wchar_t[4]", u+"ab")
387+
p = ffi.new("wchar_t[4]", "ab")
389388
assert len(p) == 4
390-
assert [p[i] for i in range(4)] == [u+'a', u+'b', u+'\x00', u+'\x00']
391-
p = ffi.new("wchar_t[2]", u+"ab")
389+
assert [p[i] for i in range(4)] == ['a', 'b', '\x00', '\x00']
390+
p = ffi.new("wchar_t[2]", "ab")
392391
assert len(p) == 2
393-
assert [p[i] for i in range(2)] == [u+'a', u+'b']
394-
pytest.raises(IndexError, ffi.new, "wchar_t[2]", u+"abc")
392+
assert [p[i] for i in range(2)] == ['a', 'b']
393+
pytest.raises(IndexError, ffi.new, "wchar_t[2]", "abc")
395394

396395
def test_none_as_null_doesnt_work(self):
397396
ffi = FFI(backend=self.Backend())
@@ -596,15 +595,15 @@ def test_string_from_char_pointer(self):
596595
assert str(x) == repr(x)
597596
assert ffi.string(x) == b"x"
598597
assert ffi.string(ffi.new("char*", b"\x00")) == b""
599-
pytest.raises(TypeError, ffi.new, "char*", unicode("foo"))
598+
pytest.raises(TypeError, ffi.new, "char*", "foo")
600599

601-
def test_unicode_from_wchar_pointer(self):
600+
def test_string_from_wchar_pointer(self):
602601
ffi = FFI(backend=self.Backend())
603602
self.check_wchar_t(ffi)
604-
x = ffi.new("wchar_t*", u+"x")
605-
assert unicode(x) == unicode(repr(x))
606-
assert ffi.string(x) == u+"x"
607-
assert ffi.string(ffi.new("wchar_t*", u+"\x00")) == u+""
603+
x = ffi.new("wchar_t*", "x")
604+
assert str(x) == str(repr(x))
605+
assert ffi.string(x) == "x"
606+
assert ffi.string(ffi.new("wchar_t*", "\x00")) == ""
608607

609608
def test_string_from_char_array(self):
610609
ffi = FFI(backend=self.Backend())
@@ -627,38 +626,38 @@ def test_string_from_char_array(self):
627626
def test_string_from_wchar_array(self):
628627
ffi = FFI(backend=self.Backend())
629628
self.check_wchar_t(ffi)
630-
assert ffi.string(ffi.cast("wchar_t", "x")) == u+"x"
631-
assert ffi.string(ffi.cast("wchar_t", u+"x")) == u+"x"
629+
assert ffi.string(ffi.cast("wchar_t", "x")) == "x"
630+
assert ffi.string(ffi.cast("wchar_t", "x")) == "x"
632631
x = ffi.cast("wchar_t", "x")
633632
assert str(x) == repr(x)
634-
assert ffi.string(x) == u+"x"
633+
assert ffi.string(x) == "x"
635634
#
636-
p = ffi.new("wchar_t[]", u+"hello.")
637-
p[5] = u+'!'
638-
assert ffi.string(p) == u+"hello!"
639-
p[6] = u+'\u04d2'
640-
assert ffi.string(p) == u+"hello!\u04d2"
641-
p[3] = u+'\x00'
642-
assert ffi.string(p) == u+"hel"
643-
assert ffi.string(p, 123) == u+"hel"
635+
p = ffi.new("wchar_t[]", "hello.")
636+
p[5] = '!'
637+
assert ffi.string(p) == "hello!"
638+
p[6] = '\u04d2'
639+
assert ffi.string(p) == "hello!\u04d2"
640+
p[3] = '\x00'
641+
assert ffi.string(p) == "hel"
642+
assert ffi.string(p, 123) == "hel"
644643
with pytest.raises(IndexError):
645-
p[7] = u+'X'
644+
p[7] = 'X'
646645
#
647-
a = ffi.new("wchar_t[]", u+"hello\x00world")
646+
a = ffi.new("wchar_t[]", "hello\x00world")
648647
assert len(a) == 12
649648
p = ffi.cast("wchar_t *", a)
650-
assert ffi.string(p) == u+'hello'
651-
assert ffi.string(p, 123) == u+'hello'
652-
assert ffi.string(p, 5) == u+'hello'
653-
assert ffi.string(p, 2) == u+'he'
649+
assert ffi.string(p) == 'hello'
650+
assert ffi.string(p, 123) == 'hello'
651+
assert ffi.string(p, 5) == 'hello'
652+
assert ffi.string(p, 2) == 'he'
654653

655654
def test_fetch_const_char_p_field(self):
656655
# 'const' is ignored so far
657656
ffi = FFI(backend=self.Backend())
658657
ffi.cdef("struct foo { const char *name; };")
659658
t = ffi.new("const char[]", b"testing")
660659
s = ffi.new("struct foo*", [t])
661-
assert type(s.name) not in (bytes, str, unicode)
660+
assert type(s.name) not in (bytes, str)
662661
assert ffi.string(s.name) == b"testing"
663662
with pytest.raises(TypeError):
664663
s.name = None
@@ -670,10 +669,10 @@ def test_fetch_const_wchar_p_field(self):
670669
ffi = FFI(backend=self.Backend())
671670
self.check_wchar_t(ffi)
672671
ffi.cdef("struct foo { const wchar_t *name; };")
673-
t = ffi.new("const wchar_t[]", u+"testing")
672+
t = ffi.new("const wchar_t[]", "testing")
674673
s = ffi.new("struct foo*", [t])
675-
assert type(s.name) not in (bytes, str, unicode)
676-
assert ffi.string(s.name) == u+"testing"
674+
assert type(s.name) not in (bytes, str)
675+
assert ffi.string(s.name) == "testing"
677676
s.name = ffi.NULL
678677
assert s.name == ffi.NULL
679678

@@ -793,7 +792,7 @@ def test_char_cast(self):
793792
def test_wchar_cast(self):
794793
ffi = FFI(backend=self.Backend())
795794
self.check_wchar_t(ffi)
796-
p = ffi.cast("int", ffi.cast("wchar_t", u+'\u1234'))
795+
p = ffi.cast("int", ffi.cast("wchar_t", '\u1234'))
797796
assert int(p) == 0x1234
798797
p = ffi.cast("long long", ffi.cast("wchar_t", -1))
799798
if SIZE_OF_WCHAR == 2: # 2 bytes, unsigned
@@ -803,7 +802,7 @@ def test_wchar_cast(self):
803802
assert int(p) == -1
804803
else: # in general, it can be either signed or not
805804
assert int(p) in [-1, 0xffffffff] # e.g. on arm, both cases occur
806-
p = ffi.cast("int", u+'\u1234')
805+
p = ffi.cast("int", '\u1234')
807806
assert int(p) == 0x1234
808807

809808
def test_cast_array_to_charp(self):
@@ -1250,7 +1249,7 @@ def test_ffi_buffer_with_file(self):
12501249
pytest.skip(str(e))
12511250
f.write(ffi.buffer(a, 1000 * ffi.sizeof("int")))
12521251
f.seek(0)
1253-
assert f.read() == arraytostring(array.array('i', range(1000)))
1252+
assert f.read() == array.array('i', range(1000)).tobytes()
12541253
f.seek(0)
12551254
b = ffi.new("int[]", 1005)
12561255
f.readinto(ffi.buffer(b, 1000 * ffi.sizeof("int")))
@@ -1269,7 +1268,7 @@ def test_ffi_buffer_with_io(self):
12691268
pytest.skip(str(e))
12701269
f.write(ffi.buffer(a, 1000 * ffi.sizeof("int")))
12711270
f.seek(0)
1272-
assert f.read() == arraytostring(array.array('i', range(1000)))
1271+
assert f.read() == array.array('i', range(1000)).tobytes()
12731272
f.seek(0)
12741273
b = ffi.new("int[]", 1005)
12751274
f.readinto(ffi.buffer(b, 1000 * ffi.sizeof("int")))

‎testing/cffi0/test_ctypes.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,3 @@ def test_nested_anonymous_union(self):
4141

4242
def test_nested_anonymous_struct_2(self):
4343
pytest.skip("ctypes backend: not supported: nested anonymous union")
44-
45-
def test_CData_CType_2(self):
46-
pytest.skip("ctypes backend: not supported in Python 3: CType")
47-
backend_tests.BackendTests.test_CData_CType_2(self)

‎testing/cffi0/test_ffi_backend.py‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys, platform
22
import pytest
33
from testing.cffi0 import backend_tests, test_function, test_ownlib
4-
from testing.support import u
4+
55
from cffi import FFI
66
import _cffi_backend
77

@@ -621,32 +621,32 @@ def test_char16_t(self):
621621
ffi = FFI()
622622
x = ffi.new("char16_t[]", 5)
623623
assert len(x) == 5 and ffi.sizeof(x) == 10
624-
x[2] = u+'\u1324'
625-
assert x[2] == u+'\u1324'
626-
y = ffi.new("char16_t[]", u+'\u1234\u5678')
624+
x[2] = '\u1324'
625+
assert x[2] == '\u1324'
626+
y = ffi.new("char16_t[]", '\u1234\u5678')
627627
assert len(y) == 3
628-
assert list(y) == [u+'\u1234', u+'\u5678', u+'\x00']
629-
assert ffi.string(y) == u+'\u1234\u5678'
630-
z = ffi.new("char16_t[]", u+'\U00012345')
628+
assert list(y) == ['\u1234', '\u5678', '\x00']
629+
assert ffi.string(y) == '\u1234\u5678'
630+
z = ffi.new("char16_t[]", '\U00012345')
631631
assert len(z) == 3
632-
assert list(z) == [u+'\ud808', u+'\udf45', u+'\x00']
633-
assert ffi.string(z) == u+'\U00012345'
632+
assert list(z) == ['\ud808', '\udf45', '\x00']
633+
assert ffi.string(z) == '\U00012345'
634634

635635
def test_char32_t(self):
636636
ffi = FFI()
637637
x = ffi.new("char32_t[]", 5)
638638
assert len(x) == 5 and ffi.sizeof(x) == 20
639-
x[3] = u+'\U00013245'
640-
assert x[3] == u+'\U00013245'
641-
y = ffi.new("char32_t[]", u+'\u1234\u5678')
639+
x[3] = '\U00013245'
640+
assert x[3] == '\U00013245'
641+
y = ffi.new("char32_t[]", '\u1234\u5678')
642642
assert len(y) == 3
643-
assert list(y) == [u+'\u1234', u+'\u5678', u+'\x00']
644-
py_uni = u+'\U00012345'
643+
assert list(y) == ['\u1234', '\u5678', '\x00']
644+
py_uni = '\U00012345'
645645
z = ffi.new("char32_t[]", py_uni)
646646
assert len(z) == 2
647-
assert list(z) == [py_uni, u+'\x00'] # maybe a 2-unichars string
647+
assert list(z) == [py_uni, '\x00'] # maybe a 2-unichars string
648648
assert ffi.string(z) == py_uni
649649
if len(py_uni) == 1: # 4-bytes unicodes in Python
650-
s = ffi.new("char32_t[]", u+'\ud808\udf00')
650+
s = ffi.new("char32_t[]", '\ud808\udf00')
651651
assert len(s) == 3
652-
assert list(s) == [u+'\ud808', u+'\udf00', u+'\x00']
652+
assert list(s) == ['\ud808', '\udf00', '\x00']

‎testing/cffi0/test_ownlib.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from cffi import FFI
55
from cffi.backend_ctypes import CTypesBackend
6-
from testing.support import u, is_musl
6+
from testing.support import is_musl
77

88

99
SOURCE = """\
@@ -164,13 +164,13 @@ def setup_class(cls):
164164
encoded = None
165165
if cls.Backend is not CTypesBackend:
166166
try:
167-
unicode_name = u+'testownlibcaf\xe9'
167+
unicode_name = 'testownlibcaf\xe9'
168168
encoded = unicode_name.encode(sys.getfilesystemencoding())
169169
encoded = str(unicode_name)
170170
except UnicodeEncodeError:
171171
pass
172172
if encoded is None:
173-
unicode_name = u+'testownlib'
173+
unicode_name = 'testownlib'
174174
encoded = str(unicode_name)
175175
subprocess.check_call(
176176
"cc testownlib.c -shared -fPIC -o '%s.so'" % (encoded,),
@@ -357,9 +357,9 @@ def test_char16_char32_t(self):
357357
char32_t foo_4bytes(char32_t);
358358
""")
359359
lib = ffi.dlopen(self.module)
360-
assert lib.foo_2bytes(u+'\u1234') == u+'\u125e'
361-
assert lib.foo_4bytes(u+'\u1234') == u+'\u125e'
362-
assert lib.foo_4bytes(u+'\U00012345') == u+'\U0001236f'
360+
assert lib.foo_2bytes('\u1234') == '\u125e'
361+
assert lib.foo_4bytes('\u1234') == '\u125e'
362+
assert lib.foo_4bytes('\U00012345') == '\U0001236f'
363363

364364
def test_modify_struct_value(self):
365365
if self.module is None:

‎testing/cffi0/test_parsing.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,11 @@ def test_unknown_name():
368368
assert 'foobarbazunknown' in str(e.value)
369369

370370
def test_redefine_common_type():
371-
prefix = "" if sys.version_info < (3,) else "b"
372371
ffi = FFI()
373372
ffi.cdef("typedef char FILE;")
374-
assert repr(ffi.cast("FILE", 123)) == "<cdata 'char' %s'{'>" % prefix
373+
assert repr(ffi.cast("FILE", 123)) == "<cdata 'char' b'{'>"
375374
ffi.cdef("typedef char int32_t;")
376-
assert repr(ffi.cast("int32_t", 123)) == "<cdata 'char' %s'{'>" % prefix
375+
assert repr(ffi.cast("int32_t", 123)) == "<cdata 'char' b'{'>"
377376
ffi = FFI()
378377
ffi.cdef("typedef int bool, *FILE;")
379378
assert repr(ffi.cast("bool", 123)) == "<cdata 'int' 123>"

‎testing/cffi0/test_verify.py‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,6 @@ def test_all_integer_and_float_types():
264264
for typename in typenames:
265265
foo = getattr(lib, 'foo_%s' % typename.replace(' ', '_'))
266266
assert foo(42) == 43
267-
if sys.version < '3':
268-
assert foo(long(44)) == 45
269267
assert foo(ffi.cast(typename, 46)) == 47
270268
pytest.raises(TypeError, foo, ffi.NULL)
271269
#
@@ -390,11 +388,11 @@ def test_char_type():
390388
def test_wchar_type():
391389
ffi = FFI()
392390
if ffi.sizeof('wchar_t') == 2:
393-
uniexample1 = u+'\u1234'
394-
uniexample2 = u+'\u1235'
391+
uniexample1 = '\u1234'
392+
uniexample2 = '\u1235'
395393
else:
396-
uniexample1 = u+'\U00012345'
397-
uniexample2 = u+'\U00012346'
394+
uniexample1 = '\U00012345'
395+
uniexample2 = '\U00012346'
398396
#
399397
ffi.cdef("wchar_t foo(wchar_t);")
400398
lib = ffi.verify("wchar_t foo(wchar_t x) { return x+1; }")

0 commit comments

Comments
 (0)