bpo-34941: Fix searching Element subclasses. (GH-9766) · python/cpython@b11c566 (original) (raw)

`@@ -204,6 +204,8 @@ typedef struct {

`

204

204

``

205

205

``

206

206

`#define Element_CheckExact(op) (Py_TYPE(op) == &Element_Type)

`

``

207

`+

#define Element_Check(op) PyObject_TypeCheck(op, &Element_Type)

`

``

208

+

207

209

``

208

210

`/* -------------------------------------------------------------------- */

`

209

211

`/* Element constructors and destructor */

`

`@@ -1132,7 +1134,7 @@ _elementtree_Element_extend(ElementObject *self, PyObject *elements)

`

1132

1134

`for (i = 0; i < PySequence_Fast_GET_SIZE(seq); i++) {

`

1133

1135

`PyObject* element = PySequence_Fast_GET_ITEM(seq, i);

`

1134

1136

`Py_INCREF(element);

`

1135

``

`-

if (!PyObject_TypeCheck(element, (PyTypeObject *)&Element_Type)) {

`

``

1137

`+

if (!Element_Check(element)) {

`

1136

1138

`PyErr_Format(

`

1137

1139

`PyExc_TypeError,

`

1138

1140

`"expected an Element, not "%.200s"",

`

`@@ -1184,7 +1186,7 @@ _elementtree_Element_find_impl(ElementObject *self, PyObject *path,

`

1184

1186

`for (i = 0; i < self->extra->length; i++) {

`

1185

1187

`PyObject* item = self->extra->children[i];

`

1186

1188

`int rc;

`

1187

``

`-

if (!Element_CheckExact(item))

`

``

1189

`+

if (!Element_Check(item))

`

1188

1190

`continue;

`

1189

1191

`Py_INCREF(item);

`

1190

1192

`rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);

`

`@@ -1229,14 +1231,14 @@ _elementtree_Element_findtext_impl(ElementObject *self, PyObject *path,

`

1229

1231

` }

`

1230

1232

``

1231

1233

`for (i = 0; i < self->extra->length; i++) {

`

1232

``

`-

ElementObject* item = (ElementObject*) self->extra->children[i];

`

``

1234

`+

PyObject *item = self->extra->children[i];

`

1233

1235

`int rc;

`

1234

``

`-

if (!Element_CheckExact(item))

`

``

1236

`+

if (!Element_Check(item))

`

1235

1237

`continue;

`

1236

1238

`Py_INCREF(item);

`

1237

``

`-

rc = PyObject_RichCompareBool(item->tag, path, Py_EQ);

`

``

1239

`+

rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);

`

1238

1240

`if (rc > 0) {

`

1239

``

`-

PyObject* text = element_get_text(item);

`

``

1241

`+

PyObject* text = element_get_text((ElementObject*)item);

`

1240

1242

`if (text == Py_None) {

`

1241

1243

`Py_DECREF(item);

`

1242

1244

`return PyUnicode_New(0, 0);

`

`@@ -1269,13 +1271,12 @@ _elementtree_Element_findall_impl(ElementObject *self, PyObject *path,

`

1269

1271

`{

`

1270

1272

`Py_ssize_t i;

`

1271

1273

`PyObject* out;

`

1272

``

`-

PyObject* tag = path;

`

1273

1274

`elementtreestate *st = ET_STATE_GLOBAL;

`

1274

1275

``

1275

``

`-

if (checkpath(tag) || namespaces != Py_None) {

`

``

1276

`+

if (checkpath(path) || namespaces != Py_None) {

`

1276

1277

`_Py_IDENTIFIER(findall);

`

1277

1278

`return _PyObject_CallMethodIdObjArgs(

`

1278

``

`-

st->elementpath_obj, &PyId_findall, self, tag, namespaces, NULL

`

``

1279

`+

st->elementpath_obj, &PyId_findall, self, path, namespaces, NULL

`

1279

1280

` );

`

1280

1281

` }

`

1281

1282

``

`@@ -1289,10 +1290,10 @@ _elementtree_Element_findall_impl(ElementObject *self, PyObject *path,

`

1289

1290

`for (i = 0; i < self->extra->length; i++) {

`

1290

1291

`PyObject* item = self->extra->children[i];

`

1291

1292

`int rc;

`

1292

``

`-

if (!Element_CheckExact(item))

`

``

1293

`+

if (!Element_Check(item))

`

1293

1294

`continue;

`

1294

1295

`Py_INCREF(item);

`

1295

``

`-

rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, tag, Py_EQ);

`

``

1296

`+

rc = PyObject_RichCompareBool(((ElementObject*)item)->tag, path, Py_EQ);

`

1296

1297

`if (rc != 0 && (rc < 0 || PyList_Append(out, item) < 0)) {

`

1297

1298

`Py_DECREF(item);

`

1298

1299

`Py_DECREF(out);

`

`@@ -2173,7 +2174,7 @@ elementiter_next(ElementIterObject *it)

`

2173

2174

`continue;

`

2174

2175

` }

`

2175

2176

``

2176

``

`-

if (!PyObject_TypeCheck(extra->children[child_index], &Element_Type)) {

`

``

2177

`+

if (!Element_Check(extra->children[child_index])) {

`

2177

2178

`PyErr_Format(PyExc_AttributeError,

`

2178

2179

`"'%.100s' object has no attribute 'iter'",

`

2179

2180

`Py_TYPE(extra->children[child_index])->tp_name);

`