MCPcopy Create free account
hub / github.com/Sigil-Ebook/Sigil / runInPython

Method runInPython

src/EmbedPython/EmbeddedPython.cpp:379–451  ·  view source on GitHub ↗

run interpreter without initiating/locking/unlocking GIL in a single thread at a time

Source from the content-addressed store, hash-verified

377// run interpreter without initiating/locking/unlocking GIL
378// in a single thread at a time
379QVariant 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

Callers 15

XMLPrettyPrintBS4Method · 0.80
WellFormedXMLCheckMethod · 0.80
IsWellFormedXMLMethod · 0.80
ValidateFileMethod · 0.80
GenerateNcxInPythonMethod · 0.80
GetMetadataInPythonMethod · 0.80
GetRepoTagsInPythonMethod · 0.80

Calls 2

QStringClass · 0.70
sizeMethod · 0.45

Tested by

no test coverage detected