Skip to content

Commit 03c2b3a

Browse files
committed
provide a __signature__ function that directly returns an inspect signature to avoid going through the ast module
1 parent bbda8c4 commit 03c2b3a

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public final class Python3Core implements PythonCore {
187187
"unicodedata",
188188
"_locale",
189189
"_sre",
190+
"function",
190191
};
191192

192193
private final PythonBuiltins[] builtins;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/AbstractFunctionBuiltins.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,15 @@ private static Object getSignature(boolean enclosingType, Arity arity) {
324324
if (keywordNames.length > 0) {
325325
int i = 0;
326326
while (i < requiredKeywords) {
327-
sb.append(", ").append(keywordNames[i++]);
327+
sb.append(", ").append(keywordNames[i++].name).append('=');
328328
}
329329
if (takesVarArgs) {
330330
sb.append(", *args");
331331
} else {
332332
sb.append(", *");
333333
}
334334
while (i < keywordNames.length) {
335-
sb.append(", ").append(keywordNames[i]).append("=?");
335+
sb.append(", ").append(keywordNames[i++].name).append("=?");
336336
}
337337
if (takesVarKeywordArgs) {
338338
sb.append(", **kwargs");

graalpython/lib-graalpython/function.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,35 @@
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
3939

40-
# an empty file for now
40+
def __signature__(self):
41+
import inspect
42+
text_sig = self.__text_signature__
43+
text_sig = text_sig[1:-1] # remove parens
44+
paramnames = [p.strip() for p in text_sig.split(",")]
45+
i = 0
46+
kind = inspect.Parameter.POSITIONAL_ONLY
47+
parameters = []
48+
while i < len(paramnames):
49+
p = paramnames[i]
50+
if p.startswith("$"):
51+
pass
52+
elif p == "/":
53+
kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
54+
elif p.startswith("**"):
55+
parameters.append(inspect.Parameter(p, inspect.Parameter.VAR_KEYWORD))
56+
elif p.startswith("*"):
57+
kind = inspect.Parameter.KEYWORD_ONLY
58+
if p != "*":
59+
parameters.append(inspect.Parameter(p, inspect.Parameter.VAR_POSITIONAL))
60+
else:
61+
if "=" in p:
62+
kind = inspect.Parameter.POSITIONAL_OR_KEYWORD
63+
parameters.append(inspect.Parameter(p, kind))
64+
i += 1
65+
66+
return inspect.Signature(parameters=parameters)
67+
68+
69+
# attach to builtin methods and builtin functions
70+
type(list.append).__signature__ = property(__signature__)
71+
type(abs).__signature__ = property(__signature__)

0 commit comments

Comments
 (0)