From ec25acf23c7acf6a72e3d239328db6fd6d08e3a4 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 30 Jun 2023 00:12:19 +0200 Subject: [PATCH 1/5] Use `debugPrint` instead of `str` for `fail` arguments --- src/main/java/net/starlark/java/eval/MethodLibrary.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/starlark/java/eval/MethodLibrary.java b/src/main/java/net/starlark/java/eval/MethodLibrary.java index 38cecba0e081b7..525b97a98d094e 100644 --- a/src/main/java/net/starlark/java/eval/MethodLibrary.java +++ b/src/main/java/net/starlark/java/eval/MethodLibrary.java @@ -714,10 +714,10 @@ public void fail(Object msg, Object attr, Tuple args, StarlarkThread thread) List elems = new ArrayList<>(); // msg acts like a leading element of args. if (msg != Starlark.NONE) { - elems.add(Starlark.str(msg, thread.getSemantics())); + elems.add(new Printer().debugPrint(msg, thread.getSemantics()).toString()); } for (Object arg : args) { - elems.add(Starlark.str(arg, thread.getSemantics())); + elems.add(new Printer().debugPrint(arg, thread.getSemantics()).toString()); } String str = Joiner.on(" ").join(elems); if (attr != Starlark.NONE) { From ce414cf036aa232c2324bcd58be1692f40ade6f1 Mon Sep 17 00:00:00 2001 From: Alexandre Rostovtsev Date: Thu, 17 Aug 2023 13:01:09 -0400 Subject: [PATCH 2/5] Fix doc string --- src/main/java/net/starlark/java/eval/MethodLibrary.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/starlark/java/eval/MethodLibrary.java b/src/main/java/net/starlark/java/eval/MethodLibrary.java index 525b97a98d094e..fcd579cf780c6e 100644 --- a/src/main/java/net/starlark/java/eval/MethodLibrary.java +++ b/src/main/java/net/starlark/java/eval/MethodLibrary.java @@ -706,8 +706,8 @@ public StarlarkList dir(Object object, StarlarkThread thread) throws EvalExce @Param( name = "args", doc = - "A list of values, formatted with str and joined with spaces, that appear in the" - + " error message."), + "A list of values, formatted with debugPrint (which is equivalent to str by" + + " default) and joined with spaces, that appear in the error message."), useStarlarkThread = true) public void fail(Object msg, Object attr, Tuple args, StarlarkThread thread) throws EvalException { From e322d539c0b35b2ba1f56b7fc51cc9061cc09a97 Mon Sep 17 00:00:00 2001 From: Alexandre Rostovtsev Date: Thu, 17 Aug 2023 13:11:01 -0400 Subject: [PATCH 3/5] Reuse the same Printer --- .../net/starlark/java/eval/MethodLibrary.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/starlark/java/eval/MethodLibrary.java b/src/main/java/net/starlark/java/eval/MethodLibrary.java index fcd579cf780c6e..a1e8c02de4f984 100644 --- a/src/main/java/net/starlark/java/eval/MethodLibrary.java +++ b/src/main/java/net/starlark/java/eval/MethodLibrary.java @@ -711,19 +711,18 @@ public StarlarkList dir(Object object, StarlarkThread thread) throws EvalExce useStarlarkThread = true) public void fail(Object msg, Object attr, Tuple args, StarlarkThread thread) throws EvalException { - List elems = new ArrayList<>(); + Printer printer = new Printer(); + if (attr != Starlark.NONE) { + printer.append("attribute ").append((String) attr).append(":"); + } // msg acts like a leading element of args. if (msg != Starlark.NONE) { - elems.add(new Printer().debugPrint(msg, thread.getSemantics()).toString()); + printer.append(" ").debugPrint(msg, thread.getSemantics()); } for (Object arg : args) { - elems.add(new Printer().debugPrint(arg, thread.getSemantics()).toString()); - } - String str = Joiner.on(" ").join(elems); - if (attr != Starlark.NONE) { - str = String.format("attribute %s: %s", attr, str); + printer.append(" ").debugPrint(arg, thread.getSemantics()); } - throw Starlark.errorf("%s", str); + throw Starlark.errorf("%s", printer.toString()); } @StarlarkMethod( From 5d0edc465ec97e8e5879eaf3fba9183a4ea8389b Mon Sep 17 00:00:00 2001 From: Alexandre Rostovtsev Date: Thu, 17 Aug 2023 13:30:55 -0400 Subject: [PATCH 4/5] Unused imports --- src/main/java/net/starlark/java/eval/MethodLibrary.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/net/starlark/java/eval/MethodLibrary.java b/src/main/java/net/starlark/java/eval/MethodLibrary.java index a1e8c02de4f984..85a0801cdca7cf 100644 --- a/src/main/java/net/starlark/java/eval/MethodLibrary.java +++ b/src/main/java/net/starlark/java/eval/MethodLibrary.java @@ -15,13 +15,10 @@ package net.starlark.java.eval; import com.google.common.base.Ascii; -import com.google.common.base.Joiner; import com.google.common.collect.Ordering; -import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Iterator; -import java.util.List; import java.util.NoSuchElementException; import net.starlark.java.annot.Param; import net.starlark.java.annot.ParamType; From 3477c379df3e6a7142f0a7ae90993c91225b62d3 Mon Sep 17 00:00:00 2001 From: Alexandre Rostovtsev Date: Thu, 17 Aug 2023 13:59:51 -0400 Subject: [PATCH 5/5] Ensure we do not append extraneous spaces to printer --- .../java/net/starlark/java/eval/MethodLibrary.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/starlark/java/eval/MethodLibrary.java b/src/main/java/net/starlark/java/eval/MethodLibrary.java index 85a0801cdca7cf..36990a8586c53b 100644 --- a/src/main/java/net/starlark/java/eval/MethodLibrary.java +++ b/src/main/java/net/starlark/java/eval/MethodLibrary.java @@ -709,15 +709,25 @@ public StarlarkList dir(Object object, StarlarkThread thread) throws EvalExce public void fail(Object msg, Object attr, Tuple args, StarlarkThread thread) throws EvalException { Printer printer = new Printer(); + boolean needSeparator = false; if (attr != Starlark.NONE) { printer.append("attribute ").append((String) attr).append(":"); + needSeparator = true; } // msg acts like a leading element of args. if (msg != Starlark.NONE) { - printer.append(" ").debugPrint(msg, thread.getSemantics()); + if (needSeparator) { + printer.append(" "); + } + printer.debugPrint(msg, thread.getSemantics()); + needSeparator = true; } for (Object arg : args) { - printer.append(" ").debugPrint(arg, thread.getSemantics()); + if (needSeparator) { + printer.append(" "); + } + printer.debugPrint(arg, thread.getSemantics()); + needSeparator = true; } throw Starlark.errorf("%s", printer.toString()); }