Skip to content

Commit

Permalink
bpo-15999: Always pass bool instead of int to the expat parser. (pyth…
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka authored Sep 1, 2019
1 parent 41c57b3 commit eb89746
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
44 changes: 22 additions & 22 deletions Lib/test/test_pyexpat.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def test_parse_bytes(self):
parser = expat.ParserCreate(namespace_separator='!')
self._hookup_callbacks(parser, out)

parser.Parse(data, 1)
parser.Parse(data, True)

operations = out.out
self._verify_parse_output(operations)
Expand All @@ -243,7 +243,7 @@ def test_parse_str(self):
parser = expat.ParserCreate(namespace_separator='!')
self._hookup_callbacks(parser, out)

parser.Parse(data.decode('iso-8859-1'), 1)
parser.Parse(data.decode('iso-8859-1'), True)

operations = out.out
self._verify_parse_output(operations)
Expand Down Expand Up @@ -316,7 +316,7 @@ def collector(name, *args):
L.append(name)
p.StartElementHandler = 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]
self.assertEqual(len(L), 6)
for entry in L:
Expand All @@ -332,14 +332,14 @@ def __init__(self, parser):

def ExternalEntityRefHandler(self, context, base, sysId, pubId):
external_parser = self.parser.ExternalEntityParserCreate("")
self.parser_result = external_parser.Parse(b"", 1)
self.parser_result = external_parser.Parse(b"", True)
return 1

parser = expat.ParserCreate(namespace_separator='!')
parser.buffer_text = 1
out = ExternalOutputter(parser)
parser.ExternalEntityRefHandler = out.ExternalEntityRefHandler
parser.Parse(data, 1)
parser.Parse(data, True)
self.assertEqual(out.parser_result, 1)


Expand Down Expand Up @@ -383,55 +383,55 @@ def test_default_to_disabled(self):
def test_buffering_enabled(self):
# Make sure buffering is turned on
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'],
"buffered text not properly collapsed")

def test1(self):
# XXX This test exposes more detail of Expat's text chunking than we
# XXX like, but it tests what we need to concisely.
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,
["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"],
"buffering control not reacting as expected")

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"],
"buffered text not properly collapsed")

def test3(self):
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"],
"buffered text not properly split")

def test4(self):
self.setHandlers(["StartElementHandler", "EndElementHandler"])
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,
["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"])

def test5(self):
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,
["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"])

