blob: 8dd14b86021b790b39e2ce496a18c37ae4a51ade [file] [log] [blame]
Guido van Rossum1ae940a1995-01-02 19:04:15 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum1ae940a1995-01-02 19:04:15 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum1ae940a1995-01-02 19:04:15 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum1ae940a1995-01-02 19:04:15 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum1ae940a1995-01-02 19:04:15 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum1ae940a1995-01-02 19:04:15 +000029
30******************************************************************/
31
32/* Support for dynamic loading of extension modules */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000033
Guido van Rossum79f25d91997-04-29 20:08:16 +000034#include "Python.h"
Guido van Rossum1ae940a1995-01-02 19:04:15 +000035
Guido van Rossum96a8fb71999-12-22 14:09:35 +000036/* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
37 supported on this platform. configure will then compile and link in one
38 of the dynload_*.c files, as appropriate. We will call a function in
39 those modules to get a function pointer to the module's init function.
Guido van Rossum1ae940a1995-01-02 19:04:15 +000040*/
Guido van Rossum6ea90921999-12-20 21:20:42 +000041#ifdef HAVE_DYNAMIC_LOADING
Guido van Rossumff4af061996-01-12 01:17:50 +000042
Guido van Rossum96a8fb71999-12-22 14:09:35 +000043#include "importdl.h"
44
Guido van Rossum6ea90921999-12-20 21:20:42 +000045extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
Guido van Rossum96a8fb71999-12-22 14:09:35 +000046 const char *shortname,
Guido van Rossum6ea90921999-12-20 21:20:42 +000047 const char *pathname, FILE *fp);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000048
Guido van Rossum38234201996-07-31 17:55:19 +000049
Guido van Rossum6ea90921999-12-20 21:20:42 +000050
Guido van Rossum79f25d91997-04-29 20:08:16 +000051PyObject *
52_PyImport_LoadDynamicModule(name, pathname, fp)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000053 char *name;
54 char *pathname;
Sjoerd Mullenderfbe6d331995-06-12 15:51:34 +000055 FILE *fp;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000056{
Guido van Rossum79f25d91997-04-29 20:08:16 +000057 PyObject *m, *d, *s;
Guido van Rossum2e58ff31997-11-19 18:53:33 +000058 char *lastdot, *shortname, *packagecontext;
Guido van Rossum96a8fb71999-12-22 14:09:35 +000059 dl_funcptr p;
Guido van Rossum6ea90921999-12-20 21:20:42 +000060
Guido van Rossum25ce5661997-08-02 03:10:38 +000061 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
62 Py_INCREF(m);
63 return m;
64 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +000065 lastdot = strrchr(name, '.');
66 if (lastdot == NULL) {
67 packagecontext = NULL;
68 shortname = name;
69 }
70 else {
71 packagecontext = name;
72 shortname = lastdot+1;
73 }
Guido van Rossumd5962ad1996-07-31 22:44:53 +000074
Guido van Rossum96a8fb71999-12-22 14:09:35 +000075 p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
Guido van Rossum6ea90921999-12-20 21:20:42 +000076 if (PyErr_Occurred())
Guido van Rossum1ae940a1995-01-02 19:04:15 +000077 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000078 if (p == NULL) {
Guido van Rossum2e58ff31997-11-19 18:53:33 +000079 PyErr_Format(PyExc_ImportError,
Guido van Rossum96a8fb71999-12-22 14:09:35 +000080 "dynamic module does not define init function (init%.200s)",
81 shortname);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000082 return NULL;
83 }
Guido van Rossum2e58ff31997-11-19 18:53:33 +000084 _Py_PackageContext = packagecontext;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000085 (*p)();
Guido van Rossum2e58ff31997-11-19 18:53:33 +000086 _Py_PackageContext = NULL;
Guido van Rossum25ce5661997-08-02 03:10:38 +000087 if (PyErr_Occurred())
88 return NULL;
89 if (_PyImport_FixupExtension(name, pathname) == NULL)
90 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +000091
Guido van Rossumef3d02e1997-07-21 14:54:36 +000092 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
Guido van Rossum1ae940a1995-01-02 19:04:15 +000093 if (m == NULL) {
Guido van Rossum25ce5661997-08-02 03:10:38 +000094 PyErr_SetString(PyExc_SystemError,
95 "dynamic module not initialized properly");
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096 return NULL;
97 }
Guido van Rossum1e612491996-08-19 22:12:10 +000098 /* Remember the filename as the __file__ attribute */
Guido van Rossum79f25d91997-04-29 20:08:16 +000099 d = PyModule_GetDict(m);
100 s = PyString_FromString(pathname);
101 if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0)
102 PyErr_Clear(); /* Not important enough to report */
103 Py_XDECREF(s);
104 if (Py_VerboseFlag)
Guido van Rossum2f3667a1998-10-12 18:23:55 +0000105 PySys_WriteStderr(
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000106 "import %s # dynamically loaded from %s\n",
107 name, pathname);
Guido van Rossum79f25d91997-04-29 20:08:16 +0000108 Py_INCREF(m);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000109 return m;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000110}
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000111
112#endif /* HAVE_DYNAMIC_LOADING */