Skip to content

Commit

Permalink
Use pipe when output file descriptor is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
Will-Tyler committed Nov 4, 2024
1 parent b73b02a commit 8ba5ac0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
22 changes: 21 additions & 1 deletion vcztools/_vcztoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,22 @@ static PyObject *
VcfEncoder_encode_all(VcfEncoder *self, PyObject *args)
{
bool allowed_threads = false;
int output_fd;
int is_pipe;
FILE* file = NULL;

if (VcfEncoder_check_state(self) != 0) {
goto out;
}
if (!PyArg_ParseTuple(args, "ip", &output_fd, &is_pipe)) {
goto out;
}

file = fdopen(output_fd, "w");

if (file == NULL) {
goto out;
}

Py_BEGIN_ALLOW_THREADS
allowed_threads = true;
Expand Down Expand Up @@ -512,14 +524,22 @@ VcfEncoder_encode_all(VcfEncoder *self, PyObject *args)
goto out;
}
} else {
puts(buf);
fputs(buf, file);
fputc('\n', file);
PyMem_RawFree(buf);
break;
} // if (line_length < 0)
} // while (true)
}

out:
if (file != NULL) {
fflush(file);

if (is_pipe) {
fclose(file);
}
}
if (allowed_threads) {
Py_END_ALLOW_THREADS
}
Expand Down
17 changes: 16 additions & 1 deletion vcztools/vcf_writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import concurrent.futures
import functools
import io
import os
import re
import sys
from datetime import datetime
Expand Down Expand Up @@ -416,7 +417,21 @@ def c_chunk_to_vcf(
if preceding_future:
concurrent.futures.wait((preceding_future,))

encoder.encode_all()
output = output or sys.stdout
pipe = None
try:
output_fd = output.fileno()
except OSError:
pipe = os.pipe()
output_fd = pipe[1]

output.flush()
encoder.encode_all(output_fd, bool(pipe))

if pipe:
with io.FileIO(pipe[0], closefd=True) as pipe_reader:
output.write(pipe_reader.read().decode("ascii"))
output.flush()


def _generate_header(ds, original_header, sample_ids, *, no_version: bool = False):
Expand Down

0 comments on commit 8ba5ac0

Please sign in to comment.