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: try: if serializer.can(value): 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)
def _emit(self, record, **kwargs): data, extra = extract_extra(record) stack = getattr(record, 'stack', None) if stack is True: stack = iter_stack_frames() if stack: stack = self._get_targetted_stack(stack, record) date = datetime.datetime.utcfromtimestamp(record.created) event_type = 'raven.events.Message' handler_kwargs = { 'params': record.args, } try: handler_kwargs['message'] = text_type(record.msg) except UnicodeDecodeError: # Handle binary strings where it should be unicode... handler_kwargs['message'] = repr(record.msg)[1:-1] try: handler_kwargs['formatted'] = text_type(record.message) except UnicodeDecodeError: # Handle binary strings where it should be unicode... handler_kwargs['formatted'] = repr(record.message)[1:-1] # If there's no exception being processed, exc_info may be a 3-tuple of None # http://docs.python.org/library/sys.html#sys.exc_info if record.exc_info and all(record.exc_info): # capture the standard message first so that we ensure # the event is recorded as an exception, in addition to having our # message interface attached handler = self.client.get_handler(event_type) data.update(handler.capture(**handler_kwargs)) event_type = 'raven.events.Exception' handler_kwargs = {'exc_info': record.exc_info} data['level'] = record.levelno data['logger'] = record.name kwargs['tags'] = tags = {} if self.tags: tags.update(self.tags) tags.update(getattr(record, 'tags', {})) kwargs.update(handler_kwargs) sample_rate = extra.pop('sample_rate', None) return self.client.capture(event_type, stack=stack, data=data, extra=extra, date=date, sample_rate=sample_rate, **kwargs)
def fetch_git_sha(path, head=None): """ >>> fetch_git_sha(os.path.dirname(__file__)) """ if not head: head_path = os.path.join(path, '.git', 'HEAD') if not os.path.exists(head_path): raise InvalidGitRepository( 'Cannot identify HEAD for git repository at %s' % (path,)) with open(head_path, 'r') as fp: head = text_type(fp.read()).strip() if head.startswith('ref: '): head = head[5:] revision_file = os.path.join( path, '.git', *head.split('/') ) else: return head else: revision_file = os.path.join(path, '.git', 'refs', 'heads', head) if not os.path.exists(revision_file): if not os.path.exists(os.path.join(path, '.git')): raise InvalidGitRepository( '%s does not seem to be the root of a git repository' % (path,)) # Check for our .git/packed-refs' file since a `git gc` may have run # https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery packed_file = os.path.join(path, '.git', 'packed-refs') if os.path.exists(packed_file): with open(packed_file, 'r') as fh: for line in fh: line = line.rstrip() if not line: continue if line[:1] in ('#', '^'): continue try: revision, ref = line.split(' ', 1) except ValueError: continue if ref == head: return text_type(revision) raise InvalidGitRepository( 'Unable to find ref to head "%s" in repository' % (head,)) fh = open(revision_file, 'r') try: return text_type(fh.read()).strip() finally: fh.close()
def to_unicode(value): try: value = text_type(force_text(value)) except (UnicodeEncodeError, UnicodeDecodeError): value = '(Error decoding value)' except Exception: # in some cases we get a different exception try: value = text_type(force_text(repr(type(value)))) except Exception: value = '(Error decoding value)' return value
def _emit(self, record, **kwargs): data, extra = extract_extra(record) stack = getattr(record, 'stack', None) if stack is True: stack = iter_stack_frames() if stack: stack = self._get_targetted_stack(stack, record) date = datetime.datetime.utcfromtimestamp(record.created) event_type = 'raven.events.Message' handler_kwargs = { 'params': record.args, } try: handler_kwargs['message'] = text_type(record.msg) except UnicodeDecodeError: # Handle binary strings where it should be unicode... handler_kwargs['message'] = repr(record.msg)[1:-1] try: handler_kwargs['formatted'] = text_type(record.message) except UnicodeDecodeError: # Handle binary strings where it should be unicode... handler_kwargs['formatted'] = repr(record.message)[1:-1] # If there's no exception being processed, exc_info may be a 3-tuple of None # http://docs.python.org/library/sys.html#sys.exc_info if record.exc_info and all(record.exc_info): # capture the standard message first so that we ensure # the event is recorded as an exception, in addition to having our # message interface attached handler = self.client.get_handler(event_type) data.update(handler.capture(**handler_kwargs)) event_type = 'raven.events.Exception' handler_kwargs = {'exc_info': record.exc_info} data['level'] = record.levelno data['logger'] = record.name kwargs['tags'] = tags = {} if self.tags: tags.update(self.tags) tags.update(getattr(record, 'tags', {})) kwargs.update(handler_kwargs) sample_rate = extra.pop('sample_rate', None) return self.client.capture( event_type, stack=stack, data=data, extra=extra, date=date, sample_rate=sample_rate, **kwargs)
def fetch_git_sha(path, head=None): """ >>> fetch_git_sha(os.path.dirname(__file__)) """ if not head: head_path = os.path.join(path, '.git', 'HEAD') if not os.path.exists(head_path): raise InvalidGitRepository( 'Cannot identify HEAD for git repository at %s' % (path, )) with open(head_path, 'r') as fp: head = text_type(fp.read()).strip() if head.startswith('ref: '): head = head[5:] revision_file = os.path.join(path, '.git', *head.split('/')) else: return head else: revision_file = os.path.join(path, '.git', 'refs', 'heads', head) if not os.path.exists(revision_file): if not os.path.exists(os.path.join(path, '.git')): raise InvalidGitRepository( '%s does not seem to be the root of a git repository' % (path, )) # Check for our .git/packed-refs' file since a `git gc` may have run # https://git-scm.com/book/en/v2/Git-Internals-Maintenance-and-Data-Recovery packed_file = os.path.join(path, '.git', 'packed-refs') if os.path.exists(packed_file): with open(packed_file, 'r') as fh: for line in fh: line = line.rstrip() if not line: continue if line[:1] in ('#', '^'): continue try: revision, ref = line.split(' ', 1) except ValueError: continue if ref == head: return text_type(revision) raise InvalidGitRepository( 'Unable to find ref to head "%s" in repository' % (head, )) fh = open(revision_file, 'r') try: return text_type(fh.read()).strip() finally: fh.close()
def test_import_string(): new_raven = import_string('raven') assert new_raven is raven # this will test unicode on python2 new_raven = import_string(text_type('raven')) assert new_raven is raven new_client = import_string('raven.Client') assert new_client is raven.Client # this will test unicode on python2 new_client = import_string(text_type('raven.Client')) assert new_client is raven.Client
def serialize(self, value, **kwargs): # try to return a reasonable string that can be decoded # correctly by the server so it doesn't show up as \uXXX for each # unicode character # e.g. we want the output to be like: "u'רונית מגן'" string_max_length = kwargs.get('string_max_length', None) return repr(text_type('%s')) % (value[:string_max_length],)
def processor(data): formatted_msg = msg format_args = args # Extract 'extra' key from kwargs and merge into data extra = kwargs.pop('extra', {}) data_value = kwargs data_value.update(extra) if args and len(args) == 1 and isinstance( args[0], collections.Mapping) and args[0]: format_args = args[0] data_value.update(format_args) # If people log bad things, this can happen. Then just don't do # anything. try: formatted_msg = text_type(msg) if format_args: formatted_msg = msg % format_args except Exception: pass # We do not want to include exc_info as argument because it often # lies (set to a constant value like 1 or True) or even if it's a # tuple it will not be particularly useful for us as we cannot # process it anyways. kwargs.pop('exc_info', None) data.update({ 'message': formatted_msg, 'category': logger.name, 'level': logging.getLevelName(level).lower(), 'data': data_value, })
def processor(data): formatted_msg = msg format_args = args # Extract 'extra' key from kwargs and merge into data extra = kwargs.pop('extra', {}) data_value = kwargs data_value.update(extra) if args and len(args) == 1 and isinstance(args[0], collections.Mapping) and args[0]: format_args = args[0] data_value.update(format_args) # If people log bad things, this can happen. Then just don't do # anything. try: formatted_msg = text_type(msg) if format_args: formatted_msg = msg % format_args except Exception: pass # We do not want to include exc_info as argument because it often # lies (set to a constant value like 1 or True) or even if it's a # tuple it will not be particularly useful for us as we cannot # process it anyways. kwargs.pop('exc_info', None) data.update({ 'message': formatted_msg, 'category': logger.name, 'level': logging.getLevelName(level).lower(), 'data': data_value, })
def recurse(self, value, max_depth=6, _depth=0, **kwargs): """ Given ``value``, recurse (using the parent serializer) to handle coercing of newly defined values. """ string_max_length = kwargs.get('string_max_length', None) _depth += 1 if _depth >= max_depth: try: value = text_type(repr(value))[:string_max_length] except Exception as e: import traceback traceback.print_exc() self.manager.logger.exception(e) return text_type(type(value)) return self.manager.transform(value, max_depth=max_depth, _depth=_depth, **kwargs)
def force_text(s, encoding='utf-8', strings_only=False, errors='strict'): """ Similar to smart_text, except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don't convert (some) non-string-like objects. """ # Handle the common case first, saves 30-40% when s is an instance of # text_type. This function gets called often in that setting. if isinstance(s, text_type): return s if strings_only and is_protected_type(s): return s try: if not isinstance(s, string_types): if hasattr(s, '__unicode__'): s = s.__unicode__() else: if not PY2: if isinstance(s, bytes): s = text_type(s, encoding, errors) else: s = text_type(s) else: s = text_type(bytes(s), encoding, errors) else: # Note: We use .decode() here, instead of text_type(s, encoding, # errors), so that if s is a SafeBytes, it ends up being a # SafeText at the end. s = s.decode(encoding, errors) except UnicodeDecodeError as e: if not isinstance(s, Exception): raise UnicodeDecodeError(*e.args) else: # If we get to here, the caller has passed in an Exception # subclass populated with non-ASCII bytestring data without a # working unicode method. Try to handle this without raising a # further exception by individually forcing the exception args # to unicode. s = ' '.join( [force_text(arg, encoding, strings_only, errors) for arg in s]) return s
def to_unicode(value): try: value = text_type(force_text(value)) except (UnicodeEncodeError, UnicodeDecodeError): value = "(Error decoding value)" except Exception: # in some cases we get a different exception try: value = binary_type(repr(type(value))) except Exception: value = "(Error decoding value)" return value
def force_text(s, encoding="utf-8", strings_only=False, errors="strict"): """ Similar to smart_text, except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don't convert (some) non-string-like objects. """ # Handle the common case first, saves 30-40% when s is an instance of # text_type. This function gets called often in that setting. if isinstance(s, text_type): return s if strings_only and is_protected_type(s): return s try: if not isinstance(s, string_types): if hasattr(s, "__unicode__"): s = s.__unicode__() else: if not PY2: if isinstance(s, bytes): s = text_type(s, encoding, errors) else: s = text_type(s) else: s = text_type(bytes(s), encoding, errors) else: # Note: We use .decode() here, instead of text_type(s, encoding, # errors), so that if s is a SafeBytes, it ends up being a # SafeText at the end. s = s.decode(encoding, errors) except UnicodeDecodeError as e: if not isinstance(s, Exception): raise UnicodeDecodeError(*e.args) else: # If we get to here, the caller has passed in an Exception # subclass populated with non-ASCII bytestring data without a # working unicode method. Try to handle this without raising a # further exception by individually forcing the exception args # to unicode. s = " ".join([force_text(arg, encoding, strings_only, errors) for arg in s]) return s
def serialize(self, value, **kwargs): # EPIC HACK # handles lazy model instances (which are proxy values that don't # easily give you the actual function) pre = value.__class__.__name__[1:] if hasattr(value, '%s__func' % pre): value = getattr(value, '%s__func' % pre)( *getattr(value, '%s__args' % pre), **getattr(value, '%s__kw' % pre)) else: return self.recurse(text_type(value)) return self.recurse(value, **kwargs)
def serialize(self, value, **kwargs): # EPIC HACK # handles lazy model instances (which are proxy values that don't # easily give you the actual function) pre = value.__class__.__name__[1:] if hasattr(value, '%s__func' % pre): value = getattr(value, '%s__func' % pre)(*getattr(value, '%s__args' % pre), **getattr(value, '%s__kw' % pre)) else: return self.recurse(text_type(value)) return self.recurse(value, **kwargs)
def test_dict_keys_utf8_as_unicode(self): x = { compat.text_type('\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'): 'bar' } result = transform(x) assert type(result) is dict keys = list(result.keys()) assert len(keys) == 1 if compat.PY3: expected = "'\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'" else: expected = "u'\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'" assert keys[0] == expected
def sanitize(self, item, value): if value is None: return if not item: # key can be a NoneType return value # Just in case we have bytes here, we want to make them into text # properly without failing so we can perform our check. if isinstance(item, bytes): item = item.decode('utf-8', 'replace') else: item = text_type(item) item = item.lower() for key in self.sanitize_keys: if key in item: # store mask as a fixed length for security return self.MASK return value
def processor(data): formatted_msg = msg # If people log bad things, this can happen. Then just don't do # anything. try: formatted_msg = text_type(msg) if args: formatted_msg = msg % args except Exception: pass # We do not want to include exc_info as argument because it often # lies (set to a constant value like 1 or True) or even if it's a # tuple it will not be particularly useful for us as we cannot # process it anyways. kwargs.pop('exc_info', None) data.update({ 'message': formatted_msg, 'category': logger.name, 'level': logging.getLevelName(level).lower(), 'data': kwargs, })
def sanitize(self, key, value): if value is None: return if isinstance(value, string_types) and self.VALUES_RE.match(value): return self.MASK if not key: # key can be a NoneType return value # Just in case we have bytes here, we want to make them into text # properly without failing so we can perform our check. if isinstance(key, bytes): key = key.decode('utf-8', 'replace') else: key = text_type(key) key = key.lower() for field in self.FIELDS: if field in key: # store mask as a fixed length for security return self.MASK return value
def __init__(self, dsn=None, raise_send_errors=False, transport=None, install_sys_hook=True, install_logging_hook=True, hook_libraries=None, enable_breadcrumbs=True, _random_seed=None, **options): global Raven o = options self._local_state = local() self.raise_send_errors = raise_send_errors # configure loggers first cls = self.__class__ self.state = ClientState() self.logger = logging.getLogger('%s.%s' % (cls.__module__, cls.__name__)) self.error_logger = logging.getLogger('sentry.errors') self.uncaught_logger = logging.getLogger('sentry.errors.uncaught') self._transport_cache = {} self.set_dsn(dsn, transport) self.include_paths = set(o.get('include_paths') or []) self.exclude_paths = set(o.get('exclude_paths') or []) self.name = text_type( o.get('name') or o.get('machine') or defaults.NAME) self.auto_log_stacks = bool( o.get('auto_log_stacks') or defaults.AUTO_LOG_STACKS) self.capture_locals = bool( o.get('capture_locals', defaults.CAPTURE_LOCALS)) self.string_max_length = int( o.get('string_max_length') or defaults.MAX_LENGTH_STRING) self.list_max_length = int( o.get('list_max_length') or defaults.MAX_LENGTH_LIST) self.site = o.get('site') self.include_versions = o.get('include_versions', True) self.processors = o.get('processors') if self.processors is None: self.processors = defaults.PROCESSORS context = o.get('context') if context is None: context = {'sys.argv': getattr(sys, 'argv', [])[:]} self.extra = context self.tags = o.get('tags') or {} self.environment = o.get('environment') or None self.release = o.get('release') or os.environ.get('HEROKU_SLUG_COMMIT') self.repos = self._format_repos(o.get('repos')) self.sample_rate = (o.get('sample_rate') if o.get('sample_rate') is not None else 1) self.transaction = TransactionStack() self.ignore_exceptions = set(o.get('ignore_exceptions') or ()) self.module_cache = ModuleProxyCache() self._random = Random(_random_seed) if not self.is_enabled(): self.logger.info( 'Raven is not configured (logging is disabled). Please see the' ' documentation for more information.') if Raven is None: Raven = self # We want to remember the creating thread id here because this # comes in useful for the context special handling self.main_thread_id = get_thread_ident() self.enable_breadcrumbs = enable_breadcrumbs from raven.context import Context self._context = Context(self) if install_sys_hook: self.install_sys_hook() if install_logging_hook: self.install_logging_hook() self.hook_libraries(hook_libraries)
def __unicode__(self): return text_type("%s: %s" % (self.message, self.code))
def test_real_gettext_lazy(self): d = {text_type('lazy_translation'): gettext_lazy(text_type('testing'))} key = "'lazy_translation'" if not PY2 else "u'lazy_translation'" value = "'testing'" if not PY2 else "u'testing'" assert transform(d) == {key: value}
def __init__(self, dsn=None, raise_send_errors=False, transport=None, install_sys_hook=True, install_logging_hook=True, hook_libraries=None, enable_breadcrumbs=True, **options): global Raven o = options self._local_state = local() self.raise_send_errors = raise_send_errors # configure loggers first cls = self.__class__ self.state = ClientState() self.logger = logging.getLogger( '%s.%s' % (cls.__module__, cls.__name__)) self.error_logger = logging.getLogger('sentry.errors') self.uncaught_logger = logging.getLogger('sentry.errors.uncaught') self._transport_cache = {} self.set_dsn(dsn, transport) self.include_paths = set(o.get('include_paths') or []) self.exclude_paths = set(o.get('exclude_paths') or []) self.name = text_type(o.get('name') or o.get('machine') or defaults.NAME) self.auto_log_stacks = bool( o.get('auto_log_stacks') or defaults.AUTO_LOG_STACKS) self.capture_locals = bool( o.get('capture_locals', defaults.CAPTURE_LOCALS)) self.string_max_length = int( o.get('string_max_length') or defaults.MAX_LENGTH_STRING) self.list_max_length = int( o.get('list_max_length') or defaults.MAX_LENGTH_LIST) self.site = o.get('site') self.include_versions = o.get('include_versions', True) self.processors = o.get('processors') if self.processors is None: self.processors = defaults.PROCESSORS context = o.get('context') if context is None: context = {'sys.argv': getattr(sys, 'argv', [])[:]} self.extra = context self.tags = o.get('tags') or {} self.environment = o.get('environment') or None self.release = o.get('release') or os.environ.get('HEROKU_SLUG_COMMIT') self.transaction = TransactionStack() self.ignore_exceptions = set(o.get('ignore_exceptions') or ()) self.module_cache = ModuleProxyCache() if not self.is_enabled(): self.logger.info( 'Raven is not configured (logging is disabled). Please see the' ' documentation for more information.') if Raven is None: Raven = self # We want to remember the creating thread id here because this # comes in useful for the context special handling self.main_thread_id = get_thread_ident() self.enable_breadcrumbs = enable_breadcrumbs from raven.context import Context self._context = Context(self) if install_sys_hook: self.install_sys_hook() if install_logging_hook: self.install_logging_hook() self.hook_libraries(hook_libraries)
class ProxyClient(object): """ A proxy which represents the currently client at all times. """ # introspection support: __members__ = property(lambda x: x.__dir__()) # Need to pretend to be the wrapped class, for the sake of objects that care # about this (especially in equality tests) __class__ = property(lambda x: get_client().__class__) __dict__ = property(lambda o: get_client().__dict__) __repr__ = lambda x: repr(get_client()) __getattr__ = lambda x, o: getattr(get_client(), o) __setattr__ = lambda x, o, v: setattr(get_client(), o, v) __delattr__ = lambda x, o: delattr(get_client(), o) __lt__ = lambda x, o: get_client() < o __le__ = lambda x, o: get_client() <= o __eq__ = lambda x, o: get_client() == o __ne__ = lambda x, o: get_client() != o __gt__ = lambda x, o: get_client() > o __ge__ = lambda x, o: get_client() >= o if PY2: __cmp__ = lambda x, o: cmp(get_client(), o) # NOQA __hash__ = lambda x: hash(get_client()) # attributes are currently not callable # __call__ = lambda x, *a, **kw: get_client()(*a, **kw) __nonzero__ = lambda x: bool(get_client()) __len__ = lambda x: len(get_client()) __getitem__ = lambda x, i: get_client()[i] __iter__ = lambda x: iter(get_client()) __contains__ = lambda x, i: i in get_client() __getslice__ = lambda x, i, j: get_client()[i:j] __add__ = lambda x, o: get_client() + o __sub__ = lambda x, o: get_client() - o __mul__ = lambda x, o: get_client() * o __floordiv__ = lambda x, o: get_client() // o __mod__ = lambda x, o: get_client() % o __divmod__ = lambda x, o: get_client().__divmod__(o) __pow__ = lambda x, o: get_client() ** o __lshift__ = lambda x, o: get_client() << o __rshift__ = lambda x, o: get_client() >> o __and__ = lambda x, o: get_client() & o __xor__ = lambda x, o: get_client() ^ o __or__ = lambda x, o: get_client() | o __div__ = lambda x, o: get_client().__div__(o) __truediv__ = lambda x, o: get_client().__truediv__(o) __neg__ = lambda x: -(get_client()) __pos__ = lambda x: +(get_client()) __abs__ = lambda x: abs(get_client()) __invert__ = lambda x: ~(get_client()) __complex__ = lambda x: complex(get_client()) __int__ = lambda x: int(get_client()) if PY2: __long__ = lambda x: long(get_client()) # NOQA __float__ = lambda x: float(get_client()) __str__ = lambda x: binary_type(get_client()) __unicode__ = lambda x: text_type(get_client()) __oct__ = lambda x: oct(get_client()) __hex__ = lambda x: hex(get_client()) __index__ = lambda x: get_client().__index__() __coerce__ = lambda x, o: x.__coerce__(x, o) __enter__ = lambda x: x.__enter__() __exit__ = lambda x, *a, **kw: x.__exit__(*a, **kw)
def __unicode__(self): return text_type(self.base_url)
def __str__(self): return text_type(self.base_url)