summaryrefslogtreecommitdiff
path: root/slixmpp/xmlstream/tostring.py
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2021-07-15 10:01:03 +0200
committermathieui <mathieui@mathieui.net>2021-07-15 10:01:03 +0200
commit22fa8bc4d91d38a1176b09d03e0d41313b1adcaa (patch)
tree7344f9e7d72b913e9dfef29fe9b926364e7db587 /slixmpp/xmlstream/tostring.py
parentb1411d8ed79792c6839f4aace13061256337e69b (diff)
parent5c54806578260adcb54b12b00a16cc8707a19263 (diff)
downloadslixmpp-22fa8bc4d91d38a1176b09d03e0d41313b1adcaa.tar.gz
slixmpp-22fa8bc4d91d38a1176b09d03e0d41313b1adcaa.tar.bz2
slixmpp-22fa8bc4d91d38a1176b09d03e0d41313b1adcaa.tar.xz
slixmpp-22fa8bc4d91d38a1176b09d03e0d41313b1adcaa.zip
Merge branch 'more-typing' into 'master'
Add more typing See merge request poezio/slixmpp!166
Diffstat (limited to 'slixmpp/xmlstream/tostring.py')
-rw-r--r--slixmpp/xmlstream/tostring.py21
1 files changed, 16 insertions, 5 deletions
diff --git a/slixmpp/xmlstream/tostring.py b/slixmpp/xmlstream/tostring.py
index efac124e..447c9017 100644
--- a/slixmpp/xmlstream/tostring.py
+++ b/slixmpp/xmlstream/tostring.py
@@ -1,4 +1,3 @@
-
# slixmpp.xmlstream.tostring
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
# This module converts XML objects into Unicode strings and
@@ -7,11 +6,20 @@
# Part of Slixmpp: The Slick XMPP Library
# :copyright: (c) 2011 Nathanael C. Fritz
# :license: MIT, see LICENSE for more details
+from __future__ import annotations
+
+from typing import Optional, Set, TYPE_CHECKING
+from xml.etree.ElementTree import Element
+if TYPE_CHECKING:
+ from slixmpp.xmlstream import XMLStream
+
XML_NS = 'http://www.w3.org/XML/1998/namespace'
-def tostring(xml=None, xmlns='', stream=None, outbuffer='',
- top_level=False, open_only=False, namespaces=None):
+def tostring(xml: Optional[Element] = None, xmlns: str = '',
+ stream: Optional[XMLStream] = None, outbuffer: str = '',
+ top_level: bool = False, open_only: bool = False,
+ namespaces: Optional[Set[str]] = None) -> str:
"""Serialize an XML object to a Unicode string.
If an outer xmlns is provided using ``xmlns``, then the current element's
@@ -35,6 +43,8 @@ def tostring(xml=None, xmlns='', stream=None, outbuffer='',
:rtype: Unicode string
"""
+ if xml is None:
+ return ''
# Add previous results to the start of the output.
output = [outbuffer]
@@ -123,11 +133,12 @@ def tostring(xml=None, xmlns='', stream=None, outbuffer='',
# Remove namespaces introduced in this context. This is necessary
# because the namespaces object continues to be shared with other
# contexts.
- namespaces.remove(ns)
+ if namespaces is not None:
+ namespaces.remove(ns)
return ''.join(output)
-def escape(text, use_cdata=False):
+def escape(text: str, use_cdata: bool = False) -> str:
"""Convert special characters in XML to escape sequences.
:param string text: The XML text to convert.