def exception_to_python(self, exc): """Convert serialized exception to Python exception.""" if exc: if not isinstance(exc, BaseException): exc_module = exc.get('exc_module') if exc_module is None: cls = create_exception_cls(from_utf8(exc['exc_type']), __name__) else: exc_module = from_utf8(exc_module) exc_type = from_utf8(exc['exc_type']) try: # Load module and find exception class in that cls = sys.modules[exc_module] # The type can contain qualified name with parent classes for name in exc_type.split('.'): cls = getattr(cls, name) except (KeyError, AttributeError): cls = create_exception_cls(exc_type, celery.exceptions.__name__) exc_msg = exc['exc_message'] try: if isinstance(exc_msg, (tuple, list)): exc = cls(*exc_msg) else: exc = cls(exc_msg) except Exception as err: # noqa exc = Exception(f'{cls}({exc_msg})') if self.serializer in EXCEPTION_ABLE_CODECS: exc = get_pickled_exception(exc) return exc
def test_utf8dict(self): uk = "foobar" d = {"følelser ær langé": "ærbadægzaååÆØÅ", from_utf8(uk): from_utf8("xuzzybaz")} for key, value in items(http.utf8dict(items(d))): self.assertIsInstance(key, str) self.assertIsInstance(value, str)
def exception_to_python(self, exc): """Convert serialized exception to Python exception.""" if exc: if not isinstance(exc, BaseException): exc_module = exc.get('exc_module') if exc_module is None: cls = create_exception_cls(from_utf8(exc['exc_type']), __name__) else: exc_module = from_utf8(exc_module) exc_type = from_utf8(exc['exc_type']) try: cls = getattr(sys.modules[exc_module], exc_type) except (KeyError, AttributeError): cls = create_exception_cls(exc_type, celery.exceptions.__name__) exc_msg = exc['exc_message'] try: if isinstance(exc_msg, tuple): exc = cls(*exc_msg) else: exc = cls(exc_msg) except Exception as err: # noqa exc = Exception('{}({})'.format(cls, exc_msg)) if self.serializer in EXCEPTION_ABLE_CODECS: exc = get_pickled_exception(exc) return exc
def test_utf8dict(self): uk = 'foobar' d = {'følelser ær langé': 'ærbadægzaååÆØÅ', from_utf8(uk): from_utf8('xuzzybaz')} for key, value in items(http.utf8dict(items(d))): self.assertIsInstance(key, str) self.assertIsInstance(value, str)
def test_utf8dict(self): uk = "foobar" d = { u"følelser ær langé": u"ærbadægzaååÆØÅ", from_utf8(uk): from_utf8("xuzzybaz") } for key, value in http.utf8dict(d.items()).items(): self.assertIsInstance(key, str) self.assertIsInstance(value, str)
def exception_to_python(self, exc): """Convert serialized exception to Python exception.""" if not isinstance(exc, BaseException): exc = create_exception_cls( from_utf8(exc['exc_type']), __name__)(exc['exc_message']) if self.serializer in EXCEPTION_ABLE_CODECS: exc = get_pickled_exception(exc) return exc
def exception_to_python(self, exc): """Convert serialized exception to Python exception.""" if exc: if not isinstance(exc, BaseException): exc_module = exc.get('exc_module') if exc_module is None: cls = create_exception_cls( from_utf8(exc['exc_type']), __name__) else: exc_module = from_utf8(exc_module) exc_type = from_utf8(exc['exc_type']) cls = getattr(sys.modules[exc_module], exc_type) exc_msg = exc['exc_message'] exc = cls(*exc_msg if isinstance(exc_msg, tuple) else exc_msg) if self.serializer in EXCEPTION_ABLE_CODECS: exc = get_pickled_exception(exc) return exc
def exception_to_python(self, exc): """Convert serialized exception to Python exception.""" if exc: if not isinstance(exc, BaseException): exc_module = exc.get('exc_module') if exc_module is None: cls = create_exception_cls(from_utf8(exc['exc_type']), __name__) else: exc_module = from_utf8(exc_module) exc_type = from_utf8(exc['exc_type']) cls = getattr(sys.modules[exc_module], exc_type) exc_msg = exc['exc_message'] exc = cls(*exc_msg if isinstance(exc_msg, tuple) else exc_msg) if self.serializer in EXCEPTION_ABLE_CODECS: exc = get_pickled_exception(exc) return exc
def exception_to_python(self, exc): """Convert serialized exception to Python exception.""" if self.serializer in EXCEPTION_ABLE_CODECS: return get_pickled_exception(exc) elif not isinstance(exc, BaseException): return create_exception_cls(from_utf8(exc['exc_type']), __name__)(exc['exc_message']) return exc
def waitexec(self, argv, path=sys.executable): args = " ".join([path] + list(argv)) argstr = shellsplit(from_utf8(args)) pipe = Popen(argstr, env=self.env) self.info(" %s" % " ".join(argstr)) retcode = pipe.wait() if retcode < 0: self.note("* Child was terminated by signal %s" % (-retcode, )) return -retcode elif retcode > 0: self.note("* Child terminated with failure code %s" % (retcode, )) return retcode
def waitexec(self, argv, path=sys.executable): args = ' '.join([path] + list(argv)) argstr = shlex.split(from_utf8(args), posix=not IS_WINDOWS) pipe = Popen(argstr, env=self.env) self.info(' {0}'.format(' '.join(argstr))) retcode = pipe.wait() if retcode < 0: self.note('* Child was terminated by signal {0}'.format(-retcode)) return -retcode elif retcode > 0: self.note('* Child terminated with errorcode {0}'.format(retcode)) return retcode
def waitexec(self, argv, path=sys.executable): args = ' '.join([path] + list(argv)) argstr = shellsplit(from_utf8(args)) pipe = Popen(argstr, env=self.env) self.info(' %s' % ' '.join(argstr)) retcode = pipe.wait() if retcode < 0: self.note('* Child was terminated by signal %s' % (-retcode, )) return -retcode elif retcode > 0: self.note('* Child terminated with failure code %s' % (retcode, )) return retcode
def unpack_exception(self, data, serializer): """ Instantiates exception stub for original exception :param module: module name for original exception :param name: class name for original exception :param args: RemoteException.args :return: new constructed exception :rtype: self.RemoteError subclass """ try: # unpacking RemoteException args content_type, content_encoding, dumps = registry._encoders[ serializer] data = loads(data, content_type, content_encoding) module, name, args = data try: # trying to import original exception original = symbol_by_name("%s.%s" % (module, name)) # creating parent class for original error and self.RemoteError class_name = from_utf8("Remote" + name) parent = type(class_name, (original, self.RemoteError), {'__module__': module}) except (AttributeError, ImportError): # alternative way for unknown errors parent = self.RemoteError # create and cache exception stub class if name not in self.__registry: self.__registry[name] = create_exception_cls(from_utf8(name), module, parent=parent) exc_class = self.__registry[name] return exc_class(*args) except (ValueError, ContentDisallowed): # loads error return None
def unpack_exception(self, data, serializer): """ Instantiates exception stub for original exception :param module: module name for original exception :param name: class name for original exception :param args: RemoteException.args :return: new constructed exception :rtype: self.RemoteError subclass """ try: # unpacking RemoteException args content_type, content_encoding, dumps = registry._encoders[serializer] data = loads(data, content_type, content_encoding) module, name, args = data try: # trying to import original exception original = symbol_by_name("%s.%s" % (module, name)) # creating parent class for original error and self.RemoteError class_name = from_utf8("Remote" + name) parent = type(class_name, (original, self.RemoteError), {'__module__': module}) except (AttributeError, ImportError): # alternative way for unknown errors parent = self.RemoteError # create and cache exception stub class if name not in self.__registry: self.__registry[name] = create_exception_cls( from_utf8(name), module, parent=parent) exc_class = self.__registry[name] return exc_class(*args) except (ValueError, ContentDisallowed): # loads error return None
def test_from_message(self): us = "æØåveéðƒeæ" body = {"task": self.mytask.name, "id": uuid(), "args": [2], "kwargs": {us: "bar"}} m = Message( None, body=anyjson.dumps(body), backend="foo", content_type="application/json", content_encoding="utf-8" ) job = Request(m.decode(), message=m, app=self.app) self.assertIsInstance(job, Request) self.assertEqual(job.name, body["task"]) self.assertEqual(job.id, body["id"]) self.assertEqual(job.args, body["args"]) us = from_utf8(us) if sys.version_info < (2, 6): self.assertEqual(next(keys(job.kwargs)), us) self.assertIsInstance(next(keys(job.kwargs)), str)
def test_from_message(self): us = u"æØåveéðƒeæ" body = {"task": mytask.name, "id": uuid(), "args": [2], "kwargs": {us: "bar"}} m = Message( None, body=anyjson.dumps(body), backend="foo", content_type="application/json", content_encoding="utf-8" ) tw = TaskRequest.from_message(m, m.decode()) self.assertIsInstance(tw, Request) self.assertEqual(tw.name, body["task"]) self.assertEqual(tw.id, body["id"]) self.assertEqual(tw.args, body["args"]) us = from_utf8(us) if sys.version_info < (2, 6): self.assertEqual(tw.kwargs.keys()[0], us) self.assertIsInstance(tw.kwargs.keys()[0], str)
def test_from_message(self): us = 'æØåveéðƒeæ' body = {'task': self.mytask.name, 'id': uuid(), 'args': [2], 'kwargs': {us: 'bar'}} m = Message(None, body=anyjson.dumps(body), backend='foo', content_type='application/json', content_encoding='utf-8') job = Request(m.decode(), message=m, app=self.app) self.assertIsInstance(job, Request) self.assertEqual(job.name, body['task']) self.assertEqual(job.id, body['id']) self.assertEqual(job.args, body['args']) us = from_utf8(us) if sys.version_info < (2, 6): self.assertEqual(next(keys(job.kwargs)), us) self.assertIsInstance(next(keys(job.kwargs)), str)
def test_from_message(self): us = 'æØåveéðƒeæ' body = {'task': self.mytask.name, 'id': uuid(), 'args': [2], 'kwargs': {us: 'bar'}} m = Message(None, body=anyjson.dumps(body), backend='foo', content_type='application/json', content_encoding='utf-8') job = TaskRequest.from_message(m, m.decode(), app=self.app) self.assertIsInstance(job, Request) self.assertEqual(job.name, body['task']) self.assertEqual(job.id, body['id']) self.assertEqual(job.args, body['args']) us = from_utf8(us) if sys.version_info < (2, 6): self.assertEqual(next(keys(job.kwargs)), us) self.assertIsInstance(next(keys(job.kwargs)), str)
def test_from_message(self): us = u'æØåveéðƒeæ' body = {'task': mytask.name, 'id': uuid(), 'args': [2], 'kwargs': {us: 'bar'}} m = Message(None, body=anyjson.dumps(body), backend='foo', content_type='application/json', content_encoding='utf-8') tw = TaskRequest.from_message(m, m.decode()) self.assertIsInstance(tw, Request) self.assertEqual(tw.name, body['task']) self.assertEqual(tw.id, body['id']) self.assertEqual(tw.args, body['args']) us = from_utf8(us) if sys.version_info < (2, 6): self.assertEqual(tw.kwargs.keys()[0], us) self.assertIsInstance(tw.kwargs.keys()[0], str)
def test_from_message(self): us = u"æØåveéðƒeæ" body = { "task": mytask.name, "id": uuid(), "args": [2], "kwargs": { us: "bar" } } m = Message(None, body=anyjson.dumps(body), backend="foo", content_type="application/json", content_encoding="utf-8") tw = TaskRequest.from_message(m, m.decode()) self.assertIsInstance(tw, Request) self.assertEqual(tw.name, body["task"]) self.assertEqual(tw.id, body["id"]) self.assertEqual(tw.args, body["args"]) us = from_utf8(us) if sys.version_info < (2, 6): self.assertEqual(tw.kwargs.keys()[0], us) self.assertIsInstance(tw.kwargs.keys()[0], str)
def test_on_failure_utf8_exception(self): self._test_on_failure(Exception( from_utf8(u"Бобры атакуют")))
def exception_to_python(self, exc): """Convert serialized exception to Python exception.""" if self.serializer in EXCEPTION_ABLE_CODECS: return get_pickled_exception(exc) return create_exception_cls(from_utf8(exc['exc_type']), sys.modules[__name__])(exc['exc_message'])
def prepare_argv(self, argv, path): args = ' '.join([path] + list(argv)) return shlex.split(from_utf8(args), posix=not IS_WINDOWS)
def test_on_failure__utf8_exception(self): self._test_on_failure(Exception(from_utf8('Бобры атакуют')))