| 337 | } |
| 338 | |
| 339 | int PyModule::load(PyThreadState *pMainThreadState) |
| 340 | { |
| 341 | ceph_assert(pMainThreadState != nullptr); |
| 342 | |
| 343 | const auto subinterpreter_modules_opt = g_conf().get_val<std::string>( |
| 344 | "mgr_subinterpreter_modules" |
| 345 | ); |
| 346 | auto subinterpreter_modules = ceph::split(subinterpreter_modules_opt); |
| 347 | use_main_interpreter = std::count( |
| 348 | subinterpreter_modules.begin(), subinterpreter_modules.end(), "*" |
| 349 | ) == 0 && std::count( |
| 350 | subinterpreter_modules.begin(), subinterpreter_modules.end(), module_name |
| 351 | ) == 0; |
| 352 | |
| 353 | if (use_main_interpreter) { |
| 354 | // Use main interpreter |
| 355 | derr << "Loading module " << module_name << " in main interpreter" << dendl; |
| 356 | pMyThreadState.set(pMainThreadState); |
| 357 | } else { |
| 358 | // Configure sub-interpreter |
| 359 | derr << "Loading module " << module_name << " in sub-interpreter" << dendl; |
| 360 | SafeThreadState sts(pMainThreadState); |
| 361 | Gil gil(sts); |
| 362 | |
| 363 | auto thread_state = Py_NewInterpreter(); |
| 364 | if (thread_state == nullptr) { |
| 365 | derr << "Failed to create python sub-interpreter for '" << module_name << '"' << dendl; |
| 366 | return -EINVAL; |
| 367 | } else { |
| 368 | pMyThreadState.set(thread_state); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Environment is all good, import the external module |
| 373 | { |
| 374 | Gil gil(pMyThreadState); |
| 375 | |
| 376 | int r; |
| 377 | |
| 378 | pPickleModule = PyImport_ImportModule("pickle"); |
| 379 | if (!pPickleModule) { |
| 380 | derr << "Unable to load pickle" << dendl; |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | |
| 384 | r = load_subclass_of("MgrModule", &pClass); |
| 385 | if (r) { |
| 386 | derr << "Class not found in module '" << module_name << "'" << dendl; |
| 387 | return r; |
| 388 | } |
| 389 | |
| 390 | r = load_commands(); |
| 391 | if (r != 0) { |
| 392 | derr << "Missing or invalid COMMANDS attribute in module '" |
| 393 | << module_name << "'" << dendl; |
| 394 | error_string = "Missing or invalid COMMANDS attribute"; |
| 395 | return r; |
| 396 | } |
no test coverage detected