Skip to content

Commit 6b2cbeb

Browse files
committed
Issue #16421: allow to load multiple modules from the same shared object.
Patch by Václav Šmilauer.
1 parent f76f0ee commit 6b2cbeb

12 files changed

+887
-10
lines changed

Lib/test/test_imp.py

+14
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ def test_issue15828_load_extensions(self):
217217
mod = imp.load_module(example, *x)
218218
self.assertEqual(mod.__name__, example)
219219

220+
def test_issue16421_multiple_modules_in_one_dll(self):
221+
# Issue 16421: loading several modules from the same compiled file fails
222+
m = '_testimportmultiple'
223+
fileobj, pathname, description = imp.find_module(m)
224+
fileobj.close()
225+
mod0 = imp.load_dynamic(m, pathname)
226+
mod1 = imp.load_dynamic('foo', pathname)
227+
mod2 = imp.load_dynamic('bar', pathname)
228+
self.assertEqual(mod0.__name__, m)
229+
self.assertEqual(mod1.__name__, 'foo')
230+
self.assertEqual(mod2.__name__, 'bar')
231+
with self.assertRaises(ImportError):
232+
imp.load_dynamic('nonexistent', pathname)
233+
220234
def test_load_dynamic_ImportError_path(self):
221235
# Issue #1559549 added `name` and `path` attributes to ImportError
222236
# in order to provide better detail. Issue #10854 implemented those

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@ George Sipe
11091109
J. Sipprell
11101110
Kragen Sitaker
11111111
Michael Sloan
1112+
Václav Šmilauer
11121113
Christopher Smith
11131114
Eric V. Smith
11141115
Gregory P. Smith

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ What's New in Python 3.4.0 Alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #16421: loading multiple modules from one shared object is now
14+
handled correctly (previously, the first module loaded from that file
15+
was silently returned). Patch by Václav Šmilauer.
16+
1317
- Issue #16602: When a weakref's target was part of a long deallocation
1418
chain, the object could remain reachable through its weakref even though
1519
its refcount had dropped to zero.

Modules/_testimportmultiple.c

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* C extensions module to test importing multiple modules from one compiled
3+
* file (issue16421). This file defines 3 modules (_testimportmodule,
4+
* foo, bar), only the first one is called the same as the compiled file.
5+
*/
6+
#include<Python.h>
7+
8+
static struct PyModuleDef _testimportmultiple = {
9+
PyModuleDef_HEAD_INIT,
10+
"_testimportmultiple",
11+
"_testimportmultiple doc",
12+
-1,
13+
NULL,
14+
NULL,
15+
NULL,
16+
NULL,
17+
NULL
18+
};
19+
20+
PyMODINIT_FUNC PyInit__testimportmultiple()
21+
{
22+
return PyModule_Create(&_testimportmultiple);
23+
}
24+
25+
static struct PyModuleDef _foomodule = {
26+
PyModuleDef_HEAD_INIT,
27+
"foo",
28+
"foo doc",
29+
-1,
30+
NULL,
31+
NULL,
32+
NULL,
33+
NULL,
34+
NULL
35+
};
36+
37+
PyMODINIT_FUNC PyInit_foo()
38+
{
39+
return PyModule_Create(&_foomodule);
40+
}
41+
42+
static struct PyModuleDef _barmodule = {
43+
PyModuleDef_HEAD_INIT,
44+
"bar",
45+
"bar doc",
46+
-1,
47+
NULL,
48+
NULL,
49+
NULL,
50+
NULL,
51+
NULL
52+
};
53+
54+
PyMODINIT_FUNC PyInit_bar(){
55+
return PyModule_Create(&_barmodule);
56+
}
57+

0 commit comments

Comments
 (0)