This commit was manufactured by cvs2svn to create tag 'r16a1'.
[python/dscho.git] / Python / importdl.c
blob8dd14b86021b790b39e2ce496a18c37ae4a51ade
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Support for dynamic loading of extension modules */
34 #include "Python.h"
36 /* ./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.
41 #ifdef HAVE_DYNAMIC_LOADING
43 #include "importdl.h"
45 extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
46 const char *shortname,
47 const char *pathname, FILE *fp);
51 PyObject *
52 _PyImport_LoadDynamicModule(name, pathname, fp)
53 char *name;
54 char *pathname;
55 FILE *fp;
57 PyObject *m, *d, *s;
58 char *lastdot, *shortname, *packagecontext;
59 dl_funcptr p;
61 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
62 Py_INCREF(m);
63 return m;
65 lastdot = strrchr(name, '.');
66 if (lastdot == NULL) {
67 packagecontext = NULL;
68 shortname = name;
70 else {
71 packagecontext = name;
72 shortname = lastdot+1;
75 p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
76 if (PyErr_Occurred())
77 return NULL;
78 if (p == NULL) {
79 PyErr_Format(PyExc_ImportError,
80 "dynamic module does not define init function (init%.200s)",
81 shortname);
82 return NULL;
84 _Py_PackageContext = packagecontext;
85 (*p)();
86 _Py_PackageContext = NULL;
87 if (PyErr_Occurred())
88 return NULL;
89 if (_PyImport_FixupExtension(name, pathname) == NULL)
90 return NULL;
92 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
93 if (m == NULL) {
94 PyErr_SetString(PyExc_SystemError,
95 "dynamic module not initialized properly");
96 return NULL;
98 /* Remember the filename as the __file__ attribute */
99 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)
105 PySys_WriteStderr(
106 "import %s # dynamically loaded from %s\n",
107 name, pathname);
108 Py_INCREF(m);
109 return m;
112 #endif /* HAVE_DYNAMIC_LOADING */