Skip to content

Commit 77d5275

Browse files
committed
Removed Python2 support.
This commit removes all Python2-isms from the code.
1 parent 743bd37 commit 77d5275

16 files changed

+95
-150
lines changed

CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ Maximilian Hils (github.com/mhils)
5252
BarkeH (github.com/BarkeH)
5353
cav71 (github.com/cav71)
5454
Crozzers (github.com/Crozzers)
55+
Bastian Venthur (https://github.com/venthur), removed Python2 support

TODO.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
- py3: py2.4 test (broken?)
21
- add "smarty-pants" extra to wiki
32
- add "html-classes" extra to wiki
43
- more on the "code-color" extra wiki page

lib/markdown2.py

+14-38
Original file line numberDiff line numberDiff line change
@@ -111,22 +111,6 @@
111111
import codecs
112112
from collections import defaultdict
113113

114-
115-
# ---- Python version compat
116-
117-
# Use `bytes` for byte strings and `unicode` for unicode strings (str in Py3).
118-
if sys.version_info[0] <= 2:
119-
py3 = False
120-
try:
121-
bytes
122-
except NameError:
123-
bytes = str
124-
base_string_type = basestring
125-
elif sys.version_info[0] >= 3:
126-
py3 = True
127-
unicode = str
128-
base_string_type = str
129-
130114
# ---- globals
131115

132116
DEBUG = False
@@ -315,9 +299,9 @@ def convert(self, text):
315299
# articles):
316300
self.reset()
317301

318-
if not isinstance(text, unicode):
302+
if not isinstance(text, str):
319303
# TODO: perhaps shouldn't presume UTF-8 for string input?
320-
text = unicode(text, 'utf-8')
304+
text = str(text, 'utf-8')
321305

322306
if self.use_file_vars:
323307
# Look for emacs-style file variable hints.
@@ -1611,7 +1595,7 @@ def header_id_from_text(self, text, prefix, n):
16111595
the TOC (if the "toc" extra is specified).
16121596
"""
16131597
header_id = _slugify(text)
1614-
if prefix and isinstance(prefix, base_string_type):
1598+
if prefix and isinstance(prefix, str):
16151599
header_id = prefix + '-' + header_id
16161600

16171601
self._count_from_header_id[header_id] += 1
@@ -2485,7 +2469,7 @@ def indent():
24852469
return '\n'.join(lines) + '\n'
24862470

24872471

2488-
class UnicodeWithAttrs(unicode):
2472+
class UnicodeWithAttrs(str):
24892473
"""A subclass of unicode used for the return value of conversion to
24902474
possibly attach some attributes. E.g. the "toc_html" attribute when
24912475
the "toc" extra is used.
@@ -2565,8 +2549,8 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False):
25652549
"""
25662550
DEBUG = False
25672551
if DEBUG:
2568-
print("dedent: dedent(..., tabsize=%d, skip_first_line=%r)"\
2569-
% (tabsize, skip_first_line))
2552+
print(("dedent: dedent(..., tabsize=%d, skip_first_line=%r)"\
2553+
% (tabsize, skip_first_line)))
25702554
margin = None
25712555
for i, line in enumerate(lines):
25722556
if i == 0 and skip_first_line: continue
@@ -2582,12 +2566,12 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False):
25822566
break
25832567
else:
25842568
continue # skip all-whitespace lines
2585-
if DEBUG: print("dedent: indent=%d: %r" % (indent, line))
2569+
if DEBUG: print(("dedent: indent=%d: %r" % (indent, line)))
25862570
if margin is None:
25872571
margin = indent
25882572
else:
25892573
margin = min(margin, indent)
2590-
if DEBUG: print("dedent: margin=%r" % margin)
2574+
if DEBUG: print(("dedent: margin=%r" % margin))
25912575

25922576
if margin is not None and margin > 0:
25932577
for i, line in enumerate(lines):
@@ -2599,16 +2583,16 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False):
25992583
elif ch == '\t':
26002584
removed += tabsize - (removed % tabsize)
26012585
elif ch in '\r\n':
2602-
if DEBUG: print("dedent: %r: EOL -> strip up to EOL" % line)
2586+
if DEBUG: print(("dedent: %r: EOL -> strip up to EOL" % line))
26032587
lines[i] = lines[i][j:]
26042588
break
26052589
else:
26062590
raise ValueError("unexpected non-whitespace char %r in "
26072591
"line %r while removing %d-space margin"
26082592
% (ch, line, margin))
26092593
if DEBUG:
2610-
print("dedent: %r: %r -> removed %d/%d"\
2611-
% (line, ch, removed, margin))
2594+
print(("dedent: %r: %r -> removed %d/%d"\
2595+
% (line, ch, removed, margin)))
26122596
if removed == margin:
26132597
lines[i] = lines[i][j+1:]
26142598
break
@@ -2860,23 +2844,15 @@ def main(argv=None):
28602844
p.stdin.write(text.encode('utf-8'))
28612845
p.stdin.close()
28622846
perl_html = p.stdout.read().decode('utf-8')
2863-
if py3:
2864-
sys.stdout.write(perl_html)
2865-
else:
2866-
sys.stdout.write(perl_html.encode(
2867-
sys.stdout.encoding or "utf-8", 'xmlcharrefreplace'))
2847+
sys.stdout.write(perl_html)
28682848
print("==== markdown2.py ====")
28692849
html = markdown(text,
28702850
html4tags=opts.html4tags,
28712851
safe_mode=opts.safe_mode,
28722852
extras=extras, link_patterns=link_patterns,
28732853
use_file_vars=opts.use_file_vars,
28742854
cli=True)
2875-
if py3:
2876-
sys.stdout.write(html)
2877-
else:
2878-
sys.stdout.write(html.encode(
2879-
sys.stdout.encoding or "utf-8", 'xmlcharrefreplace'))
2855+
sys.stdout.write(html)
28802856
if extras and "toc" in extras:
28812857
log.debug("toc_html: " +
28822858
str(html.toc_html.encode(sys.stdout.encoding or "utf-8", 'xmlcharrefreplace')))
@@ -2890,7 +2866,7 @@ def main(argv=None):
28902866
else:
28912867
norm_html = html
28922868
norm_perl_html = perl_html
2893-
print("==== match? %r ====" % (norm_perl_html == norm_html))
2869+
print(("==== match? %r ====" % (norm_perl_html == norm_html)))
28942870

28952871

28962872
if __name__ == "__main__":

perf/gen_perf_cases.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
def gen_aspn_cases(limit=0):
1717
base_dir = TMP+'aspn-cases'
1818
if exists(base_dir):
19-
print "'%s' exists, skipping" % base_dir
19+
print("'%s' exists, skipping" % base_dir)
2020
return
2121
os.makedirs(base_dir)
2222
sys.stdout.write("generate %s" % base_dir); sys.stdout.flush()
@@ -49,10 +49,10 @@ def gen_aspn_cases(limit=0):
4949
def gen_test_cases():
5050
base_dir = TMP+"test-cases"
5151
if exists(base_dir):
52-
print "'%s' exists, skipping" % base_dir
52+
print("'%s' exists, skipping" % base_dir)
5353
return
5454
os.makedirs(base_dir)
55-
print "generate %s" % base_dir
55+
print("generate %s" % base_dir)
5656
for test_cases_dir in glob(join("..", "test", "*-cases")):
5757
for text_file in glob(join(test_cases_dir, "*.text")):
5858
shutil.copy(text_file, join(base_dir, basename(text_file)))
@@ -137,7 +137,7 @@ def _markdown_from_aspn_html(html):
137137
try:
138138
idx = markdown.index(marker)
139139
except ValueError:
140-
print "marker: %r" % marker
140+
print("marker: %r" % marker)
141141
raise
142142
if not markdown[:idx].strip():
143143
#TODO: Correct this false diagnosis. Problem is not limited
@@ -194,8 +194,8 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False):
194194
"""
195195
DEBUG = False
196196
if DEBUG:
197-
print "dedent: dedent(..., tabsize=%d, skip_first_line=%r)"\
198-
% (tabsize, skip_first_line)
197+
print("dedent: dedent(..., tabsize=%d, skip_first_line=%r)"\
198+
% (tabsize, skip_first_line))
199199
indents = []
200200
margin = None
201201
for i, line in enumerate(lines):
@@ -212,12 +212,12 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False):
212212
break
213213
else:
214214
continue # skip all-whitespace lines
215-
if DEBUG: print "dedent: indent=%d: %r" % (indent, line)
215+
if DEBUG: print("dedent: indent=%d: %r" % (indent, line))
216216
if margin is None:
217217
margin = indent
218218
else:
219219
margin = min(margin, indent)
220-
if DEBUG: print "dedent: margin=%r" % margin
220+
if DEBUG: print("dedent: margin=%r" % margin)
221221

