From e3026fa23005b7c9baec2a246fcd768c993af001 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Mon, 21 Oct 2019 12:33:04 +0200 Subject: [PATCH] mgr/orchestrator: Fix raise_if_exception for Python 3 Also: Added a small test Signed-off-by: Sebastian Wagner --- src/pybind/mgr/orchestrator.py | 6 +++++- src/pybind/mgr/orchestrator_cli/module.py | 14 ++++++++++++++ src/pybind/mgr/selftest/module.py | 11 +++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/pybind/mgr/orchestrator.py b/src/pybind/mgr/orchestrator.py index 09edecbbca4..ed3b2b42503 100644 --- a/src/pybind/mgr/orchestrator.py +++ b/src/pybind/mgr/orchestrator.py @@ -119,11 +119,15 @@ def raise_if_exception(c): # This is something like `return pickle.loads(pickle.dumps(r_obj))` # Without importing anything. r_cls = r_obj.__class__ - if r_cls.__module__ == '__builtin__': + if r_cls.__module__ in ('__builtin__', 'builtins'): return r_obj my_cls = getattr(sys.modules[r_cls.__module__], r_cls.__name__) if id(my_cls) == id(r_cls): return r_obj + if hasattr(r_obj, '__reduce__'): + reduce_tuple = r_obj.__reduce__() + if len(reduce_tuple) >= 2: + return my_cls(*[copy_to_this_subinterpreter(a) for a in reduce_tuple[1]]) my_obj = my_cls.__new__(my_cls) for k,v in r_obj.__dict__.items(): setattr(my_obj, k, copy_to_this_subinterpreter(v)) diff --git a/src/pybind/mgr/orchestrator_cli/module.py b/src/pybind/mgr/orchestrator_cli/module.py index a73ba2a8363..e65b3c2acd8 100644 --- a/src/pybind/mgr/orchestrator_cli/module.py +++ b/src/pybind/mgr/orchestrator_cli/module.py @@ -466,3 +466,17 @@ Usage: self._set_backend('') assert self._select_orchestrator() is None self._set_backend(old_orch) + + e = self.remote('selftest', 'remote_from_orchestrator_cli_self_test', "ZeroDivisionError") + try: + orchestrator.raise_if_exception(e) + assert False + except ZeroDivisionError as e: + assert e.args == ('hello', 'world') + + e = self.remote('selftest', 'remote_from_orchestrator_cli_self_test', "OrchestratorError") + try: + orchestrator.raise_if_exception(e) + assert False + except orchestrator.OrchestratorError as e: + assert e.args == ('hello', 'world') diff --git a/src/pybind/mgr/selftest/module.py b/src/pybind/mgr/selftest/module.py index a3fb51cde13..444a92d63aa 100644 --- a/src/pybind/mgr/selftest/module.py +++ b/src/pybind/mgr/selftest/module.py @@ -444,6 +444,17 @@ class Module(MgrModule): else: raise RuntimeError("KeyError not raised") + def remote_from_orchestrator_cli_self_test(self, what): + import orchestrator + if what == 'OrchestratorError': + c = orchestrator.TrivialReadCompletion(result=None) + c.exception = orchestrator.OrchestratorError('hello', 'world') + return c + elif what == "ZeroDivisionError": + c = orchestrator.TrivialReadCompletion(result=None) + c.exception = ZeroDivisionError('hello', 'world') + return c + assert False, repr(what) def shutdown(self): self._workload = self.SHUTDOWN -- 2.39.5