pycapsule.pxd 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # available since Python 3.1!
  2. # note all char* in the below functions are actually const char*
  3. cdef extern from "Python.h":
  4. ctypedef struct PyCapsule_Type
  5. # This subtype of PyObject represents an opaque value, useful for
  6. # C extension modules who need to pass an opaque value (as a void*
  7. # pointer) through Python code to other C code. It is often used
  8. # to make a C function pointer defined in one module available to
  9. # other modules, so the regular import mechanism can be used to
  10. # access C APIs defined in dynamically loaded modules.
  11. ctypedef void (*PyCapsule_Destructor)(object o)
  12. # The type of a destructor callback for a capsule.
  13. #
  14. # See PyCapsule_New() for the semantics of PyCapsule_Destructor
  15. # callbacks.
  16. bint PyCapsule_CheckExact(object o)
  17. # Return true if its argument is a PyCapsule.
  18. object PyCapsule_New(void *pointer, char *name,
  19. PyCapsule_Destructor destructor)
  20. # Return value: New reference.
  21. #
  22. # Create a PyCapsule encapsulating the pointer. The pointer
  23. # argument may not be NULL.
  24. #
  25. # On failure, set an exception and return NULL.
  26. #
  27. # The name string may either be NULL or a pointer to a valid C
  28. # string. If non-NULL, this string must outlive the
  29. # capsule. (Though it is permitted to free it inside the
  30. # destructor.)
  31. #
  32. # If the destructor argument is not NULL, it will be called with
  33. # the capsule as its argument when it is destroyed.
  34. #
  35. # If this capsule will be stored as an attribute of a module, the
  36. # name should be specified as modulename.attributename. This will
  37. # enable other modules to import the capsule using
  38. # PyCapsule_Import().
  39. void* PyCapsule_GetPointer(object capsule, char *name) except? NULL
  40. # Retrieve the pointer stored in the capsule. On failure, set an
  41. # exception and return NULL.
  42. #
  43. # The name parameter must compare exactly to the name stored in
  44. # the capsule. If the name stored in the capsule is NULL, the name
  45. # passed in must also be NULL. Python uses the C function strcmp()
  46. # to compare capsule names.
  47. PyCapsule_Destructor PyCapsule_GetDestructor(object capsule) except? NULL
  48. # Return the current destructor stored in the capsule. On failure,
  49. # set an exception and return NULL.
  50. #
  51. # It is legal for a capsule to have a NULL destructor. This makes
  52. # a NULL return code somewhat ambiguous; use PyCapsule_IsValid()
  53. # or PyErr_Occurred() to disambiguate.
  54. char* PyCapsule_GetName(object capsule) except? NULL
  55. # Return the current name stored in the capsule. On failure, set
  56. # an exception and return NULL.
  57. #
  58. # It is legal for a capsule to have a NULL name. This makes a NULL
  59. # return code somewhat ambiguous; use PyCapsule_IsValid() or
  60. # PyErr_Occurred() to disambiguate.
  61. void* PyCapsule_GetContext(object capsule) except? NULL
  62. # Return the current context stored in the capsule. On failure,
  63. # set an exception and return NULL.
  64. #
  65. # It is legal for a capsule to have a NULL context. This makes a
  66. # NULL return code somewhat ambiguous; use PyCapsule_IsValid() or
  67. # PyErr_Occurred() to disambiguate.
  68. bint PyCapsule_IsValid(object capsule, char *name)
  69. # Determines whether or not capsule is a valid capsule. A valid
  70. # capsule is non-NULL, passes PyCapsule_CheckExact(), has a
  71. # non-NULL pointer stored in it, and its internal name matches the
  72. # name parameter. (See PyCapsule_GetPointer() for information on
  73. # how capsule names are compared.)
  74. #
  75. # In other words, if PyCapsule_IsValid() returns a true value,
  76. # calls to any of the accessors (any function starting with
  77. # PyCapsule_Get()) are guaranteed to succeed.
  78. #
  79. # Return a nonzero value if the object is valid and matches the
  80. # name passed in. Return 0 otherwise. This function will not fail.
  81. int PyCapsule_SetPointer(object capsule, void *pointer) except -1
  82. # Set the void pointer inside capsule to pointer. The pointer may
  83. # not be NULL.
  84. #
  85. # Return 0 on success. Return nonzero and set an exception on
  86. # failure.
  87. int PyCapsule_SetDestructor(object capsule, PyCapsule_Destructor destructor) except -1
  88. # Set the destructor inside capsule to destructor.
  89. #
  90. # Return 0 on success. Return nonzero and set an exception on
  91. # failure.
  92. int PyCapsule_SetName(object capsule, char *name) except -1
  93. # Set the name inside capsule to name. If non-NULL, the name must
  94. # outlive the capsule. If the previous name stored in the capsule
  95. # was not NULL, no attempt is made to free it.
  96. #
  97. # Return 0 on success. Return nonzero and set an exception on
  98. # failure.
  99. int PyCapsule_SetContext(object capsule, void *context) except -1
  100. # Set the context pointer inside capsule to context. Return 0 on
  101. # success. Return nonzero and set an exception on failure.
  102. void* PyCapsule_Import(char *name, int no_block) except? NULL
  103. # Import a pointer to a C object from a capsule attribute in a
  104. # module. The name parameter should specify the full name to the
  105. # attribute, as in module.attribute. The name stored in the
  106. # capsule must match this string exactly. If no_block is true,
  107. # import the module without blocking (using
  108. # PyImport_ImportModuleNoBlock()). If no_block is false, import
  109. # the module conventionally (using PyImport_ImportModule()).
  110. #
  111. # Return the capsule’s internal pointer on success. On failure,
  112. # set an exception and return NULL. However, if PyCapsule_Import()
  113. # failed to import the module, and no_block was true, no exception
  114. # is set.