home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / py2s152.zip / Objects / cobject.c < prev    next >
C/C++ Source or Header  |  1999-06-27  |  5KB  |  191 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Wrap void* pointers to be passed between C modules */
  33.  
  34. #include "Python.h"
  35.  
  36.  
  37. /* Declarations for objects of type PyCObject */
  38.  
  39. typedef void (*destructor1) Py_PROTO((void *));
  40. typedef void (*destructor2) Py_PROTO((void *, void*));
  41.  
  42. typedef struct {
  43.     PyObject_HEAD
  44.     void *cobject;
  45.         void *desc;
  46.     void (*destructor) Py_PROTO((void *));
  47. } PyCObject;
  48.  
  49. PyObject *
  50. PyCObject_FromVoidPtr(cobj, destr)
  51.     void *cobj;
  52.     void (*destr) Py_PROTO((void *));
  53. {
  54.     PyCObject *self;
  55.     
  56.     self = PyObject_NEW(PyCObject, &PyCObject_Type);
  57.     if (self == NULL)
  58.         return NULL;
  59.     self->cobject=cobj;
  60.     self->destructor=destr;
  61.     self->desc=NULL;
  62.     return (PyObject *)self;
  63. }
  64.  
  65. PyObject *
  66. PyCObject_FromVoidPtrAndDesc(cobj, desc, destr)
  67.     void *cobj;
  68.     void *desc;
  69.     void (*destr) Py_PROTO((void *, void *));
  70. {
  71.     PyCObject *self;
  72.  
  73.     if(!desc) {
  74.             PyErr_SetString(PyExc_TypeError,
  75.           "PyCObject_FromVoidPtrAndDesc called with null description");
  76.         return NULL;
  77.     }
  78.     
  79.     self = PyObject_NEW(PyCObject, &PyCObject_Type);
  80.     if (self == NULL)
  81.         return NULL;
  82.     self->cobject=cobj;
  83.     self->destructor=(destructor1)destr;
  84.     self->desc=desc;
  85.     return (PyObject *)self;
  86. }
  87.  
  88. void *
  89. PyCObject_AsVoidPtr(self)
  90.     PyObject *self;
  91. {
  92.         if(self)
  93.       {
  94.         if(self->ob_type == &PyCObject_Type)
  95.           return ((PyCObject *)self)->cobject;
  96.         PyErr_SetString(PyExc_TypeError,
  97.                 "PyCObject_AsVoidPtr with non-C-object");
  98.       }
  99.     if(! PyErr_Occurred())
  100.         PyErr_SetString(PyExc_TypeError,
  101.                 "PyCObject_AsVoidPtr called with null pointer");
  102.     return NULL;
  103. }
  104.  
  105. void *
  106. PyCObject_GetDesc(self)
  107.     PyObject *self;
  108. {
  109.         if(self)
  110.       {
  111.         if(self->ob_type == &PyCObject_Type)
  112.           return ((PyCObject *)self)->desc;
  113.         PyErr_SetString(PyExc_TypeError,
  114.                 "PyCObject_GetDesc with non-C-object");
  115.       }
  116.     if(! PyErr_Occurred())
  117.         PyErr_SetString(PyExc_TypeError,
  118.                 "PyCObject_GetDesc called with null pointer");
  119.     return NULL;
  120. }
  121.  
  122. void *
  123. PyCObject_Import(module_name, name)
  124.      char *module_name;
  125.      char *name;
  126. {
  127.   PyObject *m, *c;
  128.   void *r=NULL;
  129.   
  130.   if((m=PyImport_ImportModule(module_name)))
  131.     {
  132.       if((c=PyObject_GetAttrString(m,name)))
  133.     {
  134.       r=PyCObject_AsVoidPtr(c);
  135.       Py_DECREF(c);
  136.     }
  137.       Py_DECREF(m);
  138.     }
  139.  
  140.   return r;
  141. }
  142.  
  143. static void
  144. PyCObject_dealloc(self)
  145.     PyCObject *self;
  146. {
  147.         if(self->destructor)
  148.       {
  149.         if(self->desc)
  150.               ((destructor2)(self->destructor))(self->cobject, self->desc);
  151.         else
  152.               (self->destructor)(self->cobject);
  153.       }
  154.     PyMem_DEL(self);
  155. }
  156.  
  157.  
  158. static char PyCObject_Type__doc__[] = 
  159. "C objects to be exported from one extension module to another\n\
  160. \n\
  161. C objects are used for communication between extension modules.  They\n\
  162. provide a way for an extension module to export a C interface to other\n\
  163. extension modules, so that extension modules can use the Python import\n\
  164. mechanism to link to one another.\n"
  165. ;
  166.  
  167. PyTypeObject PyCObject_Type = {
  168.     PyObject_HEAD_INIT(&PyType_Type)
  169.     0,                /*ob_size*/
  170.     "PyCObject",            /*tp_name*/
  171.     sizeof(PyCObject),        /*tp_basicsize*/
  172.     0,                /*tp_itemsize*/
  173.     /* methods */
  174.     (destructor)PyCObject_dealloc,    /*tp_dealloc*/
  175.     (printfunc)0,        /*tp_print*/
  176.     (getattrfunc)0,    /*tp_getattr*/
  177.     (setattrfunc)0,    /*tp_setattr*/
  178.     (cmpfunc)0,        /*tp_compare*/
  179.     (reprfunc)0,        /*tp_repr*/
  180.     0,            /*tp_as_number*/
  181.     0,        /*tp_as_sequence*/
  182.     0,        /*tp_as_mapping*/
  183.     (hashfunc)0,        /*tp_hash*/
  184.     (ternaryfunc)0,        /*tp_call*/
  185.     (reprfunc)0,        /*tp_str*/
  186.  
  187.     /* Space for future expansion */
  188.     0L,0L,0L,0L,
  189.     PyCObject_Type__doc__ /* Documentation string */
  190. };
  191.