Patchwork [5,of,7] mpatch: use PYMODULEINIT

login
register
mail settings
Submitter Jun Wu
Date May 9, 2017, 1:07 a.m.
Message ID <219c4e0d41d807078e3c.1494292046@x1c>
Download mbox | patch
Permalink /patch/20534/
State Superseded
Headers show

Comments

Jun Wu - May 9, 2017, 1:07 a.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1494290506 25200
#      Mon May 08 17:41:46 2017 -0700
# Node ID 219c4e0d41d807078e3cc9663da11243e81625dd
# Parent  4bd6bf0ecf1ba186851ba6fa3776561b67fe6a7c
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 219c4e0d41d8
mpatch: use PYMODULEINIT

A side effect of the clean up is mercurial.mpatch.mpatchError gets exposed,
which seems to be a good thing.

Patch

diff --git a/mercurial/mpatch_module.c b/mercurial/mpatch_module.c
--- a/mercurial/mpatch_module.c
+++ b/mercurial/mpatch_module.c
@@ -161,35 +161,13 @@  static PyMethodDef methods[] = {
 };
 
-#ifdef IS_PY3K
-static struct PyModuleDef mpatch_module = {
-	PyModuleDef_HEAD_INIT,
-	"mpatch",
-	mpatch_doc,
-	-1,
-	methods
-};
-
-PyMODINIT_FUNC PyInit_mpatch(void)
+static int postinit(PyObject *mod)
 {
-	PyObject *m;
-
-	m = PyModule_Create(&mpatch_module);
-	if (m == NULL)
-		return NULL;
-
 	mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError",
 					  NULL, NULL);
+	if (!mpatch_Error)
+		return -1;
 	Py_INCREF(mpatch_Error);
-	PyModule_AddObject(m, "mpatchError", mpatch_Error);
-
-	return m;
+	return PyModule_AddObject(mod, "mpatchError", mpatch_Error);
 }
-#else
-PyMODINIT_FUNC
-initmpatch(void)
-{
-	Py_InitModule3("mpatch", methods, mpatch_doc);
-	mpatch_Error = PyErr_NewException("mercurial.mpatch.mpatchError",
-					  NULL, NULL);
-}
-#endif
+
+PYMODULEINIT(mpatch, methods, mpatch_doc, 1, NULL, postinit);