1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* The Universal Permissive License (UPL), Version 1.0
@@ -101,19 +101,22 @@ abstract static class GetSetNode extends PythonTernaryBuiltinNode {
101
101
@ Child private GetMroNode getMroNode ;
102
102
@ Child private GetNameNode getNameNode ;
103
103
@ Child private IsSameTypeNode isSameTypeNode ;
104
+ @ Child private GetClassNode getClassNode ;
104
105
105
- private final IsBuiltinClassProfile isNoneBuiltinClassProfile = IsBuiltinClassProfile .create ();
106
+ private final IsBuiltinClassProfile isBuiltinPythonClassObject = IsBuiltinClassProfile .create ();
106
107
private final ConditionProfile isBuiltinProfile = ConditionProfile .createBinaryProfile ();
107
108
private final IsBuiltinClassProfile isBuiltinClassProfile = IsBuiltinClassProfile .create ();
108
109
private final BranchProfile errorBranch = BranchProfile .create ();
109
110
110
111
// 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 ) {
112
113
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 )) {
114
116
return true ;
115
117
}
116
118
}
119
+ PythonAbstractClass type = getClass (obj );
117
120
if (isBuiltinProfile .profile (descrType instanceof PythonBuiltinClassType )) {
118
121
PythonBuiltinClassType builtinClassType = (PythonBuiltinClassType ) descrType ;
119
122
for (PythonAbstractClass o : getMro (type )) {
@@ -132,6 +135,14 @@ protected boolean descr_check(LazyPythonClass descrType, String name, Object obj
132
135
throw raise (TypeError , "descriptor '%s' for '%s' objects doesn't apply to '%s' object" , name , getTypeName (descrType ), getTypeName (type ));
133
136
}
134
137
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
+
135
146
private PythonAbstractClass [] getMro (LazyPythonClass clazz ) {
136
147
if (getMroNode == null ) {
137
148
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -157,16 +168,16 @@ private boolean isSameType(Object left, Object right) {
157
168
}
158
169
}
159
170
160
- @ Builtin (name = __GET__ , minNumOfPositionalArgs = 3 )
171
+ @ Builtin (name = __GET__ , minNumOfPositionalArgs = 2 , maxNumOfPositionalArgs = 3 )
161
172
@ GenerateNodeFactory
162
173
abstract static class GetSetGetNode extends GetSetNode {
163
174
private final BranchProfile branchProfile = BranchProfile .create ();
164
175
165
176
// https://github.com/python/cpython/blob/e8b19656396381407ad91473af5da8b0d4346e88/Objects/descrobject.c#L149
166
177
@ 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 ,
168
179
@ Cached ("create()" ) CallUnaryMethodNode callNode ) {
169
- if (descr_check (descr .getType (), descr .getName (), obj , type )) {
180
+ if (descr_check (descr .getType (), descr .getName (), obj )) {
170
181
return descr ;
171
182
}
172
183
if (descr .getGet () != null ) {
@@ -178,10 +189,10 @@ Object get(VirtualFrame frame, GetSetDescriptor descr, Object obj, LazyPythonCla
178
189
}
179
190
180
191
@ Specialization
181
- Object getSlot (HiddenKeyDescriptor descr , Object obj , LazyPythonClass type ,
192
+ Object getSlot (HiddenKeyDescriptor descr , Object obj , @ SuppressWarnings ( "unused" ) Object type ,
182
193
@ Cached ("create()" ) ReadAttributeFromObjectNode readNode ,
183
194
@ 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 )) {
185
196
return descr ;
186
197
}
187
198
Object val = readNode .execute (obj , descr .getKey ());
@@ -195,14 +206,12 @@ Object getSlot(HiddenKeyDescriptor descr, Object obj, LazyPythonClass type,
195
206
@ Builtin (name = __SET__ , minNumOfPositionalArgs = 3 )
196
207
@ GenerateNodeFactory
197
208
abstract static class GetSetSetNode extends GetSetNode {
198
- @ Child GetClassNode getClassNode = GetClassNode .create ();
199
209
private final BranchProfile branchProfile = BranchProfile .create ();
200
210
201
211
@ Specialization
202
212
Object set (VirtualFrame frame , GetSetDescriptor descr , Object obj , Object value ,
203
213
@ 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 )) {
206
215
return descr ;
207
216
}
208
217
if (descr .getSet () != null ) {
@@ -216,8 +225,7 @@ Object set(VirtualFrame frame, GetSetDescriptor descr, Object obj, Object value,
216
225
@ Specialization
217
226
Object setSlot (HiddenKeyDescriptor descr , Object obj , Object value ,
218
227
@ 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 )) {
221
229
return descr ;
222
230
}
223
231
return writeNode .execute (obj , descr .getKey (), value );
0 commit comments