• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python reflect.safe_str函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中twisted.python.reflect.safe_str函数的典型用法代码示例。如果您正苦于以下问题:Python safe_str函数的具体用法?Python safe_str怎么用?Python safe_str使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了safe_str函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: _pull

    def _pull(self):
        """
        A generator that calls C{resumeProducing} on the underlying producer
        forever.

        If C{resumeProducing} throws an exception, the producer is
        unregistered, which should result in streaming stopping.
        """
        while True:
            try:
                self._producer.resumeProducing()
            except:
                log.err(None, "%s failed, producing will be stopped:" %
                        (safe_str(self._producer),))
                try:
                    self._consumer.unregisterProducer()
                    # The consumer should now call stopStreaming() on us,
                    # thus stopping the streaming.
                except:
                    # Since the consumer blew up, we may not have had
                    # stopStreaming() called, so we just stop on our own:
                    log.err(None, "%s failed to unregister producer:" %
                            (safe_str(self._consumer),))
                    self._finished = True
                    return
            yield None
开发者ID:esabelhaus,项目名称:secret-octo-dubstep,代码行数:26,代码来源:tls.py


示例2: test_workingUtf8_3

 def test_workingUtf8_3(self):
     """
     L{safe_str} for C{bytes} with utf-8 encoded data should return
     the value decoded into C{str}.
     """
     x = b't\xc3\xbcst'
     self.assertEqual(reflect.safe_str(x), x.decode('utf-8'))
开发者ID:JohnDoes95,项目名称:project_parser,代码行数:7,代码来源:test_reflect.py


示例3: test_brokenUtf8

 def test_brokenUtf8(self):
     """
     Use str() for non-utf8 bytes: "b'non-utf8'"
     """
     x = b'\xff'
     xStr = reflect.safe_str(x)
     self.assertEqual(xStr, str(x))
开发者ID:JohnDoes95,项目名称:project_parser,代码行数:7,代码来源:test_reflect.py


示例4: test_workingAscii

 def test_workingAscii(self):
     """
     L{safe_str} for C{str} with ascii-only data should return the
     value unchanged.
     """
     x = 'a'
     self.assertEqual(reflect.safe_str(x), 'a')
开发者ID:JohnDoes95,项目名称:project_parser,代码行数:7,代码来源:test_reflect.py


示例5: test_workingUtf8_2

 def test_workingUtf8_2(self):
     """
     L{safe_str} for C{str} with utf-8 encoded data should return the
     value unchanged.
     """
     x = b't\xc3\xbcst'
     self.assertEqual(reflect.safe_str(x), x)
开发者ID:JohnDoes95,项目名称:project_parser,代码行数:7,代码来源:test_reflect.py


示例6: getErrorMessage

 def getErrorMessage(self):
     """
     Get a string of the exception which caused this Failure.
     """
     if isinstance(self.value, Failure):
         return self.value.getErrorMessage()
     return reflect.safe_str(self.value)
开发者ID:JohnDoes95,项目名称:project_parser,代码行数:7,代码来源:failure.py


示例7: error_page

def error_page(request, resrc, value, tb=None):
    result = "Request: %s<br />\nResource: %s<br />\nValue: %s" % (
        html.PRE(reflect.safe_repr(request)),
        html.PRE(reflect.safe_repr(resrc)),
        html.PRE(reflect.safe_repr(value)),
    )
    if tb:
        result += '\n%s' % html.PRE(reflect.safe_str(tb))
    return result
开发者ID:hitsl,项目名称:bouser,代码行数:9,代码来源:request.py


示例8: test_brokenClassAttribute

 def test_brokenClassAttribute(self):
     """
     If an object raises an exception when accessing its C{__class__}
     attribute, L{reflect.safe_str} uses C{type} to retrieve the class
     object.
     """
     b = NoClassAttr()
     b.breakStr = True
     bStr = reflect.safe_str(b)
     self.assertIn("NoClassAttr instance at 0x", bStr)
     self.assertIn(os.path.splitext(__file__)[0], bStr)
     self.assertIn("RuntimeError: str!", bStr)
开发者ID:Alberto-Beralix,项目名称:Beralix,代码行数:12,代码来源:test_reflect.py


