annotate Lib/plat-win/socket.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents efe86891f411
children 5dbffc7844f5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3728
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 "Socket wrapper for Windows, which does not support dup()."
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # (And hence, fromfd() and makefile() are unimplemented in C....)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # XXX Living dangerously here -- close() is implemented by deleting a
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # reference. Thus we rely on the real _socket module to close on
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # deallocation, and also hope that nobody keeps a reference to our _sock
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # member.
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 try:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 from _socket import *
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 except ImportError:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 from socket import *
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 _realsocketcall = socket
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 def socket(family, type, proto=0):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 return _socketobject(_realsocketcall(family, type, proto))
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 class _socketobject:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 def __init__(self, sock):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 self._sock = sock
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 def close(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 self._sock = None
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 def __del__(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 self.close()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 def accept(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 sock, addr = self._sock.accept()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 return _socketobject(sock), addr
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 def dup(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 return _socketobject(self._sock)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def makefile(self, mode='r', bufsize=-1):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 return _fileobject(self._sock, mode, bufsize)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 _s = "def %s(self, *args): return apply(self._sock.%s, args)\n\n"
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 for _m in ('bind', 'connect', 'fileno', 'listen',
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 'getpeername', 'getsockname',
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 'getsockopt', 'setsockopt',
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 'recv', 'recvfrom', 'send', 'sendto',
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 'setblocking',
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 'shutdown'):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 exec _s % (_m, _m)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
54
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 class _fileobject:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 def __init__(self, sock, mode, bufsize):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 self._sock = sock
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 self._mode = mode
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 if bufsize < 0:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 bufsize = 512
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 self._rbufsize = max(1, bufsize)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 self._wbufsize = bufsize
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 self._wbuf = self._rbuf = ""
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 def close(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 try:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 if self._sock:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 self.flush()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 finally:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 self._sock = None
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
72
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def __del__(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 self.close()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 def flush(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 if self._wbuf:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 self._sock.send(self._wbuf)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 self._wbuf = ""
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 def fileno(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 return self._sock.fileno()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 def write(self, data):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 self._wbuf = self._wbuf + data
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 if self._wbufsize == 1:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 if '\n' in data:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 self.flush()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 else:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if len(self._wbuf) >= self._wbufsize:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 self.flush()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 def writelines(self, list):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 filter(self._sock.send, list)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 self.flush()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
96
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 def read(self, n=-1):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 if n >= 0:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 while len(self._rbuf) < n:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 new = self._sock.recv(self._rbufsize)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 if not new: break
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 self._rbuf = self._rbuf + new
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 data, self._rbuf = self._rbuf[:n], self._rbuf[n:]
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 return data
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 while 1:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 new = self._sock.recv(self._rbufsize)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 if not new: break
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 self._rbuf = self._rbuf + new
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 data, self._rbuf = self._rbuf, ""
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 return data
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
111
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 def readline(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 import string
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 data = ""
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 i = string.find(self._rbuf, '\n')
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 while i < 0:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 new = self._sock.recv(self._rbufsize)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 if not new: break
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 i = string.find(new, '\n')
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 if i >= 0: i = i + len(self._rbuf)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 self._rbuf = self._rbuf + new
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 if i < 0: i = len(self._rbuf)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 else: i = i+1
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 data, self._rbuf = self._rbuf[:i], self._rbuf[i:]
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 return data
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
126
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 def readlines(self):
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 list = []
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 while 1:
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 line = self.readline()
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 if not line: break
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 list.append(line)
6f3c7b2f2b8d socket wrapper module around _socket for all Window platforms
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 return list