From 184f1d9f37b289cf7cc510ff8ba85648c1ba1fca Mon Sep 17 00:00:00 2001 From: John Marshall Date: Wed, 25 Jul 2018 10:41:46 +0100 Subject: [PATCH 1/2] Add @pysam@_puts() wrapper function Rather than trying to rewrite a function call as two function calls when we don't really know what the syntax around the call site looks like, rewrite it as a call to our own function. Fixes #646. --- devtools/import.py | 4 +--- import/pysam.c | 6 ++++++ import/pysam.h | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/devtools/import.py b/devtools/import.py index 89aa9f13..44e606a2 100644 --- a/devtools/import.py +++ b/devtools/import.py @@ -102,9 +102,7 @@ def _update_pysam_files(cf, destdir): lines = re.sub("stderr", "{}_stderr".format(basename), lines) lines = re.sub("stdout", "{}_stdout".format(basename), lines) lines = re.sub(" printf\(", " fprintf({}_stdout, ".format(basename), lines) - lines = re.sub("([^kf])puts\(([^)]+)\)", - r"\1fputs(\2, {}_stdout) & fputc('\\n', {}_stdout)".format(basename, basename), - lines) + lines = re.sub("([^kf])puts\(", r"\1{}_puts(".format(basename), lines) lines = re.sub("putchar\(([^)]+)\)", r"fputc(\1, {}_stdout)".format(basename), lines) diff --git a/import/pysam.c b/import/pysam.c index 16420137..d08cf50e 100644 --- a/import/pysam.c +++ b/import/pysam.c @@ -54,6 +54,12 @@ void @pysam@_unset_stdout(void) @pysam@_stdout_fileno = STDOUT_FILENO; } +int @pysam@_puts(const char *s) +{ + if (fputs(s, @pysam@_stdout) == EOF) return EOF; + return putc('\n', @pysam@_stdout); +} + void @pysam@_set_optind(int val) { // setting this in cython via diff --git a/import/pysam.h b/import/pysam.h index 4a6ec296..c3cc6231 100644 --- a/import/pysam.h +++ b/import/pysam.h @@ -38,6 +38,8 @@ void @pysam@_unset_stderr(void); */ void @pysam@_unset_stdout(void); +int @pysam@_puts(const char *s); + int @pysam@_dispatch(int argc, char *argv[]); void @pysam@_set_optind(int); From f548884ef05129d24240e51a004d2907f1c7dabe Mon Sep 17 00:00:00 2001 From: Kyle Beauchamp Date: Wed, 25 Jul 2018 08:02:59 -0700 Subject: [PATCH 2/2] Apply patches --- bcftools/bcftools.pysam.c | 6 ++++++ bcftools/bcftools.pysam.h | 2 ++ bcftools/tabix.c.pysam.c | 4 ++-- samtools/bam.c.pysam.c | 2 +- samtools/bedcov.c.pysam.c | 2 +- samtools/misc/ace2sam.c.pysam.c | 6 +++--- samtools/samtools.pysam.c | 6 ++++++ samtools/samtools.pysam.h | 2 ++ 8 files changed, 23 insertions(+), 7 deletions(-) diff --git a/bcftools/bcftools.pysam.c b/bcftools/bcftools.pysam.c index 63dfed5b..e3e251ef 100644 --- a/bcftools/bcftools.pysam.c +++ b/bcftools/bcftools.pysam.c @@ -54,6 +54,12 @@ void bcftools_unset_stdout(void) bcftools_stdout_fileno = STDOUT_FILENO; } +int bcftools_puts(const char *s) +{ + if (fputs(s, bcftools_stdout) == EOF) return EOF; + return putc('\n', bcftools_stdout); +} + void bcftools_set_optind(int val) { // setting this in cython via diff --git a/bcftools/bcftools.pysam.h b/bcftools/bcftools.pysam.h index 28135a31..92ab370a 100644 --- a/bcftools/bcftools.pysam.h +++ b/bcftools/bcftools.pysam.h @@ -38,6 +38,8 @@ void bcftools_unset_stderr(void); */ void bcftools_unset_stdout(void); +int bcftools_puts(const char *s); + int bcftools_dispatch(int argc, char *argv[]); void bcftools_set_optind(int); diff --git a/bcftools/tabix.c.pysam.c b/bcftools/tabix.c.pysam.c index ba9e1b31..2216d6e6 100644 --- a/bcftools/tabix.c.pysam.c +++ b/bcftools/tabix.c.pysam.c @@ -78,7 +78,7 @@ int main_tabix(int argc, char *argv[]) BGZF *fp; s.l = s.m = 0; s.s = 0; fp = bgzf_open(argv[optind], "r"); - while (bgzf_getline(fp, '\n', &s) >= 0) fputs(s.s, bcftools_stdout) & fputc('\n', bcftools_stdout); + while (bgzf_getline(fp, '\n', &s) >= 0) bcftools_puts(s.s); bgzf_close(fp); free(s.s); } else if (optind + 2 > argc) { // create index @@ -122,7 +122,7 @@ int main_tabix(int argc, char *argv[]) for (i = optind + 1; i < argc; ++i) { hts_itr_t *itr; if ((itr = tbx_itr_querys(tbx, argv[i])) == 0) continue; - while (tbx_bgzf_itr_next(fp, tbx, itr, &s) >= 0) fputs(s.s, bcftools_stdout) & fputc('\n', bcftools_stdout); + while (tbx_bgzf_itr_next(fp, tbx, itr, &s) >= 0) bcftools_puts(s.s); tbx_itr_destroy(itr); } free(s.s); diff --git a/samtools/bam.c.pysam.c b/samtools/bam.c.pysam.c index 982bf410..a92d1153 100644 --- a/samtools/bam.c.pysam.c +++ b/samtools/bam.c.pysam.c @@ -51,7 +51,7 @@ int bam_view1(const bam_header_t *header, const bam1_t *b) char *s = bam_format1(header, b); int ret = -1; if (!s) return -1; - if (fputs(s, samtools_stdout) & fputc('\n', samtools_stdout) != EOF) ret = 0; + if (samtools_puts(s) != EOF) ret = 0; free(s); return ret; } diff --git a/samtools/bedcov.c.pysam.c b/samtools/bedcov.c.pysam.c index 8aa500b4..165fed72 100644 --- a/samtools/bedcov.c.pysam.c +++ b/samtools/bedcov.c.pysam.c @@ -173,7 +173,7 @@ int main_bedcov(int argc, char *argv[]) kputc('\t', &str); kputl(cnt[i], &str); } - fputs(str.s, samtools_stdout) & fputc('\n', samtools_stdout); + samtools_puts(str.s); bam_mplp_destroy(mplp); continue; diff --git a/samtools/misc/ace2sam.c.pysam.c b/samtools/misc/ace2sam.c.pysam.c index 11359674..ee8ef5e4 100644 --- a/samtools/misc/ace2sam.c.pysam.c +++ b/samtools/misc/ace2sam.c.pysam.c @@ -164,14 +164,14 @@ int samtools_ace2sam_main(int argc, char *argv[]) if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret); ks_getuntil(ks, '\n', &s, &dret); // skip the empty line if (write_cns) { - if (t[4].l) fputs(t[4].s, samtools_stdout) & fputc('\n', samtools_stdout); + if (t[4].l) samtools_puts(t[4].s); t[4].l = 0; } } else if (strcmp(s.s, "AF") == 0) { // padded read position int reversed, neg, pos; if (t[0].l == 0) fatal("come to 'AF' before reading 'CO'"); if (write_cns) { - if (t[4].l) fputs(t[4].s, samtools_stdout) & fputc('\n', samtools_stdout); + if (t[4].l) samtools_puts(t[4].s); t[4].l = 0; } ks_getuntil(ks, 0, &s, &dret); // read name @@ -244,7 +244,7 @@ int samtools_ace2sam_main(int argc, char *argv[]) kputs("\t*\t0\t0\t", &t[4]); // empty MRNM, MPOS and TLEN kputsn(t[3].s, t[3].l, &t[4]); // unpadded SEQ kputs("\t*", &t[4]); // QUAL - fputs(t[4].s, samtools_stdout) & fputc('\n', samtools_stdout); // print to samtools_stdout + samtools_puts(t[4].s); // print to samtools_stdout ++af_i; } else if (dret != '\n') ks_getuntil(ks, '\n', &s, &dret); } diff --git a/samtools/samtools.pysam.c b/samtools/samtools.pysam.c index c276a8a0..edbd9a2d 100644 --- a/samtools/samtools.pysam.c +++ b/samtools/samtools.pysam.c @@ -54,6 +54,12 @@ void samtools_unset_stdout(void) samtools_stdout_fileno = STDOUT_FILENO; } +int samtools_puts(const char *s) +{ + if (fputs(s, samtools_stdout) == EOF) return EOF; + return putc('\n', samtools_stdout); +} + void samtools_set_optind(int val) { // setting this in cython via diff --git a/samtools/samtools.pysam.h b/samtools/samtools.pysam.h index e2bfd85e..d97ee250 100644 --- a/samtools/samtools.pysam.h +++ b/samtools/samtools.pysam.h @@ -38,6 +38,8 @@ void samtools_unset_stderr(void); */ void samtools_unset_stdout(void); +int samtools_puts(const char *s); + int samtools_dispatch(int argc, char *argv[]); void samtools_set_optind(int);