Skip to content

Commit 2240429

Browse files
Get rid of sys.version_info < (3,9) tests
1 parent 938ddcd commit 2240429

22 files changed

Lines changed: 70 additions & 200 deletions

src/c/test_c.py

Lines changed: 24 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,17 @@ def _capture_unraisable_hook(ur_args):
6969
assert __version__ == "2.0.1.dev0", ("This test_c.py file is for testing a version"
7070
" of cffi that differs from the one that we"
7171
" get from 'import _cffi_backend'")
72-
if sys.version_info < (3,):
73-
type_or_class = "type"
74-
mandatory_b_prefix = ''
75-
mandatory_u_prefix = 'u'
76-
bytechr = chr
77-
bitem2bchr = lambda x: x
78-
class U:
79-
def __add__(self, other):
80-
return eval('u'+repr(other).replace(r'\\u', r'\u')
81-
.replace(r'\\U', r'\U'))
82-
u = U()
83-
str2bytes = str
84-
strict_compare = False
85-
else:
86-
type_or_class = "class"
87-
long = int
88-
unicode = str
89-
unichr = chr
90-
mandatory_b_prefix = 'b'
91-
mandatory_u_prefix = ''
92-
bytechr = lambda n: bytes([n])
93-
bitem2bchr = bytechr
94-
u = ""
95-
str2bytes = lambda s: bytes(s, "ascii")
96-
strict_compare = True
72+
type_or_class = "class"
73+
long = int
74+
unicode = str
75+
unichr = chr
76+
mandatory_b_prefix = 'b'
77+
mandatory_u_prefix = ''
78+
bytechr = lambda n: bytes([n])
79+
bitem2bchr = bytechr
80+
u = ""
81+
str2bytes = lambda s: bytes(s, "ascii")
82+
strict_compare = True
9783

9884
def size_of_int():
9985
BInt = new_primitive_type("int")
@@ -115,14 +101,9 @@ def find_and_load_library(name, flags=RTLD_NOW):
115101
path = None
116102
else:
117103
path = ctypes.util.find_library(name)
118-
if path is None and sys.platform == 'darwin' and sys.version_info[:2] == (3, 8):
119-
pytest.xfail("find_library usually broken on MacOS Python 3.8")
120104
if path is None and name == 'c':
121105
assert sys.platform == 'win32'
122-
assert (sys.version_info >= (3,) or
123-
'__pypy__' in sys.builtin_module_names)
124-
pytest.skip("dlopen(None) cannot work on Windows "
125-
"with PyPy or Python 3")
106+
pytest.skip("dlopen(None) cannot work on Windows")
126107
return load_library(path, flags)
127108

128109
def test_load_library():
@@ -1687,10 +1668,6 @@ def test_enum_in_struct():
16871668
"expected integer, got NoneType object" in msg) # newer PyPys
16881669
with pytest.raises(TypeError):
16891670
p.a1 = "def"
1690-
if sys.version_info < (3,):
1691-
BEnum2 = new_enum_type(unicode("foo"), (unicode('abc'),), (5,), BInt)
1692-
assert string(cast(BEnum2, 5)) == 'abc'
1693-
assert type(string(cast(BEnum2, 5))) is str
16941671

16951672
def test_enum_overflow():
16961673
max_uint = 2 ** (size_of_int()*8) - 1
@@ -2024,8 +2001,6 @@ def test_string_byte():
20242001
BArray = new_array_type(new_pointer_type(BByte), None)
20252002
a = newp(BArray, [65, 66, 67])
20262003
assert type(string(a)) is bytes and string(a) == b'ABC'
2027-
if 'PY_DOT_PY' not in globals() and sys.version_info < (3,):
2028-
assert string(a, 8).startswith(b'ABC') # may contain additional garbage
20292004

20302005
def test_string_wchar():
20312006
for typename in ["wchar_t", "char16_t", "char32_t"]:
@@ -2039,12 +2014,6 @@ def _test_string_wchar_variant(typename):
20392014
BArray = new_array_type(new_pointer_type(BWChar), None)
20402015
a = newp(BArray, [u+'A', u+'B', u+'C'])
20412016
assert type(string(a)) is unicode and string(a) == u+'ABC'
2042-
if 'PY_DOT_PY' not in globals() and sys.version_info < (3,):
2043-
try:
2044-
# may contain additional garbage
2045-
assert string(a, 8).startswith(u+'ABC')
2046-
except ValueError: # garbage contains values > 0x10FFFF
2047-
assert sizeof(BWChar) == 4
20482017