示例9: write_error

        def write_error(failure):
            if failure.check(APIError):
                status = failure.value.status

                # Don't log the stack traces for 4xx responses.
                if status < 400 or status >= 500:
                    log.err(failure)
                else:
                    log.msg("status: %s message: %s" % (
                        status, safe_str(failure.value)))

                bytes = failure.value.response
                if bytes is None:
                    bytes = self.dump_error(failure.value, request)
            else:
                log.err(failure)
                bytes = safe_str(failure.value)
                status = 500
            request.setResponseCode(status)
            request.write(bytes)
            request.finish()
开发者ID:GP89,项目名称:txaws,代码行数:21,代码来源:resource.py


示例10: test_brokenClassNameAttribute

 def test_brokenClassNameAttribute(self):
     """
     If a class raises an exception when accessing its C{__name__} attribute
     B{and} when calling its C{__str__} implementation, L{reflect.safe_str}
     returns 'BROKEN CLASS' instead of the class name.
     """
     class X(BTBase):
         breakName = True
     xStr = reflect.safe_str(X())
     self.assertIn("<BROKEN CLASS AT 0x", xStr)
     self.assertIn(os.path.splitext(__file__)[0], xStr)
     self.assertIn("RuntimeError: str!", xStr)
开发者ID:Alberto-Beralix,项目名称:Beralix,代码行数:12,代码来源:test_reflect.py


示例11: augmentWithFailure

def augmentWithFailure(eventDict, failure, why=None):
    """
    Augment a log event with exception information.
    """
    eventDict['excText'] = failure.getTraceback()
    eventDict['excType'] = reflect.qual(failure.type)
    eventDict['excValue'] = reflect.safe_str(failure.value)
    eventDict.setdefault('logLevel', 'ERROR')

    eventDict['message'] = (why or
                            eventDict['excValue'] or
                            eventDict['excType'])
开发者ID:mochi,项目名称:udplog,代码行数:12,代码来源:udplog.py


示例12: printTraceback

    def printTraceback(self, file=None, elideFrameworkCode=0, detail='default'):
        """Emulate Python's standard error reporting mechanism.
        """
        if file is None:
            file = log.logerr
        w = file.write

        # Preamble
        if detail == 'verbose':
            w( '*--- Failure #%d%s---\n' %
               (self.count,
                (self.pickled and ' (pickled) ') or ' '))
        elif detail == 'brief':
            if self.frames:
                hasFrames = 'Traceback'
            else:
                hasFrames = 'Traceback (failure with no frames)'
            w("%s: %s: %s\n" % (hasFrames, self.type, self.value))
        else:
            w( 'Traceback (most recent call last):\n')

        # Frames, formatted in appropriate style
        if self.frames:
            if not elideFrameworkCode:
                format_frames(self.stack[-traceupLength:], w, detail)
                w("%s\n" % (EXCEPTION_CAUGHT_HERE,))
            format_frames(self.frames, w, detail)
        elif not detail == 'brief':
            # Yeah, it's not really a traceback, despite looking like one...
            w("Failure: ")

        # postamble, if any
        if not detail == 'brief':
            # Unfortunately, self.type will not be a class object if this
            # Failure was created implicitly from a string exception.
            # qual() doesn't make any sense on a string, so check for this
            # case here and just write out the string if that's what we
            # have.
            if isinstance(self.type, (str, unicode)):
                w(self.type + "\n")
            else:
                w("%s: %s\n" % (reflect.qual(self.type),
                                reflect.safe_str(self.value)))
        # chaining
        if isinstance(self.value, Failure):
            # TODO: indentation for chained failures?
            file.write(" (chained Failure)\n")
            self.value.printTraceback(file, elideFrameworkCode, detail)
        if detail == 'verbose':
            w('*--- End of Failure #%d ---\n' % self.count)
开发者ID:radical-software,项目名称:radicalspam,代码行数:50,代码来源:failure.py


