Skip to content

Commit 0639b56

Browse files
author
Victor Stinner
committed
Issue python#3080: Add PyModule_NewObject() function
1 parent 3a9559b commit 0639b56

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

Doc/c-api/module.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ There are only a few functions special to module objects.
2929
:c:data:`PyModule_Type`.
3030
3131
32-
.. c:function:: PyObject* PyModule_New(const char *name)
32+
.. c:function:: PyObject* PyModule_NewObject(PyObject *name)
3333
3434
.. index::
3535
single: __name__ (module attribute)
@@ -40,6 +40,14 @@ There are only a few functions special to module objects.
4040
Only the module's :attr:`__doc__` and :attr:`__name__` attributes are filled in;
4141
the caller is responsible for providing a :attr:`__file__` attribute.
4242
43+
.. versionadded:: 3.3
44+
45+
46+
.. c:function:: PyObject* PyModule_New(const char *name)
47+
48+
Similar to :c:func:`PyImport_NewObject`, but the name is an UTF-8 encoded
49+
string instead of a Unicode object.
50+
4351
4452
.. c:function:: PyObject* PyModule_GetDict(PyObject *module)
4553

Include/moduleobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ PyAPI_DATA(PyTypeObject) PyModule_Type;
1212
#define PyModule_Check(op) PyObject_TypeCheck(op, &PyModule_Type)
1313
#define PyModule_CheckExact(op) (Py_TYPE(op) == &PyModule_Type)
1414

15+
PyAPI_FUNC(PyObject *) PyModule_NewObject(
16+
PyObject *name
17+
);
1518
PyAPI_FUNC(PyObject *) PyModule_New(
1619
const char *name /* UTF-8 encoded string */
1720
);

Objects/moduleobject.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,44 @@ static PyTypeObject moduledef_type = {
2727

2828

2929
PyObject *
30-
PyModule_New(const char *name)
30+
PyModule_NewObject(PyObject *name)
3131
{
3232
PyModuleObject *m;
33-
PyObject *nameobj;
3433
m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
3534
if (m == NULL)
3635
return NULL;
3736
m->md_def = NULL;
3837
m->md_state = NULL;
39-
nameobj = PyUnicode_FromString(name);
4038
m->md_dict = PyDict_New();
41-
if (m->md_dict == NULL || nameobj == NULL)
39+
if (m->md_dict == NULL)
4240
goto fail;
43-
if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
41+
if (PyDict_SetItemString(m->md_dict, "__name__", name) != 0)
4442
goto fail;
4543
if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
4644
goto fail;
4745
if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
4846
goto fail;
49-
Py_DECREF(nameobj);
5047
PyObject_GC_Track(m);
5148
return (PyObject *)m;
5249

5350
fail:
54-
Py_XDECREF(nameobj);
5551
Py_DECREF(m);
5652
return NULL;
5753
}
5854

55+
PyObject *
56+
PyModule_New(const char *name)
57+
{
58+
PyObject *nameobj, *module;
59+
nameobj = PyUnicode_FromString(name);
60+
if (nameobj == NULL)
61+
return NULL;
62+
module = PyModule_NewObject(nameobj);
63+
Py_DECREF(nameobj);
64+
return module;
65+
}
66+
67+
5968
PyObject *
6069
PyModule_Create2(struct PyModuleDef* module, int module_api_version)
6170
{

0 commit comments

Comments
 (0)