Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 1 | |
| 2 | /* Support for dynamic loading of extension modules */ |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 6 | /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is |
| 7 | supported on this platform. configure will then compile and link in one |
| 8 | of the dynload_*.c files, as appropriate. We will call a function in |
| 9 | those modules to get a function pointer to the module's init function. |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 10 | */ |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 11 | #ifdef HAVE_DYNAMIC_LOADING |
Guido van Rossum | ff4af06 | 1996-01-12 01:17:50 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 13 | #include "importdl.h" |
| 14 | |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 15 | extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 16 | const char *shortname, |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 17 | const char *pathname, FILE *fp); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 3823420 | 1996-07-31 17:55:19 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 21 | PyObject * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 22 | _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 23 | { |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 24 | PyObject *m, *d, *s; |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 25 | char *lastdot, *shortname, *packagecontext; |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 26 | dl_funcptr p; |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 28 | if ((m = _PyImport_FindExtension(name, pathname)) != NULL) { |
| 29 | Py_INCREF(m); |
| 30 | return m; |
| 31 | } |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 32 | lastdot = strrchr(name, '.'); |
| 33 | if (lastdot == NULL) { |
| 34 | packagecontext = NULL; |
| 35 | shortname = name; |
| 36 | } |
| 37 | else { |
| 38 | packagecontext = name; |
| 39 | shortname = lastdot+1; |
| 40 | } |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 42 | p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp); |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 43 | if (PyErr_Occurred()) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 44 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 45 | if (p == NULL) { |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 46 | PyErr_Format(PyExc_ImportError, |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 47 | "dynamic module does not define init function (init%.200s)", |
| 48 | shortname); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 51 | _Py_PackageContext = packagecontext; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 52 | (*p)(); |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 53 | _Py_PackageContext = NULL; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 54 | if (PyErr_Occurred()) |
| 55 | return NULL; |
| 56 | if (_PyImport_FixupExtension(name, pathname) == NULL) |
| 57 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 59 | m = PyDict_GetItemString(PyImport_GetModuleDict(), name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 60 | if (m == NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 61 | PyErr_SetString(PyExc_SystemError, |
| 62 | "dynamic module not initialized properly"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 65 | /* Remember the filename as the __file__ attribute */ |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 66 | d = PyModule_GetDict(m); |
| 67 | s = PyString_FromString(pathname); |
| 68 | if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0) |
| 69 | PyErr_Clear(); /* Not important enough to report */ |
| 70 | Py_XDECREF(s); |
| 71 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 72 | PySys_WriteStderr( |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 73 | "import %s # dynamically loaded from %s\n", |
| 74 | name, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 75 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 76 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 77 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 78 | |
| 79 | #endif /* HAVE_DYNAMIC_LOADING */ |