DebugClients/Python3/AsyncFile.py

changeset 945
8cd4d08fa9f6
parent 791
9ec2ac20e54e
child 1213
5d4e67e59ed3
equal deleted inserted replaced
944:1b59c4ba121e 945:8cd4d08fa9f6
68 Private method to write a specific number of pending bytes. 68 Private method to write a specific number of pending bytes.
69 69
70 @param n the number of bytes to be written (int) 70 @param n the number of bytes to be written (int)
71 """ 71 """
72 if n: 72 if n:
73 try : 73 try:
74 buf = "{0!s}{1!s}".format(self.wpending[:n], EOT) 74 buf = "{0!s}{1!s}".format(self.wpending[:n], EOT)
75 try: 75 try:
76 buf = buf.encode('utf8', 'backslashreplace') 76 buf = buf.encode('utf8', 'backslashreplace')
77 except (UnicodeEncodeError, UnicodeDecodeError): 77 except (UnicodeEncodeError, UnicodeDecodeError):
78 pass 78 pass
80 self.wpending = self.wpending[n:] 80 self.wpending = self.wpending[n:]
81 self.nWriteErrors = 0 81 self.nWriteErrors = 0
82 except socket.error: 82 except socket.error:
83 self.nWriteErrors += 1 83 self.nWriteErrors += 1
84 if self.nWriteErrors > self.maxtries: 84 if self.nWriteErrors > self.maxtries:
85 self.wpending = '' # delete all output 85 self.wpending = '' # delete all output
86 86
87 def pendingWrite(self): 87 def pendingWrite(self):
88 """ 88 """
89 Public method that returns the number of bytes waiting to be written. 89 Public method that returns the number of bytes waiting to be written.
90 90
91 @return the number of bytes to be written (int) 91 @return the number of bytes to be written (int)
92 """ 92 """
93 return self.wpending.rfind('\n') + 1 93 return self.wpending.rfind('\n') + 1
94 94
95 def close(self, closeit = False): 95 def close(self, closeit=False):
96 """ 96 """
97 Public method to close the file. 97 Public method to close the file.
98 98
99 @param closeit flag to indicate a close ordered by the debugger code (boolean) 99 @param closeit flag to indicate a close ordered by the debugger code (boolean)
100 """ 100 """
126 try: 126 try:
127 return self.sock.fileno() 127 return self.sock.fileno()
128 except socket.error: 128 except socket.error:
129 return -1 129 return -1
130 130
131 def read_p(self, size = -1): 131 def read_p(self, size=-1):
132 """ 132 """
133 Public method to read bytes from this file. 133 Public method to read bytes from this file.
134 134
135 @param size maximum number of bytes to be read (int) 135 @param size maximum number of bytes to be read (int)
136 @return the bytes read (any) 136 @return the bytes read (any)
140 if size < 0: 140 if size < 0:
141 size = 20000 141 size = 20000
142 142
143 return self.sock.recv(size).decode('utf8') 143 return self.sock.recv(size).decode('utf8')
144 144
145 def read(self, size = -1): 145 def read(self, size=-1):
146 """ 146 """
147 Public method to read bytes from this file. 147 Public method to read bytes from this file.
148 148
149 @param size maximum number of bytes to be read (int) 149 @param size maximum number of bytes to be read (int)
150 @return the bytes read (any) 150 @return the bytes read (any)
154 buf = input() 154 buf = input()
155 if size >= 0: 155 if size >= 0:
156 buf = buf[:size] 156 buf = buf[:size]
157 return buf 157 return buf
158 158
159 def readline_p(self, size = -1): 159 def readline_p(self, size=-1):
160 """ 160 """
161 Public method to read a line from this file. 161 Public method to read a line from this file.
162 162
163 <b>Note</b>: This method will not block and may return 163 <b>Note</b>: This method will not block and may return
164 only a part of a line if that is all that is available. 164 only a part of a line if that is all that is available.
185 size = len(line) 185 size = len(line)
186 186
187 # Now we know how big the line is, read it for real. 187 # Now we know how big the line is, read it for real.
188 return self.sock.recv(size).decode('utf8') 188 return self.sock.recv(size).decode('utf8')
189 189
190 def readlines(self, sizehint = -1): 190 def readlines(self, sizehint=-1):
191 """ 191 """
192 Public method to read all lines from this file. 192 Public method to read all lines from this file.
193 193
194 @param sizehint hint of the numbers of bytes to be read (int) 194 @param sizehint hint of the numbers of bytes to be read (int)
195 @return list of lines read (list of strings) 195 @return list of lines read (list of strings)
214 line = self.readline_p(room) 214 line = self.readline_p(room)
215 linelen = len(line) 215 linelen = len(line)
216 216
217 return lines 217 return lines
218 218
219 def readline(self, sizehint = -1): 219 def readline(self, sizehint=-1):
220 """ 220 """
221 Public method to read one line from this file. 221 Public method to read one line from this file.
222 222
223 @param sizehint hint of the numbers of bytes to be read (int) 223 @param sizehint hint of the numbers of bytes to be read (int)
224 @return one line read (string) 224 @return one line read (string)
228 line = input() + '\n' 228 line = input() + '\n'
229 if sizehint >= 0: 229 if sizehint >= 0:
230 line = line[:sizehint] 230 line = line[:sizehint]
231 return line 231 return line
232 232
233 def seek(self, offset, whence = 0): 233 def seek(self, offset, whence=0):
234 """ 234 """
235 Public method to move the filepointer. 235 Public method to move the filepointer.
236 236
237 @param offset offset to move the filepointer to (integer) 237 @param offset offset to move the filepointer to (integer)
238 @param whence position the offset relates to 238 @param whence position the offset relates to
248 @exception IOError This method is not supported and always raises an 248 @exception IOError This method is not supported and always raises an
249 IOError. 249 IOError.
250 """ 250 """
251 raise IOError((29, '[Errno 29] Illegal seek')) 251 raise IOError((29, '[Errno 29] Illegal seek'))
252 252
253 def truncate(self, size = -1): 253 def truncate(self, size=-1):
254 """ 254 """
255 Public method to truncate the file. 255 Public method to truncate the file.
256 256
257 @param size size to truncaze to (integer) 257 @param size size to truncaze to (integer)
258 @exception IOError This method is not supported and always raises an 258 @exception IOError This method is not supported and always raises an

eric ide

mercurial