comparison pyvamp-main.cpp @ 16:acaa8ff2c606

* Gross hack to find preloadable libpython on linux
author cannam
date Mon, 07 Jul 2008 17:09:07 +0000
parents 3af6b5990ad8
children 5b8167619b76
comparison
equal deleted inserted replaced
15:5e5f2af04e86 16:acaa8ff2c606
102 102
103 103
104 static std::vector<PyPluginAdapter *> adapters; 104 static std::vector<PyPluginAdapter *> adapters;
105 static bool haveScannedPlugins = false; 105 static bool haveScannedPlugins = false;
106 106
107 static bool tryPreload(string name)
108 {
109 // cerr << "Trying to load Python interpreter library \"" << name << "\"...";
110 void *lib = dlopen(name.c_str(), RTLD_NOW | RTLD_GLOBAL);
111 if (!lib) {
112 // char *err = dlerror();
113 // if (err && err[0]) {
114 // cerr << " failed (" << err << ")" << endl;
115 // } else {
116 // cerr << " failed" << endl;
117 // }
118 return false;
119 }
120 // cerr << " succeeded" << endl;
121 return true;
122 }
123
124 static bool preloadPython()
125 {
126 // Linux-specific
127
128 string pyver = Py_GetVersion();
129 int dots = 2;
130 string shortver;
131 for (int i = 0; i < pyver.length(); ++i) {
132 if (pyver[i] == '.') {
133 if (--dots == 0) {
134 shortver = pyver.substr(0, i);
135 break;
136 }
137 }
138 }
139 cerr << "Short version: " << shortver << endl;
140
141 vector<string> pfxs;
142 pfxs.push_back("");
143 pfxs.push_back(string(Py_GetExecPrefix()) + "/lib/");
144 pfxs.push_back(string(Py_GetExecPrefix()) + "/");
145 pfxs.push_back("/usr/lib/");
146 pfxs.push_back("/usr/local/lib/");
147 char buffer[5];
148
149 // hahaha! grossness is like a brother to us
150 for (int pfxidx = 0; pfxidx < pfxs.size(); ++pfxidx) {
151 for (int minor = 8; minor >= 0; --minor) {
152 sprintf(buffer, "%d", minor);
153 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".so." + buffer)) return true;
154 }
155 if (tryPreload(pfxs[pfxidx] + string("libpython") + shortver + ".so")) return true;
156 if (tryPreload(pfxs[pfxidx] + string("libpython.so"))) return true;
157 }
158
159 return false;
160 }
161
162
107 const VampPluginDescriptor 163 const VampPluginDescriptor
108 *vampGetPluginDescriptor(unsigned int version,unsigned int index) 164 *vampGetPluginDescriptor(unsigned int version,unsigned int index)
109 { 165 {
110 if (version < 1) return 0; 166 if (version < 1) return 0;
111 167
115 171
116 if (!haveScannedPlugins) { 172 if (!haveScannedPlugins) {
117 173
118 if (!isPythonInitialized) { 174 if (!isPythonInitialized) {
119 175
120 string pythonPath = 176 if (!preloadPython()) {
121 (string) Py_GetExecPrefix() + pathsep + 177 cerr << "Warning: Could not preload Python."
122 (string) Py_GetProgramName(); 178 << " Dynamic loading in scripts will fail." << endl;
123 179 }
180
181 /*
124 void *pylib = 0; 182 void *pylib = 0;
125 183
126 cerr << "Loading Python Interpreter at: " << pythonPath << endl; 184 cerr << "Loading Python Interpreter at: " << pythonPath << endl;
127 //Preloading the binary allows the load of shared libs 185 //Preloading the binary allows the load of shared libs
128 //TODO: check how to do RTLD_NOW on Windows 186 //TODO: check how to do RTLD_NOW on Windows
131 #else 189 #else
132 pylib = dlopen(pythonPath.c_str(), RTLD_NOW|RTLD_GLOBAL); 190 pylib = dlopen(pythonPath.c_str(), RTLD_NOW|RTLD_GLOBAL);
133 #endif 191 #endif
134 if (!pylib) cerr << "Warning: Could not preload Python." 192 if (!pylib) cerr << "Warning: Could not preload Python."
135 << " Dynamic loading in scripts will fail." << endl; 193 << " Dynamic loading in scripts will fail." << endl;
194 */
136 Py_Initialize(); 195 Py_Initialize();
137 PyEval_InitThreads(); 196 PyEval_InitThreads();
138 } else { 197 } else {
139 //Py_InitializeEx(1); 198 //Py_InitializeEx(1);
140 } 199 }