@@ -216,6 +216,12 @@ static long compute_range_length_long(PyObject *start,
216216 }
217217}
218218
219+ static void print_str (PyObject * o )
220+ {
221+ PyObject_Print (o , stdout , Py_PRINT_RAW );
222+ }
223+
224+
219225/* Return number of items in range (lo, hi, step) as a PyLong object,
220226 * when arguments are PyLong objects. Arguments MUST return 1 with
221227 * PyLong_Check(). Return NULL when there is an error.
@@ -240,6 +246,17 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
240246 assert (PyLong_Check (stop ));
241247 assert (PyLong_Check (step ));
242248
249+ /* fast path for one argument case of range */
250+ if (start == zero && step == one ) {
251+ if (_PyLong_IsPositive ( (const PyLongObject * )stop ) ) {
252+ Py_INCREF (stop );
253+ return stop ;
254+ }
255+ else {
256+ return zero ;
257+ }
258+ }
259+
243260 /* fast path when all arguments fit into a long integer */
244261 long len = compute_range_length_long (start , stop , step );
245262 if (len >= 0 ) {
@@ -293,6 +310,7 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
293310 Py_DECREF (diff );
294311 Py_DECREF (step );
295312 Py_DECREF (tmp1 );
313+
296314 return result ;
297315
298316 Fail :
@@ -1135,13 +1153,19 @@ range_iter(PyObject *seq)
11351153
11361154 /* If all three fields and the length convert to long, use the int
11371155 * version */
1138- lstart = PyLong_AsLong (r -> start );
1139- if (lstart == -1 && PyErr_Occurred ()) {
1156+ lstop = PyLong_AsLong (r -> stop );
1157+ if (lstop == -1 && PyErr_Occurred ()) {
11401158 PyErr_Clear ();
11411159 goto long_range ;
11421160 }
1143- lstop = PyLong_AsLong (r -> stop );
1144- if (lstop == -1 && PyErr_Occurred ()) {
1161+
1162+ if (r -> start == (PyLongObject * )_PyLong_GetZero () && r -> step == _PyLong_GetOne () ) {
1163+ /* fast path for one argument range */
1164+ return fast_range_iter (0 , lstop , 1 , Py_MAX (0 , (long )lstop ));
1165+ }
1166+
1167+ lstart = PyLong_AsLong (r -> start );
1168+ if (lstart == -1 && PyErr_Occurred ()) {
11451169 PyErr_Clear ();
11461170 goto long_range ;
11471171 }
0 commit comments