unicode woes for funcion __name__/__qualname__

This commit is contained in:
Matthias Urlichs committed 2019-02-04 12:47:44 +01:00
1 parent 54d359f730
commit 6d447f8408
3 files changed
+21 -19

No files matched your search

+2 -2
View File
@@ -27,7 +27,7 @@ from octoprint.settings import settings as s
from octoprint.plugin.core import (PluginInfo, PluginManager, Plugin)
from octoprint.plugin.types import *
from octoprint.util import deprecated
from octoprint.util import deprecated, to_native_str
# singleton
_instance = None
@@ -508,7 +508,7 @@ class PluginSettings(object):
def _func(*args, **kwargs):
return orig_func(*args_mapper(args), **kwargs_mapper(kwargs))
_func.__name__ = item
_func.__name__ = to_native_str(item)
_func.__doc__ = orig_func.__doc__ if "__doc__" in dir(orig_func) else None
return _func
+14 -13
View File
@@ -35,10 +35,23 @@ from past.builtins import basestring, unicode
logger = logging.getLogger(__name__)
def to_native_str(s_or_u):
"""Make sure ``s_or_u`` is a 'str'."""
if sys.version_info[0] == 2:
if isinstance(s_or_u, unicode):
return s_or_u.encode("utf-8")
else:
if not isinstance(s_or_u, str):
raise RuntimeError("Please use a string here.")
return s_or_u
def warning_decorator_factory(warning_type):
def specific_warning(message, stacklevel=1, since=None, includedoc=None, extenddoc=False):
def decorator(func):
func.__qualname__ = 'warning_decorator_factory'
func.__qualname__ = to_native_str('warning_decorator_factory')
func.__annotations__ = dict()
@wraps(func)
def func_wrapper(*args, **kwargs):
@@ -514,18 +527,6 @@ to_str = deprecated("to_str has been renamed to to_bytes", since="1.4.0")(to_byt
def to_native_str(s_or_u):
"""Make sure ``s_or_u`` is a 'str'."""
if sys.version_info[0] == 2:
if isinstance(s_or_u, unicode):
return s_or_u.encode("utf-8")
else:
if not isinstance(s_or_u, str):
raise RuntimeError("Please use a string here.")
return s_or_u
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, bytes):
+5 -4
View File
@@ -14,6 +14,7 @@ from ddt import ddt, unpack, data
import octoprint.plugin
import octoprint.settings
from octoprint.util import to_native_str
@ddt
class SettingsTestCase(unittest.TestCase):
@@ -104,7 +105,7 @@ class SettingsTestCase(unittest.TestCase):
def test_deprecated_forwarded_getter(self, deprecated, current, forwarded):
with warnings.catch_warnings(record=True) as w:
called_method = getattr(self.settings, forwarded)
called_method.__name__ = forwarded
called_method.__name__ = to_native_str(forwarded)
method = getattr(self.plugin_settings, deprecated)
self.assertTrue(callable(method))
@@ -126,7 +127,7 @@ class SettingsTestCase(unittest.TestCase):
def test_deprecated_global_getter(self, deprecated, current, forwarded):
with warnings.catch_warnings(record=True) as w:
called_method = getattr(self.settings, forwarded)
called_method.__name__ = forwarded
called_method.__name__ = to_native_str(forwarded)
method = getattr(self.plugin_settings, deprecated)
self.assertTrue(callable(method))
@@ -190,7 +191,7 @@ class SettingsTestCase(unittest.TestCase):
def test_deprecated_forwarded_setter(self, deprecated, current, forwarded, value):
with warnings.catch_warnings(record=True) as w:
called_method = getattr(self.settings, forwarded)
called_method.__name__ = forwarded
called_method.__name__ = to_native_str(forwarded)
method = getattr(self.plugin_settings, deprecated)
self.assertTrue(callable(method))
@@ -212,7 +213,7 @@ class SettingsTestCase(unittest.TestCase):
def test_deprecated_global_setter(self, deprecated, current, forwarded, value):
with warnings.catch_warnings(record=True) as w:
called_method = getattr(self.settings, forwarded)
called_method.__name__ = forwarded
called_method.__name__ = to_native_str(forwarded)
method = getattr(self.plugin_settings, deprecated)
self.assertTrue(callable(method))