Skip to content

gh-94808: add tests covering PySequence_{Set,Del}Slice #99123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions Lib/test/test_capi.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,17 +437,24 @@ def __setitem__(self, index, value):
self.assertEqual(c.index, slice(0, 5))
self.assertEqual(c.value, 'abc')

# Immutable sequences must raise:
with self.assertRaises(TypeError):
_testcapi.sequence_set_slice((1, 2, 3, 4), 1, 3, (8, 9))
with self.assertRaises(TypeError):
_testcapi.sequence_set_slice('abcd', 1, 3, 'xy')

# Not a sequnce:
with self.assertRaises(TypeError):
_testcapi.sequence_set_slice(object(), 1, 3, 'xy')
with self.assertRaises(TypeError):
_testcapi.sequence_set_slice({1: 'a', 2: 'b', 3: 'c'}, 1, 3, 'xy')
# Wrong cases:
import copy

bad_calls = [
# Immutable sequences must raise:
((1, 2, 3, 4), 1, 3, (8, 9)),
('abcd', 1, 3, 'xy'),
# Not a sequnce:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: sequnce

(None, 1, 3, 'xy'),
({1: 'a', 2: 'b', 3: 'c'}, 1, 3, 'xy'), # is a mapping
]

for seq, i1, i2, obj in bad_calls:
with self.subTest(seq=seq, i1=i1, i2=i2, obj=obj):
seq_copy = copy.copy(seq)
with self.assertRaises(TypeError):
_testcapi.sequence_set_slice(seq, i1, i2, obj)
self.assertEqual(seq, seq_copy) # it must not change

def test_sequence_del_slice(self):
# Correct case:
Expand All @@ -468,17 +475,24 @@ def __delitem__(self, index):
_testcapi.sequence_del_slice(c, 0, 5)
self.assertEqual(c.index, slice(0, 5))

# Immutable sequences must raise:
with self.assertRaises(TypeError):
_testcapi.sequence_del_slice((1, 2, 3, 4), 1, 3)
with self.assertRaises(TypeError):
_testcapi.sequence_del_slice('abcd', 1, 3)

# Not a sequnce:
with self.assertRaises(TypeError):
_testcapi.sequence_del_slice(object(), 1, 3)
with self.assertRaises(TypeError):
_testcapi.sequence_del_slice({1: 'a', 2: 'b', 3: 'c'}, 1, 3)
# Wrong cases:
import copy

bad_calls = [
# Immutable sequences must raise:
((1, 2, 3, 4), 1, 3),
('abcd', 1, 3),
# Not a sequnce:
(None, 1, 3),
({1: 'a', 2: 'b', 3: 'c'}, 1, 3), # is a mapping
]

for seq, i1, i2 in bad_calls:
with self.subTest(seq=seq, i1=i1, i2=i2):
seq_copy = copy.copy(seq)
with self.assertRaises(TypeError):
_testcapi.sequence_del_slice(seq, i1, i2)
self.assertEqual(seq, seq_copy) # it must not change

@unittest.skipUnless(hasattr(_testcapi, 'negative_refcount'),
'need _testcapi.negative_refcount')
Expand Down