示例13: assertDefaultTraceback

    def assertDefaultTraceback(self, captureVars=False):
        """
        Assert that L{printTraceback} produces and prints a default traceback.

        The default traceback consists of a header::

          Traceback (most recent call last):

        The body with traceback::

          File "/twisted/trial/_synctest.py", line 1180, in _run
             runWithWarningsSuppressed(suppress, method)

        And the footer::

          --- <exception caught here> ---
            File "twisted/test/test_failure.py", line 39, in getDivisionFailure
              1/0
            exceptions.ZeroDivisionError: float division

        @param captureVars: Enables L{Failure.captureVars}.
        @type captureVars: C{bool}
        """
        if captureVars:
            exampleLocalVar = 'xyzzy'
            # Silence the linter as this variable is checked via
            # the traceback.
            exampleLocalVar

        f = getDivisionFailure(captureVars=captureVars)
        out = NativeStringIO()
        f.printTraceback(out)
        tb = out.getvalue()
        stack = ''
        for method, filename, lineno, localVars, globalVars in f.frames:
            stack += '  File "%s", line %s, in %s\n' % (filename, lineno,
                                                        method)
            stack += '    %s\n' % (linecache.getline(
                                   filename, lineno).strip(),)

        self.assertTracebackFormat(tb,
            "Traceback (most recent call last):",
            "%s\n%s%s: %s\n" % (failure.EXCEPTION_CAUGHT_HERE, stack,
            reflect.qual(f.type), reflect.safe_str(f.value)))

        if captureVars:
            self.assertEqual(None, re.search('exampleLocalVar.*xyzzy', tb))
开发者ID:12019,项目名称:OpenWrt_Luci_Lua,代码行数:47,代码来源:test_failure.py


示例14: emit

    def emit(self, record):
        """
        Emit a record.
        """
        try:
            eventDict = {
                    'category': self.category,
                    'logLevel': record.levelname,
                    'logName': record.name,
                    'filename': record.pathname,
                    'lineno': record.lineno,
                    'funcName': record.funcName,
                    'timestamp': record.created,
                    }

            if isinstance(record.args, dict):
                eventDict.update(record.args)

            extra = {name: value for name, value in vars(record).iteritems()
                     if name not in _DEFAULT_LOGGING_ATTRIBUTES}

            eventDict.update(extra)

            # Format the message for its side effects and extract the message
            # and exception information
            self.format(record)
            eventDict['message'] = record.message
            if record.exc_info:
                excType, excValue = record.exc_info[0:2]
                eventDict['excValue'] = reflect.safe_str(excValue)
                if excValue is None:
                    eventDict['excText'] = None
                    eventDict['excType'] = 'NoneType'
                else:
                    eventDict['excText'] = record.exc_text
                    eventDict['excType'] = reflect.qual(excType)

            # Extract the category, possibly overridden from record.args.
            category =  eventDict['category']
            del eventDict['category']


            self.logger.log(category, eventDict)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record)
开发者ID:mochi,项目名称:udplog,代码行数:47,代码来源:udplog.py


示例15: textFromEventDict

def textFromEventDict(eventDict):
    """
    Extract text from an event dict passed to a log observer. If it cannot
    handle the dict, it returns None.

    The possible keys of eventDict are:
     - C{message}: by default, it holds the final text. It's required, but can
       be empty if either C{isError} or C{format} is provided (the first
       having the priority).
     - C{isError}: boolean indicating the nature of the event.
     - C{failure}: L{failure.Failure} instance, required if the event is an
       error.
     - C{why}: if defined, used as header of the traceback in case of errors.
     - C{format}: string format used in place of C{message} to customize
       the event. It uses all keys present in C{eventDict} to format
       the text.
    Other keys will be used when applying the C{format}, or ignored.
    """
    edm = eventDict['message']
    if not edm:
        if eventDict['isError'] and 'failure' in eventDict:
            why = eventDict.get('why')
            if why:
                why = reflect.safe_str(why)
            else:
                why = 'Unhandled Error'
            try:
                traceback = eventDict['failure'].getTraceback()
            except Exception as e:
                traceback = '(unable to obtain traceback): ' + str(e)
            text = (why + '\n' + traceback)
        elif 'format' in eventDict:
            text = _safeFormat(eventDict['format'], eventDict)
        else:
            # We don't know how to log this
            return None
    else:
        text = ' '.join(map(reflect.safe_str, edm))
    return text
