blob: 51069dbd580673d3beda67bced3306f6cc05c750 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001
2/* Support for dynamic loading of extension modules */
3
4#include "dl.h"
5#include <errno.h>
6
7#include "Python.h"
8#include "importdl.h"
9
Guido van Rossum96a8fb71999-12-22 14:09:35 +000010#if defined(__hp9000s300)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011#define FUNCNAME_PATTERN "_PyInit_%.200s"
Guido van Rossum96a8fb71999-12-22 14:09:35 +000012#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000013#define FUNCNAME_PATTERN "PyInit_%.200s"
Guido van Rossum96a8fb71999-12-22 14:09:35 +000014#endif
Guido van Rossum22a1d361999-12-20 21:18:49 +000015
16const struct filedescr _PyImport_DynLoadFiletab[] = {
Thomas Wouters477c8d52006-05-27 19:21:47 +000017 {SHLIB_EXT, "rb", C_EXTENSION},
18 {"module"SHLIB_EXT, "rb", C_EXTENSION},
Guido van Rossum22a1d361999-12-20 21:18:49 +000019 {0, 0}
20};
21
Guido van Rossum96a8fb71999-12-22 14:09:35 +000022dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +000023 const char *pathname, FILE *fp)
24{
25 dl_funcptr p;
26 shl_t lib;
27 int flags;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000028 char funcname[258];
Guido van Rossum22a1d361999-12-20 21:18:49 +000029
30 flags = BIND_FIRST | BIND_DEFERRED;
31 if (Py_VerboseFlag) {
Neal Norwitz40cac732002-11-02 20:58:05 +000032 flags = BIND_FIRST | BIND_IMMEDIATE |
Guido van Rossum22a1d361999-12-20 21:18:49 +000033 BIND_NONFATAL | BIND_VERBOSE;
34 printf("shl_load %s\n",pathname);
35 }
36 lib = shl_load(pathname, flags, 0);
37 /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
38 if (lib == NULL) {
39 char buf[256];
40 if (Py_VerboseFlag)
41 perror(pathname);
Jeremy Hylton518ab1c2001-11-28 20:42:20 +000042 PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
43 pathname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000044 PyErr_SetString(PyExc_ImportError, buf);
45 return NULL;
46 }
Jeremy Hylton518ab1c2001-11-28 20:42:20 +000047 PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN, shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000048 if (Py_VerboseFlag)
49 printf("shl_findsym %s\n", funcname);
Martin v. Löwisfb0da9d2002-01-26 20:03:48 +000050 if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) {
51 shl_unload(lib);
52 p = NULL;
53 }
Guido van Rossum22a1d361999-12-20 21:18:49 +000054 if (p == NULL && Py_VerboseFlag)
55 perror(funcname);
56
57 return p;
58}