@@ -194,33 +194,53 @@ unwrap_weak_checked(t) = t
194
194
Base. show (io:: IO , t:: WeakThunk ) = (print (io, " ~" ); Base. show (io, t. x. value))
195
195
Base. convert (:: Type{WeakThunk} , t:: Thunk ) = WeakThunk (t)
196
196
197
+ " A summary of the data contained in a Thunk, which can be safely serialized."
198
+ struct ThunkSummary
199
+ id:: Int
200
+ f
201
+ inputs:: Vector{Pair{Union{Symbol,Nothing},Any}}
202
+ end
203
+ inputs (t:: ThunkSummary ) = t. inputs
204
+ Base. show (io:: IO , t:: ThunkSummary ) = show_thunk (io, t)
205
+ function Base. convert (:: Type{ThunkSummary} , t:: Thunk )
206
+ return ThunkSummary (t. id,
207
+ t. f,
208
+ map (pos_inp-> istask (pos_inp[2 ]) ? pos_inp[1 ]=> convert (ThunkSummary, pos_inp[2 ]) : pos_inp,
209
+ t. inputs))
210
+ end
211
+ function Base. convert (:: Type{ThunkSummary} , t:: WeakThunk )
212
+ t = unwrap_weak (t)
213
+ if t != = nothing
214
+ t = convert (ThunkSummary, t)
215
+ end
216
+ return t
217
+ end
218
+
197
219
struct ThunkFailedException{E<: Exception } <: Exception
198
- thunk:: WeakThunk
199
- origin:: WeakThunk
220
+ thunk:: ThunkSummary
221
+ origin:: ThunkSummary
200
222
ex:: E
201
223
end
202
224
ThunkFailedException (thunk, origin, ex:: E ) where E =
203
- ThunkFailedException {E} (convert (WeakThunk, thunk), convert (WeakThunk, origin), ex)
225
+ ThunkFailedException {E} (convert (ThunkSummary, thunk),
226
+ convert (ThunkSummary, origin),
227
+ ex)
204
228
function Base. showerror (io:: IO , ex:: ThunkFailedException )
205
- t = unwrap_weak ( ex. thunk)
229
+ t = ex. thunk
206
230
207
231
# Find root-cause thunk
208
232
last_tfex = ex
209
- failed_tasks = Union{Thunk ,Nothing}[]
210
- while last_tfex. ex isa ThunkFailedException && unwrap_weak (last_tfex . ex . origin) != = nothing
211
- push! (failed_tasks, unwrap_weak ( last_tfex. thunk) )
233
+ failed_tasks = Union{ThunkSummary ,Nothing}[]
234
+ while last_tfex. ex isa ThunkFailedException
235
+ push! (failed_tasks, last_tfex. thunk)
212
236
last_tfex = last_tfex. ex
213
237
end
214
- o = unwrap_weak ( last_tfex. origin)
238
+ o = last_tfex. origin
215
239
root_ex = last_tfex. ex
216
240
217
241
function thunk_string (t)
218
- if t === nothing
219
- return " Thunk(?)"
220
- end
221
242
Tinputs = Any[]
222
243
for (_, input) in t. inputs
223
- input = unwrap_weak (input)
224
244
if istask (input)
225
245
push! (Tinputs, " Thunk(id=$(input. id) )" )
226
246
else
@@ -236,13 +256,11 @@ function Base.showerror(io::IO, ex::ThunkFailedException)
236
256
end
237
257
t_str = thunk_string (t)
238
258
o_str = thunk_string (o)
239
- t_id = t != = nothing ? t. id : ' ?'
240
- o_id = o != = nothing ? o. id : ' ?'
241
259
println (io, " ThunkFailedException:" )
242
260
println (io, " Root Exception Type: $(typeof (root_ex)) " )
243
261
println (io, " Root Exception:" )
244
262
Base. showerror (io, root_ex); println (io)
245
- if t != = o
263
+ if t. id != = o. id
246
264
println (io, " Root Thunk: $o_str " )
247
265
if length (failed_tasks) <= 4
248
266
for i in failed_tasks
@@ -419,36 +437,36 @@ cache_result!(t::Thunk) = (t.cache=true; t)
419
437
Base. hash (x:: Thunk , h:: UInt ) = hash (x. id, hash (h, 0x7ad3bac49089a05f % UInt))
420
438
Base. isequal (x:: Thunk , y:: Thunk ) = x. id== y. id
421
439
422
- function Base . show (io:: IO , z :: Thunk )
440
+ function show_thunk (io:: IO , t )
423
441
lvl = get (io, :lazy_level , 2 )
424
- f = if z . f isa Chunk
425
- Tf = z . f. chunktype
442
+ f = if t . f isa Chunk
443
+ Tf = t . f. chunktype
426
444
if isdefined (Tf, :instance )
427
445
Tf. instance
428
446
else
429
447
" instance of $Tf "
430
448
end
431
449
else
432
- z . f
450
+ t . f
433
451
end
434
- print (io, " Thunk[$(z . id) ]($f , " )
452
+ print (io, " Thunk[$(t . id) ]($f , " )
435
453
if lvl > 0
436
- inputs = Any[]
437
- for (pos, input) in z . inputs
454
+ t_inputs = Any[]
455
+ for (pos, input) in inputs (t)
438
456
if pos === nothing
439
- push! (inputs , input)
457
+ push! (t_inputs , input)
440
458
else
441
- push! (inputs , pos => input)
459
+ push! (t_inputs , pos => input)
442
460
end
443
461
end
444
- show (IOContext (io, :lazy_level => lvl- 1 ), inputs )
462
+ show (IOContext (io, :lazy_level => lvl- 1 ), t_inputs )
445
463
else
446
464
print (io, " ..." )
447
465
end
448
466
print (io, " )" )
449
467
end
450
-
451
- Base. summary (z :: Thunk ) = repr (z )
468
+ Base . show (io :: IO , t :: Thunk ) = show_thunk (io, t)
469
+ Base. summary (t :: Thunk ) = repr (t )
452
470
453
471
inputs (x:: Thunk ) = x. inputs
454
472
inputs (x) = ()
0 commit comments