| 1568 | /************************************************************************/ |
| 1569 | |
| 1570 | bool PythonPluginDriver::LoadPlugin() |
| 1571 | { |
| 1572 | CPLMutexHolder oMutexHolder(&m_hMutex); |
| 1573 | if (m_poPlugin) |
| 1574 | return true; |
| 1575 | if (!InitializePythonAndLoadGDALPythonDriverModule()) |
| 1576 | return false; |
| 1577 | GIL_Holder oHolder(false); |
| 1578 | |
| 1579 | CPLString osStr; |
| 1580 | VSILFILE *fp = VSIFOpenL(m_osFilename, "rb"); |
| 1581 | VSIFSeekL(fp, 0, SEEK_END); |
| 1582 | auto nSize = VSIFTellL(fp); |
| 1583 | if (nSize > 10 * 1024 * 1024) |
| 1584 | { |
| 1585 | VSIFCloseL(fp); |
| 1586 | return false; |
| 1587 | } |
| 1588 | VSIFSeekL(fp, 0, SEEK_SET); |
| 1589 | osStr.resize(static_cast<size_t>(nSize)); |
| 1590 | VSIFReadL(&osStr[0], 1, static_cast<size_t>(nSize), fp); |
| 1591 | VSIFCloseL(fp); |
| 1592 | PyObject *poCompiledString = |
| 1593 | Py_CompileString(osStr, m_osFilename, Py_file_input); |
| 1594 | if (poCompiledString == nullptr || PyErr_Occurred()) |
| 1595 | { |
| 1596 | CPLError(CE_Failure, CPLE_AppDefined, "Couldn't compile code:\n%s", |
| 1597 | GetPyExceptionString().c_str()); |
| 1598 | return false; |
| 1599 | } |
| 1600 | const CPLString osPluginModuleName(CPLGetBasenameSafe(m_osFilename)); |
| 1601 | PyObject *poModule = |
| 1602 | PyImport_ExecCodeModule(osPluginModuleName, poCompiledString); |
| 1603 | Py_DecRef(poCompiledString); |
| 1604 | |
| 1605 | if (poModule == nullptr || PyErr_Occurred()) |
| 1606 | { |
| 1607 | CPLError(CE_Failure, CPLE_AppDefined, "%s", |
| 1608 | GetPyExceptionString().c_str()); |
| 1609 | return false; |
| 1610 | } |
| 1611 | |
| 1612 | PyObject *poInstantiate = PyObject_GetAttrString(gpoGDALPythonDriverModule, |
| 1613 | "_instantiate_plugin"); |
| 1614 | CPLAssert(poInstantiate); |
| 1615 | |
| 1616 | PyObject *pyArgs = PyTuple_New(1); |
| 1617 | PyTuple_SetItem(pyArgs, 0, poModule); |
| 1618 | PyObject *poPlugin = PyObject_Call(poInstantiate, pyArgs, nullptr); |
| 1619 | Py_DecRef(pyArgs); |
| 1620 | Py_DecRef(poInstantiate); |
| 1621 | |
| 1622 | if (ErrOccurredEmitCPLError()) |
| 1623 | { |
| 1624 | return false; |
| 1625 | } |
| 1626 | else |
| 1627 | { |
nothing calls this directly
no test coverage detected