blob: e36d608c6dca4481ff1d97b55b079b1c9c207693 [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{
Victor Stinnerc96be812019-05-14 17:34:56 +020022 int flags = BIND_FIRST | BIND_DEFERRED;
Victor Stinnerda7933e2020-04-13 03:04:28 +020023 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +020024 if (verbose) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 flags = BIND_FIRST | BIND_IMMEDIATE |
26 BIND_NONFATAL | BIND_VERBOSE;
27 printf("shl_load %s\n",pathname);
28 }
Victor Stinnerc96be812019-05-14 17:34:56 +020029
30 shl_t lib = shl_load(pathname, flags, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */
32 if (lib == NULL) {
Victor Stinnerc96be812019-05-14 17:34:56 +020033 if (verbose) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 perror(pathname);
Victor Stinnerc96be812019-05-14 17:34:56 +020035 }
36 char buf[256];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s",
38 pathname);
Kevin Adler2d2af322020-10-14 20:53:27 -050039 PyObject *buf_ob = PyUnicode_DecodeFSDefault(buf);
40 if (buf_ob == NULL)
41 return NULL;
Victor Stinnerc96be812019-05-14 17:34:56 +020042 PyObject *shortname_ob = PyUnicode_FromString(shortname);
Kevin Adler2d2af322020-10-14 20:53:27 -050043 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 }
Brett Cannon3dfc22c2012-04-20 15:31:11 -040053 PyErr_SetImportError(buf_ob, shortname_ob, pathname_ob);
54 Py_DECREF(buf_ob);
55 Py_DECREF(shortname_ob);
56 Py_DECREF(pathname_ob);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 return NULL;
58 }
Victor Stinnerc96be812019-05-14 17:34:56 +020059
60 char funcname[258];
Nick Coghland5cacbb2015-05-23 22:24:10 +100061 PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN,
62 prefix, shortname);
Victor Stinnerc96be812019-05-14 17:34:56 +020063 if (verbose) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 printf("shl_findsym %s\n", funcname);
Victor Stinnerc96be812019-05-14 17:34:56 +020065 }
66
67 dl_funcptr p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) {
69 shl_unload(lib);
70 p = NULL;
71 }
Victor Stinnerc96be812019-05-14 17:34:56 +020072 if (p == NULL && verbose) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 perror(funcname);
Victor Stinnerc96be812019-05-14 17:34:56 +020074 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 return p;
Guido van Rossum22a1d361999-12-20 21:18:49 +000076}