bpo-40792: Make the result of PyNumber_Index() always having exact type int. (GH-20443)
Previously, the result could have been an instance of a subclass of int.
Also revert bpo-26202 and make attributes start, stop and step of the range
object having exact type int.
Add private function _PyNumber_Index() which preserves the old behavior
of PyNumber_Index() for performance to use it in the conversion functions
like PyLong_AsLong().
diff --git a/Lib/test/clinic.test b/Lib/test/clinic.test
index 5e6f129..f2be613 100644
--- a/Lib/test/clinic.test
+++ b/Lib/test/clinic.test
@@ -1332,7 +1332,7 @@
}
{
Py_ssize_t ival = -1;
- PyObject *iobj = PyNumber_Index(args[0]);
+ PyObject *iobj = _PyNumber_Index(args[0]);
if (iobj != NULL) {
ival = PyLong_AsSsize_t(iobj);
Py_DECREF(iobj);
@@ -1347,7 +1347,7 @@
}
{
Py_ssize_t ival = -1;
- PyObject *iobj = PyNumber_Index(args[1]);
+ PyObject *iobj = _PyNumber_Index(args[1]);
if (iobj != NULL) {
ival = PyLong_AsSsize_t(iobj);
Py_DECREF(iobj);
@@ -1373,7 +1373,7 @@
static PyObject *
test_Py_ssize_t_converter_impl(PyObject *module, Py_ssize_t a, Py_ssize_t b,
Py_ssize_t c)
-/*[clinic end generated code: output=ea781bb7169b3436 input=3855f184bb3f299d]*/
+/*[clinic end generated code: output=3bf73f9fdfeab468 input=3855f184bb3f299d]*/
/*[clinic input]
diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py
index 35f72fb..ba3d233 100644
--- a/Lib/test/test_copy.py
+++ b/Lib/test/test_copy.py
@@ -357,7 +357,7 @@
pass
tests = [None, 42, 2**100, 3.14, True, False, 1j,
"hello", "hello\u1234", f.__code__,
- NewStyle, Classic, max, property()]
+ NewStyle, range(10), Classic, max, property()]
for x in tests:
self.assertIs(copy.deepcopy(x), x)
@@ -579,17 +579,6 @@
self.assertIsNot(y, x)
self.assertIs(y.foo, y)
- def test_deepcopy_range(self):
- class I(int):
- pass
- x = range(I(10))
- y = copy.deepcopy(x)
- self.assertIsNot(y, x)
- self.assertEqual(y, x)
- self.assertIsNot(y.stop, x.stop)
- self.assertEqual(y.stop, x.stop)
- self.assertIsInstance(y.stop, I)
-
# _reconstruct()
def test_reconstruct_string(self):
diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py
index 30fa129..107c0e2 100644
--- a/Lib/test/test_range.py
+++ b/Lib/test/test_range.py
@@ -648,11 +648,17 @@
self.assert_attrs(range(0, 10, 3), 0, 10, 3)
self.assert_attrs(range(10, 0, -1), 10, 0, -1)
self.assert_attrs(range(10, 0, -3), 10, 0, -3)
+ self.assert_attrs(range(True), 0, 1, 1)
+ self.assert_attrs(range(False, True), 0, 1, 1)
+ self.assert_attrs(range(False, True, True), 0, 1, 1)
def assert_attrs(self, rangeobj, start, stop, step):
self.assertEqual(rangeobj.start, start)
self.assertEqual(rangeobj.stop, stop)
self.assertEqual(rangeobj.step, step)
+ self.assertIs(type(rangeobj.start), int)
+ self.assertIs(type(rangeobj.stop), int)
+ self.assertIs(type(rangeobj.step), int)
with self.assertRaises(AttributeError):
rangeobj.start = 0