blob: 86ba71104afd701c3aa650d3875224baded62984 [file] [log] [blame]
Guido van Rossum22a1d361999-12-20 21:18:49 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum22a1d361999-12-20 21:18:49 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum22a1d361999-12-20 21:18:49 +00009******************************************************************/
10
11/* Support for dynamic loading of extension modules */
12
13#include <kernel/image.h>
14#include <kernel/OS.h>
15#include <stdlib.h>
16#include <unistd.h>
17
18#include "Python.h"
19#include "importdl.h"
20
21const struct filedescr _PyImport_DynLoadFiletab[] = {
22 {".so", "rb", C_EXTENSION},
23 {"module.so", "rb", C_EXTENSION},
24 {0, 0}
25};
26
27#if defined(MAXPATHLEN) && !defined(_SYS_PARAM_H)
28#undef MAXPATHLEN
29#endif
30
31#ifdef WITH_THREAD
32#include "pythread.h"
33static PyThread_type_lock beos_dyn_lock;
34#endif
35
36static PyObject *beos_dyn_images = NULL;
37
38/* ----------------------------------------------------------------------
39 * BeOS dynamic loading support
40 *
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.
45 *
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]
49 *
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]
54 *
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]
58 */
59
60/* Whack an item; the item is an image_id in disguise, so we'll call
61 * unload_add_on() for it.
62 */
63static void beos_nuke_dyn( PyObject *item )
64{
65 status_t retval;
66
67 if( item ) {
68 image_id id = (image_id)PyInt_AsLong( item );
69
70 retval = unload_add_on( id );
71 }
72}
73
74/* atexit() handler that'll call unload_add_on() for every item in the
75 * dictionary.
76 */
77static void beos_cleanup_dyn( void )
78{
79 if( beos_dyn_images ) {
80 int idx;
81 int list_size;
82 PyObject *id_list;
83
84#ifdef WITH_THREAD
85 PyThread_acquire_lock( beos_dyn_lock, 1 );
86#endif
87
88 id_list = PyDict_Values( beos_dyn_images );
89
90 list_size = PyList_Size( id_list );
91 for( idx = 0; idx < list_size; idx++ ) {
92 PyObject *the_item;
93
94 the_item = PyList_GetItem( id_list, idx );
95 beos_nuke_dyn( the_item );
96 }
97
98 PyDict_Clear( beos_dyn_images );
99
100#ifdef WITH_THREAD
101 PyThread_free_lock( beos_dyn_lock );
102#endif
103 }
104}
105
106/*
107 * Initialize our dictionary, and the dictionary mutex.
108 */
109static void beos_init_dyn( void )
110{
111 /* We're protected from a race condition here by the atomic init_count
112 * variable.
113 */
114 static int32 init_count = 0;
115 int32 val;
116
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 );
124 }
125}
126
127/*
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!).
132 */
133static void beos_add_dyn( char *name, image_id id )
134{
135 int retval;
136 PyObject *py_id;
137
138 if( beos_dyn_images == NULL ) {
139 beos_init_dyn();
140 }
141
142#ifdef WITH_THREAD
143 retval = PyThread_acquire_lock( beos_dyn_lock, 1 );
144#endif
145
146 /* If there's already an object with this key in the dictionary,
147 * we're doing a reload(), so let's nuke it.
148 */
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 );
153 }
154
155 py_id = PyInt_FromLong( (long)id );
156 if( py_id ) {
157 retval = PyDict_SetItemString( beos_dyn_images, name, py_id );
158 }
159
160#ifdef WITH_THREAD
161 PyThread_release_lock( beos_dyn_lock );
162#endif
163}
164
165
166
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000167dl_funcptr _PyImport_GetDynLoadFunc(const char *fqname, const char *shortname,
Guido van Rossum22a1d361999-12-20 21:18:49 +0000168 const char *pathname, FILE *fp)
169{
170 dl_funcptr p;
171 image_id the_id;
172 status_t retval;
173 char fullpath[PATH_MAX];
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000174 char funcname[258];
Guido van Rossum22a1d361999-12-20 21:18:49 +0000175
176 if( Py_VerboseFlag ) {
177 printf( "load_add_on( %s )\n", pathname );
178 }
179
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.
183 */
184 if( pathname[0] != '/' ) {
185 (void)getcwd( fullpath, PATH_MAX );
186 (void)strncat( fullpath, "/", PATH_MAX );
187 (void)strncat( fullpath, pathname, PATH_MAX );
188
189 if( Py_VerboseFlag ) {
190 printf( "load_add_on( %s )\n", fullpath );
191 }
192 } else {
193 (void)strcpy( fullpath, pathname );
194 }
195
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...
199 */
200 char buff[256]; /* hate hard-coded string sizes... */
201
202 if( Py_VerboseFlag ) {
203 printf( "load_add_on( %s ) failed", fullpath );
204 }
205
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;
213 }
214
215 PyErr_SetString( PyExc_ImportError, buff );
216 return NULL;
217 }
218
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000219 sprintf(funcname, "init%.200s", shortname);
Guido van Rossum22a1d361999-12-20 21:18:49 +0000220 if( Py_VerboseFlag ) {
221 printf( "get_image_symbol( %s )\n", funcname );
222 }
223
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...
227 */
228 char buff[256]; /* hate hard-coded string sizes... */
229
230 if( Py_VerboseFlag ) {
231 printf( "get_image_symbol( %s ) failed", funcname );
232 }
233
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;
247 }
248
249 retval = unload_add_on( the_id );
250
251 PyErr_SetString( PyExc_ImportError, buff );
252 return NULL;
253 }
254
255 /* Save the module name and image ID for later so we can clean up
256 * gracefully.
257 */
Guido van Rossum96a8fb71999-12-22 14:09:35 +0000258 beos_add_dyn( fqname, the_id );
Guido van Rossum22a1d361999-12-20 21:18:49 +0000259
260 return p;
261}