-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPyCell.java
More file actions
65 lines (55 loc) · 1.77 KB
/
Copy pathPyCell.java
File metadata and controls
65 lines (55 loc) · 1.77 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
/* Copyright (c) Jython Developers */
package org.python.core;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedType;
/**
* The Python cell type.
*
* Cells are used to implement variables referenced by multiple scopes.
*/
@ExposedType(name = "cell", isBaseType = false)
public class PyCell extends PyObject implements Traverseproc {
public static final PyType TYPE = PyType.fromClass(PyCell.class);
/** The underlying content of the cell, or null. */
public PyObject ob_ref;
public PyCell() {
super(TYPE);
}
@ExposedGet(name = "cell_contents")
public PyObject getCellContents() {
if (ob_ref == null) {
throw Py.ValueError("Cell is empty");
}
return ob_ref;
}
@Override
public String toString() {
if (ob_ref == null) {
return String.format("<cell at %s: empty>", Py.idstr(this));
}
return String.format("<cell at %s: %.80s object at %s>", Py.idstr(this),
ob_ref.getType().getName(), Py.idstr(ob_ref));
}
@Override
public PyObject richCompare(PyObject other, CompareOp op) {
if (!(other instanceof PyCell)) {
return Py.NotImplemented;
}
PyObject a = ob_ref;
PyObject b = ((PyCell) other).ob_ref;
if (a != null && b != null) {
return a.richCompare(b, op);
}
int result = (a == null ? 0 : 1) - (b == null ? 0 : 1);
return op.bool(result);
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
return ob_ref != null ? visit.visit(ob_ref, arg) : 0;
}
@Override
public boolean refersDirectlyTo(PyObject ob) {
return ob != null && ob_ref == ob;
}
}