bpo-41991: Remove _PyObject_HasAttrId (GH-22629)

It can silence arbitrary exceptions.
This commit is contained in:
Serhiy Storchaka 2020-10-10 22:23:42 +03:00 committed by GitHub
parent 02a1603f91
commit 98c4433a81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 22 deletions

View file

@ -306,7 +306,6 @@ PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *); PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *); PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *);
PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *); PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *);
PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *);
/* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
don't raise AttributeError. don't raise AttributeError.

View file

@ -854,17 +854,6 @@ _PyObject_GetAttrId(PyObject *v, _Py_Identifier *name)
return result; return result;
} }
int
_PyObject_HasAttrId(PyObject *v, _Py_Identifier *name)
{
int result;
PyObject *oname = _PyUnicode_FromId(name); /* borrowed */
if (!oname)
return -1;
result = PyObject_HasAttr(v, oname);
return result;
}
int int
_PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w) _PyObject_SetAttrId(PyObject *v, _Py_Identifier *name, PyObject *w)
{ {

View file

@ -311,21 +311,22 @@ union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
_Py_IDENTIFIER(__args__); _Py_IDENTIFIER(__args__);
PyObject *qualname = NULL; PyObject *qualname = NULL;
PyObject *module = NULL; PyObject *module = NULL;
PyObject *tmp;
PyObject *r = NULL; PyObject *r = NULL;
int err; int err;
int has_origin = _PyObject_HasAttrId(p, &PyId___origin__); if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
if (has_origin < 0) {
goto exit; goto exit;
} }
if (has_origin) { if (tmp) {
int has_args = _PyObject_HasAttrId(p, &PyId___args__); Py_DECREF(tmp);
if (has_args < 0) { if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
goto exit; goto exit;
} }
if (has_args) { if (tmp) {
// It looks like a GenericAlias // It looks like a GenericAlias
Py_DECREF(tmp);
goto use_repr; goto use_repr;
} }
} }

View file

@ -1593,9 +1593,18 @@ PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
} }
Py_DECREF(tmp); Py_DECREF(tmp);
} }
else {
_PyErr_Clear(tstate);
}
} }
if (exc != PyExc_SyntaxError) { if (exc != PyExc_SyntaxError) {
if (!_PyObject_HasAttrId(v, &PyId_msg)) { if (_PyObject_LookupAttrId(v, &PyId_msg, &tmp) < 0) {
_PyErr_Clear(tstate);
}
else if (tmp) {
Py_DECREF(tmp);
}
else {
tmp = PyObject_Str(v); tmp = PyObject_Str(v);
if (tmp) { if (tmp) {
if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) { if (_PyObject_SetAttrId(v, &PyId_msg, tmp)) {
@ -1607,7 +1616,13 @@ PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset)
_PyErr_Clear(tstate); _PyErr_Clear(tstate);
} }
} }
if (!_PyObject_HasAttrId(v, &PyId_print_file_and_line)) { if (_PyObject_LookupAttrId(v, &PyId_print_file_and_line, &tmp) < 0) {
_PyErr_Clear(tstate);
}
else if (tmp) {
Py_DECREF(tmp);
}
else {
if (_PyObject_SetAttrId(v, &PyId_print_file_and_line, if (_PyObject_SetAttrId(v, &PyId_print_file_and_line,
Py_None)) { Py_None)) {
_PyErr_Clear(tstate); _PyErr_Clear(tstate);

View file

@ -770,7 +770,7 @@ static void
print_exception(PyObject *f, PyObject *value) print_exception(PyObject *f, PyObject *value)
{ {
int err = 0; int err = 0;
PyObject *type, *tb; PyObject *type, *tb, *tmp;
_Py_IDENTIFIER(print_file_and_line); _Py_IDENTIFIER(print_file_and_line);
if (!PyExceptionInstance_Check(value)) { if (!PyExceptionInstance_Check(value)) {
@ -789,10 +789,12 @@ print_exception(PyObject *f, PyObject *value)
if (tb && tb != Py_None) if (tb && tb != Py_None)
err = PyTraceBack_Print(tb, f); err = PyTraceBack_Print(tb, f);
if (err == 0 && if (err == 0 &&
_PyObject_HasAttrId(value, &PyId_print_file_and_line)) (err = _PyObject_LookupAttrId(value, &PyId_print_file_and_line, &tmp)) > 0)
{ {
PyObject *message, *filename, *text; PyObject *message, *filename, *text;
Py_ssize_t lineno, offset; Py_ssize_t lineno, offset;
err = 0;
Py_DECREF(tmp);
if (!parse_syntax_error(value, &message, &filename, if (!parse_syntax_error(value, &message, &filename,
&lineno, &offset, &text)) &lineno, &offset, &text))
PyErr_Clear(); PyErr_Clear();