Skip to content

Commit 3d7fdda

Browse files
flush stdio from libc
1 parent df63327 commit 3d7fdda

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

Project.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ CBinding = "d43a6710-96b8-4a2d-833c-c424785e5374"
88
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
99
LibTeXPrintf_jll = "31c4d84e-4279-5ed4-9345-4512eef47f95"
1010
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
11-
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
1211

1312
[compat]
1413
CBinding = "1"
15-
Suppressor = "0.2"
1614
LaTeXStrings = "1"

src/LibTeXPrintf.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@ module LibTeXPrintf
33
using Base: ImmutableDict
44
using Printf: Format, argmismatch
55
using LaTeXStrings
6-
using Suppressor
76

87
include("wrapper.jl")
98
using .libtexprintf
109

10+
include("capture.jl")
11+
1112

1213
export stexprintf, texprintf, texsymbols, texfonts, texsetfont, texgetfont
1314

1415
function __init__()
1516
# this is a workaround so `stdout` can work correctly and be captured by Suppressor
16-
libtexprintf.texprintf("")
17+
# libtexprintf.texprintf("")
1718
TEXSYMBOLS[] = ImmutableDict([
1819
if length(line) == 4
1920
String(line[2] * line[3]) => String(line[4])
2021
else
2122
String(line[2]) => String(line[3])
22-
end for line in split.(split(rstrip(@capture_out libtexprintf.texlistsymbols(), '\n'), '\n'))
23+
end for line in split.(split(strip(capture_out() do
24+
libtexprintf.texlistsymbols()
25+
end, '\n'), '\n'))
2326
]...)
2427
end
2528

@@ -172,7 +175,7 @@ julia> LibTeXPrintf.texerrors() # queue cleaned
172175
```
173176
"""
174177
function texerrors()
175-
error = @capture_err begin
178+
error = capture_err() do
176179
libtexprintf.texerrors()
177180
end
178181
if iszero(length(error))
@@ -318,7 +321,9 @@ function texprintf(io::IO, format::String, args...)
318321
else
319322
length(args) == 0 || argmismatch(0, length(args))
320323
end
321-
out = @capture_out libtexprintf.texprintf(format, args...)
324+
out = capture_out() do
325+
libtexprintf.texprintf(format, args...)
326+
end
322327
if !iszero(libtexprintf.TEXPRINTF_ERR[])
323328
error = texerrors()
324329
throw(ArgumentError(error))

src/capture.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# taken from Suppressor, but flush stdio from libc
2+
3+
function capture_out(cb)
4+
if ccall(:jl_generating_output, Cint, ()) == 0
5+
original_stdout = stdout
6+
out_rd, out_wr = redirect_stdout()
7+
out_reader = @async read(out_rd, String)
8+
end
9+
try
10+
cb()
11+
Base.Libc.flush_cstdio()
12+
finally
13+
if ccall(:jl_generating_output, Cint, ()) == 0
14+
redirect_stdout(original_stdout)
15+
close(out_wr)
16+
end
17+
end
18+
if ccall(:jl_generating_output, Cint, ()) == 0
19+
fetch(out_reader)
20+
else
21+
""
22+
end
23+
end
24+
25+
function capture_err(cb)
26+
if ccall(:jl_generating_output, Cint, ()) == 0
27+
original_stderr = stderr
28+
err_rd, err_wr = redirect_stderr()
29+
err_reader = @async read(err_rd, String)
30+
end
31+
try
32+
cb()
33+
Base.Libc.flush_cstdio()
34+
finally
35+
if ccall(:jl_generating_output, Cint, ()) == 0
36+
redirect_stderr(original_stderr)
37+
close(err_wr)
38+
end
39+
end
40+
if ccall(:jl_generating_output, Cint, ()) == 0
41+
fetch(err_reader)
42+
else
43+
""
44+
end
45+
end

src/wrapper.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let
1414
end
1515
end
1616

17-
const c"size_t" = Csize_t
17+
const c"size_t" = Csize_t
1818
const c"FILE" = Cvoid
1919
const c"va_list" = Cvoid
2020

0 commit comments

Comments
 (0)