home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Python / importdl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-25  |  2.0 KB  |  80 lines

  1.  
  2. /* Support for dynamic loading of extension modules */
  3.  
  4. #include "Python.h"
  5.  
  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.
  10. */
  11. #ifdef HAVE_DYNAMIC_LOADING
  12.  
  13. #include "importdl.h"
  14.  
  15. extern dl_funcptr _PyImport_GetDynLoadFunc(const char *name,
  16.                        const char *shortname,
  17.                        const char *pathname, FILE *fp);
  18.  
  19.  
  20.  
  21. PyObject *
  22. _PyImport_LoadDynamicModule(char *name, char *pathname, FILE *fp)
  23. {
  24.     PyObject *m, *d, *s;
  25.     char *lastdot, *shortname, *packagecontext;
  26.     dl_funcptr p;
  27.  
  28.     if ((m = _PyImport_FindExtension(name, pathname)) != NULL) {
  29.         Py_INCREF(m);
  30.         return m;
  31.     }
  32.     lastdot = strrchr(name, '.');
  33.     if (lastdot == NULL) {
  34.         packagecontext = NULL;
  35.         shortname = name;
  36.     }
  37.     else {
  38.         packagecontext = name;
  39.         shortname = lastdot+1;
  40.     }
  41.  
  42.     p = _PyImport_GetDynLoadFunc(name, shortname, pathname, fp);
  43.     if (PyErr_Occurred())
  44.         return NULL;
  45.     if (p == NULL) {
  46.         PyErr_Format(PyExc_ImportError,
  47.            "dynamic module does not define init function (init%.200s)",
  48.                  shortname);
  49.         return NULL;
  50.     }
  51.     _Py_PackageContext = packagecontext;
  52.     (*p)();
  53.     _Py_PackageContext = NULL;
  54.     if (PyErr_Occurred())
  55.         return NULL;
  56.     if (_PyImport_FixupExtension(name, pathname) == NULL)
  57.         return NULL;
  58.  
  59.     m = PyDict_GetItemString(PyImport_GetModuleDict(), name);
  60.     if (m == NULL) {
  61.         PyErr_SetString(PyExc_SystemError,
  62.                 "dynamic module not initialized properly");
  63.         return NULL;
  64.     }
  65.     /* Remember the filename as the __file__ attribute */
  66.     d = PyModule_GetDict(m);
  67.     s = PyString_FromString(pathname);
  68.     if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0)
  69.         PyErr_Clear(); /* Not important enough to report */
  70.     Py_XDECREF(s);
  71.     if (Py_VerboseFlag)
  72.         PySys_WriteStderr(
  73.             "import %s # dynamically loaded from %s\n",
  74.             name, pathname);
  75.     Py_INCREF(m);
  76.     return m;
  77. }
  78.  
  79. #endif /* HAVE_DYNAMIC_LOADING */
  80.