diff --git a/librefi/compat.py b/librefi/compat.py index 7238026..15f9930 100644 --- a/librefi/compat.py +++ b/librefi/compat.py @@ -3004,6 +3004,7 @@ else: def compat_ctypes_WINFUNCTYPE(*args, **kwargs): return ctypes.WINFUNCTYPE(*args, **kwargs) +py_major_ver = platform.python_version_tuple()[0] __all__ = [ 'compat_HTMLParseError', @@ -3063,5 +3064,6 @@ __all__ = [ 'compat_xml_parse_error', 'compat_xpath', 'compat_zip', + 'py_major_ver', 'workaround_optparse_bug9161', ] diff --git a/librefi/utils.py b/librefi/utils.py index 30d2cad..1e44eb6 100644 --- a/librefi/utils.py +++ b/librefi/utils.py @@ -9,7 +9,9 @@ import random from .compat import ( compat_parse_qs as parse_qs, compat_urllib_parse_quote as qs_quote, - compat_urlparse as urlparse, + compat_urllib_parse_urlparse as urlparse, + compat_str, + py_major_ver, ) @@ -127,15 +129,19 @@ def regex_search_string(regexes, string, default=None, multiple=False, whole_mat def dump_qs(obj): - old_qs = [] + if py_major_ver == 2: + old_qs = obj.iteritems() + else: + old_qs = list(obj.items()) + # sorting by key to unify the result string between python versions + # https://stackoverflow.com/a/3295662/8222484 + old_qs.sort() qs = [] - for key in obj: - old_qs.append((key, obj[key])) not_flat = True while not_flat: not_flat = False for old_qs_element in old_qs: - if isinstance(old_qs_element[1], (str, int, float)): + if isinstance(old_qs_element[1], (compat_str, int, float)): qs.append((old_qs_element[0], old_qs_element[1])) elif isinstance(old_qs_element[1], (dict)): for subkey in old_qs_element[1]: @@ -149,7 +155,7 @@ def dump_qs(obj): element = old_qs_element[1][index] if element is not None: qs.append( - (old_qs_element[0] + "[" + str(index) + "]", element)) + (old_qs_element[0] + "[" + compat_str(index) + "]", element)) if isinstance(element, (dict, list)): not_flat = True if not_flat: @@ -157,8 +163,8 @@ def dump_qs(obj): qs = [] strng = "" for el in qs: - strng += qs_quote(str(el[0]), encoding="utf8") + "=" + \ - qs_quote(str(el[1]), encoding="utf8") + "&" + strng += qs_quote(compat_str(el[0]).encode("utf-8")) + "=" + \ + qs_quote(compat_str(el[1]).encode("utf-8")) + "&" return strng[:-1] diff --git a/tests/querystring.py b/tests/querystring.py index fca09a6..75fba3d 100644 --- a/tests/querystring.py +++ b/tests/querystring.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from librefi.utils import dump_qs +from librefi.compat import compat_str from hashlib import sha512 @@ -15,7 +16,7 @@ def test_dump(): {"przestań mi": ["kurwa", "rodzinę prześladować"]}], } result = dump_qs(obj) - assert isinstance(result, str) + assert isinstance(result, compat_str) assert sha512(result.encode("utf-8")).hexdigest( # flake8: noqa: E501 ) == "80cb0feb585e8a5969598797b74c6f7f2f314ee97dfd2d6f4aaf431b800f6c7a1dfe77efd550a1009d41ef2886b21b87ce1d9e4f11444af554a916987344aee1" diff --git a/tests/regex_utils.py b/tests/regex_utils.py index b0fe665..eed76aa 100644 --- a/tests/regex_utils.py +++ b/tests/regex_utils.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from librefi.utils import regex_search_string +from librefi.compat import compat_str HTML_STRING = """