summaryrefslogtreecommitdiffstats
path: root/Modules/_testmultiphase.c
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2016-08-21 07:41:56 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2016-08-21 07:41:56 (GMT)
commit8682f578c1c8fd0486c886b001729907a5409a9f (patch)
tree2f54886ef578b7faf94e26fe32e24fbb258b896a /Modules/_testmultiphase.c
parent9c8aa9bffe755fe6126dc72dfd037c6b20e65906 (diff)
downloadcpython-8682f578c1c8fd0486c886b001729907a5409a9f.zip
cpython-8682f578c1c8fd0486c886b001729907a5409a9f.tar.gz
cpython-8682f578c1c8fd0486c886b001729907a5409a9f.tar.bz2
Issue #27782: Fix m_methods handling in multiphase init
Multi-phase extension module import now correctly allows the ``m_methods`` field to be used to add module level functions to instances of non-module types returned from ``Py_create_mod``. Patch by Xiang Zhang.
Diffstat (limited to 'Modules/_testmultiphase.c')
-rw-r--r--Modules/_testmultiphase.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/Modules/_testmultiphase.c b/Modules/_testmultiphase.c
index 2005205..41da997 100644
--- a/Modules/_testmultiphase.c
+++ b/Modules/_testmultiphase.c
@@ -248,6 +248,7 @@ PyInit__testmultiphase(PyObject *spec)
/**** Importing a non-module object ****/
static PyModuleDef def_nonmodule;
+static PyModuleDef def_nonmodule_with_methods;
/* Create a SimpleNamespace(three=3) */
static PyObject*
@@ -255,7 +256,7 @@ createfunc_nonmodule(PyObject *spec, PyModuleDef *def)
{
PyObject *dct, *ns, *three;
- if (def != &def_nonmodule) {
+ if (def != &def_nonmodule && def != &def_nonmodule_with_methods) {
PyErr_SetString(PyExc_SystemError, "def does not match");
return NULL;
}
@@ -291,6 +292,36 @@ PyInit__testmultiphase_nonmodule(PyObject *spec)
return PyModuleDef_Init(&def_nonmodule);
}
+PyDoc_STRVAR(nonmodule_bar_doc,
+"bar(i,j)\n\
+\n\
+Return the difference of i - j.");
+
+static PyObject *
+nonmodule_bar(PyObject *self, PyObject *args)
+{
+ long i, j;
+ long res;
+ if (!PyArg_ParseTuple(args, "ll:bar", &i, &j))
+ return NULL;
+ res = i - j;
+ return PyLong_FromLong(res);
+}
+
+static PyMethodDef nonmodule_methods[] = {
+ {"bar", nonmodule_bar, METH_VARARGS, nonmodule_bar_doc},
+ {NULL, NULL} /* sentinel */
+};
+
+static PyModuleDef def_nonmodule_with_methods = TEST_MODULE_DEF(
+ "_testmultiphase_nonmodule_with_methods", slots_create_nonmodule, nonmodule_methods);
+
+PyMODINIT_FUNC
+PyInit__testmultiphase_nonmodule_with_methods(PyObject *spec)
+{
+ return PyModuleDef_Init(&def_nonmodule_with_methods);
+}
+
/**** Non-ASCII-named modules ****/
static PyModuleDef def_nonascii_latin = { \