[PATCH 1 of 7] util: add a macro initializing CPython modules

Yuya Nishihara yuya at tcha.org
Sat May 13 09:45:14 EDT 2017


On Mon, 8 May 2017 18:07:22 -0700, Jun Wu wrote:
> # 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

> +#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) \

I got lots of warnings because of the precheck/postinit expansion:

  mercurial/util.h:80:16: warning: the comparison will always evaluate as
  'true' for the address of 'module_init' will never be NULL [-Waddress]

> +			((int (*)(PyObject *))postinit)(m); \

It's dangerous to disable compiler type checking.

Overall, I prefer not using a big macro like this even though it could get
rid of some redundant codes.

Adding "version" constants seems good to me.


More information about the Mercurial-devel mailing list