本文整理匯總了Python中twisted.python.failure.trap方法的典型用法代碼示例。如果您正苦於以下問題:Python failure.trap方法的具體用法?Python failure.trap怎麽用?Python failure.trap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類twisted.python.failure
的用法示例。
在下文中一共展示了failure.trap方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ftp_PORT
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def ftp_PORT(self, address):
addr = tuple(map(int, address.split(',')))
ip = '%d.%d.%d.%d' % tuple(addr[:4])
port = addr[4] << 8 | addr[5]
# if we have a DTP port set up, lose it.
if self.dtpFactory is not None:
self.cleanupDTP()
self.dtpFactory = DTPFactory(pi=self, peerHost=self.transport.getPeer().host)
self.dtpFactory.setTimeout(self.dtpTimeout)
self.dtpPort = reactor.connectTCP(ip, port, self.dtpFactory)
def connected(ignored):
return ENTERING_PORT_MODE
def connFailed(err):
err.trap(PortConnectionError)
return CANT_OPEN_DATA_CNX
return self.dtpFactory.deferred.addCallbacks(connected, connFailed)
示例2: testFailedStartTLS
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def testFailedStartTLS(self):
failures = []
def breakServerTLS(ign):
self.server.canStartTLS = False
self.connected.addCallback(breakServerTLS)
self.connected.addCallback(lambda ign: self.client.startTLS())
self.connected.addErrback(
lambda err: failures.append(err.trap(imap4.IMAP4Exception)))
self.connected.addCallback(self._cbStopClient)
self.connected.addErrback(self._ebGeneral)
def check(ignored):
self.assertTrue(failures)
self.assertIdentical(failures[0], imap4.IMAP4Exception)
return self.loopback().addCallback(check)
示例3: test_serverTimeout
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def test_serverTimeout(self):
"""
The *client* has a timeout mechanism which will close connections that
are inactive for a period.
"""
c = Clock()
self.server.timeoutTest = True
self.client.timeout = 5 #seconds
self.client.callLater = c.callLater
self.selectedArgs = None
def login():
d = self.client.login(b'testuser', b'password-test')
c.advance(5)
d.addErrback(timedOut)
return d
def timedOut(failure):
self._cbStopClient(None)
failure.trap(error.TimeoutError)
d = self.connected.addCallback(strip(login))
d.addErrback(self._ebGeneral)
return defer.gatherResults([d, self.loopback()])
示例4: _ebRoundRobinBackoff
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def _ebRoundRobinBackoff(self, failure, fakeProto):
failure.trap(defer.TimeoutError)
# Assert that each server is tried with a particular timeout
# before the timeout is increased and the attempts are repeated.
for t in (1, 3, 11, 45):
tries = fakeProto.queries[:len(self.testServers)]
del fakeProto.queries[:len(self.testServers)]
tries.sort()
expected = list(self.testServers)
expected.sort()
for ((addr, query, timeout, id), expectedAddr) in zip(tries, expected):
self.assertEqual(addr, (expectedAddr, 53))
self.assertEqual(timeout, t)
self.assertFalse(fakeProto.queries)
示例5: test_errbackAddedBeforeTimeoutSuppressesCancellation
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def test_errbackAddedBeforeTimeoutSuppressesCancellation(self):
"""
An errback added before a timeout is added errbacks with a
L{defer.CancelledError} when the timeout fires. If the
errback suppresses the L{defer.CancelledError}, the deferred
successfully completes.
"""
clock = Clock()
d = defer.Deferred()
dErrbacked = [None]
def errback(f):
dErrbacked[0] = f
f.trap(defer.CancelledError)
d.addErrback(errback)
d.addTimeout(10, clock)
clock.advance(15)
self.assertIsInstance(dErrbacked[0], failure.Failure)
self.assertIsInstance(dErrbacked[0].value, defer.CancelledError)
self.successResultOf(d)
示例6: test_waitUntilLockedWithTimeoutUnlocked
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def test_waitUntilLockedWithTimeoutUnlocked(self):
"""
Test that a lock can be acquired while a lock is held
but the lock is unlocked before our timeout.
"""
def onTimeout(f):
f.trap(defer.TimeoutError)
self.fail("Should not have timed out")
self.assertTrue(self.lock.lock())
self.clock.callLater(1, self.lock.unlock)
d = self.lock.deferUntilLocked(timeout=10)
d.addErrback(onTimeout)
self.clock.pump([1] * 10)
return d
示例7: ftp_PORT
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def ftp_PORT(self, address):
addr = map(int, address.split(','))
ip = '%d.%d.%d.%d' % tuple(addr[:4])
port = addr[4] << 8 | addr[5]
# if we have a DTP port set up, lose it.
if self.dtpFactory is not None:
self.cleanupDTP()
self.dtpFactory = DTPFactory(pi=self, peerHost=self.transport.getPeer().host)
self.dtpFactory.setTimeout(self.dtpTimeout)
self.dtpPort = reactor.connectTCP(ip, port, self.dtpFactory)
def connected(ignored):
return ENTERING_PORT_MODE
def connFailed(err):
err.trap(PortConnectionError)
return CANT_OPEN_DATA_CNX
return self.dtpFactory.deferred.addCallbacks(connected, connFailed)
示例8: test_serverTimeout
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def test_serverTimeout(self):
"""
The *client* has a timeout mechanism which will close connections that
are inactive for a period.
"""
c = Clock()
self.server.timeoutTest = True
self.client.timeout = 5 #seconds
self.client.callLater = c.callLater
self.selectedArgs = None
def login():
d = self.client.login('testuser', 'password-test')
c.advance(5)
d.addErrback(timedOut)
return d
def timedOut(failure):
self._cbStopClient(None)
failure.trap(error.TimeoutError)
d = self.connected.addCallback(strip(login))
d.addErrback(self._ebGeneral)
return defer.gatherResults([d, self.loopback()])
示例9: testServerTimeout
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def testServerTimeout(self):
self.server.timeoutTest = True
self.client.timeout = 5 #seconds
self.selectedArgs = None
def login():
d = self.client.login('testuser', 'password-test')
d.addErrback(timedOut)
return d
def timedOut(failure):
self._cbStopClient(None)
failure.trap(error.TimeoutError)
d = self.connected.addCallback(strip(login))
d.addErrback(self._ebGeneral)
self.loopback()
示例10: ftp_PASS
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def ftp_PASS(self, password):
"""
Second part of login. Get the password the peer wants to
authenticate with.
"""
if self.factory.allowAnonymous and self._user == self.factory.userAnonymous:
# anonymous login
creds = credentials.Anonymous()
reply = GUEST_LOGGED_IN_PROCEED
else:
# user login
creds = credentials.UsernamePassword(self._user, password)
reply = USR_LOGGED_IN_PROCEED
del self._user
def _cbLogin(result):
(interface, avatar, logout) = result
assert interface is IFTPShell, "The realm is busted, jerk."
self.shell = avatar
self.logout = logout
self.workingDirectory = []
self.state = self.AUTHED
return reply
def _ebLogin(failure):
failure.trap(cred_error.UnauthorizedLogin, cred_error.UnhandledCredentials)
self.state = self.UNAUTH
raise AuthorizationError
d = self.portal.login(creds, None, IFTPShell)
d.addCallbacks(_cbLogin, _ebLogin)
return d
示例11: _unwrapFirstError
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def _unwrapFirstError(failure):
failure.trap(defer.FirstError)
return failure.value.subFailure
示例12: _rcodeTest
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def _rcodeTest(self, rcode, exc):
m = dns.Message(rCode=rcode)
err = self.resolver.filterAnswers(m)
err.trap(exc)
示例13: test_circularChainException
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def test_circularChainException(self):
"""
If the deprecation warning for circular deferred callbacks is
configured to be an error, the exception will become the failure
result of the Deferred.
"""
self.addCleanup(setattr, warnings, "filters", warnings.filters)
warnings.filterwarnings("error", category=DeprecationWarning)
d = defer.Deferred()
def circularCallback(result):
return d
d.addCallback(circularCallback)
d.callback("foo")
failure = self.failureResultOf(d)
failure.trap(DeprecationWarning)
示例14: _check
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def _check(self):
"""
Check the output of the log observer to see if the error is present.
"""
c2 = self._loggedErrors()
self.assertEqual(len(c2), 2)
c2[1]["failure"].trap(ZeroDivisionError)
self.flushLoggedErrors(ZeroDivisionError)
示例15: test_chainedErrorCleanup
# 需要導入模塊: from twisted.python import failure [as 別名]
# 或者: from twisted.python.failure import trap [as 別名]
def test_chainedErrorCleanup(self):
"""
If one Deferred with an error result is returned from a callback on
another Deferred, when the first Deferred is garbage collected it does
not log its error.
"""
d = defer.Deferred()
d.addCallback(lambda ign: defer.fail(RuntimeError("zoop")))
d.callback(None)
# Sanity check - this isn't too interesting, but we do want the original
# Deferred to have gotten the failure.
results = []
errors = []
d.addCallbacks(results.append, errors.append)
self.assertEqual(results, [])
self.assertEqual(len(errors), 1)
errors[0].trap(Exception)
# Get rid of any references we might have to the inner Deferred (none of
# these should really refer to it, but we're just being safe).
del results, errors, d
# Force a collection cycle so that there's a chance for an error to be
# logged, if it's going to be logged.
gc.collect()
# And make sure it is not.
self.assertEqual(self._loggedErrors(), [])