MCPcopy
hub / github.com/pytest-dev/pytest / bin_xml_escape

Function bin_xml_escape

src/_pytest/junitxml.py:37–59  ·  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

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

Callers 10

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_errorMethod · 0.85
append_skippedMethod · 0.85
add_global_propertyMethod · 0.85

Calls

no outgoing calls

Tested by 1

test_invalid_xml_escapeFunction · 0.72