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.

154 lines
4.0 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. #ifdef __VMS
  32. ".exe",
  33. ".EXE",
  34. #else /* !__VMS */
  35. "." SOABI ".so",
  36. ".abi" PYTHON_ABI_STRING ".so",
  37. ".so",
  38. #endif /* __VMS */
  39. #endif /* __CYGWIN__ */
  40. NULL,
  41. };
  42. static struct {
  43. dev_t dev;
  44. #ifdef __VMS
  45. ino_t ino[3];
  46. #else
  47. ino_t ino;
  48. #endif
  49. void *handle;
  50. } handles[128];
  51. static int nhandles = 0;
  52. dl_funcptr _PyImport_GetDynLoadFunc(const char *shortname,
  53. const char *pathname, FILE *fp)
  54. {
  55. dl_funcptr p;
  56. void *handle;
  57. char funcname[258];
  58. char pathbuf[260];
  59. int dlopenflags=0;
  60. if (strchr(pathname, '/') == NULL) {
  61. /* Prefix bare filename with "./" */
  62. PyOS_snprintf(pathbuf, sizeof(pathbuf), "./%-.255s", pathname);
  63. pathname = pathbuf;
  64. }
  65. PyOS_snprintf(funcname, sizeof(funcname),
  66. LEAD_UNDERSCORE "PyInit_%.200s", shortname);
  67. if (fp != NULL) {
  68. int i;
  69. struct stat statb;
  70. if (fstat(fileno(fp), &statb) == -1) {
  71. PyErr_SetFromErrno(PyExc_IOError);
  72. return NULL;
  73. }
  74. for (i = 0; i < nhandles; i++) {
  75. if (statb.st_dev == handles[i].dev &&
  76. statb.st_ino == handles[i].ino) {
  77. p = (dl_funcptr) dlsym(handles[i].handle,
  78. funcname);
  79. return p;
  80. }
  81. }
  82. if (nhandles < 128) {
  83. handles[nhandles].dev = statb.st_dev;
  84. #ifdef __VMS
  85. handles[nhandles].ino[0] = statb.st_ino[0];
  86. handles[nhandles].ino[1] = statb.st_ino[1];
  87. handles[nhandles].ino[2] = statb.st_ino[2];
  88. #else
  89. handles[nhandles].ino = statb.st_ino;
  90. #endif
  91. }
  92. }
  93. dlopenflags = PyThreadState_GET()->interp->dlopenflags;
  94. #ifdef __VMS
  95. /* VMS currently don't allow a pathname, use a logical name instead */
  96. /* Concatenate 'python_module_' and shortname */
  97. /* so "import vms.bar" will use the logical python_module_bar */
  98. /* As C module use only one name space this is probably not a */
  99. /* important limitation */
  100. PyOS_snprintf(pathbuf, sizeof(pathbuf), "python_module_%-.200s",
  101. shortname);
  102. pathname = pathbuf;
  103. #endif
  104. handle = dlopen(pathname, dlopenflags);
  105. if (handle == NULL) {
  106. PyObject *mod_name;
  107. PyObject *path;
  108. PyObject *error_ob;
  109. const char *error = dlerror();
  110. if (error == NULL)
  111. error = "unknown dlopen() error";
  112. error_ob = PyUnicode_FromString(error);
  113. if (error_ob == NULL)
  114. return NULL;
  115. mod_name = PyUnicode_FromString(shortname);
  116. if (mod_name == NULL) {
  117. Py_DECREF(error_ob);
  118. return NULL;
  119. }
  120. path = PyUnicode_FromString(pathname);
  121. if (path == NULL) {
  122. Py_DECREF(error_ob);
  123. Py_DECREF(mod_name);
  124. return NULL;
  125. }
  126. PyErr_SetImportError(error_ob, mod_name, path);
  127. Py_DECREF(error_ob);
  128. Py_DECREF(mod_name);
  129. Py_DECREF(path);
  130. return NULL;
  131. }
  132. if (fp != NULL && nhandles < 128)
  133. handles[nhandles++].handle = handle;
  134. p = (dl_funcptr) dlsym(handle, funcname);
  135. return p;
  136. }