-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
gh-54873: Add support for namespaces prefixes to xml.sax.expatreader
#118317
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
6ecfc28
e9ac454
9c365c2
2fc666a
fe9d82f
be878cb
0e3f870
c88e7ed
27cb273
91112b9
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 | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||||||
| # regression test for SAX 2.0 | ||||||||
|
|
||||||||
| from xml.sax import make_parser, ContentHandler, \ | ||||||||
| SAXException, SAXReaderNotAvailable, SAXParseException | ||||||||
| SAXException, SAXReaderNotAvailable, SAXParseException, handler | ||||||||
|
Member
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. Why import handler when ContentHandler is already imported? why not use the latter directly?
Contributor
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. I replaced the
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. How about?:
Suggested change
|
||||||||
| import unittest | ||||||||
| from unittest import mock | ||||||||
| try: | ||||||||
|
|
@@ -1307,6 +1307,38 @@ def test_expat_locator_withinfo_nonascii(self): | |||||||
| self.assertEqual(parser.getSystemId(), fname) | ||||||||
| self.assertEqual(parser.getPublicId(), None) | ||||||||
|
|
||||||||
| def test_namespace_prefix(self): | ||||||||
| parser = create_parser() | ||||||||
| parser.setFeature(handler.feature_namespaces, 1) | ||||||||
| parser.setFeature(handler.feature_namespace_prefixes, 1) | ||||||||
|
|
||||||||
| class Handler(handler.ContentHandler): | ||||||||
| def startElementNS(self, name, qname, attrs): | ||||||||
| self.qname = qname | ||||||||
|
|
||||||||
| h = Handler() | ||||||||
|
|
||||||||
| parser.setContentHandler(h) | ||||||||
| parser.feed("<Q:E xmlns:Q='http://example.org/testuri'/>") | ||||||||
| parser.close() | ||||||||
| self.assertEqual(h.qname, "Q:E") | ||||||||
|
|
||||||||
| def test_default_namespace(self): | ||||||||
| parser = create_parser() | ||||||||
| parser.setFeature(handler.feature_namespaces, 1) | ||||||||
|
|
||||||||
| class Handler(handler.ContentHandler): | ||||||||
| def startElementNS(self, name, qname, attrs): | ||||||||
| self.qname = qname | ||||||||
|
|
||||||||
| h = Handler() | ||||||||
|
|
||||||||
| parser.setContentHandler(h) | ||||||||
| parser.feed("<E xmlns='http://example.org/testuri'/>") | ||||||||
| parser.close() | ||||||||
|
||||||||
| self.assertEqual(h.qname, "E") | ||||||||
|
|
||||||||
|
Member
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. Don't leave 3 blank lines, only 2 is sufficient.
Contributor
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. Fixed |
||||||||
|
|
||||||||
|
|
||||||||
| # =========================================================================== | ||||||||
| # | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -91,6 +91,7 @@ def __init__(self, namespaceHandling=0, bufsize=2**16-20): | |||||||||||
| self._entity_stack = [] | ||||||||||||
| self._external_ges = 0 | ||||||||||||
| self._interning = None | ||||||||||||
| self._namespace_prefixes = 1 | ||||||||||||
|
||||||||||||
|
|
||||||||||||
| # XMLReader methods | ||||||||||||
|
|
||||||||||||
|
|
@@ -126,8 +127,9 @@ def getFeature(self, name): | |||||||||||
| return self._namespaces | ||||||||||||
| elif name == feature_string_interning: | ||||||||||||
| return self._interning is not None | ||||||||||||
| elif name in (feature_validation, feature_external_pes, | ||||||||||||
| feature_namespace_prefixes): | ||||||||||||
| elif name == feature_namespace_prefixes: | ||||||||||||
| return self._namespace_prefixes | ||||||||||||
| elif name in (feature_validation, feature_external_pes): | ||||||||||||
| return 0 | ||||||||||||
| elif name == feature_external_ges: | ||||||||||||
| return self._external_ges | ||||||||||||
|
|
@@ -147,6 +149,8 @@ def setFeature(self, name, state): | |||||||||||
| self._interning = {} | ||||||||||||
| else: | ||||||||||||
| self._interning = None | ||||||||||||
| elif name == feature_namespace_prefixes: | ||||||||||||
| self._namespace_prefixes = state | ||||||||||||
|
Comment on lines
+152
to
+153
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. My vote for moving this one further down — right after |
||||||||||||
| elif name == feature_validation: | ||||||||||||
| if state: | ||||||||||||
| raise SAXNotSupportedException( | ||||||||||||
|
|
@@ -155,10 +159,6 @@ def setFeature(self, name, state): | |||||||||||
| if state: | ||||||||||||
| raise SAXNotSupportedException( | ||||||||||||
| "expat does not read external parameter entities") | ||||||||||||
| elif name == feature_namespace_prefixes: | ||||||||||||
| if state: | ||||||||||||
| raise SAXNotSupportedException( | ||||||||||||
| "expat does not report namespace prefixes") | ||||||||||||
|
Member
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. Was it a limitation from the C extension module? was it a Expat version limitation?
Contributor
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. I did a quick git blame, and looks like this specific check was added in this commit: 18476a3 (from 2002). |
||||||||||||
| else: | ||||||||||||
| raise SAXNotRecognizedException( | ||||||||||||
| "Feature '%s' not recognized" % name) | ||||||||||||
|
|
@@ -347,11 +347,14 @@ def start_element_ns(self, name, attrs): | |||||||||||
| pair = name.split() | ||||||||||||
| if len(pair) == 1: | ||||||||||||
| # no namespace | ||||||||||||
| elem_qname = name | ||||||||||||
|
Member
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. I have no idea whether this is correct or not here. Is there some specs that we could follow for the implementation?
Contributor
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. if you're referring specificaly to elem_qname, there is the spec: https://www.w3.org/TR/xml-names/#ns-qualnames.
Contributor
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. I also added another test to test the "else" branch.
Member
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. Thanks!
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. @picnixz these lines are 1:1 from PyXML 0.8.4:
|
||||||||||||
| pair = (None, name) | ||||||||||||
| elif len(pair) == 3: | ||||||||||||
| elem_qname = "%s:%s" % (pair[2], pair[1]) | ||||||||||||
|
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. Note to self: This is the key value provider in this pull request. |
||||||||||||
| pair = pair[0], pair[1] | ||||||||||||
| else: | ||||||||||||
| # default namespace | ||||||||||||
| elem_qname = pair[1] | ||||||||||||
| pair = tuple(pair) | ||||||||||||
|
|
||||||||||||
| newattrs = {} | ||||||||||||
|
|
@@ -374,7 +377,7 @@ def start_element_ns(self, name, attrs): | |||||||||||
| newattrs[apair] = value | ||||||||||||
| qnames[apair] = qname | ||||||||||||
|
|
||||||||||||
| self._cont_handler.startElementNS(pair, None, | ||||||||||||
| self._cont_handler.startElementNS(pair, elem_qname, | ||||||||||||
|
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 will never pass The following patch is arguably a bit silly, but it keeps things backwards compatible (and demos the idea):
Suggested change
|
||||||||||||
| AttributesNSImpl(newattrs, qnames)) | ||||||||||||
|
|
||||||||||||
| def end_element_ns(self, name): | ||||||||||||
|
|
||||||||||||

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.
This should be also documented in the expat docs.
Uh oh!
There was an error while loading. Please reload this page.
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.
do you mean I should add a link to the C libexpat doc ?
EDIT: I think you probably meant to document this here: https://docs.python.org/3/library/pyexpat.html
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.
No, to the specs
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.
done