Skip to content

Commit 850e516

Browse files
committed
Change range_repr() to use %R for the start/stop/step attributes.
1 parent 7569dfe commit 850e516

1 file changed

Lines changed: 6 additions & 30 deletions

File tree

Objects/rangeobject.c

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -234,52 +234,28 @@ range_item(rangeobject *r, Py_ssize_t i)
234234
static PyObject *
235235
range_repr(rangeobject *r)
236236
{
237-
PyObject *start_str = NULL, *stop_str = NULL, *step_str = NULL;
238-
PyObject *result = NULL;
239237
Py_ssize_t istart, istep;
240238

241-
/* We always need the stop value. */
242-
stop_str = PyObject_Str(r->stop);
243-
if (!stop_str)
244-
return NULL;
245-
246-
/* XXX(nnorwitz): should we use PyObject_Repr instead of str? */
247-
248239
/* Check for special case values for printing. We don't always
249240
need the start or step values. We don't care about errors
250241
(it means overflow), so clear the errors. */
251242
istart = PyNumber_AsSsize_t(r->start, NULL);
252243
if (istart != 0 || (istart == -1 && PyErr_Occurred())) {
253244
PyErr_Clear();
254-
start_str = PyObject_Str(r->start);
255245
}
256246

257247
istep = PyNumber_AsSsize_t(r->step, NULL);
258248
if (istep != 1 || (istep == -1 && PyErr_Occurred())) {
259249
PyErr_Clear();
260-
step_str = PyObject_Str(r->step);
261250
}
262251

263252
if (istart == 0 && istep == 1)
264-
result = PyUnicode_FromFormat("range(%s)",
265-
PyString_AS_STRING(stop_str));
266-
else if (istep == 1) {
267-
if (start_str)
268-
result = PyUnicode_FromFormat("range(%s, %s)",
269-
PyString_AS_STRING(start_str),
270-
PyString_AS_STRING(stop_str));
271-
}
272-
else if (start_str && step_str)
273-
result = PyUnicode_FromFormat("range(%s, %s, %s)",
274-
PyString_AS_STRING(start_str),
275-
PyString_AS_STRING(stop_str),
276-
PyString_AS_STRING(step_str));
277-
/* else result is NULL and an error should already be set. */
278-
279-
Py_XDECREF(start_str);
280-
Py_XDECREF(stop_str);
281-
Py_XDECREF(step_str);
282-
return result;
253+
return PyUnicode_FromFormat("range(%R)", r->stop);
254+
else if (istep == 1)
255+
return PyUnicode_FromFormat("range(%R, %R)", r->start, r->stop);
256+
else
257+
return PyUnicode_FromFormat("range(%R, %R, %R)",
258+
r->start, r->stop, r->step);
283259
}
284260

285261
static PySequenceMethods range_as_sequence = {

0 commit comments

Comments
 (0)