-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
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 all commits
6ecfc28
e9ac454
9c365c2
2fc666a
fe9d82f
be878cb
0e3f870
c88e7ed
27cb273
91112b9
d3cbe5f
1d343f1
256a97b
7c0da60
e219450
34af031
8075f8b
2004846
c3e00e2
d3b08ec
e8bcb30
dfff704
7de4ce5
08d659e
7cdc402
cd7de73
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 |
|---|---|---|
|
|
@@ -89,6 +89,7 @@ def __init__(self, namespaceHandling=0, bufsize=2**16-20): | |
| self._entity_stack = [] | ||
| self._external_ges = 0 | ||
| self._interning = None | ||
| self._namespace_prefixes = 0 | ||
|
|
||
| # XMLReader methods | ||
|
|
||
|
|
@@ -124,8 +125,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 | ||
|
|
@@ -154,9 +156,9 @@ def setFeature(self, name, 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). |
||
| if state and not self._namespaces: | ||
| raise SAXException(f"{feature_namespace_prefixes} requires {feature_namespaces} to be enabled") | ||
| self._namespace_prefixes = state | ||
|
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. @ukarroum I wonder what the expected behaviors is here when
Just thinking aloud.
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. good point, I think it will make sense to just enable feature_namespaces when feature_namespace_prefixes is also enabled (and also disable feature_namespace_prefixes if feature_namespaces is disabled).
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. actually one drawback I see for this is : enable feature_namespace_prefixes this will result in feature_namespace_prefixes being disabled which feels odd.
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 went with 1) instead.
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. @ukarroum I like it. I think one small dedicated test for that would be great where namespaces are disabled and the exception is raise with the expected message. (Arguably, the success case is already covered by existing tests.)
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. added test |
||
| else: | ||
| raise SAXNotRecognizedException( | ||
| "Feature '%s' not recognized" % name) | ||
|
|
@@ -345,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 = {} | ||
|
|
@@ -372,7 +377,10 @@ def start_element_ns(self, name, attrs): | |
| newattrs[apair] = value | ||
| qnames[apair] = qname | ||
|
|
||
| self._cont_handler.startElementNS(pair, None, | ||
| if not self._namespace_prefixes: | ||
| elem_qname = None | ||
|
|
||
| self._cont_handler.startElementNS(pair, elem_qname, | ||
|
ukarroum marked this conversation as resolved.
|
||
| AttributesNSImpl(newattrs, qnames)) | ||
|
|
||
| def end_element_ns(self, name): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Backported namespaces prefixes support for xml.sax.expatreader from PyXML | ||
| (0.8.4) |

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.
Don't leave 3 blank lines, only 2 is sufficient.
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.
Fixed