Skip to content

Commit f9e99fa

Browse files
committed
add builtin_function_or_method.__doc__ and method.__doc__ getters that delegate to the underlying function
1 parent 6ff3fe0 commit f9e99fa

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/method/AbstractMethodBuiltins.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__FUNC__;
3030
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__MODULE__;
31+
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__DOC__;
3132
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__NAME__;
3233
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__SELF__;
3334
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
@@ -183,4 +184,50 @@ Object getModule(PBuiltinMethod self, Object value) {
183184
return PNone.NONE;
184185
}
185186
}
187+
188+
@Builtin(name = __DOC__, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
189+
@GenerateNodeFactory
190+
abstract static class DocNode extends PythonBinaryBuiltinNode {
191+
@Child ReadAttributeFromObjectNode readFunc;
192+
193+
private Object readFromFunc(Object func) {
194+
if (readFunc == null) {
195+
CompilerDirectives.transferToInterpreterAndInvalidate();
196+
readFunc = insert(ReadAttributeFromObjectNode.create());
197+
}
198+
return readFunc.execute(func, __DOC__);
199+
}
200+
201+
@Specialization(guards = "isNoValue(none)")
202+
Object getModule(PMethod self, @SuppressWarnings("unused") PNone none,
203+
@Cached("create()") ReadAttributeFromObjectNode readSelf) {
204+
Object doc = readSelf.execute(self, __DOC__);
205+
if (doc == PNone.NO_VALUE) {
206+
return readFromFunc(self.getFunction());
207+
}
208+
return doc;
209+
}
210+
211+
@Specialization(guards = "isNoValue(none)")
212+
Object getModule(PBuiltinMethod self, @SuppressWarnings("unused") PNone none,
213+
@Cached("create()") ReadAttributeFromObjectNode readSelf) {
214+
Object doc = readSelf.execute(self, __DOC__);
215+
if (doc == PNone.NO_VALUE) {
216+
return readFromFunc(self.getFunction());
217+
}
218+
return doc;
219+
}
220+
221+
@Specialization(guards = {"!isNoValue(value)"})
222+
Object getModule(PMethod self, Object value,
223+
@Cached("create()") WriteAttributeToObjectNode writeObject) {
224+
writeObject.execute(self, __DOC__, value);
225+
return PNone.NONE;
226+
}
227+
228+
@Specialization(guards = {"!isNoValue(value)"})
229+
Object getModule(@SuppressWarnings("unused") PBuiltinMethod self, @SuppressWarnings("unused") Object value) {
230+
throw raise(PythonBuiltinClassType.AttributeError, "attribute '__doc__' of 'builtin_function_or_method' objects is not writable");
231+
}
232+
}
186233
}

0 commit comments

Comments
 (0)