diff -r 6d5d2bda6acb Lib/operator.py --- a/Lib/operator.py Wed Jun 03 11:11:22 2015 -0600 +++ b/Lib/operator.py Tue Jun 23 18:08:19 2015 -0400 @@ -408,6 +408,20 @@ return a +class subscript(object): + def __new__(cls): + raise TypeError("cannot create '{}' instances".format(cls.__name__)) + + def __getitem__(self, key): + return key + + def __reduce__(self): + return 'subscript' + + +subscript = object.__new__(subscript) + + try: from _operator import * except ImportError: diff -r 6d5d2bda6acb Lib/test/test_operator.py --- a/Lib/test/test_operator.py Wed Jun 03 11:11:22 2015 -0600 +++ b/Lib/test/test_operator.py Tue Jun 23 18:08:19 2015 -0400 @@ -596,5 +596,35 @@ module2 = c_operator +class SubscriptTestCase(object): + def test_literal_syntax(self): + pairs = ( + (slice(1, 1, 1), self.module.subscript[1:1:1]), + (slice(1, 1, None), self.module.subscript[1:1]), + (slice(1, 1, None), self.module.subscript[1:1:]), + (slice(1, None, 1), self.module.subscript[1::1]), + (slice(1, None, None), self.module.subscript[1:]), + (slice(1, None, None), self.module.subscript[1::]), + (slice(None, 1, 1), self.module.subscript[:1:1]), + (slice(None, 1, None), self.module.subscript[:1]), + (slice(None, 1, None), self.module.subscript[:1:]), + (slice(None, None, None), self.module.subscript[:]), + (slice(None, None, None), self.module.subscript[::]), + ) + for call, literal in pairs: + self.assertEqual(call, literal) + + def test_singleton(self): + with self.assertRaises(TypeError): + type(self.module.subscript)() + + +class PySubscriptTestCase(SubscriptTestCase, PyOperatorTestCase): + pass + + +class CSubscriptTestCase(SubscriptTestCase, COperatorTestCase): + pass + if __name__ == "__main__": unittest.main()