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

afa at codespeak.net afa at codespeak.net
Fri May 21 12:02:37 CEST 2010


Author: afa
Date: Fri May 21 12:02:36 2010
New Revision: 74619

Modified:
   pypy/trunk/pypy/module/cpyext/object.py
   pypy/trunk/pypy/module/cpyext/stubsactive.py
   pypy/trunk/pypy/module/cpyext/test/test_object.py
Log:
implement PyObject_Print()


Modified: pypy/trunk/pypy/module/cpyext/object.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/object.py	(original)
+++ pypy/trunk/pypy/module/cpyext/object.py	Fri May 21 12:02:36 2010
@@ -368,3 +368,31 @@
     sizep[0] = size
     return 0
 
+FILEP = rffi.COpaquePtr('FILE')
+fwrite = rffi.llexternal('fwrite',
+                         [rffi.VOIDP, rffi.SIZE_T, rffi.SIZE_T, FILEP],
+                         rffi.SIZE_T)
+
+# Also in include/object.h
+Py_PRINT_RAW = 1 # No string quotes etc.
+
+ at cpython_api([PyObject, FILEP, rffi.INT_real], rffi.INT_real, error=-1)
+def PyObject_Print(space, w_obj, fp, flags):
+    """Print an object o, on file fp.  Returns -1 on error.  The flags argument
+    is used to enable certain printing options.  The only option currently
+    supported is Py_PRINT_RAW; if given, the str() of the object is written
+    instead of the repr()."""
+    if rffi.cast(lltype.Signed, flags) & Py_PRINT_RAW:
+        w_str = space.str(w_obj)
+    else:
+        w_str = space.repr(w_obj)
+
+    count = space.int_w(space.len(w_str))
+    data = space.str_w(w_str)
+    buf = rffi.get_nonmovingbuffer(data)
+    try:
+        fwrite(buf, 1, count, fp)
+    finally:
+        rffi.free_nonmovingbuffer(data, buf)
+    return 0
+

Modified: pypy/trunk/pypy/module/cpyext/stubsactive.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stubsactive.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stubsactive.py	Fri May 21 12:02:36 2010
@@ -15,14 +15,6 @@
     PyFile_DecUseCount() functions described below as appropriate."""
     raise NotImplementedError
 
- at cpython_api([PyObject, FILEP, rffi.INT_real], rffi.INT_real, error=-1)
-def PyObject_Print(space, o, fp, flags):
-    """Print an object o, on file fp.  Returns -1 on error.  The flags argument
-    is used to enable certain printing options.  The only option currently supported
-    is Py_PRINT_RAW; if given, the str() of the object is written
-    instead of the repr()."""
-    raise NotImplementedError
-
 @cpython_api([PyInterpreterState], PyThreadState, error=CANNOT_FAIL)
 def PyThreadState_New(space, interp):
     """Create a new thread state object belonging to the given interpreter object.

Modified: pypy/trunk/pypy/module/cpyext/test/test_object.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_object.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_object.py	Fri May 21 12:02:36 2010
@@ -1,9 +1,11 @@
 import py
 
 from pypy.module.cpyext.test.test_api import BaseApiTest
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import Py_LT, Py_LE, Py_NE, Py_EQ,\
     Py_GE, Py_GT
+from pypy.tool.udir import udir
 
 class TestObject(BaseApiTest):
     def test_IsTrue(self, space, api):
@@ -178,3 +180,28 @@
         assert space.unwrap(api.PyObject_Unicode(space.wrap("e"))) == u"e"
         assert api.PyObject_Unicode(space.wrap("\xe9")) is None
         api.PyErr_Clear()
+
+class AppTestObjectPrint(AppTestCpythonExtensionBase):
+    def setup_class(cls):
+        AppTestCpythonExtensionBase.setup_class.im_func(cls)
+        cls.w_tmpname = cls.space.wrap(str(py.test.ensuretemp("out", dir=0)))
+
+    def test_print(self):
+        module = self.import_extension('foo', [
+            ("dump", "METH_VARARGS",
+             """
+                 PyObject *fname = PyTuple_GetItem(args, 0);
+                 PyObject *obj = PyTuple_GetItem(args, 1);
+
+                 FILE *fp = fopen(PyString_AsString(fname), "wb");
+                 int ret;
+                 if (fp == NULL)
+                     Py_RETURN_NONE;
+                 ret = PyObject_Print(obj, fp, Py_PRINT_RAW);
+                 fclose(fp);
+                 if (ret < 0)
+                     return NULL;
+                 Py_RETURN_TRUE;
+             """)])
+        assert module.dump(self.tmpname, None)
+        assert open(self.tmpname).read() == 'None'



More information about the Pypy-commit mailing list