Skip to content
74 changes: 72 additions & 2 deletions Lib/test/test_sax.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
XMLFilterBase, prepare_input_source
from xml.sax.expatreader import create_parser
from xml.sax.handler import feature_namespaces, feature_external_ges
from xml.sax.handler import feature_namespaces, feature_external_ges,\
property_xml_string
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
from io import BytesIO, StringIO
import codecs
Expand Down Expand Up @@ -1311,6 +1312,73 @@ def test_nsattrs_wattr(self):
self.assertEqual(attrs.getQNameByName((ns_uri, "attr")), "ns:attr")


# ===========================================================================

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

Could you remove these comments, they don't need to be in the source code.
Thank you

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the rest of this test module, you'll see that it is in keeping with its existing style. IMO this is fine.

#
# Sax parser property tests
#
# Currently only tests the condition reported in issue bpo-6686

@taleinat taleinat Oct 7, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note (the second line) shouldn't be in this comment block though, it should be in the class itself.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you point me at something that discusses this idiom? I don't understand the rationale.

#
# ===========================================================================

class PropertyContentHandler(ContentHandler):
def __init__(self, test_harness, reader, test, *args, **kwargs):
super().__init__(*args, **kwargs)
self.test_harness = test_harness
self.reader = reader
self.test_data = test

def startElement(self, name, attr):
property_ = self.reader.getProperty(property_xml_string)
self.test_harness.assertIsInstance(property_, bytes)
if self.test_harness.test_data is not None:
prop = property_
self.test_harness\
.assertEqual(prop.decode(encoding=self.test_data[0]),
self.test_data[1][1])
super().startElement(name, attr)


class SaxPropertyTest(unittest.TestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.result = None
self.test_data = [['ascii', ['Hello']],
['utf-8', ['abc˦']],
['iso-8859-1', ['ghiéñ']]]
for t in self.test_data:
d = '<test>{}</test>\n'.format(t[1][0])
t[1].append(d)

def test_property_xml_string_from_bytes(self):
for prolog in (True, False):
for t in self.test_data:
reader = create_parser()
reader.setContentHandler(PropertyContentHandler(self,
reader,
t))
source = InputSource()
data = b''
if prolog:
data += b'<?xml version="1.0" encoding="'
data += '{}'.format(t[0]).encode(encoding='ascii')
data += b'"?>\n'
data += t[1][1].encode(t[0])
source.setByteStream(BytesIO(data))
if not prolog:
source.setEncoding(t[0])
reader.parse(source)
pass

def test_property_xml_str_from_str(self):
self.test_data = None
reader = create_parser()
reader.setContentHandler(PropertyContentHandler(self,
reader,
None))
in_ = '<?xml version="1.0" encoding="UTF-8"?>\n<test>Hi there</test>'
reader.parse(StringIO(in_))


def test_main():
run_unittest(MakeParserTest,
ParseTest,
Expand All @@ -1323,7 +1391,9 @@ def test_main():
StreamReaderWriterXmlgenTest,
ExpatReaderTest,
ErrorReportingTest,
XmlReaderTest)
XmlReaderTest,
SaxPropertyTest)


if __name__ == "__main__":
test_main()
6 changes: 4 additions & 2 deletions Lib/xml/sax/expatreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
pyexpat.__version__ == '2.22'.
"""

version = "0.20"

from xml.sax._exceptions import *
from xml.sax.handler import feature_validation, feature_namespaces
from xml.sax.handler import feature_namespace_prefixes
Expand All @@ -27,6 +25,8 @@
raise SAXReaderNotAvailable("expat not supported", None)
from xml.sax import xmlreader, saxutils, handler

version = "0.20"

AttributesImpl = xmlreader.AttributesImpl
AttributesNSImpl = xmlreader.AttributesNSImpl

Expand Down Expand Up @@ -266,12 +266,14 @@ def _reset_lex_handler_prop(self):
parser.EndCdataSectionHandler = None
parser.StartDoctypeDeclHandler = None
parser.EndDoctypeDeclHandler = None
parser.XmlDeclHandler = None
else:
parser.CommentHandler = lex.comment
parser.StartCdataSectionHandler = lex.startCDATA
parser.EndCdataSectionHandler = lex.endCDATA
parser.StartDoctypeDeclHandler = self.start_doctype_decl
parser.EndDoctypeDeclHandler = lex.endDTD
parser.XmlDeclHandler = lex.xml_decl_handler

def reset(self):
if self._namespaces:
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ Chris Gonnerman
Shelley Gooch
David Goodger
Elliot Gorokhovsky
Jonathan Gossage
Hans de Graaff
Tim Graham
Kim Gräsman
Expand Down