Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-39200: Correct the error message for range() empty constructor #17813

Open
wants to merge 2 commits into
base: master
from

Conversation

@pablogsal
Copy link
Member

pablogsal commented Jan 3, 2020

@pablogsal pablogsal force-pushed the pablogsal:bpo-39200 branch from 3b628b2 to 7a5723d Jan 3, 2020
Copy link
Member

corona10 left a comment

For me, this is more correct :)

Copy link
Member

serhiy-storchaka left a comment

Would be nice to add tests.

@@ -78,8 +78,12 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw)
return NULL;

if (PyTuple_Size(args) <= 1) {
if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop))
if (!PyArg_UnpackTuple(args, "range", 1, 1, &stop)) {

This comment has been minimized.

Copy link
@serhiy-storchaka

serhiy-storchaka Jan 3, 2020

Member

I think it is better to get rid of PyArg_UnpackTuple(). Its only benefit is checking the size of the tuple and formatting an error message which is wrong. Note also that the second PyArg_UnpackTuple() also can produce incorrect error message.

switch (PyTuple_GET_SIZE(args)) {
case 3:
    step = PyTuple_GET_ITEM(args, 2);
    /* fall through */
case 2:
    start = PyTuple_GET_ITEM(args, 0);
    stop = PyTuple_GET_ITEM(args, 1);
    ...
    break;
case 1:
    stop = PyTuple_GET_ITEM(args, 0);
    ...
    break;
case 0:
    // error
    return NULL;
default:
    // error
    return NULL;
}

This comment has been minimized.

Copy link
@pablogsal

pablogsal Jan 3, 2020

Author Member

Thanks for the suggestion, @serhiy-storchaka!

@serhiy-storchaka

This comment has been minimized.

Copy link
Member

serhiy-storchaka commented Jan 3, 2020

Other option - just use PyArg_UnpackTuple(args, "range", 1, 3, &start, &stop, &step) and check if stop == NULL.

@pablogsal

This comment has been minimized.

Copy link
Member Author

pablogsal commented Jan 3, 2020

Other option - just use PyArg_UnpackTuple(args, "range", 1, 3, &start, &stop, &step) and check if stop == NULL.

I have implemented the switch statement as we would need to do the dance with step and start anyway and I find the switch statement a bit more clear.

@pablogsal

This comment has been minimized.

Copy link
Member Author

pablogsal commented Jan 3, 2020

Would be nice to add tests.

Done in eb9e784

@pablogsal pablogsal force-pushed the pablogsal:bpo-39200 branch from 146591c to e73eab4 Jan 3, 2020
@pablogsal pablogsal force-pushed the pablogsal:bpo-39200 branch from e73eab4 to eb9e784 Jan 3, 2020
@pablogsal

This comment has been minimized.

Copy link
Member Author

pablogsal commented Jan 3, 2020

Btw, as a bonus the constructor of range is anecdotally faster:

master

./python.exe -m pyperf timeit "range(1,3,4)"
.....................
Mean +- std dev: 893 ns +- 44 ns

patched

./python.exe -m pyperf timeit "range(1,3,4)"
.....................
Mean +- std dev: 822 ns +- 14 ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.