Skip to content

Commit fbe6d33

Browse files
Check if we've already loaded a dynamic module under a different name.
1 parent 7d0bc83 commit fbe6d33

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Python/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ load_module(name)
487487
break;
488488

489489
case C_EXTENSION:
490-
m = load_dynamic_module(name, buf);
490+
m = load_dynamic_module(name, buf, fp);
491491
break;
492492

493493
#ifdef macintosh
@@ -868,7 +868,7 @@ imp_load_dynamic(self, args)
868868
object *dummy;
869869
if (!newgetargs(args, "ss|O", &name, &pathname, &dummy))
870870
return NULL;
871-
return load_dynamic_module(name, pathname);
871+
return load_dynamic_module(name, pathname, NULL);
872872
}
873873

874874
static object *

Python/importdl.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ static void aix_loaderror(char *name);
127127
#ifdef DYNAMIC_LINK
128128

129129
#ifdef USE_SHLIB
130+
#include <sys/types.h>
131+
#include <sys/stat.h>
130132
#ifdef __NetBSD__
131133
#include <nlist.h>
132134
#include <link.h>
@@ -204,9 +206,10 @@ struct filedescr import_filetab[] = {
204206
};
205207

206208
object *
207-
load_dynamic_module(name, pathname)
209+
load_dynamic_module(name, pathname, fp)
208210
char *name;
209211
char *pathname;
212+
FILE *fp;
210213
{
211214
#ifndef DYNAMIC_LINK
212215
err_setstr(ImportError, "dynamically linked modules not supported");
@@ -215,7 +218,34 @@ load_dynamic_module(name, pathname)
215218
object *m;
216219
char funcname[258];
217220
dl_funcptr p = NULL;
221+
#ifdef USE_SHLIB
222+
static struct {
223+
dev_t dev;
224+
ino_t ino;
225+
void *handle;
226+
} handles[128];
227+
static int nhandles = 0;
228+
#endif
218229
sprintf(funcname, FUNCNAME_PATTERN, name);
230+
#ifdef USE_SHLIB
231+
if (fp != NULL) {
232+
int i;
233+
struct stat statb;
234+
fstat(fileno(fp), &statb);
235+
for (i = 0; i < nhandles; i++) {
236+
if (statb.st_dev == handles[i].dev &&
237+
statb.st_ino == handles[i].ino) {
238+
p = (dl_funcptr) dlsym(handles[i].handle,
239+
funcname);
240+
goto got_it;
241+
}
242+
}
243+
if (nhandles < 128) {
244+
handles[nhandles].dev = statb.st_dev;
245+
handles[nhandles].ino = statb.st_ino;
246+
}
247+
}
248+
#endif /* USE_SHLIB */
219249
#ifdef USE_MAC_SHARED_LIBRARY
220250
/* Dynamic loading of CFM shared libraries on the Mac */
221251
{
@@ -255,6 +285,8 @@ load_dynamic_module(name, pathname)
255285
err_setstr(ImportError, dlerror());
256286
return NULL;
257287
}
288+
if (fp != NULL && nhandles < 128)
289+
handles[nhandles++].handle = handle;
258290
p = (dl_funcptr) dlsym(handle, funcname);
259291
}
260292
#endif /* USE_SHLIB */
@@ -345,6 +377,7 @@ load_dynamic_module(name, pathname)
345377
perror(funcname);
346378
}
347379
#endif /* hpux */
380+
got_it:
348381
if (p == NULL) {
349382
err_setstr(ImportError,
350383
"dynamic module does not define init function");
@@ -385,7 +418,7 @@ void aix_loaderror(char *pathname)
385418
int errno;
386419
char *errstr;
387420
} load_errtab[] = {
388-
{L_ERROR_TOOMANY, "to many errors, rest skipped."},
421+
{L_ERROR_TOOMANY, "too many errors, rest skipped."},
389422
{L_ERROR_NOLIB, "can't load library:"},
390423
{L_ERROR_UNDEF, "can't find symbol in library:"},
391424
{L_ERROR_RLDBAD,

Python/importdl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ extern struct filedescr {
3737

3838
extern object *import_modules;
3939

40-
extern object *load_dynamic_module PROTO((char *name, char *pathname));
40+
extern object *load_dynamic_module PROTO((char *name, char *pathname, FILE *));
4141

4242
extern int import_maxsuffixsize;

0 commit comments

Comments
 (0)