/Python/dynload_beos.c

http://unladen-swallow.googlecode.com/ · C · 254 lines · 156 code · 44 blank · 54 comment · 28 complexity · 3221ebba998c0a14d308c9102f980223 MD5 · raw file

  1. /* Support for dynamic loading of extension modules */
  2. #include <kernel/image.h>
  3. #include <kernel/OS.h>
  4. #include <stdlib.h>
  5. #include "Python.h"
  6. #include "importdl.h"
  7. const struct filedescr _PyImport_DynLoadFiletab[] = {
  8. {".so", "rb", C_EXTENSION},
  9. {"module.so", "rb", C_EXTENSION},
  10. {0, 0}
  11. };
  12. #if defined(MAXPATHLEN) && !defined(_SYS_PARAM_H)
  13. #undef MAXPATHLEN
  14. #endif
  15. #ifdef WITH_THREAD
  16. #include "pythread.h"
  17. static PyThread_type_lock beos_dyn_lock;
  18. #endif
  19. static PyObject *beos_dyn_images = NULL;
  20. /* ----------------------------------------------------------------------
  21. * BeOS dynamic loading support
  22. *
  23. * This uses shared libraries, but BeOS has its own way of doing things
  24. * (much easier than dlfnc.h, from the look of things). We'll use a
  25. * Python Dictionary object to store the images_ids so we can be very
  26. * nice and unload them when we exit.
  27. *
  28. * Note that this is thread-safe. Probably irrelevent, because of losing
  29. * systems... Python probably disables threads while loading modules.
  30. * Note the use of "probably"! Better to be safe than sorry. [chrish]
  31. *
  32. * As of 1.5.1 this should also work properly when you've configured
  33. * Python without thread support; the 1.5 version required it, which wasn't
  34. * very friendly. Note that I haven't tested it without threading... why
  35. * would you want to avoid threads on BeOS? [chrish]
  36. *
  37. * As of 1.5.2, the PyImport_BeImageID() function has been removed; Donn
  38. * tells me it's not necessary anymore because of PyCObject_Import().
  39. * [chrish]
  40. */
  41. /* Whack an item; the item is an image_id in disguise, so we'll call
  42. * unload_add_on() for it.
  43. */
  44. static void beos_nuke_dyn( PyObject *item )
  45. {
  46. status_t retval;
  47. if( item ) {
  48. image_id id = (image_id)PyInt_AsLong( item );
  49. retval = unload_add_on( id );
  50. }
  51. }
  52. /* atexit() handler that'll call unload_add_on() for every item in the
  53. * dictionary.
  54. */
  55. static void beos_cleanup_dyn( void )
  56. {
  57. if( beos_dyn_images ) {
  58. int idx;
  59. int list_size;
  60. PyObject *id_list;
  61. #ifdef WITH_THREAD
  62. PyThread_acquire_lock( beos_dyn_lock, 1 );
  63. #endif
  64. id_list = PyDict_Values( beos_dyn_images );
  65. list_size = PyList_Size( id_list );
  66. for( idx = 0; idx < list_size; idx++ ) {
  67. PyObject *the_item;
  68. the_item = PyList_GetItem( id_list, idx );
  69. beos_nuke_dyn( the_item );
  70. }
  71. PyDict_Clear( beos_dyn_images );
  72. #ifdef WITH_THREAD
  73. PyThread_free_lock( beos_dyn_lock );
  74. #endif
  75. }
  76. }
  77. /*
  78. * Initialize our dictionary, and the dictionary mutex.
  79. */
  80. static void beos_init_dyn( void )
  81. {
  82. /* We're protected from a race condition here by the atomic init_count
  83. * variable.
  84. */
  85. static int32 init_count = 0;
  86. int32 val;
  87. val = atomic_add( &init_count, 1 );
  88. if( beos_dyn_images == NULL && val == 0 ) {
  89. beos_dyn_images = PyDict_New();
  90. #ifdef WITH_THREAD
  91. beos_dyn_lock = PyThread_allocate_lock();
  92. #endif
  93. atexit( beos_cleanup_dyn );
  94. }
  95. }
  96. /*
  97. * Add an image_id to the dictionary; the module name of the loaded image
  98. * is the key. Note that if the key is already in the dict, we unload
  99. * that image; this should allow reload() to work on dynamically loaded
  100. * modules (super-keen!).
  101. */
  102. static void beos_add_dyn( char *name, image_id id )
  103. {
  104. int retval;
  105. PyObject *py_id;
  106. if( beos_dyn_images == NULL ) {
  107. beos_init_dyn();
  108. }
  109. #ifdef WITH_THREAD
  110. retval = PyThread_acquire_lock( beos_dyn_lock, 1 );
  111. #endif
  112. /* If there's already an object with this key in the dictionary,
  113. * we're doing a reload(), so let's nuke it.
  114. */
  115. py_id = PyDict_GetItemString( beos_dyn_images, name );
  116. if( py_id ) {
  117. beos_nuke_dyn( py_id );
  118. retval = PyDict_DelItemString( beos_dyn_images, name );
  119. }
  120. py_id = PyInt_FromLong( (long)id );
  121. if( py_id ) {
  122. retval = PyDict_SetItemString( beos_dyn_images, name, py_id );
  123. }
  124. #ifdef WITH_THREAD
  125. PyThread_release_lock( beos_dyn_lock );
  126. #endif
  127. }
  128. dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
  129. const char *pathname, FILE *fp)
  130. {
  131. dl_funcptr p;
  132. image_id the_id;
  133. status_t retval;
  134. char fullpath[PATH_MAX];
  135. char funcname[258];
  136. if( Py_VerboseFlag ) {
  137. printf( "load_add_on( %s )\n", pathname );
  138. }
  139. /* Hmm, this old bug appears to have regenerated itself; if the
  140. * path isn't absolute, load_add_on() will fail. Reported to Be
  141. * April 21, 1998.
  142. */
  143. if( pathname[0] != '/' ) {
  144. (void)getcwd( fullpath, PATH_MAX );
  145. (void)strncat( fullpath, "/", PATH_MAX );
  146. (void)strncat( fullpath, pathname, PATH_MAX );
  147. if( Py_VerboseFlag ) {
  148. printf( "load_add_on( %s )\n", fullpath );
  149. }
  150. } else {
  151. (void)strcpy( fullpath, pathname );
  152. }
  153. the_id = load_add_on( fullpath );
  154. if( the_id < B_NO_ERROR ) {
  155. /* It's too bad load_add_on() doesn't set errno or something...
  156. */
  157. char buff[256]; /* hate hard-coded string sizes... */
  158. if( Py_VerboseFlag ) {
  159. printf( "load_add_on( %s ) failed", fullpath );
  160. }
  161. if( the_id == B_ERROR )
  162. PyOS_snprintf( buff, sizeof(buff),
  163. "BeOS: Failed to load %.200s",
  164. fullpath );
  165. else
  166. PyOS_snprintf( buff, sizeof(buff),
  167. "Unknown error loading %.200s",
  168. fullpath );
  169. PyErr_SetString( PyExc_ImportError, buff );
  170. return NULL;
  171. }
  172. PyOS_snprintf(funcname, sizeof(funcname), "init%.200s", shortname);
  173. if( Py_VerboseFlag ) {
  174. printf( "get_image_symbol( %s )\n", funcname );
  175. }
  176. retval = get_image_symbol( the_id, funcname, B_SYMBOL_TYPE_TEXT, &p );
  177. if( retval != B_NO_ERROR || p == NULL ) {
  178. /* That's bad, we can't find that symbol in the module...
  179. */
  180. char buff[256]; /* hate hard-coded string sizes... */
  181. if( Py_VerboseFlag ) {
  182. printf( "get_image_symbol( %s ) failed", funcname );
  183. }
  184. switch( retval ) {
  185. case B_BAD_IMAGE_ID:
  186. PyOS_snprintf( buff, sizeof(buff),
  187. "can't load init function for dynamic module: "
  188. "Invalid image ID for %.180s", fullpath );
  189. break;
  190. case B_BAD_INDEX:
  191. PyOS_snprintf( buff, sizeof(buff),
  192. "can't load init function for dynamic module: "
  193. "Bad index for %.180s", funcname );
  194. break;
  195. default:
  196. PyOS_snprintf( buff, sizeof(buff),
  197. "can't load init function for dynamic module: "
  198. "Unknown error looking up %.180s", funcname );
  199. break;
  200. }
  201. retval = unload_add_on( the_id );
  202. PyErr_SetString( PyExc_ImportError, buff );
  203. return NULL;
  204. }
  205. /* Save the module name and image ID for later so we can clean up
  206. * gracefully.
  207. */
  208. beos_add_dyn( fqname, the_id );
  209. return p;
  210. }