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

jandem at codespeak.net jandem at codespeak.net
Mon Mar 22 11:10:44 CET 2010


Author: jandem
Date: Mon Mar 22 11:10:42 2010
New Revision: 72525

Added:
   pypy/trunk/pypy/module/cpyext/test/test_stringobject.py
Modified:
   pypy/trunk/pypy/module/cpyext/__init__.py
   pypy/trunk/pypy/module/cpyext/include/stringobject.h
   pypy/trunk/pypy/module/cpyext/stringobject.py
Log:
Add PyString_FromString and string tests


Modified: pypy/trunk/pypy/module/cpyext/__init__.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/__init__.py	(original)
+++ pypy/trunk/pypy/module/cpyext/__init__.py	Mon Mar 22 11:10:42 2010
@@ -34,5 +34,6 @@
 import pypy.module.cpyext.pyerrors
 import pypy.module.cpyext.typeobject
 import pypy.module.cpyext.object
+import pypy.module.cpyext.stringobject
 import pypy.module.cpyext.tupleobject
 import pypy.module.cpyext.dictobject

Modified: pypy/trunk/pypy/module/cpyext/include/stringobject.h
==============================================================================
--- pypy/trunk/pypy/module/cpyext/include/stringobject.h	(original)
+++ pypy/trunk/pypy/module/cpyext/include/stringobject.h	Mon Mar 22 11:10:42 2010
@@ -8,6 +8,7 @@
 #endif
 
 PyObject * PyString_FromStringAndSize(const char *, Py_ssize_t);
+PyObject * PyString_FromString(const char *);
 
 #ifdef __cplusplus
 }

Modified: pypy/trunk/pypy/module/cpyext/stringobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/stringobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/stringobject.py	Mon Mar 22 11:10:42 2010
@@ -2,12 +2,16 @@
 from pypy.module.cpyext.api import cpython_api, PyObject, make_ref, Py_ssize_t
 
 @cpython_api([rffi.CCHARP, Py_ssize_t], PyObject)
-def PyString_FromStringAndSize(char_p, length):
+def PyString_FromStringAndSize(space, char_p, length):
     l = []
     i = 0
     while length > 0:
-        l.append(cp[i])
+        l.append(char_p[i])
         i += 1
         length -= 1
-    return "".join(l)
+    return space.wrap("".join(l))
 
+ at cpython_api([rffi.CCHARP], PyObject)
+def PyString_FromString(space, char_p):
+    s = rffi.charp2str(char_p)
+    return space.wrap(s)

Added: pypy/trunk/pypy/module/cpyext/test/test_stringobject.py
==============================================================================
--- (empty file)
+++ pypy/trunk/pypy/module/cpyext/test/test_stringobject.py	Mon Mar 22 11:10:42 2010
@@ -0,0 +1,31 @@
+from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
+
+import py
+import sys
+
+class AppTestStringObject(AppTestCpythonExtensionBase):
+    def test_stringobject(self):
+        import sys
+        init = """
+        if (Py_IsInitialized())
+            Py_InitModule("foo", methods);
+        """
+        body = """
+        static PyObject* foo_get_hello1(PyObject* self, PyObject *args)
+        {
+            return PyString_FromStringAndSize("Hello world<should not be included>", 11);
+        }
+        static PyObject* foo_get_hello2(PyObject* self, PyObject *args)
+        {
+            return PyString_FromString("Hello world");
+        }
+        static PyMethodDef methods[] = {
+            { "get_hello1", foo_get_hello1, METH_NOARGS },
+            { "get_hello2", foo_get_hello2, METH_NOARGS },
+            { NULL }
+        };
+        """
+        module = self.import_module(name='foo', init=init, body=body)
+        assert 'foo' in sys.modules
+        assert module.get_hello1() == 'Hello world'
+        assert module.get_hello2() == 'Hello world'



More information about the Pypy-commit mailing list