Uh oh!
There was an error while loading. Please reload this page.
gh-139489: Add xml.is_valid_text() - #149412
Conversation
Documentation build overview
|
vstinner
commented
May 5, 2026
What do you think of adding also a sanitize function which takes a callback? Example: importreILLEGAL_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']+')
defsanitize(text, replace_func):
defcallback(regs):
returnreplace_func(regs[0])
returnILLEGAL_XML_CHARS_RE.sub(callback, text)
defescape(text):
return''.join(f'\\x{ord(char):02x}'forcharintext)
invalid='\x00'test=f'a{invalid}b'print(sanitize(test, lambdatext: '#'*len(text)))
print(sanitize(test, escape))It would be useful for |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Stan Ulbrych <stan@python.org> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka
commented
May 5, 2026
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. |
Uh oh!
There was an error while loading. Please reload this page.
vstinner
commented
May 6, 2026
Ok, that makes sense. |
…8qqIM.rst Co-authored-by: Victor Stinner <vstinner@python.org>
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.