Skip to content

Commit e820164

Browse files
committed
Remove 'object.__getattr__'.
1 parent ef751a5 commit e820164

File tree

6 files changed

+127
-65
lines changed

6 files changed

+127
-65
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_property.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -88,6 +88,13 @@ def prop_x(self, ax):
8888
X.prop_x.fset(self, ax)
8989

9090

91+
ERROR_FROM_GETTER = "ERROR FROM GETTER"
92+
class Z:
93+
@property
94+
def prop_x(self):
95+
raise AttributeError(ERROR_FROM_GETTER)
96+
97+
9198
def test_properties():
9299
c = C(10)
93100
assert c.prop_x == 10
@@ -114,3 +121,11 @@ def test_properties():
114121
assert not_found
115122

116123
assert X.prop_x is not Y.prop_x
124+
125+
126+
def test_property_error():
127+
try:
128+
Z().prop_x
129+
except BaseException as e:
130+
assert isinstance(e, AttributeError), "did not get AttributeError, was %s" % type(e)
131+
assert str(e) == ERROR_FROM_GETTER, "did not get expected error message; was %s" % str(e)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,19 +1534,23 @@ public abstract static class PInteropGetAttributeNode extends Node {
15341534
public abstract Object execute(Object object, String attrName);
15351535

15361536
@Specialization
1537-
Object doIt(Object object, String attrName,
1537+
static Object doIt(Object object, String attrName,
15381538
@Cached LookupInheritedAttributeNode.Dynamic lookupGetattributeNode,
1539-
@Cached CallNode callGetattributeNode,
1539+
@Cached CallBinaryMethodNode callGetattributeNode,
15401540
@Cached LookupInheritedAttributeNode.Dynamic lookupGetattrNode,
1541-
@Cached CallNode callGetattrNode,
1542-
@Cached IsBuiltinClassProfile isBuiltinClassProfile) {
1541+
@Cached CallBinaryMethodNode callGetattrNode,
1542+
@Cached IsBuiltinClassProfile isBuiltinClassProfile,
1543+
@Cached("createBinaryProfile()") ConditionProfile hasGetattrProfile) {
15431544
try {
15441545
Object attrGetattribute = lookupGetattributeNode.execute(object, __GETATTRIBUTE__);
1545-
return callGetattributeNode.execute(null, attrGetattribute, object, attrName);
1546+
return callGetattributeNode.executeObject(attrGetattribute, object, attrName);
15461547
} catch (PException pe) {
15471548
pe.expect(AttributeError, isBuiltinClassProfile);
15481549
Object attrGetattr = lookupGetattrNode.execute(object, __GETATTR__);
1549-
return callGetattrNode.execute(null, attrGetattr, object, attrName);
1550+
if (hasGetattrProfile.profile(attrGetattr != PNone.NO_VALUE)) {
1551+
return callGetattrNode.executeObject(attrGetattr, object, attrName);
1552+
}
1553+
throw pe;
15501554
}
15511555
}
15521556

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
3636
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FORMAT__;
3737
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
38-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__;
3938
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__;
4039
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
4140
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INIT_SUBCLASS__;
@@ -429,15 +428,6 @@ public static GetAttributeNode create() {
429428
}
430429
}
431430

432-
@Builtin(name = __GETATTR__, minNumOfPositionalArgs = 2)
433-
@GenerateNodeFactory
434-
public abstract static class GetattrNode extends PythonBinaryBuiltinNode {
435-
@Specialization
436-
Object getattr(Object object, Object key) {
437-
throw raise(AttributeError, "'%p' object has no attribute '%s'", object, key);
438-
}
439-
}
440-
441431
@Builtin(name = __SETATTR__, minNumOfPositionalArgs = 3)
442432
@GenerateNodeFactory
443433
public abstract static class SetattrNode extends PythonTernaryBuiltinNode {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/attributes/GetAttributeNode.java

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -44,8 +44,10 @@
4444
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__;
4545
import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
4646

47+
import com.oracle.graal.python.builtins.objects.PNone;
4748
import com.oracle.graal.python.nodes.attributes.GetAttributeNodeGen.GetAnyAttributeNodeGen;
4849
import com.oracle.graal.python.nodes.attributes.GetAttributeNodeGen.GetFixedAttributeNodeGen;
50+
import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
4951
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
5052
import com.oracle.graal.python.nodes.expression.ExpressionNode;
5153
import com.oracle.graal.python.nodes.frame.ReadNode;
@@ -59,6 +61,7 @@
5961
import com.oracle.truffle.api.frame.VirtualFrame;
6062
import com.oracle.truffle.api.nodes.Node;
6163
import com.oracle.truffle.api.nodes.UnexpectedResultException;
64+
import com.oracle.truffle.api.profiles.ConditionProfile;
6265

6366
@NodeChild(value = "object", type = ExpressionNode.class)
6467
public abstract class GetAttributeNode extends ExpressionNode implements ReadNode {
@@ -108,12 +111,68 @@ public final StatementNode makeWriteNode(ExpressionNode rhs) {
108111

109112
public abstract ExpressionNode getObject();
110113

111-
public abstract static class GetFixedAttributeNode extends Node {
114+
abstract static class GetAttributeBaseNode extends Node {
115+
116+
@Child private LookupInheritedAttributeNode lookupGetattrNode;
117+
@Child private CallBinaryMethodNode callBinaryMethodNode;
118+
119+
@CompilationFinal private ConditionProfile hasGetattrProfile;
120+
121+
int dispatchGetAttrOrRethrowInt(VirtualFrame frame, Object object, Object key, PException pe) throws UnexpectedResultException {
122+
return ensureCallGetattrNode().executeInt(frame, lookupGetattrOrRethrow(object, pe), object, key);
123+
}
124+
125+
long dispatchGetAttrOrRethrowLong(VirtualFrame frame, Object object, Object key, PException pe) throws UnexpectedResultException {
126+
return ensureCallGetattrNode().executeLong(frame, lookupGetattrOrRethrow(object, pe), object, key);
127+
}
128+
129+
boolean dispatchGetAttrOrRethrowBool(VirtualFrame frame, Object object, Object key, PException pe) throws UnexpectedResultException {
130+
return ensureCallGetattrNode().executeBool(frame, lookupGetattrOrRethrow(object, pe), object, key);
131+
}
132+
133+
Object dispatchGetAttrOrRethrowObject(VirtualFrame frame, Object object, Object key, PException pe) {
134+
return ensureCallGetattrNode().executeObject(frame, lookupGetattrOrRethrow(object, pe), object, key);
135+
}
136+
137+
/** Lookup {@code __getattr__} or rethrow {@code pe} if it does not exist. */
138+
private Object lookupGetattrOrRethrow(Object object, PException pe) {
139+
Object getattrAttribute = ensureLookupGetattrNode().execute(object);
140+
if (ensureHasGetattrProfile().profile(getattrAttribute == PNone.NO_VALUE)) {
141+
throw pe;
142+
}
143+
return getattrAttribute;
144+
}
145+
146+
private LookupInheritedAttributeNode ensureLookupGetattrNode() {
147+
if (lookupGetattrNode == null) {
148+
CompilerDirectives.transferToInterpreterAndInvalidate();
149+
lookupGetattrNode = insert(LookupInheritedAttributeNode.create(__GETATTR__));
150+
}
151+
return lookupGetattrNode;
152+
}
153+
154+
private CallBinaryMethodNode ensureCallGetattrNode() {
155+
if (callBinaryMethodNode == null) {
156+
CompilerDirectives.transferToInterpreterAndInvalidate();
157+
callBinaryMethodNode = insert(CallBinaryMethodNode.create());
158+
}
159+
return callBinaryMethodNode;
160+
}
161+
162+
private ConditionProfile ensureHasGetattrProfile() {
163+
if (hasGetattrProfile == null) {
164+
CompilerDirectives.transferToInterpreterAndInvalidate();
165+
hasGetattrProfile = ConditionProfile.createBinaryProfile();
166+
}
167+
return hasGetattrProfile;
168+
}
169+
}
170+
171+
public abstract static class GetFixedAttributeNode extends GetAttributeBaseNode {
112172

113173
private final String key;
114174

115175
@Child private LookupAndCallBinaryNode dispatchNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
116-
@Child private LookupAndCallBinaryNode dispatchGetAttr;
117176
@CompilationFinal private IsBuiltinClassProfile isBuiltinClassProfile = IsBuiltinClassProfile.create();
118177

119178
public GetFixedAttributeNode(String key) {
@@ -138,7 +197,7 @@ protected int doItInt(VirtualFrame frame, Object object) throws UnexpectedResult
138197
return dispatchNode.executeInt(frame, object, key);
139198
} catch (PException pe) {
140199
pe.expect(AttributeError, isBuiltinClassProfile);
141-
return getDispatchGetAttr().executeInt(frame, object, key);
200+
return dispatchGetAttrOrRethrowInt(frame, object, key, pe);
142201
}
143202
}
144203

@@ -148,7 +207,7 @@ protected long doItLong(VirtualFrame frame, Object object) throws UnexpectedResu
148207
return dispatchNode.executeLong(frame, object, key);
149208
} catch (PException pe) {
150209
pe.expect(AttributeError, isBuiltinClassProfile);
151-
return getDispatchGetAttr().executeInt(frame, object, key);
210+
return dispatchGetAttrOrRethrowLong(frame, object, key, pe);
152211
}
153212
}
154213

@@ -158,7 +217,7 @@ protected boolean doItBoolean(VirtualFrame frame, Object object) throws Unexpect
158217
return dispatchNode.executeBool(frame, object, key);
159218
} catch (PException pe) {
160219
pe.expect(AttributeError, isBuiltinClassProfile);
161-
return getDispatchGetAttr().executeBool(frame, object, key);
220+
return dispatchGetAttrOrRethrowBool(frame, object, key, pe);
162221
}
163222
}
164223

@@ -168,27 +227,18 @@ protected Object doIt(VirtualFrame frame, Object object) {
168227
return dispatchNode.executeObject(frame, object, key);
169228
} catch (PException pe) {
170229
pe.expect(AttributeError, isBuiltinClassProfile);
171-
return getDispatchGetAttr().executeObject(frame, object, key);
230+
return dispatchGetAttrOrRethrowObject(frame, object, key, pe);
172231
}
173232
}
174233

