Commit 2a304b0f authored by Matti Picus's avatar Matti Picus Committed by GitHub

Use context manager when opening files instead of leaving a file dangling open. (GH-4621)

parent 2c7c53b4
......@@ -32,7 +32,8 @@ import re
def extract_names(path):
# All parse tree types are #defined in these files as ints.
type_names = {}
for line in open(path):
with open(path) as fid:
for line in fid:
if line.startswith('#define'):
try:
_, name, value = line.strip().split()
......@@ -61,7 +62,8 @@ def handle_includes(source, path):
included = os.path.join(os.path.dirname(path), include_line.group(1)[1:-1])
if not os.path.exists(included):
return include_line.group(0) + ' # no such path: ' + included
return handle_includes(open(included).read(), path)
with open(included) as fid:
return handle_includes(fid.read(), path)
# TODO: Proper string tokenizing.
return re.sub(r'^include\s+([^\n]+[\'"])\s*(#.*)?$', include_here, source, flags=re.M)
......@@ -69,7 +71,8 @@ def p_module(path):
cdef perrdetail err
cdef int flags
cdef node* n
source = open(path).read()
with open(path) as fid:
source = fid.read()
if '\ninclude ' in source:
# TODO: Tokanizer needs to understand includes.
source = handle_includes(source, path)
......
......@@ -117,7 +117,8 @@ def handle_special_build(modname, pyxfilename):
# locs = {}
# execfile(special_build, globls, locs)
# ext = locs["make_ext"](modname, pyxfilename)
mod = imp.load_source("XXXX", special_build, open(special_build))
with open(special_build) as fid:
mod = imp.load_source("XXXX", special_build, fid)
make_ext = getattr(mod,'make_ext',None)
if make_ext:
ext = make_ext(modname, pyxfilename)
......@@ -144,7 +145,8 @@ def handle_dependencies(pyxfilename):
# but we know more about dependencies so force a rebuild if
# some of the dependencies are newer than the pyxfile.
if os.path.exists(dependfile):
depends = open(dependfile).readlines()
with open(dependfile) as fid:
depends = fid.readlines()
depends = [depend.strip() for depend in depends]
# gather dependencies in the "files" variable
......
......@@ -41,33 +41,36 @@ def test_with_reload():
tempdir = make_tempdir()
sys.path.append(tempdir)
filename = os.path.join(tempdir, "dummy.pyx")
open(filename, "w").write("print 'Hello world from the Pyrex install hook'")
with open(filename, "w") as fid:
fid.write("print 'Hello world from the Pyrex install hook'")
import dummy
reload(dummy)
depend_filename = os.path.join(tempdir, "dummy.pyxdep")
depend_file = open(depend_filename, "w")
with open(depend_filename, "w") as depend_file:
depend_file.write("*.txt\nfoo.bar")
depend_file.close()
build_filename = os.path.join(tempdir, "dummy.pyxbld")
build_file = open(build_filename, "w")
with open(build_filename, "w") as build_file:
build_file.write("""
from distutils.extension import Extension
def make_ext(name, filename):
return Extension(name=name, sources=[filename])
""")
build_file.close()
open(os.path.join(tempdir, "foo.bar"), "w").write(" ")
open(os.path.join(tempdir, "1.txt"), "w").write(" ")
open(os.path.join(tempdir, "abc.txt"), "w").write(" ")
with open(os.path.join(tempdir, "foo.bar"), "w") as fid:
fid.write(" ")
with open(os.path.join(tempdir, "1.txt"), "w") as fid:
fid.write(" ")
with open(os.path.join(tempdir, "abc.txt"), "w") as fid:
fid.write(" ")
reload(dummy)
assert len(pyximport._test_files)==1, pyximport._test_files
reload(dummy)
time.sleep(1) # sleep a second to get safer mtimes
open(os.path.join(tempdir, "abc.txt"), "w").write(" ")
with open(os.path.join(tempdir, "abc.txt"), "w") as fid:
fid.write(" ")
print("Here goes the reload")
reload(dummy)
assert len(pyximport._test_files) == 1, pyximport._test_files
......
......@@ -18,14 +18,16 @@ def test():
tempdir = test_pyximport.make_tempdir()
sys.path.append(tempdir)
hello_file = os.path.join(tempdir, "hello.pyx")
open(hello_file, "w").write("x = 1; print x; before = 'before'\n")
with open(hello_file, "w") as fid:
fid.write("x = 1; print x; before = 'before'\n")
import hello
assert hello.x == 1
time.sleep(1) # sleep to make sure that new "hello.pyx" has later
# timestamp than object file.
open(hello_file, "w").write("x = 2; print x; after = 'after'\n")
with open(hello_file, "w") as fid:
fid.write("x = 2; print x; after = 'after'\n")
reload(hello)
assert hello.x == 2, "Reload should work on Python 2.3 but not 2.2"
test_pyximport.remove_tempdir(tempdir)
......
......@@ -1352,8 +1352,11 @@ class CythonCompileTestCase(unittest.TestCase):
self.run_cython(test_directory, module, module_path, workdir2, incdir, annotate)
diffs = []
for file in os.listdir(workdir2):
if (open(os.path.join(workdir, file)).read()
!= open(os.path.join(workdir2, file)).read()):
with open(os.path.join(workdir, file)) as fid:
txt1 = fid.read()
with open(os.path.join(workdir2, file)) as fid:
txt2 = fid.read()
if txt1 != txt2:
diffs.append(file)
os.system('diff -u %s/%s %s/%s > %s/%s.diff' % (
workdir, file,
......
......@@ -78,7 +78,8 @@ if sys.platform == 'win32':
assert opt == '-c'
count = 0
regex = re.compile(pattern)
for line in open(file):
with open(file) as fid:
for line in fid:
if regex.search(line):
count += 1
print(count)
......
......@@ -56,7 +56,8 @@ from lib.cy import WithoutC, WithCPDef, WithCDefWrapper
def tryThis(obj):
print("Pickling %s ..." % obj.__class__.__name__)
try:
pkl.dump(obj, open("test.pkl", "wb"))
with open("test.pkl", "wb") as fid:
pkl.dump(obj, fid)
print("\t... OK")
except Exception as e:
print("\t... KO: %s" % str(e))
......
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