issue #477: Py2.4 cannot tolerate unicode kwargs.

issue510
David Wilson 6 years ago
parent 08cecb92f6
commit 0ee8ee78b8

@ -236,10 +236,15 @@ class Secret(UnicodeType):
class Kwargs(dict): class Kwargs(dict):
"""A serializable dict subclass that indicates the contained keys should be """
be coerced to Unicode on Python 3 as required. Python 2 produces keyword A serializable dict subclass that indicates the contained keys should be be
argument dicts whose keys are bytestrings, requiring a helper to ensure coerced to Unicode on Python 3 and to bytes on Python<2.6.
compatibility with Python 3."""
Python 2 produces keyword argument dicts whose keys are bytestrings,
requiring a helper to ensure compatibility with Python 3, whereas Python 3
produces keyword argument dicts whose keys are unicode, requiring a helper
to ensure compatibility with Python 2.4 and 2.5.
"""
if PY3: if PY3:
def __init__(self, dct): def __init__(self, dct):
for k, v in dct.items(): for k, v in dct.items():
@ -247,6 +252,13 @@ class Kwargs(dict):
self[k.decode()] = v self[k.decode()] = v
else: else:
self[k] = v self[k] = v
elif sys.version_info < (2, 6):
def __init__(self, dct):
for k, v in dct.iteritems():
if type(k) is unicode:
self[k.encode()] = v
else:
self[k] = v
def __repr__(self): def __repr__(self):
return 'Kwargs(%s)' % (dict.__repr__(self),) return 'Kwargs(%s)' % (dict.__repr__(self),)

@ -1935,8 +1935,10 @@ class Router(mitogen.core.Router):
via = kwargs.pop(u'via', None) via = kwargs.pop(u'via', None)
if via is not None: if via is not None:
return self.proxy_connect(via, method_name, name=name, **kwargs) return self.proxy_connect(via, method_name, name=name,
return self._connect(klass, name=name, **kwargs) **mitogen.core.Kwargs(kwargs))
return self._connect(klass, name=name,
**mitogen.core.Kwargs(kwargs))
def proxy_connect(self, via_context, method_name, name=None, **kwargs): def proxy_connect(self, via_context, method_name, name=None, **kwargs):
resp = via_context.call(_proxy_connect, resp = via_context.call(_proxy_connect,

@ -1,4 +1,6 @@
import sys
try: try:
from io import StringIO from io import StringIO
from io import BytesIO from io import BytesIO
@ -88,7 +90,14 @@ class KwargsTest(testlib.TestCase):
self.assertTrue(type(dct) is dict) self.assertTrue(type(dct) is dict)
self.assertEquals({}, dct) self.assertEquals({}, dct)
@unittest2.skipIf(condition=lambda: not mitogen.core.PY3, @unittest2.skipIf(condition=(sys.version_info >= (2, 6)),
reason='py<2.6 only')
def test_bytes_conversion(self):
kw = self.klass({u'key': 123})
self.assertEquals({'key': 123}, kw)
self.assertEquals("Kwargs({'key': 123})", repr(kw))
@unittest2.skipIf(condition=not mitogen.core.PY3,
reason='py3 only') reason='py3 only')
def test_unicode_conversion(self): def test_unicode_conversion(self):
kw = self.klass({mitogen.core.b('key'): 123}) kw = self.klass({mitogen.core.b('key'): 123})

Loading…
Cancel
Save