Skip to content

Commit 39e19b4

Browse files
committed
Python 3 preparation: update tokenize use in cgitb.py.
Note that the same interface that has changed incompatibly is also used in tools/pygettext.py. That file also needs fixing, but this patch does *not* attempt such a fix.
1 parent 4e65484 commit 39e19b4

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

roundup/cgi/cgitb.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pydoc, traceback
1212

1313
from roundup.cgi import templating, TranslationService
14+
from roundup.anypy.strings import s2b
1415

1516
def get_translator(i18n=None):
1617
"""Return message translation function (gettext)
@@ -156,12 +157,23 @@ def tokeneater(type, token, start, end, line, names=names):
156157
names.append(token)
157158
if type == tokenize.NEWLINE: raise IndexError
158159
def linereader(file=file, lnum=[lnum]):
159-
line = linecache.getline(file, lnum[0])
160+
line = s2b(linecache.getline(file, lnum[0]))
160161
lnum[0] = lnum[0] + 1
161162
return line
162163

164+
# The interface that is tokenize.tokenize in Python 3 is
165+
# called tokenize.generate_tokens in Python 2. However,
166+
# Python 2 has tokenize.tokenize with a different interface,
167+
# and Python 3 has an undocumented generate_tokens function,
168+
# also with a different interface, so a version check is
169+
# needed instead of checking for which functions exist.
170+
if sys.version_info[0] > 2:
171+
tokenize_fn = tokenize.tokenize
172+
else:
173+
tokenize_fn = tokenize.generate_tokens
163174
try:
164-
tokenize.tokenize(linereader, tokeneater)
175+
for t in tokenize_fn(linereader):
176+
tokeneater(*t)
165177
except IndexError:
166178
pass
167179
lvals = []

0 commit comments

Comments
 (0)