summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_operator.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2015-08-17 02:43:34 (GMT)
committerRaymond Hettinger <python@rcn.com>2015-08-17 02:43:34 (GMT)
commit47d159f5220918f9c1216ba93583f3aa740a8944 (patch)
tree1e324fa8f68399c96643d09e0168b45f9be2a654 /Lib/test/test_operator.py
parent755cb0ae8d02daa7f231da90c303ebf9a0f3b01f (diff)
downloadcpython-47d159f5220918f9c1216ba93583f3aa740a8944.zip
cpython-47d159f5220918f9c1216ba93583f3aa740a8944.tar.gz
cpython-47d159f5220918f9c1216ba93583f3aa740a8944.tar.bz2
Issue #24379: Add operator.subscript() as a convenience for building slices.
Diffstat (limited to 'Lib/test/test_operator.py')
-rw-r--r--Lib/test/test_operator.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py
index 54fd1f4..27501c2 100644
--- a/Lib/test/test_operator.py
+++ b/Lib/test/test_operator.py
@@ -596,5 +596,38 @@ class CCOperatorPickleTestCase(OperatorPickleTestCase, unittest.TestCase):
module2 = c_operator
+class SubscriptTestCase:
+ def test_subscript(self):
+ subscript = self.module.subscript
+ self.assertIsNone(subscript[None])
+ self.assertEqual(subscript[0], 0)
+ self.assertEqual(subscript[0:1:2], slice(0, 1, 2))
+ self.assertEqual(
+ subscript[0, ..., :2, ...],
+ (0, Ellipsis, slice(2), Ellipsis),
+ )
+
+ def test_pickle(self):
+ from operator import subscript
+ for proto in range(pickle.HIGHEST_PROTOCOL + 1):
+ with self.subTest(proto=proto):
+ self.assertIs(
+ pickle.loads(pickle.dumps(subscript, proto)),
+ subscript,
+ )
+
+ def test_singleton(self):
+ with self.assertRaises(TypeError):
+ type(self.module.subscript)()
+
+ def test_immutable(self):
+ with self.assertRaises(AttributeError):
+ self.module.subscript.attr = None
+
+
+class PySubscriptTestCase(SubscriptTestCase, PyOperatorTestCase):
+ pass
+
+
if __name__ == "__main__":
unittest.main()