diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index 8821139eb3d..78561b76fe6 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -72,9 +72,18 @@ class DictSetTest(unittest.TestCase): def test_dict_repr(self): d = {1: 10, "a": "ABC"} self.assertTrue(isinstance(repr(d), str)) - self.assertTrue(isinstance(repr(d.viewitems()), str)) - self.assertTrue(isinstance(repr(d.viewkeys()), str)) - self.assertTrue(isinstance(repr(d.viewvalues()), str)) + r = repr(d.viewitems()) + self.assertTrue(isinstance(r, str)) + self.assertTrue(r == "dict_items([('a', 'ABC'), (1, 10)])" or + r == "dict_items([(1, 10), ('a', 'ABC')])") + r = repr(d.viewkeys()) + self.assertTrue(isinstance(r, str)) + self.assertTrue(r == "dict_keys(['a', 1])" or + r == "dict_keys([1, 'a'])") + r = repr(d.viewvalues()) + self.assertTrue(isinstance(r, str)) + self.assertTrue(r == "dict_values(['ABC', 10])" or + r == "dict_values([10, 'ABC'])") def test_main(): diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 9f46dfafef4..b20d6f36e9a 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -2886,7 +2886,8 @@ dictview_repr(dictviewobject *dv) return NULL; seq_str = PyObject_Repr(seq); - result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name, seq_str); + result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name, + PyString_AS_STRING(seq_str)); Py_DECREF(seq_str); Py_DECREF(seq); return result;