Skip to content

Commit 4ec2044

Browse files
committed
Allow owner argument for getset_descr to be None
1 parent 7b9c0fb commit 4ec2044

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/getsetdescriptor/GetSetDescriptorTypeBuiltins.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 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
@@ -101,19 +101,22 @@ abstract static class GetSetNode extends PythonTernaryBuiltinNode {
101101
@Child private GetMroNode getMroNode;
102102
@Child private GetNameNode getNameNode;
103103
@Child private IsSameTypeNode isSameTypeNode;
104+
@Child private GetClassNode getClassNode;
104105

105-
private final IsBuiltinClassProfile isNoneBuiltinClassProfile = IsBuiltinClassProfile.create();
106+
private final IsBuiltinClassProfile isBuiltinPythonClassObject = IsBuiltinClassProfile.create();
106107
private final ConditionProfile isBuiltinProfile = ConditionProfile.createBinaryProfile();
107108
private final IsBuiltinClassProfile isBuiltinClassProfile = IsBuiltinClassProfile.create();
108109
private final BranchProfile errorBranch = BranchProfile.create();
109110

110111
// https://github.com/python/cpython/blob/e8b19656396381407ad91473af5da8b0d4346e88/Objects/descrobject.c#L70
111-
protected boolean descr_check(LazyPythonClass descrType, String name, Object obj, LazyPythonClass type) {
112+
protected boolean descr_check(LazyPythonClass descrType, String name, Object obj) {
112113
if (PGuards.isNone(obj)) {
113-
if (!isNoneBuiltinClassProfile.profileClass(type, PythonBuiltinClassType.PNone)) {
114+
// object's descriptors (__class__,...) need to work on every object including None
115+
if (!isBuiltinPythonClassObject.profileClass(descrType, PythonBuiltinClassType.PythonObject)) {
114116
return true;
115117
}
116118
}
119+
PythonAbstractClass type = getClass(obj);
117120
if (isBuiltinProfile.profile(descrType instanceof PythonBuiltinClassType)) {
118121
PythonBuiltinClassType builtinClassType = (PythonBuiltinClassType) descrType;
119122
for (PythonAbstractClass o : getMro(type)) {
@@ -132,6 +135,14 @@ protected boolean descr_check(LazyPythonClass descrType, String name, Object obj
132135
throw raise(TypeError, "descriptor '%s' for '%s' objects doesn't apply to '%s' object", name, getTypeName(descrType), getTypeName(type));
133136
}
134137

138+
private PythonAbstractClass getClass(Object obj) {
139+
if (getClassNode == null) {
140+
CompilerDirectives.transferToInterpreterAndInvalidate();
141+
getClassNode = insert(GetClassNode.create());
142+
}
143+
return getClassNode.execute(obj);
144+
}
145+
135146
private PythonAbstractClass[] getMro(LazyPythonClass clazz) {
136147
if (getMroNode == null) {
137148
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -157,16 +168,16 @@ private boolean isSameType(Object left, Object right) {
157168
}
158169
}
159170

160-
@Builtin(name = __GET__, minNumOfPositionalArgs = 3)
171+
@Builtin(name = __GET__, minNumOfPositionalArgs = 2, maxNumOfPositionalArgs = 3)
161172
@GenerateNodeFactory
162173
abstract static class GetSetGetNode extends GetSetNode {
163174
private final BranchProfile branchProfile = BranchProfile.create();
164175

165176
// https://github.com/python/cpython/blob/e8b19656396381407ad91473af5da8b0d4346e88/Objects/descrobject.c#L149
166177
@Specialization
167-
Object get(VirtualFrame frame, GetSetDescriptor descr, Object obj, LazyPythonClass type,
178+
Object get(VirtualFrame frame, GetSetDescriptor descr, Object obj, @SuppressWarnings("unused") Object type,
168179
@Cached("create()") CallUnaryMethodNode callNode) {
169-
if (descr_check(descr.getType(), descr.getName(), obj, type)) {
180+
if (descr_check(descr.getType(), descr.getName(), obj)) {
170181
return descr;
171182
}
172183
if (descr.getGet() != null) {
@@ -178,10 +189,10 @@ Object get(VirtualFrame frame, GetSetDescriptor descr, Object obj, LazyPythonCla
178189
}
179190

180191
@Specialization
181-
Object getSlot(HiddenKeyDescriptor descr, Object obj, LazyPythonClass type,
192+
Object getSlot(HiddenKeyDescriptor descr, Object obj, @SuppressWarnings("unused") Object type,
182193
@Cached("create()") ReadAttributeFromObjectNode readNode,
183194
@Cached("createBinaryProfile()") ConditionProfile profile) {
184-
if (descr_check(descr.getType(), descr.getKey().getName(), obj, type)) {
195+
if (descr_check(descr.getType(), descr.getKey().getName(), obj)) {
185196
return descr;
186197
}
187198
Object val = readNode.execute(obj, descr.getKey());
@@ -195,14 +206,12 @@ Object getSlot(HiddenKeyDescriptor descr, Object obj, LazyPythonClass type,
195206
@Builtin(name = __SET__, minNumOfPositionalArgs = 3)
196207
@GenerateNodeFactory
197208
abstract static class GetSetSetNode extends GetSetNode {
198-
@Child GetClassNode getClassNode = GetClassNode.create();
199209
private final BranchProfile branchProfile = BranchProfile.create();
200210

201211
@Specialization
202212
Object set(VirtualFrame frame, GetSetDescriptor descr, Object obj, Object value,
203213
@Cached("create()") CallBinaryMethodNode callNode) {
204-
// the noneType is not important here - there are no setters on None
205-
if (descr_check(descr.getType(), descr.getName(), obj, getClassNode.execute(obj))) {
214+
if (descr_check(descr.getType(), descr.getName(), obj)) {
206215
return descr;
207216
}
208217
if (descr.getSet() != null) {
@@ -216,8 +225,7 @@ Object set(VirtualFrame frame, GetSetDescriptor descr, Object obj, Object value,
216225
@Specialization
217226
Object setSlot(HiddenKeyDescriptor descr, Object obj, Object value,
218227
@Cached("create()") WriteAttributeToObjectNode writeNode) {
219-
// the noneType is not important here - there are no setters on None
220-
if (descr_check(descr.getType(), descr.getKey().getName(), obj, getClassNode.execute(obj))) {
228+
if (descr_check(descr.getType(), descr.getKey().getName(), obj)) {
221229
return descr;
222230
}
223231
return writeNode.execute(obj, descr.getKey(), value);

0 commit comments

Comments
 (0)