Skip to content

Commit 4b4b885

Browse files
authored
Merge pull request #14 from c42f/cjf/fix-redirect-stdio
Fix stdio redirections for the Julia 1.7 API breakage
2 parents 4a9063f + b25dc00 commit 4b4b885

File tree

5 files changed

+37
-18
lines changed

5 files changed

+37
-18
lines changed

.github/workflows/CI.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
name: CI
22
on:
3-
- push
4-
- pull_request
3+
push:
4+
branches:
5+
- main
6+
tags: '*'
7+
pull_request:
58
jobs:
69
test:
710
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
@@ -10,7 +13,7 @@ jobs:
1013
fail-fast: false
1114
matrix:
1215
version:
13-
- '1.5'
16+
- '1.6'
1417
- '1'
1518
- 'nightly'
1619
os:

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ResourceContexts"
22
uuid = "8d208092-d35c-4dd3-a0d7-8325f9cce6b4"
33
authors = ["Chris Foster <chris42f@gmail.com> and contributors"]
4-
version = "0.1.0"
4+
version = "0.1.1"
55

66
[deps]
77
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
@@ -11,6 +11,7 @@ julia = "1.5"
1111

1212
[extras]
1313
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
14+
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
1415

1516
[targets]
16-
test = ["Test"]
17+
test = ["Test", "Compat"]

src/base_interop.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ end
4545

4646
# Standard streams
4747

48+
# Incompatibility due to
49+
# https://github.com/JuliaLang/julia/pull/39132
50+
@static if VERSION < v"1.7"
51+
4852
@! function Base.redirect_stdout(stream)
4953
prev_stream = stdout
5054
x = redirect_stdout(stream)
@@ -66,3 +70,21 @@ end
6670
x
6771
end
6872

73+
else
74+
75+
@! function (f::Base.RedirectStdStream)(stream)
76+
# See https://github.com/JuliaLang/julia/blob/294b0dfcd308b3c3f829b2040ca1e3275595e058/base/stream.jl#L1417
77+
stdold = f.unix_fd == 0 ? stdin :
78+
f.unix_fd == 1 ? stdout :
79+
f.unix_fd == 2 ? stderr :
80+
throw(ArgumentError("Not implemented to get old handle of fd except for stdio"))
81+
x = f(stream)
82+
@defer f(stdold)
83+
x
84+
end
85+
86+
@! function Base.redirect_stdio(; kws...)
87+
@! enter_do(redirect_stdio; kws...)
88+
end
89+
90+
end

test/base_interop.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@
9797
seek(io, 0)
9898
stderr_result = readline(io)
9999
end
100-
if VERSION < v"1.7-DEV"
101-
@test stderr_result == "hi"
102-
else
103-
@test_broken stderr_result == "hi"
104-
end
100+
@test stderr_result == "hi"
105101

106102
stdin_result = nothing
107103
@context begin
@@ -112,11 +108,7 @@
112108
@! redirect_stdin(io)
113109
stdin_result = readline()
114110
end
115-
if VERSION < v"1.7-DEV"
116-
@test stdin_result == "hi"
117-
else
118-
@test_broken stdin_result == "hi"
119-
end
111+
@test stdin_result == "hi"
120112

121113
@test orig_stdin == stdin
122114
@test orig_stdout == stdout

test/runtests.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ResourceContexts
22
using Test
33
using Logging
4+
using Compat
45

56
# Use of @! to pass context to resource creation function
67
@! function foo(x, label)
@@ -35,7 +36,7 @@ end
3536
@defer error("B")
3637
end
3738
catch exc
38-
stack = Base.catch_stack()
39+
stack = current_exceptions()
3940
@test stack[1][1] == ErrorException("B")
4041
@test stack[2][1] == ErrorException("A")
4142
end
@@ -92,7 +93,7 @@ end
9293
end
9394
catch e
9495
@test e isa TaskFailedException
95-
first(Base.catch_stack(e.task))[1]
96+
first(current_exceptions(e.task))[1]
9697
end == ErrorException("Oops1")
9798

9899
@test try
@@ -101,7 +102,7 @@ end
101102
end
102103
catch e
103104
@test e isa TaskFailedException
104-
first(Base.catch_stack(e.task))[1]
105+
first(current_exceptions(e.task))[1]
105106
end == ErrorException("Oops2")
106107
end
107108

0 commit comments

Comments
 (0)