You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.1 KiB

23 years ago
  1. /* Wrap void* pointers to be passed between C modules */
  2. #include "Python.h"
  3. /* Declarations for objects of type PyCObject */
  4. typedef void (*destructor1)(void *);
  5. typedef void (*destructor2)(void *, void*);
  6. static int deprecation_exception(void)
  7. {
  8. return PyErr_WarnEx(PyExc_PendingDeprecationWarning,
  9. "The CObject API is deprecated as of Python 3.1. "
  10. "Please convert to using the Capsule API.", 1);
  11. }
  12. PyObject *
  13. PyCObject_FromVoidPtr(void *cobj, void (*destr)(void *))
  14. {
  15. PyCObject *self;
  16. if (deprecation_exception()) {
  17. return NULL;
  18. }
  19. self = PyObject_NEW(PyCObject, &PyCObject_Type);
  20. if (self == NULL)
  21. return NULL;
  22. self->cobject=cobj;
  23. self->destructor=destr;
  24. self->desc=NULL;
  25. return (PyObject *)self;
  26. }
  27. PyObject *
  28. PyCObject_FromVoidPtrAndDesc(void *cobj, void *desc,
  29. void (*destr)(void *, void *))
  30. {
  31. PyCObject *self;
  32. if (deprecation_exception()) {
  33. return NULL;
  34. }
  35. if (!desc) {
  36. PyErr_SetString(PyExc_TypeError,
  37. "PyCObject_FromVoidPtrAndDesc called with null"
  38. " description");
  39. return NULL;
  40. }
  41. self = PyObject_NEW(PyCObject, &PyCObject_Type);
  42. if (self == NULL)
  43. return NULL;
  44. self->cobject = cobj;
  45. self->destructor = (destructor1)destr;
  46. self->desc = desc;
  47. return (PyObject *)self;
  48. }
  49. void *
  50. PyCObject_AsVoidPtr(PyObject *self)
  51. {
  52. if (self) {
  53. if (self->ob_type == &PyCObject_Type)
  54. return ((PyCObject *)self)->cobject;
  55. PyErr_SetString(PyExc_TypeError,
  56. "PyCObject_AsVoidPtr with non-C-object");
  57. }
  58. if (!PyErr_Occurred())
  59. PyErr_SetString(PyExc_TypeError,
  60. "PyCObject_AsVoidPtr called with null pointer");
  61. return NULL;
  62. }
  63. void *
  64. PyCObject_GetDesc(PyObject *self)
  65. {
  66. if (self) {
  67. if (self->ob_type == &PyCObject_Type)
  68. return ((PyCObject *)self)->desc;
  69. PyErr_SetString(PyExc_TypeError,
  70. "PyCObject_GetDesc with non-C-object");
  71. }
  72. if (!PyErr_Occurred())
  73. PyErr_SetString(PyExc_TypeError,
  74. "PyCObject_GetDesc called with null pointer");
  75. return NULL;
  76. }
  77. void *
  78. PyCObject_Import(char *module_name, char *name)
  79. {
  80. PyObject *m, *c;
  81. void *r = NULL;
  82. if ((m = PyImport_ImportModule(module_name))) {
  83. if ((c = PyObject_GetAttrString(m,name))) {
  84. r = PyCObject_AsVoidPtr(c);
  85. Py_DECREF(c);
  86. }
  87. Py_DECREF(m);
  88. }
  89. return r;
  90. }
  91. int
  92. PyCObject_SetVoidPtr(PyObject *self, void *cobj)
  93. {
  94. PyCObject* cself = (PyCObject*)self;
  95. if (cself == NULL || !PyCObject_Check(cself) ||
  96. cself->destructor != NULL) {
  97. PyErr_SetString(PyExc_TypeError,
  98. "Invalid call to PyCObject_SetVoidPtr");
  99. return 0;
  100. }
  101. cself->cobject = cobj;
  102. return 1;
  103. }
  104. static void
  105. PyCObject_dealloc(PyCObject *self)
  106. {
  107. if (self->destructor) {
  108. if(self->desc)
  109. ((destructor2)(self->destructor))(self->cobject, self->desc);
  110. else
  111. (self->destructor)(self->cobject);
  112. }
  113. PyObject_DEL(self);
  114. }
  115. PyDoc_STRVAR(PyCObject_Type__doc__,
  116. "C objects to be exported from one extension module to another\n\
  117. \n\
  118. C objects are used for communication between extension modules. They\n\
  119. provide a way for an extension module to export a C interface to other\n\
  120. extension modules, so that extension modules can use the Python import\n\
  121. mechanism to link to one another.");
  122. PyTypeObject PyCObject_Type = {
  123. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  124. "PyCObject", /*tp_name*/
  125. sizeof(PyCObject), /*tp_basicsize*/
  126. 0, /*tp_itemsize*/
  127. /* methods */
  128. (destructor)PyCObject_dealloc, /*tp_dealloc*/
  129. 0, /*tp_print*/
  130. 0, /*tp_getattr*/
  131. 0, /*tp_setattr*/
  132. 0, /*tp_reserved*/
  133. 0, /*tp_repr*/
  134. 0, /*tp_as_number*/
  135. 0, /*tp_as_sequence*/
  136. 0, /*tp_as_mapping*/
  137. 0, /*tp_hash*/
  138. 0, /*tp_call*/
  139. 0, /*tp_str*/
  140. 0, /*tp_getattro*/
  141. 0, /*tp_setattro*/
  142. 0, /*tp_as_buffer*/
  143. 0, /*tp_flags*/
  144. PyCObject_Type__doc__ /*tp_doc*/
  145. };