Commit a1458850 authored by Denis Bilenko's avatar Denis Bilenko

socket: import from the stdlib's socket everything that we should be importing

- add __implements__, __extensions__ and __imports__
- do not add 'ssl' and 'sslerror' to __all__ if on Python >= 2.6
- add 'SSLType' to __all__ if on Python <= 2.5
parent ead3939c
...@@ -31,26 +31,45 @@ For convenience, exceptions (like :class:`error <socket.error>` and :class:`time ...@@ -31,26 +31,45 @@ For convenience, exceptions (like :class:`error <socket.error>` and :class:`time
as well as the constants from :mod:`socket` module are imported into this module. as well as the constants from :mod:`socket` module are imported into this module.
""" """
# standard functions and classes that this module re-implements in a gevent-aware way:
__all__ = ['create_connection', __implements__ = ['create_connection',
'error',
'fromfd',
'gaierror',
'getaddrinfo', 'getaddrinfo',
'gethostbyname', 'gethostbyname',
'socket',
'SocketType',
'fromfd',
'socketpair']
# non-standard functions that this module provides:
__extensions__ = ['wait_read',
'wait_write',
'wait_readwrite']
# standard functions and classes that this module re-imports
__imports__ = ['error',
'gaierror',
'getfqdn',
'herror',
'htonl',
'htons',
'ntohl',
'ntohs',
'inet_aton', 'inet_aton',
'inet_ntoa', 'inet_ntoa',
'inet_pton', 'inet_pton',
'inet_ntop', 'inet_ntop',
'socket',
'socketpair',
'timeout', 'timeout',
'ssl', 'gethostname',
'sslerror', 'getprotobyname',
'SocketType', 'getservbyname',
'wait_read', 'getservbyport',
'wait_write', 'getdefaulttimeout',
'wait_readwrite'] 'setdefaulttimeout',
# Python 2.5 and older:
'RAND_add',
'RAND_egd',
'RAND_status']
import sys import sys
import time import time
...@@ -83,49 +102,26 @@ except ImportError: ...@@ -83,49 +102,26 @@ except ImportError:
EBADF = 9 EBADF = 9
import _socket import _socket
error = _socket.error
timeout = _socket.timeout
_realsocket = _socket.socket _realsocket = _socket.socket
__socket__ = __import__('socket') __socket__ = __import__('socket')
_fileobject = __socket__._fileobject _fileobject = __socket__._fileobject
gaierror = _socket.gaierror
# Import public constants from the standard socket (called __socket__ here) into this module. for name in __imports__[:]:
try:
value = getattr(__socket__, name)
globals()[name] = value
except AttributeError:
__imports__.remove(name)
for name in __socket__.__all__: for name in __socket__.__all__:
if name[:1].isupper():
value = getattr(__socket__, name) value = getattr(__socket__, name)
if isinstance(value, (int, long, basestring)): if isinstance(value, (int, long, basestring)):
globals()[name] = value globals()[name] = value
__all__.append(name) __imports__.append(name)
elif name == 'getfqdn':
globals()[name] = getattr(__socket__, name)
__all__.append(name)
del name, value del name, value
inet_ntoa = _socket.inet_ntoa
inet_aton = _socket.inet_aton
try:
inet_ntop = _socket.inet_ntop
except AttributeError:
def inet_ntop(address_family, packed_ip):
if address_family == AF_INET:
return inet_ntoa(packed_ip)
# XXX: ipv6 won't work on windows
raise NotImplementedError('inet_ntop() is not available on this platform')
try:
inet_pton = _socket.inet_pton
except AttributeError:
def inet_pton(address_family, ip_string):
if address_family == AF_INET:
return inet_aton(ip_string)
# XXX: ipv6 won't work on windows
raise NotImplementedError('inet_ntop() is not available on this platform')
# XXX: import other non-blocking stuff, like ntohl
# XXX: implement blocking functions that are not yet implemented # XXX: implement blocking functions that are not yet implemented
# XXX: add test that checks that socket.__all__ matches gevent.socket.__all__ on all supported platforms
from gevent.hub import getcurrent, get_hub from gevent.hub import getcurrent, get_hub
from gevent import core from gevent import core
...@@ -721,16 +717,17 @@ else: ...@@ -721,16 +717,17 @@ else:
_have_ssl = False _have_ssl = False
try: try:
from gevent.ssl import sslwrap_simple as ssl, SSLError as sslerror from gevent.ssl import sslwrap_simple as ssl, SSLError as sslerror, SSLSocket as SSLType
_have_ssl = True _have_ssl = True
except ImportError: except ImportError:
try: try:
from gevent.sslold import ssl, sslerror from gevent.sslold import ssl, sslerror, SSLObject as SSLType
_have_ssl = True _have_ssl = True
except ImportError: except ImportError:
pass pass
if not _have_ssl: if sys.version_info[:2] <= (2, 5) and _have_ssl:
__all__.remove('ssl') __implements__.extend(['ssl', 'sslerror', 'SSLType'])
__all__.remove('sslerror')
__all__ = __implements__ + __extensions__ + __imports__
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment