Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 1 | |
| 2 | /* C objects to be exported from one extension module to another. |
| 3 | |
| 4 | C objects are used for communication between extension modules. |
| 5 | They provide a way for an extension module to export a C interface |
| 6 | to other extension modules, so that extension modules can use the |
| 7 | Python import mechanism to link to one another. |
| 8 | |
Larry Hastings | 402b73f | 2010-03-25 00:54:54 +0000 | [diff] [blame] | 9 | DEPRECATED - Use PyCapsule objects instead. |
| 10 | CObject will be removed in 2.8 (if there is one). |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Fred Drake | ea9cb5a | 2000-07-09 00:20:36 +0000 | [diff] [blame] | 13 | #ifndef Py_COBJECT_H |
| 14 | #define Py_COBJECT_H |
| 15 | #ifdef __cplusplus |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 19 | PyAPI_DATA(PyTypeObject) PyCObject_Type; |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 20 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 21 | #define PyCObject_Check(op) (Py_TYPE(op) == &PyCObject_Type) |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 22 | |
| 23 | /* Create a PyCObject from a pointer to a C object and an optional |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 24 | destructor function. If the second argument is non-null, then it |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 25 | will be called with the first argument if and when the PyCObject is |
| 26 | destroyed. |
| 27 | |
| 28 | */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 29 | PyAPI_FUNC(PyObject *) PyCObject_FromVoidPtr( |
| 30 | void *cobj, void (*destruct)(void*)); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 32 | |
| 33 | /* Create a PyCObject from a pointer to a C object, a description object, |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 34 | and an optional destructor function. If the third argument is non-null, |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 35 | then it will be called with the first and second arguments if and when |
| 36 | the PyCObject is destroyed. |
| 37 | */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 38 | PyAPI_FUNC(PyObject *) PyCObject_FromVoidPtrAndDesc( |
| 39 | void *cobj, void *desc, void (*destruct)(void*,void*)); |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 41 | /* Retrieve a pointer to a C object from a PyCObject. */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 42 | PyAPI_FUNC(void *) PyCObject_AsVoidPtr(PyObject *); |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 44 | /* Retrieve a pointer to a description object from a PyCObject. */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 45 | PyAPI_FUNC(void *) PyCObject_GetDesc(PyObject *); |
Guido van Rossum | 1f84449 | 1997-10-21 19:48:35 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 47 | /* Import a pointer to a C object from a module using a PyCObject. */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 48 | PyAPI_FUNC(void *) PyCObject_Import(char *module_name, char *cobject_name); |
Guido van Rossum | e0e6962 | 1997-01-22 20:48:48 +0000 | [diff] [blame] | 49 | |
Martin v. Löwis | 01a74b2 | 2003-10-19 18:30:01 +0000 | [diff] [blame] | 50 | /* Modify a C object. Fails (==0) if object has a destructor. */ |
| 51 | PyAPI_FUNC(int) PyCObject_SetVoidPtr(PyObject *self, void *cobj); |
| 52 | |
Antoine Pitrou | d4ae97b | 2008-08-29 18:39:48 +0000 | [diff] [blame] | 53 | |
| 54 | typedef struct { |
| 55 | PyObject_HEAD |
| 56 | void *cobject; |
| 57 | void *desc; |
| 58 | void (*destructor)(void *); |
| 59 | } PyCObject; |
| 60 | |
| 61 | |
Guido van Rossum | 77654a7 | 1996-01-12 00:44:03 +0000 | [diff] [blame] | 62 | #ifdef __cplusplus |
| 63 | } |
| 64 | #endif |
| 65 | #endif /* !Py_COBJECT_H */ |