@@ -64,6 +64,7 @@ cdef class _IterparseContext(_ParserContext):
6464 cdef xmlparser.endElementNsSAX2Func _origSaxEnd
6565 cdef xmlparser.startElementSAXFunc _origSaxStartNoNs
6666 cdef xmlparser.endElementSAXFunc _origSaxEndNoNs
67+ cdef xmlparser.startDocumentSAXFunc _origSaxStartDocument
6768 cdef xmlparser.commentSAXFunc _origSaxComment
6869 cdef xmlparser.processingInstructionSAXFunc _origSaxPI
6970 cdef _Element _root
@@ -88,6 +89,8 @@ cdef class _IterparseContext(_ParserContext):
8889 cdef xmlparser.xmlSAXHandler* sax
8990 _ParserContext._initParserContext(self , c_ctxt)
9091 sax = c_ctxt.sax
92+ self ._origSaxStartDocument = sax.startDocument
93+ sax.startDocument = _iterparseSaxStartDocument
9194 self ._origSaxStart = sax.startElementNs
9295 self ._origSaxStartNoNs = sax.startElement
9396 # only override start event handler if needed
@@ -231,6 +234,16 @@ cdef inline void _pushSaxEvent(_IterparseContext context,
231234 context._c_ctxt.disableSAX = 1
232235 context._store_raised()
233236
237+ cdef void _iterparseSaxStartDocument(void * ctxt):
238+ cdef xmlparser.xmlParserCtxt* c_ctxt
239+ c_ctxt = < xmlparser.xmlParserCtxt* > ctxt
240+ context = < _IterparseContext> c_ctxt._private
241+ context._origSaxStartDocument(ctxt)
242+ if c_ctxt.myDoc and c_ctxt.dict and not c_ctxt.myDoc.dict:
243+ # I have no idea why libxml2 disables this - we need it
244+ c_ctxt.dictNames = 1
245+ c_ctxt.myDoc.dict = c_ctxt.dict
246+
234247cdef void _iterparseSaxStart(void * ctxt, char * localname, char * prefix,
235248 char * URI, int nb_namespaces, char ** namespaces,
236249 int nb_attributes, int nb_defaulted,
0 commit comments