From 0ee8ee78b8cec429ef17f802bef4fb66ee4874df Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 23 Jan 2019 12:44:08 +0000 Subject: [PATCH] issue #477: Py2.4 cannot tolerate unicode kwargs. --- mitogen/core.py | 20 ++++++++++++++++---- mitogen/parent.py | 6 ++++-- tests/types_test.py | 11 ++++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index 4f5e15be..7273f3d0 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -236,10 +236,15 @@ class Secret(UnicodeType): 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 - argument dicts whose keys are bytestrings, requiring a helper to ensure - compatibility with Python 3.""" + """ + A serializable dict subclass that indicates the contained keys should be be + coerced to Unicode on Python 3 and to bytes on Python<2.6. + + 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: def __init__(self, dct): for k, v in dct.items(): @@ -247,6 +252,13 @@ class Kwargs(dict): self[k.decode()] = v else: 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): return 'Kwargs(%s)' % (dict.__repr__(self),) diff --git a/mitogen/parent.py b/mitogen/parent.py index 6e902bdd..6305aca8 100644 --- a/mitogen/parent.py +++ b/mitogen/parent.py @@ -1935,8 +1935,10 @@ class Router(mitogen.core.Router): via = kwargs.pop(u'via', None) if via is not None: - return self.proxy_connect(via, method_name, name=name, **kwargs) - return self._connect(klass, name=name, **kwargs) + return self.proxy_connect(via, method_name, name=name, + **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): resp = via_context.call(_proxy_connect, diff --git a/tests/types_test.py b/tests/types_test.py index bd243949..9166f577 100644 --- a/tests/types_test.py +++ b/tests/types_test.py @@ -1,4 +1,6 @@ +import sys + try: from io import StringIO from io import BytesIO @@ -88,7 +90,14 @@ class KwargsTest(testlib.TestCase): self.assertTrue(type(dct) is dict) 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') def test_unicode_conversion(self): kw = self.klass({mitogen.core.b('key'): 123})