Load the subset of the Python C API that we need */
| 167 | |
| 168 | /** Load the subset of the Python C API that we need */ |
| 169 | static bool LoadPythonAPI() |
| 170 | { |
| 171 | static int nInit = -1; |
| 172 | if (nInit >= 0) |
| 173 | return nInit == TRUE; |
| 174 | nInit = FALSE; |
| 175 | |
| 176 | #ifdef LOAD_NOCHECK_WITH_NAME |
| 177 | static LibraryHandle libHandle = nullptr; |
| 178 | const char *pszPythonSO = CPLGetConfigOption("PYTHONSO", nullptr); |
| 179 | #if defined(HAVE_DLFCN_H) && !defined(_WIN32) |
| 180 | |
| 181 | // First try in the current process in case the python symbols would |
| 182 | // be already loaded |
| 183 | libHandle = dlopen(nullptr, RTLD_LAZY); |
| 184 | if (libHandle != nullptr && |
| 185 | dlsym(libHandle, "Py_SetProgramName") != nullptr) |
| 186 | { |
| 187 | CPLDebug("GDAL", "Current process has python symbols loaded"); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | if (libHandle) |
| 192 | dlclose(libHandle); |
| 193 | libHandle = nullptr; |
| 194 | } |
| 195 | |
| 196 | // Then try the user provided shared object name |
| 197 | if (libHandle == nullptr && pszPythonSO != nullptr) |
| 198 | { |
| 199 | // coverity[tainted_string] |
| 200 | libHandle = dlopen(pszPythonSO, RTLD_NOW | RTLD_GLOBAL); |
| 201 | if (libHandle == nullptr) |
| 202 | { |
| 203 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot load %s", |
| 204 | pszPythonSO); |
| 205 | return false; |
| 206 | } |
| 207 | if (dlsym(libHandle, "Py_SetProgramName") == nullptr) |
| 208 | { |
| 209 | CPLError(CE_Failure, CPLE_AppDefined, |
| 210 | "Cannot find Py_SetProgramName symbol in %s", pszPythonSO); |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // Then try the PYTHONSO_DEFAULT if defined at compile time |
| 216 | #ifdef PYTHONSO_DEFAULT |
| 217 | if (libHandle == nullptr) |
| 218 | { |
| 219 | libHandle = dlopen(PYTHONSO_DEFAULT, RTLD_NOW | RTLD_GLOBAL); |
| 220 | if (!libHandle) |
| 221 | { |
| 222 | CPLDebug("GDAL", "%s found", PYTHONSO_DEFAULT); |
| 223 | } |
| 224 | } |
| 225 | #endif |
| 226 |
no test coverage detected