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-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem() #11112

Merged
merged 6 commits into from
Feb 25, 2019

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Dec 11, 2018

Objects/dictobject.c Outdated Show resolved Hide resolved
Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, thanks for working on this! :) Overall it looks good.

My main concern with this PR is changing semantics. From what I can tell you're introducing a bunch of changes in behavior, albeit it corner error cases. What is the risk to compatibility? My gut tells me there's at least a slight risk.

Secondly, you've touched a lot of critical code. Please make sure to run the benchmark suite to ensure the PR doesn't slow down Python. :)

Also, there a number of places where I wanted to suggest a better spelling. However, such changes would be slightly riskier and would mostly clutter up the PR, obscuring the core changes. So I've left out those comments and focused mostly on checking correctness.

Finally, the most likely thing I might have missed in this review is refcounts. You've added quite a few places that exit early when there's an error. I'm not sure that I checked to make sure everything was properly decref'ed in those new error cases.

Modules/_csv.c Show resolved Hide resolved
Modules/_sre.c Outdated
PyExc_IndexError,
"no such group"
);
if (index < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change intentional? At first glance it looks like the code you've removed actually matters.

Is this function only ever called with a pre-validated index (e.g. the one returned from match_getindex())? If so, it would be helpful to have a comment here indicating that validation of the index must be done by the caller. And if that's the case then why have this check (and short-circuit) here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed code was moved into match_getindex() because it was repeated after every call of match_getindex(). This function is only called with valid index or the result of match_getindex(). This check was here just to make the caller place simpler. Will move it to the caller place and add an assert instead.

Modules/pyexpat.c Show resolved Hide resolved
@@ -1692,7 +1700,7 @@ MODULE_INITFUNC(void)
}
}
Py_DECREF(errmod_name);
model_module = PyDict_GetItem(d, modelmod_name);
model_module = PyDict_GetItemWithError(d, modelmod_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the error need to be returned or cleared?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is returned below, at line 1713.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model_module gets reset on line 1705, so the error from the first attempt may remain uncleared or swallowed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 1705 is executed only no error was raised at this line.

Error handling in this function (as well as in many other module initialization functions) is pretty poor. Results of PyModule_AddObject() and derived functions are not checked, and references are leaked in case of error. But this is different issue(s).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then shouldn't line 1704 have && !PyErr_Occurred() like line 1694 does?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already added.

int status = PyDict_SetItem(d, key, value);
if (status < 0) {
if (override || PyDict_GetItemWithError(d, key) == NULL) {
if ((!override && PyErr_Occurred()) || PyDict_SetItem(d, key, value) < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little hard to read. There's a lot going on in these two lines. Perhaps split it up a little?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -4797,14 +4824,12 @@ static int
add_methods(PyTypeObject *type, PyMethodDef *meth)
{
PyObject *dict = type->tp_dict;
PyObject *name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put this down where it's first used (i.e. right above line 4852 if (isdescr) {).

Objects/typeobject.c Show resolved Hide resolved
Objects/typeobject.c Show resolved Hide resolved
Python/ceval.c Show resolved Hide resolved
Python/getargs.c Show resolved Hide resolved
@bedevere-bot
Copy link

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@ericsnowcurrently
Copy link
Member

Incidentally, how many uses of the non-WithError API remain after this?

Copy link
Member Author

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your review @ericsnowcurrently!

Modules/_sre.c Outdated
PyExc_IndexError,
"no such group"
);
if (index < 0) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The removed code was moved into match_getindex() because it was repeated after every call of match_getindex(). This function is only called with valid index or the result of match_getindex(). This check was here just to make the caller place simpler. Will move it to the caller place and add an assert instead.

Modules/_csv.c Show resolved Hide resolved
Modules/pyexpat.c Show resolved Hide resolved
@@ -1692,7 +1700,7 @@ MODULE_INITFUNC(void)
}
}
Py_DECREF(errmod_name);
model_module = PyDict_GetItem(d, modelmod_name);
model_module = PyDict_GetItemWithError(d, modelmod_name);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is returned below, at line 1713.

Objects/dictobject.c Outdated Show resolved Hide resolved
Objects/typeobject.c Show resolved Hide resolved
Objects/typeobject.c Show resolved Hide resolved
Objects/typeobject.c Show resolved Hide resolved
Objects/typeobject.c Show resolved Hide resolved
Python/getargs.c Show resolved Hide resolved
@serhiy-storchaka
Copy link
Member Author

As for benchmarks, running the benchmark suite exposes some slowdown on some tests, but results can have significant random component. I'll research this in more details to get more trustworthy result.

@serhiy-storchaka
Copy link
Member Author

I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@ericsnowcurrently: please review the changes made to this pull request.

@brettcannon brettcannon changed the title bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem(). bpo-35459: Use PyDict_GetItemWithError() instead of PyDict_GetItem() Feb 16, 2019
@serhiy-storchaka serhiy-storchaka merged commit a24107b into python:master Feb 25, 2019
@serhiy-storchaka serhiy-storchaka deleted the pydict-getitem branch February 25, 2019 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants