Server IP : 66.29.132.122 / Your IP : 3.133.129.64 Web Server : LiteSpeed System : Linux business142.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : admazpex ( 531) PHP Version : 7.2.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /proc/self/root/proc/thread-self/root/proc/thread-self/root/opt/alt/python27/lib/python2.7/site-packages/raven/utils/serializer/ |
Upload File : |
""" raven.utils.serializer.manager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details. :license: BSD, see LICENSE for more details. """ from __future__ import absolute_import import logging from contextlib import closing from raven.utils.compat import text_type __all__ = ('register', 'transform') logger = logging.getLogger('sentry.errors.serializer') class SerializationManager(object): logger = logger def __init__(self): self.__registry = [] self.__serializers = {} @property def serializers(self): # XXX: Would serializers ever need state that we shouldn't cache them? for serializer in self.__registry: yield serializer def register(self, serializer): if serializer not in self.__registry: self.__registry.append(serializer) return serializer class Serializer(object): logger = logger def __init__(self, manager): self.manager = manager self.context = set() self.serializers = [] for serializer in manager.serializers: self.serializers.append(serializer(self)) def close(self): del self.serializers del self.context def transform(self, value, **kwargs): """ Primary function which handles recursively transforming values via their serializers """ if value is None: return None objid = id(value) if objid in self.context: return '<...>' self.context.add(objid) try: for serializer in self.serializers: if serializer.can(value): try: return serializer.serialize(value, **kwargs) except Exception as e: logger.exception(e) return text_type(type(value)) # if all else fails, lets use the repr of the object try: return repr(value) except Exception as e: logger.exception(e) # It's common case that a model's __unicode__ definition # may try to query the database which if it was not # cleaned up correctly, would hit a transaction aborted # exception return text_type(type(value)) finally: self.context.remove(objid) manager = SerializationManager() register = manager.register def transform(value, manager=manager, **kwargs): with closing(Serializer(manager)) as serializer: return serializer.transform(value, **kwargs)