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) |
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 |