MCPcopy Index your code
hub / github.com/python/cpython / canonicalize

Function canonicalize

Lib/xml/etree/ElementTree.py:1761–1788  ·  view source on GitHub ↗

Convert XML to its C14N 2.0 serialised form. If *out* is provided, it must be a file or file-like object that receives the serialised canonical XML output (text, not bytes) through its ``.write()`` method. To write to a file, open it in text mode with encoding "utf-8". If *out* is

(xml_data=None, *, out=None, from_file=None, **options)

Source from the content-addressed store, hash-verified

1759# C14N 2.0
1760
1761def canonicalize(xml_data=None, *, out=None, from_file=None, **options):
1762 """Convert XML to its C14N 2.0 serialised form.
1763
1764 If *out* is provided, it must be a file or file-like object that receives
1765 the serialised canonical XML output (text, not bytes) through its ``.write()``
1766 method. To write to a file, open it in text mode with encoding "utf-8".
1767 If *out* is not provided, this function returns the output as text string.
1768
1769 Either *xml_data* (an XML string) or *from_file* (a file path or
1770 file-like object) must be provided as input.
1771
1772 The configuration options are the same as for the ``C14NWriterTarget``.
1773 """
1774 if xml_data is None and from_file is None:
1775 raise ValueError("Either 'xml_data' or 'from_file' must be provided as input")
1776 sio = None
1777 if out is None:
1778 sio = out = io.StringIO()
1779
1780 parser = XMLParser(target=C14NWriterTarget(out.write, **options))
1781
1782 if xml_data is not None:
1783 parser.feed(xml_data)
1784 parser.close()
1785 elif from_file is not None:
1786 parse(from_file, parser=parser)
1787
1788 return sio.getvalue() if sio is not None else None
1789
1790
1791_looks_like_prefix_name = re.compile(r'^\w+:\w+$', re.UNICODE).match

Callers

nothing calls this directly

Calls 6

feedMethod · 0.95
closeMethod · 0.95
XMLParserClass · 0.85
C14NWriterTargetClass · 0.85
parseFunction · 0.70
getvalueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…