2
2
3
3
#include "Python.h"
4
4
#include "structmember.h"
5
+ #include "pycore_tupleobject.h"
5
6
6
7
/* Support objects whose length is > PY_SSIZE_T_MAX.
7
8
@@ -71,43 +72,35 @@ make_range_object(PyTypeObject *type, PyObject *start,
71
72
range(0, 5, -1)
72
73
*/
73
74
static PyObject *
74
- range_new (PyTypeObject * type , PyObject * args , PyObject * kw )
75
+ range_from_array (PyTypeObject * type , PyObject * const * args , Py_ssize_t num_args )
75
76
{
76
77
rangeobject * obj ;
77
78
PyObject * start = NULL , * stop = NULL , * step = NULL ;
78
79
79
- if (!_PyArg_NoKeywords ("range" , kw ))
80
- return NULL ;
81
-
82
- Py_ssize_t num_args = PyTuple_GET_SIZE (args );
83
80
switch (num_args ) {
84
81
case 3 :
85
- step = PyTuple_GET_ITEM ( args , 2 ) ;
82
+ step = args [ 2 ] ;
86
83
/* fallthrough */
87
84
case 2 :
88
- start = PyTuple_GET_ITEM ( args , 0 );
89
- start = PyNumber_Index (start );
85
+ /* Convert borrowed refs to owned refs */
86
+ start = PyNumber_Index (args [ 0 ] );
90
87
if (!start ) {
91
88
return NULL ;
92
89
}
93
-
94
- stop = PyTuple_GET_ITEM (args , 1 );
95
- stop = PyNumber_Index (stop );
90
+ stop = PyNumber_Index (args [1 ]);
96
91
if (!stop ) {
97
92
Py_DECREF (start );
98
93
return NULL ;
99
94
}
100
-
101
- step = validate_step (step );
95
+ step = validate_step (step ); /* Caution, this can clear exceptions */
102
96
if (!step ) {
103
97
Py_DECREF (start );
104
98
Py_DECREF (stop );
105
99
return NULL ;
106
100
}
107
101
break ;
108
102
case 1 :
109
- stop = PyTuple_GET_ITEM (args , 0 );
110
- stop = PyNumber_Index (stop );
103
+ stop = PyNumber_Index (args [0 ]);
111
104
if (!stop ) {
112
105
return NULL ;
113
106
}
@@ -126,10 +119,10 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
126
119
num_args );
127
120
return NULL ;
128
121
}
129
-
130
122
obj = make_range_object (type , start , stop , step );
131
- if (obj != NULL )
123
+ if (obj != NULL ) {
132
124
return (PyObject * ) obj ;
125
+ }
133
126
134
127
/* Failed to create object, release attributes */
135
128
Py_DECREF (start );
@@ -138,6 +131,28 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
138
131
return NULL ;
139
132
}
140
133
134
+ static PyObject *
135
+ range_new (PyTypeObject * type , PyObject * args , PyObject * kw )
136
+ {
137
+ if (!_PyArg_NoKeywords ("range" , kw ))
138
+ return NULL ;
139
+
140
+ return range_from_array (type , _PyTuple_ITEMS (args ), PyTuple_GET_SIZE (args ));
141
+ }
142
+
143
+
144
+ static PyObject *
145
+ range_vectorcall (PyTypeObject * type , PyObject * const * args ,
146
+ size_t nargsf , PyObject * kwnames )
147
+ {
148
+ Py_ssize_t nargs = PyVectorcall_NARGS (nargsf );
149
+ if (kwnames && PyTuple_GET_SIZE (kwnames ) != 0 ) {
150
+ PyErr_Format (PyExc_TypeError , "range() takes no keyword arguments" );
151
+ return NULL ;
152
+ }
153
+ return range_from_array (type , args , nargs );
154
+ }
155
+
141
156
PyDoc_STRVAR (range_doc ,
142
157
"range(stop) -> range object\n\
143
158
range(start, stop[, step]) -> range object\n\
@@ -719,6 +734,7 @@ PyTypeObject PyRange_Type = {
719
734
0 , /* tp_init */
720
735
0 , /* tp_alloc */
721
736
range_new , /* tp_new */
737
+ .tp_vectorcall = (vectorcallfunc )range_vectorcall
722
738
};
723
739
724
740
/*********************** range Iterator **************************/
0 commit comments