run interpreter without initiating/locking/unlocking GIL in a single thread at a time
| 377 | // run interpreter without initiating/locking/unlocking GIL |
| 378 | // in a single thread at a time |
| 379 | QVariant EmbeddedPython::runInPython(const QString &mname, |
| 380 | const QString &fname, |
| 381 | const QVariantList &args, |
| 382 | int *rv, |
| 383 | QString &tb, |
| 384 | bool ret_python_object) |
| 385 | { |
| 386 | EmbeddedPython::m_mutex.lock(); |
| 387 | PyGILState_STATE gstate = PyGILState_Ensure(); |
| 388 | QVariant res = QVariant(QString()); |
| 389 | PyObject *moduleName = NULL; |
| 390 | PyObject *module = NULL; |
| 391 | PyObject *func = NULL; |
| 392 | PyObject *pyargs = NULL; |
| 393 | PyObject *pyres = NULL; |
| 394 | int idx = 0; |
| 395 | |
| 396 | moduleName = PyUnicode_FromString(mname.toUtf8().constData()); |
| 397 | if (moduleName == NULL) { |
| 398 | *rv = -1; |
| 399 | goto cleanup; |
| 400 | } |
| 401 | |
| 402 | module = PyImport_Import(moduleName); |
| 403 | if (module == NULL) { |
| 404 | *rv = -2; |
| 405 | goto cleanup; |
| 406 | } |
| 407 | |
| 408 | func = PyObject_GetAttrString(module,fname.toUtf8().constData()); |
| 409 | if (func == NULL) { |
| 410 | *rv = -3; |
| 411 | goto cleanup; |
| 412 | } |
| 413 | |
| 414 | if (!PyCallable_Check(func)) { |
| 415 | *rv = -4; |
| 416 | goto cleanup; |
| 417 | } |
| 418 | |
| 419 | // Build up Python argument List from args |
| 420 | pyargs = PyTuple_New(args.size()); |
| 421 | idx = 0; |
| 422 | foreach(QVariant arg, args) { |
| 423 | PyTuple_SetItem(pyargs, idx, QVariantToPyObject(arg)); |
| 424 | idx++; |
| 425 | } |
| 426 | |
| 427 | pyres = PyObject_CallObject(func, pyargs); |
| 428 | if (pyres == NULL) { |
| 429 | *rv = -5; |
| 430 | goto cleanup; |
| 431 | } |
| 432 | |
| 433 | *rv = 0; |
| 434 | |
| 435 | res = PyObjectToQVariant(pyres, ret_python_object); |
| 436 |
no test coverage detected