[Checkins] SVN: zope.hookable/branches/regebro-python3/src/zope/hookable/_zope_hookable.c Cleaning up the module initialization with #defines.

Lennart Regebro regebro at gmail.com
Thu Nov 25 02:58:51 EST 2010


Log message for revision 118579:
  Cleaning up the module initialization with #defines.
  

Changed:
  U   zope.hookable/branches/regebro-python3/src/zope/hookable/_zope_hookable.c

-=-
Modified: zope.hookable/branches/regebro-python3/src/zope/hookable/_zope_hookable.c
===================================================================
--- zope.hookable/branches/regebro-python3/src/zope/hookable/_zope_hookable.c	2010-11-25 07:55:30 UTC (rev 118578)
+++ zope.hookable/branches/regebro-python3/src/zope/hookable/_zope_hookable.c	2010-11-25 07:58:51 UTC (rev 118579)
@@ -21,10 +21,10 @@
 #include "Python.h"
 #include "structmember.h"
 
-/* Support for Python 2.4 and 2.5: */
+/* Support for Python < 2.6 */
 
 #ifndef Py_TYPE
-  #define Py_TYPE(o) ((o)->ob_type)
+  #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
 #endif
 
 #ifndef cmpfunc
@@ -36,13 +36,7 @@
     PyObject_HEAD_INIT(type) size,
 #endif
 
-#if PY_MAJOR_VERSION >= 3
-  #define MOD_ERROR_VAL NULL
-#else
-  #define MOD_ERROR_VAL
-#endif
 
-
 typedef struct {
 	PyObject_HEAD
         PyObject *old;
@@ -170,7 +164,7 @@
 	/* tp_print          */ (printfunc)0,
 	/* tp_getattr        */ (getattrfunc)0,
 	/* tp_setattr        */ (setattrfunc)0,
-	/* tp_compare        */ (cmpfunc)0,
+	/* tp_compare        */ 0,
 	/* tp_repr           */ (reprfunc)0,
 	/* tp_as_number      */ 0,
 	/* tp_as_sequence    */ 0,
@@ -206,33 +200,27 @@
 };
 
 
+#if PY_MAJOR_VERSION >= 3
+  #define MOD_ERROR_VAL NULL
+  #define MOD_SUCCESS_VAL(val) val
+  #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
+  #define MOD_DEF(ob, name, doc, methods) \
+	  static struct PyModuleDef moduledef = { \
+	    PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
+	  ob = PyModule_Create(&moduledef);
+#else
+  #define MOD_ERROR_VAL
+  #define MOD_SUCCESS_VAL(val)
+  #define MOD_INIT(name) void init##name(void)
+  #define MOD_DEF(ob, name, doc, methods) \
+          ob = Py_InitModule3(name, methods, doc);
+#endif
 
-static struct PyMethodDef zch_methods[] = {
+static struct PyMethodDef module_methods[] = {
 	{NULL, NULL}
 };
 
-
-#if PY_MAJOR_VERSION >= 3
-  static struct PyModuleDef moduledef = {
-    PyModuleDef_HEAD_INIT,
-    "_zope_hookable",/* m_name */
-    "Provide an efficient implementation for hookable objects",/* m_doc */
-    -1,/* m_size */
-    zch_methods,/* m_methods */
-    NULL,/* m_reload */
-    NULL,/* m_traverse */
-    NULL,/* m_clear */
-    NULL,/* m_free */
-  };
-#endif
-
-#if PY_MAJOR_VERSION >= 3
-PyMODINIT_FUNC
-PyInit__zope_hookable(void)
-#else
-void
-init_zope_hookable(void)
-#endif
+MOD_INIT(_zope_hookable)
 {
   PyObject *m;
 
@@ -243,13 +231,9 @@
   if (PyType_Ready(&hookabletype) < 0)
     return MOD_ERROR_VAL;
 
-#if PY_MAJOR_VERSION >= 3
-  m = PyModule_Create(&moduledef);
-#else
-  m = Py_InitModule3("_zope_hookable", zch_methods,
-                     "Provide an efficient implementation for hookable objects"
-                     );
-#endif
+  MOD_DEF(m, "_zope_hookable", 
+    "Provide an efficient implementation for hookable objects",
+    module_methods)
 
   if (m == NULL)
     return MOD_ERROR_VAL;
@@ -257,9 +241,6 @@
   if (PyModule_AddObject(m, "hookable", (PyObject *)&hookabletype) < 0)
     return MOD_ERROR_VAL;
 
-#if PY_MAJOR_VERSION >= 3
-  return m;
-#endif
+  return MOD_SUCCESS_VAL(m);
 
 }
-



More information about the checkins mailing list