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) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 11 | #define FUNCNAME_PATTERN "_PyInit_%.200s" |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 12 | #else |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 13 | #define FUNCNAME_PATTERN "PyInit_%.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 | |
| 16 | const struct filedescr _PyImport_DynLoadFiletab[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | {SHLIB_EXT, "rb", C_EXTENSION}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | {0, 0} |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
Victor Stinner | 42040fb | 2011-02-22 23:16:19 +0000 | [diff] [blame] | 21 | dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | const char *pathname, FILE *fp) |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 23 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | dl_funcptr p; |
| 25 | shl_t lib; |
| 26 | int flags; |
| 27 | char funcname[258]; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 28 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | flags = BIND_FIRST | BIND_DEFERRED; |
| 30 | if (Py_VerboseFlag) { |
| 31 | flags = BIND_FIRST | BIND_IMMEDIATE | |
| 32 | BIND_NONFATAL | BIND_VERBOSE; |
| 33 | printf("shl_load %s\n",pathname); |
| 34 | } |
| 35 | lib = shl_load(pathname, flags, 0); |
| 36 | /* XXX Chuck Blake once wrote that 0 should be BIND_NOSTART? */ |
| 37 | if (lib == NULL) { |
| 38 | char buf[256]; |
Brett Cannon | 3dfc22c | 2012-04-20 15:31:11 -0400 | [diff] [blame] | 39 | PyObject *pathname_ob = NULL; |
| 40 | PyObject *buf_ob = NULL; |
| 41 | PyObject *shortname_ob = NULL; |
| 42 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 43 | if (Py_VerboseFlag) |
| 44 | perror(pathname); |
| 45 | PyOS_snprintf(buf, sizeof(buf), "Failed to load %.200s", |
| 46 | pathname); |
Brett Cannon | 3dfc22c | 2012-04-20 15:31:11 -0400 | [diff] [blame] | 47 | buf_ob = PyUnicode_FromString(buf); |
| 48 | shortname_ob = PyUnicode_FromString(shortname); |
| 49 | pathname_ob = PyUnicode_FromString(pathname); |
| 50 | PyErr_SetImportError(buf_ob, shortname_ob, pathname_ob); |
| 51 | Py_DECREF(buf_ob); |
| 52 | Py_DECREF(shortname_ob); |
| 53 | Py_DECREF(pathname_ob); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
| 56 | PyOS_snprintf(funcname, sizeof(funcname), FUNCNAME_PATTERN, shortname); |
| 57 | if (Py_VerboseFlag) |
| 58 | printf("shl_findsym %s\n", funcname); |
| 59 | if (shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p) == -1) { |
| 60 | shl_unload(lib); |
| 61 | p = NULL; |
| 62 | } |
| 63 | if (p == NULL && Py_VerboseFlag) |
| 64 | perror(funcname); |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | return p; |
Guido van Rossum | 22a1d36 | 1999-12-20 21:18:49 +0000 | [diff] [blame] | 67 | } |