From 810e049f65b44e06dbfd7c8800750a6230f93b39 Mon Sep 17 00:00:00 2001 From: Micka?l Schoentgen Date: Mon, 7 Jan 2019 18:20:59 +0100 Subject: Fix ResourceWarning: unclosed file Also uniformize usage of the 'with' contact manager to prevent resource leaks. --- pygments/formatters/__init__.py | 3 ++- pygments/formatters/html.py | 7 +++---- pygments/lexers/__init__.py | 3 ++- pygments/lexers/_cocoa_builtins.py | 3 ++- pygments/lexers/_php_builtins.py | 28 ++++++++++++++-------------- scripts/check_sources.py | 3 ++- tests/test_html_formatter.py | 10 ++++------ 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 '->' 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 '->' 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: -- cgit v1.2.1 From 82300e5231c4d917c6221ab347226faf5a3bea3c Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sat, 12 Jan 2019 17:13:50 +0100 Subject: Django lexer: add support for % and != operators. --- pygments/lexers/templates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygments/lexers/templates.py b/pygments/lexers/templates.py index c184b2dd..8000deba 100644 --- a/pygments/lexers/templates.py +++ b/pygments/lexers/templates.py @@ -375,7 +375,7 @@ class DjangoLexer(RegexLexer): (r'\.\w+', Name.Variable), (r':?"(\\\\|\\"|[^"])*"', String.Double), (r":?'(\\\\|\\'|[^'])*'", String.Single), - (r'([{}()\[\]+\-*/,:~]|[><=]=?)', Operator), + (r'([{}()\[\]+\-*/%,:~]|[><=]=?|!=)', Operator), (r"[0-9](\.[0-9]*)?(eE[+-][0-9])?[flFLdD]?|" r"0[xX][0-9a-fA-F]+[Ll]?", Number), ], -- cgit v1.2.1