Make 3-arg form of getattr() work when third arg is None -- MOS_MIGRATED_REVID=128463789
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java index 59690f4..f103211 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/MethodLibrary.java
@@ -2120,7 +2120,7 @@ parameters = { @Param(name = "x", doc = "The struct whose attribute is accessed."), @Param(name = "name", doc = "The name of the struct attribute."), - @Param(name = "default", defaultValue = "None", + @Param(name = "default", defaultValue = "unbound", doc = "The default value to return in case the struct " + "doesn't have an attribute of the given name.")}, useLocation = true, useEnvironment = true) @@ -2133,7 +2133,7 @@ // 'Real' describes methods with structField() == false. Because DotExpression.eval returned // null in this case, we know that structField() cannot return true. boolean isRealMethod = hasMethod(obj, name, loc); - if (defaultValue != Runtime.NONE && !isRealMethod) { + if (defaultValue != Runtime.UNBOUND && !isRealMethod) { return defaultValue; } throw new EvalException(loc, Printer.format("Object of type '%s' has no attribute %r%s",
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java index 725a21f..1eaecb7 100644 --- a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java +++ b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
@@ -460,7 +460,8 @@ .testIfExactError( "Object of type 'string' has no attribute \"not_there\"", "getattr('a string', 'not_there')") - .testStatement("getattr('a string', 'not_there', 'use this')", "use this"); + .testStatement("getattr('a string', 'not_there', 'use this')", "use this") + .testStatement("getattr('a string', 'not there', None)", Runtime.NONE); } @Test