本文整理汇总了Python中twisted.python.reflect.safe_str方法的典型用法代码示例。如果您正苦于以下问题:Python reflect.safe_str方法的具体用法?Python reflect.safe_str怎么用?Python reflect.safe_str使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.reflect
的用法示例。
在下文中一共展示了reflect.safe_str方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _pull
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
def _pull(self):
while True:
try:
self._producer.resumeProducing()
except Exception:
log.err(None, "%s failed, producing will be stopped:" %
(safe_str(self._producer),))
try:
self._unregister()
# The consumer should now call stopStreaming() on us,
# thus stopping the streaming.
except Exception:
# 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._unregister),))
self._finished = True
return
yield None
示例2: emit
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
def emit(self, eventDict):
edm = eventDict['message']
if not edm:
if eventDict['isError'] and eventDict.has_key('failure'):
text = eventDict['failure'].getTraceback()
elif eventDict.has_key('format'):
text = self._safeFormat(eventDict['format'], eventDict)
else:
# we don't know how to log this
return
else:
text = ' '.join(map(reflect.safe_str, edm))
timeStr = time.strftime(self.timeFormat, time.localtime(eventDict['time']))
fmtDict = {'system': eventDict['system'], 'text': text.replace("\n", "\n\t")}
msgStr = self._safeFormat("[%(system)s] %(text)s\n", fmtDict)
util.untilConcludes(self.write, timeStr + " " + msgStr)
util.untilConcludes(self.flush) # Hoorj!
示例3: _pull
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
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
示例4: textFromEventDict
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
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
示例5: getErrorMessage
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
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)
示例6: test_workingStr
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
def test_workingStr(self):
x = [1, 2, 3]
self.assertEqual(reflect.safe_str(x), str(x))
示例7: test_brokenStr
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
def test_brokenStr(self):
b = Breakable()
b.breakStr = True
reflect.safe_str(b)
示例8: test_workingUtf8_2
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
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)
示例9: test_workingUtf8_3
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
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'))
示例10: test_brokenUtf8
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
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))
示例11: test_brokenRepr
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
def test_brokenRepr(self):
b = Breakable()
b.breakRepr = True
reflect.safe_str(b)
示例12: test_brokenClassStr
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
def test_brokenClassStr(self):
class X(BTBase):
breakStr = True
reflect.safe_str(X)
reflect.safe_str(X())
示例13: test_brokenClassAttribute
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
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)
示例14: test_brokenClassNameAttribute
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [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_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)
示例15: test_unicode
# 需要导入模块: from twisted.python import reflect [as 别名]
# 或者: from twisted.python.reflect import safe_str [as 别名]
def test_unicode(self):
"""
A unicode string is encoded to ``ascii`` with
``backslashreplace`` error handling on Python 2.
"""
unicodeString = u'\N{DOUBLE EXCLAMATION MARK} !!'
safe = reflect.safe_str(unicodeString)
self.assertEqual(safe, b'\u203c !!')