Skip to content

Commit 71c2460

Browse files
committed
Remove all deprecated usages of codecs.open and fix many unclosed file warnings
1 parent cb5d253 commit 71c2460

9 files changed

Lines changed: 102 additions & 101 deletions

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ test:
1212
testone:
1313
cd test && python test.py -- -knownfailure
1414

15+
.PHONY: testwarn
16+
testwarn:
17+
cd test && python -Wd test.py -- -knownfailure
18+
1519
.PHONY: testredos
1620
testredos:
1721
python test/test_redos.py

lib/markdown2.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@
114114
__author__ = "Trent Mick"
115115

116116
import argparse
117-
import codecs
118117
import logging
119118
import re
120119
import sys
@@ -179,9 +178,8 @@ def markdown_path(
179178
footnote_return_symbol: Optional[str] = None,
180179
use_file_vars: bool = False
181180
) -> 'UnicodeWithAttrs':
182-
fp = codecs.open(path, 'r', encoding)
183-
text = fp.read()
184-
fp.close()
181+
with open(path, 'r', encoding=encoding) as f:
182+
text = f.read()
185183
return Markdown(html4tags=html4tags, tab_width=tab_width,
186184
safe_mode=safe_mode, extras=extras,
187185
link_patterns=link_patterns,
@@ -4766,9 +4764,8 @@ def main(argv=None):
47664764
if path == '-':
47674765
text = sys.stdin.read()
47684766
else:
4769-
fp = codecs.open(path, 'r', opts.encoding)
4770-
text = fp.read()
4771-
fp.close()
4767+
with open(path, 'r', encoding=opts.encoding) as f:
4768+
text = f.read()
47724769
if opts.compare:
47734770
from subprocess import PIPE, Popen
47744771
print("==== Markdown.pl ====")

perf/gen_perf_cases.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from glob import glob
88
import operator
99
import shutil
10-
import codecs
1110

1211

1312
TMP = "tmp-"
@@ -16,16 +15,16 @@ def gen_aspn_cases(limit=0):
1615
base_dir = TMP+'aspn-cases'
1716
if exists(base_dir):
1817
print("'%s' exists, skipping" % base_dir)
19-
return
18+
return
2019
os.makedirs(base_dir)
2120
sys.stdout.write("generate %s" % base_dir); sys.stdout.flush()
2221
recipes_path = expanduser("~/as/code.as.com/db/aspn/recipes.pprint")
23-
recipe_dicts = eval(open(recipes_path).read())
22+
with open(recipes_path) as f:
23+
recipe_dicts = eval(f.read())
2424
for i, r in enumerate(recipe_dicts):
2525
sys.stdout.write('.'); sys.stdout.flush()
26-
f = codecs.open(join(base_dir, "r%04d.text" % i), "w", "utf-8")
27-
f.write(r["desc"])
28-
f.close()
26+
with open(join(base_dir, "r%04d.text" % i), "w", encoding="utf-8") as f:
27+
f.write(r["desc"])
2928

3029
for j, c in enumerate(sorted(r["comments"],
3130
key=operator.itemgetter("pub_date"))):
@@ -36,10 +35,8 @@ def gen_aspn_cases(limit=0):
3635
headline += '.'
3736
headline = _markdown_from_aspn_html(headline).strip()
3837
text = "**" + headline + "** " + text
39-
f = codecs.open(join(base_dir, "r%04dc%02d.text" % (i, j)),
40-
'w', "utf-8")
41-
f.write(text)
42-
f.close()
38+
with open(join(base_dir, "r%04dc%02d.text" % (i, j)), 'w', encoding="utf-8") as f:
39+
f.write(text)
4340

4441
if limit and i >= limit:
4542
break
@@ -49,7 +46,7 @@ def gen_test_cases():
4946
base_dir = TMP+"test-cases"
5047
if exists(base_dir):
5148
print("'%s' exists, skipping" % base_dir)
52-
return
49+
return
5350
os.makedirs(base_dir)
5451
print("generate %s" % base_dir)
5552
for test_cases_dir in glob(join("..", "test", "*-cases")):
@@ -106,10 +103,10 @@ def _markdown_from_aspn_html(html):
106103
if title is None:
107104
replacement = '[{}]({})'.format(content, escaped_href)
108105
else:
109-
replacement = '[{}]({} "{}")'.format(content, escaped_href,
106+
replacement = '[{}]({} "{}")'.format(content, escaped_href,
110107
title.replace('"', "'"))
111108
markdown = markdown[:start] + replacement + markdown[end:]
112-
109+
113110
markdown = markdown.replace(" ", ' ')
114111

115112
# <pre> part 1: Pull out <pre>-blocks and put in placeholders
@@ -179,18 +176,18 @@ def _markdown_from_aspn_html(html):
179176
# Recipe: dedent (0.1.2)
180177
def _dedentlines(lines, tabsize=8, skip_first_line=False):
181178
"""_dedentlines(lines, tabsize=8, skip_first_line=False) -> dedented lines
182-
179+
183180
"lines" is a list of lines to dedent.
184181
"tabsize" is the tab width to use for indent width calculations.
185182
"skip_first_line" is a boolean indicating if the first line should
186183
be skipped for calculating the indent width and for dedenting.
187184
This is sometimes useful for docstrings and similar.
188-
185+
189186
Same as dedent() except operates on a sequence of lines. Note: the
190187
lines list is modified **in-place**.
191188
"""
192189
DEBUG = False
193-
if DEBUG:
190+
if DEBUG:
194191
print("dedent: dedent(..., tabsize=%d, skip_first_line=%r)"\
195192
% (tabsize, skip_first_line))
196193
indents = []
@@ -255,7 +252,7 @@ def _dedent(text, tabsize=8, skip_first_line=False):
255252
"skip_first_line" is a boolean indicating if the first line should
256253
be skipped for calculating the indent width and for dedenting.
257254
This is sometimes useful for docstrings and similar.
258-
255+
259256
textwrap.dedent(s), but don't expand tabs to spaces
260257
"""
261258
lines = text.splitlines(1)

perf/strip_cookbook_data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
def doit():
55
recipes_path = expanduser("recipes.pprint")
6-
recipe_dicts = eval(open(recipes_path).read())
6+
with open(recipes_path) as f:
7+
recipe_dicts = eval(f.read())
78
for r in recipe_dicts:
89
for key in r.keys():
910
if key not in ('desc', 'comments'):

0 commit comments

Comments
 (0)