def __str__(self):
return "nfc.llcp.Error: [{0}] {1}".format(
errno.errorcode[self.errno], self.strerror)
python类errorcode()的实例源码
def __str__(self):
return "nfc.llcp.ConnectRefused: [{0}] {1} with reason {2}".format(
errno.errorcode[self.errno], self.strerror, self.reason)
def str_errno(self):
code = self.get_errno()
if code is None:
return 'Errno: no errno support'
return 'Errno=%s (%s)' % (os.strerror(code), errno.errorcode[code])
def str_errno(self):
code = self.get_errno()
if code is None:
return 'Errno: no errno support'
return 'Errno=%s (%s)' % (os.strerror(code), errno.errorcode[code])
def test_using_errorcode(self):
# Every key value in errno.errorcode should be on the module.
for value in errno.errorcode.values():
self.assertTrue(hasattr(errno, value),
'no %s attr in errno' % value)
def test_attributes_in_errorcode(self):
for attribute in errno.__dict__.keys():
if attribute.isupper():
self.assertIn(getattr(errno, attribute), errno.errorcode,
'no %s attr in errno.errorcode' % attribute)
def handle_stream_closed_error(self, error, event):
if error.real_error:
err_num = abs(error.real_error[0])
try:
errorcode = errno.errorcode[err_num]
except KeyError:
errorcode = "undefined(code={0})".format(err_num)
logger.debug("connect to {0}:{1} with error code {2}".format(
event.addr, event.port, errorcode))
# NOTE: if we submit an incorrect address type,
# the error code will be:
# - ENOEXEC in macos.
# - EBADF in linux.
if err_num in (errno.ENOEXEC, errno.EBADF):
reason = "ADDRESS_TYPE_NOT_SUPPORTED"
elif err_num == errno.ETIMEDOUT:
reason = "NETWORK_UNREACHABLE"
else:
logger.error("unhandled error code {0} received".format(errorcode))
reason = "GENRAL_FAILURE"
yield self.send_event_to_src_conn(Response(
RESP_STATUS[reason],
event.atyp, event.addr, event.port), raise_exception=False)
raise DestNotConnectedError((event.addr, event.port))
else: # pragma: no cover
# TODO: StreamClosedError without real_error?
# need check that tornado would occur this situation?
raise
def test_using_errorcode(self):
# Every key value in errno.errorcode should be on the module.
for value in errno.errorcode.itervalues():
self.assertTrue(hasattr(errno, value), 'no %s attr in errno' % value)
def test_attributes_in_errorcode(self):
for attribute in errno.__dict__.iterkeys():
if attribute.isupper():
self.assertIn(getattr(errno, attribute), errno.errorcode,
'no %s attr in errno.errorcode' % attribute)
def test_using_errorcode(self):
# Every key value in errno.errorcode should be on the module.
for value in errno.errorcode.itervalues():
self.assertTrue(hasattr(errno, value), 'no %s attr in errno' % value)
def test_attributes_in_errorcode(self):
for attribute in errno.__dict__.iterkeys():
if attribute.isupper():
self.assertIn(getattr(errno, attribute), errno.errorcode,
'no %s attr in errno.errorcode' % attribute)
def _acceptFailureTest(self, socketErrorNumber):
"""
Test behavior in the face of an exception from C{accept(2)}.
On any exception which indicates the platform is unable or unwilling
to allocate further resources to us, the existing port should remain
listening, a message should be logged, and the exception should not
propagate outward from doRead.
@param socketErrorNumber: The errno to simulate from accept.
"""
class FakeSocket(object):
"""
Pretend to be a socket in an overloaded system.
"""
def accept(self):
raise socket.error(
socketErrorNumber, os.strerror(socketErrorNumber))
factory = ServerFactory()
port = self.port(0, factory, interface='127.0.0.1')
originalSocket = port.socket
try:
port.socket = FakeSocket()
port.doRead()
expectedFormat = "Could not accept new connection (%s)"
expectedErrorCode = errno.errorcode[socketErrorNumber]
expectedMessage = expectedFormat % (expectedErrorCode,)
for msg in self.messages:
if msg.get('message') == (expectedMessage,):
break
else:
self.fail("Log event for failed accept not found in "
"%r" % (self.messages,))
finally:
port.socket = originalSocket
def __str__(self):
return "nfc.llcp.Error: [{0}] {1}".format(
errno.errorcode[self.errno], self.strerror)
def __str__(self):
return "nfc.llcp.ConnectRefused: [{0}] {1} with reason {2}".format(
errno.errorcode[self.errno], self.strerror, self.reason)
def test_using_errorcode(self):
# Every key value in errno.errorcode should be on the module.
for value in errno.errorcode.values():
self.assertTrue(hasattr(errno, value),
'no %s attr in errno' % value)
def test_attributes_in_errorcode(self):
for attribute in errno.__dict__.keys():
if attribute.isupper():
self.assertIn(getattr(errno, attribute), errno.errorcode,
'no %s attr in errno.errorcode' % attribute)
def test_errno_mapping(self):
# The OSError constructor maps errnos to subclasses
# A sample test for the basic functionality
e = OSError(EEXIST, "Bad file descriptor")
self.assertIs(type(e), FileExistsError)
# Exhaustive testing
for errcode, exc in self._map.items():
e = OSError(errcode, "Some message")
self.assertIs(type(e), exc)
othercodes = set(errno.errorcode) - set(self._map)
for errcode in othercodes:
e = OSError(errcode, "Some message")
self.assertIs(type(e), OSError)
def _IOR(type, nr, struct):
request = _IOC(_IOC_READ, ord(type), nr, ctypes.sizeof(struct))
def f(fileno):
buffer = struct()
if c.ioctl(fileno, request, ctypes.byref(buffer)) < 0:
err = ctypes.c_int.in_dll(c, 'errno').value
raise OSError(err, errno.errorcode[err])
return buffer
return f
def _IOR_len(type, nr):
def f(fileno, buffer):
request = _IOC(_IOC_READ, ord(type), nr, ctypes.sizeof(buffer))
if c.ioctl(fileno, request, ctypes.byref(buffer)) < 0:
err = ctypes.c_int.in_dll(c, 'errno').value
raise OSError(err, errno.errorcode[err])
return buffer
return f
def socket_connect(descriptor, address):
"""
Attempts to connect to the address, returns the descriptor if it succeeds,
returns None if it needs to trampoline, and raises any exceptions.
"""
err = descriptor.connect_ex(address)
if err in CONNECT_ERR:
return None
if err not in CONNECT_SUCCESS:
raise socket.error(err, errno.errorcode[err])
return descriptor