From 43fe5cd8be60034b81e386eb0010374784d57516 Mon Sep 17 00:00:00 2001 From: OllisGit <24205767+OllisGit@users.noreply.github.com> Date: Sun, 12 Apr 2020 11:11:22 +0200 Subject: [PATCH] Update __init__.py Copied the description about errors-argument from Pyh-Doc (https://docs.python.org/3/howto/unicode.html). Im not sure how to create the HTML-Doc out of this file. So, I could not test the doc-output. --- src/octoprint/util/__init__.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index f9e7ffe54..1c601935a 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -41,7 +41,19 @@ logger = logging.getLogger(__name__) def to_bytes(s_or_u, encoding="utf-8", errors="strict"): # type: (Union[unicode, bytes], str, str) -> bytes - """Make sure ``s_or_u`` is a bytestring.""" + """ + Make sure ``s_or_u`` is a bytestring. + + Arguments: + s_or_u (string or unicode): The value to convert + encoding (string): default 'utf-8' + errors (string): The errors argument is the same as the argument of the decode() method but supports a few more possible handlers. + As well as 'strict', 'ignore', and 'replace' (which in this case inserts a question mark instead of the unencodable character), + there is also 'xmlcharrefreplace' (inserts an XML character reference), backslashreplace (inserts a \uNNNN escape sequence) + and namereplace (inserts a \N{...} escape sequence). + Returns: + bytes: converted bytes. + """ if s_or_u is None: return s_or_u @@ -56,7 +68,18 @@ def to_bytes(s_or_u, encoding="utf-8", errors="strict"): def to_unicode(s_or_u, encoding="utf-8", errors="strict"): # type: (Union[unicode, bytes], str, str) -> unicode - """Make sure ``s_or_u`` is a unicode string.""" + """ + Make sure ``s_or_u`` is a unicode string. + + Arguments: + s_or_u (string or unicode): The value to convert + encoding (string): default 'utf-8' + errors (string): The errors argument specifies the response when the input string can’t be converted according to the encoding’s rules. + Legal values for this argument are 'strict' (raise a UnicodeDecodeError exception), 'replace' (use U+FFFD, REPLACEMENT CHARACTER), + 'ignore' (just leave the character out of the Unicode result), or 'backslashreplace' (inserts a \xNN escape sequence). + Returns: + string: converted string. + """ if s_or_u is None: return s_or_u