diff options
-rwxr-xr-x | doc/cdoc/numpyfilter.py | 26 | ||||
-rwxr-xr-x | doc/postprocess.py | 10 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 90 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 27 | ||||
-rw-r--r-- | numpy/distutils/tests/test_exec_command.py | 5 | ||||
-rw-r--r-- | numpy/f2py/tests/util.py | 5 | ||||
-rw-r--r-- | numpy/testing/_private/utils.py | 10 | ||||
-rw-r--r-- | tools/allocation_tracking/track_allocations.py | 43 |
8 files changed, 93 insertions, 123 deletions
diff --git a/doc/cdoc/numpyfilter.py b/doc/cdoc/numpyfilter.py index 65c801206..fe03fcef7 100755 --- a/doc/cdoc/numpyfilter.py +++ b/doc/cdoc/numpyfilter.py @@ -27,13 +27,12 @@ def main(): cache = load_cache() - f = open(args[0], 'r') try: - text = f.read() - text = comment_re.sub(lambda m: process_match(m, cache), text) - sys.stdout.write(text) + with open(args[0], 'r') as f: + text = f.read() + text = comment_re.sub(lambda m: process_match(m, cache), text) + sys.stdout.write(text) finally: - f.close() save_cache(cache) def filter_comment(text): @@ -67,23 +66,18 @@ def process_match(m, cache=None): def load_cache(): if os.path.exists(CACHE_FILE): - f = open(CACHE_FILE, 'rb') - try: - cache = pickle.load(f) - except Exception: - cache = {} - finally: - f.close() + with open(CACHE_FILE, 'rb') as f: + try: + cache = pickle.load(f) + except Exception: + cache = {} else: cache = {} return cache def save_cache(cache): - f = open(CACHE_FILE + '.new', 'wb') - try: + with open(CACHE_FILE + '.new', 'wb') as f: pickle.dump(cache, f) - finally: - f.close() os.rename(CACHE_FILE + '.new', CACHE_FILE) def render_html(text): diff --git a/doc/postprocess.py b/doc/postprocess.py index b6d067437..e4cbecf7b 100755 --- a/doc/postprocess.py +++ b/doc/postprocess.py @@ -23,18 +23,14 @@ def main(): p.error('unknown mode %s' % mode) for fn in args: - f = io.open(fn, 'r', encoding="utf-8") - try: + with io.open(fn, 'r', encoding="utf-8") as f: if mode == 'html': lines = process_html(fn, f.readlines()) elif mode == 'tex': lines = process_tex(f.readlines()) - finally: - f.close() - f = io.open(fn, 'w', encoding="utf-8") - f.write("".join(lines)) - f.close() + with io.open(fn, 'w', encoding="utf-8") as f: + f.write("".join(lines)) def process_html(fn, lines): return lines diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 600941cfd..270daad1e 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -4661,25 +4661,23 @@ class TestIO: assert_array_equal(d, e) def test_empty_files_binary(self): - f = open(self.filename, 'w') - f.close() + with open(self.filename, 'w') as f: + pass y = np.fromfile(self.filename) assert_(y.size == 0, "Array not empty") def test_empty_files_text(self): - f = open(self.filename, 'w') - f.close() + with open(self.filename, 'wb') as f: + pass y = np.fromfile(self.filename, sep=" ") assert_(y.size == 0, "Array not empty") def test_roundtrip_file(self): - f = open(self.filename, 'wb') - self.x.tofile(f) - f.close() + with open(self.filename, 'wb') as f: + self.x.tofile(f) # NB. doesn't work with flush+seek, due to use of C stdio - f = open(self.filename, 'rb') - y = np.fromfile(f, dtype=self.dtype) - f.close() + with open(self.filename, 'rb') as f: + y = np.fromfile(f, dtype=self.dtype) assert_array_equal(y, self.x.flat) def test_roundtrip_filename(self): @@ -4778,19 +4776,17 @@ class TestIO: io.DEFAULT_BUFFER_SIZE*8] for size in sizes: - f = open(self.filename, 'wb') - f.seek(size-1) - f.write(b'\0') - f.close() + with open(self.filename, 'wb') as f: + f.seek(size-1) + f.write(b'\0') for mode in ['rb', 'r+b']: err_msg = "%d %s" % (size, mode) - f = open(self.filename, mode) - f.read(2) - np.fromfile(f, dtype=np.float64, count=1) - pos = f.tell() - f.close() + with open(self.filename, mode) as f: + f.read(2) + np.fromfile(f, dtype=np.float64, count=1) + pos = f.tell() assert_equal(pos, 10, err_msg=err_msg) def test_file_position_after_tofile(self): @@ -4802,22 +4798,20 @@ class TestIO: for size in sizes: err_msg = "%d" % (size,) - f = open(self.filename, 'wb') - f.seek(size-1) - f.write(b'\0') - f.seek(10) - f.write(b'12') - np.array([0], dtype=np.float64).tofile(f) - pos = f.tell() - f.close() + with open(self.filename, 'wb') as f: + f.seek(size-1) + f.write(b'\0') + f.seek(10) + f.write(b'12') + np.array([0], dtype=np.float64).tofile(f) + pos = f.tell() assert_equal(pos, 10 + 2 + 8, err_msg=err_msg) - f = open(self.filename, 'r+b') - f.read(2) - f.seek(0, 1) # seek between read&write required by ANSI C - np.array([0], dtype=np.float64).tofile(f) - pos = f.tell() - f.close() + with open(self.filename, 'r+b') as f: + f.read(2) + f.seek(0, 1) # seek between read&write required by ANSI C + np.array([0], dtype=np.float64).tofile(f) + pos = f.tell() assert_equal(pos, 10, err_msg=err_msg) def test_load_object_array_fromfile(self): @@ -4870,9 +4864,8 @@ class TestIO: y = np.fromstring(s, **kw) assert_array_equal(y, value) - f = open(self.filename, 'wb') - f.write(s) - f.close() + with open(self.filename, 'wb') as f: + f.write(s) y = np.fromfile(self.filename, **kw) assert_array_equal(y, value) @@ -4956,33 +4949,28 @@ class TestIO: # can't use _check_from because fromstring can't handle True/False v = np.array([True, False, True, False], dtype=np.bool_) s = b'1,0,-2.3,0' - f = open(self.filename, 'wb') - f.write(s) - f.close() + with open(self.filename, 'wb') as f: + f.write(s) y = np.fromfile(self.filename, sep=',', dtype=np.bool_) assert_(y.dtype == '?') assert_array_equal(y, v) def test_tofile_sep(self): x = np.array([1.51, 2, 3.51, 4], dtype=float) - f = open(self.filename, 'w') - x.tofile(f, sep=',') - f.close() - f = open(self.filename, 'r') - s = f.read() - f.close() + with open(self.filename, 'w') as f: + x.tofile(f, sep=',') + with open(self.filename, 'r') as f: + s = f.read() #assert_equal(s, '1.51,2.0,3.51,4.0') y = np.array([float(p) for p in s.split(',')]) assert_array_equal(x,y) def test_tofile_format(self): x = np.array([1.51, 2, 3.51, 4], dtype=float) - f = open(self.filename, 'w') - x.tofile(f, sep=',', format='%.2f') - f.close() - f = open(self.filename, 'r') - s = f.read() - f.close() + with open(self.filename, 'w') as f: + x.tofile(f, sep=',', format='%.2f') + with open(self.filename, 'r') as f: + s = f.read() assert_equal(s, '1.51,2.00,3.51,4.00') def test_locale(self): diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 3a9b96886..321723b9b 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -36,11 +36,10 @@ class TestRegression: # Ticket #16 a = np.transpose(np.array([[2, 9], [7, 0], [3, 8]])) for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): - f = BytesIO() - pickle.dump(a, f, protocol=proto) - f.seek(0) - b = pickle.load(f) - f.close() + with BytesIO() as f: + pickle.dump(a, f, protocol=proto) + f.seek(0) + b = pickle.load(f) assert_array_equal(a, b) def test_typeNA(self): @@ -94,11 +93,10 @@ class TestRegression: # Ticket #50 ca = np.char.array(np.arange(1000, 1010), itemsize=4) for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): - f = BytesIO() - pickle.dump(ca, f, protocol=proto) - f.seek(0) - ca = np.load(f, allow_pickle=True) - f.close() + with BytesIO() as f: + pickle.dump(ca, f, protocol=proto) + f.seek(0) + ca = np.load(f, allow_pickle=True) def test_noncontiguous_fill(self): # Ticket #58. @@ -358,11 +356,10 @@ class TestRegression: # Implemented in r2840 dt = np.dtype([('x', int), ('y', np.object_), ('z', 'O')]) for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): - f = BytesIO() - pickle.dump(dt, f, protocol=proto) - f.seek(0) - dt_ = pickle.load(f) - f.close() + with BytesIO() as f: + pickle.dump(dt, f, protocol=proto) + f.seek(0) + dt_ = pickle.load(f) assert_equal(dt, dt_) def test_mem_array_creation_invalid_specification(self): diff --git a/numpy/distutils/tests/test_exec_command.py b/numpy/distutils/tests/test_exec_command.py index 3bbad9386..d6eb7d1c3 100644 --- a/numpy/distutils/tests/test_exec_command.py +++ b/numpy/distutils/tests/test_exec_command.py @@ -186,9 +186,8 @@ class TestExecCommand: with tempdir() as tmpdir: fn = "file" tmpfile = os.path.join(tmpdir, fn) - f = open(tmpfile, 'w') - f.write('Hello') - f.close() + with open(tmpfile, 'w') as f: + f.write('Hello') s, o = exec_command.exec_command( '"%s" -c "f = open(\'%s\', \'r\'); f.close()"' % diff --git a/numpy/f2py/tests/util.py b/numpy/f2py/tests/util.py index c430a6608..6dcc2ed12 100644 --- a/numpy/f2py/tests/util.py +++ b/numpy/f2py/tests/util.py @@ -282,9 +282,8 @@ def build_module_distutils(source_files, config_code, module_name, **kw): script = os.path.join(d, get_temp_module_name() + '.py') dst_sources.append(script) - f = open(script, 'wb') - f.write(asbytes(code)) - f.close() + with open(script, 'wb') as f: + f.write(asbytes(code)) # Build cwd = os.getcwd() diff --git a/numpy/testing/_private/utils.py b/numpy/testing/_private/utils.py index 1b88d91f6..8b098f1d1 100644 --- a/numpy/testing/_private/utils.py +++ b/numpy/testing/_private/utils.py @@ -192,9 +192,8 @@ elif sys.platform[:5] == 'linux': """ try: - f = open(_proc_pid_stat, 'r') - l = f.readline().split(' ') - f.close() + with open(_proc_pid_stat, 'r') as f: + l = f.readline().split(' ') return int(l[22]) except Exception: return @@ -221,9 +220,8 @@ if sys.platform[:5] == 'linux': if not _load_time: _load_time.append(time.time()) try: - f = open(_proc_pid_stat, 'r') - l = f.readline().split(' ') - f.close() + with open(_proc_pid_stat, 'r') as f: + l = f.readline().split(' ') return int(l[13]) except Exception: return int(100*(time.time()-_load_time[0])) diff --git a/tools/allocation_tracking/track_allocations.py b/tools/allocation_tracking/track_allocations.py index 94d07d50e..2a80d8f87 100644 --- a/tools/allocation_tracking/track_allocations.py +++ b/tools/allocation_tracking/track_allocations.py @@ -106,30 +106,29 @@ class AllocationTracker: self.current_line = line def write_html(self, filename): - f = open(filename, "w") - f.write('<HTML><HEAD><script src="sorttable.js"></script></HEAD><BODY>\n') - f.write('<TABLE class="sortable" width=100%>\n') - f.write("<TR>\n") - cols = "event#,lineinfo,bytes allocated,bytes freed,#allocations,#frees,max memory usage,long lived bytes".split(',') - for header in cols: - f.write(" <TH>{0}</TH>".format(header)) - f.write("\n</TR>\n") - for idx, event in enumerate(self.allocation_trace): + with open(filename, "w") as f: + f.write('<HTML><HEAD><script src="sorttable.js"></script></HEAD><BODY>\n') + f.write('<TABLE class="sortable" width=100%>\n') f.write("<TR>\n") - event = [idx] + list(event) - for col, val in zip(cols, event): - if col == 'lineinfo': - # special handling - try: - filename, line, module, code, index = val - val = "{0}({1}): {2}".format(filename, line, code[index]) - except Exception: - # sometimes this info is not available (from eval()?) - val = str(val) - f.write(" <TD>{0}</TD>".format(val)) + cols = "event#,lineinfo,bytes allocated,bytes freed,#allocations,#frees,max memory usage,long lived bytes".split(',') + for header in cols: + f.write(" <TH>{0}</TH>".format(header)) f.write("\n</TR>\n") - f.write("</TABLE></BODY></HTML>\n") - f.close() + for idx, event in enumerate(self.allocation_trace): + f.write("<TR>\n") + event = [idx] + list(event) + for col, val in zip(cols, event): + if col == 'lineinfo': + # special handling + try: + filename, line, module, code, index = val + val = "{0}({1}): {2}".format(filename, line, code[index]) + except Exception: + # sometimes this info is not available (from eval()?) + val = str(val) + f.write(" <TD>{0}</TD>".format(val)) + f.write("\n</TR>\n") + f.write("</TABLE></BODY></HTML>\n") if __name__ == '__main__': |