Commits

Alexey Borzenkov committed f61fffb

Switch to using PyCapsule for Python 2.7 and 3.1

  • Participants
  • Parent commits c510f32

Comments (0)

Files changed (2)

 	_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);
 #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 */
 	 _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"); \
 		Py_DECREF(module); \
 	} \
 }
+#endif
 
 #endif /* GREENLET_MODULE */