X Tutup
The Wayback Machine - https://web.archive.org/web/20250816154315/https://github.com/python/cpython/pull/1883
Skip to content

bpo-30509: Clean up calling type slots. #1883

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

Merged

Conversation

serhiy-storchaka
Copy link
Member

No description provided.

@serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label May 31, 2017
@serhiy-storchaka serhiy-storchaka requested a review from vstinner May 31, 2017 06:47
@mention-bot
Copy link

@serhiy-storchaka, thanks for your PR! By analyzing the history of the files in this pull request, we identified @tim-one, @benjaminp and @larryhastings to be potential reviewers.


retval = PyObject_CallFunctionObjArgs(func, ival, NULL);
Py_DECREF(func);
retval = call_method(self, &PyId___getitem__, &ival, 1);
Copy link
Member

Choose a reason for hiding this comment

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

This change optimizes the __getitem__ slot if I understand correctly, since it handles bound/unbound methods. Good :-) It might be useful to mention it.

About &ival: FYI I removed the _PyObject_CallArg1() macro since it increased the stack usage: https://bugs.python.org/issue28858

PyErr_SetObject(PyExc_AttributeError, getitem_str);
PyObject *retval;
PyObject *ival = PyLong_FromSsize_t(i);
if (ival == NULL)
Copy link
Member

Choose a reason for hiding this comment

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

PEP 7 now requires { ... } for if block.

@@ -6243,7 +6198,7 @@ slot_tp_hash(PyObject *self)
Py_ssize_t h;
int unbound;

func = lookup_method(self, &PyId___hash__, &unbound);
func = lookup_maybe_method(self, &PyId___hash__, &unbound);
Copy link
Member

Choose a reason for hiding this comment

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

ok: Error handled below by PyObject_HashNotImplemented().

@@ -6422,7 +6377,7 @@ slot_tp_richcompare(PyObject *self, PyObject *other, int op)
int unbound;
PyObject *func, *res;

func = lookup_method(self, &name_op[op], &unbound);
func = lookup_maybe_method(self, &name_op[op], &unbound);
Copy link
Member

Choose a reason for hiding this comment

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

ok: exception ignored, PyErr_Clear().

@@ -6441,7 +6396,7 @@ slot_tp_iter(PyObject *self)
PyObject *func, *res;
_Py_IDENTIFIER(__iter__);

func = lookup_method(self, &PyId___iter__, &unbound);
func = lookup_maybe_method(self, &PyId___iter__, &unbound);
Copy link
Member

Choose a reason for hiding this comment

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

ok: exception replaced below.

@@ -6616,7 +6571,7 @@ slot_am_aiter(PyObject *self)
PyObject *func, *res;
_Py_IDENTIFIER(__aiter__);

func = lookup_method(self, &PyId___aiter__, &unbound);
func = lookup_maybe_method(self, &PyId___aiter__, &unbound);
Copy link
Member

Choose a reason for hiding this comment

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

ok: exception replaced below.

@@ -6635,7 +6590,7 @@ slot_am_anext(PyObject *self)
PyObject *func, *res;
_Py_IDENTIFIER(__anext__);

func = lookup_method(self, &PyId___anext__, &unbound);
func = lookup_maybe_method(self, &PyId___anext__, &unbound);
Copy link
Member

Choose a reason for hiding this comment

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

ok: exception replaced below.

if (func == NULL) {
if (!PyErr_Occurred())
PyErr_SetObject(PyExc_AttributeError, name->object);
func = lookup_method(obj, name, &unbound);
Copy link
Member

Choose a reason for hiding this comment

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

ok: reuse lookup_method() which handles exceptions the same way.

if (!PyErr_Occurred())
PyErr_SetObject(PyExc_AttributeError, name->object);
func = lookup_method(obj, name, &unbound);
if (func == NULL)
Copy link
Member

Choose a reason for hiding this comment

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

PEP 7: need { ... }

"braces are strongly preferred" https://www.python.org/dev/peps/pep-0007/

Copy link
Member Author

Choose a reason for hiding this comment

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

"... but may be omitted where C permits"

@@ -6223,7 +6178,7 @@ slot_tp_repr(PyObject *self)
_Py_IDENTIFIER(__repr__);
int unbound;

func = lookup_method(self, &PyId___repr__, &unbound);
func = lookup_maybe_method(self, &PyId___repr__, &unbound);
Copy link
Member

Choose a reason for hiding this comment

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

ok: exception ignored using PyErr_Clear().

@vstinner
Copy link
Member

LGTM except of missing { ... } at two places.

@vstinner
Copy link
Member

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

I opened a discussion about braces on python-dev to try to clarify the coding style.

In the meanwhile, I don't think that it's worth it to block such nice enhancement: LGTM, I approve it.

@serhiy-storchaka serhiy-storchaka merged commit 4e624ca into python:master Jun 1, 2017
@serhiy-storchaka serhiy-storchaka deleted the type-slot-calls-cleanup branch June 1, 2017 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
X Tutup