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.
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>
21 const struct filedescr _PyImport_DynLoadFiletab
[] = {
22 {".so", "rb", C_EXTENSION
},
23 {"module.so", "rb", C_EXTENSION
},
27 #if defined(MAXPATHLEN) && !defined(_SYS_PARAM_H)
33 static PyThread_type_lock beos_dyn_lock
;
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().
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
)
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
77 static void beos_cleanup_dyn( void )
79 if( beos_dyn_images
) {
85 PyThread_acquire_lock( beos_dyn_lock
, 1 );
88 id_list
= PyDict_Values( beos_dyn_images
);
90 list_size
= PyList_Size( id_list
);
91 for( idx
= 0; idx
< list_size
; idx
++ ) {
94 the_item
= PyList_GetItem( id_list
, idx
);
95 beos_nuke_dyn( the_item
);
98 PyDict_Clear( beos_dyn_images
);
101 PyThread_free_lock( beos_dyn_lock
);
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
114 static int32 init_count
= 0;
117 val
= atomic_add( &init_count
, 1 );
118 if( beos_dyn_images
== NULL
&& val
== 0 ) {
119 beos_dyn_images
= PyDict_New();
121 beos_dyn_lock
= PyThread_allocate_lock();
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
)
138 if( beos_dyn_images
== NULL
) {
143 retval
= PyThread_acquire_lock( beos_dyn_lock
, 1 );
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
);
151 beos_nuke_dyn( py_id
);
152 retval
= PyDict_DelItemString( beos_dyn_images
, name
);
155 py_id
= PyInt_FromLong( (long)id
);
157 retval
= PyDict_SetItemString( beos_dyn_images
, name
, py_id
);
161 PyThread_release_lock( beos_dyn_lock
);
167 dl_funcptr
_PyImport_GetDynLoadFunc(const char *fqname
, const char *shortname
,
168 const char *pathname
, FILE *fp
)
173 char fullpath
[PATH_MAX
];
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
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
);
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
);
208 sprintf( buff
, "BeOS: Failed to load %.200s", fullpath
);
211 sprintf( buff
, "Unknown error loading %.200s", fullpath
);
215 PyErr_SetString( PyExc_ImportError
, buff
);
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
);
236 sprintf( buff
, "can't load init function for dynamic module: "
237 "Invalid image ID for %.180s", fullpath
);
240 sprintf( buff
, "can't load init function for dynamic module: "
241 "Bad index for %.180s", funcname
);
244 sprintf( buff
, "can't load init function for dynamic module: "
245 "Unknown error looking up %.180s", funcname
);
249 retval
= unload_add_on( the_id
);
251 PyErr_SetString( PyExc_ImportError
, buff
);
255 /* Save the module name and image ID for later so we can clean up
258 beos_add_dyn( fqname
, the_id
);