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