@@ -72,10 +72,11 @@ pyexpat_get_state(PyObject *module)
72
72
73
73
/* Declarations for objects of type xmlparser */
74
74
75
- typedef struct {
75
+ typedef struct xmlparseobject_ {
76
76
PyObject_HEAD
77
77
78
78
XML_Parser itself ;
79
+ struct xmlparseobject_ * parent ;
79
80
int ordered_attributes ; /* Return attributes as a list. */
80
81
int specified_attributes ; /* Report only specified attributes. */
81
82
int in_callback ; /* Is a callback active? */
@@ -1065,6 +1066,11 @@ pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self,
1065
1066
return NULL ;
1066
1067
}
1067
1068
1069
+ // The new subparser will make use of the parent XML_Parser inside of Expat.
1070
+ // So we need to take subparsers into account with the reference counting
1071
+ // of their parent parser.
1072
+ Py_INCREF (self );
1073
+
1068
1074
new_parser -> buffer_size = self -> buffer_size ;
1069
1075
new_parser -> buffer_used = 0 ;
1070
1076
new_parser -> buffer = NULL ;
@@ -1074,18 +1080,21 @@ pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self,
1074
1080
new_parser -> ns_prefixes = self -> ns_prefixes ;
1075
1081
new_parser -> itself = XML_ExternalEntityParserCreate (self -> itself , context ,
1076
1082
encoding );
1083
+ new_parser -> parent = self ;
1077
1084
new_parser -> handlers = 0 ;
1078
1085
new_parser -> intern = Py_XNewRef (self -> intern );
1079
1086
1080
1087
if (self -> buffer != NULL ) {
1081
1088
new_parser -> buffer = PyMem_Malloc (new_parser -> buffer_size );
1082
1089
if (new_parser -> buffer == NULL ) {
1083
1090
Py_DECREF (new_parser );
1091
+ Py_DECREF (self );
1084
1092
return PyErr_NoMemory ();
1085
1093
}
1086
1094
}
1087
1095
if (!new_parser -> itself ) {
1088
1096
Py_DECREF (new_parser );
1097
+ Py_DECREF (self );
1089
1098
return PyErr_NoMemory ();
1090
1099
}
1091
1100
@@ -1099,6 +1108,7 @@ pyexpat_xmlparser_ExternalEntityParserCreate_impl(xmlparseobject *self,
1099
1108
new_parser -> handlers = PyMem_New (PyObject * , i );
1100
1109
if (!new_parser -> handlers ) {
1101
1110
Py_DECREF (new_parser );
1111
+ Py_DECREF (self );
1102
1112
return PyErr_NoMemory ();
1103
1113
}
1104
1114
clear_handlers (new_parser , 1 );
@@ -1479,6 +1489,7 @@ newxmlparseobject(pyexpat_state *state, const char *encoding,
1479
1489
/* namespace_separator is either NULL or contains one char + \0 */
1480
1490
self -> itself = XML_ParserCreate_MM (encoding , & ExpatMemoryHandler ,
1481
1491
namespace_separator );
1492
+ self -> parent = NULL ;
1482
1493
if (self -> itself == NULL ) {
1483
1494
PyErr_SetString (PyExc_RuntimeError ,
1484
1495
"XML_ParserCreate failed" );
@@ -1538,6 +1549,10 @@ xmlparse_dealloc(PyObject *op)
1538
1549
XML_ParserFree (self -> itself );
1539
1550
}
1540
1551
self -> itself = NULL ;
1552
+ if (self -> parent != NULL ) {
1553
+ Py_DECREF (self -> parent );
1554
+ self -> parent = NULL ;
1555
+ }
1541
1556
1542
1557
if (self -> handlers != NULL ) {
1543
1558
PyMem_Free (self -> handlers );
0 commit comments