Skip to content

Commit a8f0b3e

Browse files
authored
Use byte streams for all file handles (#125)
* use byte streams for all file handles * set log persistency as default behavior * update process log block skipping to work with binary stream
1 parent 3a18359 commit a8f0b3e

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

xprocess/xprocess.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,23 @@ def ensure(self, name, preparefunc, restart=False, persist_logs=True):
271271
self.log.debug("process %r started pid=%s", name, pid)
272272
stdout.close()
273273

274-
log_file_handle = open(info.logpath, errors="ignore")
274+
log_file_handle = open(info.logpath, "rb")
275275
# keep track of all file handles so we can
276276
# cleanup later during teardown phase
277277
xresource.fhandles.append(log_file_handle)
278278

279279
if persist_logs:
280280
# skip previous process logs
281-
process_log_block_handle = info.logpath.open()
282-
lines = process_log_block_handle.readlines()
281+
process_log_block_handle = open(info.logpath, "rb")
282+
lines = [
283+
str(line, "utf-8") for line in process_log_block_handle.readlines()
284+
]
283285
if lines:
284286
proc_block_counter = sum(
285287
1 for line in lines if XPROCESS_BLOCK_DELIMITER in line
286288
)
287289
for line in log_file_handle:
288-
if XPROCESS_BLOCK_DELIMITER in line:
290+
if XPROCESS_BLOCK_DELIMITER in str(line, "utf-8"):
289291
proc_block_counter -= 1
290292
if proc_block_counter <= 0:
291293
break
@@ -381,14 +383,12 @@ def pattern(self):
381383

382384
def startup_check(self):
383385
"""Used to assert process responsiveness after pattern match"""
384-
385386
return True
386387

387388
def wait_callback(self):
388389
"""Assert that process is ready to answer queries using provided
389390
callback funtion. Will raise TimeoutError if self.callback does not
390391
return True before self.timeout seconds"""
391-
392392
while True:
393393
sleep(0.1)
394394
if self.startup_check():
@@ -404,7 +404,6 @@ def wait_callback(self):
404404

405405
def wait(self, log_file):
406406
"""Wait until the pattern is mached and callback returns successful."""
407-
408407
self._max_time = datetime.now() + timedelta(seconds=self.timeout)
409408
lines = map(self.log_line, self.filter_lines(self.get_lines(log_file)))
410409
has_match = any(std.re.search(self.pattern, line) for line in lines)
@@ -413,23 +412,20 @@ def wait(self, log_file):
413412

414413
def filter_lines(self, lines):
415414
"""fetch first <max_read_lines>, ignoring blank lines."""
416-
417415
non_empty_lines = (x for x in lines if x.strip())
418416
return itertools.islice(non_empty_lines, self.max_read_lines)
419417

420418
def log_line(self, line):
421419
"""Write line to process log file."""
422-
423420
self.process.log.debug(line)
424421
return line
425422

426423
def get_lines(self, log_file):
427424
"""Read and yield one line at a time from log_file. Will raise
428425
TimeoutError if pattern is not matched before self.timeout
429426
seconds."""
430-
431427
while True:
432-
line = log_file.readline()
428+
line = str(log_file.readline(), "utf-8")
433429

434430
if not line:
435431
std.time.sleep(0.1)

0 commit comments

Comments
 (0)