-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy patherror.g
More file actions
447 lines (402 loc) · 13.5 KB
/
Copy patherror.g
File metadata and controls
447 lines (402 loc) · 13.5 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#############################################################################
##
## This file is part of GAP, a system for computational discrete algebra.
##
## Copyright of GAP belongs to its developers, whose names are too numerous
## to list here. Please refer to the COPYRIGHT file for details.
##
## SPDX-License-Identifier: GPL-2.0-or-later
##
## Error handling, break loop, etc. Now in GAP
##
CallAndInstallPostRestore( function()
ASS_GVAR( "ERROR_COUNT", 0 );
ASS_GVAR( "ErrorLevel", 0 );
if IsHPCGAP then
MakeThreadLocal("ErrorLevel");
fi;
ASS_GVAR( "QUITTING", false );
end);
#############################################################################
##
## Default stream for error messages.
ERROR_OUTPUT := MakeImmutable("*errout*");
#############################################################################
##
#F OnQuit( )
##
Unbind(OnQuit);
BIND_GLOBAL( "OnQuit", function()
if not IsEmpty(OptionsStack) then
if IsBound(ResetMethodReordering) and IsFunction(ResetMethodReordering)
and ValueOption( "ReadingPackageFiles" ) = true then
ResetMethodReordering();
fi;
repeat
PopOptions();
until IsEmpty(OptionsStack);
Info(InfoWarning,2,"Options stack has been reset");
fi;
if REREADING = true then
MakeReadWriteGlobal("REREADING");
REREADING := false;
MakeReadOnlyGlobal("REREADING");
fi;
end);
BIND_GLOBAL("AncestorLVars", function(lv, depth)
local i;
for i in [1..depth] do
lv := ParentLVars(lv);
od;
return lv;
end);
ErrorLVars := fail;
ErrorTracebackLVars := fail;
BIND_GLOBAL("GET_VISIBLE_ERROR_LVARS", function()
local active, context, bottom;
if ErrorTracebackLVars = fail then
return fail;
fi;
active := CurrentEnv();
if active = fail then
return ErrorTracebackLVars;
fi;
bottom := GetBottomLVars();
context := ErrorTracebackLVars;
while context <> bottom do
if context = active then
return active;
fi;
context := ParentLVars(context);
od;
return ErrorTracebackLVars;
end);
BIND_GLOBAL("ERROR_MESSAGE_ENDS_WITH_NEWLINE", function(message)
local last;
if Length(message) = 0 then
return false;
fi;
last := Last(message);
return IsString(last) and Length(last) > 0 and Last(last) = '\n';
end);
# In this method the line hint changes in every PrintTo are balanced, because at the moment
# PrintTo(ERROR_OUTPUT,...) resets the indentation level every time it is called.
# If/when this is fixed, the indentation in this function could be simplified
BIND_GLOBAL("PRETTY_PRINT_VARS", function(context)
local vars, i, argcount, val;
vars := ContentsLVars(context);
if not IsRecord(vars) then
return;
fi;
argcount := ABS_RAT(NumberArgumentsFunction(vars.func));
PrintTo(ERROR_OUTPUT, "\>\>\n\<\<arguments:");
if argcount = 0 then
PrintTo(ERROR_OUTPUT, " <none>");
else
for i in [1..argcount] do
if IsBound(vars.values[i]) then
val := vars.values[i];
else
val := "<unassigned>";
fi;
PrintTo(ERROR_OUTPUT, "\>\>\>\>\n", vars.names[i], " :=\>\> ", val, "\<\<\<\<\<\<");
od;
fi;
PrintTo(ERROR_OUTPUT, "\>\>\n\<\<local variables:");
if argcount = Length(vars.names) then
PrintTo(ERROR_OUTPUT, " <none>");
else
for i in [argcount+1..Length(vars.names)] do
if IsBound(vars.values[i]) then
val := vars.values[i];
else
val := "<unassigned>";
fi;
PrintTo(ERROR_OUTPUT, "\>\>\>\>\n", vars.names[i], " :=\>\> ", val, "\<\<\<\<\<\<");
od;
fi;
PrintTo(ERROR_OUTPUT,"\n");
end);
BIND_GLOBAL("WHERE", function(depth, context, activecontext, showlocals)
local bottom, lastcontext, f, level, totaldepth, countcontext;
if depth <= 0 then
return;
fi;
bottom := GetBottomLVars();
if context = bottom then
PrintTo(ERROR_OUTPUT, "called from read-eval loop ");
return;
fi;
lastcontext := context;
totaldepth := 0;
countcontext := context;
while totaldepth < depth and countcontext <> bottom do
totaldepth := totaldepth + 1;
countcontext := ParentLVars(countcontext);
od;
level := 1;
while depth > 0 and context <> bottom do
PRINT_CURRENT_STATEMENT(
ERROR_OUTPUT,
context,
activecontext,
level,
totaldepth);
if showlocals then
PRETTY_PRINT_VARS(context);
else
PrintTo(ERROR_OUTPUT, "\n");
fi;
lastcontext := context;
context := ParentLVars(context);
depth := depth-1;
level := level + 1;
od;
if depth = 0 then
PrintTo(ERROR_OUTPUT, "... ");
else
f := ContentsLVars(lastcontext).func;
PrintTo(ERROR_OUTPUT, "<function \"",NAME_FUNC(f)
,"\">( <arguments> )\n called from read-eval loop ");
fi;
end);
BIND_GLOBAL("WHERE_INTERNAL", function(depth, showlocals)
local activecontext;
if not IsInt(depth) or depth < 0 then
depth := 5;
fi;
if ErrorLVars = fail or ErrorLVars = GetBottomLVars() then
PrintTo(ERROR_OUTPUT, "not in any function ");
else
activecontext := GET_VISIBLE_ERROR_LVARS();
WHERE(depth, ErrorTracebackLVars, activecontext, showlocals);
fi;
PrintTo(ERROR_OUTPUT, "at ", INPUT_FILENAME(), ":", INPUT_LINENUMBER(), "\n");
end);
BIND_GLOBAL("WhereWithVars", function(arg)
local depth;
if LEN_LIST(arg) = 0 then
depth := UserPreference("WhereDepth");
else
depth := arg[1];
fi;
WHERE_INTERNAL(depth, true);
end);
BIND_GLOBAL("Where", function(arg)
local depth;
if LEN_LIST(arg) = 0 then
depth := UserPreference("WhereDepth");
else
depth := arg[1];
fi;
WHERE_INTERNAL(depth, false);
end);
OnBreak := Where;
BIND_GLOBAL("ErrorCount", function()
return ERROR_COUNT;
end);
#
#
#
Unbind(ErrorInner);
BIND_GLOBAL("ErrorInner", function(options, earlyMessage)
local context, tracebackContext, mayReturnVoid,
lateMessage, x, prompt, res, errorLVars, errorTracebackLVars,
kernelErrorLVars, justQuit, printEarlyMessage,
printEarlyTraceback, printAutomaticTraceback, lastErrorStream;
context := options.context;
if not IsLVarsBag(context) then
PrintTo(ERROR_OUTPUT, "ErrorInner: option context must be a local variables bag\n");
LEAVE_ALL_NAMESPACES();
JUMP_TO_CATCH(1);
fi;
if IsBound(options.justQuit) then
justQuit := options.justQuit;
if not justQuit in [false, true] then
PrintTo(ERROR_OUTPUT, "ErrorInner: option justQuit must be true or false\n");
LEAVE_ALL_NAMESPACES();
JUMP_TO_CATCH(1);
fi;
else
justQuit := false;
fi;
if IsBound(options.mayReturnVoid) then
mayReturnVoid := options.mayReturnVoid;
if not mayReturnVoid in [false, true] then
PrintTo(ERROR_OUTPUT, "ErrorInner: option mayReturnVoid must be true or false\n");
LEAVE_ALL_NAMESPACES();
JUMP_TO_CATCH(1);
fi;
else
mayReturnVoid := false;
fi;
if IsBound(options.tracebackContext) then
tracebackContext := options.tracebackContext;
if not IsLVarsBag(tracebackContext) then
PrintTo(ERROR_OUTPUT, "ErrorInner: option tracebackContext must be a local variables bag\n");
LEAVE_ALL_NAMESPACES();
JUMP_TO_CATCH(1);
fi;
else
tracebackContext := context;
fi;
if IsBound(options.lateMessage) then
lateMessage := options.lateMessage;
if not lateMessage in [false, true] and not IsString(lateMessage) then
PrintTo(ERROR_OUTPUT, "ErrorInner: option lateMessage must be a string or false\n");
LEAVE_ALL_NAMESPACES();
JUMP_TO_CATCH(1);
fi;
else
lateMessage := "";
fi;
# Local functions that print the user feedback.
printEarlyMessage := function(stream)
PrintTo(stream, "Error, ");
# earlyMessage usually contains information about what went wrong.
for x in earlyMessage do
PrintTo(stream, x);
od;
end;
printEarlyTraceback := function(stream)
if not ERROR_MESSAGE_ENDS_WITH_NEWLINE(earlyMessage) then
PrintTo(stream, "\n");
fi;
end;
printAutomaticTraceback := function()
if IsBound(OnBreak) and IsFunction(OnBreak) then
OnBreak();
fi;
end;
ErrorLevel := ErrorLevel+1;
ERROR_COUNT := ERROR_COUNT+1;
errorLVars := ErrorLVars;
errorTracebackLVars := ErrorTracebackLVars;
kernelErrorLVars := CurrentEnv();
if kernelErrorLVars = fail then
kernelErrorLVars := GetBottomLVars();
fi;
ErrorLVars := context;
ErrorTracebackLVars := tracebackContext;
SET_ERROR_LVARS(context);
# Do we want to skip the break loop?
# BreakOnError is initialized by the `-T` command line flag in init.g
if QUITTING or not BreakOnError then
# If we skip the break loop, the standard behaviour is to print only
# the earlyMessage. If SilentNonInteractiveErrors is true we do not
# print any messages. If AlwaysPrintTracebackOnError is true we also
# call OnBreak(), which by default prints the traceback.
# SilentNonInteractiveErrors supersedes AlwaysPrintTracebackOnError.
# It is used by HPC-GAP to e.g. suppress error messages in worker
# threads.
if not SilentNonInteractiveErrors then
printEarlyMessage(ERROR_OUTPUT);
if AlwaysPrintTracebackOnError then
printEarlyTraceback(ERROR_OUTPUT);
printAutomaticTraceback();
else
PrintTo(ERROR_OUTPUT, "\n");
fi;
fi;
if IsHPCGAP then
# In HPC-GAP we want to access error messages encountered in
# tasks via TaskError. To this end we store the error message
# in the thread local variable LastErrorMessage.
LastErrorMessage := "";
lastErrorStream := OutputTextString(LastErrorMessage, true);
printEarlyMessage(lastErrorStream);
if AlwaysPrintTracebackOnError then
printEarlyTraceback(lastErrorStream);
# FIXME: Also make HPCGAP work with OnBreak().
# If AlwaysPrintTracebackOnError is true, the output of
# OnBreak() should also be put into LastErrorMessage.
# To do this there needs to be a way to put its output
# into lastErrorStream.
# OnBreak() is documented to not take any arguments.
# One could work around that if there were e.g. a GAP error
# stream which all error functions print to.
fi;
CloseStream(lastErrorStream);
MakeImmutable(LastErrorMessage);
fi;
ErrorLevel := ErrorLevel-1;
ErrorLVars := errorLVars;
ErrorTracebackLVars := errorTracebackLVars;
SET_ERROR_LVARS(kernelErrorLVars);
if IsBound(OnQuit) and IsFunction(OnQuit) then
OnQuit();
fi;
if ErrorLevel = 0 then LEAVE_ALL_NAMESPACES(); fi;
JUMP_TO_CATCH(0);
fi;
printEarlyMessage(ERROR_OUTPUT);
printEarlyTraceback(ERROR_OUTPUT);
if SHOULD_QUIT_ON_BREAK() then
# Again, the default is to not print the rest of the traceback.
# If AlwaysPrintTracebackOnError is true we do so anyways.
if AlwaysPrintTracebackOnError then
printAutomaticTraceback();
fi;
ForceQuitGap(1);
fi;
# OnBreak() is set to Where() by default, which prints the traceback.
printAutomaticTraceback();
# Now print lateMessage and OnBreakMessage a la "press return; to .."
if IsString(lateMessage) then
PrintTo(ERROR_OUTPUT, lateMessage,"\n");
elif lateMessage then
if IsBound(OnBreakMessage) and IsFunction(OnBreakMessage) then
OnBreakMessage();
fi;
fi;
if ErrorLevel > 1 then
prompt := Concatenation("brk_",String(ErrorLevel),"> ");
else
prompt := "brk> ";
fi;
if not justQuit then
res := SHELL(context,mayReturnVoid,false,true,prompt,false);
else
res := fail;
fi;
ErrorLevel := ErrorLevel-1;
ErrorLVars := errorLVars;
ErrorTracebackLVars := errorTracebackLVars;
SET_ERROR_LVARS(kernelErrorLVars);
if res = fail then
if IsBound(OnQuit) and IsFunction(OnQuit) then
OnQuit();
fi;
if ErrorLevel = 0 then LEAVE_ALL_NAMESPACES(); fi;
if not justQuit then
# dont try and do anything else after this before the longjump
SetUserHasQuit(1);
fi;
JUMP_TO_CATCH(3);
fi;
if Length(res) > 0 then
return res[1];
else
return;
fi;
end);
Unbind(Error);
BIND_GLOBAL("Error", function(arg)
ErrorInner(
rec(
context := ParentLVars(GetCurrentLVars()),
mayReturnVoid := true,
lateMessage := true,
),
arg);
end);
Unbind(ErrorNoReturn);
BIND_GLOBAL("ErrorNoReturn", function(arg)
ErrorInner(
rec(
context := ParentLVars(GetCurrentLVars()),
mayReturnVoid := false,
lateMessage := "type 'quit;' to quit to outer loop",
),
arg);
end);