@@ -293,12 +293,13 @@ error_external_entity_ref_handler(XML_Parser parser,
293293
294294/* Dummy character data handler used when an error (exception) has
295295 been detected, and the actual parsing can be terminated early.
296- This is needed since character data handler can't be safely removed
297- from within the character data handler, but can be replaced. It is
298- used from the character data handler trampoline, and must be
299- used right after `flag_error()` is called. It is also installed as
300- the default handler (same signature) by clear_handlers(), since Expat
301- can't cope with that one being removed from within a callback either. */
296+ This is needed since the character data and default handlers can't be
297+ safely removed from within a callback, only replaced: Expat may call
298+ them several times for one piece of text (a long comment in a document
299+ that is not UTF-8, for example). The default handler has the same
300+ signature, so this function serves for both. clear_handlers() installs
301+ it after `flag_error()`, and the handler setter installs it when a
302+ handler is set to None from within a callback. */
302303static void
303304noop_character_data_handler (void * userData , const XML_Char * data , int len )
304305{
@@ -1841,8 +1842,9 @@ xmlparse_handler_setter(PyObject *op, PyObject *v, void *closure)
18411842 otherwise would, but that's really an odd case. A more
18421843 elaborate system of handlers and state could remove the
18431844 C handler more effectively. The same applies to the
1844- default handler, which Expat calls repeatedly for a single
1845- token that needs encoding conversion. */
1845+ default handler, which Expat can call several times for one
1846+ piece of text, such as a long comment in a document that is
1847+ not UTF-8. */
18461848 if (self -> in_callback
18471849 && (handlernum == CharacterData
18481850 || handlernum == Default
@@ -2796,9 +2798,10 @@ clear_handlers(xmlparseobject *self, int initial)
27962798 xmlhandler c_handler = NULL ;
27972799 if (i == Default || i == DefaultHandlerExpand ) {
27982800 /* Like the character data handler, the default handler
2799- can't be safely removed from within a callback: Expat's
2800- reportDefault() re-reads it between the pieces of a
2801- converted token. Replace it with a no-op instead. */
2801+ can't be removed from within a callback: for a document
2802+ that is not UTF-8, Expat passes a long piece of text to
2803+ it in several calls and re-reads the handler before each
2804+ one (reportDefault() in xmlparse.c). */
28022805 c_handler = (xmlhandler )noop_character_data_handler ;
28032806 }
28042807 CALL_XML_HANDLER_SETTER (& handler_info [i ], self -> itself , c_handler );
0 commit comments