[pypy-svn] r74635 - in pypy/trunk/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Fri May 21 17:01:16 CEST 2010


Author: afa
Date: Fri May 21 17:01:15 2010
New Revision: 74635

Modified:
   pypy/trunk/pypy/module/cpyext/eval.py
   pypy/trunk/pypy/module/cpyext/test/test_eval.py
Log:
Implement PyEval_GetGlobals(), PyEval_GetLocals()


Modified: pypy/trunk/pypy/module/cpyext/eval.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/eval.py	(original)
+++ pypy/trunk/pypy/module/cpyext/eval.py	Fri May 21 17:01:15 2010
@@ -22,6 +22,24 @@
         w_builtins = space.builtin.getdict()
     return borrow_from(None, w_builtins)
 
+ at cpython_api([], PyObject, error=CANNOT_FAIL)
+def PyEval_GetLocals(space):
+    """Return a dictionary of the local variables in the current execution
+    frame, or NULL if no frame is currently executing."""
+    caller = space.getexecutioncontext().gettopframe_nohidden()
+    if caller is None:
+        return None
+    return borrow_from(None, caller.getdictscope())
+
+ at cpython_api([], PyObject, error=CANNOT_FAIL)
+def PyEval_GetGlobals(space):
+    """Return a dictionary of the global variables in the current execution
+    frame, or NULL if no frame is currently executing."""
+    caller = space.getexecutioncontext().gettopframe_nohidden()
+    if caller is None:
+        return None
+    return borrow_from(None, caller.w_globals)
+
 @cpython_api([PyObject, PyObject], PyObject)
 def PyObject_CallObject(space, w_obj, w_arg):
     """

Modified: pypy/trunk/pypy/module/cpyext/test/test_eval.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_eval.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_eval.py	Fri May 21 17:01:15 2010
@@ -2,6 +2,7 @@
 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 from pypy.module.cpyext.test.test_api import BaseApiTest
 from pypy.module.cpyext.eval import Py_single_input, Py_file_input, Py_eval_input
+from pypy.interpreter.gateway import interp2app
 
 class TestEval(BaseApiTest):
     def test_eval(self, space, api):
@@ -79,10 +80,8 @@
     def test_getbuiltins(self, space, api):
         assert api.PyEval_GetBuiltins() is space.builtin.w_dict
 
-        from pypy.interpreter.gateway import ObjSpace, interp2app
         def cpybuiltins(space):
             return api.PyEval_GetBuiltins()
-        cpybuiltins.unwrap_spec = [ObjSpace]
         w_cpybuiltins = space.wrap(interp2app(cpybuiltins))
 
         w_result = space.appexec([w_cpybuiltins], """(cpybuiltins):
@@ -96,6 +95,24 @@
         """)
         assert space.int_w(space.len(w_result)) == 1
 
+    def test_getglobals(self, space, api):
+        assert api.PyEval_GetLocals() is None
+        assert api.PyEval_GetGlobals() is None
+
+        def cpyvars(space):
+            return space.newtuple([api.PyEval_GetGlobals(),
+                                   api.PyEval_GetLocals()])
+        w_cpyvars = space.wrap(interp2app(cpyvars))
+
+        w_result = space.appexec([w_cpyvars], """(cpyvars):
+            x = 1
+            return cpyvars()
+        \ny = 2
+        """)
+        globals, locals = space.unwrap(w_result)
+        assert sorted(locals) == ['cpyvars', 'x']
+        assert sorted(globals) == ['__builtins__', 'anonymous', 'y']
+
 
 class AppTestCall(AppTestCpythonExtensionBase):
     def test_CallFunction(self):



More information about the Pypy-commit mailing list