blob: 4967afc39c12e1f1c29d2630cc496aed6abd60b0 [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)
Steve Doweradc2fb82015-05-23 14:13:41 -070011#define FUNCNAME_PATTERN "_%.20s_%.200s"
Guido van Rossum96a8fb71999-12-22 14:09:35 +000012#else
Steve Doweradc2fb82015-05-23 14:13:41 -070013#define FUNCNAME_PATTERN "%.20s_%.200s"
Guido van Rossum96a8fb71999-12-22 14:09:35 +000014#endif
Guido van Rossum22a1d361999-12-20 21:18:49 +000015
Brett Cannon2657df42012-05-04 15:20:40 -040016const char *_PyImport_DynLoadFiletab[] = {SHLIB_EXT, NULL};
Guido van Rossum22a1d361999-12-20 21:18:49 +000017
Nick Coghland5cacbb2015-05-23 22:24:10 +100018dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix,
19 const char *shortname,
20 const char *pathname, FILE *fp)
Guido van Rossum22a1d361999-12-20 21:18:49 +000021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 dl_funcptr p;
23 shl_t lib;
24 int flags;
25 char funcname[258];
Guido van Rossum22a1d361999-12-20 21:18:49 +000026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 flags = BIND_FIRST | BIND_DEFERRED;
28 if (Py_VerboseFlag) {
29 flags = BIND_FIRST | BIND_IMMEDIATE |
30 BIND_NONFATAL | BIND_VERBOSE;
31 printf("shl_load %s\n",pathname);
32 }
33 lib = shl_load(pathname, flags, 0);
34 /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
35 if (lib == NULL) {
36 char buf[256];
Brett Cannon3dfc22c2012-04-20 15:31:11 -040037 PyObject *pathname_ob = NULL;
38 PyObject *buf_ob = NULL;
39 PyObject *shortname_ob = NULL;
40
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 if (Py_VerboseFlag)
42 perror(pathname);
43 PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
44 pathname);
Brett Cannon3dfc22c2012-04-20 15:31:11 -040045 buf_ob = PyUnicode_FromString(buf);
46 shortname_ob = PyUnicode_FromString(shortname);
47 pathname_ob = PyUnicode_FromString(pathname);
48 PyErr_SetImportError(buf_ob, shortname_ob, pathname_ob);
49 Py_DECREF(buf_ob);
50 Py_DECREF(shortname_ob);
51 Py_DECREF(pathname_ob);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 return NULL;
53 }
Nick Coghland5cacbb2015-05-23 22:24:10 +100054 PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN,
55 prefix, shortname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 if (Py_VerboseFlag)
57 printf("shl_findsym %s\n", funcname);
58 if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) {
59 shl_unload(lib);
60 p = NULL;
61 }
62 if (p == NULL && Py_VerboseFlag)
63 perror(funcname);
Guido van Rossum22a1d361999-12-20 21:18:49 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 return p;
Guido van Rossum22a1d361999-12-20 21:18:49 +000066}