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): diff --git a/src/cffi/recompiler.py b/src/cffi/recompiler.py index 495b897a..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 @@ -538,8 +536,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)' 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 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_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): 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'))