Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c3a787e

Browse files
committed
Surprising fix for SF bug 563060: module can be used as base class.
Change the module constructor (module_init) to have the signature __init__(name:str, doc=None); this prevents the call from type_new() to succeed. While we're at it, prevent repeated calling of module_init for the same module from leaking the dict, changing the semantics so that __dict__ is only initialized if NULL. Also adding a unittest, test_module.py. This is an incompatibility with 2.2, if anybody was instantiating the module class before, their argument list was probably empty; so this can't be backported to 2.2.x.
1 parent 88f72ff commit c3a787e

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

Lib/test/test_module.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Test the module type
2+
3+
from test_support import verify, vereq, verbose, TestFailed
4+
5+
import sys
6+
module = type(sys)
7+
8+
# An uninitialized module has no __dict__ or __name__, and __doc__ is None
9+
foo = module.__new__(module)
10+
verify(foo.__dict__ is None)
11+
try:
12+
s = foo.__name__
13+
except AttributeError:
14+
pass
15+
else:
16+
raise TestFailed, "__name__ = %s" % repr(s)
17+
vereq(foo.__doc__, None)
18+
19+
# Regularly initialized module, no docstring
20+
foo = module("foo")
21+
vereq(foo.__name__, "foo")
22+
vereq(foo.__doc__, None)
23+
vereq(foo.__dict__, {"__name__": "foo", "__doc__": None})
24+
25+
# ASCII docstring
26+
foo = module("foo", "foodoc")
27+
vereq(foo.__name__, "foo")
28+
vereq(foo.__doc__, "foodoc")
29+
vereq(foo.__dict__, {"__name__": "foo", "__doc__": "foodoc"})
30+
31+
# Unicode docstring
32+
foo = module("foo", u"foodoc\u1234")
33+
vereq(foo.__name__, "foo")
34+
vereq(foo.__doc__, u"foodoc\u1234")
35+
vereq(foo.__dict__, {"__name__": "foo", "__doc__": u"foodoc\u1234"})
36+
37+
# Reinitialization should not replace the __dict__
38+
foo.bar = 42
39+
d = foo.__dict__
40+
foo.__init__("foo", "foodoc")
41+
vereq(foo.__name__, "foo")
42+
vereq(foo.__doc__, "foodoc")
43+
vereq(foo.bar, 42)
44+
vereq(foo.__dict__, {"__name__": "foo", "__doc__": "foodoc", "bar": 42})
45+
verify(foo.__dict__ is d)
46+
47+
if verbose:
48+
print "All OK"

Objects/moduleobject.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,23 @@ _PyModule_Clear(PyObject *m)
147147
/* Methods */
148148

149149
static int
150-
module_init(PyModuleObject *m, PyObject *args, PyObject *kw)
150+
module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
151151
{
152-
m->md_dict = PyDict_New();
153-
if (m->md_dict == NULL)
152+
static char *kwlist[] = {"name", "doc", NULL};
153+
PyObject *dict, *name = Py_None, *doc = Py_None;
154+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|O", kwlist,
155+
&name, &doc))
156+
return -1;
157+
dict = m->md_dict;
158+
if (dict == NULL) {
159+
dict = PyDict_New();
160+
if (dict == NULL)
161+
return -1;
162+
m->md_dict = dict;
163+
}
164+
if (PyDict_SetItemString(dict, "__name__", name) < 0)
165+
return -1;
166+
if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
154167
return -1;
155168
return 0;
156169
}

0 commit comments

Comments
 (0)