-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathPyBuiltinCallable.java
More file actions
150 lines (118 loc) · 4.02 KB
/
PyBuiltinCallable.java
File metadata and controls
150 lines (118 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* Copyright (c) Jython Developers */
package org.python.core;
import java.io.Serializable;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedType;
@Untraversable
@ExposedType(name = "builtin_function_or_method", isBaseType = false)
public abstract class PyBuiltinCallable extends PyObject {
protected Info info;
protected String doc;
protected PyBuiltinCallable(PyType type, Info info) {
super(type);
this.info = info;
}
protected PyBuiltinCallable(Info info) {
this.info = info;
}
/**
* Returns a new instance of this type of PyBuiltinFunction bound to self
*/
abstract public PyBuiltinCallable bind(PyObject self);
@ExposedGet(name = "__name__")
public PyObject fastGetName() {
return Py.newString(this.info.getName());
}
@ExposedGet(name = "__doc__")
public String getDoc() {
return doc;
}
@ExposedGet(name = "__module__")
public PyObject getModule() {
return Py.None;
}
@ExposedGet(name = "__call__")
public PyObject makeCall() {
return this;
}
@ExposedGet(name = "__self__")
public PyObject getSelf() {
return Py.None;
}
public void setInfo(Info info) {
this.info = info;
}
@Override
public String toString() {
PyObject self = getSelf();
if (self == null) {
return String.format("<built-in function %s>", info.getName());
} else {
return String.format("<built-in method %s of %s object at %s>", info.getName(),
self.getType().fastGetName(), Py.idstr(self));
}
}
public interface Info extends Serializable {
String getName();
int getMaxargs();
int getMinargs();
PyException unexpectedCall(int nargs, boolean keywords);
}
public static class DefaultInfo implements Info {
private String name;
private int maxargs, minargs;
public DefaultInfo(String name, int minargs, int maxargs) {
this.name = name;
this.minargs = minargs;
this.maxargs = maxargs;
}
public DefaultInfo(String name) {
this(name, -1, -1);
}
public String getName() {
return name;
}
public int getMaxargs() {
return maxargs;
}
public int getMinargs() {
return minargs;
}
public static boolean check(int nargs, int minargs, int maxargs) {
if (nargs < minargs) {
return false;
}
if (maxargs != -1 && nargs > maxargs) {
return false;
}
return true;
}
public static PyException unexpectedCall(int nargs, boolean keywords, String name,
int minargs, int maxargs) {
if (keywords) {
return Py.TypeError(name + "() takes no keyword arguments");
}
String argsblurb;
if (minargs == maxargs) {
if (minargs == 0) {
argsblurb = "no arguments";
} else if (minargs == 1) {
argsblurb = "exactly one argument";
} else {
argsblurb = minargs + " arguments";
}
} else if (maxargs == -1) {
return Py.TypeError(String.format("%s() requires at least %d arguments (%d) given",
name, minargs, nargs));
} else if (minargs <= 0) {
argsblurb = "at most " + maxargs + " arguments";
} else {
argsblurb = minargs + "-" + maxargs + " arguments";
}
return Py.TypeError(String.format("%s() takes %s (%d given)", name, argsblurb, nargs));
}
public PyException unexpectedCall(int nargs, boolean keywords) {
return unexpectedCall(nargs, keywords, name, minargs, maxargs);
}
}
}