add a replacement API for PyCObject, PyCapsule #5630

All stdlib modules with C-APIs now use this.

Patch by Larry Hastings
diff --git a/Doc/c-api/capsule.rst b/Doc/c-api/capsule.rst
new file mode 100644
index 0000000..163ad60
--- /dev/null
+++ b/Doc/c-api/capsule.rst
@@ -0,0 +1,168 @@
+.. highlightlang:: c
+
+.. _capsules:
+
+Capsules
+--------
+
+.. index:: object: Capsule
+
+Refer to :ref:`using-capsules` for more information on using these objects.
+
+
+.. ctype:: PyCapsule
+
+   This subtype of :ctype:`PyObject` represents an opaque value, useful for C
+   extension modules who need to pass an opaque value (as a :ctype:`void\*`
+   pointer) through Python code to other C code.  It is often used to make a C
+   function pointer defined in one module available to other modules, so the
+   regular import mechanism can be used to access C APIs defined in dynamically
+   loaded modules.
+
+.. ctype:: PyCapsule_Destructor
+
+   The type of a destructor callback for a capsule.  Defined as::
+
+      typedef void (*PyCapsule_Destructor)(PyObject *);
+
+   See :cfunc:`PyCapsule_New` for the semantics of PyCapsule_Destructor
+   callbacks.
+
+
+.. cfunction:: int PyCapsule_CheckExact(PyObject *p)
+
+   Return true if its argument is a :ctype:`PyCapsule`.
+
+.. cfunction:: PyObject* PyCapsule_New(void* pointer, const char* name, PyCapsule_Destructor destructor)
+
+   Create a :ctype:`PyCapsule` encapsulating the *pointer*.  The *pointer*
+   argument may not be *NULL*.
+
+   The *name* string may either be *NULL* or a pointer to a valid
+   C string.  If non-*NULL*, this string must outlive the capsule.
+   (Though it is permitted to free it inside the *destructor*.)
+
+   If the *destructor* argument is not *NULL*,
+   it will be called with the capsule ``PyObject *`` when it is destroyed.
+
+   If this capsule will be stored as an attribute of a module, it
+   is strongly suggested that the *name* string be specified as::
+
+      modulename.attributename
+
+   This will enable other modules to import the capsule
+   using :cfunc:`PyCapsule_Import`.
+
+   Return a valid capsule on success.
+   On failure, set an exception and return *NULL*.
+
+
+.. cfunction:: void* PyCapsule_GetPointer(PyObject* capsule, const char* name)
+
+   Retrieve the *pointer* stored in the capsule.
+
+   The *name* parameter must compare exactly to the name stored in the capsule.
+   If the name stored in the capsule is *NULL*, the *name* passed in must
+   also be *NULL*.  If the name stored in the capsule is non-*NULL*,
+   the *name* passed in must also be non-*NULL*, and must match the name
+   stored in the capsule.  Python uses the C function *strcmp* to compare
+   capsule names.
+
+   Return the internal *pointer* on success.
+   On failure, set an exception and return *NULL*.
+
+
+.. cfunction:: PyCapsule_Destructor PyCapsule_GetDestructor(PyObject* capsule)
+
+   Return the current *destructor* stored in the capsule.
+   On failure, set an exception and return *NULL*.
+
+   It is legal for a capsule to have a *NULL* destructor.
+   This makes a *NULL* return code somewhat ambiguous;
+   use :cfunc:`PyCapsule_IsValid` or :cfunc:`PyErr_Occurred` to disambugate.
+
+
+.. cfunction:: void* PyCapsule_GetContext(PyObject* capsule)
+
+   Return the current *context* stored in the capsule.
+   On failure, set an exception and return *NULL*.
+
+   It is legal for a capsule to have a *NULL* context.
+   This makes a *NULL* return code somewhat ambiguous;
+   use :cfunc:`PyCapsule_IsValid` or :cfunc:`PyErr_Occurred` to disambugate.
+
+
+.. cfunction:: const char* PyCapsule_GetName(PyObject* capsule)
+
+   Return the current *name* stored in the capsule.
+   On failure, set an exception and return *NULL*.
+
+   It is legal for a capsule to have a *NULL* name.
+   This makes a *NULL* return code somewhat ambiguous;
+   use :cfunc:`PyCapsule_IsValid` or :cfunc:`PyErr_Occurred` to disambugate.
+
+
+.. cfunction:: void* PyCapsule_Import(const char* name, int no_block)
+
+   Import a pointer to a C object from a ``capsule`` attribute in a module.
+   The *name* parameter should specify the full name to the attribute, as
+   in *"module.attribute"*.
+   The *name* stored in the capsule must match this string exactly.
+   If *no_block* is true, import the module without blocking
+   (using :cfunc:`PyImport_ImportModuleNoBlock`).
+   If *no_block* is false, import the module conventionally
+   (using :cfunc:`PyImport_ImportModule`).
+
+   Return the capsule's internal *pointer* on success.
+   On failure, set an exception and return *NULL*.
+   Exception: if *PyCapsule_Import* failed to import the module,
+   and *no_block* was true, no exception is set.
+
+.. cfunction:: int PyCapsule_IsValid(PyObject* capsule, const char* name)
+
+   Determines whether or not a :ctype:`PyObject \*` is a valid capsule.
+   A valid capsule is non-*NULL*, passes :cfunc:`PyCapsule_CheckExact`,
+   has a non-NULL *pointer*, and its internal name matches the
+   *name* parameter.  (See :cfunc:`PyCapsule_GetPointer` for
+   information on how capsule names are compared.)
+
+   In other words, if :cfunc:`PyCapsule_IsValid` returns a true value,
+   calls to any of the accessors (any function starting
+   with :cfunc:`PyCapsule_Get`) are guaranteed to succeed.
+
+   Return a nonzero value if the object is valid and matches the name
+   passed in.
+   Return 0 otherwise.
+   This function will not fail.
+
+.. cfunction:: int PyCapsule_SetContext(PyObject* capsule, void* context)
+
+   Set the context pointer inside *capsule* to *context*.
+
+   Return 0 on success.
+   Return nonzero and set an exception on failure.
+
+.. cfunction:: int PyCapsule_SetDestructor(PyObject* capsule, void (*)(PyObject *) destructor)
+
+   Set the destructor inside *capsule* to *destructor*.
+
+   Return 0 on success.
+   Return nonzero and set an exception on failure.
+
+.. cfunction:: int PyCapsule_SetName(PyObject* capsule, const char* name)
+
+   Set the name inside *capsule* to *name*.  If non-*NULL*, the name
+   must outlive the capsule.  If the previous *name* stored in the
+   capsule was not *NULL*, no attempt is made to free it.
+
+   Return 0 on success.
+   Return nonzero and set an exception on failure.
+
+.. cfunction:: int PyCapsule_SetPointer(PyObject* capsule, void* pointer)
+
+   Set the void pointer inside *capsule* to *pointer*.  The pointer
+   may not be *NULL*.
+
+   Return 0 on success.
+   Return nonzero and set an exception on failure.
+
diff --git a/Doc/c-api/cobject.rst b/Doc/c-api/cobject.rst
index 10f7bba..ee65a98 100644
--- a/Doc/c-api/cobject.rst
+++ b/Doc/c-api/cobject.rst
@@ -7,8 +7,11 @@
 
 .. index:: object: CObject
 
