mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
bpo-31572: Get rid of _PyObject_HasAttrId() in the ASDL parser. (#3725)
Silence only expected AttributeError.
This commit is contained in:
parent
60c3d3551a
commit
bba2239c17
2 changed files with 802 additions and 628 deletions
|
@ -497,18 +497,15 @@ class Obj2ModVisitor(PickleVisitor):
|
||||||
|
|
||||||
def visitField(self, field, name, sum=None, prod=None, depth=0):
|
def visitField(self, field, name, sum=None, prod=None, depth=0):
|
||||||
ctype = get_c_type(field.type)
|
ctype = get_c_type(field.type)
|
||||||
if field.opt:
|
if not field.opt:
|
||||||
check = "exists_not_none(obj, &PyId_%s)" % (field.name,)
|
self.emit("tmp = _PyObject_GetAttrId(obj, &PyId_%s);" % field.name, depth)
|
||||||
else:
|
else:
|
||||||
check = "_PyObject_HasAttrId(obj, &PyId_%s)" % (field.name,)
|
self.emit("tmp = get_not_none(obj, &PyId_%s);" % field.name, depth)
|
||||||
self.emit("if (%s) {" % (check,), depth, reflow=False)
|
self.emit("if (tmp != NULL) {", depth)
|
||||||
self.emit("int res;", depth+1)
|
self.emit("int res;", depth+1)
|
||||||
if field.seq:
|
if field.seq:
|
||||||
self.emit("Py_ssize_t len;", depth+1)
|
self.emit("Py_ssize_t len;", depth+1)
|
||||||
self.emit("Py_ssize_t i;", depth+1)
|
self.emit("Py_ssize_t i;", depth+1)
|
||||||
self.emit("tmp = _PyObject_GetAttrId(obj, &PyId_%s);" % field.name, depth+1)
|
|
||||||
self.emit("if (tmp == NULL) goto failed;", depth+1)
|
|
||||||
if field.seq:
|
|
||||||
self.emit("if (!PyList_Check(tmp)) {", depth+1)
|
self.emit("if (!PyList_Check(tmp)) {", depth+1)
|
||||||
self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
|
self.emit("PyErr_Format(PyExc_TypeError, \"%s field \\\"%s\\\" must "
|
||||||
"be a list, not a %%.200s\", tmp->ob_type->tp_name);" %
|
"be a list, not a %%.200s\", tmp->ob_type->tp_name);" %
|
||||||
|
@ -542,13 +539,19 @@ class Obj2ModVisitor(PickleVisitor):
|
||||||
self.emit("if (res != 0) goto failed;", depth+1)
|
self.emit("if (res != 0) goto failed;", depth+1)
|
||||||
|
|
||||||
self.emit("Py_CLEAR(tmp);", depth+1)
|
self.emit("Py_CLEAR(tmp);", depth+1)
|
||||||
self.emit("} else {", depth)
|
|
||||||
if not field.opt:
|
if not field.opt:
|
||||||
|
self.emit("} else {", depth)
|
||||||
|
self.emit("if (PyErr_ExceptionMatches(PyExc_AttributeError)) {", depth+1)
|
||||||
message = "required field \\\"%s\\\" missing from %s" % (field.name, name)
|
message = "required field \\\"%s\\\" missing from %s" % (field.name, name)
|
||||||
format = "PyErr_SetString(PyExc_TypeError, \"%s\");"
|
format = "PyErr_SetString(PyExc_TypeError, \"%s\");"
|
||||||
self.emit(format % message, depth+1, reflow=False)
|
self.emit(format % message, depth+2, reflow=False)
|
||||||
|
self.emit("}", depth+1)
|
||||||
self.emit("return 1;", depth+1)
|
self.emit("return 1;", depth+1)
|
||||||
else:
|
else:
|
||||||
|
self.emit("} else if (PyErr_Occurred()) {", depth)
|
||||||
|
self.emit("return 1;", depth+1)
|
||||||
|
self.emit("} else {", depth)
|
||||||
|
|
||||||
if self.isNumeric(field):
|
if self.isNumeric(field):
|
||||||
self.emit("%s = 0;" % field.name, depth+1)
|
self.emit("%s = 0;" % field.name, depth+1)
|
||||||
elif not self.isSimpleType(field):
|
elif not self.isSimpleType(field):
|
||||||
|
@ -660,13 +663,17 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
int res = -1;
|
int res = -1;
|
||||||
PyObject *key, *value, *fields;
|
PyObject *key, *value, *fields;
|
||||||
fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
|
fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
|
||||||
if (!fields)
|
|
||||||
PyErr_Clear();
|
|
||||||
if (fields) {
|
if (fields) {
|
||||||
numfields = PySequence_Size(fields);
|
numfields = PySequence_Size(fields);
|
||||||
if (numfields == -1)
|
if (numfields == -1)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
else if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
||||||
|
PyErr_Clear();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
res = 0; /* if no error occurs, this stays 0 to the end */
|
res = 0; /* if no error occurs, this stays 0 to the end */
|
||||||
if (numfields < PyTuple_GET_SIZE(args)) {
|
if (numfields < PyTuple_GET_SIZE(args)) {
|
||||||
|
@ -958,17 +965,20 @@ static int add_ast_fields(void)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int exists_not_none(PyObject *obj, _Py_Identifier *id)
|
static PyObject *get_not_none(PyObject *obj, _Py_Identifier *id)
|
||||||
{
|
{
|
||||||
int isnone;
|
|
||||||
PyObject *attr = _PyObject_GetAttrId(obj, id);
|
PyObject *attr = _PyObject_GetAttrId(obj, id);
|
||||||
if (!attr) {
|
if (!attr) {
|
||||||
|
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
||||||
PyErr_Clear();
|
PyErr_Clear();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
isnone = attr == Py_None;
|
return NULL;
|
||||||
|
}
|
||||||
|
else if (attr == Py_None) {
|
||||||
Py_DECREF(attr);
|
Py_DECREF(attr);
|
||||||
return !isnone;
|
return NULL;
|
||||||
|
}
|
||||||
|
return attr;
|
||||||
}
|
}
|
||||||
|
|
||||||
""", 0, reflow=False)
|
""", 0, reflow=False)
|
||||||
|
|
1120
Python/Python-ast.c
1120
Python/Python-ast.c
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue