Skip to content

Commit f2d4d69

Browse files
Apply ruff/pyupgrade rule UP036
These are automatic fixes using: * `--select UP036 --fix --unsafe-fixes`
1 parent b559cfe commit f2d4d69

17 files changed

Lines changed: 70 additions & 174 deletions

‎src/c/test_c.py‎

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,17 @@ def _capture_unraisable_hook(ur_args):
6969
assert __version__ == "2.2.0.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")
@@ -1687,10 +1673,6 @@ def test_enum_in_struct():
16871673
"expected integer, got NoneType object" in msg) # newer PyPys
16881674
with pytest.raises(TypeError):
16891675
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
16941676

16951677
def test_enum_overflow():
16961678
max_uint = 2 ** (size_of_int()*8) - 1
@@ -2485,11 +2467,7 @@ def test_buffer():
24852467
assert repr(buf).startswith('<_cffi_backend.buffer object at 0x')
24862468
assert bytes(buf) == b"hi there\x00"
24872469
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)
2470+
assert str(buf) == repr(buf)
24932471
# --mb_length--
24942472
assert len(buf) == len(b"hi there\x00")
24952473
# --mb_item--
@@ -3015,12 +2993,11 @@ def test_string_assignment_to_byte_array():
30152993
assert list(p) == [ord("X"), ord("Y"), ord("Z"), 0, 0]
30162994

30172995
# XXX hack
3018-
if sys.version_info >= (3,):
3019-
try:
3020-
import posix
3021-
posix.fdopen = open
3022-
except ImportError:
3023-
pass # win32
2996+
try:
2997+
import posix
2998+
posix.fdopen = open
2999+
except ImportError:
3000+
pass # win32
30243001

30253002
@pytest.mark.skipif(
30263003
is_ios,
@@ -3838,16 +3815,14 @@ def test_from_buffer_more_cases():
38383815
def check1(bufobj, expected):
38393816
c = from_buffer(BCharA, bufobj)
38403817
assert typeof(c) is BCharA
3841-
if sys.version_info >= (3,):
3842-
expected = [bytes(c, "ascii") for c in expected]
3818+
expected = [bytes(c, "ascii") for c in expected]
38433819
assert list(c) == list(expected)
38443820
#
38453821
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
3822+
if methods <= 7:
3823+
return
3824+
if expected_for_memoryview is not None:
3825+
expected = expected_for_memoryview
38513826
class X:
38523827
pass
38533828
_testbuff(X, methods)

‎src/cffi/api.py‎

Lines changed: 1 addition & 4 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

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,14 +1398,7 @@ def _emit_bytecode_EnumType(self, tp, index):
13981398
self.cffi_types[index] = CffiOp(OP_ENUM, enum_index)
13991399

14001400

1401-
if sys.version_info >= (3,):
1402-
NativeIO = io.StringIO
1403-
else:
1404-
class NativeIO(io.BytesIO):
1405-
def write(self, s):
1406-
if isinstance(s, unicode):
1407-
s = s.encode('ascii')
1408-
super().write(s)
1401+
NativeIO = io.StringIO
14091402

14101403
def _is_file_like(maybefile):
14111404
# 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, 'utf-8')
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,5 @@ def test_nested_anonymous_struct_2(self):
4343
pytest.skip("ctypes backend: not supported: nested anonymous union")
4444

4545
def test_CData_CType_2(self):
46-
if sys.version_info >= (3,):
47-
pytest.skip("ctypes backend: not supported in Python 3: CType")
46+
pytest.skip("ctypes backend: not supported in Python 3: CType")
4847
backend_tests.BackendTests.test_CData_CType_2(self)

0 commit comments

Comments
 (0)