-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathWeakrefModule.java
More file actions
62 lines (53 loc) · 2.03 KB
/
WeakrefModule.java
File metadata and controls
62 lines (53 loc) · 2.03 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
/* Copyright (c) Jython Developers */
package org.python.modules._weakref;
import org.python.core.ClassDictInit;
import org.python.core.Py;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyString;
/**
* The _weakref module.
*/
public class WeakrefModule implements ClassDictInit {
public static final PyString __doc__ = new PyString("Weak-reference support module.");
public static void classDictInit(PyObject dict)
{
dict.__setitem__("__doc__", __doc__);
dict.__setitem__("__name__", Py.newString("_weakref"));
dict.__setitem__("ref", ReferenceType.TYPE);
dict.__setitem__("ReferenceType", ReferenceType.TYPE);
dict.__setitem__("ProxyType", ProxyType.TYPE);
dict.__setitem__("CallableProxyType", CallableProxyType.TYPE);
// __doc__, functions: getweakrefcount, getweakrefs, proxy
dict.__setitem__("classDictInit", null);
}
public static ProxyType proxy(PyObject object) {
ReferenceBackend gref = GlobalRef.newInstance(object);
boolean callable = object.isCallable();
ProxyType ret = (ProxyType)gref.find(callable ? CallableProxyType.class : ProxyType.class);
if (ret != null) {
return ret;
}
if (callable) {
return new CallableProxyType(GlobalRef.newInstance(object), null);
} else {
return new ProxyType(GlobalRef.newInstance(object), null);
}
}
public static ProxyType proxy(PyObject object, PyObject callback) {
if (callback == Py.None) {
return proxy(object);
}
if (object.isCallable()) {
return new CallableProxyType(GlobalRef.newInstance(object), callback);
} else {
return new ProxyType(GlobalRef.newInstance(object), callback);
}
}
public static int getweakrefcount(PyObject object) {
return GlobalRef.getCount(object);
}
public static PyList getweakrefs(PyObject object) {
return GlobalRef.getRefs(object);
}
}