本文整理匯總了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)
示例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><%s></pre><br />'
'Resource: <pre><%s></pre><br />'
'Value: <pre>my>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)
示例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)
示例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))
示例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
示例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)
)
示例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)
示例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))
示例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())
示例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])
示例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())
示例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)
示例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)
示例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)
示例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)