Skip to content

Commit bfce988

Browse files
author
Josh Hoyt
committed
[project @ Silence warnings when converting unicode to string in kvform]
1 parent 5478936 commit bfce988

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

‎openid/kvform.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def err(msg):
2323

2424
lines = []
2525
for k, v in seq:
26-
if not isinstance(k, types.StringType):
26+
if isinstance(k, types.UnicodeType):
27+
k = k.encode('UTF8')
28+
elif not isinstance(k, types.StringType):
2729
err('Converting key to string: %r' % k)
2830
k = str(k)
2931

@@ -40,7 +42,7 @@ def err(msg):
4042

4143
if isinstance(v, types.UnicodeType):
4244
v = v.encode('UTF8')
43-
if not isinstance(v, types.StringType):
45+
elif not isinstance(v, types.StringType):
4446
err('Converting value to string: %r' % v)
4547
v = str(v)
4648

‎openid/test/kvform.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class KVBaseTest(unittest.TestCase):
66
def shortDescription(self):
77
return '%s test for %r' % (self.__class__.__name__, self.kvform)
88

9-
def log(self, unused_message, unused_priority=None):
10-
self.warnings += 1
9+
def log(self, message, unused_priority=None):
10+
self.warnings.append(message)
1111

1212
def checkWarnings(self, num_warnings):
13-
self.failUnlessEqual(num_warnings, self.warnings)
13+
self.failUnlessEqual(num_warnings, len(self.warnings), repr(self.warnings))
1414

1515
def setUp(self):
16-
self.warnings = 0
16+
self.warnings = []
1717
self.old_log = oidutil.log
1818
self.log_func = oidutil.log = self.log
1919
self.failUnless(self.log_func is oidutil.log,
@@ -46,10 +46,11 @@ def runTest(self):
4646
self.failUnlessEqual(d, d2)
4747

4848
class KVSeqTest(KVBaseTest):
49-
def __init__(self, seq, kv):
49+
def __init__(self, seq, kv, expected_warnings):
5050
unittest.TestCase.__init__(self)
5151
self.kvform = kv
5252
self.seq = seq
53+
self.expected_warnings = expected_warnings
5354

5455
def cleanSeq(self, seq):
5556
"""Create a new sequence by stripping whitespace from start
@@ -71,6 +72,7 @@ def runTest(self):
7172
clean_seq = self.cleanSeq(seq)
7273

7374
self.failUnlessEqual(seq, clean_seq)
75+
self.checkWarnings(self.expected_warnings)
7476

7577
kvdict_cases = [
7678
# (kvform, parsed dictionary, expected warnings)
@@ -105,13 +107,14 @@ def runTest(self):
105107
]
106108

107109
kvseq_cases = [
108-
([], ''),
109-
([('openid', 'useful'), ('a', 'b')], 'openid:useful\na:b\n'),
110-
([(' openid', 'useful'), ('a', 'b')], ' openid:useful\na:b\n'),
110+
([], '', 0),
111+
([('openid', 'useful'), ('a', 'b')], 'openid:useful\na:b\n', 0),
112+
([(' openid', 'useful'), ('a', 'b')], ' openid:useful\na:b\n', 2),
111113
([(' openid ', ' useful '),
112-
(' a ', ' b ')], ' openid : useful \n a : b \n'),
114+
(' a ', ' b ')], ' openid : useful \n a : b \n', 8),
113115
([(' open id ', ' use ful '),
114-
(' a ', ' b ')], ' open id : use ful \n a : b \n'),
116+
(' a ', ' b ')], ' open id : use ful \n a : b \n', 8),
117+
([(u'foo', 'bar')], 'foo:bar\n', 0),
115118
]
116119

117120
kvexc_cases = [

0 commit comments

Comments
 (0)