當前位置: 首頁>>代碼示例>>Python>>正文


Python reflect.safe_str方法代碼示例

本文整理匯總了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 
開發者ID:warner,項目名稱:magic-wormhole,代碼行數:21,代碼來源:outbound.py

示例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! 
開發者ID:kenorb-contrib,項目名稱:BitTorrent,代碼行數:21,代碼來源:log.py

示例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 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:28,代碼來源:_producer_helpers.py

示例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 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:41,代碼來源:log.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:7,代碼來源:failure.py

示例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)) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:5,代碼來源:test_reflect.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:6,代碼來源:test_reflect.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:9,代碼來源:test_reflect.py

示例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')) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:9,代碼來源:test_reflect.py

示例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)) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:9,代碼來源:test_reflect.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:6,代碼來源:test_reflect.py

示例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()) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:7,代碼來源:test_reflect.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:14,代碼來源:test_reflect.py

示例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) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:14,代碼來源:test_reflect.py

示例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 !!') 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:10,代碼來源:test_reflect.py


注:本文中的twisted.python.reflect.safe_str方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。