Skip to content

Commit b48aed8

Browse files
fmeumKila2
authored andcommitted
Add sep keyword argument to fail
The argument was already available on 'print' and is necessary for full control over the formatting of error messages that rely on StarlarkValue's `debugPrint`. Work towards bazelbuild#20486 Closes bazelbuild#21961. PiperOrigin-RevId: 625022818 Change-Id: I895b5844d8543f936fc31d367b12bc6291a10bf8
1 parent 75e3580 commit b48aed8

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/main/java/net/starlark/java/eval/MethodLibrary.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -812,16 +812,23 @@ public StarlarkList<?> dir(Object object, StarlarkThread thread) throws EvalExce
812812
"Deprecated. Causes an optional prefix containing this string to be added to the"
813813
+ " error message.",
814814
positional = false,
815-
named = true)
815+
named = true),
816+
@Param(
817+
name = "sep",
818+
defaultValue = "\" \"",
819+
named = true,
820+
positional = false,
821+
doc = "The separator string between the objects, default is space (\" \").")
816822
},
817823
extraPositionals =
818824
@Param(
819825
name = "args",
820826
doc =
821827
"A list of values, formatted with debugPrint (which is equivalent to str by"
822-
+ " default) and joined with spaces, that appear in the error message."),
828+
+ " default) and joined with sep (defaults to \" \"), that appear in the"
829+
+ " error message."),
823830
useStarlarkThread = true)
824-
public void fail(Object msg, Object attr, Tuple args, StarlarkThread thread)
831+
public void fail(Object msg, Object attr, String sep, Tuple args, StarlarkThread thread)
825832
throws EvalException {
826833
Printer printer = new Printer();
827834
boolean needSeparator = false;
@@ -832,14 +839,14 @@ public void fail(Object msg, Object attr, Tuple args, StarlarkThread thread)
832839
// msg acts like a leading element of args.
833840
if (msg != Starlark.NONE) {
834841
if (needSeparator) {
835-
printer.append(" ");
842+
printer.append(sep);
836843
}
837844
printer.debugPrint(msg, thread.getSemantics());
838845
needSeparator = true;
839846
}
840847
for (Object arg : args) {
841848
if (needSeparator) {
842-
printer.append(" ");
849+
printer.append(sep);
843850
}
844851
printer.debugPrint(arg, thread.getSemantics());
845852
needSeparator = true;

src/test/java/net/starlark/java/eval/MethodLibraryTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ ev.new Scenario()
755755
.testIfErrorContains("abc", "fail('abc')")
756756
.testIfErrorContains("18", "fail(18)")
757757
.testIfErrorContains("1 2 3", "fail(1, 2, 3)")
758+
.testIfErrorContains("1, 2, 3", "fail(1, 2, 3, sep=', ')")
758759
.testIfErrorContains("attribute foo: 1 2 3", "fail(1, 2, 3, attr='foo')") // deprecated
759760
.testIfErrorContains("0 1 2 3", "fail(1, 2, 3, msg=0)"); // deprecated
760761
}

0 commit comments

Comments
 (0)