diff options
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/packages/MethodLibrary.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/packages/MethodLibrary.java | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/packages/MethodLibrary.java b/src/main/java/com/google/devtools/build/lib/packages/MethodLibrary.java index f2d1b4fe87..f67b0feec9 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/MethodLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/packages/MethodLibrary.java @@ -32,6 +32,7 @@ import com.google.devtools.build.lib.syntax.Environment; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.EvalUtils; import com.google.devtools.build.lib.syntax.FuncallExpression; +import com.google.devtools.build.lib.syntax.Printer; import com.google.devtools.build.lib.syntax.SelectorList; import com.google.devtools.build.lib.syntax.SelectorValue; import com.google.devtools.build.lib.syntax.SkylarkEnvironment; @@ -472,7 +473,7 @@ public class MethodLibrary { int res = stringFind(false, self, sub, start, end, "'end' argument to rindex"); if (res < 0) { throw new EvalException(loc, String.format("substring %s not found in %s", - EvalUtils.prettyPrintValue(sub), EvalUtils.prettyPrintValue(self))); + Printer.repr(sub), Printer.repr(self))); } return res; } @@ -498,7 +499,7 @@ public class MethodLibrary { int res = stringFind(true, self, sub, start, end, "'end' argument to index"); if (res < 0) { throw new EvalException(loc, String.format("substring %s not found in %s", - EvalUtils.prettyPrintValue(sub), EvalUtils.prettyPrintValue(self))); + Printer.repr(sub), Printer.repr(self))); } return res; } @@ -578,7 +579,7 @@ public class MethodLibrary { if (!kwargs.containsKey(word)) { throw new EvalException(loc, "No replacement found for '" + word + "'"); } - matcher.appendReplacement(result, EvalUtils.printValue(kwargs.get(word))); + matcher.appendReplacement(result, Printer.str(kwargs.get(word))); } matcher.appendTail(result); return result.toString(); @@ -718,7 +719,7 @@ public class MethodLibrary { Map<?, ?> dictionary = (Map<?, ?>) self; if (!dictionary.containsKey(key)) { throw new EvalException(loc, String.format("Key %s not found in dictionary", - EvalUtils.prettyPrintValue(key))); + Printer.repr(key))); } return dictionary.get(key); @@ -872,8 +873,17 @@ public class MethodLibrary { "Converts any object to string. This is useful for debugging.", mandatoryPositionals = {@Param(name = "x", doc = "The object to convert.")}) private static BuiltinFunction str = new BuiltinFunction("str") { - public String invoke(Object x) throws EvalException { - return EvalUtils.printValue(x); + public String invoke(Object x) { + return Printer.str(x); + } + }; + + @SkylarkSignature(name = "repr", returnType = String.class, doc = + "Converts any object to a string representation. This is useful for debugging.", + mandatoryPositionals = {@Param(name = "x", doc = "The object to convert.")}) + private static BuiltinFunction repr = new BuiltinFunction("repr") { + public String invoke(Object x) { + return Printer.repr(x); } }; @@ -908,12 +918,11 @@ public class MethodLibrary { return Integer.parseInt((String) x); } catch (NumberFormatException e) { throw new EvalException(loc, - "invalid literal for int(): " + EvalUtils.prettyPrintValue(x)); + "invalid literal for int(): " + Printer.repr(x)); } } else { throw new EvalException(loc, - String.format("argument must be string, int, or bool, not '%s'", - EvalUtils.prettyPrintValue(x))); + String.format("%s is not of type string or int or bool", Printer.repr(x))); } } }; @@ -1113,7 +1122,7 @@ public class MethodLibrary { return defaultValue; } else { throw new EvalException(loc, String.format("Object of type '%s' has no field %s", - EvalUtils.getDataTypeName(obj), EvalUtils.prettyPrintValue(name))); + EvalUtils.getDataTypeName(obj), Printer.repr(name))); } } return result; @@ -1189,7 +1198,7 @@ public class MethodLibrary { new com.google.common.base.Function<Object, String>() { @Override public String apply(Object input) { - return EvalUtils.printValue(input); + return Printer.str(input); }})); env.handleEvent(Event.warn(loc, msg)); return Environment.NONE; @@ -1298,7 +1307,7 @@ public class MethodLibrary { items, get, keys, values); private static final List<BaseFunction> pureGlobalFunctions = ImmutableList.<BaseFunction>of( - bool, int_, len, minus, select, sorted, str); + bool, int_, len, minus, repr, select, sorted, str); private static final List<BaseFunction> skylarkGlobalFunctions = ImmutableList.<BaseFunction>builder() |