From 23e0670e25861bd162e7c6b4a5be6fb4e1f887c6 Mon Sep 17 00:00:00 2001 From: Laura Liberda Date: Sun, 29 Nov 2020 02:36:57 +0100 Subject: [PATCH] py2.7c: various compatibility fixes --- librefi/compat.py | 2 ++ librefi/utils.py | 22 ++++++++++++++-------- tests/querystring.py | 3 ++- tests/regex_utils.py | 3 ++- tests/string_generators.py | 5 +++-- tests/url_utils.py | 3 ++- 6 files changed, 25 insertions(+), 13 deletions(-) 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 = """
@@ -50,7 +51,7 @@ def test_regex_search_multiple_results(): assert isinstance(results, list) assert len(results) == len(EXPECTED_RESULT_2) for i in range(len(results)): - assert isinstance(results[i], str) + assert isinstance(results[i], compat_str) assert results[i] == EXPECTED_RESULT_2[i] diff --git a/tests/string_generators.py b/tests/string_generators.py index c5125f6..b494c12 100644 --- a/tests/string_generators.py +++ b/tests/string_generators.py @@ -2,12 +2,13 @@ from __future__ import unicode_literals from librefi.utils import get_user_agent, get_email_address +from librefi.compat import compat_str import re def test_email_address(): email = get_email_address() - assert isinstance(email, str) + assert isinstance(email, compat_str) assert re.search( # intentionally dumb and not covering a lot of actual emails r"^[a-zA-Z\d](?:[a-zA-Z\d._-]*[a-zA-Z\d])?@[a-z-\d]+(?:\.[a-z-\d]+)+$", @@ -17,5 +18,5 @@ def test_email_address(): def test_user_agent(): ua = get_user_agent() - assert isinstance(ua, str) + assert isinstance(ua, compat_str) pass diff --git a/tests/url_utils.py b/tests/url_utils.py index eecb207..641dea7 100644 --- a/tests/url_utils.py +++ b/tests/url_utils.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from librefi.utils import absolute_url +from librefi.compat import compat_str OLD_URL = "https://sakamoto.pl/ddd" NEW_URL_RELATIVE = "/DDD?test=yes" @@ -12,7 +13,7 @@ NEW_URL_ABSOLUTE = "https://sakamoto.pl/DDD?test=yes" def test_basic(): abso = absolute_url(NEW_URL_RELATIVE, OLD_URL) - assert isinstance(abso, (str)) + assert isinstance(abso, (compat_str)) print(abso) assert abso == NEW_URL_ABSOLUTE pass