add a replacement API for PyCObject, PyCapsule #5630

All stdlib modules with C-APIs now use this.

Patch by Larry Hastings
diff --git a/Include/Python.h b/Include/Python.h
index 54058a5..969ef0f 100644
--- a/Include/Python.h
+++ b/Include/Python.h
@@ -89,6 +89,7 @@
 #include "classobject.h"
 #include "fileobject.h"
 #include "cobject.h"
+#include "pycapsule.h"
 #include "traceback.h"
 #include "sliceobject.h"
 #include "cellobject.h"
diff --git a/Include/cobject.h b/Include/cobject.h
index 4e24d7d..9efe4ce 100644
--- a/Include/cobject.h
+++ b/Include/cobject.h
@@ -1,10 +1,8 @@
 
-/* C objects to be exported from one extension module to another.
+/* 
  
-   C objects are used for communication between extension modules.
-   They provide a way for an extension module to export a C interface
-   to other extension modules, so that extension modules can use the
-   Python import mechanism to link to one another.
+The CObject module is now *deprecated* as of Python 3.1.
+Please use the Capsule API instead; see "pycapsule.h".
 
 */
 
diff --git a/Include/datetime.h b/Include/datetime.h
index 0d310b4..4b506b2 100644
--- a/Include/datetime.h
+++ b/Include/datetime.h
@@ -158,9 +158,8 @@
 
 } PyDateTime_CAPI;
 
+#define PyDateTime_CAPSULE_NAME "datetime.datetime_CAPI"
 
-/* "magic" constant used to partially protect against developer mistakes. */
-#define DATETIME_API_MAGIC 0x414548d5
 
 #ifdef Py_BUILD_CORE
 
@@ -186,15 +185,7 @@
 static PyDateTime_CAPI *PyDateTimeAPI;
 
 #define PyDateTime_IMPORT \
-        PyDateTimeAPI = (PyDateTime_CAPI*) PyCObject_Import("datetime", \
-                                                            "datetime_CAPI")
-
-/* This macro would be used if PyCObject_ImportEx() was created.
-#define PyDateTime_IMPORT \
-        PyDateTimeAPI = (PyDateTime_CAPI*) PyCObject_ImportEx("datetime", \
-                                                            "datetime_CAPI", \
-                                                            DATETIME_API_MAGIC)
-*/
+        PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
 
 /* Macros for type checking when not building the Python core. */
 #define PyDate_Check(op) PyObject_TypeCheck(op, PyDateTimeAPI->DateType)
diff --git a/Include/py_curses.h b/Include/py_curses.h
index b4ad8f7..ae7b12b 100644
--- a/Include/py_curses.h
+++ b/Include/py_curses.h
@@ -75,6 +75,9 @@
 
 #define PyCursesWindow_Check(v)	 (Py_TYPE(v) == &PyCursesWindow_Type)
 
+#define PyCurses_CAPSULE_NAME "_curses._C_API"
+
+
 #ifdef CURSES_MODULE
 /* This section is used when compiling _cursesmodule.c */
 
@@ -89,16 +92,8 @@
 #define PyCursesInitialisedColor {if (! ((int (*)(void))PyCurses_API[3]) () ) return NULL;}
 
 #define import_curses() \
-{ \
-  PyObject *module = PyImport_ImportModuleNoBlock("_curses"); \
-  if (module != NULL) { \
-    PyObject *module_dict = PyModule_GetDict(module); \
-    PyObject *c_api_object = PyDict_GetItemString(module_dict, "_C_API"); \
-    if (PyCObject_Check(c_api_object)) { \
-      PyCurses_API = (void **)PyCObject_AsVoidPtr(c_api_object); \
-    } \
-  } \
-}
+    PyCurses_API = (void **)PyCapsule_Import(PyCurses_CAPSULE_NAME, 1);
+
 #endif
 
 /* general error messages */
diff --git a/Include/pycapsule.h b/Include/pycapsule.h
new file mode 100644
index 0000000..17a509e
--- /dev/null
+++ b/Include/pycapsule.h
@@ -0,0 +1,57 @@
+
+/* Capsule objects let you wrap a C "void *" pointer in a Python
+   object.  They're a way of passing data through the Python interpreter
+   without creating your own custom type.
+ 
+   Capsules are used for communication between extension modules.
+   They provide a way for an extension module to export a C interface
+   to other extension modules, so that extension modules can use the
+   Python import mechanism to link to one another.
+ 
+   For more information, please see "c-api/capsule.html" in the
+   documentation.
+*/
+
+#ifndef Py_CAPSULE_H
+#define Py_CAPSULE_H
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+PyAPI_DATA(PyTypeObject) PyCapsule_Type;
+
+typedef void (*PyCapsule_Destructor)(PyObject *);
+
+#define PyCapsule_CheckExact(op) (Py_TYPE(op) == &PyCapsule_Type)
+
+
+PyAPI_FUNC(PyObject *) PyCapsule_New(
+    void *pointer, 
+    const char *name,
+    PyCapsule_Destructor destructor);
+
+PyAPI_FUNC(void *) PyCapsule_GetPointer(PyObject *capsule, const char *name);
+
+PyAPI_FUNC(PyCapsule_Destructor) PyCapsule_GetDestructor(PyObject *capsule);
+
+PyAPI_FUNC(const char *) PyCapsule_GetName(PyObject *capsule);
+
+PyAPI_FUNC(void *) PyCapsule_GetContext(PyObject *capsule);
+
+PyAPI_FUNC(int) PyCapsule_IsValid(PyObject *capsule, const char *name);
+
+PyAPI_FUNC(int) PyCapsule_SetPointer(PyObject *capsule, void *pointer);
+
+PyAPI_FUNC(int) PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor);
+
+PyAPI_FUNC(int) PyCapsule_SetName(PyObject *capsule, const char *name);
+
+PyAPI_FUNC(int) PyCapsule_SetContext(PyObject *capsule, void *context);
+
+PyAPI_FUNC(void *) PyCapsule_Import(const char *name, int no_block);
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* !Py_CAPSULE_H */
diff --git a/Include/pyexpat.h b/Include/pyexpat.h
index 1e79f4e..5340ef5 100644
--- a/Include/pyexpat.h
+++ b/Include/pyexpat.h
@@ -4,6 +4,7 @@
 /* note: you must import expat.h before importing this module! */
 
 #define PyExpat_CAPI_MAGIC  "pyexpat.expat_CAPI 1.0"
+#define PyExpat_CAPSULE_NAME "pyexpat.expat_CAPI"
 
 struct PyExpat_CAPI 
 {
diff --git a/Include/ucnhash.h b/Include/ucnhash.h
index 6231c98..69b7774 100644
--- a/Include/ucnhash.h
+++ b/Include/ucnhash.h
@@ -6,7 +6,9 @@
 extern "C" {
 #endif
 
-/* revised ucnhash CAPI interface (exported through a PyCObject) */
+/* revised ucnhash CAPI interface (exported through a "wrapper") */
+
+#define PyUnicodeData_CAPSULE_NAME "unicodedata.ucnhash_CAPI"
 
 typedef struct {