diff options
author | Vlastimil Zíma <vlastimil.zima@nic.cz> | 2018-05-02 15:26:20 +0200 |
---|---|---|
committer | Vlastimil Zíma <vlastimil.zima@nic.cz> | 2018-05-11 10:05:43 +0200 |
commit | 85574cd2161cf74782f51b04e988d83200bb2bf4 (patch) | |
tree | d0d9a14ed240424dd4add1af44708835e7e885a7 /openid | |
parent | 455b4239eeeac12d209d723c66d27df12447a946 (diff) | |
download | openid-85574cd2161cf74782f51b04e988d83200bb2bf4.tar.gz |
Transform key-value form API to text strings
Diffstat (limited to 'openid')
-rw-r--r-- | openid/association.py | 4 | ||||
-rw-r--r-- | openid/kvform.py | 46 | ||||
-rw-r--r-- | openid/server/server.py | 2 | ||||
-rw-r--r-- | openid/test/test_kvform.py | 18 |
4 files changed, 41 insertions, 29 deletions
diff --git a/openid/association.py b/openid/association.py index 920bd63..a265bd4 100644 --- a/openid/association.py +++ b/openid/association.py @@ -402,7 +402,7 @@ class Association(object): @return: String in KV form suitable for deserialization by deserialize. - @rtype: str + @rtype: six.text_type """ data = { 'version': '2', @@ -465,7 +465,7 @@ class Association(object): @return: The binary signature of this sequence of pairs - @rtype: str + @rtype: six.text_type """ kv = kvform.seqToKV(pairs) diff --git a/openid/kvform.py b/openid/kvform.py index e0e91a0..ca196c5 100644 --- a/openid/kvform.py +++ b/openid/kvform.py @@ -1,7 +1,14 @@ -__all__ = ['seqToKV', 'kvToSeq', 'dictToKV', 'kvToDict'] +"""Utilities for key-value format conversions.""" +from __future__ import unicode_literals import logging -import types + +import six + +from .oidutil import string_to_text + +__all__ = ['seqToKV', 'kvToSeq', 'dictToKV', 'kvToDict'] + _LOGGER = logging.getLogger(__name__) @@ -15,10 +22,10 @@ def seqToKV(seq, strict=False): key:value pairs. The pairs are generated in the order given. @param seq: The pairs - @type seq: [(str, (unicode|str))] + @type seq: List[Tuple[six.text_type, six.text_type]], binary_type values are deprecated. @return: A string representation of the sequence - @rtype: str + @rtype: six.text_type """ def err(msg): formatted = 'seqToKV warning: %s: %r' % (msg, seq) @@ -29,11 +36,15 @@ def seqToKV(seq, strict=False): lines = [] for k, v in seq: - if isinstance(k, types.StringType): - k = k.decode('UTF8') - elif not isinstance(k, types.UnicodeType): - err('Converting key to string: %r' % k) - k = str(k) + if not isinstance(k, (six.text_type, six.binary_type)): + err('Converting key to text: %r' % k) + k = six.text_type(k) + if not isinstance(v, (six.text_type, six.binary_type)): + err('Converting value to text: %r' % v) + v = six.text_type(v) + + k = string_to_text(k, "Binary values for keys are deprecated. Use text input instead.") + v = string_to_text(v, "Binary values for values are deprecated. Use text input instead.") if '\n' in k: raise KVFormError( @@ -46,12 +57,6 @@ def seqToKV(seq, strict=False): if k.strip() != k: err('Key has whitespace at beginning or end: %r' % (k,)) - if isinstance(v, types.StringType): - v = v.decode('UTF8') - elif not isinstance(v, types.UnicodeType): - err('Converting value to string: %r' % (v,)) - v = str(v) - if '\n' in v: raise KVFormError( 'Invalid input for seqToKV: value contains newline: %r' % (v,)) @@ -61,16 +66,21 @@ def seqToKV(seq, strict=False): lines.append(k + ':' + v + '\n') - return ''.join(lines).encode('UTF8') + return ''.join(lines) def kvToSeq(data, strict=False): """ + Parse newline-terminated key:value pair string into a sequence. After one parse, seqToKV and kvToSeq are inverses, with no warnings:: seq = kvToSeq(s) seqToKV(kvToSeq(seq)) == seq + + @type data: six.text_type, six.binary_type is deprecated + + @rtype: List[Tuple[six.text_type, six.text_type]] """ def err(msg): formatted = 'kvToSeq warning: %s: %r' % (msg, data) @@ -79,6 +89,8 @@ def kvToSeq(data, strict=False): else: _LOGGER.warn(formatted) + data = string_to_text(data, "Binary values for data are deprecated. Use text input instead.") + lines = data.split('\n') if lines[-1]: err('Does not end in a newline') @@ -112,7 +124,7 @@ def kvToSeq(data, strict=False): 'whitespace in value %r') err(fmt % (line_num, v)) - pairs.append((k_s.decode('UTF8'), v_s.decode('UTF8'))) + pairs.append((k_s, v_s)) else: err('Line %d does not contain a colon' % line_num) diff --git a/openid/server/server.py b/openid/server/server.py index dfe4444..4576c82 100644 --- a/openid/server/server.py +++ b/openid/server/server.py @@ -1068,7 +1068,7 @@ class OpenIDResponse(object): @see: OpenID Specs, U{Key-Value Colon/Newline format<http://openid.net/specs.bml#keyvalue>} - @returntype: str + @returntype: six.text_type """ return self.fields.toKVForm() diff --git a/openid/test/test_kvform.py b/openid/test/test_kvform.py index 187629a..5ea6822 100644 --- a/openid/test/test_kvform.py +++ b/openid/test/test_kvform.py @@ -1,5 +1,9 @@ +"""Tests for `openid.kvform` module.""" +from __future__ import unicode_literals + import unittest +import six from testfixtures import LogCapture from openid import kvform @@ -33,10 +37,6 @@ class KVSeqTest(unittest.TestCase): and end of each value of each pair""" clean = [] for k, v in seq: - if isinstance(k, str): - k = k.decode('utf8') - if isinstance(v, str): - v = v.decode('utf8') clean.append((k.strip(), v.strip())) return clean @@ -46,7 +46,7 @@ class KVSeqTest(unittest.TestCase): with LogCapture() as logbook: actual = kvform.seqToKV(kv_data) self.assertEqual(actual, result) - self.assertIsInstance(actual, str) + self.assertIsInstance(actual, six.text_type) # Parse back to sequence. Expected to be unchanged, except # stripping whitespace from start and end of values @@ -92,12 +92,12 @@ kvdict_cases = [ kvseq_cases = [ ([], '', 0), - # Make sure that we handle non-ascii characters (also wider than 8 bits) - ([(u'\u03bbx', u'x')], '\xce\xbbx:x\n', 0), + # Make sure that we handle unicode characters + ([('\u03bbx', 'x')], '\u03bbx:x\n', 0), # If it's a UTF-8 str, make sure that it's equivalent to the same # string, decoded. - ([('\xce\xbbx', 'x')], '\xce\xbbx:x\n', 0), + ([(b'\xce\xbbx', b'x')], '\u03bbx:x\n', 0), ([('openid', 'useful'), ('a', 'b')], 'openid:useful\na:b\n', 0), @@ -113,7 +113,7 @@ kvseq_cases = [ ([(' open id ', ' use ful '), (' a ', ' b ')], ' open id : use ful \n a : b \n', 4), - ([(u'foo', 'bar')], 'foo:bar\n', 0), + ([('foo', 'bar')], 'foo:bar\n', 0), ] kvexc_cases = [ |