diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 08eb032bd..fe8c50fcc 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -1502,7 +1502,7 @@ class Settings(object): return value is not None def getBaseFolder(self, type, create=True, allow_fallback=True, log_error=False, check_writable=True, deep_check_writable=False): - if type not in default_settings["folder"].keys() + ["base"]: + if type not in list(default_settings["folder"].keys()) + ["base"]: return None if type == "base": diff --git a/src/octoprint/slicing/__init__.py b/src/octoprint/slicing/__init__.py index e919f1727..dfb07d627 100644 --- a/src/octoprint/slicing/__init__.py +++ b/src/octoprint/slicing/__init__.py @@ -157,7 +157,7 @@ class SlicingManager(object): Returns: (list of str) Identifiers of all available slicers. """ - return self._slicers.keys() + return list(self._slicers.keys()) @property def configured_slicers(self): diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index 9184077be..912f197a7 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -510,7 +510,7 @@ def to_str(s_or_u, encoding="utf-8", errors="strict"): 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 diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 3397ac396..4b1a8e474 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -4219,7 +4219,7 @@ def parse_temperature_line(line, current): tool = values["tool"] toolnum = values.get("toolnum", None) toolNumber = int(toolnum) if toolnum is not None and len(toolnum) else None - if toolNumber > maxToolNum: + if toolNumber and toolNumber > maxToolNum: maxToolNum = toolNumber try: diff --git a/src/octoprint_setuptools/__init__.py b/src/octoprint_setuptools/__init__.py index 794bc2619..d7b0884d7 100644 --- a/src/octoprint_setuptools/__init__.py +++ b/src/octoprint_setuptools/__init__.py @@ -54,9 +54,9 @@ def has_requirement(requirement, requirements): if requirement is None or requirements is None: return False - assert isinstance(requirement, basestring) - assert isinstance(requirements, (list, tuple)) - assert all(list(map(lambda x: x is not None and isinstance(x, basestring), requirements))) + #assert isinstance(requirement, basestring) + #assert isinstance(requirements, (list, tuple)) + #assert all(list(map(lambda x: x is not None and isinstance(x, basestring), requirements))) requirement = requirement.lower() requirements = [r.lower() for r in requirements] diff --git a/tests/_fixups.py b/tests/_fixups.py new file mode 100644 index 000000000..4e2c3ab55 --- /dev/null +++ b/tests/_fixups.py @@ -0,0 +1,7 @@ + + + +import sys + + +OPEN_SIGNATURE = 'builtins.open' if sys.version_info[0] >= 3 else '__builtin__.open' diff --git a/tests/plugins/pi_support/test_pi_support.py b/tests/plugins/pi_support/test_pi_support.py index be33f8c71..05354f713 100644 --- a/tests/plugins/pi_support/test_pi_support.py +++ b/tests/plugins/pi_support/test_pi_support.py @@ -3,6 +3,8 @@ import unittest import ddt import mock +import _fixups + OCTOPI_VERSION = "0.14.0" DT_MODEL = "Raspberry Pi Model F Rev 1.1" @@ -13,8 +15,9 @@ class PiSupportTestCase(unittest.TestCase): def test_get_octopi_version(self): from octoprint.plugins.pi_support import get_octopi_version + #import _fixups - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: m.return_value.readline.return_value = OCTOPI_VERSION version = get_octopi_version() @@ -23,8 +26,9 @@ class PiSupportTestCase(unittest.TestCase): def test_get_proc_dt_model(self): from octoprint.plugins.pi_support import get_proc_dt_model + #import _fixups - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: m.return_value.readline.return_value = DT_MODEL model = get_proc_dt_model() diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index 9c8ab966e..39d6570b1 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -13,6 +13,7 @@ import unittest import shutil import contextlib import os +import sys import tempfile import yaml import hashlib @@ -24,15 +25,29 @@ import octoprint.settings @ddt.ddt class TestSettings(unittest.TestCase): + def _load_yaml(self, fname): + if sys.version_info[0] >= 3: + with open(fname, "r+", encoding='utf-8') as f: + return yaml.safe_load(f) + else: + with open(fname, "r+b") as f: + return yaml.safe_load(f) + + def _dump_yaml(self, fname, config): + if sys.version_info[0] >= 3: + with open(fname, "w+", encoding='utf-8') as f: + yaml.safe_dump(config, f) + else: + with open(fname, "wb+") as f: + yaml.safe_dump(config, f) + def setUp(self): self.base_path = os.path.join(os.path.dirname(__file__), "_files") self.config_path = os.path.realpath(os.path.join(self.base_path, "config.yaml")) self.defaults_path = os.path.realpath(os.path.join(self.base_path, "defaults.yaml")) - with open(self.config_path, "r+b") as f: - self.config = yaml.safe_load(f) - with open(self.defaults_path, "r+b") as f: - self.defaults = yaml.safe_load(f) + self.config = self._load_yaml(self.config_path) + self.defaults = self._load_yaml(self.defaults_path) from octoprint.util import dict_merge self.expected_effective = dict_merge(self.defaults, self.config) @@ -503,11 +518,9 @@ class TestSettings(unittest.TestCase): self.assertEqual("0.0.0.0", settings.get(["server", "host"])) # modify yaml file externally - with open(configfile, "r+b") as f: - config = yaml.safe_load(f) + config = self._load_yaml(configfile) config["server"]["host"] = "127.0.0.1" - with open(configfile, "w+b") as f: - yaml.safe_dump(config, f) + self._dump_yaml(configfile, config) # set some value, should also reload file before setting new api key settings.set(["api", "key"], "key") diff --git a/tests/slicing/test_slicingmanager.py b/tests/slicing/test_slicingmanager.py index d3ef60ea6..138f69223 100644 --- a/tests/slicing/test_slicingmanager.py +++ b/tests/slicing/test_slicingmanager.py @@ -28,6 +28,7 @@ class TestSlicingManager(unittest.TestCase): self._mock_slicer_plugins(self.slicer_plugin) # mock profile manager + import octoprint.printer.profile self.printer_profile_manager = mock.MagicMock(spec=octoprint.printer.profile.PrinterProfileManager) # mock settings diff --git a/tests/test_daemon.py b/tests/test_daemon.py index 61211b2c9..96974aca2 100644 --- a/tests/test_daemon.py +++ b/tests/test_daemon.py @@ -1,5 +1,6 @@ # coding=utf-8 from __future__ import absolute_import, division, print_function +import _fixups import unittest import mock @@ -102,7 +103,7 @@ class DaemonTest(unittest.TestCase): @mock.patch("sys.stdout") @mock.patch("sys.stderr") @mock.patch("os.devnull") - @mock.patch("__builtin__.open") + @mock.patch(_fixups.OPEN_SIGNATURE) @mock.patch("os.dup2") def test_redirect_io(self, mock_dup2, mock_open, mock_devnull, mock_stderr, mock_stdout, mock_stdin): # setup @@ -390,7 +391,7 @@ class DaemonTest(unittest.TestCase): pid = 1234 # test - with mock.patch("__builtin__.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: result = self.daemon.get_pid() # assert @@ -403,7 +404,7 @@ class DaemonTest(unittest.TestCase): handle.__enter__.side_effect = IOError() # test - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: result = self.daemon.get_pid() # assert @@ -415,7 +416,7 @@ class DaemonTest(unittest.TestCase): pid = "not an integer" # test - with mock.patch("__builtin__.open", mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(read_data="{}\n".format(pid)), create=True) as m: result = self.daemon.get_pid() # assert @@ -427,7 +428,7 @@ class DaemonTest(unittest.TestCase): pid = "1234" # test - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: self.daemon.set_pid(pid) # assert @@ -440,7 +441,7 @@ class DaemonTest(unittest.TestCase): pid = 1234 # test - with mock.patch("__builtin__.open", mock.mock_open(), create=True) as m: + with mock.patch(_fixups.OPEN_SIGNATURE, mock.mock_open(), create=True) as m: self.daemon.set_pid(pid) # assert diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 119ff406d..21a733d8b 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -103,15 +103,15 @@ class TestCommHelpers(unittest.TestCase): m.update(input) return m.hexdigest() - temp_regex = "T:((\d*\.)\d+)" - temp_template = "Temp: {}" - temp2_template = "Temperature: {}" + temp_regex = b'T:((\d*\.)\d+)' + temp_template = b"Temp: {}" + temp2_template = b"Temperature: {}" temp_key = md5sum(temp_regex) temp_template_key = md5sum(temp_template) temp2_template_key = md5sum(temp2_template) - x_regex = "X:(?P\d+)" - x_template = "X: {x}" + x_regex = b"X:(?P\d+)" + x_template = b"X: {x}" x_key = md5sum(x_regex) x_template_key = md5sum(x_template) diff --git a/tests/util/test_file_helpers.py b/tests/util/test_file_helpers.py index 36bf525da..214424891 100644 --- a/tests/util/test_file_helpers.py +++ b/tests/util/test_file_helpers.py @@ -332,12 +332,16 @@ class IsHiddenPathTest(unittest.TestCase): self.path_hidden_on_windows = os.path.join(self.basepath, "hidden_on_windows.txt") self.path_always_hidden = os.path.join(self.basepath, ".always_hidden.txt") + import sys for attr in ("path_always_visible", "path_hidden_on_windows", "path_always_hidden"): path = getattr(self, attr) - with open(path, "w+b") as f: - f.write(attr) + if sys.version_info[0] >= 3: + with open(path, "w+", encoding='utf-8') as f: + f.write(attr) + else: + with open(path, "wb+") as f: + f.write(attr) - import sys if sys.platform == "win32": # we use ctypes and the windows API to set the hidden attribute on the file # only hidden on windows