summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Helie <jean@semmle.com>2017-06-26 14:01:15 +0100
committerEric Wieser <wieser.eric@gmail.com>2017-06-26 14:01:15 +0100
commitc6533b6c386dc0f4009e5f3c5c545dde4d1b48a4 (patch)
treefac4fa235d93e25373608412968a30f974fecd85
parent62d3e4e641a1aec201e5f890c851f91226e75ded (diff)
downloadnumpy-c6533b6c386dc0f4009e5f3c5c545dde4d1b48a4.tar.gz
MAINT: Fix alerts from http://lgtm.com (#9292)
* make exception raising 2/3 compatible * remove unnecesary else statement after while loop without break clause * ensure file is always enclosed even in the event of an exception * ensure list comprehension variable does not override enclosing loop variable
-rw-r--r--numpy/_import_tools.py3
-rwxr-xr-xnumpy/f2py/crackfortran.py34
-rw-r--r--numpy/lib/npyio.py2
-rw-r--r--tools/npy_tempita/__init__.py52
4 files changed, 41 insertions, 50 deletions
diff --git a/numpy/_import_tools.py b/numpy/_import_tools.py
index 18ac78d29..cb8bc477c 100644
--- a/numpy/_import_tools.py
+++ b/numpy/_import_tools.py
@@ -303,8 +303,7 @@ class PackageLoader(object):
lines.append(line)
line = tab
line += ' ' + word
- else:
- lines.append(line)
+ lines.append(line)
return '\n'.join(lines)
def get_pkgdocs(self):
diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py
index 0f5e6e023..677f4bae3 100755
--- a/numpy/f2py/crackfortran.py
+++ b/numpy/f2py/crackfortran.py
@@ -308,22 +308,21 @@ def is_free_format(file):
# f90 allows both fixed and free format, assuming fixed unless
# signs of free format are detected.
result = 0
- f = open(file, 'r')
- line = f.readline()
- n = 15 # the number of non-comment lines to scan for hints
- if _has_f_header(line):
- n = 0
- elif _has_f90_header(line):
- n = 0
- result = 1
- while n > 0 and line:
- if line[0] != '!' and line.strip():
- n -= 1
- if (line[0] != '\t' and _free_f90_start(line[:5])) or line[-2:-1] == '&':
- result = 1
- break
+ with open(file, 'r') as f:
line = f.readline()
- f.close()
+ n = 15 # the number of non-comment lines to scan for hints
+ if _has_f_header(line):
+ n = 0
+ elif _has_f90_header(line):
+ n = 0
+ result = 1
+ while n > 0 and line:
+ if line[0] != '!' and line.strip():
+ n -= 1
+ if (line[0] != '\t' and _free_f90_start(line[:5])) or line[-2:-1] == '&':
+ result = 1
+ break
+ line = f.readline()
return result
@@ -3335,8 +3334,7 @@ if __name__ == "__main__":
if pyffilename:
outmess('Writing fortran code to file %s\n' % repr(pyffilename), 0)
pyf = crack2fortran(postlist)
- f = open(pyffilename, 'w')
- f.write(pyf)
- f.close()
+ with open(pyffilename, 'w') as f:
+ f.write(pyf)
if showblocklist:
show(postlist)
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index cb3b7534d..187a6722a 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1014,7 +1014,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
if len(vals) == 0:
continue
if usecols:
- vals = [vals[i] for i in usecols]
+ vals = [vals[j] for j in usecols]
if len(vals) != N:
line_num = i + skiprows + 1
raise ValueError("Wrong number of columns at line %d"
diff --git a/tools/npy_tempita/__init__.py b/tools/npy_tempita/__init__.py
index daf2606c8..dfb40e965 100644
--- a/tools/npy_tempita/__init__.py
+++ b/tools/npy_tempita/__init__.py
@@ -153,9 +153,8 @@ class Template(object):
def from_filename(cls, filename, namespace=None, encoding=None,
default_inherit=None, get_template=get_file_template):
- f = open(filename, 'rb')
- c = f.read()
- f.close()
+ with open(filename, 'rb') as f:
+ c = f.read()
if encoding:
c = c.decode(encoding)
elif PY3:
@@ -315,33 +314,31 @@ class Template(object):
'invalid syntax in expression: %s' % code)
return value
except:
- exc_info = sys.exc_info()
- e = exc_info[1]
- if getattr(e, 'args', None):
- arg0 = e.args[0]
+ e_type, e_value, e_traceback = sys.exc_info()
+ if getattr(e_value, 'args', None):
+ arg0 = e_value.args[0]
else:
- arg0 = coerce_text(e)
- e.args = (self._add_line_info(arg0, pos),)
+ arg0 = coerce_text(e_value)
+ e_value.args = (self._add_line_info(arg0, pos),)
if PY3:
- raise(e)
+ raise e_value
else:
- raise (exc_info[1], e, exc_info[2])
+ exec('raise e_type, e_value, e_traceback')
def _exec(self, code, ns, pos):
# __traceback_hide__ = True
try:
exec(code, self.default_namespace, ns)
except:
- exc_info = sys.exc_info()
- e = exc_info[1]
- if e.args:
- e.args = (self._add_line_info(e.args[0], pos),)
+ e_type, e_value, e_traceback = sys.exc_info()
+ if e_value.args:
+ e_value.args = (self._add_line_info(e_value.args[0], pos),)
else:
- e.args = (self._add_line_info(None, pos),)
+ e_value.args = (self._add_line_info(None, pos),)
if PY3:
- raise(e)
+ raise e_value
else:
- raise (exc_info[1], e, exc_info[2])
+ exec('raise e_type, e_value, e_traceback')
def _repr(self, value, pos):
# __traceback_hide__ = True
@@ -358,13 +355,12 @@ class Template(object):
if (is_unicode(value) and self.default_encoding):
value = value.encode(self.default_encoding)
except:
- exc_info = sys.exc_info()
- e = exc_info[1]
- e.args = (self._add_line_info(e.args[0], pos),)
+ e_type, e_value, e_traceback = sys.exc_info()
+ e_value.args = (self._add_line_info(e_value.args[0], pos),)
if PY3:
- raise(e)
+ raise e_value
else:
- raise (exc_info[1], e, exc_info[2])
+ exec('raise e_type, e_value, e_traceback')
else:
if self._unicode and isinstance(value, bytes):
if not self.default_encoding:
@@ -1295,9 +1291,8 @@ def fill_command(args=None):
template_content = sys.stdin.read()
template_name = '<stdin>'
else:
- f = open(template_name, 'rb', encoding="latin-1")
- template_content = f.read()
- f.close()
+ with open(template_name, 'rb', encoding="latin-1") as f:
+ template_content = f.read()
if options.use_html:
TemplateClass = HTMLTemplate
else:
@@ -1305,9 +1300,8 @@ def fill_command(args=None):
template = TemplateClass(template_content, name=template_name)
result = template.substitute(vars)
if options.output:
- f = open(options.output, 'wb')
- f.write(result)
- f.close()
+ with open(options.output, 'wb') as f:
+ f.write(result)
else:
sys.stdout.write(result)