From 807864b886f0784874ff98bcd9e3da09913fcbab Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 22 Nov 2025 14:58:53 +0100 Subject: [PATCH 1/5] Apply ruff/flake8-simplify rule SIM101 SIM101 Multiple `isinstance` calls for `tp`, merge into a single call --- src/cffi/recompiler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cffi/recompiler.py b/src/cffi/recompiler.py index 495b897a..2e049337 100644 --- a/src/cffi/recompiler.py +++ b/src/cffi/recompiler.py @@ -538,8 +538,7 @@ def _convert_funcarg_to_c(self, tp, fromvar, tovar, errcode): tovar, errcode) return # - elif (isinstance(tp, model.StructOrUnionOrEnum) or - isinstance(tp, model.BasePrimitiveType)): + elif (isinstance(tp, (model.StructOrUnionOrEnum, model.BasePrimitiveType))): # a struct (not a struct pointer) as a function argument; # or, a complex (the same code works) self._prnt(' if (_cffi_to_c((char *)&%s, _cffi_type(%d), %s) < 0)' From 183844876d9cee9b5de91245ac3734a5aaed89b5 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 22 Nov 2025 14:59:30 +0100 Subject: [PATCH 2/5] Apply ruff/flake8-simplify rule SIM103 SIM103 Return the condition directly --- demo/_curses.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/demo/_curses.py b/demo/_curses.py index 60a21e8f..504f42bc 100644 --- a/demo/_curses.py +++ b/demo/_curses.py @@ -513,9 +513,7 @@ def is_linetouched(self, line): code = lib.is_linetouched(self._win, line) if code == lib.ERR: raise error("is_linetouched: line number outside of boundaries") - if code == lib.FALSE: - return False - return True + return code != lib.FALSE def noutrefresh(self, *args): if lib._m_ispad(self._win): From 2cb459979978953829d5f7babce7a3ef792947b1 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 22 Nov 2025 15:04:54 +0100 Subject: [PATCH 3/5] Partially apply ruff/flake8-simplify rule SIM115 SIM115 Use a context manager for opening files --- src/cffi/recompiler.py | 6 ++---- testing/cffi0/test_verify.py | 10 ++++------ testing/cffi1/test_verify1.py | 5 ++--- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/cffi/recompiler.py b/src/cffi/recompiler.py index 2e049337..83ee5c7d 100644 --- a/src/cffi/recompiler.py +++ b/src/cffi/recompiler.py @@ -287,10 +287,8 @@ def write_source_to_f(self, f, preamble): self.write_c_source_to_f(f, preamble) def _rel_readlines(self, filename): - g = open(os.path.join(os.path.dirname(__file__), filename), 'r') - lines = g.readlines() - g.close() - return lines + with open(os.path.join(os.path.dirname(__file__), filename), 'r') as g: + return g.readlines() def write_c_source_to_f(self, f, preamble): self._f = f diff --git a/testing/cffi0/test_verify.py b/testing/cffi0/test_verify.py index f7a9c881..1d3ddd40 100644 --- a/testing/cffi0/test_verify.py +++ b/testing/cffi0/test_verify.py @@ -38,9 +38,8 @@ def setup_module(): def _write_source_and_check(self, file=None): base_write_source(self, file) if file is None: - f = open(self.sourcefilename) - data = f.read() - f.close() + with open(self.sourcefilename) as f: + data = f.read() data = _r_comment.sub(' ', data) data = _r_string.sub('"skipped"', data) assert '$' not in data @@ -1440,9 +1439,8 @@ def test_relative_to(): tmpdir = tempfile.mkdtemp(dir=str(udir)) ffi = FFI() ffi.cdef("int foo(int);") - f = open(os.path.join(tmpdir, 'foo.h'), 'w') - f.write("int foo(int a) { return a + 42; }\n") - f.close() + with open(os.path.join(tmpdir, 'foo.h'), 'w') as f: + f.write("int foo(int a) { return a + 42; }\n") lib = ffi.verify('#include "foo.h"', include_dirs=['.'], relative_to=os.path.join(tmpdir, 'x')) diff --git a/testing/cffi1/test_verify1.py b/testing/cffi1/test_verify1.py index e8ffabd3..320e338c 100644 --- a/testing/cffi1/test_verify1.py +++ b/testing/cffi1/test_verify1.py @@ -1410,9 +1410,8 @@ def test_relative_to(): tmpdir = tempfile.mkdtemp(dir=str(udir)) ffi = FFI() ffi.cdef("int foo(int);") - f = open(os.path.join(tmpdir, 'foo.h'), 'w') - f.write("int foo(int a) { return a + 42; }\n") - f.close() + with open(os.path.join(tmpdir, 'foo.h'), 'w') as f: + f.write("int foo(int a) { return a + 42; }\n") lib = ffi.verify('#include "foo.h"', include_dirs=['.'], relative_to=os.path.join(tmpdir, 'x')) From 762fdceb83813d2d90d139ce9a5e3d9221eefb39 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 22 Nov 2025 15:06:30 +0100 Subject: [PATCH 4/5] Apply ruff/flake8-simplify rule SIM118 SIM118 Use `key in dict` instead of `key in dict.keys()` --- testing/cffi1/test_new_ffi_1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/cffi1/test_new_ffi_1.py b/testing/cffi1/test_new_ffi_1.py index 676d235d..fd193cb3 100644 --- a/testing/cffi1/test_new_ffi_1.py +++ b/testing/cffi1/test_new_ffi_1.py @@ -1811,7 +1811,7 @@ def test_import_from_lib(self): # equivalent to "import ffi, lib" d = {} exec("from _test_import_from_lib import *", d) - assert (sorted([x for x in d.keys() if not x.startswith('__')]) == + assert (sorted(x for x in d if not x.startswith('__')) == ['ffi', 'lib']) def test_char16_t(self): From 0b656efc14c1af3dce4d8718a94e4cc86963fd0e Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sat, 22 Nov 2025 15:07:48 +0100 Subject: [PATCH 5/5] Apply ruff/flake8-simplify rule SIM201 SIM201 Use `... != ...` instead of `not ... == ...` --- src/cffi/verifier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cffi/verifier.py b/src/cffi/verifier.py index 5ad0105e..96baadb6 100644 --- a/src/cffi/verifier.py +++ b/src/cffi/verifier.py @@ -182,7 +182,7 @@ def _write_source(self, file=None): # Determine if this matches the current file if os.path.exists(self.sourcefilename): with open(self.sourcefilename, "r") as fp: - needs_written = not (fp.read() == source_data) + needs_written = fp.read() != source_data else: needs_written = True