Base: Add function to get Python object types for SWIG interfaces

This commit is contained in:
marioalexis
2022-06-06 20:28:58 -03:00
committed by Chris Hennes
parent 3a9caa928c
commit adfac77e61
3 changed files with 55 additions and 9 deletions

View File

@@ -825,7 +825,12 @@ int getSWIGVersionFromModule(const std::string& module)
}
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
namespace Swig_python { extern int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own); }
namespace Swig_python {
extern int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own);
extern int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags);
extern void cleanupSWIG_T(const char* TypeName);
extern int getSWIGPointerTypeObj_T(const char* TypeName, PyTypeObject** ptr);
}
#endif
PyObject* InterpreterSingleton::createSWIGPointerObj(const char* Module, const char* TypeName, void* Pointer, int own)
@@ -850,10 +855,6 @@ PyObject* InterpreterSingleton::createSWIGPointerObj(const char* Module, const c
throw Base::RuntimeError("No SWIG wrapped library loaded");
}
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
namespace Swig_python { extern int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags); }
#endif
bool InterpreterSingleton::convertSWIGPointerObj(const char* Module, const char* TypeName, PyObject* obj, void** ptr, int flags)
{
int result = 0;
@@ -876,10 +877,6 @@ bool InterpreterSingleton::convertSWIGPointerObj(const char* Module, const char*
throw Base::RuntimeError("No SWIG wrapped library loaded");
}
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
namespace Swig_python { extern void cleanupSWIG_T(const char* TypeName); }
#endif
void InterpreterSingleton::cleanupSWIG(const char* TypeName)
{
PyGILStateLocker locker;
@@ -889,3 +886,23 @@ void InterpreterSingleton::cleanupSWIG(const char* TypeName)
(void)TypeName;
#endif
}
PyTypeObject* InterpreterSingleton::getSWIGPointerTypeObj(const char* Module, const char* TypeName)
{
int result = 0;
PyTypeObject* proxy = nullptr;
PyGILStateLocker locker;
(void)Module;
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
result = Swig_python::getSWIGPointerTypeObj_T(TypeName, &proxy);
#else
(void)TypeName;
result = -1; // indicates error
#endif
if (result == 0)
return proxy;
// none of the SWIG's succeeded
throw Base::RuntimeError("No SWIG wrapped library loaded");
}

View File

@@ -283,6 +283,7 @@ public:
PyObject* createSWIGPointerObj(const char* Modole, const char* TypeName, void* Pointer, int own);
bool convertSWIGPointerObj(const char* Module, const char* TypeName, PyObject* obj, void** ptr, int flags);
void cleanupSWIG(const char* TypeName);
PyTypeObject* getSWIGPointerTypeObj(const char* Module, const char* TypeName);
//@}
/** @name methods for debugging facility

View File

@@ -51,6 +51,7 @@ int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int
swig_type_info * swig_type = nullptr;
swig_type = SWIG_TypeQuery(TypeName);
if (!swig_type)
throw Base::RuntimeError("Cannot find type information for requested type");
@@ -101,3 +102,30 @@ void cleanupSWIG_T(const char* TypeName)
// Run garbage collector
PyGC_Collect();
}
int getSWIGPointerTypeObj_T(const char* TypeName, PyTypeObject** ptr)
{
swig_module_info *module = SWIG_GetModule(nullptr);
if (!module)
return 1;
swig_type_info * swig_type = nullptr;
SwigPyClientData* clientData = nullptr;
PyTypeObject* pyType = nullptr;
swig_type = SWIG_TypeQuery(TypeName);
if (swig_type)
clientData = static_cast<SwigPyClientData*>(swig_type->clientdata);
if (clientData)
pyType = reinterpret_cast<PyTypeObject*>(clientData->newargs);
if (!pyType) {
std::stringstream str;
str << "SWIG: Cannot find type information for requested type: " << TypeName;
throw Base::RuntimeError(str.str());
}
*ptr = pyType;
return 0;
}