-Refer to :ref:`using-cobjects` for more information on using these objects.
 
+.. warning::
+
+   The CObject API is deprecated as of Python 3.1.  Please switch to the new
+   :ref:`capsules` API.
 
 .. ctype:: PyCObject
 
diff --git a/Doc/c-api/concrete.rst b/Doc/c-api/concrete.rst
index 2ba0833..d1fdec0 100644
--- a/Doc/c-api/concrete.rst
+++ b/Doc/c-api/concrete.rst
@@ -101,6 +101,7 @@
    descriptor.rst
    slice.rst
    weakref.rst
+   capsule.rst
    cobject.rst
    cell.rst
    gen.rst
diff --git a/Doc/data/refcounts.dat b/Doc/data/refcounts.dat
index a0e7e0c..400cf64 100644
--- a/Doc/data/refcounts.dat
+++ b/Doc/data/refcounts.dat
@@ -55,6 +55,45 @@
 PyBuffer_New:PyObject*::+1:
 PyBuffer_New:int:size::
 
+PyCapsule_GetContext:void *:::
+PyCapsule_GetContext:PyObject*:self:0:
+
+PyCapsule_GetDestructor:void (*)(PyObject *):::
+PyCapsule_GetDestructor:PyObject*:self:0:
+
+PyCapsule_GetName:const char *:::
+PyCapsule_GetName:PyObject*:self:0:
+
+PyCapsule_GetPointer:void*:::
+PyCapsule_GetPointer:PyObject*:self:0:
+PyCapsule_GetPointer:const char *:name::
+
+PyCapsule_Import:void *:::
+PyCapsule_Import:const char *:name::
+PyCapsule_Import:int:no_block::
+
+PyCapsule_New:PyObject*::+1:
+PyCapsule_New:void*:pointer::
+PyCapsule_New:const char *:name::
+PyCapsule_New::void (* destructor)(PyObject* )::
+
+PyCapsule_SetContext:int:::
+PyCapsule_SetContext:PyObject*:self:0:
+PyCapsule_SetContext:void *:context::
+
+PyCapsule_SetDestructor:int:::
+PyCapsule_SetDestructor:PyObject*:self:0:
+PyCapsule_SetDestructor:void (*)(PyObject *):destructor::
+
+PyCapsule_SetName:int:::
+PyCapsule_SetName:PyObject*:self:0:
+PyCapsule_SetName:const char *:name::
+
+PyCapsule_SetPointer:int:::
+PyCapsule_SetPointer:PyObject*:self:0:
+PyCapsule_SetPointer:void*:pointer::
+
+
 PyCObject_AsVoidPtr:void*:::
 PyCObject_AsVoidPtr:PyObject*:self:0:
 
diff --git a/Doc/extending/extending.rst b/Doc/extending/extending.rst
index 82cc40b..7e680db 100644
--- a/Doc/extending/extending.rst
+++ b/Doc/extending/extending.rst
@@ -1075,7 +1075,7 @@
 define this symbol).
 
 
