-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
bpo-6686: Fix Lib.xml.sax.expatreader.GetProperty to return a string object #9715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ad2faee
01146ad
6bd0b47
3c616f7
afd7b3f
b6ce633
26451d2
c2bb9db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -1311,6 +1312,73 @@ def test_nsattrs_wattr(self): | |
| self.assertEqual(attrs.getQNameByName((ns_uri, "attr")), "ns:attr") | ||
|
|
||
|
|
||
| # =========================================================================== | ||
| # | ||
| # Sax parser property tests | ||
| # | ||
| # Currently only tests the condition reported in issue bpo-6686 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
@@ -1323,7 +1391,9 @@ def test_main(): | |
| StreamReaderWriterXmlgenTest, | ||
| ExpatReaderTest, | ||
| ErrorReportingTest, | ||
| XmlReaderTest) | ||
| XmlReaderTest, | ||
| SaxPropertyTest) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_main() | ||
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.