[New-bugs-announce] [issue16194] imp.load_dynamic imports wrong module when called several times on a multi-module .so

Václav Šmilauer report at bugs.python.org
Thu Oct 11 13:11:10 CEST 2012


New submission from Václav Šmilauer:

I have several compiled modules linked into one .so file and import them using imp.load_dynamic.

Only the first module imported with load_dynamic is imported properly, all subsequent calls of load_dynamic on the same file ignore the first argument (name) and return the first module again. The init function is also called only for the first module imported by load_dynamic.

The bug is reproducible for python 2.7.3 and 3.2.2. Test case is attached.

Here inline simplified source for 2.7:

foo.c:

	#include<stdio.h>
	#include<Python.h>
	PyMODINIT_FUNC initfoo(){
		(void) Py_InitModule("foo",NULL);
		printf("initfoo()\n");
	}
	PyMODINIT_FUNC initbar(void){
		(void) Py_InitModule("bar",NULL);
		printf("initbar()\n");
	}
	PyMODINIT_FUNC initbaz(void){
		(void) Py_InitModule("baz",NULL);
		printf("initbaz()\n");
	}

test.py:

	import sys,imp
	# import foo using the normal machinery
	sys.path.append('.')
	import foo
	# this is OK
	print imp.load_dynamic('bar','foo.so')
	# this imports *bar* again, but should import baz
	print imp.load_dynamic('baz','foo.so')
	# this imports *bar* again, although the module is not defined at all
	print imp.load_dynamic('nonsense','foo.so')

Compiled with

         gcc -shared -fPIC foo.c -o foo.so `pkg-config python --cflags --libs`

I get when running "python test.py" output:

        initfoo()
        initbar()
        <module 'bar' from 'foo.so'>
        <module 'bar' from 'foo.so'>
        <module 'bar' from 'foo.so'>

The module 'bar' is imported 3 times, although the 2nd import should import *baz* and the third import should fail ("nonsense" module does not exist).

----------
components: Library (Lib)
files: load_dynamic-test.zip
messages: 172632
nosy: eudoxos
priority: normal
severity: normal
status: open
title: imp.load_dynamic imports wrong module when called several times on a multi-module .so
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file27527/load_dynamic-test.zip

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16194>
_______________________________________


More information about the New-bugs-announce mailing list