175-
private LookupAndCallBinaryNode getDispatchGetAttr() {
176-
if (dispatchGetAttr == null) {
177-
CompilerDirectives.transferToInterpreterAndInvalidate();
178-
dispatchGetAttr = insert(LookupAndCallBinaryNode.create(__GETATTR__));
179-
}
180-
return dispatchGetAttr;
181-
}
182-
183234
public static GetFixedAttributeNode create(String key) {
184235
return GetFixedAttributeNodeGen.create(key);
185236
}
186237
}
187238

188-
public abstract static class GetAnyAttributeNode extends Node {
239+
public abstract static class GetAnyAttributeNode extends GetAttributeBaseNode {
189240

190241
@Child private LookupAndCallBinaryNode dispatchNode = LookupAndCallBinaryNode.create(__GETATTRIBUTE__);
191-
@Child private LookupAndCallBinaryNode dispatchGetAttr;
192242
@CompilationFinal private IsBuiltinClassProfile isBuiltinClassProfile = IsBuiltinClassProfile.create();
193243

194244
public abstract int executeInt(VirtualFrame frame, Object object, Object key) throws UnexpectedResultException;
@@ -205,7 +255,7 @@ protected int doItInt(VirtualFrame frame, Object object, String key) throws Unex
205255
return dispatchNode.executeInt(frame, object, key);
206256
} catch (PException pe) {
207257
pe.expect(AttributeError, isBuiltinClassProfile);
208-
return getDispatchGetAttr().executeInt(frame, object, key);
258+
return dispatchGetAttrOrRethrowInt(frame, object, key, pe);
209259
}
210260
}
211261

