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


Python reflect.safe_repr方法代码示例

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


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

示例1: test_unsignedID

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def test_unsignedID(self):
        """
        L{unsignedID} is used to print ID of the object in case of error, not
        standard ID value which can be negative.
        """
        class X(BTBase):
            breakRepr = True

        ids = {X: 100}
        def fakeID(obj):
            try:
                return ids[obj]
            except (TypeError, KeyError):
                return id(obj)
        self.addCleanup(util.setIDFunction, util.setIDFunction(fakeID))

        xRepr = reflect.safe_repr(X)
        self.assertIn("0x64", xRepr) 
开发者ID:kuri65536,项目名称:python-for-android,代码行数:20,代码来源:test_reflect.py

示例2: test_noBytesResult

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def test_noBytesResult(self):
        """
        When implemented C{render} method does not return bytes an internal
        server error is returned.
        """
        class RiggedRepr(object):
            def __repr__(self):
                return 'my>repr'

        result = RiggedRepr()
        no_bytes_resource = resource.Resource()
        no_bytes_resource.render = lambda request: result
        request = self._getReq(no_bytes_resource)

        request.requestReceived(b"GET", b"/newrender", b"HTTP/1.0")

        headers, body = request.transport.written.getvalue().split(b'\r\n\r\n')
        self.assertEqual(request.code, 500)
        expected = [
            '',
            '<html>',
            '  <head><title>500 - Request did not return bytes</title></head>',
            '  <body>',
            '    <h1>Request did not return bytes</h1>',
            '    <p>Request: <pre>&lt;%s&gt;</pre><br />'
                'Resource: <pre>&lt;%s&gt;</pre><br />'
                'Value: <pre>my&gt;repr</pre></p>' % (
                    reflect.safe_repr(request)[1:-1],
                    reflect.safe_repr(no_bytes_resource)[1:-1],
                    ),
            '  </body>',
            '</html>',
            '']
        self.assertEqual('\n'.join(expected).encode('ascii'), body) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:36,代码来源:test_web.py

示例3: __str__

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def __str__(self):
        if self._str is not None:
            return self._str
        if hasattr(self, 'func'):
            # This code should be replaced by a utility function in reflect;
            # see ticket #6066:
            if hasattr(self.func, '__qualname__'):
                func = self.func.__qualname__
            elif hasattr(self.func, '__name__'):
                func = self.func.func_name
                if hasattr(self.func, 'im_class'):
                    func = self.func.im_class.__name__ + '.' + func
            else:
                func = reflect.safe_repr(self.func)
        else:
            func = None

        now = self.seconds()
        L = ["<DelayedCall 0x%x [%ss] called=%s cancelled=%s" % (
                id(self), self.time - now, self.called,
                self.cancelled)]
        if func is not None:
            L.extend((" ", func, "("))
            if self.args:
                L.append(", ".join([reflect.safe_repr(e) for e in self.args]))
                if self.kw:
                    L.append(", ")
            if self.kw:
                L.append(", ".join(['%s=%s' % (k, reflect.safe_repr(v)) for (k, v) in self.kw.items()]))
            L.append(")")

        if self.debug:
            L.append("\n\ntraceback at creation: \n\n%s" % ('    '.join(self.creator)))
        L.append('>')

        return "".join(L) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:38,代码来源:base.py

示例4: __repr__

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def __repr__(self):
        if hasattr(self.f, '__qualname__'):
            func = self.f.__qualname__
        elif hasattr(self.f, '__name__'):
            func = self.f.__name__
            if hasattr(self.f, 'im_class'):
                func = self.f.im_class.__name__ + '.' + func
        else:
            func = reflect.safe_repr(self.f)

        return 'LoopingCall<%r>(%s, *%s, **%s)' % (
            self.interval, func, reflect.safe_repr(self.a),
            reflect.safe_repr(self.kw)) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:15,代码来源:task.py

示例5: _safeReprVars

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def _safeReprVars(varsDictItems):
    """
    Convert a list of (name, object) pairs into (name, repr) pairs.

    L{twisted.python.reflect.safe_repr} is used to generate the repr, so no
    exceptions will be raised by faulty C{__repr__} methods.

    @param varsDictItems: a sequence of (name, value) pairs as returned by e.g.
        C{locals().items()}.
    @returns: a sequence of (name, repr) pairs.
    """
    return [(name, reflect.safe_repr(obj)) for (name, obj) in varsDictItems]


# slyphon: make post-morteming exceptions tweakable 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:17,代码来源:failure.py

示例6: formatUnformattableEvent

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def formatUnformattableEvent(event, error):
    """
    Formats an event as a L{unicode} that describes the event generically and a
    formatting error.

    @param event: A logging event.
    @type event: L{dict}

    @param error: The formatting error.
    @type error: L{Exception}

    @return: A formatted string.
    @rtype: L{unicode}
    """
    try:
        return (
            u"Unable to format event {event!r}: {error}"
            .format(event=event, error=error)
        )
    except BaseException:
        # Yikes, something really nasty happened.
        #
        # Try to recover as much formattable data as possible; hopefully at
        # least the namespace is sane, which will help you find the offending
        # logger.
        failure = Failure()

        text = u", ".join(
            u" = ".join((safe_repr(key), safe_repr(value)))
            for key, value in event.items()
        )

        return (
            u"MESSAGE LOST: unformattable object logged: {error}\n"
            u"Recoverable data: {text}\n"
            u"Exception during formatting:\n{failure}"
            .format(error=safe_repr(error), failure=failure, text=text)
        ) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:40,代码来源:_format.py

