https://bitbucket.org/ambroff/greenlet/changeset/f61fffb6feeb --- greenlet.c +++ greenlet.c @@ -1267,7 +1267,11 @@ _PyGreenlet_API[PyGreenlet_SetParent_NUM] = (void *) PyGreenlet_SetParent; +#ifdef GREENLET_USE_PYCAPSULE + c_api_object = PyCapsule_New((void *) _PyGreenlet_API, "greenlet._C_API", NULL); +#else c_api_object = PyCObject_FromVoidPtr((void *) _PyGreenlet_API, NULL); +#endif if (c_api_object != NULL) { PyModule_AddObject(m, "_C_API", c_api_object); --- greenlet.h +++ greenlet.h @@ -31,6 +31,10 @@ #define PyGreenlet_ACTIVE(op) (((PyGreenlet*)(op))->stack_start != NULL) #define PyGreenlet_GET_PARENT(op) (((PyGreenlet*)(op))->parent) +#if (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 7) || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 1) || PY_MAJOR_VERSION > 3 +#define GREENLET_USE_PYCAPSULE +#endif + /* C API functions */ /* Total number of symbols that are exported */ @@ -108,6 +112,12 @@ _PyGreenlet_API[PyGreenlet_SetParent_NUM]) /* Macro that imports greenlet and initializes C API */ +#ifdef GREENLET_USE_PYCAPSULE +#define PyGreenlet_Import() \ +{ \ + _PyGreenlet_API = (void**)PyCapsule_Import("greenlet._C_API", 0); \ +} +#else #define PyGreenlet_Import() \ { \ PyObject *module = PyImport_ImportModule("greenlet"); \ @@ -122,6 +132,7 @@ Py_DECREF(module); \ } \ } +#endif #endif /* GREENLET_MODULE */