With reference to improvement issue 43560 :
If those improvements remain unimplemented, or are demoted to "don't fix", users are left in the tricky situation where XML parsing applications can fail, apparently "losing content" in a rare and unpredictable manner. It would be useful to patch the documentation to give users fair warning of this hazard.
For example: the "xml.sax.handler" page in the Python 3.9.2 Documentation for the Python Standard Library (and many prior versions) currently states:
-----------
ContentHandler.characters(content) -- The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks...
-----------
The modified documentation would read something like the following:
-----------
ContentHandler.characters(content) -- The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks... To avoid a situation in which one small content fragment unexpectedly overwrites another one, it is essential for the characters() method to collect content by appending, rather than by assignment.
-----------
To give a concrete example, suppose that a Python programming site recommends the following coding to preserve a small text chunk bracketed by "<p>" tags:
# Note the name attribute of the current tag group
def element_handler(self, tagname, attrs) :
self.CurrentTag = tagname
# Record the content from each "p" tag when encountered
def characters(self, content):
if self.CurrentTag == "p" :
self.name = content
Even though that coding could be expected to work most of the time, it is exposed to the hazard that an unanticipated sequence of calls to the characters() function would overwrite data.
Instead, the coding should look something like this.
# Note the name attribute of the current tag group
def element_handler(self, tagname, attrs) :
self.CurrentTag = tagname
self.name = ""
# Accumulate the content from each "p" tag when encountered
def characters(self, content):
if self.CurrentTag == "p":
self.name.append(content)
|