diff Python/importdl.c @ 6602:4514a5ea3ec4 legacy-trunk

Fix importing of shared libraries from inside packages. This is a bit of a hack: when the shared library is loaded, the module name is "package.module", but the module calls Py_InitModule*() with just "module" for the name. The shared library loader squirrels away the true name of the module in _Py_PackageContext, and Py_InitModule*() will substitute this (if the name actually matches).
author Guido van Rossum <guido@python.org>
date Wed, 19 Nov 1997 18:53:33 +0000
parents 58b4d9ea8e51
children 5e31d381fa47
line wrap: on
line diff
--- a/Python/importdl.c	Wed Nov 19 18:51:35 1997 +0000
+++ b/Python/importdl.c	Wed Nov 19 18:53:33 1997 +0000
@@ -233,6 +233,7 @@
 #else
 	PyObject *m, *d, *s;
 	char funcname[258];
+	char *lastdot, *shortname, *packagecontext;
 	dl_funcptr p = NULL;
 #ifdef USE_SHLIB
 	static struct {
@@ -252,7 +253,16 @@
 		Py_INCREF(m);
 		return m;
 	}
-	sprintf(funcname, FUNCNAME_PATTERN, name);
+	lastdot = strrchr(name, '.');
+	if (lastdot == NULL) {
+		packagecontext = NULL;
+		shortname = name;
+	}
+	else {
+		packagecontext = name;
+		shortname = lastdot+1;
+	}
+	sprintf(funcname, FUNCNAME_PATTERN, shortname);
 #ifdef USE_SHLIB
 	if (fp != NULL) {
 		int i;
@@ -519,11 +529,14 @@
   got_it:
 #endif
 	if (p == NULL) {
-		PyErr_SetString(PyExc_ImportError,
-		   "dynamic module does not define init function");
+		PyErr_Format(PyExc_ImportError,
+		   "dynamic module does not define init function (%s)",
+			     funcname);
 		return NULL;
 	}
+	_Py_PackageContext = packagecontext;
 	(*p)();
+	_Py_PackageContext = NULL;
 	if (PyErr_Occurred())
 		return NULL;
 	if (_PyImport_FixupExtension(name, pathname) == NULL)