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 | { |
Fred Drake | 6049cb8 | 2002-08-26 21:15:11 +0000 | [diff] [blame] | 24 | PyObject *m; |
Guido van Rossum | ae9e796 | 2001-10-16 20:07:34 +0000 | [diff] [blame] | 25 | char *lastdot, *shortname, *packagecontext, *oldcontext; |
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 | ae9e796 | 2001-10-16 20:07:34 +0000 | [diff] [blame] | 51 | oldcontext = _Py_PackageContext; |
Guido van Rossum | 2e58ff3 | 1997-11-19 18:53:33 +0000 | [diff] [blame] | 52 | _Py_PackageContext = packagecontext; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 53 | (*p)(); |
Guido van Rossum | ae9e796 | 2001-10-16 20:07:34 +0000 | [diff] [blame] | 54 | _Py_PackageContext = oldcontext; |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 55 | if (PyErr_Occurred()) |
| 56 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | ef3d02e | 1997-07-21 14:54:36 +0000 | [diff] [blame] | 58 | m = PyDict_GetItemString(PyImport_GetModuleDict(), name); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 59 | if (m == NULL) { |
Guido van Rossum | 25ce566 | 1997-08-02 03:10:38 +0000 | [diff] [blame] | 60 | PyErr_SetString(PyExc_SystemError, |
| 61 | "dynamic module not initialized properly"); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 62 | return NULL; |
| 63 | } |
Guido van Rossum | 1e61249 | 1996-08-19 22:12:10 +0000 | [diff] [blame] | 64 | /* Remember the filename as the __file__ attribute */ |
Fred Drake | 6049cb8 | 2002-08-26 21:15:11 +0000 | [diff] [blame] | 65 | if (PyModule_AddStringConstant(m, "__file__", pathname) < 0) |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 66 | PyErr_Clear(); /* Not important enough to report */ |
Martin v. Löwis | e81e9b1 | 2003-09-04 18:45:59 +0000 | [diff] [blame] | 67 | |
| 68 | if (_PyImport_FixupExtension(name, pathname) == NULL) |
| 69 | return NULL; |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 70 | if (Py_VerboseFlag) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 71 | PySys_WriteStderr( |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 72 | "import %s # dynamically loaded from %s\n", |
| 73 | name, pathname); |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 74 | Py_INCREF(m); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 75 | return m; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 76 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 77 | |
| 78 | #endif /* HAVE_DYNAMIC_LOADING */ |