Bump version to 0.9.1.
[python/dscho.git] / Python / importdl.c
blob42bf5b447a114fc45d5edec8a542c62186fdfd87
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Support for dynamic loading of extension modules */
13 #include "Python.h"
15 /* ./configure sets HAVE_DYNAMIC_LOADING if dynamic loading of modules is
16 supported on this platform. configure will then compile and link in one
17 of the dynload_*.c files, as appropriate. We will call a function in
18 those modules to get a function pointer to the module's init function.
20 #ifdef HAVE_DYNAMIC_LOADING
22 #include "importdl.h"
24 extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
25 const char *shortname,
26 const char *pathname, FILE *fp);
30 PyObject *
31 _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
33 PyObject *m, *d, *s;
34 char *lastdot, *shortname, *packagecontext;
35 dl_funcptr p;
37 if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
38 Py_INCREF(m);
39 return m;
41 lastdot = strrchr(name, '.');
42 if (lastdot == NULL) {
43 packagecontext = NULL;
44 shortname = name;
46 else {
47 packagecontext = name;
48 shortname = lastdot+1;
51 p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
52 if (PyErr_Occurred())
53 return NULL;
54 if (p == NULL) {
55 PyErr_Format(PyExc_ImportError,
56 "dynamic module does not define init function (init%.200s)",
57 shortname);
58 return NULL;
60 _Py_PackageContext = packagecontext;
61 (*p)();
62 _Py_PackageContext = NULL;
63 if (PyErr_Occurred())
64 return NULL;
65 if (_PyImport_FixupExtension(name, pathname) == NULL)
66 return NULL;
68 m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
69 if (m == NULL) {
70 PyErr_SetString(PyExc_SystemError,
71 "dynamic module not initialized properly");
72 return NULL;
74 /* Remember the filename as the __file__ attribute */
75 d = PyModule_GetDict(m);
76 s = PyString_FromString(pathname);
77 if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0)
78 PyErr_Clear(); /* Not important enough to report */
79 Py_XDECREF(s);
80 if (Py_VerboseFlag)
81 PySys_WriteStderr(
82 "import %s # dynamically loaded from %s\n",
83 name, pathname);
84 Py_INCREF(m);
85 return m;
88 #endif /* HAVE_DYNAMIC_LOADING */