Fix #328 - handle unicode __repr__ on Python 2.x
diff --git a/mock/mock.py b/mock/mock.py
index 1db1b4f..d2c469b 100644
--- a/mock/mock.py
+++ b/mock/mock.py
@@ -2059,8 +2059,15 @@
message = '%s(%%s)' % name
formatted_args = ''
args_string = ', '.join([repr(arg) for arg in args])
+
+ def encode_item(item):
+ if six.PY2 and isinstance(item, unicode):
+ return item.encode("utf-8")
+ else:
+ return item
+
kwargs_string = ', '.join([
- '%s=%r' % (key, value) for key, value in sorted(kwargs.items())
+ '%s=%r' % (encode_item(key), value) for key, value in sorted(kwargs.items())
])
if args_string:
formatted_args = args_string
diff --git a/mock/tests/testhelpers.py b/mock/tests/testhelpers.py
index d96dbf4..a87df1b 100644
--- a/mock/tests/testhelpers.py
+++ b/mock/tests/testhelpers.py
@@ -928,6 +928,20 @@
self.assertEqual(str(mock.mock_calls), expected)
+ @unittest.skipIf(six.PY3, "Unicode is properly handled with Python 3")
+ def test_call_list_unicode(self):
+ # See github issue #328
+ mock = Mock()
+
+ class NonAsciiRepr(object):
+ def __repr__(self):
+ return "\xe9"
+
+ mock(**{unicode("a"): NonAsciiRepr()})
+
+ self.assertEqual(str(mock.mock_calls), "[call(a=\xe9)]")
+
+
def test_propertymock(self):
p = patch('%s.SomeClass.one' % __name__, new_callable=PropertyMock)
mock = p.start()