You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2020/12/31 22:29:45 UTC
[allura] 02/04: [#8382] simpler version of smart_str,
handles already encoded/binary better too
This is an automated email from the ASF dual-hosted git repository.
brondsem pushed a commit to branch db/8382
in repository https://gitbox.apache.org/repos/asf/allura.git
commit 602d13d610fd1c24c930a41911cfff3c60946d38
Author: Dave Brondsema <db...@slashdotmedia.com>
AuthorDate: Thu Dec 31 16:47:52 2020 -0500
[#8382] simpler version of smart_str, handles already encoded/binary better too
---
Allura/allura/lib/utils.py | 38 +++++++++++---------------------------
Allura/allura/tests/test_utils.py | 8 ++++++++
2 files changed, 19 insertions(+), 27 deletions(-)
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 9dd2a75..dca2d3b 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -783,38 +783,22 @@ def is_nofollow_url(url):
return url_domain and url_domain not in ok_domains
-def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
- """
- Returns a bytestring version of 's', encoded as specified in 'encoding'.
-
- If strings_only is True, don't convert (some) non-string-like objects.
-
- This function was borrowed from Django
- """
- if strings_only and isinstance(s, (type(None), int)):
+def smart_str(s):
+ '''
+ Returns a bytestring version of 's' from any type
+ '''
+ if isinstance(s, six.binary_type):
return s
- elif not isinstance(s, six.string_types):
- try:
- return str(s)
- except UnicodeEncodeError:
- if isinstance(s, Exception):
- # An Exception subclass containing non-ASCII data that doesn't
- # know how to print itself properly. We shouldn't raise a
- # further exception.
- return ' '.join([smart_str(arg, encoding, strings_only,
- errors) for arg in s])
- return six.text_type(s).encode(encoding, errors)
- elif isinstance(s, six.text_type):
- r = s.encode(encoding, errors)
- return r
- elif s and encoding != 'utf-8':
- return s.decode('utf-8', errors).encode(encoding, errors)
else:
- return s
+ return six.text_type(s).encode('utf-8')
def generate_smart_str(params):
- for (key, value) in six.iteritems(params):
+ if isinstance(params, collections.Mapping):
+ params_list = six.iteritems(params)
+ else:
+ params_list = params
+ for key, value in params_list:
yield smart_str(key), smart_str(value)
diff --git a/Allura/allura/tests/test_utils.py b/Allura/allura/tests/test_utils.py
index cff7a5d..98f5b12 100644
--- a/Allura/allura/tests/test_utils.py
+++ b/Allura/allura/tests/test_utils.py
@@ -421,3 +421,11 @@ def test_close_ipv4_addrs():
assert utils.close_ipv4_addrs('1.2.3.4', '1.2.3.255')
assert not utils.close_ipv4_addrs('1.2.3.4', '1.2.4.4')
+
+def test_urlencode():
+ # dict - a simple one so arbitrary ordering doesn't cause problems on py2
+ assert_equal(utils.urlencode({'a': 'hello'}),
+ 'a=hello')
+ # list of pairs - including unicode and bytes
+ assert_equal(utils.urlencode([('a', 1), ('b', 'ƒ'), ('c', 'ƒ'.encode('utf8'))]),
+ 'a=1&b=%C6%92&c=%C6%92')