@@ -215,7 +265,7 @@ protected long doItLong(VirtualFrame frame, Object object, Object key) throws Un
215265
return dispatchNode.executeLong(frame, object, key);
216266
} catch (PException pe) {
217267
pe.expect(AttributeError, isBuiltinClassProfile);
218-
return getDispatchGetAttr().executeInt(frame, object, key);
268+
return dispatchGetAttrOrRethrowLong(frame, object, key, pe);
219269
}
220270
}
221271

@@ -225,7 +275,7 @@ protected boolean doItBoolean(VirtualFrame frame, Object object, Object key) thr
225275
return dispatchNode.executeBool(frame, object, key);
226276
} catch (PException pe) {
227277
pe.expect(AttributeError, isBuiltinClassProfile);
228-
return getDispatchGetAttr().executeBool(frame, object, key);
278+
return dispatchGetAttrOrRethrowBool(frame, object, key, pe);
229279
}
230280
}
231281

@@ -235,16 +285,8 @@ protected Object doIt(VirtualFrame frame, Object object, Object key) {
235285
return dispatchNode.executeObject(frame, object, key);
236286
} catch (PException pe) {
237287
pe.expect(AttributeError, isBuiltinClassProfile);
238-
return getDispatchGetAttr().executeObject(frame, object, key);
239-
}
240-
}
241-
242-
private LookupAndCallBinaryNode getDispatchGetAttr() {
243-
if (dispatchGetAttr == null) {
244-
CompilerDirectives.transferToInterpreterAndInvalidate();
245-
dispatchGetAttr = insert(LookupAndCallBinaryNode.create(__GETATTR__));
288+
return dispatchGetAttrOrRethrowObject(frame, object, key, pe);
246289
}
247-
return dispatchGetAttr;
248290
}
249291

