|
115 | 115 | import com.oracle.graal.python.builtins.Python3Core;
|
116 | 116 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
117 | 117 | import com.oracle.graal.python.builtins.PythonBuiltins;
|
118 |
| -import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode; |
119 |
| -import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.GetAttrNodeFactory; |
120 | 118 | import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.HexNodeFactory;
|
121 | 119 | import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.OctNodeFactory;
|
| 120 | +import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode; |
122 | 121 | import com.oracle.graal.python.builtins.modules.ast.AstModuleBuiltins;
|
123 | 122 | import com.oracle.graal.python.builtins.modules.io.IOModuleBuiltins;
|
124 | 123 | import com.oracle.graal.python.builtins.modules.io.IONodes;
|
|
194 | 193 | import com.oracle.graal.python.nodes.argument.ReadArgumentNode;
|
195 | 194 | import com.oracle.graal.python.nodes.attributes.DeleteAttributeNode;
|
196 | 195 | import com.oracle.graal.python.nodes.attributes.GetAttributeNode;
|
197 |
| -import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetAnyAttributeNode; |
198 |
| -import com.oracle.graal.python.nodes.attributes.GetAttributeNode.GetFixedAttributeNode; |
199 | 196 | import com.oracle.graal.python.nodes.attributes.LookupCallableSlotInMRONode;
|
200 | 197 | import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
|
201 | 198 | import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
|
@@ -1304,71 +1301,41 @@ Object delattr(VirtualFrame frame, Object object, Object name) {
|
1304 | 1301 | // getattr(object, name[, default])
|
1305 | 1302 | @Builtin(name = J_GETATTR, minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
|
1306 | 1303 | @GenerateNodeFactory
|
1307 |
| - public abstract static class GetAttrNode extends PythonTernaryBuiltinNode { |
1308 |
| - @NeverDefault |
1309 |
| - public static GetAttrNode create() { |
1310 |
| - return GetAttrNodeFactory.create(); |
1311 |
| - } |
1312 |
| - |
1313 |
| - public abstract Object executeWithArgs(VirtualFrame frame, Object primary, TruffleString name, Object defaultValue); |
| 1304 | + abstract static class GetAttrNode extends PythonTernaryBuiltinNode { |
1314 | 1305 |
|
1315 |
| - @SuppressWarnings({"unused", "truffle-static-method"}) |
1316 |
| - @Specialization(guards = {"stringEquals(cachedName, name, equalNode, inliningTarget, stringProfile)", "isNoValue(defaultValue)"}) |
1317 |
| - public Object getAttrDefault(VirtualFrame frame, Object primary, TruffleString name, PNone defaultValue, |
1318 |
| - @Bind("this") Node inliningTarget, |
1319 |
| - @Shared @Cached InlinedConditionProfile stringProfile, |
1320 |
| - @Shared @Cached TruffleString.EqualNode equalNode, |
1321 |
| - @Shared @Cached("name") TruffleString cachedName, |
1322 |
| - @Cached("create(name)") GetFixedAttributeNode getAttributeNode) { |
1323 |
| - return getAttributeNode.executeObject(frame, primary); |
| 1306 | + @Specialization(guards = "isNoValue(defaultValue)") |
| 1307 | + static Object getAttrNoDefault(VirtualFrame frame, Object primary, Object nameObj, @SuppressWarnings("unused") Object defaultValue, |
| 1308 | + @Shared @Cached CastToTruffleStringNode cast, |
| 1309 | + @Shared @Cached PRaiseNode raiseNode, |
| 1310 | + @Cached PyObjectGetAttr getAttr) { |
| 1311 | + TruffleString name = castName(nameObj, cast, raiseNode); |
| 1312 | + return getAttr.execute(frame, primary, name); |
1324 | 1313 | }
|
1325 | 1314 |
|
1326 |
| - @SuppressWarnings({"unused", "truffle-static-method"}) |
1327 |
| - @Specialization(guards = {"stringEquals(cachedName, name, equalNode, inliningTarget, stringProfile)", "!isNoValue(defaultValue)"}) |
1328 |
| - Object getAttr(VirtualFrame frame, Object primary, TruffleString name, Object defaultValue, |
| 1315 | + @Specialization(guards = "!isNoValue(defaultValue)") |
| 1316 | + static Object getAttrWithDefault(VirtualFrame frame, Object primary, Object nameObj, Object defaultValue, |
1329 | 1317 | @Bind("this") Node inliningTarget,
|
1330 |
| - @Shared @Cached InlinedConditionProfile stringProfile, |
1331 |
| - @Shared @Cached TruffleString.EqualNode equalNode, |
1332 |
| - @Shared @Cached("name") TruffleString cachedName, |
1333 |
| - @Cached("create(name)") GetFixedAttributeNode getAttributeNode, |
1334 |
| - @Shared @Cached IsBuiltinObjectProfile errorProfile) { |
1335 |
| - try { |
1336 |
| - return getAttributeNode.executeObject(frame, primary); |
1337 |
| - } catch (PException e) { |
1338 |
| - e.expectAttributeError(inliningTarget, errorProfile); |
| 1318 | + @Shared @Cached CastToTruffleStringNode cast, |
| 1319 | + @Shared @Cached PRaiseNode raiseNode, |
| 1320 | + @Cached InlinedConditionProfile noValueProfile, |
| 1321 | + @Cached PyObjectLookupAttr lookupAttr) { |
| 1322 | + TruffleString name = castName(nameObj, cast, raiseNode); |
| 1323 | + Object result = lookupAttr.execute(frame, primary, name); |
| 1324 | + if (noValueProfile.profile(inliningTarget, result == NO_VALUE)) { |
1339 | 1325 | return defaultValue;
|
| 1326 | + } else { |
| 1327 | + return result; |
1340 | 1328 | }
|
1341 | 1329 | }
|
1342 | 1330 |
|
1343 |
| - @Specialization(replaces = {"getAttr", "getAttrDefault"}, guards = "isNoValue(defaultValue)") |
1344 |
| - Object getAttrFromObject(VirtualFrame frame, Object primary, TruffleString name, @SuppressWarnings("unused") PNone defaultValue, |
1345 |
| - @Shared @Cached GetAnyAttributeNode getAttributeNode) { |
1346 |
| - return getAttributeNode.executeObject(frame, primary, name); |
1347 |
| - } |
1348 |
| - |
1349 |
| - @Specialization(replaces = {"getAttr", "getAttrDefault"}, guards = "!isNoValue(defaultValue)") |
1350 |
| - @SuppressWarnings("truffle-static-method") |
1351 |
| - Object getAttrFromObject(VirtualFrame frame, Object primary, TruffleString name, Object defaultValue, |
1352 |
| - @Bind("this") Node inliningTarget, |
1353 |
| - @Shared @Cached GetAnyAttributeNode getAttributeNode, |
1354 |
| - @Shared @Cached IsBuiltinObjectProfile errorProfile) { |
| 1331 | + private static TruffleString castName(Object nameObj, CastToTruffleStringNode cast, PRaiseNode raiseNode) { |
| 1332 | + TruffleString name; |
1355 | 1333 | try {
|
1356 |
| - return getAttributeNode.executeObject(frame, primary, name); |
1357 |
| - } catch (PException e) { |
1358 |
| - e.expectAttributeError(inliningTarget, errorProfile); |
1359 |
| - return defaultValue; |
| 1334 | + name = cast.execute(nameObj); |
| 1335 | + } catch (CannotCastException e) { |
| 1336 | + throw raiseNode.raise(TypeError, ErrorMessages.GETATTR_ATTRIBUTE_NAME_MUST_BE_STRING); |
1360 | 1337 | }
|
1361 |
| - } |
1362 |
| - |
1363 |
| - @Specialization |
1364 |
| - Object getAttr2(VirtualFrame frame, Object object, PString name, Object defaultValue) { |
1365 |
| - return executeWithArgs(frame, object, name.getValueUncached(), defaultValue); |
1366 |
| - } |
1367 |
| - |
1368 |
| - @Specialization(guards = "!isString(name)") |
1369 |
| - @SuppressWarnings("unused") |
1370 |
| - Object getAttrGeneric(Object primary, Object name, Object defaultValue) { |
1371 |
| - throw raise(TypeError, ErrorMessages.GETATTR_ATTRIBUTE_NAME_MUST_BE_STRING); |
| 1338 | + return name; |
1372 | 1339 | }
|
1373 | 1340 | }
|
1374 | 1341 |
|
|
0 commit comments