Skip to content

Commit 83d94c2

Browse files
committed
Python 3 preparation: update string translate method call in cgi/accept_language.py.
The str translate method in Python 3 is sufficiently different from that in Python 2 that different arguments are needed here. Note that the existing code "ascii = ''.join([chr(x) for x in range(256)])" is redundant with both versions (in Python 2, None can be used instead as the first translate argument from Python 2.6 onwards).
1 parent 66f3ecf commit 83d94c2

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

roundup/cgi/accept_language.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,21 @@
3535
# both
3636
lre = re.compile(nqlre + "|" + qlre)
3737

38-
ascii = ''.join([chr(x) for x in range(256)])
3938
whitespace = ' \t\n\r\v\f'
39+
try:
40+
# Python 3.
41+
remove_ws = (str.maketrans('', '', whitespace),)
42+
except AttributeError:
43+
# Python 2.
44+
remove_ws = (None, whitespace)
4045

4146
def parse(language_header):
4247
"""parse(string_with_accept_header_content) -> languages list"""
4348

4449
if language_header is None: return []
4550

4651
# strip whitespaces.
47-
lh = language_header.translate(ascii, whitespace)
52+
lh = language_header.translate(*remove_ws)
4853

4954
# if nothing, return
5055
if lh == "": return []

0 commit comments

Comments
 (0)