[pypy-svn] r74176 - in pypy/branch/cpython-extension/pypy/module/cpyext: . test

afa at codespeak.net afa at codespeak.net
Wed Apr 28 15:39:37 CEST 2010


Author: afa
Date: Wed Apr 28 15:39:35 2010
New Revision: 74176

Modified:
   pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py
   pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
   pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py
Log:
PyCObject_Import


Modified: pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/pycobject.py	Wed Apr 28 15:39:35 2010
@@ -2,8 +2,10 @@
 from pypy.interpreter.typedef import TypeDef
 from pypy.rpython.lltypesystem import rffi, lltype
 from pypy.module.cpyext.pyobject import make_ref, make_typedescr
+from pypy.module.cpyext.import_ import PyImport_ImportModule
+from pypy.module.cpyext.object import PyObject_GetAttrString
 from pypy.module.cpyext.api import generic_cpy_call, cpython_api, bootstrap_function, \
-     PyObject, cpython_struct, PyObjectFields, build_type_checkers
+     PyObject, cpython_struct, PyObjectFields, build_type_checkers, CONST_STRING
 
 
 destructor_short = lltype.Ptr(lltype.FuncType([rffi.VOIDP_real], lltype.Void))
@@ -83,3 +85,12 @@
     created with."""
     assert isinstance(w_obj, W_PyCObjectFromVoidPtr)
     return w_obj.pyo.c_cobject
+
+ at cpython_api([CONST_STRING, CONST_STRING], rffi.VOIDP_real,
+             error=lltype.nullptr(rffi.VOIDP_real.TO))
+def PyCObject_Import(space, module_name, name):
+    """Return the object void * that the PyCObject self was
+    created with."""
+    w_mod = PyImport_ImportModule(space, module_name)
+    w_obj = PyObject_GetAttrString(space, w_mod, name)
+    return PyCObject_AsVoidPtr(space, w_obj)

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/stubs.py	Wed Apr 28 15:39:35 2010
@@ -1464,21 +1464,6 @@
     only be called from the main thread."""
     raise NotImplementedError
 
- at cpython_api([rffi.CCHARP, PyObject, PyObject], PyObject)
-def PyErr_NewException(space, name, base, dict):
-    """This utility function creates and returns a new exception object. The name
-    argument must be the name of the new exception, a C string of the form
-    module.class.  The base and dict arguments are normally NULL.  This
-    creates a class object derived from Exception (accessible in C as
-    PyExc_Exception).
-    
-    The __module__ attribute of the new class is set to the first part (up
-    to the last dot) of the name argument, and the class name is set to the last
-    part (after the last dot).  The base argument can be used to specify alternate
-    base classes; it can either be only one class or a tuple of classes. The dict
-    argument can be used to specify a dictionary of class variables and methods."""
-    raise NotImplementedError
-
 @cpython_api([rffi.CCHARP, rffi.CCHARP, PyObject, PyObject], PyObject)
 def PyErr_NewExceptionWithDoc(space, name, doc, base, dict):
     """Same as PyErr_NewException(), except that the new exception class can

Modified: pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py
==============================================================================
--- pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py	(original)
+++ pypy/branch/cpython-extension/pypy/module/cpyext/test/test_pycobject.py	Wed Apr 28 15:39:35 2010
@@ -16,3 +16,17 @@
         obj = api.PyCObject_FromVoidPtrAndDesc(ptr, ptr,
                                                lltype.nullptr(destructor_short.TO))
         api.Py_DecRef(obj)
+
+    def test_pycobject_import(self, space, api):
+        ptr = rffi.cast(rffi.VOIDP_real, 1234)
+        obj = api.PyCObject_FromVoidPtr(ptr, lltype.nullptr(destructor_short.TO))
+        space.setattr(space.sys, space.wrap("_cpyext_cobject"), obj)
+
+        charp1 = rffi.str2charp("sys")
+        charp2 = rffi.str2charp("_cpyext_cobject")
+        assert api.PyCObject_Import(charp1, charp2) == ptr
+        rffi.free_charp(charp1)
+        rffi.free_charp(charp2)
+
+        api.Py_DecRef(obj)
+        space.delattr(space.sys, space.wrap("_cpyext_cobject"))



More information about the Pypy-commit mailing list