[Python-checkins] python/dist/src/Lib socket.py,1.27,1.28
gvanrossum@users.sourceforge.net
gvanrossum@users.sourceforge.net
Thu, 08 Aug 2002 08:17:13 -0700
Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv6833
Modified Files:
socket.py
Log Message:
Another refactoring. Changed 'socket' from being a factory function
to being a new-style class, to be more similar to the socket class
in the _socket module; it is now the same as the _socketobject class.
Added __slots__. Added docstrings, copied from the real socket class
where possible.
The _fileobject class is now also a new-style class with __slots__
(though without docstrings). The mode, name, softspace, bufsize and
closed attributes are properly supported (closed as a property; name
as a class attributes; the softspace, mode and bufsize as slots).
Index: socket.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/socket.py,v
retrieving revision 1.27
retrieving revision 1.28
diff -C2 -d -r1.27 -r1.28
*** socket.py 8 Aug 2002 01:02:16 -0000 1.27
--- socket.py 8 Aug 2002 15:16:20 -0000 1.28
***************
*** 44,53 ****
from _socket import *
! SSL_EXISTS = 1
try:
- import _ssl
from _ssl import *
except ImportError:
! SSL_EXISTS = 0
import os, sys
--- 44,53 ----
from _socket import *
! _have_ssl = False
try:
from _ssl import *
+ _have_ssl = True
except ImportError:
! pass
import os, sys
***************
*** 57,81 ****
# XXX shouldn't there be something similar to the above for _ssl exports?
if (sys.platform.lower().startswith("win")
or (hasattr(os, 'uname') and os.uname()[0] == "BeOS")
or sys.platform=="riscos"):
! _realsocketcall = _socket.socket
!
! def socket(family=AF_INET, type=SOCK_STREAM, proto=0):
! return _socketobject(_realsocketcall(family, type, proto))
! socket.__doc__ = _realsocketcall.__doc__
! if SSL_EXISTS:
! _realsslcall = _ssl.ssl
def ssl(sock, keyfile=None, certfile=None):
if hasattr(sock, "_sock"):
sock = sock._sock
! return _realsslcall(sock, keyfile, certfile)
!
! del _socket
! if SSL_EXISTS:
! del _ssl
! del SSL_EXISTS
# WSA error codes
--- 57,74 ----
# XXX shouldn't there be something similar to the above for _ssl exports?
+ _realsocket = socket
+ _needwrapper = False
if (sys.platform.lower().startswith("win")
or (hasattr(os, 'uname') and os.uname()[0] == "BeOS")
or sys.platform=="riscos"):
! _needwrapper = True
! if _have_ssl:
! _realssl = ssl
def ssl(sock, keyfile=None, certfile=None):
if hasattr(sock, "_sock"):
sock = sock._sock
! return _realssl(sock, keyfile, certfile)
# WSA error codes
***************
*** 141,156 ****
'settimeout', 'gettimeout', 'shutdown')
! class _socketobject:
! class _closedsocket:
def __getattr__(self, name):
raise error(9, 'Bad file descriptor')
! def __init__(self, sock):
! self._sock = sock
def close(self):
# Avoid referencing globals here
self._sock = self.__class__._closedsocket()
def __del__(self):
--- 134,157 ----
'settimeout', 'gettimeout', 'shutdown')
! class _socketobject(object):
! __doc__ = _realsocket.__doc__
!
! __slots__ = ["_sock"]
!
! class _closedsocket(object):
! __slots__ = []
def __getattr__(self, name):
raise error(9, 'Bad file descriptor')
! def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, _sock=None):
! if _sock is None:
! _sock = _realsocket(family, type, proto)
! self._sock = _sock
def close(self):
# Avoid referencing globals here
self._sock = self.__class__._closedsocket()
+ close.__doc__ = _realsocket.close.__doc__
def __del__(self):
***************
*** 159,185 ****
def accept(self):
sock, addr = self._sock.accept()
! return _socketobject(sock), addr
def dup(self):
! return _socketobject(self._sock)
def makefile(self, mode='r', bufsize=-1):
return _fileobject(self._sock, mode, bufsize)
! _s = "def %s(self, *args): return self._sock.%s(*args)\n\n"
for _m in _socketmethods:
! exec _s % (_m, _m)
! class _fileobject:
"""Faux file object attached to a socket object."""
default_bufsize = 8192
def __init__(self, sock, mode='rb', bufsize=-1):
self._sock = sock
! self._mode = mode # Not actually used in this version
if bufsize < 0:
bufsize = self.default_bufsize
if bufsize == 0:
self._rbufsize = 1
--- 160,204 ----
def accept(self):
sock, addr = self._sock.accept()
! return _socketobject(_sock=sock), addr
! accept.__doc__ = _realsocket.accept.__doc__
def dup(self):
! """dup() -> socket object
!
! Return a new socket object connected to the same system resource."""
! return _socketobject(_sock=self._sock)
def makefile(self, mode='r', bufsize=-1):
+ """makefile([mode[, bufsize]]) -> file object
+
+ Return a regular file object corresponding to the socket. The mode
+ and bufsize arguments are as for the built-in open() function."""
return _fileobject(self._sock, mode, bufsize)
! _s = ("def %s(self, *args): return self._sock.%s(*args)\n\n"
! "%s.__doc__ = _realsocket.%s.__doc__\n")
for _m in _socketmethods:
! exec _s % (_m, _m, _m, _m)
+ if _needwrapper:
+ socket = _socketobject
! class _fileobject(object):
"""Faux file object attached to a socket object."""
default_bufsize = 8192
+ name = "<socket>"
+
+ __slots__ = ["mode", "bufsize", "softspace",
+ # "closed" is a property, see below
+ "_sock", "_rbufsize", "_wbufsize", "_rbuf", "_wbuf"]
def __init__(self, sock, mode='rb', bufsize=-1):
self._sock = sock
! self.mode = mode # Not actually used in this version
if bufsize < 0:
bufsize = self.default_bufsize
+ self.bufsize = bufsize
+ self.softspace = False
if bufsize == 0:
self._rbufsize = 1
***************
*** 192,195 ****
--- 211,218 ----
self._rbuf = []
self._wbuf = []
+
+ def _getclosed(self):
+ return self._sock is not None
+ closed = property(_getclosed, doc="True if the file is closed")
def close(self):