Skip to content

Commit

Permalink
Merge pull request dabeaz#190 from BoboTiG/fix-resource-warnings
Browse files Browse the repository at this point in the history
Fix several ResourceWarning: unclosed file
  • Loading branch information
dabeaz authored Dec 22, 2018
2 parents 046ff4b + 94cb676 commit af91e60
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
16 changes: 8 additions & 8 deletions doc/makedoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
heading = re.compile(r"(_nn\d)", re.IGNORECASE)

def getheadingname(m):
autogeneratedheading = True;
autogeneratedheading = True
if m.group(1) != None:
amatch = alink.match(m.group(1))
if amatch:
Expand Down Expand Up @@ -65,8 +65,9 @@ def getheadingname(m):
h4 = re.compile(r".*?<H4>(<a.*a>)*[\d\.\s]*(.*?)</H4>", re.IGNORECASE)
h5 = re.compile(r".*?<H5>(<a.*a>)*[\d\.\s]*(.*?)</H5>", re.IGNORECASE)

data = open(filename).read() # Read data
open(filename+".bak","w").write(data) # Make backup
# Make backup
with open(filename) as src, open(filename+".bak","w") as dst:
dst.write(src.read())

lines = data.splitlines()
result = [ ] # This is the result of postprocessing the file
Expand All @@ -82,7 +83,7 @@ def getheadingname(m):
skip = 1
else:
skip = 0
continue;
continue
if skip:
continue

Expand Down Expand Up @@ -186,9 +187,8 @@ def getheadingname(m):

data = "\n".join(result)

data = data.replace("@INDEX@",index) + "\n";
data = data.replace("@INDEX@",index) + "\n"

# Write the file back out
open(filename,"w").write(data)


with open(filename,"w") as f:
f.write(data)
3 changes: 2 additions & 1 deletion example/BASIC/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# If a runtime error occurs, we bail out and enter
# interactive mode below
if len(sys.argv) == 2:
data = open(sys.argv[1]).read()
with open(sys.argv[1]) as f:
data = f.read()
prog = basparse.parse(data)
if not prog:
raise SystemExit
Expand Down
3 changes: 2 additions & 1 deletion example/BASIC/basiclog.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
# If a runtime error occurs, we bail out and enter
# interactive mode below
if len(sys.argv) == 2:
data = open(sys.argv[1]).read()
with open(sys.argv[1]) as f:
data = f.read()
prog = basparse.parse(data, debug=log)
if not prog:
raise SystemExit
Expand Down
7 changes: 4 additions & 3 deletions ply/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,8 @@ def include(self,tokens):
for p in path:
iname = os.path.join(p,filename)
try:
data = open(iname,"r").read()
with open(iname) as f:
data = f.read()
dname = os.path.dirname(iname)
if dname:
self.temp_path.insert(0,dname)
Expand Down Expand Up @@ -903,8 +904,8 @@ def token(self):

# Run a preprocessor
import sys
f = open(sys.argv[1])
input = f.read()
with open(sys.argv[1]) as f:
input = f.read()

p = Preprocessor(lexer)
p.parse(input,sys.argv[1])
Expand Down
5 changes: 2 additions & 3 deletions ply/lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,9 +1057,8 @@ def runmain(lexer=None, data=None):
if not data:
try:
filename = sys.argv[1]
f = open(filename)
data = f.read()
f.close()
with open(filename) as f:
data = f.read()
except IndexError:
sys.stdout.write('Reading from standard input (type EOF to end):\n')
data = sys.stdin.read()
Expand Down
6 changes: 4 additions & 2 deletions test/testlex.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,10 @@ def test_lex_optimize3(self):

os.mkdir("lexdir")
os.mkdir("lexdir/sub")
open("lexdir/__init__.py","w").write("")
open("lexdir/sub/__init__.py","w").write("")
with open("lexdir/__init__.py","w") as f:
f.write("")
with open("lexdir/sub/__init__.py","w") as f:
f.write("")
run_import("lex_optimize3")
result = sys.stdout.getvalue()
self.assert_(check_expected(result,
Expand Down

0 comments on commit af91e60

Please sign in to comment.