-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
gh-139489: Add xml.is_valid_name() #139768
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
Merged
serhiy-storchaka
merged 9 commits into
python:main
from
serhiy-storchaka:xml-sax-utils-is_valid_name
May 5, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c7cbb61
gh-139489: Add xml.sax.utils.is_valid_name()
serhiy-storchaka 4f37eed
Fix references.
serhiy-storchaka 6ac604e
dd
serhiy-storchaka e814cfa
Move to the toplevel xml module.
serhiy-storchaka bc26874
Use possesive quantifier for optimization.
serhiy-storchaka b631754
Merge branch 'main' into xml-sax-utils-is_valid_name
serhiy-storchaka 8db41b9
Merge branch 'main' into xml-sax-utils-is_valid_name
serhiy-storchaka 7e96492
Apply suggestions from code review
serhiy-storchaka 3245a65
Import re as _re.
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import xml | ||
| import unittest | ||
|
|
||
|
|
||
| class TestUtils(unittest.TestCase): | ||
|
|
||
| def test_is_valid_name(self): | ||
| is_valid_name = xml.is_valid_name | ||
| self.assertFalse(is_valid_name('')) | ||
| self.assertTrue(is_valid_name('name')) | ||
| self.assertTrue(is_valid_name('NAME')) | ||
| self.assertTrue(is_valid_name('name0:-._·')) | ||
| self.assertTrue(is_valid_name('_')) | ||
| self.assertTrue(is_valid_name(':')) | ||
| self.assertTrue(is_valid_name('Ñàḿĕ')) | ||
| self.assertTrue(is_valid_name('\U000EFFFF')) | ||
| self.assertFalse(is_valid_name('0')) | ||
| self.assertFalse(is_valid_name('-')) | ||
| self.assertFalse(is_valid_name('.')) | ||
| self.assertFalse(is_valid_name('·')) | ||
| self.assertFalse(is_valid_name('na me')) | ||
| for c in '<>/!?=\x00\x01\x7f\ud800\udfff\ufffe\uffff\U000F0000': | ||
| self.assertFalse(is_valid_name('name' + c)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| lazy import re as _re | ||
|
|
||
|
|
||
|
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'd suggest adding an
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. It's needed. Currently, the $ ./python
>>> import xml
>>> xml.re
<module 're' from '/home/vstinner/python/main/Lib/re/__init__.py'> |
||
| def is_valid_name(name): | ||
| """Test whether a string is a valid element or attribute name.""" | ||
| # https://www.w3.org/TR/xml/#NT-Name | ||
| return _re.fullmatch( | ||
| # NameStartChar | ||
| '[' | ||
| ':A-Z_a-z' | ||
| '\xC0-\xD6\xD8-\xF6\xF8-\u02FF\u0370-\u037D\u037F-\u1FFF' | ||
| '\u200C\u200D' | ||
| '\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF' | ||
| '\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF' | ||
| ']' | ||
| # NameChar | ||
| '[' | ||
| r'\-.0-9:A-Z_a-z' | ||
| '\xB7' | ||
| '\xC0-\xD6\xD8-\xF6\xF8-\u037D\u037F-\u1FFF' | ||
| '\u200C\u200D\u203F\u2040' | ||
| '\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF' | ||
| '\uF900-\uFDCF\uFDF0-\uFFFD\U00010000-\U000EFFFF' | ||
| ']*+', | ||
| name) is not None | ||
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2025-10-08-15-36-00.gh-issue-139489.W46tvn.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add the :func:`xml.is_valid_name` function, which allows to check | ||
| whether a string can be used as an element or attribute name in XML. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe add a link to the specs if any?
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.
Link found in the implementation:
https://www.w3.org/TR/xml/#NT-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.
I think it would be good to add it in the online docs as well. It'll be more accessible.
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.
I am going to add several more functions. Some functions will need several links, and some links will be repeated for different functions. If we want to add links, it is better to add root links to the XML specifications at the top of the module documentation.
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.
That's also ok. As long as the online docs contain some links to some specifications, it's ok.