eric6/DebugClients/Python/AsyncFile.py

branch
maintenance
changeset 8043
0acf98cd089a
parent 7924
8a96736d465e
parent 8014
efa0c52fee56
child 8273
698ae46f40a4
equal deleted inserted replaced
7991:866adc8c315b 8043:0acf98cd089a
7 Module implementing an asynchronous file like socket interface for the 7 Module implementing an asynchronous file like socket interface for the
8 debugger. 8 debugger.
9 """ 9 """
10 10
11 import socket 11 import socket
12 import threading
12 13
13 from DebugUtilities import prepareJsonCommand 14 from DebugUtilities import prepareJsonCommand
14 15
15 16
16 def AsyncPendingWrite(file): 17 def AsyncPendingWrite(file):
56 self.encoding = "utf-8" 57 self.encoding = "utf-8"
57 self.errors = None 58 self.errors = None
58 self.newlines = None 59 self.newlines = None
59 self.line_buffering = False 60 self.line_buffering = False
60 61
62 self.writeLock = threading.RLock()
61 self.wpending = [] 63 self.wpending = []
62 64
63 def __checkMode(self, mode): 65 def __checkMode(self, mode):
64 """ 66 """
65 Private method to check the mode. 67 Private method to check the mode.
97 99
98 def flush(self): 100 def flush(self):
99 """ 101 """
100 Public method to write all pending entries. 102 Public method to write all pending entries.
101 """ 103 """
104 self.writeLock.acquire()
102 while self.wpending: 105 while self.wpending:
103 try: 106 try:
104 buf = self.wpending.pop(0) 107 buf = self.wpending.pop(0)
105 except IndexError: 108 except IndexError:
106 break 109 break
114 self.nWriteErrors = 0 117 self.nWriteErrors = 0
115 except OSError: 118 except OSError:
116 self.nWriteErrors += 1 119 self.nWriteErrors += 1
117 if self.nWriteErrors > self.maxtries: 120 if self.nWriteErrors > self.maxtries:
118 self.wpending = [] # delete all output 121 self.wpending = [] # delete all output
122 self.writeLock.release()
119 123
120 def isatty(self): 124 def isatty(self):
121 """ 125 """
122 Public method to indicate whether a tty interface is supported. 126 Public method to indicate whether a tty interface is supported.
123 127
189 # The command string is prefixed by a 9 character long length field. 193 # The command string is prefixed by a 9 character long length field.
190 length = self.sock.recv(9) 194 length = self.sock.recv(9)
191 length = int(length) 195 length = int(length)
192 data = b'' 196 data = b''
193 while len(data) < length: 197 while len(data) < length:
194 newData = self.sock.recv(length - len(data)) 198 newByte = self.sock.recv(1)
195 data += newData 199 data += newByte
200 if newByte == b'\n':
201 break
196 202
197 # step 2: convert the data 203 # step 2: convert the data
198 return data.decode('utf8', 'backslashreplace') 204 return data.decode('utf8', 'backslashreplace')
199 205
200 def readline_p(self, size=-1): 206 def readline_p(self, size=-1):
335 @param s text to be written 341 @param s text to be written
336 @type str, bytes or bytearray 342 @type str, bytes or bytearray
337 """ 343 """
338 self.__checkMode('w') 344 self.__checkMode('w')
339 345
346 self.writeLock.acquire()
340 if isinstance(s, (bytes, bytearray)): 347 if isinstance(s, (bytes, bytearray)):
341 # convert to string to send it 348 # convert to string to send it
342 s = repr(s) 349 s = repr(s)
343 350
344 cmd = prepareJsonCommand("ClientOutput", { 351 cmd = prepareJsonCommand("ClientOutput", {
345 "text": s, 352 "text": s,
353 "debuggerId": "",
346 }) 354 })
347 self.wpending.append(cmd) 355 self.wpending.append(cmd)
348 self.flush() 356 self.flush()
357 self.writeLock.release()
349 358
350 def write_p(self, s): 359 def write_p(self, s):
351 """ 360 """
352 Public method to write a json-rpc 2.0 coded string to the file. 361 Public method to write a json-rpc 2.0 coded string to the file.
353 362

eric ide

mercurial