def test6(self):
self.setHandlers(["CommentHandler", "EndElementHandler",
"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,
["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"],
"buffered text not properly split")

def test7(self):
self.setHandlers(["CommentHandler", "EndElementHandler",
"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,
["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3",
"<!--abc-->", "4", "<!--def-->", "5", "</a>"],
Expand All @@ -451,7 +451,7 @@ def test_exception(self):
parser = expat.ParserCreate()
parser.StartElementHandler = self.StartElementHandler
try:
parser.Parse(b"<a><b><c/></b></a>", 1)
parser.Parse(b"<a><b><c/></b></a>", True)
self.fail()
except RuntimeError as e:
self.assertEqual(e.args[0], 'a',
Expand Down Expand Up @@ -499,7 +499,7 @@ def test(self):
('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)]

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):
Expand Down Expand Up @@ -579,7 +579,7 @@ def test_disabling_buffer(self):

# Parse one chunk of XML
self.n = 0
parser.Parse(xml1, 0)
parser.Parse(xml1, False)
self.assertEqual(parser.buffer_size, 1024)
self.assertEqual(self.n, 1)

Expand All @@ -588,13 +588,13 @@ def test_disabling_buffer(self):
self.assertFalse(parser.buffer_text)
self.assertEqual(parser.buffer_size, 1024)
for i in range(10):
parser.Parse(xml2, 0)
parser.Parse(xml2, False)
self.assertEqual(self.n, 11)

parser.buffer_text = 1
self.assertTrue(parser.buffer_text)
self.assertEqual(parser.buffer_size, 1024)
parser.Parse(xml3, 1)
parser.Parse(xml3, True)
self.assertEqual(self.n, 12)

def counting_handler(self, text):
Expand All @@ -621,10 +621,10 @@ def test_change_size_1(self):
self.assertEqual(parser.buffer_size, 1024)

self.n = 0
parser.Parse(xml1, 0)
parser.Parse(xml1, False)
parser.buffer_size *= 2
self.assertEqual(parser.buffer_size, 2048)
parser.Parse(xml2, 1)
parser.Parse(xml2, True)
self.assertEqual(self.n, 2)

def test_change_size_2(self):
Expand All @@ -637,10 +637,10 @@ def test_change_size_2(self):
self.assertEqual(parser.buffer_size, 2048)

self.n=0
parser.Parse(xml1, 0)
parser.Parse(xml1, False)
parser.buffer_size = parser.buffer_size // 2
self.assertEqual(parser.buffer_size, 1024)
parser.Parse(xml2, 1)
parser.Parse(xml2, True)
self.assertEqual(self.n, 4)

class MalformedInputTest(unittest.TestCase):
Expand Down
8 changes: 4 additions & 4 deletions Lib/xml/dom/expatbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ def parseFile(self, file):
buffer = file.read(16*1024)
if not buffer:
break
parser.Parse(buffer, 0)
parser.Parse(buffer, False)
if first_buffer and self.document.documentElement:
self._setup_subset(buffer)
first_buffer = False
parser.Parse("", True)
parser.Parse(b"", True)
except ParseEscape:
pass
doc = self.document
Expand Down Expand Up @@ -637,7 +637,7 @@ def parseString(self, string):
nsattrs = self._getNSattrs() # get ns decls from node's ancestors
document = _FRAGMENT_BUILDER_TEMPLATE % (ident, subset, nsattrs)
try:
parser.Parse(document, 1)
parser.Parse(document, True)
except:
self.reset()
raise
Expand Down Expand Up @@ -697,7 +697,7 @@ def external_entity_ref_handler(self, context, base, systemId, publicId):
self.fragment = self.document.createDocumentFragment()
self.curNode = self.fragment
try:
parser.Parse(self._source, 1)
parser.Parse(self._source, True)
finally:
self.curNode = old_cur_node
self.document = old_document
Expand Down
4 changes: 2 additions & 2 deletions Lib/xml/etree/ElementTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1710,14 +1710,14 @@ def _default(self, text):
def feed(self, data):
"""Feed encoded data to parser."""
try:
self.parser.Parse(data, 0)
self.parser.Parse(data, False)
except self._error as v:
self._raiseerror(v)

def close(self):
"""Finish feeding data to parser and return element structure."""
try:
self.parser.Parse("", 1) # end of data
self.parser.Parse(b"", True) # end of data
except self._error as v:
self._raiseerror(v)
try:
Expand Down
14 changes: 7 additions & 7 deletions Lib/xml/sax/expatreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self, namespaceHandling=0, bufsize=2**16-20):
self._parser = None
self._namespaces = namespaceHandling
self._lex_handler_prop = None
self._parsing = 0
self._parsing = False
self._entity_stack = []
self._external_ges = 0
self._interning = None
Expand Down Expand Up @@ -203,10 +203,10 @@ def setProperty(self, name, value):

# IncrementalParser methods

def feed(self, data, isFinal = 0):
def feed(self, data, isFinal=False):
if not self._parsing:
self.reset()
self._parsing = 1
self._parsing = True
self._cont_handler.startDocument()

try:
Expand Down Expand Up @@ -237,13 +237,13 @@ def close(self):
# If we are completing an external entity, do nothing here
return
try:
self.feed("", isFinal = 1)
self.feed(b"", isFinal=True)
self._cont_handler.endDocument()
self._parsing = 0
self._parsing = False
# break cycle created by expat handlers pointing to our methods
self._parser = None
finally:
self._parsing = 0
self._parsing = False
if self._parser is not None:
# Keep ErrorColumnNumber and ErrorLineNumber after closing.
parser = _ClosedParser()
Expand Down Expand Up @@ -307,7 +307,7 @@ def reset(self):
self._parser.SetParamEntityParsing(
expat.XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE)

self._parsing = 0
self._parsing = False
self._entity_stack = []

# Locator methods
Expand Down
2 changes: 1 addition & 1 deletion Lib/xmlrpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def __init__(self, target):
target.xml(encoding, None)

def feed(self, data):
self._parser.Parse(data, 0)
self._parser.Parse(data, False)

def close(self):
try:
Expand Down

0 comments on commit eb89746

Please sign in to comment.