开发者ID:alfonsjose,项目名称:international-orders-app,代码行数:39,代码来源:log.py


示例16: write_error

        def write_error(failure):
            if failure.check(APIError):
                status = failure.value.status

                # Don't log the stack traces for 4xx responses.
                if status < 400 or status >= 500:
                    log.err(failure)
                else:
                    log.msg("status: %s message: %s" % (status, safe_str(failure.value)))

                body = failure.value.response
                if body is None:
                    body = self.dump_error(failure.value, request)
            else:
                # If the error is a generic one (not an APIError), log the
                # message , but don't send it back to the client, as it could
                # contain sensitive information. Send a generic server error
                # message instead.
                log.err(failure)
                body = "Server error"
                status = 500
            request.setResponseCode(status)
            write_response(body)
开发者ID:antisvin,项目名称:txAWS,代码行数:23,代码来源:resource.py


示例17: testBrokenStr

 def testBrokenStr(self):
     b = Breakable()
     b.breakStr = True
     reflect.safe_str(b)
开发者ID:AnthonyNystrom,项目名称:YoGoMee,代码行数:4,代码来源:test_reflect.py


示例18: emit

    def emit(self, eventDict):
        """
        Log an event.

        This converts C{eventDict} so that it can be serialized to JSON and
        sent over UDP to the logging server.

        The key C{'time'} that is automatically provided by Twisted is renamed
        to C{'timestamp'} that is used in UDP log.

        When Twisted logs an error, the associated Failure is in the
        C{eventDict} with key C{'failure'}. For warnings, C{'warning'} holds
        the warning class and its arguments, and C{'filename'}, C{'lineno'} the
        location where the warning was reported from. See
        L{twisted.python.log.textFromEventDict} for how C{'format'} is used to
        render failures and warnings.

        See L{twisted.python.log.ILogObserver}.
        """
        eventDict = eventDict.copy()
        eventDict['message'] = log.textFromEventDict(eventDict)
        eventDict['timestamp'] = eventDict['time']

        if 'warning' in eventDict:
            # Twisted passes the warning category in 'category' and the
            # warning instance in 'warning'. Override message to only contain
            # actual warning message and put the category in 'warning'.
            eventDict['message'] = reflect.safe_str(eventDict['warning'])
            eventDict['warningCategory'] = eventDict['category']
            eventDict.setdefault('logLevel', 'WARNING')
            del eventDict['category']
            del eventDict['warning']

        if 'isError' in eventDict:
            eventDict['isError'] = bool(eventDict['isError'])

        if eventDict.get('isError', False) and 'failure' in eventDict:
            # Twisted passed the failure instance in 'failure'. Add a field
            # 'excType' containing the exception type and remove 'failure'.
            # We always want to render the traceback in a separate field, so we
            # override the actual message that textFromEventDict created for
            # us.

            udplog.augmentWithFailure(eventDict,
                                      eventDict['failure'],
                                      eventDict['why']
                                      )
            del eventDict['why']
            del eventDict['failure']

        eventDict.setdefault('logLevel', 'INFO')

        # Clean up unneeded Twisted specific keys.
        #   * time is replaced by timeformat
        #   * format, if present, is used by textFromEventDict.
        for key in ('time', 'format'):
            if key in eventDict:
                del eventDict[key]

        category = eventDict.get('category', self.defaultCategory)

        self.logger.log(category, eventDict)
开发者ID:mailgun,项目名称:udplog,代码行数:62,代码来源:twisted.py


示例19: test_brokenClassRepr

 def test_brokenClassRepr(self):
     class X(BTBase):
         breakRepr = True
     reflect.safe_str(X)
     reflect.safe_str(X())
开发者ID:Alberto-Beralix,项目名称:Beralix,代码行数:5,代码来源:test_reflect.py


示例20: test_brokenRepr

 def test_brokenRepr(self):
     b = Breakable()
     b.breakRepr = True
     reflect.safe_str(b)
开发者ID:Alberto-Beralix,项目名称:Beralix,代码行数:4,代码来源:test_reflect.py



注:本文中的twisted.python.reflect.safe_str函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python runtime.seconds函数代码示例发布时间:2022-05-27
下一篇:
Python reflect.qual函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap