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 # six.text_type. This function gets called often in that setting. # # Adapted from Django if isinstance(s, six.text_type): return s if strings_only and is_protected_type(s): return s try: if not isinstance(s, six.string_types): if hasattr(s, '__unicode__'): s = s.__unicode__() else: if six.PY3: if isinstance(s, bytes): s = six.text_type(s, encoding, errors) else: s = six.text_type(s) else: s = six.text_type(bytes(s), encoding, errors) else: # Note: We use .decode() here, instead of six.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 test_dict_keys_utf8_as_unicode(self): x = { six.text_type('\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df'): 'bar' } result = transform(x) keys = list(result.keys()) self.assertEquals(len(keys), 1) self.assertTrue(type(keys[0]), str) self.assertEquals(keys[0], '\u05e8\u05d5\u05e0\u05d9\u05ea \u05de\u05d2\u05df')
def to_unicode(value): try: value = six.text_type(force_text(value)) except (UnicodeEncodeError, UnicodeDecodeError): value = '(Error decoding value)' except Exception: # in some cases we get a different exception try: value = six.binary_type(repr(type(value))) except Exception: value = '(Error decoding value)' return value
def __init__(self, organization_id=None, app_id=None, secret_token=None, include_paths=None, exclude_paths=None, timeout=None, hostname=None, auto_log_stacks=None, key=None, string_max_length=None, list_max_length=None, processors=None, servers=None, api_path=None, **kwargs): # configure loggers first cls = self.__class__ self.state = ClientState() self.logger = logging.getLogger('%s.%s' % (cls.__module__, cls.__name__)) self.error_logger = logging.getLogger('opbeat.errors') if organization_id is None and os.environ.get('OPBEAT_ORGANIZATION_ID'): msg = "Configuring opbeat from environment variable 'OPBEAT_ORGANIZATION_ID'" self.logger.info(msg) organization_id = os.environ['OPBEAT_ORGANIZATION_ID'] if secret_token is None and os.environ.get('OPBEAT_SECRET_TOKEN'): msg = "Configuring opbeat from environment variable 'OPBEAT_SECRET_TOKEN'" self.logger.info(msg) secret_token = os.environ['OPBEAT_SECRET_TOKEN'] self.servers = servers or defaults.SERVERS # servers may be set to a NoneType (for Django) if self.servers and not (organization_id and app_id and secret_token): msg = 'Missing configuration for client. Please see documentation.' self.logger.info(msg) self.include_paths = set(include_paths or defaults.INCLUDE_PATHS) self.exclude_paths = set(exclude_paths or defaults.EXCLUDE_PATHS) self.timeout = int(timeout or defaults.TIMEOUT) self.hostname = six.text_type(hostname or defaults.HOSTNAME) self.auto_log_stacks = bool(auto_log_stacks or defaults.AUTO_LOG_STACKS) self.string_max_length = int(string_max_length or defaults.MAX_LENGTH_STRING) self.list_max_length = int(list_max_length or defaults.MAX_LENGTH_LIST) self.organization_id = organization_id self.app_id = app_id self.secret_token = secret_token self.processors = processors or defaults.PROCESSORS self.module_cache = ModuleProxyCache()
class ProxyClient(object): """ A proxy which represents the currenty 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: 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 six.PY2: __cmp__ = lambda x, o: cmp(get_client(), o) __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 not six.PY2: __long__ = lambda x: long(get_client()) __float__ = lambda x: float(get_client()) __str__ = lambda x: str(get_client()) __unicode__ = lambda x: six.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 get_lines_from_file(filename, lineno, context_lines, loader=None, module_name=None): """ Returns context_lines before and after lineno from file. Returns (pre_context_lineno, pre_context, context_line, post_context). """ source = None if loader is not None and hasattr(loader, "get_source"): try: source = loader.get_source(module_name) except ImportError: # Traceback (most recent call last): # File "/Users/dcramer/Development/django-sentry/sentry/client/handlers.py", line 31, in emit # get_client().create_from_record(record, request=request) # File "/Users/dcramer/Development/django-sentry/sentry/client/base.py", line 325, in create_from_record # data['__opbeat__']['frames'] = varmap(shorten, get_stack_info(stack)) # File "/Users/dcramer/Development/django-sentry/sentry/utils/stacks.py", line 112, in get_stack_info # pre_context_lineno, pre_context, context_line, post_context = get_lines_from_file(filename, lineno, 7, loader, module_name) # File "/Users/dcramer/Development/django-sentry/sentry/utils/stacks.py", line 24, in get_lines_from_file # source = loader.get_source(module_name) # File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 287, in get_source # fullname = self._fix_name(fullname) # File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 262, in _fix_name # "module %s" % (self.fullname, fullname)) # ImportError: Loader for module cProfile cannot handle module __main__ source = None if source is not None: source = source.splitlines() if source is None: try: f = open(filename, 'rb') try: source = f.readlines() finally: f.close() except (OSError, IOError): pass if source is None: return None, None, None encoding = 'utf8' for line in source[:2]: # File coding may be specified. Match pattern from PEP-263 # (http://www.python.org/dev/peps/pep-0263/) match = _coding_re.search(line.decode('utf8')) # let's assume utf8 if match: encoding = match.group(1) break source = [ six.text_type(sline, encoding, 'replace') for sline in source ] lower_bound = max(0, lineno - context_lines) upper_bound = lineno + context_lines try: pre_context = [ line.strip('\r\n') for line in source[lower_bound:lineno] ] context_line = source[lineno].strip('\r\n') post_context = [ line.strip('\r\n') for line in source[(lineno + 1):upper_bound] ] except IndexError: # the file may have changed since it was loaded into memory return None, None, None return pre_context, context_line, post_context
def get_lines_from_file(filename, lineno, context_lines, loader=None, module_name=None): """ Returns context_lines before and after lineno from file. Returns (pre_context_lineno, pre_context, context_line, post_context). """ source = None if loader is not None and hasattr(loader, "get_source"): try: source = loader.get_source(module_name) except ImportError: # Traceback (most recent call last): # File "/Users/dcramer/Development/django-sentry/sentry/client/handlers.py", line 31, in emit # get_client().create_from_record(record, request=request) # File "/Users/dcramer/Development/django-sentry/sentry/client/base.py", line 325, in create_from_record # data['__opbeat__']['frames'] = varmap(shorten, get_stack_info(stack)) # File "/Users/dcramer/Development/django-sentry/sentry/utils/stacks.py", line 112, in get_stack_info # pre_context_lineno, pre_context, context_line, post_context = get_lines_from_file(filename, lineno, 7, loader, module_name) # File "/Users/dcramer/Development/django-sentry/sentry/utils/stacks.py", line 24, in get_lines_from_file # source = loader.get_source(module_name) # File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 287, in get_source # fullname = self._fix_name(fullname) # File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pkgutil.py", line 262, in _fix_name # "module %s" % (self.fullname, fullname)) # ImportError: Loader for module cProfile cannot handle module __main__ source = None if source is not None: source = source.splitlines() if source is None: try: f = open(filename, 'rb') try: source = f.readlines() finally: f.close() except (OSError, IOError): pass if source is None: return None, None, None encoding = 'utf8' for line in source[:2]: # File coding may be specified. Match pattern from PEP-263 # (http://www.python.org/dev/peps/pep-0263/) match = _coding_re.search(line.decode('utf8')) # let's assume utf8 if match: encoding = match.group(1) break source = [six.text_type(sline, encoding, 'replace') for sline in source] lower_bound = max(0, lineno - context_lines) upper_bound = lineno + context_lines try: pre_context = [line.strip('\r\n') for line in source[lower_bound:lineno]] context_line = source[lineno].strip('\r\n') post_context = [line.strip('\r\n') for line in source[(lineno + 1):upper_bound]] except IndexError: # the file may have changed since it was loaded into memory return None, None, None return pre_context, context_line, post_context
def __unicode__(self): return six.text_type(self.foo)
def __init__(self, organization_id=None, app_id=None, secret_token=None, transport_class=None, include_paths=None, exclude_paths=None, timeout=None, hostname=None, auto_log_stacks=None, key=None, string_max_length=None, list_max_length=None, processors=None, filter_exception_types=None, servers=None, api_path=None, asynk=None, async_mode=None, traces_send_freq_secs=None, transactions_ignore_patterns=None, framework_version='', **kwargs): # configure loggers first cls = self.__class__ self.logger = logging.getLogger('%s.%s' % (cls.__module__, cls.__name__)) self.error_logger = logging.getLogger('opbeat.errors') self.state = ClientState() if organization_id is None and os.environ.get( 'OPBEAT_ORGANIZATION_ID'): msg = "Configuring opbeat from environment variable 'OPBEAT_ORGANIZATION_ID'" self.logger.info(msg) organization_id = os.environ['OPBEAT_ORGANIZATION_ID'] if app_id is None and os.environ.get('OPBEAT_APP_ID'): msg = "Configuring opbeat from environment variable 'OPBEAT_APP_ID'" self.logger.info(msg) app_id = os.environ['OPBEAT_APP_ID'] if secret_token is None and os.environ.get('OPBEAT_SECRET_TOKEN'): msg = "Configuring opbeat from environment variable 'OPBEAT_SECRET_TOKEN'" self.logger.info(msg) secret_token = os.environ['OPBEAT_SECRET_TOKEN'] self.servers = servers or defaults.SERVERS if asynk is not None and async_mode is None: warnings.warn( 'Usage of "async" argument is deprecated. Use "async_mode"', category=DeprecationWarning, stacklevel=2, ) async_mode = asynk self.async_mode = (async_mode is True or (defaults.ASYNC_MODE and async_mode is not False)) if not transport_class: transport_class = (defaults.ASYNC_TRANSPORT_CLASS if self.async_mode else defaults.SYNC_TRANSPORT_CLASS) self._transport_class = import_string(transport_class) self._transports = {} # servers may be set to a NoneType (for Django) if self.servers and not (organization_id and app_id and secret_token): msg = 'Missing configuration for Opbeat client. Please see documentation.' self.logger.info(msg) self.is_send_disabled = (os.environ.get('OPBEAT_DISABLE_SEND', '').lower() in ('1', 'true')) if self.is_send_disabled: self.logger.info( 'Not sending any data to Opbeat due to OPBEAT_DISABLE_SEND ' 'environment variable') self.include_paths = set(include_paths or defaults.INCLUDE_PATHS) self.exclude_paths = set(exclude_paths or defaults.EXCLUDE_PATHS) self.timeout = int(timeout or defaults.TIMEOUT) self.hostname = six.text_type(hostname or defaults.HOSTNAME) self.auto_log_stacks = bool(auto_log_stacks or defaults.AUTO_LOG_STACKS) self.string_max_length = int(string_max_length or defaults.MAX_LENGTH_STRING) self.list_max_length = int(list_max_length or defaults.MAX_LENGTH_LIST) self.traces_send_freq_secs = (traces_send_freq_secs or defaults.TRACES_SEND_FREQ_SECS) self.organization_id = six.text_type(organization_id) self.app_id = six.text_type(app_id) self.secret_token = six.text_type(secret_token) self.filter_exception_types_dict = {} for exc_to_filter in (filter_exception_types or []): exc_to_filter_type = exc_to_filter.split(".")[-1] exc_to_filter_module = ".".join(exc_to_filter.split(".")[:-1]) self.filter_exception_types_dict[ exc_to_filter_type] = exc_to_filter_module if processors is None: self.processors = defaults.PROCESSORS else: self.processors = processors self._framework_version = framework_version self.module_cache = ModuleProxyCache() self.instrumentation_store = RequestsStore( lambda: self.get_stack_info_for_trace(iter_stack_frames(), False), self.traces_send_freq_secs, transactions_ignore_patterns) atexit_register(self.close)