Mercurial > p > roundup > code
comparison roundup/cgi/accept_language.py @ 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 | 74476eaac38a |
| children | 936275dfe1fa |
comparison
equal
deleted
inserted
replaced
| 5438:e2382945d302 | 5439:b00cd44fea16 |
|---|---|
| 33 # regexp for languange-range search with quality value | 33 # regexp for languange-range search with quality value |
| 34 qlre = "([A-Za-z]+[-[A-Za-z]+]*);q=([\d\.]+)" | 34 qlre = "([A-Za-z]+[-[A-Za-z]+]*);q=([\d\.]+)" |
| 35 # both | 35 # both |
| 36 lre = re.compile(nqlre + "|" + qlre) | 36 lre = re.compile(nqlre + "|" + qlre) |
| 37 | 37 |
| 38 ascii = ''.join([chr(x) for x in range(256)]) | |
| 39 whitespace = ' \t\n\r\v\f' | 38 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) | |
| 40 | 45 |
| 41 def parse(language_header): | 46 def parse(language_header): |
| 42 """parse(string_with_accept_header_content) -> languages list""" | 47 """parse(string_with_accept_header_content) -> languages list""" |
| 43 | 48 |
| 44 if language_header is None: return [] | 49 if language_header is None: return [] |
| 45 | 50 |
| 46 # strip whitespaces. | 51 # strip whitespaces. |
| 47 lh = language_header.translate(ascii, whitespace) | 52 lh = language_header.translate(*remove_ws) |
| 48 | 53 |
| 49 # if nothing, return | 54 # if nothing, return |
| 50 if lh == "": return [] | 55 if lh == "": return [] |
| 51 | 56 |
| 52 # split by commas and parse the quality values. | 57 # split by commas and parse the quality values. |
