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