py2.7c: various compatibility fixes
This commit is contained in:
parent
320c29c6b3
commit
23e0670e25
|
@ -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',
|
||||
]
|
||||
|
|
|
@ -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]
|
||||
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from librefi.utils import regex_search_string
|
||||
from librefi.compat import compat_str
|
||||
|
||||
HTML_STRING = """
|
||||
<form method="POST" action="/?your=mother">
|
||||
|
@ -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]
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue