@@ -315,7 +315,7 @@ sub externalCommand {
315315
316316# Isolate the call to the sage server in case we have to jazz it up.
317317sub query_sage_server {
318- my ($python , $url , $accepted_tos , $ setSeed , $webworkfunc , $debug , $curlCommand ) = @_ ;
318+ my ($python , $url , $setSeed , $webworkfunc , $debug , $curlCommand ) = @_ ;
319319
320320 # Validate url to prevent shell injection — must look like a URL.
321321 if ($url && $url !~ m { ^https?://[^\s ;|&`\$ ]+$} ) {
@@ -328,7 +328,7 @@ sub query_sage_server {
328328 $curlCommand , ' -i' ,
329329 ' -k' , ' -sS' ,
330330 ' -L' , ' --data-urlencode' ,
331- " accepted_tos=$accepted_tos " , ' --data-urlencode' ,
331+ " accepted_tos=true " , ' --data-urlencode' ,
332332 ' user_expressions={"WEBWORK":"_webwork_safe_json(WEBWORK)"}' , ' --data-urlencode' ,
333333 " code=${setSeed}${webworkfunc}$python " , $url // (),
334334 );
@@ -400,8 +400,7 @@ sub query_sage_server {
400400 # Success! Put any extraneous splits back together.
401401 $result = join (" \r\n\r\n " , @lines );
402402 } else {
403- warn " ERROR in contacting sage server. Did you accept the terms of service by "
404- . " setting { accepted_tos => 'true' } in the askSage options?\n $content \n " ;
403+ warn " ERROR in contacting sage server.\n $content \n " ;
405404 $result = undef ;
406405 }
407406
@@ -421,44 +420,44 @@ sub AskSage {
421420 chomp ($python );
422421
423422 # To send values back in a hash, add them to the python WEBWORK dictionary.
424- my $url = $args -> {url } || ' https://sagecell.sagemath.org/service' ;
425- my $seed = $args -> {seed };
426- my $accepted_tos = $args -> {accepted_tos } || ' false' ; # Force author to accept terms of service explicitly.
427- my $debug = $args -> {debug } || 0;
428- my $setSeed = $seed ? " set_random_seed($seed )\n " : ' ' ;
429- my $curlCommand = $args -> {curlCommand };
423+ my $url = $args -> {url } || $pg_envir -> {URLs }{sagecellService } || ' https://sagecell.sagemath.org/service' ;
424+ my $seed = $args -> {seed };
425+ my $debug = $args -> {debug } || 0;
426+ my $setSeed = $seed ? " set_random_seed($seed )\n " : ' ' ;
427+ my $curlCommand = $args -> {curlCommand };
430428
431429 my $webworkfunc = <<END ;
432430WEBWORK={}
433431
434432def _webwork_safe_json(o):
435433 import json
436- def default(o):
437- try:
438- if isinstance(o,sage.rings.integer.Integer):
439- json_obj = int(o)
440- elif isinstance(o,(sage.rings.real_mpfr.RealLiteral, sage.rings.real_mpfr.RealNumber)):
441- json_obj = float(o)
442- elif sage.modules.free_module_element.is_FreeModuleElement(o):
443- json_obj = list(o)
444- elif sage.matrix.matrix.is_Matrix(o):
445- json_obj = [list(i) for i in o.rows()]
446- elif isinstance(o, SageObject):
447- json_obj = repr(o)
434+ class WebworkEncoder(json.JSONEncoder):
435+ def default(self, o):
436+ try:
437+ if isinstance(o,sage.rings.integer.Integer):
438+ json_obj = int(o)
439+ elif isinstance(o,(sage.rings.real_mpfr.RealLiteral, sage.rings.real_mpfr.RealNumber)):
440+ json_obj = float(o)
441+ elif isinstance(o, sage.modules.free_module_element.FreeModuleElement):
442+ json_obj = list(o)
443+ elif isinstance(o, sage.matrix.matrix0.Matrix):
444+ json_obj = [list(i) for i in o.rows()]
445+ elif isinstance(o, SageObject):
446+ json_obj = repr(o)
447+ else:
448+ raise TypeError
449+ except TypeError:
450+ pass
448451 else:
449- raise TypeError
450- except TypeError:
451- pass
452- else:
453- return json_obj
454- # Let the base class default method raise the TypeError
455- return json.JSONEncoder.default(self, o)
456- return json.dumps(o, default=default)
452+ return json_obj
453+ # Let the base class default method raise the TypeError
454+ return super().default(o)
455+ return json.dumps(o, cls=WebworkEncoder)
457456END
458457
459458 my $ret = { success => 0 }; # We want to export more than one piece of information.
460459 eval {
461- my $output = query_sage_server($python , $url , $accepted_tos , $ setSeed , $webworkfunc , $debug , $curlCommand );
460+ my $output = query_sage_server($python , $url , $setSeed , $webworkfunc , $debug , $curlCommand );
462461
463462 # has something been returned?
464463 not_null($output ) or die " Unable to make a sage call to $url ." ;
486485 warn " now success is $success because status was ok" if $debug ;
487486 if ($success ) {
488487 my $WEBWORK_variable_non_empty = 0;
489- my $sage_WEBWORK_data = $decoded -> {execute_reply }{user_expressions }{WEBWORK }{data }{' text/plain' };
488+ my $sage_WEBWORK_data = $decoded -> {execute_reply }{user_expressions }{WEBWORK }{data }{' text/plain' } // ' ' ;
490489 warn " sage_WEBWORK_data $sage_WEBWORK_data " if $debug ;
491490 if (not_null($sage_WEBWORK_data )) {
492491 $WEBWORK_variable_non_empty = # another hack because '{}' is sometimes returned
0 commit comments