diff --git a/src/octoprint/plugin/__init__.py b/src/octoprint/plugin/__init__.py index 29febf4cc..49867fa7d 100644 --- a/src/octoprint/plugin/__init__.py +++ b/src/octoprint/plugin/__init__.py @@ -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 diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index c9f3b809f..6a63fa039 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -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): diff --git a/tests/plugin/test_settings.py b/tests/plugin/test_settings.py index c1351861d..1f2a0468f 100644 --- a/tests/plugin/test_settings.py +++ b/tests/plugin/test_settings.py @@ -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))