FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-139489: Add xml.is_valid_text() by serhiy-storchaka · Pull Request #149412 · python/cpython · GitHub

Repository navigation

gh-139489: Add xml.is_valid_text() - #149412

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:xml-is_valid_text-1.0
May 6, 2026
Merged

serhiy-storchaka merged 3 commits into
python:mainfrom
serhiy-storchaka:xml-is_valid_text-1.0

Conversation

serhiy-storchaka commented May 5, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

read-the-docs-community Bot commented May 5, 2026 •
edited
Loading

Copy link
Copy Markdown

vstinner commented May 5, 2026

Copy link
Copy Markdown
Member

What do you think of adding also a sanitize function which takes a callback? Example:

import re

ILLEGAL_XML_CHARS_RE = re.compile(
    '['
    # Control characters; newline (\x0A and \x0D) and TAB (\x09) are legal
    '\x00-\x08\x0B\x0C\x0E-\x1F'
    # Surrogate characters
    '\uD800-\uDFFF'
    # Special Unicode characters
    '\uFFFE'
    '\uFFFF'
    # Match multiple sequential invalid characters for better efficiency
    ']+')

def sanitize(text, replace_func):
    def callback(regs):
        return replace_func(regs[0])

    return ILLEGAL_XML_CHARS_RE.sub(callback, text)

def escape(text):
    return ''.join(f'\\x{ord(char):02x}' for char in text)

invalid = '\x00'
test = f'a{invalid}b'
print(sanitize(test, lambda text: '#' * len(text)))
print(sanitize(test, escape))

It would be useful for sanitize_xml() of Lib/test/libregrtest/utils.py.

Comment thread Doc/library/xml.rst Outdated
Comment thread Doc/library/xml.rst Outdated
Comment thread Doc/library/xml.rst Outdated
Co-authored-by: Stan Ulbrych <stan@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>

Copy link
Copy Markdown
Member Author

What do you think of adding also a sanitize function which takes a callback?

I left it for a separate PR. I have similar function in a different branch. But it accepts also the name of the registered error handler ('strict', 'ignore', 'replace, 'backslashreplace', etc) and the callback has different interface. It needs a separate discussion.

vstinner left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

vstinner commented May 6, 2026

Copy link
Copy Markdown
Member

I left it for a separate PR. (...). It needs a separate discussion.

Ok, that makes sense.

…8qqIM.rst

Co-authored-by: Victor Stinner <vstinner@python.org>
serhiy-storchaka enabled auto-merge (squash) May 6, 2026 14:11
serhiy-storchaka merged commit a5c7a74 into python:main May 6, 2026
55 checks passed
serhiy-storchaka deleted the xml-is_valid_text-1.0 branch July 1, 2026 16:47
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL