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 | |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 15 | #ifdef MS_WINDOWS |
| 16 | extern dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname, |
| 17 | PyObject *pathname, FILE *fp); |
| 18 | #else |
Victor Stinner | 42040fb | 2011-02-22 23:16:19 +0000 | [diff] [blame] | 19 | extern dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 20 | const char *pathname, FILE *fp); |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 21 | #endif |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 23 | PyObject * |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 24 | _PyImport_LoadDynamicModule(PyObject *name, PyObject *path, FILE *fp) |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 25 | { |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 26 | PyObject *m = NULL; |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 27 | #ifndef MS_WINDOWS |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 28 | PyObject *pathbytes; |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 29 | #endif |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 30 | PyObject *nameascii; |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 31 | char *namestr, *lastdot, *shortname, *packagecontext, *oldcontext; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | dl_funcptr p0; |
| 33 | PyObject* (*p)(void); |
| 34 | struct PyModuleDef *def; |
Guido van Rossum | 6ea9092 | 1999-12-20 21:20:42 +0000 | [diff] [blame] | 35 | |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 36 | m = _PyImport_FindExtensionObject(name, path); |
Victor Stinner | 9587286 | 2011-03-07 18:20:56 +0100 | [diff] [blame] | 37 | if (m != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | Py_INCREF(m); |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 39 | return m; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | } |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 41 | |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 42 | /* name must be encodable to ASCII because dynamic module must have a |
| 43 | function called "PyInit_NAME", they are written in C, and the C language |
| 44 | doesn't accept non-ASCII identifiers. */ |
| 45 | nameascii = PyUnicode_AsEncodedString(name, "ascii", NULL); |
| 46 | if (nameascii == NULL) |
| 47 | return NULL; |
| 48 | |
| 49 | namestr = PyBytes_AS_STRING(nameascii); |
| 50 | if (namestr == NULL) |
| 51 | goto error; |
| 52 | |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 53 | lastdot = strrchr(namestr, '.'); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 54 | if (lastdot == NULL) { |
| 55 | packagecontext = NULL; |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 56 | shortname = namestr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | } |
| 58 | else { |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 59 | packagecontext = namestr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | shortname = lastdot+1; |
| 61 | } |
Guido van Rossum | d5962ad | 1996-07-31 22:44:53 +0000 | [diff] [blame] | 62 | |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 63 | #ifdef MS_WINDOWS |
| 64 | p0 = _PyImport_GetDynLoadWindows(shortname, path, fp); |
| 65 | #else |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 66 | pathbytes = PyUnicode_EncodeFSDefault(path); |
| 67 | if (pathbytes == NULL) |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 68 | goto error; |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 69 | p0 = _PyImport_GetDynLoadFunc(shortname, |
| 70 | PyBytes_AS_STRING(pathbytes), fp); |
| 71 | Py_DECREF(pathbytes); |
Victor Stinner | 2d32227 | 2011-04-04 23:05:53 +0200 | [diff] [blame] | 72 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 73 | p = (PyObject*(*)(void))p0; |
| 74 | if (PyErr_Occurred()) |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 75 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | if (p == NULL) { |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 77 | PyObject *msg = PyUnicode_FromFormat("dynamic module does not define " |
| 78 | "init function (PyInit_%s)", |
| 79 | shortname); |
Victor Stinner | 8e54b1c | 2013-07-17 21:53:23 +0200 | [diff] [blame] | 80 | if (msg == NULL) |
| 81 | goto error; |
Brett Cannon | f0434e6 | 2012-04-20 15:22:50 -0400 | [diff] [blame] | 82 | PyErr_SetImportError(msg, name, path); |
| 83 | Py_DECREF(msg); |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 84 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | } |
| 86 | oldcontext = _Py_PackageContext; |
| 87 | _Py_PackageContext = packagecontext; |
| 88 | m = (*p)(); |
| 89 | _Py_PackageContext = oldcontext; |
| 90 | if (m == NULL) |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 91 | goto error; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 92 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | PyErr_Format(PyExc_SystemError, |
| 95 | "initialization of %s raised unreported exception", |
| 96 | shortname); |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 97 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | /* Remember pointer to module init function. */ |
| 101 | def = PyModule_GetDef(m); |
Christian Heimes | 7ce57d6 | 2013-07-11 13:02:30 +0200 | [diff] [blame] | 102 | if (def == NULL) { |
| 103 | PyErr_Format(PyExc_SystemError, |
| 104 | "initialization of %s did not return an extension " |
| 105 | "module", shortname); |
Christian Heimes | 848ee09 | 2013-07-11 11:22:21 +0200 | [diff] [blame] | 106 | goto error; |
Christian Heimes | 7ce57d6 | 2013-07-11 13:02:30 +0200 | [diff] [blame] | 107 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | def->m_base.m_init = p; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | /* Remember the filename as the __file__ attribute */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | if (PyModule_AddObject(m, "__file__", path) < 0) |
| 112 | PyErr_Clear(); /* Not important enough to report */ |
Victor Stinner | 49d3f25 | 2010-10-17 01:24:53 +0000 | [diff] [blame] | 113 | else |
| 114 | Py_INCREF(path); |
Martin v. Löwis | e81e9b1 | 2003-09-04 18:45:59 +0000 | [diff] [blame] | 115 | |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 116 | if (_PyImport_FixupExtensionObject(m, name, path) < 0) |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 117 | goto error; |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 118 | Py_DECREF(nameascii); |
Victor Stinner | fefd70c | 2011-03-14 15:54:07 -0400 | [diff] [blame] | 119 | return m; |
Victor Stinner | 65b7eff | 2011-05-07 12:46:05 +0200 | [diff] [blame] | 120 | |
| 121 | error: |
| 122 | Py_DECREF(nameascii); |
| 123 | Py_XDECREF(m); |
| 124 | return NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 125 | } |
Guido van Rossum | 96a8fb7 | 1999-12-22 14:09:35 +0000 | [diff] [blame] | 126 | |
| 127 | #endif /* HAVE_DYNAMIC_LOADING */ |