GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-32571: Avoid raising unneeded AttributeError and silencing it C code. by serhiy-storchaka · Pull Request #5205 · python/cpython

bpo-32571: Avoid raising unneeded AttributeError and silencing it C code. #5205

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.

Sign up for GitHub

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

Closed
serhiy-storchaka wants to merge 1 commit into python:master from serhiy-storchaka:getattr-without-error

Uh oh!

There was an error while loading. Please reload this page.

Closed

bpo-32571: Avoid raising unneeded AttributeError and silencing it C code. #5205

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
a0d3e42
bpo-32571: Avoid raising unneeded AttributeError and silencing it C c…
serhiy-storchaka Jan 16, 2018
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

1 change: 1 addition & 0 deletions 1 Include/object.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
/* Same as PyObject_GetAttr(), but don't raise AttributeError. */
PyAPI_FUNC(PyObject *) _PyObject_GetAttrWithoutError(PyObject *, PyObject *);
PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
PyAPI_FUNC(PyObject *) _PyObject_GetAttrIdWithoutError(PyObject *, struct _Py_Identifier *);
Comment thread

@1st1 1st1 Jan 16, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Can we have an int return value so that we don't need to call PyErr_Occurred()? IMHO, it's an API anti-pattern to require double checking what NULL means.

PyAPI_FUNC(int) _PyObject_GetAttrIdWithoutError(
    PyObject *, struct _Py_Identifier *, PyObject **);
  • -1 error
  • 0 not found
  • 1 found

I'd also do this for _PyObject_GetAttrWithoutError which we committed today. /cc @methane

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@methane methane Jan 17, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Make sense. Go ahead.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@methane methane Jan 17, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

For usability, third argument should be filled with NULL when returning 1.
By it, caller can use if (val == NULL) instead of assigning return value to local variable.

