Bump version to 0.9.1.
[python/dscho.git] / Python / dynload_beos.c
blob86ba71104afd701c3aa650d3875224baded62984
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 <kernel/image.h>
14 #include <kernel/OS.h>
15 #include <stdlib.h>
16 #include <unistd.h>
18 #include "Python.h"
19 #include "importdl.h"
21 const struct filedescr _PyImport_DynLoadFiletab[] = {
22 {".so", "rb", C_EXTENSION},
23 {"module.so", "rb", C_EXTENSION},
24 {0, 0}
27 #if defined(MAXPATHLEN) && !defined(_SYS_PARAM_H)
28 #undef MAXPATHLEN
29 #endif
31 #ifdef WITH_THREAD
32 #include "pythread.h"
33 static PyThread_type_lock beos_dyn_lock;
34 #endif
36 static PyObject *beos_dyn_images = NULL;
38 /* ----------------------------------------------------------------------
39 * BeOS dynamic loading support
41 * This uses shared libraries, but BeOS has its own way of doing things
42 * (much easier than dlfnc.h, from the look of things). We'll use a
43 * Python Dictionary object to store the images_ids so we can be very
44 * nice and unload them when we exit.
46 * Note that this is thread-safe. Probably irrelevent, because of losing
47 * systems... Python probably disables threads while loading modules.
48 * Note the use of "probably"! Better to be safe than sorry. [chrish]
50 * As of 1.5.1 this should also work properly when you've configured
51 * Python without thread support; the 1.5 version required it, which wasn't
52 * very friendly. Note that I haven't tested it without threading... why
53 * would you want to avoid threads on BeOS? [chrish]
55 * As of 1.5.2, the PyImport_BeImageID() function has been removed; Donn
56 * tells me it's not necessary anymore because of PyCObject_Import().
57 * [chrish]
60 /* Whack an item; the item is an image_id in disguise, so we'll call
61 * unload_add_on() for it.
63 static void beos_nuke_dyn( PyObject *item )
65 status_t retval;
67 if( item ) {
68 image_id id = (image_id)PyInt_AsLong( item );
70 retval = unload_add_on( id );
74 /* atexit() handler that'll call unload_add_on() for every item in the
75 * dictionary.
77 static void beos_cleanup_dyn( void )
79 if( beos_dyn_images ) {
80 int idx;
81 int list_size;
82 PyObject *id_list;
84 #ifdef WITH_THREAD
85 PyThread_acquire_lock( beos_dyn_lock, 1 );
86 #endif
88 id_list = PyDict_Values( beos_dyn_images );
90 list_size = PyList_Size( id_list );
91 for( idx = 0; idx < list_size; idx++ ) {
92 PyObject *the_item;
94 the_item = PyList_GetItem( id_list, idx );
95 beos_nuke_dyn( the_item );
98 PyDict_Clear( beos_dyn_images );
100 #ifdef WITH_THREAD
101 PyThread_free_lock( beos_dyn_lock );
102 #endif
107 * Initialize our dictionary, and the dictionary mutex.
109 static void beos_init_dyn( void )
111 /* We're protected from a race condition here by the atomic init_count
112 * variable.
114 static int32 init_count = 0;
115 int32 val;
117 val = atomic_add( &init_count, 1 );
118 if( beos_dyn_images == NULL && val == 0 ) {
119 beos_dyn_images = PyDict_New();
120 #ifdef WITH_THREAD
121 beos_dyn_lock = PyThread_allocate_lock();
122 #endif
123 atexit( beos_cleanup_dyn );
128 * Add an image_id to the dictionary; the module name of the loaded image
129 * is the key. Note that if the key is already in the dict, we unload
130 * that image; this should allow reload() to work on dynamically loaded
131 * modules (super-keen!).
133 static void beos_add_dyn( char *name, image_id id )
135 int retval;
136 PyObject *py_id;
138 if( beos_dyn_images == NULL ) {
139 beos_init_dyn();
142 #ifdef WITH_THREAD
143 retval = PyThread_acquire_lock( beos_dyn_lock, 1 );
144 #endif
146 /* If there's already an object with this key in the dictionary,
147 * we're doing a reload(), so let's nuke it.
149 py_id = PyDict_GetItemString( beos_dyn_images, name );
150 if( py_id ) {
151 beos_nuke_dyn( py_id );
152 retval = PyDict_DelItemString( beos_dyn_images, name );
155 py_id = PyInt_FromLong( (long)id );
156 if( py_id ) {
157 retval = PyDict_SetItemString( beos_dyn_images, name, py_id );
160 #ifdef WITH_THREAD
161 PyThread_release_lock( beos_dyn_lock );
162 #endif
167 dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
168 const char *pathname, FILE *fp)
170 dl_funcptr p;
171 image_id the_id;
172 status_t retval;
173 char fullpath[PATH_MAX];
174 char funcname[258];
176 if( Py_VerboseFlag ) {
177 printf( "load_add_on( %s )\n", pathname );
180 /* Hmm, this old bug appears to have regenerated itself; if the
181 * path isn't absolute, load_add_on() will fail. Reported to Be
182 * April 21, 1998.
184 if( pathname[0] != '/' ) {
185 (void)getcwd( fullpath, PATH_MAX );
186 (void)strncat( fullpath, "/", PATH_MAX );
187 (void)strncat( fullpath, pathname, PATH_MAX );
189 if( Py_VerboseFlag ) {
190 printf( "load_add_on( %s )\n", fullpath );
192 } else {
193 (void)strcpy( fullpath, pathname );
196 the_id = load_add_on( fullpath );
197 if( the_id < B_NO_ERROR ) {
198 /* It's too bad load_add_on() doesn't set errno or something...
200 char buff[256]; /* hate hard-coded string sizes... */
202 if( Py_VerboseFlag ) {
203 printf( "load_add_on( %s ) failed", fullpath );
206 switch( the_id ) {
207 case B_ERROR:
208 sprintf( buff, "BeOS: Failed to load %.200s", fullpath );
209 break;
210 default:
211 sprintf( buff, "Unknown error loading %.200s", fullpath );
212 break;
215 PyErr_SetString( PyExc_ImportError, buff );
216 return NULL;
219 sprintf(funcname, "init%.200s", shortname);
220 if( Py_VerboseFlag ) {
221 printf( "get_image_symbol( %s )\n", funcname );
224 retval = get_image_symbol( the_id, funcname, B_SYMBOL_TYPE_TEXT, &p );
225 if( retval != B_NO_ERROR || p == NULL ) {
226 /* That's bad, we can't find that symbol in the module...
228 char buff[256]; /* hate hard-coded string sizes... */
230 if( Py_VerboseFlag ) {
231 printf( "get_image_symbol( %s ) failed", funcname );
234 switch( retval ) {
235 case B_BAD_IMAGE_ID:
236 sprintf( buff, "can't load init function for dynamic module: "
237 "Invalid image ID for %.180s", fullpath );
238 break;
239 case B_BAD_INDEX:
240 sprintf( buff, "can't load init function for dynamic module: "
241 "Bad index for %.180s", funcname );
242 break;
243 default:
244 sprintf( buff, "can't load init function for dynamic module: "
245 "Unknown error looking up %.180s", funcname );
246 break;
249 retval = unload_add_on( the_id );
251 PyErr_SetString( PyExc_ImportError, buff );
252 return NULL;
255 /* Save the module name and image ID for later so we can clean up
256 * gracefully.
258 beos_add_dyn( fqname, the_id );
260 return p;