comparison vampy-main.cpp @ 53:7e59caea821b

* Make a better job of preloading Python, especially when it's in a framework. Go for the Python file in the frameworks directory in preference to any libpythonX.Y.dylib. Particularly, don't try to preload any library without an absolute path until we've exhausted all our framework possibilities (so as to avoid picking up an ancient system library).
author cannam
date Fri, 09 Oct 2009 13:48:25 +0000
parents c1e4f706ca9a
children f0592002c61d
comparison
equal deleted inserted replaced
52:d56f48aafb99 53:7e59caea821b
206 static std::vector<PyPluginAdapter *> adapters; 206 static std::vector<PyPluginAdapter *> adapters;
207 static bool haveScannedPlugins = false; 207 static bool haveScannedPlugins = false;
208 208
209 static bool tryPreload(string name) 209 static bool tryPreload(string name)
210 { 210 {
211 // cerr << "tryPreload: " << name << endl;
211 #ifdef _WIN32 212 #ifdef _WIN32
212 void *lib = LoadLibrary(name.c_str()); 213 void *lib = LoadLibrary(name.c_str());
213 if (!lib) { 214 if (!lib) {
214 return false; 215 return false;
215 } 216 }
216 #else 217 #else
217 void *lib = dlopen(name.c_str(), RTLD_NOW | RTLD_GLOBAL); 218 void *lib = dlopen(name.c_str(), RTLD_NOW | RTLD_GLOBAL);
218 if (!lib) { 219 if (!lib) {
220 perror("dlopen");
219 return false; 221 return false;
220 } 222 }
221 #endif 223 #endif
224 cerr << "Preloaded Python from " << name << endl;
222 return true; 225 return true;
223 } 226 }
224 227
225 static bool preloadPython() 228 static bool preloadPython()
226 { 229 {
243 cerr << "Short version: " << shortver << endl; 246 cerr << "Short version: " << shortver << endl;
244 // this is useful to find out where the loaded library might be loaded from 247 // this is useful to find out where the loaded library might be loaded from
245 cerr << "Python exec prefix: " << Py_GetExecPrefix() << endl; 248 cerr << "Python exec prefix: " << Py_GetExecPrefix() << endl;
246 249
247 vector<string> pfxs; 250 vector<string> pfxs;
251 pfxs.push_back(string(Py_GetExecPrefix()) + "/");
252 pfxs.push_back(string(Py_GetExecPrefix()) + "/lib/");
248 pfxs.push_back(""); 253 pfxs.push_back("");
249 pfxs.push_back(string(Py_GetExecPrefix()) + "/lib/");
250 pfxs.push_back(string(Py_GetExecPrefix()) + "/");
251 pfxs.push_back("/usr/lib/"); 254 pfxs.push_back("/usr/lib/");
252 pfxs.push_back("/usr/local/lib/"); 255 pfxs.push_back("/usr/local/lib/");
253 char buffer[5]; 256 char buffer[5];
254 257
255 // hahaha! grossness is like a brother to us 258 // hahaha! grossness is like a brother to us
256 #ifdef __APPLE__ 259 #ifdef __APPLE__
257 for (size_t pfxidx = 0; pfxidx < pfxs.size(); ++pfxidx) { 260 for (size_t pfxidx = 0; pfxidx < pfxs.size(); ++pfxidx) {
261 // cerr << "prefix: " << pfxs[pfxidx] << endl;
262 if (tryPreload(pfxs[pfxidx] + string("Python"))) return true;
258 for (int minor = 8; minor >= 0; --minor) { 263 for (int minor = 8; minor >= 0; --minor) {
259 sprintf(buffer, "%d", minor); 264 sprintf(buffer, "%d", minor);
260 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".dylib." + buffer)) return true; 265 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".dylib." + buffer)) return true;
261 } 266 }
262 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".dylib")) return true; 267 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".dylib")) return true;