Skip to content
This repository was archived by the owner on Oct 5, 2019. It is now read-only.

Commit 41038be

Browse files
committed
Fix ResourceWarning: unclosed file
Also uniformize usage of the 'with' contact manager to prevent resource leaks.
1 parent c790adc commit 41038be

File tree

7 files changed

+29
-28
lines changed

7 files changed

+29
-28
lines changed

pygments/formatters/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def load_formatter_from_file(filename, formattername="CustomFormatter",
9898
try:
9999
# This empty dict will contain the namespace for the exec'd file
100100
custom_namespace = {}
101-
exec(open(filename, 'rb').read(), custom_namespace)
101+
with open(filename, 'rb') as f:
102+
exec(f.read(), custom_namespace)
102103
# Retrieve the class `formattername` from that namespace
103104
if formattername not in custom_namespace:
104105
raise ClassNotFound('no valid %s class found in %s' %

pygments/formatters/html.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,9 @@ def _wrap_full(self, inner, outfile):
535535
# write CSS file only if noclobber_cssfile isn't given as an option.
536536
try:
537537
if not os.path.exists(cssfilename) or not self.noclobber_cssfile:
538-
cf = open(cssfilename, "w")
539-
cf.write(CSSFILE_TEMPLATE %
540-
{'styledefs': self.get_style_defs('body')})
541-
cf.close()
538+
with open(cssfilename, "w") as cf:
539+
cf.write(CSSFILE_TEMPLATE %
540+
{'styledefs': self.get_style_defs('body')})
542541
except IOError as err:
543542
err.strerror = 'Error writing CSS file: ' + err.strerror
544543
raise

pygments/lexers/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def load_lexer_from_file(filename, lexername="CustomLexer", **options):
133133
try:
134134
# This empty dict will contain the namespace for the exec'd file
135135
custom_namespace = {}
136-
exec(open(filename, 'rb').read(), custom_namespace)
136+
with open(filename, 'rb') as f:
137+
exec(f.read(), custom_namespace)
137138
# Retrieve the class `lexername` from that namespace
138139
if lexername not in custom_namespace:
139140
raise ClassNotFound('no valid %s class found in %s' %

pygments/lexers/_cocoa_builtins.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
continue
4141

4242
headerFilePath = frameworkHeadersDir + f
43-
content = open(headerFilePath).read()
43+
with open(headerFilePath) as f:
44+
content = f.read()
4445
res = re.findall(r'(?<=@interface )\w+', content)
4546
for r in res:
4647
all_interfaces.add(r)

pygments/lexers/_php_builtins.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4698,18 +4698,19 @@ def get_php_functions():
46984698

46994699
for file in get_php_references():
47004700
module = ''
4701-
for line in open(file):
4702-
if not module:
4703-
search = module_re.search(line)
4704-
if search:
4705-
module = search.group(1)
4706-
modules[module] = []
4701+
with open(file) as f:
4702+
for line in f:
4703+
if not module:
4704+
search = module_re.search(line)
4705+
if search:
4706+
module = search.group(1)
4707+
modules[module] = []
47074708

4708-
elif 'href="function.' in line:
4709-
for match in function_re.finditer(line):
4710-
fn = match.group(1)
4711-
if '-&gt;' not in fn and '::' not in fn and fn not in modules[module]:
4712-
modules[module].append(fn)
4709+
elif 'href="function.' in line:
4710+
for match in function_re.finditer(line):
4711+
fn = match.group(1)
4712+
if '-&gt;' not in fn and '::' not in fn and fn not in modules[module]:
4713+
modules[module].append(fn)
47134714

47144715
if module:
47154716
# These are dummy manual pages, not actual functions
@@ -4726,9 +4727,8 @@ def get_php_functions():
47264727

47274728
def get_php_references():
47284729
download = urlretrieve(PHP_MANUAL_URL)
4729-
tar = tarfile.open(download[0])
4730-
tar.extractall()
4731-
tar.close()
4730+
with tarfile.open(download[0]) as tar:
4731+
tar.extractall()
47324732
for file in glob.glob("%s%s" % (PHP_MANUAL_DIR, PHP_REFERENCE_GLOB)):
47334733
yield file
47344734
os.remove(download[0])

scripts/check_sources.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ def main(argv):
185185
print("Checking %s..." % fn)
186186

187187
try:
188-
lines = open(fn, 'rb').read().decode('utf-8').splitlines()
188+
with open(fn, 'rb') as f:
189+
lines = f.read().decode('utf-8').splitlines()
189190
except (IOError, OSError) as err:
190191
print("%s: cannot open: %s" % (fn, err))
191192
num += 1

tests/test_html_formatter.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,8 @@ def test_valid_output(self):
132132
outencoding='utf-8')
133133

134134
handle, pathname = tempfile.mkstemp('.html')
135-
tfile = os.fdopen(handle, 'w+b')
136-
fmt.format(tokensource, tfile)
137-
tfile.close()
135+
with os.fdopen(handle, 'w+b') as tfile:
136+
fmt.format(tokensource, tfile)
138137
catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc')
139138
try:
140139
import subprocess
@@ -173,9 +172,8 @@ def test_unicode_options(self):
173172
cssstyles=u'div:before { content: \'bäz\' }',
174173
encoding='utf-8')
175174
handle, pathname = tempfile.mkstemp('.html')
176-
tfile = os.fdopen(handle, 'w+b')
177-
fmt.format(tokensource, tfile)
178-
tfile.close()
175+
with os.fdopen(handle, 'w+b') as tfile:
176+
fmt.format(tokensource, tfile)
179177

180178
def test_ctags(self):
181179
try:

0 commit comments

Comments
 (0)