20492018
def test_string_typeerror():
20502019
BShort = new_primitive_type("short")
@@ -2485,11 +2454,7 @@ def test_buffer():
24852454
assert repr(buf).startswith('<_cffi_backend.buffer object at 0x')
24862455
assert bytes(buf) == b"hi there\x00"
24872456
assert type(buf) is buffer
2488-
if sys.version_info < (3,):
2489-
assert str(buf) == "hi there\x00"
2490-
assert unicode(buf) == u+"hi there\x00"
2491-
else:
2492-
assert str(buf) == repr(buf)
2457+
assert str(buf) == repr(buf)
24932458
# --mb_length--
24942459
assert len(buf) == len(b"hi there\x00")
24952460
# --mb_item--
@@ -3015,12 +2980,11 @@ def test_string_assignment_to_byte_array():
30152980
assert list(p) == [ord("X"), ord("Y"), ord("Z"), 0, 0]
30162981

30172982
# XXX hack
3018-
if sys.version_info >= (3,):
3019-
try:
3020-
import posix
3021-
posix.fdopen = open
3022-
except ImportError:
3023-
pass # win32
2983+
try:
2984+
import posix
2985+
posix.fdopen = open
2986+
except ImportError:
2987+
pass # win32
30242988

30252989
@pytest.mark.skipif(
30262990
is_ios,
@@ -3107,7 +3071,7 @@ def test_FILE_object():
31073071
res = fputs(b"hello\n", fw1p)
31083072
assert res >= 0
31093073
res = fileno(fw1p)
3110-
assert (res == fdw) == (sys.version_info < (3,))
3074+
assert res != fdw
31113075
fw1.close()
31123076
#
31133077
data = posix.read(fdr, 256)
@@ -3838,16 +3802,14 @@ def test_from_buffer_more_cases():
38383802
def check1(bufobj, expected):
38393803
c = from_buffer(BCharA, bufobj)
38403804
assert typeof(c) is BCharA
3841-
if sys.version_info >= (3,):
3842-
expected = [bytes(c, "ascii") for c in expected]
3805+
expected = [bytes(c, "ascii") for c in expected]
38433806
assert list(c) == list(expected)
38443807
#
38453808
def check(methods, expected, expected_for_memoryview=None):
3846-
if sys.version_info >= (3,):
3847-
if methods <= 7:
3848-
return
3849-
if expected_for_memoryview is not None:
3850-
expected = expected_for_memoryview
3809+
if methods <= 7:
3810+
return
3811+
if expected_for_memoryview is not None:
3812+
expected = expected_for_memoryview
38513813
class X:
38523814
pass
38533815
_testbuff(X, methods)

src/cffi/api.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,7 @@ def ensure(key, value):
596596
# we need 'libpypy-c.{so,dylib}', which should be by
597597
# default located in 'sys.prefix/bin' for installed
598598
# systems.
599-
if sys.version_info < (3,):
600-
pythonlib = "pypy-c"
601-
else:
602-
pythonlib = "pypy3-c"
599+
pythonlib = "pypy3-c"
603600
if hasattr(sys, 'prefix'):
604601
ensure('library_dirs', os.path.join(sys.prefix, 'bin'))
605602
# On uninstalled pypy's, the libpypy-c is typically found in
@@ -812,8 +809,8 @@ def _load_backend_lib(backend, name, flags):
812809
import ctypes.util
813810
path = ctypes.util.find_library(name)
814811
if path is None:
815-
if name == "c" and sys.platform == "win32" and sys.version_info >= (3,):
816-
raise OSError("dlopen(None) cannot work on Windows for Python 3 "
812+
if name == "c" and sys.platform == "win32":
813+
raise OSError("dlopen(None) cannot work on Windows "
817814
"(see http://bugs.python.org/issue23606)")
818815
msg = ("ctypes.util.find_library() did not manage "
819816
"to locate a library called %r" % (name,))

src/cffi/backend_ctypes.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import ctypes, ctypes.util, operator, sys
22
from . import model
33

4-
if sys.version_info < (3,):
5-
bytechr = chr
6-
else:
7-
unicode = str
8-
long = int
9-
xrange = range
10-
bytechr = lambda num: bytes([num])
4+
unicode = str
5+
long = int
6+
xrange = range
7+
bytechr = lambda num: bytes([num])
118

129
class CTypesType(type):
1310
pass

src/cffi/cparser.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import weakref, re, sys
99

1010
try:
11-
if sys.version_info < (3,):
12-
import thread as _thread
13-
else:
14-
import _thread
11+
import _thread
1512
lock = _thread.allocate_lock()
1613
except ImportError:
1714
lock = None

src/cffi/lock.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import sys
22

3-
if sys.version_info < (3,):
4-
try:
5-
from thread import allocate_lock
6-
except ImportError:
7-
from dummy_thread import allocate_lock
8-
else:
9-
try:
10-
from _thread import allocate_lock
11-
except ImportError:
12-
from _dummy_thread import allocate_lock
3+
try:
4+
from _thread import allocate_lock
5+
except ImportError:
6+
from _dummy_thread import allocate_lock
137

148

159
##import sys

src/cffi/pkgconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def call(libname, flag, encoding=sys.getfilesystemencoding()):
4242
pass
4343
raise PkgConfigError(berr.strip())
4444

45-
if sys.version_info >= (3,) and not isinstance(bout, str): # Python 3.x
45+
if not isinstance(bout, str):
4646
try:
4747
bout = bout.decode(encoding)
4848
except UnicodeDecodeError:

src/cffi/recompiler.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
VERSION_EMBEDDED = 0x2701
88
VERSION_CHAR16CHAR32 = 0x2801
99

10-
USE_LIMITED_API = ((sys.platform != 'win32' or sys.version_info < (3, 0) or
11-
sys.version_info >= (3, 5)) and
12-
not sysconfig.get_config_var("Py_GIL_DISABLED")) # free-threaded doesn't yet support limited API
10+
USE_LIMITED_API = not sysconfig.get_config_var("Py_GIL_DISABLED") # free-threaded doesn't yet support limited API
1311

1412
class GlobalExpr:
1513
def __init__(self, name, address, type_op, size=0, check_value=0):
@@ -1387,14 +1385,7 @@ def _emit_bytecode_EnumType(self, tp, index):
13871385
self.cffi_types[index] = CffiOp(OP_ENUM, enum_index)
13881386

13891387

1390-
if sys.version_info >= (3,):
1391-
NativeIO = io.StringIO
1392-
else:
1393-
class NativeIO(io.BytesIO):
1394-
def write(self, s):
1395-
if isinstance(s, unicode):
1396-
s = s.encode('ascii')
1397-
super().write(s)
1388+
NativeIO = io.StringIO
13981389

13991390
def _is_file_like(maybefile):
14001391
# compare to xml.etree.ElementTree._get_writer

src/cffi/vengine_gen.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ def write_source_to_f(self):
5555
# 'export_symbols', so instead of fighting it, just give up and
5656
# give it one
5757
if sys.platform == 'win32':
58-
if sys.version_info >= (3,):
59-
prefix = 'PyInit_'
60-
else:
61-
prefix = 'init'
58+
prefix = 'PyInit_'
6259
modname = self.verifier.get_module_name()
6360
prnt("void %s%s(void) { }\n" % (prefix, modname))
6461

src/cffi/verifier.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,12 @@
66
from . import ffiplatform
77
from .error import VerificationError
88

9-
if sys.version_info >= (3, 3):
10-
import importlib.machinery
11-
def _extension_suffixes():
12-
return importlib.machinery.EXTENSION_SUFFIXES[:]
13-
else:
14-
import imp
15-
def _extension_suffixes():
16-
return [suffix for suffix, _, type in imp.get_suffixes()
17-
if type == imp.C_EXTENSION]
18-
19-
20-
if sys.version_info >= (3,):
21-
NativeIO = io.StringIO
22-
else:
23-
class NativeIO(io.BytesIO):
24-
def write(self, s):
25-
if isinstance(s, unicode):
26-
s = s.encode('ascii')
27-
super().write(s)
9+
import importlib.machinery
10+
def _extension_suffixes():
11+
return importlib.machinery.EXTENSION_SUFFIXES[:]
12+
13+
14+
NativeIO = io.StringIO
2815

2916

3017
class Verifier:
@@ -54,8 +41,7 @@ def __init__(self, ffi, preamble, tmpdir=None, modulename=None,
5441
__version_verifier_modules__,
5542
preamble, flattened_kwds] +
5643
ffi._cdefsources)
57-
if sys.version_info >= (3,):
58-
key = key.encode('utf-8')
44+
key = key.encode('utf-8')
5945
k1 = hex(binascii.crc32(key[0::2]) & 0xffffffff)
6046
k1 = k1.lstrip('0x').rstrip('L')
6147
k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff)

testing/cffi0/backend_tests.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,8 +1279,7 @@ def test_ffi_buffer_with_io(self):
12791279
def test_ffi_buffer_comparisons(self):
12801280
ffi = FFI(backend=self.Backend())
12811281
ba = bytearray(range(100, 110))
1282-
if sys.version_info >= (2, 7):
1283-
assert ba == memoryview(ba) # justification for the following
1282+
assert ba == memoryview(ba) # justification for the following
12841283
a = ffi.new("uint8_t[]", list(ba))
12851284
c = ffi.new("uint8_t[]", [99] + list(ba))
12861285
try:
@@ -1968,10 +1967,7 @@ def do_init():
19681967

19691968
def test_init_once_multithread(self):
19701969
import sys, time
1971-
if sys.version_info < (3,):
1972-
import thread
1973-
else:
1974-
import _thread as thread
1970+
import _thread as thread
19751971
#
19761972
def do_init():
19771973
seen.append('init!')

0 commit comments

Comments
 (0)