[pypy-commit] pypy default: implement PyEval_GetFrame()

arigo pypy.commits at gmail.com
Mon Oct 8 14:05:28 EDT 2018


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r95191:1d9271dd9a3d
Date: 2018-10-08 19:58 +0200
http://bitbucket.org/pypy/pypy/changeset/1d9271dd9a3d/

Log:	implement PyEval_GetFrame()

diff --git a/pypy/module/cpyext/eval.py b/pypy/module/cpyext/eval.py
--- a/pypy/module/cpyext/eval.py
+++ b/pypy/module/cpyext/eval.py
@@ -9,6 +9,7 @@
 from pypy.module.cpyext.pyobject import PyObject
 from pypy.module.cpyext.pyerrors import PyErr_SetFromErrno
 from pypy.module.cpyext.funcobject import PyCodeObject
+from pypy.module.cpyext.frameobject import PyFrameObject
 from pypy.module.__builtin__ import compiling
 
 PyCompilerFlags = cpython_struct(
@@ -58,6 +59,11 @@
         return None
     return caller.get_w_globals()    # borrowed ref
 
+ at cpython_api([], PyFrameObject, error=CANNOT_FAIL, result_borrowed=True)
+def PyEval_GetFrame(space):
+    caller = space.getexecutioncontext().gettopframe_nohidden()
+    return caller    # borrowed ref, may be null
+
 @cpython_api([PyCodeObject, PyObject, PyObject], PyObject)
 def PyEval_EvalCode(space, w_code, w_globals, w_locals):
     """This is a simplified interface to PyEval_EvalCodeEx(), with just
diff --git a/pypy/module/cpyext/test/test_eval.py b/pypy/module/cpyext/test/test_eval.py
--- a/pypy/module/cpyext/test/test_eval.py
+++ b/pypy/module/cpyext/test/test_eval.py
@@ -420,3 +420,15 @@
             except StopIteration:
                 pass
             assert out == [0, 1, 2, 3, 4]
+
+    def test_getframe(self):
+        import sys
+        module = self.import_extension('foo', [
+            ("getframe1", "METH_NOARGS",
+             """
+                PyFrameObject *x = PyEval_GetFrame();
+                Py_INCREF(x);
+                return (PyObject *)x;
+             """),], prologue="#include <frameobject.h>\n")
+        res = module.getframe1()
+        assert res is sys._getframe(0)


More information about the pypy-commit mailing list