当前位置: 首页>>代码示例>>Python>>正文


Python reflect.safe_str函数代码示例

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


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


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