PythonLoader::PythonLoader(QObject *parent) : QObject(parent) { if (!Py_IsInitialized()) { QString sysPath = QCoreApplication::applicationDirPath(); QString programPath = sysPath + "/thirdparty/Python/bin/python3"; wchar_t* programName = new wchar_t[programPath.length() + 1]; programPath.toWCharArray(programName); programName[programPath.length()] = 0; Py_SetProgramName(programName); wprintf(L"python prefix path: %S\n", Py_GetPrefix()); wprintf(L"python full path: %S\n", Py_GetProgramFullPath()); Py_Initialize(); QStringList paths = {sysPath+"/thirdparty/Python/lib/python3.4", sysPath+"/thirdparty/Python/lib/python3.4/plat-linux", sysPath+"/thirdparty/Python/lib/python3.4/lib-dynload", sysPath+"/thirdparty/Python/lib/python3.4/site-packages", sysPath, sysPath+"/thirdparty/Python/lib", sysPath+"/thirdparty/Python/bin"}; QString wholePath = paths.join(":"); PySys_SetPath(wholePath.toStdWString().c_str()); getSipAPI(); PyEval_InitThreads(); PyEval_SaveThread(); } }
// Construct the C++ plugin. PyQt5QmlPlugin::PyQt5QmlPlugin(QObject *parent) : QQmlExtensionPlugin(parent), py_plugin_obj(0), sip(0) { // Make sure the interpreter is initialised. if (!Py_IsInitialized()) { Py_Initialize(); getSipAPI(); #ifdef WITH_THREAD // Make sure we don't have the GIL. PyEval_InitThreads(); PyEval_SaveThread(); #endif } }
/* sipExportedModuleDef * getSipModule( const char * name ) { const sipAPIDef * api = getSipAPI(); if( !api ) return 0; sipExportedModuleDef * module = api->api_find_module( name ); if( !module ) LOG_5( "getSipModule: Unable to lookup module " + QString::fromLatin1(name) + " using api_find_module" ); return module; } */ sipTypeDef * getSipType( const char * /*module_name*/, const char * typeName ) { /* sipExportedModuleDef * module = getSipModule(module_name); if( !module ) return 0; for( int i = module->em_nrtypes - 1; i >= 0; i-- ) { sipTypeDef * type = module->em_types[i]; // We could try checking type->u.td_py_type->name if( //strcmp( type->td_name, typeName ) == 0 || ( strcmp( sipNameFromPool( type->td_module, type->td_cname ), typeName ) == 0 ) ) return type; }*/ const sipTypeDef * type = getSipAPI()->api_find_type(typeName); if( type ) return const_cast<sipTypeDef*>(type); LOG_5( "getSipType: Unabled to find " + QString::fromLatin1(typeName) );// + " in module " + QString::fromLatin1(module_name) ); return 0; }
PyObject * sipWrapRecord( Record * r, bool makeCopy, TableSchema * defaultType ) { PyObject * ret = 0; // First we convert to Record using sip methods static sipTypeDef * recordType = getRecordType( "blur.Stone", "Record" ); sipTypeDef * type = recordType; if( type ) { if( makeCopy ) ret = getSipAPI()->api_convert_from_new_type( new Record(*r), type, NULL ); else { ret = getSipAPI()->api_convert_from_type( r, type, Py_None ); } } else { LOG_1( "Stone.Record not found" ); return 0; } Table * table = r->table(); TableSchema * tableSchema = 0; if( table ) tableSchema = table->schema(); else if( defaultType ) tableSchema = defaultType; else return ret; bool isErr = false; if( tableSchema ) { QString className = tableSchema->className(); // Then we try to find the python class for the particular schema class type // from the desired module set using addSchemaCastModule // BORROWED ref PyObject * dict = getSchemaCastModule( tableSchema->schema() ); if( dict ) { SIP_BLOCK_THREADS // BORROWED ref PyObject * klass = PyDict_GetItemString( dict, className.toLatin1().constData() ); if( klass ) { PyObject * tuple = PyTuple_New(1); // Tuple now holds only ref to ret PyTuple_SET_ITEM( tuple, 0, ret ); PyObject * result = PyObject_CallObject( klass, tuple ); if( result ) { if( PyObject_IsInstance( result, klass ) == 1 ) { ret = result; } else { LOG_1( "Cast Ctor Result is not a subclass of " + className ); Py_INCREF( ret ); Py_DECREF( result ); } } else { LOG_1( "runPythonFunction: Execution Failed, Error Was:\n" ); PyErr_Print(); isErr = true; } Py_DECREF( tuple ); } SIP_UNBLOCK_THREADS if( isErr ) return 0; } else LOG_1( "No cast module set for schema" ); } else LOG_1( "Table has no schema" );