Skip to content

Commit b1721bc

Browse files
committed
add an option to not call repr in PythonLanguage#toString
1 parent e381f60 commit b1721bc

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/PythonOptions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ private PythonOptions() {
151151
@Option(category = OptionCategory.EXPERT, help = "Determines wether context startup tries to re-use previously cached sources of the core library.") //
152152
public static final OptionKey<Boolean> WithCachedSources = new OptionKey<>(true);
153153

154+
@Option(category = OptionCategory.EXPERT, help = "Embedder option: what to print in response to PythonLanguage#toString.") //
155+
public static final OptionKey<Boolean> UseReprForPrintString = new OptionKey<>(true);
156+
154157
public static OptionDescriptors createDescriptors() {
155158
return new PythonOptionsOptionDescriptors();
156159
}

0 commit comments

Comments
 (0)