Skip to content

Commit d6fbe29

Browse files
committed
Ensure stdout filename object is not garbage-collected while in use in C
Store the output of `force_bytes(stdout_f)` in a live object to ensure it is not garbage-collected before it is used within samtools_dispatch(). This fixes a test failure on i386 (SamtoolsTest/PysamTest for calmd).
1 parent 1de219a commit d6fbe29

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

pysam/libcutils.pyx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,11 @@ def _pysam_dispatch(collection,
327327
# redirect stdout to file
328328
if save_stdout:
329329
stdout_f = save_stdout
330-
stdout_h = c_open(force_bytes(stdout_f),
331-
O_WRONLY|O_CREAT|O_TRUNC, 0666)
330+
stdout_f_bytes = force_bytes(stdout_f)
331+
stdout_h = c_open(stdout_f_bytes, O_WRONLY|O_CREAT|O_TRUNC, 0666)
332332
if stdout_h == -1:
333333
raise OSError_from_errno("Could not redirect standard output", stdout_f)
334334

335-
samtools_set_stdout_fn(force_bytes(stdout_f))
336-
bcftools_set_stdout_fn(force_bytes(stdout_f))
337-
338335
elif catch_stdout:
339336
stdout_h, stdout_f = tempfile.mkstemp()
340337
MAP_STDOUT_OPTIONS = {
@@ -360,13 +357,14 @@ def _pysam_dispatch(collection,
360357

361358
if stdout_option is not None and not is_usage:
362359
os.close(stdout_h)
363-
samtools_set_stdout_fn(force_bytes(stdout_f))
364-
bcftools_set_stdout_fn(force_bytes(stdout_f))
360+
stdout_f_bytes = force_bytes(stdout_f)
365361
args.extend(stdout_option.format(stdout_f).split(" "))
366362
stdout_h = c_open(b"/dev/null", O_WRONLY)
363+
else:
364+
stdout_f_bytes = None
365+
367366
else:
368-
samtools_set_stdout_fn("-")
369-
bcftools_set_stdout_fn("-")
367+
stdout_f_bytes = b"-"
370368
if catch_stdout is None: stdout_h = c_dup(STDOUT_FILENO)
371369
else: stdout_h = c_open(b"/dev/null", O_WRONLY)
372370

@@ -397,15 +395,19 @@ def _pysam_dispatch(collection,
397395

398396
# call samtools/bcftools
399397
if collection == b"samtools":
398+
if stdout_f_bytes is not None: samtools_set_stdout_fn(stdout_f_bytes)
400399
samtools_set_stdout(stdout_h)
401400
samtools_set_stderr(stderr_h)
402401
retval = samtools_dispatch(n + 2, cargs)
402+
samtools_set_stdout_fn(NULL)
403403
samtools_close_stdout()
404404
samtools_close_stderr()
405405
elif collection == b"bcftools":
406+
if stdout_f_bytes is not None: bcftools_set_stdout_fn(stdout_f_bytes)
406407
bcftools_set_stdout(stdout_h)
407408
bcftools_set_stderr(stderr_h)
408409
retval = bcftools_dispatch(n + 2, cargs)
410+
bcftools_set_stdout_fn(NULL)
409411
bcftools_close_stdout()
410412
bcftools_close_stderr()
411413
else:

0 commit comments

Comments
 (0)