if (_PyObject_GetAttrIdWithoutError((PyObject *)deque, &PyId___dict__, &func) < 0) {
    // when error happens
    return NULL;
}
if (func == NULL) {
    // when `__dict__` not found.
    Py_RETURN_NONE;
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@1st1 1st1 Jan 17, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yeah, I agree, it's even better.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@serhiy-storchaka serhiy-storchaka Jan 17, 2018

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Then I think It would be better to rename the function to _PyObject_LookupAttrId or _PyObject_FindAttrId. _PyObject_GetAttrIdWithoutError looks misleading bacause despite its name it can raise an error.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@methane methane Jan 17, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I agree. "find" is far better than "without error".

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@1st1 1st1 Jan 17, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I like LookupAttrId. It sounds better.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@1st1 1st1 Jan 17, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

BTW, I'd also remove the leading underscore from _PyObject_GetAttrWithoutError, making it PyObject_LookupAttr (and we keep the underscore for _PyObject_LookupAttrId).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@methane methane Jan 17, 2018

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hm, "lookup" make sense too because there are _PyType_Lookup and _PyType_LookupId
and they doesn't set exception.
And +1 to make it public API.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
Expand Down
5 changes: 2 additions & 3 deletions 5 Modules/_collectionsmodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1339,12 +1339,11 @@ deque_reduce(dequeobject *deque)
PyObject *dict, *it;
_Py_IDENTIFIER(__dict__);

dict = _PyObject_GetAttrId((PyObject *)deque, &PyId___dict__);
dict = _PyObject_GetAttrIdWithoutError((PyObject *)deque, &PyId___dict__);
if (dict == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
if (PyErr_Occurred()) {
return NULL;
}
PyErr_Clear();
dict = Py_None;
Py_INCREF(dict);
}
Expand Down
6 changes: 2 additions & 4 deletions 6 Modules/_io/fileio.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,9 @@ fileio_repr(fileio *self)
if (self->fd < 0)
return PyUnicode_FromFormat("<_io.FileIO [closed]>");

nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
nameobj = _PyObject_GetAttrIdWithoutError((PyObject *) self, &PyId_name);
if (nameobj == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
if (PyErr_Occurred())
return NULL;
res = PyUnicode_FromFormat(
"<_io.FileIO fd=%d mode='%s' closefd=%s>",
Expand Down
5 changes: 2 additions & 3 deletions 5 Modules/_io/iobase.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,11 @@ iobase_is_closed(PyObject *self)
PyObject *res;
/* This gets the derived attribute, which is *not* __IOBase_closed
in most cases! */
res = _PyObject_GetAttrId(self, &PyId___IOBase_closed);
res = _PyObject_GetAttrIdWithoutError(self, &PyId___IOBase_closed);
if (res == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
if (PyErr_Occurred()) {
return -1;
}
PyErr_Clear();
return 0;
}
Py_DECREF(res);
Expand Down
12 changes: 4 additions & 8 deletions 12 Modules/_io/textio.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -924,11 +924,9 @@ _textiowrapper_set_encoder(textio *self, PyObject *codec_info,
return -1;

/* Get the normalized named of the codec */
res = _PyObject_GetAttrId(codec_info, &PyId_name);
res = _PyObject_GetAttrIdWithoutError(codec_info, &PyId_name);
if (res == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
if (PyErr_Occurred())
return -1;
}
else if (PyUnicode_Check(res)) {
Expand Down Expand Up @@ -1178,12 +1176,10 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
if (Py_TYPE(buffer) == &PyBufferedReader_Type ||
Py_TYPE(buffer) == &PyBufferedWriter_Type ||
Py_TYPE(buffer) == &PyBufferedRandom_Type) {
raw = _PyObject_GetAttrId(buffer, &PyId_raw);
raw = _PyObject_GetAttrIdWithoutError(buffer, &PyId_raw);
/* Cache the raw FileIO object to speed up 'closed' checks */
if (raw == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
if (PyErr_Occurred())
goto error;
}
else if (Py_TYPE(raw) == &PyFileIO_Type)
Expand Down
108 changes: 39 additions & 69 deletions 108 Modules/_pickle.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,13 @@ init_method_ref(PyObject *self, _Py_Identifier *name,

/* *method_func and *method_self should be consistent. All refcount decrements
should be occurred after setting *method_self and *method_func. */
func = _PyObject_GetAttrId(self, name);
func = _PyObject_GetAttrIdWithoutError(self, name);
if (func == NULL) {
*method_self = NULL;
Py_CLEAR(*method_func);
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
if (PyErr_Occurred()) {
return -1;
}
PyErr_Clear();
return 0;
}

Expand Down Expand Up @@ -1155,11 +1154,12 @@ _Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
{
_Py_IDENTIFIER(write);
assert(file != NULL);
self->write = _PyObject_GetAttrId(file, &PyId_write);
self->write = _PyObject_GetAttrIdWithoutError(file, &PyId_write);
if (self->write == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"file must have a 'write' attribute");
}
return -1;
}

Expand Down Expand Up @@ -1504,19 +1504,17 @@ _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
_Py_IDENTIFIER(read);
_Py_IDENTIFIER(readline);

self->peek = _PyObject_GetAttrId(file, &PyId_peek);
if (self->peek == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else
return -1;
self->peek = _PyObject_GetAttrIdWithoutError(file, &PyId_peek);
if (self->peek == NULL && PyErr_Occurred()) {
return -1;
}
self->read = _PyObject_GetAttrId(file, &PyId_read);
self->readline = _PyObject_GetAttrId(file, &PyId_readline);
self->read = _PyObject_GetAttrIdWithoutError(file, &PyId_read);
self->readline = _PyObject_GetAttrIdWithoutError(file, &PyId_readline);
if (self->readline == NULL || self->read == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"file must have 'read' and 'readline' attributes");
}
Py_CLEAR(self->read);
Py_CLEAR(self->readline);
Py_CLEAR(self->peek);
Expand Down Expand Up @@ -1691,7 +1689,7 @@ get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
PyObject *name = PyList_GET_ITEM(names, i);
Py_XDECREF(parent);
parent = obj;
obj = PyObject_GetAttr(parent, name);
obj = _PyObject_GetAttrWithoutError(parent, name);
if (obj == NULL) {
Py_DECREF(parent);
return NULL;
Expand All @@ -1704,16 +1702,6 @@ get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
return obj;
}

static void
reformat_attribute_error(PyObject *obj, PyObject *name)
{
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
PyErr_Format(PyExc_AttributeError,
"Can't get attribute %R on %R", name, obj);
}
}


static PyObject *
getattribute(PyObject *obj, PyObject *name, int allow_qualname)
Expand All @@ -1728,9 +1716,11 @@ getattribute(PyObject *obj, PyObject *name, int allow_qualname)
Py_DECREF(dotted_path);
}
else
attr = PyObject_GetAttr(obj, name);
if (attr == NULL)
reformat_attribute_error(obj, name);
attr = _PyObject_GetAttrWithoutError(obj, name);
if (attr == NULL && !PyErr_Occurred()) {
PyErr_Format(PyExc_AttributeError,
"Can't get attribute %R on %R", name, obj);
}
return attr;
}

Expand All @@ -1748,9 +1738,6 @@ _checkmodule(PyObject *module_name, PyObject *module,

PyObject *candidate = get_deep_attribute(module, dotted_path, NULL);
if (candidate == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
return -1;
}
if (candidate != global) {
Expand All @@ -1772,12 +1759,11 @@ whichmodule(PyObject *global, PyObject *dotted_path)
_Py_IDENTIFIER(modules);
_Py_IDENTIFIER(__main__);

module_name = _PyObject_GetAttrId(global, &PyId___module__);
module_name = _PyObject_GetAttrIdWithoutError(global, &PyId___module__);

if (module_name == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
if (PyErr_Occurred())
return NULL;
PyErr_Clear();
}
else {
/* In some rare cases (e.g., bound methods of extension types),
Expand Down Expand Up @@ -3328,13 +3314,10 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name)
global_name = name;
}
else {
global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
global_name = _PyObject_GetAttrIdWithoutError(obj, &PyId___qualname__);
if (global_name == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
if (PyErr_Occurred())
goto error;
PyErr_Clear();
}
if (global_name == NULL) {
global_name = _PyObject_GetAttrId(obj, &PyId___name__);
if (global_name == NULL)
goto error;
Expand Down Expand Up @@ -3656,13 +3639,10 @@ get_class(PyObject *obj)
PyObject *cls;
_Py_IDENTIFIER(__class__);

cls = _PyObject_GetAttrId(obj, &PyId___class__);
if (cls == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
cls = (PyObject *) Py_TYPE(obj);
Py_INCREF(cls);
}
cls = _PyObject_GetAttrIdWithoutError(obj, &PyId___class__);
if (cls == NULL && !PyErr_Occurred()) {
cls = (PyObject *) Py_TYPE(obj);
Py_INCREF(cls);
}
return cls;
}
Expand Down Expand Up @@ -3734,12 +3714,11 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
PyObject *name;
_Py_IDENTIFIER(__name__);

name = _PyObject_GetAttrId(callable, &PyId___name__);
name = _PyObject_GetAttrIdWithoutError(callable, &PyId___name__);
if (name == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
if (PyErr_Occurred()) {
return -1;
}
PyErr_Clear();
}
else if (PyUnicode_Check(name)) {
_Py_IDENTIFIER(__newobj_ex__);
Expand Down Expand Up @@ -4108,23 +4087,20 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
don't actually have to check for a __reduce__ method. */

/* Check for a __reduce_ex__ method. */
reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
reduce_func = _PyObject_GetAttrIdWithoutError(obj, &PyId___reduce_ex__);
if (reduce_func != NULL) {
PyObject *proto;
proto = PyLong_FromLong(self->proto);
if (proto != NULL) {
reduce_value = _Pickle_FastCall(reduce_func, proto);
}
}
else if (PyErr_Occurred()) {
goto error;
}
else {
PickleState *st = _Pickle_GetGlobalState();

if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
}
else {
goto error;
}
/* Check for a __reduce__ method. */
reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
if (reduce_func != NULL) {
Expand Down Expand Up @@ -4401,13 +4377,10 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
return -1;
}

self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
self->dispatch_table = _PyObject_GetAttrIdWithoutError((PyObject *)self,
&PyId_dispatch_table);
if (self->dispatch_table == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
return -1;
}
PyErr_Clear();
if (self->dispatch_table == NULL && PyErr_Occurred()) {
return -1;
}

return 0;
Expand Down Expand Up @@ -5370,12 +5343,11 @@ instantiate(PyObject *cls, PyObject *args)
if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) {
_Py_IDENTIFIER(__getinitargs__);
_Py_IDENTIFIER(__new__);
PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__);
PyObject *func = _PyObject_GetAttrIdWithoutError(cls, &PyId___getinitargs__);
if (func == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
if (PyErr_Occurred()) {
return NULL;
}
PyErr_Clear();
return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL);
}
Py_DECREF(func);
Expand Down Expand Up @@ -6225,11 +6197,9 @@ load_build(UnpicklerObject *self)

inst = self->stack->data[Py_SIZE(self->stack) - 1];

setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
setstate = _PyObject_GetAttrIdWithoutError(inst, &PyId___setstate__);
if (setstate == NULL) {
if (PyErr_ExceptionMatches(PyExc_AttributeError))
PyErr_Clear();
else {
if (PyErr_Occurred()) {
Py_DECREF(state);
return -1;
}
Expand Down
5 changes: 2 additions & 3 deletions 5 Modules/arraymodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2203,11 +2203,10 @@ array_array___reduce_ex__(arrayobject *self, PyObject *value)
if (protocol == -1 && PyErr_Occurred())
return NULL;

dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
dict = _PyObject_GetAttrIdWithoutError((PyObject *)self, &PyId___dict__);
if (dict == NULL) {
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
if (PyErr_Occurred())
return NULL;
PyErr_Clear();
dict = Py_None;
Py_INCREF(dict);
}
Expand Down
5 changes: 2 additions & 3 deletions 5 Modules/itertoolsmodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -830,17 +830,16 @@ tee(PyObject *self, PyObject *args)
return NULL;
}

copyfunc = _PyObject_GetAttrId(it, &PyId___copy__);
copyfunc = _PyObject_GetAttrIdWithoutError(it, &PyId___copy__);
if (copyfunc != NULL) {
copyable = it;
}
else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
else if (PyErr_Occurred()) {
Py_DECREF(it);
Py_DECREF(result);
return NULL;
}
else {
PyErr_Clear();
copyable = tee_fromiterable(it);
Py_DECREF(it);
if (copyable == NULL) {
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.