[PATCH 1 of 7] util: add a macro initializing CPython modules
Jun Wu
quark at fb.com
Tue May 9 01:07:22 UTC 2017
# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1494288921 25200
# Mon May 08 17:15:21 2017 -0700
# Node ID a2c5e183cafca6d58a0dd986870ac620be1fb107
# Parent 52ec3072fe46bc4b193b6273357e3cc40b4421ad
# Available At https://bitbucket.org/quark-zju/hg-draft
# hg pull https://bitbucket.org/quark-zju/hg-draft -r a2c5e183cafc
util: add a macro initializing CPython modules
This will greatly simplify C module initialization code. The signature
requires a "version" field to help detect incompatible ".so" modules usually
caused by forgetting to build.
diff --git a/mercurial/util.h b/mercurial/util.h
--- a/mercurial/util.h
+++ b/mercurial/util.h
@@ -43,3 +43,43 @@ typedef unsigned char bool;
#endif
+#ifdef IS_PY3K
+#define PYMODULEINIT(name, methods, doc, version, precheck, postinit) \
+ static struct PyModuleDef name ## _module = { \
+ PyModuleDef_HEAD_INIT, \
+ #name, \
+ doc, \
+ -1, \
+ methods \
+ }; \
+ PyMODINIT_FUNC PyInit_ ## name(void) \
+ { \
+ PyObject *m; \
+ if (precheck && ((int (*)(void))precheck)() == -1) \
+ return NULL; \
+ m = PyModule_Create(&(name ## _module)); \
+ if (m == NULL) \
+ return NULL; \
+ if (PyModule_AddIntConstant(m, "version", version) == -1) \
+ return NULL; \
+ if (postinit && ((int (*)(PyObject *))postinit)(m) == -1) \
+ return NULL; \
+ return m; \
+ }
+#else
+#define PYMODULEINIT(name, methods, doc, version, precheck, postinit) \
+ PyMODINIT_FUNC (init ## name)(void) \
+ { \
+ PyObject *m; \
+ if (precheck && ((int (*)(void))precheck)() == -1) \
+ return; \
+ m = Py_InitModule3(#name, methods, doc); \
+ if (m == NULL) \
+ return; \
+ if (PyModule_AddIntConstant(m, "version", version) == -1) \
+ return; \
+ if (postinit != NULL) \
+ ((int (*)(PyObject *))postinit)(m); \
+ }
+#endif
+
#endif /* _HG_UTIL_H_ */
More information about the Mercurial-devel
mailing list