1
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
10 #if defined(__hp9000s300)
11 #define FUNCNAME_PATTERN "_%.20s_%.200s"
12 #else
13 #define FUNCNAME_PATTERN "%.20s_%.200s"
14 #endif
15
16 const char *_PyImport_DynLoadFiletab[] = {SHLIB_EXT, ".sl", NULL};
17
_PyImport_FindSharedFuncptr(const char * prefix,const char * shortname,const char * pathname,FILE * fp)18 dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
19 const char *shortname,
20 const char *pathname, FILE *fp)
21 {
22 int flags = BIND_FIRST | BIND_DEFERRED;
23 int verbose = _Py_GetConfig()->verbose;
24 if (verbose) {
25 flags = BIND_FIRST | BIND_IMMEDIATE |
26 BIND_NONFATAL | BIND_VERBOSE;
27 printf("shl_load %s\n",pathname);
28 }
29
30 shl_t lib = shl_load(pathname, flags, 0);
31 /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
32 if (lib == NULL) {
33 if (verbose) {
34 perror(pathname);
35 }
36 char buf[256];
37 PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
38 pathname);
39 PyObject *buf_ob = PyUnicode_DecodeFSDefault(buf);
40 if (buf_ob == NULL)
41 return NULL;
42 PyObject *shortname_ob = PyUnicode_FromString(shortname);
43 if (shortname_ob == NULL) {
44 Py_DECREF(buf_ob);
45 return NULL;
46 }
47 PyObject *pathname_ob = PyUnicode_DecodeFSDefault(pathname);
48 if (pathname_ob == NULL) {
49 Py_DECREF(buf_ob);
50 Py_DECREF(shortname_ob);
51 return NULL;
52 }
53 PyErr_SetImportError(buf_ob, shortname_ob, pathname_ob);
54 Py_DECREF(buf_ob);
55 Py_DECREF(shortname_ob);
56 Py_DECREF(pathname_ob);
57 return NULL;
58 }
59
60 char funcname[258];
61 PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN,
62 prefix, shortname);
63 if (verbose) {
64 printf("shl_findsym %s\n", funcname);
65 }
66
67 dl_funcptr p;
68 if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) {
69 shl_unload(lib);
70 p = NULL;
71 }
72 if (p == NULL && verbose) {
73 perror(funcname);
74 }
75 return p;
76 }
77