bpo-15999: Always pass bool instead of int to the expat parser. (GH-15622)

This commit is contained in:
Serhiy Storchaka 2019-09-01 12:11:43 +03:00 committed by GitHub
parent 41c57b3353
commit eb8974616b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 36 deletions

View File

@ -231,7 +231,7 @@ class ParseTest(unittest.TestCase):
parser = expat.ParserCreate(namespace_separator='!') parser = expat.ParserCreate(namespace_separator='!')
self._hookup_callbacks(parser, out) self._hookup_callbacks(parser, out)
parser.Parse(data, 1) parser.Parse(data, True)
operations = out.out operations = out.out
self._verify_parse_output(operations) self._verify_parse_output(operations)
@ -243,7 +243,7 @@ class ParseTest(unittest.TestCase):
parser = expat.ParserCreate(namespace_separator='!') parser = expat.ParserCreate(namespace_separator='!')
self._hookup_callbacks(parser, out) self._hookup_callbacks(parser, out)
parser.Parse(data.decode('iso-8859-1'), 1) parser.Parse(data.decode('iso-8859-1'), True)
operations = out.out operations = out.out
self._verify_parse_output(operations) self._verify_parse_output(operations)
@ -316,7 +316,7 @@ class InterningTest(unittest.TestCase):
L.append(name) L.append(name)
p.StartElementHandler = collector p.StartElementHandler = collector
p.EndElementHandler = collector p.EndElementHandler = collector
p.Parse(b"<e> <e/> <e></e> </e>", 1) p.Parse(b"<e> <e/> <e></e> </e>", True)
tag = L[0] tag = L[0]
self.assertEqual(len(L), 6) self.assertEqual(len(L), 6)
for entry in L: for entry in L:
@ -332,14 +332,14 @@ class InterningTest(unittest.TestCase):
def ExternalEntityRefHandler(self, context, base, sysId, pubId): def ExternalEntityRefHandler(self, context, base, sysId, pubId):
external_parser = self.parser.ExternalEntityParserCreate("") external_parser = self.parser.ExternalEntityParserCreate("")
self.parser_result = external_parser.Parse(b"", 1) self.parser_result = external_parser.Parse(b"", True)
return 1 return 1
parser = expat.ParserCreate(namespace_separator='!') parser = expat.ParserCreate(namespace_separator='!')
parser.buffer_text = 1 parser.buffer_text = 1
out = ExternalOutputter(parser) out = ExternalOutputter(parser)
parser.ExternalEntityRefHandler = out.ExternalEntityRefHandler parser.ExternalEntityRefHandler = out.ExternalEntityRefHandler
parser.Parse(data, 1) parser.Parse(data, True)
self.assertEqual(out.parser_result, 1) self.assertEqual(out.parser_result, 1)
@ -383,7 +383,7 @@ class BufferTextTest(unittest.TestCase):
def test_buffering_enabled(self): def test_buffering_enabled(self):
# Make sure buffering is turned on # Make sure buffering is turned on
self.assertTrue(self.parser.buffer_text) self.assertTrue(self.parser.buffer_text)
self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1) self.parser.Parse(b"<a>1<b/>2<c/>3</a>", True)
self.assertEqual(self.stuff, ['123'], self.assertEqual(self.stuff, ['123'],
"buffered text not properly collapsed") "buffered text not properly collapsed")
@ -391,39 +391,39 @@ class BufferTextTest(unittest.TestCase):
# XXX This test exposes more detail of Expat's text chunking than we # XXX This test exposes more detail of Expat's text chunking than we
# XXX like, but it tests what we need to concisely. # XXX like, but it tests what we need to concisely.
self.setHandlers(["StartElementHandler"]) self.setHandlers(["StartElementHandler"])
self.parser.Parse(b"<a>1<b buffer-text='no'/>2\n3<c buffer-text='yes'/>4\n5</a>", 1) self.parser.Parse(b"<a>1<b buffer-text='no'/>2\n3<c buffer-text='yes'/>4\n5</a>", True)
self.assertEqual(self.stuff, self.assertEqual(self.stuff,
["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"], ["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"],
"buffering control not reacting as expected") "buffering control not reacting as expected")
def test2(self): def test2(self):
self.parser.Parse(b"<a>1<b/>&lt;2&gt;<c/>&#32;\n&#x20;3</a>", 1) self.parser.Parse(b"<a>1<b/>&lt;2&gt;<c/>&#32;\n&#x20;3</a>", True)
self.assertEqual(self.stuff, ["1<2> \n 3"], self.assertEqual(self.stuff, ["1<2> \n 3"],
"buffered text not properly collapsed") "buffered text not properly collapsed")
def test3(self): def test3(self):
self.setHandlers(["StartElementHandler"]) self.setHandlers(["StartElementHandler"])
self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1) self.parser.Parse(b"<a>1<b/>2<c/>3</a>", True)
self.assertEqual(self.stuff, ["<a>", "1", "<b>", "2", "<c>", "3"], self.assertEqual(self.stuff, ["<a>", "1", "<b>", "2", "<c>", "3"],
"buffered text not properly split") "buffered text not properly split")
def test4(self): def test4(self):
self.setHandlers(["StartElementHandler", "EndElementHandler"]) self.setHandlers(["StartElementHandler", "EndElementHandler"])
self.parser.CharacterDataHandler = None self.parser.CharacterDataHandler = None
self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1) self.parser.Parse(b"<a>1<b/>2<c/>3</a>", True)
self.assertEqual(self.stuff, self.assertEqual(self.stuff,
["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"]) ["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"])
def test5(self): def test5(self):
self.setHandlers(["StartElementHandler", "EndElementHandler"]) self.setHandlers(["StartElementHandler", "EndElementHandler"])
self.parser.Parse(b"<a>1<b></b>2<c/>3</a>", 1) self.parser.Parse(b"<a>1<b></b>2<c/>3</a>", True)
self.assertEqual(self.stuff, self.assertEqual(self.stuff,
["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"]) ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"])
def test6(self): def test6(self):
self.setHandlers(["CommentHandler", "EndElementHandler", self.setHandlers(["CommentHandler", "EndElementHandler",
"StartElementHandler"]) "StartElementHandler"])
self.parser.Parse(b"<a>1<b/>2<c></c>345</a> ", 1) self.parser.Parse(b"<a>1<b/>2<c></c>345</a> ", True)
self.assertEqual(self.stuff, self.assertEqual(self.stuff,
["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"], ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"],
"buffered text not properly split") "buffered text not properly split")
@ -431,7 +431,7 @@ class BufferTextTest(unittest.TestCase):
def test7(self): def test7(self):
self.setHandlers(["CommentHandler", "EndElementHandler", self.setHandlers(["CommentHandler", "EndElementHandler",
"StartElementHandler"]) "StartElementHandler"])
self.parser.Parse(b"<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", 1) self.parser.Parse(b"<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", True)
self.assertEqual(self.stuff, self.assertEqual(self.stuff,
["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3",
"<!--abc-->", "4", "<!--def-->", "5", "</a>"], "<!--abc-->", "4", "<!--def-->", "5", "</a>"],
@ -451,7 +451,7 @@ class HandlerExceptionTest(unittest.TestCase):
parser = expat.ParserCreate() parser = expat.ParserCreate()
parser.StartElementHandler = self.StartElementHandler parser.StartElementHandler = self.StartElementHandler
try: try:
parser.Parse(b"<a><b><c/></b></a>", 1) parser.Parse(b"<a><b><c/></b></a>", True)
self.fail() self.fail()
except RuntimeError as e: except RuntimeError as e:
self.assertEqual(e.args[0], 'a', self.assertEqual(e.args[0], 'a',
@ -499,7 +499,7 @@ class PositionTest(unittest.TestCase):
('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)] ('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)]
xml = b'<a>\n <b>\n <c/>\n </b>\n</a>' xml = b'<a>\n <b>\n <c/>\n </b>\n</a>'
self.parser.Parse(xml, 1) self.parser.Parse(xml, True)
class sf1296433Test(unittest.TestCase): class sf1296433Test(unittest.TestCase):
@ -579,7 +579,7 @@ class ChardataBufferTest(unittest.TestCase):
# Parse one chunk of XML # Parse one chunk of XML
self.n = 0 self.n = 0
parser.Parse(xml1, 0) parser.Parse(xml1, False)
self.assertEqual(parser.buffer_size, 1024) self.assertEqual(parser.buffer_size, 1024)
self.assertEqual(self.n, 1) self.assertEqual(self.n, 1)
@ -588,13 +588,13 @@ class ChardataBufferTest(unittest.TestCase):
self.assertFalse(parser.buffer_text) self.assertFalse(parser.buffer_text)
self.assertEqual(parser.buffer_size, 1024) self.assertEqual(parser.buffer_size, 1024)
for i in range(10): for i in range(10):
parser.Parse(xml2, 0) parser.Parse(xml2, False)
self.assertEqual(self.n, 11) self.assertEqual(self.n, 11)
parser.buffer_text = 1 parser.buffer_text = 1
self.assertTrue(parser.buffer_text) self.assertTrue(parser.buffer_text)
self.assertEqual(parser.buffer_size, 1024) self.assertEqual(parser.buffer_size, 1024)
parser.Parse(xml3, 1) parser.Parse(xml3, True)
self.assertEqual(self.n, 12) self.assertEqual(self.n, 12)
def counting_handler(self, text): def counting_handler(self, text):
@ -621,10 +621,10 @@ class ChardataBufferTest(unittest.TestCase):
self.assertEqual(parser.buffer_size, 1024) self.assertEqual(parser.buffer_size, 1024)
self.n = 0 self.n = 0
parser.Parse(xml1, 0) parser.Parse(xml1, False)
parser.buffer_size *= 2 parser.buffer_size *= 2
self.assertEqual(parser.buffer_size, 2048) self.assertEqual(parser.buffer_size, 2048)
parser.Parse(xml2, 1) parser.Parse(xml2, True)
self.assertEqual(self.n, 2) self.assertEqual(self.n, 2)
def test_change_size_2(self): def test_change_size_2(self):
@ -637,10 +637,10 @@ class ChardataBufferTest(unittest.TestCase):
self.assertEqual(parser.buffer_size, 2048) self.assertEqual(parser.buffer_size, 2048)
self.n=0 self.n=0
parser.Parse(xml1, 0) parser.Parse(xml1, False)
parser.buffer_size = parser.buffer_size // 2 parser.buffer_size = parser.buffer_size // 2
self.assertEqual(parser.buffer_size, 1024) self.assertEqual(parser.buffer_size, 1024)
parser.Parse(xml2, 1) parser.Parse(xml2, True)
self.assertEqual(self.n, 4) self.assertEqual(self.n, 4)
class MalformedInputTest(unittest.TestCase): class MalformedInputTest(unittest.TestCase):

View File

@ -204,11 +204,11 @@ class ExpatBuilder:
buffer = file.read(16*1024) buffer = file.read(16*1024)
if not buffer: if not buffer:
break break
parser.Parse(buffer, 0) parser.Parse(buffer, False)
if first_buffer and self.document.documentElement: if first_buffer and self.document.documentElement:
self._setup_subset(buffer) self._setup_subset(buffer)
first_buffer = False first_buffer = False
parser.Parse("", True) parser.Parse(b"", True)
except ParseEscape: except ParseEscape:
pass pass
doc = self.document doc = self.document
@ -637,7 +637,7 @@ class FragmentBuilder(ExpatBuilder):
nsattrs = self._getNSattrs() # get ns decls from node's ancestors nsattrs = self._getNSattrs() # get ns decls from node's ancestors
document = _FRAGMENT_BUILDER_TEMPLATE % (ident, subset, nsattrs) document = _FRAGMENT_BUILDER_TEMPLATE % (ident, subset, nsattrs)
try: try:
parser.Parse(document, 1) parser.Parse(document, True)
except: except:
self.reset() self.reset()
raise raise
@ -697,7 +697,7 @@ class FragmentBuilder(ExpatBuilder):
self.fragment = self.document.createDocumentFragment() self.fragment = self.document.createDocumentFragment()
self.curNode = self.fragment self.curNode = self.fragment
try: try:
parser.Parse(self._source, 1) parser.Parse(self._source, True)
finally: finally:
self.curNode = old_cur_node self.curNode = old_cur_node
self.document = old_document self.document = old_document

View File

@ -1710,14 +1710,14 @@ class XMLParser:
def feed(self, data): def feed(self, data):
"""Feed encoded data to parser.""" """Feed encoded data to parser."""
try: try:
self.parser.Parse(data, 0) self.parser.Parse(data, False)
except self._error as v: except self._error as v:
self._raiseerror(v) self._raiseerror(v)
def close(self): def close(self):
"""Finish feeding data to parser and return element structure.""" """Finish feeding data to parser and return element structure."""
try: try:
self.parser.Parse("", 1) # end of data self.parser.Parse(b"", True) # end of data
except self._error as v: except self._error as v:
self._raiseerror(v) self._raiseerror(v)
try: try:

View File

@ -93,7 +93,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self._parser = None self._parser = None
self._namespaces = namespaceHandling self._namespaces = namespaceHandling
self._lex_handler_prop = None self._lex_handler_prop = None
self._parsing = 0 self._parsing = False
self._entity_stack = [] self._entity_stack = []
self._external_ges = 0 self._external_ges = 0
self._interning = None self._interning = None
@ -203,10 +203,10 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
# IncrementalParser methods # IncrementalParser methods
def feed(self, data, isFinal = 0): def feed(self, data, isFinal=False):
if not self._parsing: if not self._parsing:
self.reset() self.reset()
self._parsing = 1 self._parsing = True
self._cont_handler.startDocument() self._cont_handler.startDocument()
try: try:
@ -237,13 +237,13 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
# If we are completing an external entity, do nothing here # If we are completing an external entity, do nothing here
return return
try: try:
self.feed("", isFinal = 1) self.feed(b"", isFinal=True)
self._cont_handler.endDocument() self._cont_handler.endDocument()
self._parsing = 0 self._parsing = False
# break cycle created by expat handlers pointing to our methods # break cycle created by expat handlers pointing to our methods
self._parser = None self._parser = None
finally: finally:
self._parsing = 0 self._parsing = False
if self._parser is not None: if self._parser is not None:
# Keep ErrorColumnNumber and ErrorLineNumber after closing. # Keep ErrorColumnNumber and ErrorLineNumber after closing.
parser = _ClosedParser() parser = _ClosedParser()
@ -307,7 +307,7 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
self._parser.SetParamEntityParsing( self._parser.SetParamEntityParsing(
expat.XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE) expat.XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)
self._parsing = 0 self._parsing = False
self._entity_stack = [] self._entity_stack = []
# Locator methods # Locator methods

View File

@ -442,7 +442,7 @@ class ExpatParser:
target.xml(encoding, None) target.xml(encoding, None)
def feed(self, data): def feed(self, data):
self._parser.Parse(data, 0) self._parser.Parse(data, False)
def close(self): def close(self):
try: try: