changeset 5439:b00cd44fea16

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).
author Joseph Myers <jsm@polyomino.org.uk>
date Wed, 25 Jul 2018 11:44:01 +0000
parents e2382945d302
children 60535a4822ab
files roundup/cgi/accept_language.py
diffstat 1 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/roundup/cgi/accept_language.py	Wed Jul 25 11:42:51 2018 +0000
+++ b/roundup/cgi/accept_language.py	Wed Jul 25 11:44:01 2018 +0000
@@ -35,8 +35,13 @@
 # both
 lre   = re.compile(nqlre + "|" + qlre)
 
-ascii = ''.join([chr(x) for x in range(256)])
 whitespace = ' \t\n\r\v\f'
+try:
+    # Python 3.
+    remove_ws = (str.maketrans('', '', whitespace),)
+except AttributeError:
+    # Python 2.
+    remove_ws = (None, whitespace)
 
 def parse(language_header):
     """parse(string_with_accept_header_content) -> languages list"""
@@ -44,7 +49,7 @@
     if language_header is None: return []
 
     # strip whitespaces.
-    lh = language_header.translate(ascii, whitespace)
+    lh = language_header.translate(*remove_ws)
 
     # if nothing, return
     if lh == "": return []

Roundup Issue Tracker: http://roundup-tracker.org/