MCPcopy Create free account
hub / github.com/pytest-dev/pytest / bin_xml_escape

Function bin_xml_escape

src/_pytest/junitxml.py:40–64  ·  view source on GitHub ↗

r"""Visually escape invalid XML characters. For example, transforms 'hello\aworld\b' into 'hello#x07world#x08' Note that the #xABs are *not* XML escapes - missing the ampersand &#xAB. The idea is to escape visually for the user rather than for XML itself.

(arg: object)

Source from the content-addressed store, hash-verified

38
39
40def bin_xml_escape(arg: object) -> str:
41 r"""Visually escape invalid XML characters.
42
43 For example, transforms
44 'hello\aworld\b'
45 into
46 'hello#x07world#x08'
47 Note that the #xABs are *not* XML escapes - missing the ampersand &#xAB.
48 The idea is to escape visually for the user rather than for XML itself.
49 """
50
51 def repl(matchobj: Match[str]) -> str:
52 i = ord(matchobj.group())
53 if i <= 0xFF:
54 return "#x%02X" % i
55 else:
56 return "#x%04X" % i
57
58 # The spec range of valid chars is:
59 # Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
60 # For an unknown(?) reason, we disallow #x7F (DEL) as well.
61 illegal_xml_re = (
62 "[^\u0009\u000A\u000D\u0020-\u007E\u0080-\uD7FF\uE000-\uFFFD\u10000-\u10FFFF]"
63 )
64 return re.sub(illegal_xml_re, repl, str(arg))
65
66
67def merge_family(left, right) -> None:

Callers 9

test_invalid_xml_escapeFunction · 0.90
add_propertyMethod · 0.85
add_attributeMethod · 0.85
record_testreportMethod · 0.85
_add_simpleMethod · 0.85
_write_contentMethod · 0.85
append_failureMethod · 0.85
append_skippedMethod · 0.85
add_global_propertyMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_invalid_xml_escapeFunction · 0.72