-.. _using-cobjects:
+.. _using-capsules:
 
 Providing a C API for an Extension Module
 =========================================
@@ -1111,23 +1111,40 @@
 other extension modules must be exported in a different way.
 
 Python provides a special mechanism to pass C-level information (pointers) from
-one extension module to another one: CObjects. A CObject is a Python data type
-which stores a pointer (:ctype:`void \*`).  CObjects can only be created and
+one extension module to another one: Capsules. A Capsule is a Python data type
+which stores a pointer (:ctype:`void \*`).  Capsules can only be created and
 accessed via their C API, but they can be passed around like any other Python
 object. In particular,  they can be assigned to a name in an extension module's
 namespace. Other extension modules can then import this module, retrieve the
-value of this name, and then retrieve the pointer from the CObject.
+value of this name, and then retrieve the pointer from the Capsule.
 
-There are many ways in which CObjects can be used to export the C API of an
-extension module. Each name could get its own CObject, or all C API pointers
-could be stored in an array whose address is published in a CObject. And the
+There are many ways in which Capsules can be used to export the C API of an
+extension module. Each function could get its own Capsule, or all C API pointers
+could be stored in an array whose address is published in a Capsule. And the
 various tasks of storing and retrieving the pointers can be distributed in
 different ways between the module providing the code and the client modules.
 
+Whichever method you choose, it's important to name your Capsules properly.
+The function :cfunc:`PyCapsule_New` takes a name parameter
+(:ctype:`const char \*`); you're permitted to pass in a *NULL* name, but
+we strongly encourage you to specify a name.  Properly named Capsules provide
+a degree of runtime type-safety; there is no feasible way to tell one unnamed
+Capsule from another.
+
+In particular, Capsules used to expose C APIs should be given a name following
+this convention::
+
+    modulename.attributename
+
+The convenience function :cfunc:`PyCapsule_Import` makes it easy to
+load a C API provided via a Capsule, but only if the Capsule's name
+matches this convention.  This behavior gives C API users a high degree
+of certainty that the Capsule they load contains the correct C API.
+
 The following example demonstrates an approach that puts most of the burden on
 the writer of the exporting module, which is appropriate for commonly used
 library modules. It stores all C API pointers (just one in the example!) in an
-array of :ctype:`void` pointers which becomes the value of a CObject. The header
+array of :ctype:`void` pointers which becomes the value of a Capsule. The header
 file corresponding to the module provides a macro that takes care of importing
 the module and retrieving its C API pointers; client modules only have to call
 this macro before accessing the C API.
@@ -1189,8 +1206,8 @@
        /* Initialize the C API pointer array */
        PySpam_API[PySpam_System_NUM] = (void *)PySpam_System;
 
-       /* Create a CObject containing the API pointer array's address */
-       c_api_object = PyCObject_FromVoidPtr((void *)PySpam_API, NULL);
+       /* Create a Capsule containing the API pointer array's address */
+       c_api_object = PyCapsule_New((void *)PySpam_API, "spam._C_API", NULL);
 
        if (c_api_object != NULL)
            PyModule_AddObject(m, "_C_API", c_api_object);
@@ -1233,21 +1250,14 @@
    #define PySpam_System \
     (*(PySpam_System_RETURN (*)PySpam_System_PROTO) PySpam_API[PySpam_System_NUM])
 
-   /* Return -1 and set exception on error, 0 on success. */
+   /* Return -1 on error, 0 on success.
+    * PyCapsule_Import will set an exception if there's an error.
+    */
    static int
    import_spam(void)
    {
-       PyObject *module = PyImport_ImportModule("spam");
-
-       if (module != NULL) {
-           PyObject *c_api_object = PyObject_GetAttrString(module, "_C_API");
-           if (c_api_object == NULL)
-               return -1;
-           if (PyCObject_Check(c_api_object))
-               PySpam_API = (void **)PyCObject_AsVoidPtr(c_api_object);
-           Py_DECREF(c_api_object);
-       }
-       return 0;
+       PySpam_API = (void **)PyCapsule_Import("spam._C_API", 0);
+       return (PySpam_API != NULL) ? 0 : -1;
    }
 
    #endif
@@ -1280,11 +1290,11 @@
 rather complicated. However, the basic structure is the same for each function
 that is exported, so it has to be learned only once.
 
-Finally it should be mentioned that CObjects offer additional functionality,
+Finally it should be mentioned that Capsules offer additional functionality,
 which is especially useful for memory allocation and deallocation of the pointer
-stored in a CObject. The details are described in the Python/C API Reference
-Manual in the section :ref:`cobjects` and in the implementation of CObjects (files
-:file:`Include/cobject.h` and :file:`Objects/cobject.c` in the Python source
+stored in a Capsule. The details are described in the Python/C API Reference
+Manual in the section :ref:`capsules` and in the implementation of Capsules (files
+:file:`Include/pycapsule.h` and :file:`Objects/pycapsule.c` in the Python source
 code distribution).
 
 .. rubric:: Footnotes