@@ -408,30 +408,32 @@ protected SourceSection findSourceLocation(PythonContext context, Object value)
408408
409409 @ Override
410410 protected String toString (PythonContext context , Object value ) {
411- final PythonModule builtins = context .getBuiltins ();
412- if (builtins != null ) {
413- // may be null during initialization
414- Object reprAttribute = builtins .getAttribute (BuiltinNames .REPR );
415- if (reprAttribute instanceof PBuiltinMethod ) {
416- // may be false if e.g. someone accessed our builtins reflectively
417- Object reprFunction = ((PBuiltinMethod ) reprAttribute ).getFunction ();
418- if (reprFunction instanceof PBuiltinFunction ) {
419- // may be false if our builtins were tampered with
420- Object [] userArgs = PArguments .create (2 );
421- PArguments .setArgument (userArgs , 0 , PNone .NONE );
422- PArguments .setArgument (userArgs , 1 , value );
423- try {
424- Object result = InvokeNode .invokeUncached ((PBuiltinFunction ) reprFunction , userArgs );
425- if (result instanceof String ) {
426- return (String ) result ;
427- } else if (result instanceof PString ) {
428- return ((PString ) result ).getValue ();
429- } else {
430- // This is illegal for a repr implementation, we ignore the result.
431- // At this point it's probably difficult to report this properly.
411+ if (PythonOptions .getFlag (context , PythonOptions .UseReprForPrintString )) {
412+ final PythonModule builtins = context .getBuiltins ();
413+ if (builtins != null ) {
414+ // may be null during initialization
415+ Object reprAttribute = builtins .getAttribute (BuiltinNames .REPR );
416+ if (reprAttribute instanceof PBuiltinMethod ) {
417+ // may be false if e.g. someone accessed our builtins reflectively
418+ Object reprFunction = ((PBuiltinMethod ) reprAttribute ).getFunction ();
419+ if (reprFunction instanceof PBuiltinFunction ) {
420+ // may be false if our builtins were tampered with
421+ Object [] userArgs = PArguments .create (2 );
422+ PArguments .setArgument (userArgs , 0 , PNone .NONE );
423+ PArguments .setArgument (userArgs , 1 , value );
424+ try {
425+ Object result = InvokeNode .invokeUncached ((PBuiltinFunction ) reprFunction , userArgs );
426+ if (result instanceof String ) {
427+ return (String ) result ;
428+ } else if (result instanceof PString ) {
429+ return ((PString ) result ).getValue ();
430+ } else {
431+ // This is illegal for a repr implementation, we ignore the result.
432+ // At this point it's probably difficult to report this properly.
433+ }
434+ } catch (PException e ) {
435+ // Fall through to default
432436 }
433- } catch (PException e ) {
434- // Fall through to default
435437 }
436438 }
437439 }
@@ -440,8 +442,12 @@ protected String toString(PythonContext context, Object value) {
440442 // return a String
441443 if (value instanceof PythonAbstractObject ) {
442444 return ((PythonAbstractObject ) value ).toString ();
445+ } else if (value instanceof String ) {
446+ return (String ) value ;
447+ } else if (value instanceof Number ) {
448+ return ((Number ) value ).toString ();
443449 } else {
444- return "illegal object" ;
450+ return "not a Python object" ;
445451 }
446452 }
447453
0 commit comments