blob: 9c325e447d3f485144f4210e9714c5782cd354da [file] [log] [blame]
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001
2/* Support for dynamic loading of extension modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +00005
Guido van Rossum96a8fb71999-12-22 14:09:35 +00006/* ./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 Rossum1ae940a1995-01-02 19:04:15 +000010*/
Guido van Rossum6ea90921999-12-20 21:20:42 +000011#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumff4af061996-01-12 01:17:50 +000012
Guido van Rossum96a8fb71999-12-22 14:09:35 +000013#include "importdl.h"
14
Guido van Rossum6ea90921999-12-20 21:20:42 +000015extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
Guido van Rossum96a8fb71999-12-22 14:09:35 +000016 const char *shortname,
Guido van Rossum6ea90921999-12-20 21:20:42 +000017 const char *pathname, FILE *fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000018
Guido van Rossum38234201996-07-31 17:55:19 +000019
Guido van Rossum6ea90921999-12-20 21:20:42 +000020
Guido van Rossum79f25d91997-04-29 20:08:16 +000021PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000022_PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000023{
Fred Drake6049cb82002-08-26 21:15:11 +000024 PyObject *m;
Guido van Rossumae9e7962001-10-16 20:07:34 +000025 char *lastdot, *shortname, *packagecontext, *oldcontext;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000026 dl_funcptr p;
Guido van Rossum6ea90921999-12-20 21:20:42 +000027
Guido van Rossum25ce5661997-08-02 03:10:38 +000028 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
29 Py_INCREF(m);
30 return m;
31 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +000032 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 Rossumd5962ad1996-07-31 22:44:53 +000041
Guido van Rossum96a8fb71999-12-22 14:09:35 +000042 p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
Guido van Rossum6ea90921999-12-20 21:20:42 +000043 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +000044 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000045 if (p == NULL) {
Guido van Rossum2e58ff31997-11-19 18:53:33 +000046 PyErr_Format(PyExc_ImportError,
Guido van Rossum96a8fb71999-12-22 14:09:35 +000047 "dynamic module does not define init function (init%.200s)",
48 shortname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000049 return NULL;
50 }
Guido van Rossumae9e7962001-10-16 20:07:34 +000051 oldcontext = _Py_PackageContext;
Guido van Rossum2e58ff31997-11-19 18:53:33 +000052 _Py_PackageContext = packagecontext;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053 (*p)();
Guido van Rossumae9e7962001-10-16 20:07:34 +000054 _Py_PackageContext = oldcontext;
Guido van Rossum25ce5661997-08-02 03:10:38 +000055 if (PyErr_Occurred())
56 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000057
Guido van Rossumef3d02e1997-07-21 14:54:36 +000058 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000059 if (m == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000060 PyErr_SetString(PyExc_SystemError,
61 "dynamic module not initialized properly");
Guido van Rossum1ae940a1995-01-02 19:04:15 +000062 return NULL;
63 }
Guido van Rossum1e612491996-08-19 22:12:10 +000064 /* Remember the filename as the __file__ attribute */
Fred Drake6049cb82002-08-26 21:15:11 +000065 if (PyModule_AddStringConstant(m, "__file__", pathname) < 0)
Guido van Rossum79f25d91997-04-29 20:08:16 +000066 PyErr_Clear(); /* Not important enough to report */
Martin v. Löwise81e9b12003-09-04 18:45:59 +000067
68 if (_PyImport_FixupExtension(name, pathname) == NULL)
69 return NULL;
Guido van Rossum79f25d91997-04-29 20:08:16 +000070 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +000071 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +000072 "import %s # dynamically loaded from %s\n",
73 name, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +000074 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000075 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000076}
Guido van Rossum96a8fb71999-12-22 14:09:35 +000077
78#endif /* HAVE_DYNAMIC_LOADING */