222222
if margin is not None and margin > 0:
223223
for i, line in enumerate(lines):
@@ -229,16 +229,16 @@ def _dedentlines(lines, tabsize=8, skip_first_line=False):
229229
elif ch == '\t':
230230
removed += tabsize - (removed % tabsize)
231231
elif ch in '\r\n':
232-
if DEBUG: print "dedent: %r: EOL -> strip up to EOL" % line
232+
if DEBUG: print("dedent: %r: EOL -> strip up to EOL" % line)
233233
lines[i] = lines[i][j:]
234234
break
235235
else:
236236
raise ValueError("unexpected non-whitespace char %r in "
237237
"line %r while removing %d-space margin"
238238
% (ch, line, margin))
239239
if DEBUG:
240-
print "dedent: %r: %r -> removed %d/%d"\
241-
% (line, ch, removed, margin)
240+
print("dedent: %r: %r -> removed %d/%d"\
241+
% (line, ch, removed, margin))
242242
if removed == margin:
243243
lines[i] = lines[i][j+1:]
244244
break

perf/perf.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def time_markdown_py(cases_dir, repeat):
4545
pass
4646
end = clock()
4747
times.append(end - start)
48-
print " markdown.py: best of %d: %.3fs" % (repeat, min(times))
48+
print(" markdown.py: best of %d: %.3fs" % (repeat, min(times)))
4949

