Skip to content

Commit c83b633

Browse files
committed
Implement PyThreadState_GetFrame
1 parent 1c2b305 commit c83b633

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_pystate.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
39+
import sys
3940

40-
from . import CPyExtTestCase, CPyExtFunction
41+
from . import CPyExtTestCase, CPyExtFunction, CPyExtType
4142

4243
__dir__ = __file__.rpartition("/")[0]
4344

4445

4546
class TestPystate(CPyExtTestCase):
46-
4747
test_PyThreadState_GetDict = CPyExtFunction(
4848
lambda args: type({}),
4949
lambda: (
@@ -58,3 +58,15 @@ class TestPystate(CPyExtTestCase):
5858
arguments=["PyObject* ignored"],
5959
callfunction="wrap_PyThreadState_GetDict",
6060
)
61+
62+
def test_PyThreadState_GetFrame(self):
63+
Tester = CPyExtType(
64+
"PyThreadState_GetFrameTester",
65+
code="""
66+
static PyObject* get_frame(PyObject* unused) {
67+
return (PyObject*)PyThreadState_GetFrame(PyThreadState_GET());
68+
}
69+
""",
70+
tp_methods='{"get_frame", (PyCFunction)get_frame, METH_NOARGS | METH_STATIC, NULL}',
71+
)
72+
assert Tester.get_frame() is sys._getframe(0)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextPyStateBuiltins.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static com.oracle.graal.python.builtins.modules.cext.PythonCextBuiltins.CApiCallPath.Ignored;
4545
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Int;
4646
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Pointer;
47+
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PyFrameObjectTransfer;
4748
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PyObjectBorrowed;
4849
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.PyThreadState;
4950
import static com.oracle.graal.python.builtins.objects.cext.capi.transitions.ArgDescriptor.Py_ssize_t;
@@ -55,18 +56,23 @@
5556
import com.oracle.graal.python.builtins.objects.PNone;
5657
import com.oracle.graal.python.builtins.objects.cext.capi.PThreadState;
5758
import com.oracle.graal.python.builtins.objects.dict.PDict;
59+
import com.oracle.graal.python.builtins.objects.frame.PFrame;
5860
import com.oracle.graal.python.builtins.objects.ints.PInt;
61+
import com.oracle.graal.python.nodes.frame.GetCurrentFrameRef;
62+
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
5963
import com.oracle.graal.python.nodes.util.CannotCastException;
6064
import com.oracle.graal.python.runtime.GilNode;
6165
import com.oracle.graal.python.runtime.PythonContext;
6266
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
6367
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6468
import com.oracle.graal.python.util.OverflowException;
6569
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
70+
import com.oracle.truffle.api.dsl.Bind;
6671
import com.oracle.truffle.api.dsl.Cached;
6772
import com.oracle.truffle.api.dsl.Specialization;
6873
import com.oracle.truffle.api.interop.InteropLibrary;
6974
import com.oracle.truffle.api.library.CachedLibrary;
75+
import com.oracle.truffle.api.nodes.Node;
7076

7177
public final class PythonCextPyStateBuiltins {
7278

@@ -131,6 +137,18 @@ PDict get(@Cached PythonObjectFactory factory) {
131137
}
132138
}
133139

140+
@CApiBuiltin(ret = PyFrameObjectTransfer, args = {PyThreadState}, call = Direct)
141+
abstract static class PyThreadState_GetFrame extends CApiUnaryBuiltinNode {
142+
@Specialization
143+
PFrame get(
144+
@Bind("this") Node inliningTarget,
145+
@Cached GetCurrentFrameRef getCurrentFrameRef,
146+
@Cached ReadCallerFrameNode readCallerFrameNode) {
147+
PFrame.Reference frameRef = getCurrentFrameRef.execute(null, inliningTarget);
148+
return readCallerFrameNode.executeWith(frameRef, 0);
149+
}
150+
}
151+
134152
@CApiBuiltin(ret = PyObjectBorrowed, args = {Py_ssize_t}, call = Ignored)
135153
abstract static class PyTruffleState_FindModule extends CApiUnaryBuiltinNode {
136154

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CApiFunction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,6 @@ public final class CApiFunction {
865865
@CApiBuiltin(name = "PySys_WriteStdout", ret = Void, args = {ConstCharPtrAsTruffleString, VARARGS}, call = NotImplemented)
866866
@CApiBuiltin(name = "PyThreadState_Delete", ret = Void, args = {PyThreadState}, call = NotImplemented)
867867
@CApiBuiltin(name = "PyThreadState_EnterTracing", ret = Void, args = {PyThreadState}, call = NotImplemented)
868-
@CApiBuiltin(name = "PyThreadState_GetFrame", ret = PyFrameObjectTransfer, args = {PyThreadState}, call = NotImplemented)
869868
@CApiBuiltin(name = "PyThreadState_GetID", ret = UINT64_T, args = {PyThreadState}, call = NotImplemented)
870869
@CApiBuiltin(name = "PyThreadState_GetInterpreter", ret = PyInterpreterState, args = {PyThreadState}, call = NotImplemented)
871870
@CApiBuiltin(name = "PyThreadState_LeaveTracing", ret = Void, args = {PyThreadState}, call = NotImplemented)

0 commit comments

Comments
 (0)