Skip to content

Commit 3e5f793

Browse files
committed
Expose PyBuiltinFunction using annotations, and add support for the equivalent to exposed_as with a marker interface, ExposeAsSuperclass. Rip out exposed_as since only PyBuiltinFunction was using it and rip out exposed_methods as well since that wasn't used at all.
1 parent 6e61227 commit 3e5f793

6 files changed

Lines changed: 33 additions & 60 deletions

File tree

CoreExposed.includes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
org/python/core/PyBaseString.class
2+
org/python/core/PyBuiltinFunction.class
23
org/python/core/PyInteger.class
34
org/python/core/PyNone.class
45
org/python/core/PyObject.class

src/org/python/core/PyBuiltinFunction.java

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
package org.python.core;
22

3-
public abstract class PyBuiltinFunction extends PyObject implements
4-
PyType.Newstyle {
5-
6-
public static final String exposed_name = "builtin_function_or_method";
7-
8-
public static void typeSetup(PyObject dict, PyType.Newstyle marker) {
9-
dict.__setitem__("__name__", new PyGetSetDescr("__name__",
10-
PyBuiltinFunction.class,
11-
"fastGetName",
12-
null));
13-
dict.__setitem__("__self__", new PyGetSetDescr("__self__",
14-
PyBuiltinFunction.class,
15-
"getSelf",
16-
null));
17-
dict.__setitem__("__doc__", new PyGetSetDescr("__doc__",
18-
PyBuiltinFunction.class,
19-
"fastGetDoc",
20-
null));
21-
dict.__setitem__("__call__", new PyGetSetDescr("__call__",
22-
PyBuiltinFunction.class,
23-
"makeCall",
24-
null));
25-
}
3+
import org.python.expose.ExposedGet;
4+
import org.python.expose.ExposedType;
5+
6+
@ExposedType(name="builtin_function_or_method")
7+
public abstract class PyBuiltinFunction extends PyObject {
268

279
public interface Info {
2810

@@ -119,6 +101,7 @@ public void setInfo(Info info) {
119101
*/
120102
abstract public PyBuiltinFunction bind(PyObject self);
121103

104+
@ExposedGet(name="__self__")
122105
public PyObject getSelf() {
123106
return Py.None;
124107
}
@@ -134,14 +117,17 @@ public String toString() {
134117
}
135118
}
136119

120+
@ExposedGet(name="__name__")
137121
public PyObject fastGetName() {
138122
return Py.newString(this.info.getName());
139123
}
140124

125+
@ExposedGet(name="__doc__")
141126
public PyObject fastGetDoc() {
142127
return Py.None;
143128
}
144129

130+
@ExposedGet(name="__call__")
145131
public PyObject makeCall() {
146132
return this;
147133
}

src/org/python/core/PyBuiltinFunctionSet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) Corporation for National Research Initiatives
22
package org.python.core;
33

4+
import org.python.expose.ExposeAsSuperclass;
5+
46
/**
57
* A helper class for faster implementations of commonly called methods.
68
* <p>
79
* Subclasses of PyBuiltinFunctionSet will implement some or all of the __call__
810
* method with a switch on the index number.
911
*
1012
*/
11-
public class PyBuiltinFunctionSet extends PyBuiltinFunction {
12-
13-
public static final Class exposed_as = PyBuiltinFunction.class;
13+
public class PyBuiltinFunctionSet extends PyBuiltinFunction implements ExposeAsSuperclass {
1414

1515
// used as an index into a big switch statement in the various derived
1616
// class's __call__() methods.

src/org/python/core/PyBuiltinMethod.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package org.python.core;
22

3+
import org.python.expose.ExposeAsSuperclass;
34

4-
public abstract class PyBuiltinMethod extends PyBuiltinFunction {
55

6-
public static final Class exposed_as = PyBuiltinFunction.class;
6+
public abstract class PyBuiltinMethod extends PyBuiltinFunction implements ExposeAsSuperclass {
77

88
protected PyBuiltinMethod(PyObject self, Info info) {
99
super(info);

src/org/python/core/PyType.java

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Iterator;
1515
import java.util.List;
1616

17+
import org.python.expose.ExposeAsSuperclass;
1718
import org.python.expose.ExposedDelete;
1819
import org.python.expose.ExposedGet;
1920
import org.python.expose.ExposedMethod;
@@ -786,7 +787,6 @@ private static void fillFromClass(PyType newtype,
786787
Class base,
787788
boolean newstyle,
788789
Method setup,
789-
String[] exposed_methods,
790790
TypeBuilder tb) {
791791

792792
if(base == null) {
@@ -827,7 +827,7 @@ private static void fillFromClass(PyType newtype,
827827
} else {
828828
dict = new PyStringMap();
829829
if(newstyle) {
830-
fillInNewstyle(newtype, setup, exposed_methods, dict);
830+
fillInNewstyle(newtype, setup, dict);
831831
} else {
832832
fillInClassic(c, base, dict);
833833
}
@@ -987,13 +987,7 @@ public PyObject new_impl(boolean init,
987987

988988
private static void fillInNewstyle(PyType newtype,
989989
Method setup,
990-
String[] exposed_methods,
991990
PyObject dict) {
992-
for(int i = 0; i < exposed_methods.length; i++) {
993-
String methname = exposed_methods[i];
994-
dict.__setitem__(normalize_name(methname),
995-
new PyReflectedFunction(methname));
996-
}
997991
if(setup != null) {
998992
try {
999993
setup.invoke(null, new Object[] {dict, null});
@@ -1016,21 +1010,26 @@ public static void addBuilder(Class forClass, TypeBuilder builder) {
10161010
classToBuilder = new HashMap<Class, TypeBuilder>();
10171011
}
10181012
classToBuilder.put(forClass, builder);
1019-
if(builder.getTypeClass().equals(PyObject.class)
1020-
|| builder.getTypeClass().equals(PyType.class)) {
1013+
1014+
if(class_to_type.containsKey(forClass)) {
10211015
// PyObject and PyType are loaded as part of creating their
10221016
// builders, so they need to be bootstrapped
10231017
PyType objType = fromClass(builder.getTypeClass());
1018+
objType.name = builder.getName();
10241019
objType.dict = builder.getDict(objType);
10251020
}
10261021
}
10271022

10281023
private static PyType addFromClass(Class c) {
1024+
if(ExposeAsSuperclass.class.isAssignableFrom(c)) {
1025+
PyType exposedAs = fromClass(c.getSuperclass());
1026+
class_to_type.put(c, exposedAs);
1027+
return exposedAs;
1028+
}
10291029
Method setup = null;
10301030
boolean newstyle = Newstyle.class.isAssignableFrom(c);
10311031
Class base = null;
10321032
String name = null;
1033-
String[] exposed_methods = null;
10341033
TypeBuilder tb = classToBuilder == null ? null : classToBuilder.get(c);
10351034
if(tb != null) {
10361035
name = tb.getName();
@@ -1049,28 +1048,13 @@ private static PyType addFromClass(Class c) {
10491048
if(newstyle) { // newstyle
10501049
base = (Class)exposed_decl_get_object(c, "base");
10511050
name = (String)exposed_decl_get_object(c, "name");
1052-
if(base == null) {
1053-
Class cur = c;
1054-
while(cur != PyObject.class) {
1055-
Class exposed_as = (Class)exposed_decl_get_object(cur, "as");
1056-
if(exposed_as != null) {
1057-
PyType exposed_as_type = fromClass(exposed_as);
1058-
class_to_type.put(c, exposed_as_type);
1059-
return exposed_as_type;
1060-
}
1061-
cur = cur.getSuperclass();
1062-
}
1063-
}
1064-
exposed_methods = (String[])exposed_decl_get_object(c, "methods");
1065-
if(exposed_methods == null)
1066-
exposed_methods = new String[0];
10671051
}
10681052
}
10691053
PyType newtype = class_to_type.get(c);
10701054
if (newtype == null) {
10711055
newtype = c == PyType.class ? new PyType(true) : new PyType();
10721056
class_to_type.put(c, newtype);
1073-
fillFromClass(newtype, name, c, base, newstyle, setup, exposed_methods, tb);
1057+
fillFromClass(newtype, name, c, base, newstyle, setup, tb);
10741058
}
10751059
return newtype;
10761060
}
@@ -1090,11 +1074,6 @@ private static PyType addFromClass(Class c) {
10901074
* Class exposed_base
10911075
* String exposed_name
10921076
*
1093-
* Class exposed_as => instances are exposed as implementing
1094-
* just this superclass
1095-
*
1096-
* (String[] exposed_methods)
1097-
*
10981077
*/
10991078

11001079
public static synchronized PyType fromClass(Class c) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.python.expose;
2+
3+
/**
4+
* Marker interface that indicates this class and all of its subclasses should
5+
* be exposed to Python with the same Python type as its superclass.
6+
*/
7+
public interface ExposeAsSuperclass {}

0 commit comments

Comments
 (0)