Skip to content

Commit a8a370b

Browse files
Apply ruff/pyupgrade rule UP036
UP036 Version block is outdated for minimum Python version Lots of manual fixes here, in order to discard Python 2 aliases.
1 parent 415f64e commit a8a370b

18 files changed

+184
-345
lines changed

src/c/test_c.py

Lines changed: 126 additions & 189 deletions
Large diffs are not rendered by default.

src/cffi/api.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,7 @@ def ensure(key, value):
603603
# we need 'libpypy-c.{so,dylib}', which should be by
604604
# default located in 'sys.prefix/bin' for installed
605605
# systems.
606-
if sys.version_info < (3,):
607-
pythonlib = "pypy-c"
608-
else:
609-
pythonlib = "pypy3-c"
606+
pythonlib = "pypy3-c"
610607
if hasattr(sys, 'prefix'):
611608
ensure('library_dirs', os.path.join(sys.prefix, 'bin'))
612609
# On uninstalled pypy's, the libpypy-c is typically found in

src/cffi/backend_ctypes.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
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])
11-
124
class CTypesType(type):
135
pass
146

@@ -161,7 +153,7 @@ def _newp(cls, init):
161153
return cls(init)
162154

163155
def __iter__(self):
164-
for i in xrange(len(self)):
156+
for i in range(len(self)):
165157
yield self[i]
166158

167159
def _get_own_repr(self):
@@ -183,7 +175,7 @@ def _cast_from(cls, source):
183175
address = 0
184176
elif isinstance(source, CTypesData):
185177
address = source._cast_to_integer()
186-
elif isinstance(source, (int, long)):
178+
elif isinstance(source, int):
187179
address = source
188180
else:
189181
raise TypeError("bad type for cast to %r: %r" %
@@ -399,7 +391,7 @@ def __int__(self):
399391
if kind == 'bool':
400392
@classmethod
401393
def _cast_from(cls, source):
402-
if not isinstance(source, (int, long, float)):
394+
if not isinstance(source, (int, float)):
403395
source = _cast_source_to_int(source)
404396
return cls(bool(source))
405397
def __int__(self):
@@ -409,7 +401,7 @@ def __int__(self):
409401
@classmethod
410402
def _cast_from(cls, source):
411403
source = _cast_source_to_int(source)
412-
source = bytechr(source & 0xFF)
404+
source = bytes([source & 0xFF])
413405
return cls(source)
414406
def __int__(self):
415407
return ord(self._value)
@@ -438,7 +430,7 @@ def __float__(self):
438430
if kind == 'int' or kind == 'byte' or kind == 'bool':
439431
@staticmethod
440432
def _to_ctypes(x):
441-
if not isinstance(x, (int, long)):
433+
if not isinstance(x, int):
442434
if isinstance(x, CTypesData):
443435
x = int(x)
444436
else:
@@ -471,7 +463,7 @@ def __nonzero__(self):
471463
if kind == 'float':
472464
@staticmethod
473465
def _to_ctypes(x):
474-
if not isinstance(x, (int, long, float, CTypesData)):
466+
if not isinstance(x, (int, float, CTypesData)):
475467
raise TypeError("float expected, got %s" %
476468
type(x).__name__)
477469
return ctype(x).value
@@ -535,14 +527,14 @@ def __init__(self, init):
535527
self._own = True
536528

537529
def __add__(self, other):
538-
if isinstance(other, (int, long)):
530+
if isinstance(other, int):
539531
return self._new_pointer_at(self._address +
540532
other * self._bitem_size)
541533
else:
542534
return NotImplemented
543535

544536
def __sub__(self, other):
545-
if isinstance(other, (int, long)):
537+
if isinstance(other, int):
546538
return self._new_pointer_at(self._address -
547539
other * self._bitem_size)
548540
elif type(self) is type(other):
@@ -617,7 +609,7 @@ class CTypesArray(CTypesGenericArray):
617609

618610
def __init__(self, init):
619611
if length is None:
620-
if isinstance(init, (int, long)):
612+
if isinstance(init, int):
621613
len1 = init
622614
init = None
623615
elif kind == 'char' and isinstance(init, bytes):
@@ -696,7 +688,7 @@ def _arg_to_ctypes(value):
696688
return CTypesPtr._arg_to_ctypes(value)
697689

698690
def __add__(self, other):
699-
if isinstance(other, (int, long)):
691+
if isinstance(other, long):
700692
return CTypesPtr._new_pointer_at(
701693
ctypes.addressof(self._blob) +
702694
other * ctypes.sizeof(BItem._ctype))
@@ -776,7 +768,7 @@ def initialize(blob, init):
776768
"only one supported (use a dict if needed)"
777769
% (len(init),))
778770
if not isinstance(init, dict):
779-
if isinstance(init, (bytes, unicode)):
771+
if isinstance(init, (bytes, str)):
780772
raise TypeError("union initializer: got a str")
781773
init = tuple(init)
782774
if len(init) > len(fnames):
@@ -1061,7 +1053,7 @@ def typeoffsetof(self, BType, fieldname, num=0):
10611053
if BField is Ellipsis:
10621054
raise TypeError("not supported for bitfields")
10631055
return (BField, BType._offsetof(fieldname))
1064-
elif isinstance(fieldname, (int, long)):
1056+
elif isinstance(fieldname, int):
10651057
if issubclass(BType, CTypesGenericArray):
10661058
BType = BType._CTPtr
10671059
if not issubclass(BType, CTypesGenericPtr):

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/recompiler.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,12 +1316,8 @@ def _print_string_literal_in_array(self, s):
13161316
else:
13171317
s.decode('utf-8') # got bytes, check for valid utf-8
13181318
for line in s.splitlines(True):
1319-
comment = line
1320-
if type('//') is bytes: # python2
1321-
line = map(ord, line) # make a list of integers
1322-
else: # python3
1323-
# type(line) is bytes, which enumerates like a list of integers
1324-
comment = ascii(comment)[1:-1]
1319+
# type(line) is bytes, which enumerates like a list of integers
1320+
comment = ascii(line)[1:-1]
13251321
prnt(('// ' + comment).rstrip())
13261322
printed_line = ''
13271323
for c in line:
@@ -1406,14 +1402,7 @@ def _emit_bytecode_EnumType(self, tp, index):
14061402
self.cffi_types[index] = CffiOp(OP_ENUM, enum_index)
14071403

14081404

1409-
if sys.version_info >= (3,):
1410-
NativeIO = io.StringIO
1411-
else:
1412-
class NativeIO(io.BytesIO):
1413-
def write(self, s):
1414-
if isinstance(s, unicode):
1415-
s = s.encode('ascii')
1416-
super().write(s)
1405+
NativeIO = io.StringIO
14171406

14181407
def _is_file_like(maybefile):
14191408
# compare to xml.etree.ElementTree._get_writer

src/cffi/vengine_gen.py

Lines changed: 2 additions & 6 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

@@ -497,8 +494,7 @@ def _load_known_int_constant(self, module, funcname):
497494
p = self.ffi.new(BType, 256)
498495
if function(p) < 0:
499496
error = self.ffi.string(p)
500-
if sys.version_info >= (3,):
501-
error = str(error, 'utf-8')
497+
error = str(error)
502498
raise VerificationError(error)
503499

504500
def _enum_funcname(self, prefix, name):

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!')

testing/cffi0/test_ctypes.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +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-
if sys.version_info >= (3,):
47-
pytest.skip("ctypes backend: not supported in Python 3: CType")
48-
backend_tests.BackendTests.test_CData_CType_2(self)

0 commit comments

Comments
 (0)