[pypy-svn] pypy default: (moguri) Implement PyRun_SimpleString in cpyext

amauryfa commits-noreply at bitbucket.org
Mon Mar 21 08:42:08 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r42809:b418623246c9
Date: 2011-03-21 08:40 +0100
http://bitbucket.org/pypy/pypy/changeset/b418623246c9/

Log:	(moguri) Implement PyRun_SimpleString in cpyext

diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -3072,12 +3072,6 @@
     "???" as the filename."""
     raise NotImplementedError
 
- at cpython_api([rffi.CCHARP], rffi.INT_real, error=-1)
-def PyRun_SimpleString(space, command):
-    """This is a simplified interface to PyRun_SimpleStringFlags() below,
-    leaving the PyCompilerFlags* argument set to NULL."""
-    raise NotImplementedError
-
 @cpython_api([rffi.CCHARP, PyCompilerFlags], rffi.INT_real, error=-1)
 def PyRun_SimpleStringFlags(space, command, flags):
     """Executes the Python source code from command in the __main__ module

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
@@ -84,6 +84,15 @@
     w_code = compiling.compile(space, w_source, filename, mode)
     return compiling.eval(space, w_code, w_globals, w_locals)
 
+ at cpython_api([CONST_STRING], rffi.INT_real, error=-1)
+def PyRun_SimpleString(space, command):
+    """This is a simplified interface to PyRun_SimpleStringFlags() below,
+    leaving the PyCompilerFlags* argument set to NULL."""
+    command = rffi.charp2str(command)
+    run_string(space, command, "<string>", Py_file_input,
+               space.w_None, space.w_None)
+    return 0
+
 @cpython_api([CONST_STRING, rffi.INT_real,PyObject, PyObject], PyObject)
 def PyRun_String(space, source, start, w_globals, w_locals):
     """This is a simplified interface to PyRun_StringFlags() below, leaving

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
@@ -63,6 +63,21 @@
 
         assert space.int_w(w_res) == 10
 
+    def test_run_simple_string(self, space, api):
+        def run(code):
+            buf = rffi.str2charp(code)
+            try:
+                return api.PyRun_SimpleString(buf)
+            finally:
+                rffi.free_charp(buf)
+
+        assert 0 == run("42 * 43")
+        
+        assert -1 == run("4..3 * 43")
+        
+        assert api.PyErr_Occurred()
+        api.PyErr_Clear()
+        
     def test_run_string(self, space, api):
         def run(code, start, w_globals, w_locals):
             buf = rffi.str2charp(code)


More information about the Pypy-commit mailing list