Commit 4fe5191b authored by jbrockmendel's avatar jbrockmendel Committed by Stefan Behnel

CLN: flake8 fixups in Cython.Utils (#2833)

Resolve flake8 findings in Cython.Utils.
parent 573f3fe4
# """
# Cython -- Things that don't belong Cython -- Things that don't belong
# anywhere else in particular anywhere else in particular
# """
from __future__ import absolute_import from __future__ import absolute_import
...@@ -28,24 +28,31 @@ PACKAGE_FILES = ("__init__.py", "__init__.pyc", "__init__.pyx", "__init__.pxd") ...@@ -28,24 +28,31 @@ PACKAGE_FILES = ("__init__.py", "__init__.pyc", "__init__.pyx", "__init__.pxd")
modification_time = os.path.getmtime modification_time = os.path.getmtime
_function_caches = [] _function_caches = []
def clear_function_caches(): def clear_function_caches():
for cache in _function_caches: for cache in _function_caches:
cache.clear() cache.clear()
def cached_function(f): def cached_function(f):
cache = {} cache = {}
_function_caches.append(cache) _function_caches.append(cache)
uncomputed = object() uncomputed = object()
def wrapper(*args): def wrapper(*args):
res = cache.get(args, uncomputed) res = cache.get(args, uncomputed)
if res is uncomputed: if res is uncomputed:
res = cache[args] = f(*args) res = cache[args] = f(*args)
return res return res
wrapper.uncached = f wrapper.uncached = f
return wrapper return wrapper
def cached_method(f): def cached_method(f):
cache_name = '__%s_cache' % f.__name__ cache_name = '__%s_cache' % f.__name__
def wrapper(self, *args): def wrapper(self, *args):
cache = getattr(self, cache_name, None) cache = getattr(self, cache_name, None)
if cache is None: if cache is None:
...@@ -55,8 +62,10 @@ def cached_method(f): ...@@ -55,8 +62,10 @@ def cached_method(f):
return cache[args] return cache[args]
res = cache[args] = f(self, *args) res = cache[args] = f(self, *args)
return res return res
return wrapper return wrapper
def replace_suffix(path, newsuf): def replace_suffix(path, newsuf):
base, _ = os.path.splitext(path) base, _ = os.path.splitext(path)
return base + newsuf return base + newsuf
...@@ -93,6 +102,7 @@ def castrate_file(path, st): ...@@ -93,6 +102,7 @@ def castrate_file(path, st):
if st: if st:
os.utime(path, (st.st_atime, st.st_mtime-1)) os.utime(path, (st.st_atime, st.st_mtime-1))
def file_newer_than(path, time): def file_newer_than(path, time):
ftime = modification_time(path) ftime = modification_time(path)
return ftime > time return ftime > time
...@@ -156,17 +166,17 @@ def search_include_directories(dirs, qualified_name, suffix, pos, ...@@ -156,17 +166,17 @@ def search_include_directories(dirs, qualified_name, suffix, pos,
module_filename = module_name + suffix module_filename = module_name + suffix
package_filename = "__init__" + suffix package_filename = "__init__" + suffix
for dir in dirs: for dirname in dirs:
path = os.path.join(dir, dotted_filename) path = os.path.join(dirname, dotted_filename)
if path_exists(path): if path_exists(path):
return path return path
if not include: if not include:
package_dir = check_package_dir(dir, package_names) package_dir = check_package_dir(dirname, package_names)
if package_dir is not None: if package_dir is not None:
path = os.path.join(package_dir, module_filename) path = os.path.join(package_dir, module_filename)
if path_exists(path): if path_exists(path):
return path return path
path = os.path.join(dir, package_dir, module_name, path = os.path.join(dirname, package_dir, module_name,
package_filename) package_filename)
if path_exists(path): if path_exists(path):
return path return path
...@@ -183,6 +193,7 @@ def find_root_package_dir(file_path): ...@@ -183,6 +193,7 @@ def find_root_package_dir(file_path):
else: else:
return dir return dir
@cached_function @cached_function
def check_package_dir(dir, package_names): def check_package_dir(dir, package_names):
for dirname in package_names: for dirname in package_names:
...@@ -191,6 +202,7 @@ def check_package_dir(dir, package_names): ...@@ -191,6 +202,7 @@ def check_package_dir(dir, package_names):
return None return None
return dir return dir
@cached_function @cached_function
def is_package_dir(dir_path): def is_package_dir(dir_path):
for filename in PACKAGE_FILES: for filename in PACKAGE_FILES:
...@@ -198,6 +210,7 @@ def is_package_dir(dir_path): ...@@ -198,6 +210,7 @@ def is_package_dir(dir_path):
if path_exists(path): if path_exists(path):
return 1 return 1
@cached_function @cached_function
def path_exists(path): def path_exists(path):
# try on the filesystem first # try on the filesystem first
...@@ -222,6 +235,7 @@ def path_exists(path): ...@@ -222,6 +235,7 @@ def path_exists(path):
pass pass
return False return False
# file name encodings # file name encodings
def decode_filename(filename): def decode_filename(filename):
...@@ -235,6 +249,7 @@ def decode_filename(filename): ...@@ -235,6 +249,7 @@ def decode_filename(filename):
pass pass
return filename return filename
# support for source file encoding detection # support for source file encoding detection
_match_file_encoding = re.compile(br"(\w*coding)[:=]\s*([-\w.]+)").search _match_file_encoding = re.compile(br"(\w*coding)[:=]\s*([-\w.]+)").search
...@@ -252,6 +267,7 @@ def detect_opened_file_encoding(f): ...@@ -252,6 +267,7 @@ def detect_opened_file_encoding(f):
lines = start.split(b"\n") lines = start.split(b"\n")
if not data: if not data:
break break
m = _match_file_encoding(lines[0]) m = _match_file_encoding(lines[0])
if m and m.group(1) != b'c_string_encoding': if m and m.group(1) != b'c_string_encoding':
return m.group(2).decode('iso8859-1') return m.group(2).decode('iso8859-1')
...@@ -434,33 +450,41 @@ def print_bytes(s, header_text=None, end=b'\n', file=sys.stdout, flush=True): ...@@ -434,33 +450,41 @@ def print_bytes(s, header_text=None, end=b'\n', file=sys.stdout, flush=True):
if flush: if flush:
out.flush() out.flush()
class LazyStr: class LazyStr:
def __init__(self, callback): def __init__(self, callback):
self.callback = callback self.callback = callback
def __str__(self): def __str__(self):
return self.callback() return self.callback()
def __repr__(self): def __repr__(self):
return self.callback() return self.callback()
def __add__(self, right): def __add__(self, right):
return self.callback() + right return self.callback() + right
def __radd__(self, left): def __radd__(self, left):
return left + self.callback() return left + self.callback()
class OrderedSet(object): class OrderedSet(object):
def __init__(self, elements=()): def __init__(self, elements=()):
self._list = [] self._list = []
self._set = set() self._set = set()
self.update(elements) self.update(elements)
def __iter__(self):
return iter(self._list) def __iter__(self):
def update(self, elements): return iter(self._list)
for e in elements:
self.add(e) def update(self, elements):
def add(self, e): for e in elements:
if e not in self._set: self.add(e)
self._list.append(e)
self._set.add(e) def add(self, e):
if e not in self._set:
self._list.append(e)
self._set.add(e)
# Class decorator that adds a metaclass and recreates the class with it. # Class decorator that adds a metaclass and recreates the class with it.
...@@ -482,7 +506,7 @@ def add_metaclass(metaclass): ...@@ -482,7 +506,7 @@ def add_metaclass(metaclass):
def raise_error_if_module_name_forbidden(full_module_name): def raise_error_if_module_name_forbidden(full_module_name):
#it is bad idea to call the pyx-file cython.pyx, so fail early # it is bad idea to call the pyx-file cython.pyx, so fail early
if full_module_name == 'cython' or full_module_name.startswith('cython.'): if full_module_name == 'cython' or full_module_name.startswith('cython.'):
raise ValueError('cython is a special module, cannot be used as a module name') raise ValueError('cython is a special module, cannot be used as a module name')
......
...@@ -7,3 +7,5 @@ ignore = ...@@ -7,3 +7,5 @@ ignore =
# W504 line break after binary operator # W504 line break after binary operator
S001, S001,
# S001 found module formatter # S001 found module formatter
E226,
# E226 missing whitespace around operator
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment