Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 10 | #if defined(__hp9000s300) |
Steve Dower | adc2fb8 | 2015-05-23 14:13:41 -0700 | [diff] [blame] | 11 | #define FUNCNAME_PATTERN "_%.20s_%.200s" |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 12 | #else |
Steve Dower | adc2fb8 | 2015-05-23 14:13:41 -0700 | [diff] [blame] | 13 | #define FUNCNAME_PATTERN "%.20s_%.200s" |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 14 | #endif |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 15 | |
Brett Cannon | 2657df4 | 2012-05-04 15:20:40 -0400 | [diff] [blame] | 16 | const char *_PyImport_DynLoadFiletab[] = {SHLIB_EXT, NULL}; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 17 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 18 | dl_funcptr _PyImport_FindSharedFuncptr(const char *prefix, |
| 19 | const char *shortname, |
| 20 | const char *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 21 | { |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 22 | int flags = BIND_FIRST | BIND_DEFERRED; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 23 | int verbose = _Py_GetConfig()->verbose; |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 24 | if (verbose) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 25 | flags = BIND_FIRST | BIND_IMMEDIATE | |
| 26 | BIND_NONFATAL | BIND_VERBOSE; |
| 27 | printf("shl_load %s\n",pathname); |
| 28 | } |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 29 | |
| 30 | shl_t lib = shl_load(pathname, flags, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */ |
| 32 | if (lib == NULL) { |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 33 | if (verbose) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 34 | perror(pathname); |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 35 | } |
| 36 | char buf[256]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s", |
| 38 | pathname); |
Kevin Adler | 2d2af32 | 2020-10-14 20:53:27 -0500 | [diff] [blame] | 39 | PyObject *buf_ob = PyUnicode_DecodeFSDefault(buf); |
| 40 | if (buf_ob == NULL) |
| 41 | return NULL; |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 42 | PyObject *shortname_ob = PyUnicode_FromString(shortname); |
Kevin Adler | 2d2af32 | 2020-10-14 20:53:27 -0500 | [diff] [blame] | 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 | } |
Brett Cannon | 3dfc22c | 2012-04-20 15:31:11 -0400 | [diff] [blame] | 53 | PyErr_SetImportError(buf_ob, shortname_ob, pathname_ob); |
| 54 | Py_DECREF(buf_ob); |
| 55 | Py_DECREF(shortname_ob); |
| 56 | Py_DECREF(pathname_ob); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | return NULL; |
| 58 | } |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 59 | |
| 60 | char funcname[258]; |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 61 | PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN, |
| 62 | prefix, shortname); |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 63 | if (verbose) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | printf("shl_findsym %s\n", funcname); |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | dl_funcptr p; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 68 | if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) { |
| 69 | shl_unload(lib); |
| 70 | p = NULL; |
| 71 | } |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 72 | if (p == NULL && verbose) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | perror(funcname); |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 74 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | return p; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 76 | } |