Log Message:
-----------
Added a routine to trap errors in a function call. I've needed this
for a long time, and got tired of working around the lack by hacks
involving PG_restricted_eval, which can't handle local variables from
the calling function. This addition is still safe because it is
passed code (not a string) and the code is already compiled in the
safe compartment, so it can't include the disallowed commands.
Now I need to go back to remove the hacks from the various pg/macros
files where they currently exist.
Modified Files:
--------------
pg/lib/Value:
WeBWorK.pm
Revision Data
-------------
Index: WeBWorK.pm
===================================================================
RCS file: /webwork/cvs/system/pg/lib/Value/WeBWorK.pm,v
retrieving revision 1.14
retrieving revision 1.15
diff -Llib/Value/WeBWorK.pm -Llib/Value/WeBWorK.pm -u -r1.14 -r1.15
--- lib/Value/WeBWorK.pm
+++ lib/Value/WeBWorK.pm
@@ -32,10 +32,24 @@
}
#
+# Call a subtroutine with error trapping.
+# (We need to be able to do this from custom
+# answer checkers and other MathObject code.
+# PG_restricted_eval is not sufficient for this
+# since that uses a string and so can't access
+# local variables from the calling routine.)
+#
+sub Eval {
+ my $f = shift;
+ return unless defined($f);
+ eval {&$f(@_)};
+}
+
+#
# Remove backtrace and line number, since these
# will be reported in the student message area.
#
-sub Parser::reportEvalError {
+sub reportEvalError {
my $error = shift; my $fullerror = $error;
$error =~ s/ at \S+\.\S+ line \d+(\n|.)*//;
$error =~ s/ at line \d+ of (\n|.)*//;
|