|
| 1 | +"""Compatibility wrappers for Py2/Py3.""" |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +if sys.version_info[0] < 3: |
| 6 | + from UserDict import UserDict, IterableUserDict |
| 7 | + from urllib import quote |
| 8 | + from urllib import quote_plus |
| 9 | + from urllib import unquote as urllib_unquote |
| 10 | + from urllib import urlopen |
| 11 | + from urlparse import urlparse |
| 12 | + |
| 13 | + def unquote(uri): |
| 14 | + """Specialized unquote that uses UTF-8 for parsing.""" |
| 15 | + uri = uri.encode('ascii') |
| 16 | + unquoted = urllib_unquote(uri) |
| 17 | + return unquoted.decode('utf-8') |
| 18 | + |
| 19 | + # Old-style of re-raising an exception is SyntaxError in Python 3, |
| 20 | + # so hide behind exec() so the Python 3 parser doesn't see it |
| 21 | + exec('''def reraise(exc_type, exc_value, exc_traceback): |
| 22 | + """Re-raise an exception given information from sys.exc_info() |
| 23 | +
|
| 24 | + Note that unlike six.reraise, this does not support replacing the |
| 25 | + traceback. All arguments must come from a single sys.exc_info() call. |
| 26 | + """ |
| 27 | + raise exc_type, exc_value, exc_traceback |
| 28 | + ''') |
| 29 | + |
| 30 | +else: |
| 31 | + from collections import UserDict |
| 32 | + IterableUserDict = UserDict |
| 33 | + from urllib.parse import quote, quote_plus, unquote, urlparse |
| 34 | + from urllib.request import urlopen |
| 35 | + |
| 36 | + def reraise(exc_type, exc_value, exc_traceback): |
| 37 | + """Re-raise an exception given information from sys.exc_info() |
| 38 | +
|
| 39 | + Note that unlike six.reraise, this does not support replacing the |
| 40 | + traceback. All arguments must come from a single sys.exc_info() call. |
| 41 | + """ |
| 42 | + # In Python 3, all exception info is contained in one object. |
| 43 | + raise exc_value |
0 commit comments