示例7: test_brokenRepr

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def test_brokenRepr(self):
        """
        L{reflect.safe_repr} returns a string with class name, address, and
        traceback when the repr call failed.
        """
        b = Breakable()
        b.breakRepr = True
        bRepr = reflect.safe_repr(b)
        self.assertIn("Breakable instance at 0x", bRepr)
        # Check that the file is in the repr, but without the extension as it
        # can be .py/.pyc
        self.assertIn(os.path.splitext(__file__)[0], bRepr)
        self.assertIn("RuntimeError: repr!", bRepr) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:15,代码来源:test_reflect.py

示例8: test_brokenStr

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def test_brokenStr(self):
        """
        L{reflect.safe_repr} isn't affected by a broken C{__str__} method.
        """
        b = Breakable()
        b.breakStr = True
        self.assertEqual(reflect.safe_repr(b), repr(b)) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:9,代码来源:test_reflect.py

示例9: test_brokenClassRepr

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def test_brokenClassRepr(self):
        class X(BTBase):
            breakRepr = True
        reflect.safe_repr(X)
        reflect.safe_repr(X()) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:7,代码来源:test_reflect.py

示例10: test_brokenReprIncludesID

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def test_brokenReprIncludesID(self):
        """
        C{id} is used to print the ID of the object in case of an error.

        L{safe_repr} includes a traceback after a newline, so we only check
        against the first line of the repr.
        """
        class X(BTBase):
            breakRepr = True

        xRepr = reflect.safe_repr(X)
        xReprExpected = ('<BrokenType instance at 0x%x with repr error:'
                         % (id(X),))
        self.assertEqual(xReprExpected, xRepr.split('\n')[0]) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:16,代码来源:test_reflect.py

示例11: test_brokenClassStr

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def test_brokenClassStr(self):
        class X(BTBase):
            breakStr = True
        reflect.safe_repr(X)
        reflect.safe_repr(X()) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:7,代码来源:test_reflect.py

示例12: test_brokenClassNameAttribute

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
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_repr}
        returns 'BROKEN CLASS' instead of the class name.
        """
        class X(BTBase):
            breakName = True
        xRepr = reflect.safe_repr(X())
        self.assertIn("<BROKEN CLASS AT 0x", xRepr)
        self.assertIn(os.path.splitext(__file__)[0], xRepr)
        self.assertIn("RuntimeError: repr!", xRepr) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:14,代码来源:test_reflect.py

示例13: _callable_repr

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def _callable_repr(method):
    """Get a useful representation ``method``."""
    try:
        return fullyQualifiedName(method)
    except AttributeError:
        return safe_repr(method) 
开发者ID:ClusterHQ,项目名称:flocker,代码行数:8,代码来源:_retry.py

示例14: __str__

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def __str__(self):
        if self._str is not None:
            return self._str
        if hasattr(self, 'func'):
            if hasattr(self.func, 'func_name'):
                func = self.func.func_name
                if hasattr(self.func, 'im_class'):
                    func = self.func.im_class.__name__ + '.' + func
            else:
                func = reflect.safe_repr(self.func)
        else:
            func = None

        now = self.seconds()
        L = ["<DelayedCall %s [%ss] called=%s cancelled=%s" % (
                id(self), self.time - now, self.called, self.cancelled)]
        if func is not None:
            L.extend((" ", func, "("))
            if self.args:
                L.append(", ".join([reflect.safe_repr(e) for e in self.args]))
                if self.kw:
                    L.append(", ")
            if self.kw:
                L.append(", ".join([
                    '%s=%s' % (k, reflect.safe_repr(v))
                    for (k, v) in iteritems(self.kw)]))
            L.append(")")

        if self.debug:
            L.append(("\n\ntraceback at creation: \n\n%s"
                      ) % ('    '.join(self.creator)))
        L.append('>')

        return "".join(L) 
开发者ID:CanonicalLtd,项目名称:landscape-client,代码行数:36,代码来源:clock.py

示例15: __str__

# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_repr [as 别名]
def __str__(self):
        if self._str is not None:
            return self._str
        if hasattr(self, 'func'):
            if hasattr(self.func, 'func_name'):
                func = self.func.func_name
                if hasattr(self.func, 'im_class'):
                    func = self.func.im_class.__name__ + '.' + func
            else:
                func = reflect.safe_repr(self.func)
        else:
            func = None

        now = self.seconds()
        L = ["<DelayedCall 0x%x [%ss] called=%s cancelled=%s" % (
                unsignedID(self), self.time - now, self.called,
                self.cancelled)]
        if func is not None:
            L.extend((" ", func, "("))
            if self.args:
                L.append(", ".join([reflect.safe_repr(e) for e in self.args]))
                if self.kw:
                    L.append(", ")
            if self.kw:
                L.append(", ".join(['%s=%s' % (k, reflect.safe_repr(v)) for (k, v) in self.kw.iteritems()]))
            L.append(")")

        if self.debug:
            L.append("\n\ntraceback at creation: \n\n%s" % ('    '.join(self.creator)))
        L.append('>')

        return "".join(L) 
开发者ID:kuri65536,项目名称:python-for-android,代码行数:34,代码来源:base.py


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