DebugClients/Python3/AsyncFile.py

branch
debugger speed
changeset 5174
8c48f5e0cd92
parent 5161
f7b6ded9cc37
equal deleted inserted replaced
5170:fb9168c2e069 5174:8c48f5e0cd92
8 debugger. 8 debugger.
9 """ 9 """
10 10
11 import socket 11 import socket
12 12
13 from DebugProtocol import EOT 13 from DebugUtilities import prepareJsonCommand
14 14
15 15
16 def AsyncPendingWrite(file): 16 def AsyncPendingWrite(file):
17 """ 17 """
18 Module function to check for data to be written. 18 Module function to check for data to be written.
49 self.mode = mode 49 self.mode = mode
50 self.name = name 50 self.name = name
51 self.nWriteErrors = 0 51 self.nWriteErrors = 0
52 self.encoding = "utf-8" 52 self.encoding = "utf-8"
53 53
54 self.line_buffering = True
55 self.errors = None
56
57 self.wpending = '' 54 self.wpending = ''
58 55
59 def __checkMode(self, mode): 56 def __checkMode(self, mode):
60 """ 57 """
61 Private method to check the mode. 58 Private method to check the mode.
75 72
76 @param n the number of bytes to be written (int) 73 @param n the number of bytes to be written (int)
77 """ 74 """
78 if n: 75 if n:
79 try: 76 try:
80 buf = "{0!s}{1!s}".format(self.wpending[:n], EOT) 77 buf = self.wpending[:n]
81 try: 78 try:
82 buf = buf.encode('utf-8', 'backslashreplace') 79 buf = buf.encode('utf-8', 'backslashreplace')
83 except (UnicodeEncodeError, UnicodeDecodeError): 80 except (UnicodeEncodeError, UnicodeDecodeError):
84 pass 81 pass
85 self.sock.sendall(buf) 82 self.sock.sendall(buf)
153 self.__checkMode('r') 150 self.__checkMode('r')
154 151
155 if size < 0: 152 if size < 0:
156 size = 20000 153 size = 20000
157 154
158 return self.sock.recv(size).decode('utf8') 155 return self.sock.recv(size).decode('utf8', 'backslashreplace')
159 156
160 def read(self, size=-1): 157 def read(self, size=-1):
161 """ 158 """
162 Public method to read bytes from this file. 159 Public method to read bytes from this file.
163 160
198 size = eol + 1 195 size = eol + 1
199 else: 196 else:
200 size = len(line) 197 size = len(line)
201 198
202 # Now we know how big the line is, read it for real. 199 # Now we know how big the line is, read it for real.
203 return self.sock.recv(size).decode('utf8') 200 return self.sock.recv(size).decode('utf8', 'backslashreplace')
204 201
205 def readlines(self, sizehint=-1): 202 def readlines(self, sizehint=-1):
206 """ 203 """
207 Public method to read all lines from this file. 204 Public method to read all lines from this file.
208 205
275 272
276 def truncate(self, size=-1): 273 def truncate(self, size=-1):
277 """ 274 """
278 Public method to truncate the file. 275 Public method to truncate the file.
279 276
280 @param size size to truncaze to (integer) 277 @param size size to truncate to (integer)
281 @exception IOError This method is not supported and always raises an 278 @exception IOError This method is not supported and always raises an
282 IOError. 279 IOError.
283 """ 280 """
284 raise IOError((29, '[Errno 29] Illegal seek')) 281 raise IOError((29, '[Errno 29] Illegal seek'))
285 282
293 290
294 def write(self, s): 291 def write(self, s):
295 """ 292 """
296 Public method to write a string to the file. 293 Public method to write a string to the file.
297 294
298 @param s bytes to be written (string) 295 @param s text to be written (string)
296 """
297 self.__checkMode('w')
298
299 cmd = prepareJsonCommand("ClientOutput", {
300 "text": s,
301 })
302 self.write_p(cmd)
303
304 def write_p(self, s):
305 """
306 Public method to write a string to the file.
307
308 @param s text to be written (string)
299 @exception socket.error raised to indicate too many send attempts 309 @exception socket.error raised to indicate too many send attempts
300 """ 310 """
301 self.__checkMode('w') 311 self.__checkMode('w')
302 tries = 0 312 tries = 0
303 if not self.wpending: 313 if not self.wpending:
304 self.wpending = s 314 self.wpending = s
305 elif len(self.wpending) + len(s) > self.maxbuffersize: 315 elif len(self.wpending) + len(s) > self.maxbuffersize:
306 # flush wpending so that different string types are not 316 # flush wpending if it is too big
307 # concatenated
308 while self.wpending: 317 while self.wpending:
309 # if we have a persistent error in sending the data, an 318 # if we have a persistent error in sending the data, an
310 # exception will be raised in __nWrite 319 # exception will be raised in __nWrite
311 self.flush() 320 self.flush()
312 tries += 1 321 tries += 1
315 self.wpending = s 324 self.wpending = s
316 else: 325 else:
317 self.wpending += s 326 self.wpending += s
318 self.__nWrite(self.pendingWrite()) 327 self.__nWrite(self.pendingWrite())
319 328
320 def writelines(self, list): 329 def writelines(self, lines):
321 """ 330 """
322 Public method to write a list of strings to the file. 331 Public method to write a list of strings to the file.
323 332
324 @param list the list to be written (list of string) 333 @param lines list of texts to be written (list of string)
325 """ 334 """
326 for l in list: 335 self.write("".join(lines))
327 self.write(l)
328 336
329 # 337 #
330 # eflag: noqa = M702 338 # eflag: noqa = M702

eric ide

mercurial