250292
public static GetAnyAttributeNode create() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/call/special/CallBinaryMethodNode.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,18 @@ public static CallBinaryMethodNode getUncached() {
6767

6868
public abstract boolean executeBool(VirtualFrame frame, Object callable, boolean arg, boolean arg2) throws UnexpectedResultException;
6969

70+
public abstract boolean executeBool(VirtualFrame frame, Object callable, Object arg, Object arg2) throws UnexpectedResultException;
71+
7072
public abstract int executeInt(VirtualFrame frame, Object callable, boolean arg, boolean arg2) throws UnexpectedResultException;
7173

7274
public abstract int executeInt(VirtualFrame frame, Object callable, int arg, int arg2) throws UnexpectedResultException;
7375

76+
public abstract int executeInt(VirtualFrame frame, Object callable, Object arg, Object arg2) throws UnexpectedResultException;
77+
7478
public abstract long executeLong(VirtualFrame frame, Object callable, long arg, long arg2) throws UnexpectedResultException;
7579

80+
public abstract long executeLong(VirtualFrame frame, Object callable, Object arg, Object arg2) throws UnexpectedResultException;
81+
7682
public abstract double executeDouble(VirtualFrame frame, Object callable, double arg, double arg2) throws UnexpectedResultException;
7783

7884
public abstract boolean executeBool(VirtualFrame frame, Object callable, int arg, int arg2) throws UnexpectedResultException;
@@ -289,6 +295,11 @@ public boolean executeBool(VirtualFrame frame, Object callable, boolean arg, boo
289295
return expectBooleanResult(executeObject(frame, callable, arg, arg2));
290296
}
291297

298+
@Override
299+
public boolean executeBool(VirtualFrame frame, Object callable, Object arg, Object arg2) throws UnexpectedResultException {
300+
return expectBooleanResult(executeObject(frame, callable, arg, arg2));
301+
}
302+
292303
@Override
293304
public int executeInt(VirtualFrame frame, Object callable, boolean arg, boolean arg2) throws UnexpectedResultException {
294305
return expectIntegerResult(executeObject(frame, callable, arg, arg2));
@@ -299,11 +310,21 @@ public int executeInt(VirtualFrame frame, Object callable, int arg, int arg2) th
299310
return expectIntegerResult(executeObject(frame, callable, arg, arg2));
300311
}
301312

313+
@Override
314+
public int executeInt(VirtualFrame frame, Object callable, Object arg, Object arg2) throws UnexpectedResultException {
315+
return expectIntegerResult(executeObject(frame, callable, arg, arg2));
316+
}
317+
302318
@Override
303319
public long executeLong(VirtualFrame frame, Object callable, long arg, long arg2) throws UnexpectedResultException {
304320
return expectLongResult(executeObject(frame, callable, arg, arg2));
305321
}
306322

323+
@Override
324+
public long executeLong(VirtualFrame frame, Object callable, Object arg, Object arg2) throws UnexpectedResultException {
325+
return expectLongResult(executeObject(frame, callable, arg, arg2));
326+
}
327+
307328
@Override
308329
public double executeDouble(VirtualFrame frame, Object callable, double arg, double arg2) throws UnexpectedResultException {
309330
return expectDoubleResult(executeObject(frame, callable, arg, arg2));

0 commit comments

Comments
 (0)