Always use native strings as keys.

This commit is contained in:
Itamar Turner-Trauring 2020-11-17 13:15:57 -05:00
parent dd5092f656
commit feb85f4c4a
2 changed files with 17 additions and 1 deletions

View File

@ -9,7 +9,7 @@ from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from future.utils import PY2
from future.utils import PY2, native_str
if PY2:
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
@ -154,3 +154,17 @@ class Log(unittest.TestCase):
obj.log("four")
self.assertEqual([m[2] for m in self.messages],
["grand", "par1", "par2", "msg1", "msg1"])
def test_native_string_keys(self):
"""Keyword argument keys are all native strings."""
class LoggingObject17(tahoe_log.PrefixingLogMixin):
pass
obj = LoggingObject17()
# Native string by default:
obj.log(hello="world")
# Will be Unicode on Python 2:
obj.log(**{"my": "message"})
for message in self.messages:
for k in message[-1].keys():
self.assertIsInstance(k, native_str)

View File

@ -11,6 +11,7 @@ from __future__ import unicode_literals
from future.utils import PY2
if PY2:
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
from six import ensure_str
from pyutil import nummedobj
@ -55,6 +56,7 @@ class LogMixin(object):
pmsgid = self._parentmsgid
if pmsgid is None:
pmsgid = self._grandparentmsgid
kwargs = {ensure_str(k): v for (k, v) in kwargs.items()}
msgid = log.msg(msg, facility=facility, parent=pmsgid, *args, **kwargs)
if self._parentmsgid is None:
self._parentmsgid = msgid