-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyLocal.java
More file actions
143 lines (124 loc) · 4.03 KB
/
PyLocal.java
File metadata and controls
143 lines (124 loc) · 4.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
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
/* Copyright (c) Jython Developers */
package org.python.modules.thread;
import org.python.core.Py;
import org.python.core.PyDictionary;
import org.python.core.PyNewWrapper;
import org.python.core.PyObject;
import org.python.core.PyType;
import org.python.core.Traverseproc;
import org.python.core.Visitproc;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedSet;
import org.python.expose.ExposedType;
@ExposedType(name = "thread._local")
public class PyLocal extends PyObject implements Traverseproc {
public static final PyType TYPE = PyType.fromClass(PyLocal.class);
private ThreadLocal<Object[]> tdict = new ThreadLocal() {
@Override
protected Object initialValue() {
return new Object[1];
}
};
private PyObject args[];
private String keywords[];
public PyLocal() {
this(TYPE);
}
public PyLocal(PyType subType) {
super(subType);
// Don't lazy load the underlying dict in the instantiating thread; that would
// call __init__ a the second time
tdict.set(new Object[] { new PyDictionary() });
}
@ExposedNew
final static PyObject _local___new__(PyNewWrapper new_,
boolean init,
PyType subtype,
PyObject[] args,
String[] keywords) {
PyObject[] where = new PyObject[1];
subtype.lookup_where("__init__", where);
if (where[0] == PyObject.TYPE && args.length > 0) {
throw Py.TypeError("Initialization arguments are not supported");
}
PyLocal newobj;
if (new_.getWrappedType() == subtype) {
newobj = new PyLocal();
} else {
newobj = new PyLocalDerived(subtype);
}
newobj.args = args;
newobj.keywords = keywords;
return newobj;
}
@Override
@ExposedGet(name = "__dict__")
public PyObject getDict() {
return fastGetDict();
}
@Override
@ExposedSet(name = "__dict__")
public void setDict(PyObject dict) {
super.setDict(dict);
}
@Override
public PyObject fastGetDict() {
Object[] local = tdict.get();
PyDictionary ldict = (PyDictionary)(local[0]);
if (ldict == null) {
ldict = new PyDictionary();
local[0] = ldict;
dispatch__init__(args, keywords);
}
return ldict;
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retVal;
if (args != null) {
for (PyObject ob: args) {
if (ob != null) {
retVal = visit.visit(ob, arg);
if (retVal != 0) {
return retVal;
}
}
}
}
Object[] ob0 = tdict.get();
if (ob0 != null) {
for (Object obj: ob0) {
if (obj != null) {
if (obj instanceof PyObject) {
retVal = visit.visit((PyObject) obj, arg);
if (retVal != 0) {
return retVal;
}
} else if (obj instanceof Traverseproc) {
retVal = ((Traverseproc) obj).traverse(visit, arg);
if (retVal != 0) {
return retVal;
}
}
}
}
}
return 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) throws UnsupportedOperationException {
if (ob == null) {
return false;
}
if (args != null) {
for (PyObject obj: args) {
if (obj == ob) {
return true;
}
}
}
throw new UnsupportedOperationException();
}
}