summaryrefslogtreecommitdiff
path: root/pygments
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2014-10-07 14:10:28 +0200
committerGeorg Brandl <georg@python.org>2014-10-07 14:10:28 +0200
commit9a51e6a6df8a56aebede133687e91e519a186122 (patch)
treeffebef0e0f0b63c2749e2858fdc50972a336fc2b /pygments
parent9cbc7803dd8e7826393721fe4acbd702843e131c (diff)
downloadpygments-9a51e6a6df8a56aebede133687e91e519a186122.tar.gz
Closes #980: fix DeprecationWarnings (mostly due to files closed by __del__) on Py3.
Also fix a bunch of other uses of open() to use the with statement.
Diffstat (limited to 'pygments')
-rw-r--r--pygments/cmdline.py3
-rwxr-xr-xpygments/formatters/_mapping.py16
-rw-r--r--pygments/lexers/_lua_builtins.py16
-rw-r--r--pygments/lexers/_mapping.py16
-rw-r--r--pygments/lexers/_php_builtins.py16
-rw-r--r--pygments/lexers/_sourcemod_builtins.py16
-rw-r--r--pygments/unistring.py7
7 files changed, 34 insertions, 56 deletions
diff --git a/pygments/cmdline.py b/pygments/cmdline.py
index 7c23ebee..035f8c87 100644
--- a/pygments/cmdline.py
+++ b/pygments/cmdline.py
@@ -371,7 +371,8 @@ def main(args=sys.argv):
infn = args[0]
try:
- code = open(infn, 'rb').read()
+ with open(infn, 'rb') as infp:
+ code = infp.read()
except Exception as err:
print('Error: cannot read infile:', err, file=sys.stderr)
return 1
diff --git a/pygments/formatters/_mapping.py b/pygments/formatters/_mapping.py
index 1a083904..d4aeaeb0 100755
--- a/pygments/formatters/_mapping.py
+++ b/pygments/formatters/_mapping.py
@@ -62,19 +62,15 @@ if __name__ == '__main__':
found_formatters.sort()
# extract useful sourcecode from this file
- f = open(__file__)
- try:
- content = f.read()
- finally:
- f.close()
+ with open(__file__) as fp:
+ content = fp.read()
header = content[:content.find('FORMATTERS = {')]
footer = content[content.find("if __name__ == '__main__':"):]
# write new file
- f = open(__file__, 'w')
- f.write(header)
- f.write('FORMATTERS = {\n %s\n}\n\n' % ',\n '.join(found_formatters))
- f.write(footer)
- f.close()
+ with open(__file__, 'w') as fp:
+ fp.write(header)
+ fp.write('FORMATTERS = {\n %s\n}\n\n' % ',\n '.join(found_formatters))
+ fp.write(footer)
print ('=== %d formatters processed.' % len(found_formatters))
diff --git a/pygments/lexers/_lua_builtins.py b/pygments/lexers/_lua_builtins.py
index 0519c6cb..79a8da62 100644
--- a/pygments/lexers/_lua_builtins.py
+++ b/pygments/lexers/_lua_builtins.py
@@ -221,21 +221,17 @@ if __name__ == '__main__':
return 'basic'
def regenerate(filename, modules):
- f = open(filename)
- try:
- content = f.read()
- finally:
- f.close()
+ with open(filename) as fp:
+ content = fp.read()
header = content[:content.find('MODULES = {')]
footer = content[content.find("if __name__ == '__main__':"):]
- f = open(filename, 'w')
- f.write(header)
- f.write('MODULES = %s\n\n' % pprint.pformat(modules))
- f.write(footer)
- f.close()
+ with open(filename, 'w') as fp:
+ fp.write(header)
+ fp.write('MODULES = %s\n\n' % pprint.pformat(modules))
+ fp.write(footer)
def run():
version = get_newest_version()
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index a714941b..80b4da4a 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -388,19 +388,15 @@ if __name__ == '__main__':
found_lexers.sort()
# extract useful sourcecode from this file
- f = open(__file__)
- try:
- content = f.read()
- finally:
- f.close()
+ with open(__file__) as fp:
+ content = fp.read()
header = content[:content.find('LEXERS = {')]
footer = content[content.find("if __name__ == '__main__':"):]
# write new file
- f = open(__file__, 'w')
- f.write(header)
- f.write('LEXERS = {\n %s,\n}\n\n' % ',\n '.join(found_lexers))
- f.write(footer)
- f.close()
+ with open(__file__, 'w') as fp:
+ fp.write(header)
+ fp.write('LEXERS = {\n %s,\n}\n\n' % ',\n '.join(found_lexers))
+ fp.write(footer)
print ('=== %d lexers processed.' % len(found_lexers))
diff --git a/pygments/lexers/_php_builtins.py b/pygments/lexers/_php_builtins.py
index 1e04cfc0..343b4416 100644
--- a/pygments/lexers/_php_builtins.py
+++ b/pygments/lexers/_php_builtins.py
@@ -4733,20 +4733,16 @@ if __name__ == '__main__':
os.remove(download[0])
def regenerate(filename, modules):
- f = open(filename)
- try:
- content = f.read()
- finally:
- f.close()
+ with open(filename) as fp:
+ content = fp.read()
header = content[:content.find('MODULES = {')]
footer = content[content.find("if __name__ == '__main__':"):]
- f = open(filename, 'w')
- f.write(header)
- f.write('MODULES = %s\n\n' % pprint.pformat(modules))
- f.write(footer)
- f.close()
+ with open(filename, 'w') as fp:
+ fp.write(header)
+ fp.write('MODULES = %s\n\n' % pprint.pformat(modules))
+ fp.write(footer)
def run():
print('>> Downloading Function Index')
diff --git a/pygments/lexers/_sourcemod_builtins.py b/pygments/lexers/_sourcemod_builtins.py
index 8ef6cbef..40ef5299 100644
--- a/pygments/lexers/_sourcemod_builtins.py
+++ b/pygments/lexers/_sourcemod_builtins.py
@@ -1132,21 +1132,17 @@ if __name__ == '__main__':
return functions
def regenerate(filename, natives):
- f = open(filename)
- try:
- content = f.read()
- finally:
- f.close()
+ with open(filename) as fp:
+ content = fp.read()
header = content[:content.find('FUNCTIONS = (')]
footer = content[content.find("if __name__ == '__main__':")-1:]
- f = open(filename, 'w')
- f.write(header)
- f.write(format_lines('FUNCTIONS', natives))
- f.write(footer)
- f.close()
+ with open(filename, 'w') as fp:
+ fp.write(header)
+ fp.write(format_lines('FUNCTIONS', natives))
+ fp.write(footer)
def run():
version = get_version()
diff --git a/pygments/unistring.py b/pygments/unistring.py
index 3db3ae8e..c2a171dd 100644
--- a/pygments/unistring.py
+++ b/pygments/unistring.py
@@ -99,11 +99,8 @@ if __name__ == '__main__':
categories = {}
- f = open(__file__)
- try:
- content = f.read()
- finally:
- f.close()
+ with open(__file__) as fp:
+ content = fp.read()
header = content[:content.find('Cc =')]
footer = content[content.find("def combine("):]