summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMicka?l Schoentgen <contact@tiger-222.fr>2019-01-07 18:20:59 +0100
committerMicka?l Schoentgen <contact@tiger-222.fr>2019-01-07 18:20:59 +0100
commit810e049f65b44e06dbfd7c8800750a6230f93b39 (patch)
tree0532e3686d3260167c974bef44a4f7da69d44aa4
parent867fbbd26114b70d04c5885e1447af1d8acb08df (diff)
downloadpygments-810e049f65b44e06dbfd7c8800750a6230f93b39.tar.gz
Fix ResourceWarning: unclosed file
Also uniformize usage of the 'with' contact manager to prevent resource leaks.
-rw-r--r--pygments/formatters/__init__.py3
-rw-r--r--pygments/formatters/html.py7
-rw-r--r--pygments/lexers/__init__.py3
-rw-r--r--pygments/lexers/_cocoa_builtins.py3
-rw-r--r--pygments/lexers/_php_builtins.py28
-rwxr-xr-xscripts/check_sources.py3
-rw-r--r--tests/test_html_formatter.py10
7 files changed, 29 insertions, 28 deletions
diff --git a/pygments/formatters/__init__.py b/pygments/formatters/__init__.py
index 965c5f1a..457d76ec 100644
--- a/pygments/formatters/__init__.py
+++ b/pygments/formatters/__init__.py
@@ -98,7 +98,8 @@ def load_formatter_from_file(filename, formattername="CustomFormatter",
try:
# This empty dict will contain the namespace for the exec'd file
custom_namespace = {}
- exec(open(filename, 'rb').read(), custom_namespace)
+ with open(filename, 'rb') as f:
+ exec(f.read(), custom_namespace)
# Retrieve the class `formattername` from that namespace
if formattername not in custom_namespace:
raise ClassNotFound('no valid %s class found in %s' %
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index 2969d502..7d7605eb 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -535,10 +535,9 @@ class HtmlFormatter(Formatter):
# write CSS file only if noclobber_cssfile isn't given as an option.
try:
if not os.path.exists(cssfilename) or not self.noclobber_cssfile:
- cf = open(cssfilename, "w")
- cf.write(CSSFILE_TEMPLATE %
- {'styledefs': self.get_style_defs('body')})
- cf.close()
+ with open(cssfilename, "w") as cf:
+ cf.write(CSSFILE_TEMPLATE %
+ {'styledefs': self.get_style_defs('body')})
except IOError as err:
err.strerror = 'Error writing CSS file: ' + err.strerror
raise
diff --git a/pygments/lexers/__init__.py b/pygments/lexers/__init__.py
index 328e072c..50f39d4e 100644
--- a/pygments/lexers/__init__.py
+++ b/pygments/lexers/__init__.py
@@ -133,7 +133,8 @@ def load_lexer_from_file(filename, lexername="CustomLexer", **options):
try:
# This empty dict will contain the namespace for the exec'd file
custom_namespace = {}
- exec(open(filename, 'rb').read(), custom_namespace)
+ with open(filename, 'rb') as f:
+ exec(f.read(), custom_namespace)
# Retrieve the class `lexername` from that namespace
if lexername not in custom_namespace:
raise ClassNotFound('no valid %s class found in %s' %
diff --git a/pygments/lexers/_cocoa_builtins.py b/pygments/lexers/_cocoa_builtins.py
index f55e9dd7..f17ea876 100644
--- a/pygments/lexers/_cocoa_builtins.py
+++ b/pygments/lexers/_cocoa_builtins.py
@@ -40,7 +40,8 @@ if __name__ == '__main__': # pragma: no cover
continue
headerFilePath = frameworkHeadersDir + f
- content = open(headerFilePath).read()
+ with open(headerFilePath) as f:
+ content = f.read()
res = re.findall(r'(?<=@interface )\w+', content)
for r in res:
all_interfaces.add(r)
diff --git a/pygments/lexers/_php_builtins.py b/pygments/lexers/_php_builtins.py
index bd4b7d99..c6084003 100644
--- a/pygments/lexers/_php_builtins.py
+++ b/pygments/lexers/_php_builtins.py
@@ -4698,18 +4698,19 @@ if __name__ == '__main__': # pragma: no cover
for file in get_php_references():
module = ''
- for line in open(file):
- if not module:
- search = module_re.search(line)
- if search:
- module = search.group(1)
- modules[module] = []
+ with open(file) as f:
+ for line in f:
+ if not module:
+ search = module_re.search(line)
+ if search:
+ module = search.group(1)
+ modules[module] = []
- elif 'href="function.' in line:
- for match in function_re.finditer(line):
- fn = match.group(1)
- if '-&gt;' not in fn and '::' not in fn and fn not in modules[module]:
- modules[module].append(fn)
+ elif 'href="function.' in line:
+ for match in function_re.finditer(line):
+ fn = match.group(1)
+ if '-&gt;' not in fn and '::' not in fn and fn not in modules[module]:
+ modules[module].append(fn)
if module:
# These are dummy manual pages, not actual functions
@@ -4726,9 +4727,8 @@ if __name__ == '__main__': # pragma: no cover
def get_php_references():
download = urlretrieve(PHP_MANUAL_URL)
- tar = tarfile.open(download[0])
- tar.extractall()
- tar.close()
+ with tarfile.open(download[0]) as tar:
+ tar.extractall()
for file in glob.glob("%s%s" % (PHP_MANUAL_DIR, PHP_REFERENCE_GLOB)):
yield file
os.remove(download[0])
diff --git a/scripts/check_sources.py b/scripts/check_sources.py
index db09de42..c0524b6c 100755
--- a/scripts/check_sources.py
+++ b/scripts/check_sources.py
@@ -185,7 +185,8 @@ def main(argv):
print("Checking %s..." % fn)
try:
- lines = open(fn, 'rb').read().decode('utf-8').splitlines()
+ with open(fn, 'rb') as f:
+ lines = f.read().decode('utf-8').splitlines()
except (IOError, OSError) as err:
print("%s: cannot open: %s" % (fn, err))
num += 1
diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py
index 10450c56..670a5be9 100644
--- a/tests/test_html_formatter.py
+++ b/tests/test_html_formatter.py
@@ -132,9 +132,8 @@ class HtmlFormatterTest(unittest.TestCase):
outencoding='utf-8')
handle, pathname = tempfile.mkstemp('.html')
- tfile = os.fdopen(handle, 'w+b')
- fmt.format(tokensource, tfile)
- tfile.close()
+ with os.fdopen(handle, 'w+b') as tfile:
+ fmt.format(tokensource, tfile)
catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc')
try:
import subprocess
@@ -173,9 +172,8 @@ class HtmlFormatterTest(unittest.TestCase):
cssstyles=u'div:before { content: \'bäz\' }',
encoding='utf-8')
handle, pathname = tempfile.mkstemp('.html')
- tfile = os.fdopen(handle, 'w+b')
- fmt.format(tokensource, tfile)
- tfile.close()
+ with os.fdopen(handle, 'w+b') as tfile:
+ fmt.format(tokensource, tfile)
def test_ctags(self):
try: