You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.3 KiB

13 years ago
  1. /* Support for dynamic loading of extension modules */
  2. #include "Python.h"
  3. #include "importdl.h"
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #if defined(__NetBSD__)
  7. #include <sys/param.h>
  8. #if (NetBSD < 199712)
  9. #include <nlist.h>
  10. #include <link.h>
  11. #define dlerror() "error in dynamic linking"
  12. #endif
  13. #endif /* NetBSD */
  14. #ifdef HAVE_DLFCN_H
  15. #include <dlfcn.h>
  16. #endif
  17. #if (defined(__OpenBSD__) || defined(__NetBSD__)) && !defined(__ELF__)
  18. #define LEAD_UNDERSCORE "_"
  19. #else
  20. #define LEAD_UNDERSCORE ""
  21. #endif
  22. /* The .so extension module ABI tag, supplied by the Makefile via
  23. Makefile.pre.in and configure. This is used to discriminate between
  24. incompatible .so files so that extensions for different Python builds can
  25. live in the same directory. E.g. foomodule.cpython-32.so
  26. */
  27. const char *_PyImport_DynLoadFiletab[] = {
  28. #ifdef __CYGWIN__
  29. ".dll",
  30. #else /* !__CYGWIN__ */
  31. "." SOABI ".so",
  32. ".abi" PYTHON_ABI_STRING ".so",
  33. ".so",
  34. #endif /* __CYGWIN__ */
  35. NULL,
  36. };
  37. static struct {
  38. dev_t dev;
  39. ino_t ino;
  40. void *handle;
  41. } handles[128];
  42. static int nhandles = 0;
  43. dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
  44. const char *pathname, FILE *fp)
  45. {
  46. dl_funcptr p;
  47. void *handle;
  48. char funcname[258];
  49. char pathbuf[260];
  50. int dlopenflags=0;
  51. if (strchr(pathname, '/') == NULL) {
  52. /* Prefix bare filename with "./" */
  53. PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
  54. pathname = pathbuf;
  55. }
  56. PyOS_snprintf(funcname, sizeof(funcname),
  57. LEAD_UNDERSCORE "PyInit_%.200s", shortname);
  58. if (fp != NULL) {
  59. int i;
  60. struct stat statb;
  61. if (fstat(fileno(fp), &statb) == -1) {
  62. PyErr_SetFromErrno(PyExc_IOError);
  63. return NULL;
  64. }
  65. for (i = 0; i < nhandles; i++) {
  66. if (statb.st_dev == handles[i].dev &&
  67. statb.st_ino == handles[i].ino) {
  68. p = (dl_funcptr) dlsym(handles[i].handle,
  69. funcname);
  70. return p;
  71. }
  72. }
  73. if (nhandles < 128) {
  74. handles[nhandles].dev = statb.st_dev;
  75. handles[nhandles].ino = statb.st_ino;
  76. }
  77. }
  78. dlopenflags = PyThreadState_GET()->interp->dlopenflags;
  79. handle = dlopen(pathname, dlopenflags);
  80. if (handle == NULL) {
  81. PyObject *mod_name;
  82. PyObject *path;
  83. PyObject *error_ob;
  84. const char *error = dlerror();
  85. if (error == NULL)
  86. error = "unknown dlopen() error";
  87. error_ob = PyUnicode_FromString(error);
  88. if (error_ob == NULL)
  89. return NULL;
  90. mod_name = PyUnicode_FromString(shortname);
  91. if (mod_name == NULL) {
  92. Py_DECREF(error_ob);
  93. return NULL;
  94. }
  95. path = PyUnicode_FromString(pathname);
  96. if (path == NULL) {
  97. Py_DECREF(error_ob);
  98. Py_DECREF(mod_name);
  99. return NULL;
  100. }
  101. PyErr_SetImportError(error_ob, mod_name, path);
  102. Py_DECREF(error_ob);
  103. Py_DECREF(mod_name);
  104. Py_DECREF(path);
  105. return NULL;
  106. }
  107. if (fp != NULL && nhandles < 128)
  108. handles[nhandles++].handle = handle;
  109. p = (dl_funcptr) dlsym(handle, funcname);
  110. return p;
  111. }