diff options
author | Oren Milman <orenmn@gmail.com> | 2017-10-08 08:17:46 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-10-08 08:17:46 (GMT) |
commit | 0ccc0f6c7495be9043300e22d8f38e6d65e8884f (patch) | |
tree | 0e93d02086fb4997860e82ca68969a5cfbe24a2f /Lib/test/test_capi.py | |
parent | f07e2b64df6304a36fb5e29397d3c77a7ba17704 (diff) | |
download | cpython-0ccc0f6c7495be9043300e22d8f38e6d65e8884f.zip cpython-0ccc0f6c7495be9043300e22d8f38e6d65e8884f.tar.gz cpython-0ccc0f6c7495be9043300e22d8f38e6d65e8884f.tar.bz2 |
bpo-28280: Make PyMapping_Keys(), PyMapping_Values() and PyMapping_Items() always return a list (#3840)
Diffstat (limited to 'Lib/test/test_capi.py')
-rw-r--r-- | Lib/test/test_capi.py | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py index 1b826ee..921735e 100644 --- a/Lib/test/test_capi.py +++ b/Lib/test/test_capi.py @@ -1,7 +1,7 @@ # Run the _testcapi module tests (tests for the Python/C API): by defn, # these are all functions _testcapi exports whose name begins with 'test_'. -from collections import namedtuple +from collections import namedtuple, OrderedDict import os import pickle import platform @@ -272,6 +272,50 @@ class CAPITest(unittest.TestCase): self.assertIn(b'MemoryError 2 20', out) self.assertIn(b'MemoryError 3 30', out) + def test_mapping_keys_values_items(self): + class Mapping1(dict): + def keys(self): + return list(super().keys()) + def values(self): + return list(super().values()) + def items(self): + return list(super().items()) + class Mapping2(dict): + def keys(self): + return tuple(super().keys()) + def values(self): + return tuple(super().values()) + def items(self): + return tuple(super().items()) + dict_obj = {'foo': 1, 'bar': 2, 'spam': 3} + + for mapping in [{}, OrderedDict(), Mapping1(), Mapping2(), + dict_obj, OrderedDict(dict_obj), + Mapping1(dict_obj), Mapping2(dict_obj)]: + self.assertListEqual(_testcapi.get_mapping_keys(mapping), + list(mapping.keys())) + self.assertListEqual(_testcapi.get_mapping_values(mapping), + list(mapping.values())) + self.assertListEqual(_testcapi.get_mapping_items(mapping), + list(mapping.items())) + + def test_mapping_keys_values_items_bad_arg(self): + self.assertRaises(AttributeError, _testcapi.get_mapping_keys, None) + self.assertRaises(AttributeError, _testcapi.get_mapping_values, None) + self.assertRaises(AttributeError, _testcapi.get_mapping_items, None) + + class BadMapping: + def keys(self): + return None + def values(self): + return None + def items(self): + return None + bad_mapping = BadMapping() + self.assertRaises(TypeError, _testcapi.get_mapping_keys, bad_mapping) + self.assertRaises(TypeError, _testcapi.get_mapping_values, bad_mapping) + self.assertRaises(TypeError, _testcapi.get_mapping_items, bad_mapping) + class TestPendingCalls(unittest.TestCase): |