Skip to content

Commit cbf46ef

Browse files
committed
Add ContextManager __enter__/__exit__ methods to PyLock
1 parent 0282cfc commit cbf46ef

File tree

3 files changed

+183
-73
lines changed

3 files changed

+183
-73
lines changed

.idea/misc.xml

Lines changed: 0 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 169 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/org/python/modules/thread/PyLock.java

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

4-
import org.python.core.Py;
5-
import org.python.core.PyObject;
4+
import org.python.core.*;
65

7-
public class PyLock extends PyObject {
6+
public class PyLock extends PyObject implements ContextManager {
87

98
private boolean locked = false;
109

@@ -45,4 +44,16 @@ public synchronized void release() {
4544
public boolean locked() {
4645
return locked;
4746
}
47+
48+
@Override
49+
public PyObject __enter__(ThreadState ts) {
50+
acquire();
51+
return this;
52+
}
53+
54+
@Override
55+
public boolean __exit__(ThreadState ts, PyException exception) {
56+
release();
57+
return false;
58+
}
4859
}

0 commit comments

Comments
 (0)