pycapsule.pxd 5.6 KB

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