5050
@hotshotit
5151
def hotshot_markdown2_py(cases_dir, repeat):
@@ -66,7 +66,7 @@ def time_markdown2_py(cases_dir, repeat):
6666
markdowner.convert(content)
6767
end = clock()
6868
times.append(end - start)
69-
print " markdown2.py: best of %d: %.3fs" % (repeat, min(times))
69+
print(" markdown2.py: best of %d: %.3fs" % (repeat, min(times)))
7070

7171
def time_markdown_pl(cases_dir, repeat):
7272
times = []
@@ -75,7 +75,7 @@ def time_markdown_pl(cases_dir, repeat):
7575
os.system('perl time_markdown_pl.pl "%s"' % cases_dir)
7676
end = clock()
7777
times.append(end - start)
78-
print " Markdown.pl: best of %d: %.3fs" % (repeat, min(times))
78+
print(" Markdown.pl: best of %d: %.3fs" % (repeat, min(times)))
7979

8080
def time_all(cases_dir, repeat):
8181
time_markdown_pl(cases_dir, repeat=repeat)
@@ -131,10 +131,10 @@ def main(args=sys.argv):
131131
if timer_name not in d:
132132
raise ValueError("no '%s' timer function" % timer_name)
133133
timer = d[timer_name]
134-
print "Profile conversion of %s (plat=%s):" \
135-
% (os.path.join(cases_dir, "*.text"), sys.platform)
134+
print("Profile conversion of %s (plat=%s):" \
135+
% (os.path.join(cases_dir, "*.text"), sys.platform))
136136
timer(cases_dir, repeat=opts.repeat)
137-
print
137+
print()
138138
os.system("python show_stats.py %s.prof" % timer_name)
139139

140140
else:
@@ -145,8 +145,8 @@ def main(args=sys.argv):
145145
if timer_name not in d:
146146
raise ValueError("no '%s' timer function" % timer_name)
147147
timer = d[timer_name]
148-
print "Time conversion of %s (plat=%s):" \
149-
% (os.path.join(cases_dir, "*.text"), sys.platform)
148+
print("Time conversion of %s (plat=%s):" \
149+
% (os.path.join(cases_dir, "*.text"), sys.platform))
150150
timer(cases_dir, repeat=opts.repeat)
151151

152152
if __name__ == "__main__":

perf/strip_cookbook_data.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def doit():
77
recipes_path = expanduser("recipes.pprint")
88
recipe_dicts = eval(open(recipes_path).read())
99
for r in recipe_dicts:
10-
for key in r.keys():
10+
for key in list(r.keys()):
1111
if key not in ('desc', 'comments'):
1212
del r[key]
1313
for c in r['comments']:
14-
for key in c.keys():
14+
for key in list(c.keys()):
1515
if key not in ('comment', 'title'):
1616
del c[key]
1717

perf/util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def wrapper(*args, **kw):
3838
return func(*args, **kw)
3939
finally:
4040
total_time = clock() - start_time
41-
print "%s took %.3fs" % (func.func_name, total_time)
41+
print("%s took %.3fs" % (func.__name__, total_time))
4242
return wrapper
4343

4444
def hotshotit(func):
4545
def wrapper(*args, **kw):
4646
import hotshot
4747
global hotshotProfilers
48-
prof_name = func.func_name+".prof"
48+
prof_name = func.__name__+".prof"
4949
profiler = hotshotProfilers.get(prof_name)
5050
if profiler is None:
5151
profiler = hotshot.Profile(prof_name)

sandbox/wiki.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
]
1919
processor = markdown2.Markdown(extras=["link-patterns"],
2020
link_patterns=link_patterns)
21-
print processor.convert(wiki_page)
21+
print(processor.convert(wiki_page))

0 commit comments

Comments
 (0)