-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinternal.jl
More file actions
174 lines (145 loc) · 4.12 KB
/
internal.jl
File metadata and controls
174 lines (145 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
ContextManagers.maybeenter(::Any) = nothing
function _enter(source)
context = ContextManagers.maybeenter(source)
context === nothing && notasource(source)
return something(context)
end
function notasource(@nospecialize(source))::Union{}
error("Not a context source: ", source)
end
function ContextManagers.exit(x, @nospecialize(err))
ContextManagers.exit(x)
return nothing
end
ContextManagers.exit(ctx) = close(ctx)
Base.getindex(err::ContextManagers.NonException) = err.value
function _exit(context, @nospecialize(err))::Bool
if !(err isa Exception)
err = ContextManagers.NonException(err)
end
y = ContextManagers.exit(context, err)::Union{Handled,Nothing}
# TODO: better error message
return y isa Handled
end
macro with(doblock::Expr, bindings...)
unsupported() = error("unsupported syntax")
Meta.isexpr(doblock, :(->), 2) && doblock.args[1] == Expr(:tuple) || unsupported()
body = doblock.args[2]
ex = foldr(bindings; init = body) do b, ex
@gensym err ans handled context
if b isa Symbol
value = resource = b
elseif Meta.isexpr(b, (:kw,:(=)), 2)
value, resource = b.args
if value === :_
@gensym value
end
else
@gensym value
resource = b
end
quote
# Using `let` so that it works with `value === resource`
let $context = $_enter($resource),
$value = $ContextManagers.value($context),
$ans = nothing,
$handled = false
try
$ans = $ex
catch $err
$handled = $_exit($context, $err)
$handled || rethrow()
end
$handled || $ContextManagers.exit($context, nothing)
$ans
end
end
end
return esc(ex)
end
ContextManagers.with(f::F, resource1, resources...) where {F} =
_with(f, resource1, resources...)
_with(f) = f()
function _with(f::F, resource1, resources...) where {F}
ans = nothing
handled = false
context = _enter(resource1)
try
x = ContextManagers.value(context)
ans = _with(resources...) do args...
f(x, args...)
end
catch err
handled = _exit(context, err)
handled || rethrow()
end
handled || _exit(context, nothing)
return ans
end
ContextManagers.value(x) = x
ContextManagers.closing(x) = ContextManagers.onexit(close, x)
"""
ContextManagers.onexit(cleanup, x)
ContextManagers.onexit(cleanup)
Create a context manager that runs `cleanup(x)` upon exit.
# Example
```jldoctest
julia> using ContextManagers: @with, onexit
julia> @with(
onexit(111) do x
@show x
end,
) do
end;
x = 111
```
"""
ContextManagers.onexit
struct OnExit{C,T}
close::C
value::T
end
ContextManagers.onexit(close::F, value) where {F} = OnExit(close, value)
ContextManagers.maybeenter(c::OnExit) = c
ContextManagers.value(c::OnExit) = c.value
ContextManagers.exit(c::OnExit) = c.close(c.value)
ContextManagers.onexit(close::F) where {F} = OnExit(_ -> close(), nothing)
"""
ContextManagers.onfail(cleanup, x)
ContextManagers.onfail(cleanup)
Create a context manager that runs `cleanup(x)` upon unsuccessful exit.
# Example
```jldoctest
julia> using ContextManagers: @with, IgnoreError, onfail
julia> @with(
onfail(111) do x
@show x
end,
) do
end; # prints nothing
julia> @with(
IgnoreError(),
onfail(111) do x
@show x
end,
) do
error("error")
end;
x = 111
```
"""
ContextManagers.onfail
struct OnFail{C,T}
close::C
value::T
end
ContextManagers.onfail(close::F, value) where {F} = OnFail(close, value)
ContextManagers.maybeenter(c::OnFail) = c
ContextManagers.value(c::OnFail) = c.value
function ContextManagers.exit(c::OnFail, err)
if err !== nothing
c.close(c.value)
end
return nothing
end
ContextManagers.onfail(close::F) where {F} = OnFail(_ -> close(), nothing)