[pypy-svn] r72553 - pypy/trunk/pypy/module/cpyext/test

afa at codespeak.net afa at codespeak.net
Mon Mar 22 14:43:38 CET 2010


Author: afa
Date: Mon Mar 22 14:43:36 2010
New Revision: 72553

Modified:
   pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
   pypy/trunk/pypy/module/cpyext/test/test_stringobject.py
Log:
Slightly better interface for building extension,
it focuses on the function content, not the declarations.


Modified: pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_cpyext.py	Mon Mar 22 14:43:36 2010
@@ -79,8 +79,34 @@
             self.space.sys.get('modules'),
             self.space.wrap(name))
 
+    def import_extension(self, modname, functions):
+
+        methods_table = []
+        codes = []
+        for funcname, flags, code in functions:
+            cfuncname = "%s_%s" % (modname, funcname)
+            methods_table.append("{\"%s\", %s, %s}," %
+                                 (funcname, cfuncname, flags))
+            func_code = """
+            static PyObject* %s(PyObject* self, PyObject* args)
+            {
+            %s
+            }
+            """ % (cfuncname, code)
+            codes.append(func_code)
+
+        body = "\n".join(codes) + """
+        static PyMethodDef methods[] = {
+        %s
+        { NULL }
+        };
+        """ % ('\n'.join(methods_table),)
+        init = """Py_InitModule("%s", methods);""" % (modname,)
+        return self.import_module(name=modname, init=init, body=body)
+
     def setup_method(self, func):
         self.w_import_module = self.space.wrap(self.import_module)
+        self.w_import_extension = self.space.wrap(self.import_extension)
         self.w_check_refcnts = self.space.wrap(self.check_refcnts)
         #self.check_refcnts("Object has refcnt != 1: %r -- Not executing test!")
         #self.space.fromcache(State).print_refcounts()

Modified: pypy/trunk/pypy/module/cpyext/test/test_stringobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_stringobject.py	(original)
+++ pypy/trunk/pypy/module/cpyext/test/test_stringobject.py	Mon Mar 22 14:43:36 2010
@@ -5,49 +5,36 @@
 
 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 PyObject* foo_test_Size(PyObject* self, PyObject *args)
-        {
-            PyObject* s = PyString_FromString("Hello world");
-            int result = 0;
-            
-            if(PyString_Size(s) == 11) {
-                result = 1;
-            }
-            Py_DECREF(s);
-            return PyBool_FromLong(result);
-        }
-        static PyObject* foo_test_Size_exception(PyObject* self, PyObject *args)
-        {
-            PyObject* f = PyFloat_FromDouble(1.0);
-            Py_ssize_t size = PyString_Size(f);
-            
-            Py_DECREF(f);
-            return NULL;
-        }
-        static PyMethodDef methods[] = {
-            { "get_hello1", foo_get_hello1, METH_NOARGS },
-            { "get_hello2", foo_get_hello2, METH_NOARGS },
-            { "test_Size", foo_test_Size, METH_NOARGS },
-            { "test_Size_exception", foo_test_Size_exception, METH_NOARGS },
-            { NULL }
-        };
-        """
-        module = self.import_module(name='foo', init=init, body=body)
-        assert 'foo' in sys.modules
+        module = self.import_extension('foo', [
+            ("get_hello1", "METH_NOARGS",
+             """
+                 return PyString_FromStringAndSize(
+                     "Hello world<should not be included>", 11);
+             """),
+            ("get_hello2", "METH_NOARGS",
+             """
+                 return PyString_FromString("Hello world");
+             """),
+            ("test_Size", "METH_NOARGS",
+             """
+                 PyObject* s = PyString_FromString("Hello world");
+                 int result = 0;
+
+                 if(PyString_Size(s) == 11) {
+                     result = 1;
+                 }
+                 Py_DECREF(s);
+                 return PyBool_FromLong(result);
+             """),
+            ("test_Size_exception", "METH_NOARGS",
+             """
+                 PyObject* f = PyFloat_FromDouble(1.0);
+                 Py_ssize_t size = PyString_Size(f);
+
+                 Py_DECREF(f);
+                 return NULL;
+             """),
+            ])
         assert module.get_hello1() == 'Hello world'
         assert module.get_hello2() == 'Hello world'
         assert module.test_Size()



More information about the Pypy-commit mailing list