♻ Backport to_str -> to_bytes from devel
Add unit tests.
This commit is contained in:
1 parent
deacd9c188
commit
81d523b73d
5 files changed
+82
-11
No files matched your search
@@ -29,20 +29,20 @@ def _log(lines, prefix=None, stream=None):
|
||||
output_stream = sys.stderr
|
||||
|
||||
for line in lines:
|
||||
to_print = _to_str(u"{} {}".format(prefix, _to_unicode(line.rstrip(), errors="replace")),
|
||||
errors="replace")
|
||||
to_print = _to_bytes(u"{} {}".format(prefix, _to_unicode(line.rstrip(), errors="replace")),
|
||||
errors="replace")
|
||||
print(to_print, file=output_stream)
|
||||
|
||||
|
||||
def _to_unicode(s_or_u, encoding="utf-8", errors="strict"):
|
||||
"""Make sure ``s_or_u`` is a unicode string."""
|
||||
if isinstance(s_or_u, str):
|
||||
if isinstance(s_or_u, bytes):
|
||||
return s_or_u.decode(encoding, errors=errors)
|
||||
else:
|
||||
return s_or_u
|
||||
|
||||
|
||||
def _to_str(s_or_u, encoding="utf-8", errors="strict"):
|
||||
def _to_bytes(s_or_u, encoding="utf-8", errors="strict"):
|
||||
"""Make sure ``s_or_u`` is a str."""
|
||||
if isinstance(s_or_u, unicode):
|
||||
return s_or_u.encode(encoding, errors=errors)
|
||||
|
||||
@@ -549,7 +549,7 @@ def _extended_header_value(value):
|
||||
# RFC 5987 section 3.2
|
||||
from urllib import unquote
|
||||
encoding, _, value = value.split("'", 2)
|
||||
return unquote(octoprint.util.to_str(value, encoding="iso-8859-1")).decode(encoding)
|
||||
return unquote(octoprint.util.to_bytes(value, encoding="iso-8859-1")).decode(encoding)
|
||||
|
||||
else:
|
||||
# no encoding provided, strip potentially present quotes and call it a day
|
||||
|
||||
@@ -20,7 +20,7 @@ from builtins import range, bytes
|
||||
|
||||
from octoprint.settings import settings
|
||||
|
||||
from octoprint.util import atomic_write, to_str, deprecated
|
||||
from octoprint.util import atomic_write, to_bytes, deprecated
|
||||
|
||||
class UserManager(object):
|
||||
valid_roles = ["user", "admin"]
|
||||
@@ -140,7 +140,7 @@ class UserManager(object):
|
||||
settings().set(["accessControl", "salt"], salt)
|
||||
settings().save()
|
||||
|
||||
return hashlib.sha512(to_str(password, encoding="utf-8", errors="replace") + to_str(salt)).hexdigest()
|
||||
return hashlib.sha512(to_bytes(password, encoding="utf-8", errors="replace") + to_bytes(salt)).hexdigest()
|
||||
|
||||
def checkPassword(self, username, password):
|
||||
user = self.findUser(username)
|
||||
|
||||
@@ -427,23 +427,26 @@ def filter_non_ascii(line):
|
||||
"""
|
||||
|
||||
try:
|
||||
to_str(to_unicode(line, encoding="ascii"), encoding="ascii")
|
||||
to_bytes(to_unicode(line, encoding="ascii"), encoding="ascii")
|
||||
return False
|
||||
except ValueError:
|
||||
return True
|
||||
|
||||
|
||||
def to_str(s_or_u, encoding="utf-8", errors="strict"):
|
||||
"""Make sure ``s_or_u`` is a str."""
|
||||
def to_bytes(s_or_u, encoding="utf-8", errors="strict"):
|
||||
"""Make sure ``s_or_u`` is a bytestring."""
|
||||
if isinstance(s_or_u, unicode):
|
||||
return s_or_u.encode(encoding, errors=errors)
|
||||
else:
|
||||
return s_or_u
|
||||
|
||||
to_str = deprecated("to_str has been renamed to to_bytes", since="1.3.11")(to_bytes)
|
||||
|
||||
|
||||
|
||||
def to_unicode(s_or_u, encoding="utf-8", errors="strict"):
|
||||
"""Make sure ``s_or_u`` is a unicode string."""
|
||||
if isinstance(s_or_u, str):
|
||||
if isinstance(s_or_u, bytes):
|
||||
return s_or_u.decode(encoding, errors=errors)
|
||||
else:
|
||||
return s_or_u
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
|
||||
__copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
||||
|
||||
import unittest
|
||||
|
||||
import octoprint.util
|
||||
|
||||
from past.builtins import unicode
|
||||
|
||||
class StringHelperTest(unittest.TestCase):
|
||||
|
||||
def test_to_unicode_unicode(self):
|
||||
result = octoprint.util.to_unicode("test")
|
||||
self.assertEqual(result, "test")
|
||||
self.assertIsInstance(result, unicode)
|
||||
|
||||
def test_to_unicode_bytes(self):
|
||||
result = octoprint.util.to_unicode(b"test")
|
||||
self.assertEqual(result, "test")
|
||||
self.assertIsInstance(result, unicode)
|
||||
|
||||
def test_to_unicode_bytes_utf8(self):
|
||||
data = "äöüß"
|
||||
result = octoprint.util.to_unicode(data.encode("utf-8"), encoding="utf-8")
|
||||
self.assertEqual(result, data)
|
||||
self.assertIsInstance(result, unicode)
|
||||
|
||||
def test_to_unicode_bytes_utf8_vs_ascii(self):
|
||||
self.assertRaises(UnicodeDecodeError, octoprint.util.to_unicode, "äöüß".encode("utf-8"), encoding="ascii")
|
||||
|
||||
def test_to_unicode_bytes_utf8_vs_ascii_replace(self):
|
||||
data = "äöüß"
|
||||
result = octoprint.util.to_unicode(data.encode("utf-8"), encoding="ascii", errors="replace")
|
||||
self.assertEqual(result, data.encode("utf-8").decode("ascii", errors="replace"))
|
||||
self.assertIsInstance(result, unicode)
|
||||
|
||||
def test_to_bytes_bytes(self):
|
||||
result = octoprint.util.to_bytes(b"test")
|
||||
self.assertEqual(result, b"test")
|
||||
self.assertIsInstance(result, bytes)
|
||||
|
||||
def test_to_bytes_unicode(self):
|
||||
result = octoprint.util.to_bytes("test")
|
||||
self.assertEqual(result, b"test")
|
||||
self.assertIsInstance(result, bytes)
|
||||
|
||||
def test_to_bytes_unicode_utf8(self):
|
||||
data = b"äöüß"
|
||||
result = octoprint.util.to_bytes(data.decode("utf-8"), encoding="utf-8")
|
||||
self.assertEqual(result, data)
|
||||
self.assertIsInstance(result, bytes)
|
||||
|
||||
def test_to_bytes_unicode_utf8_vs_ascii(self):
|
||||
self.assertRaises(UnicodeEncodeError, octoprint.util.to_bytes, b"äöüß".decode("utf-8"), encoding="ascii")
|
||||
|
||||
def test_to_bytes_unicode_utf8_vs_ascii_replace(self):
|
||||
data = b"äöüß"
|
||||
result = octoprint.util.to_bytes(data.decode("utf-8"), encoding="ascii", errors="replace")
|
||||
self.assertEqual(result, data.decode("utf-8").encode("ascii", errors="replace"))
|
||||
self.assertIsInstance(result, bytes)
|
||||
|
||||
def test_to_str_still_works(self):
|
||||
result = octoprint.util.to_str("test")
|
||||
self.assertEqual(result, b"test")
|
||||
self.assertIsInstance(result, bytes)
|
||||
Reference in new issue
Block a user