Skip to content

Commit

Permalink
Base: Add function to get Python object types for SWIG interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
marioalexis84 authored and chennes committed Jun 22, 2022
1 parent b7cc726 commit 82da006
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/Base/Interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -876,16 +877,32 @@ bool InterpreterSingleton::convertSWIGPointerObj(const char* Module, const char*
throw Base::RuntimeError("No SWIG wrapped library loaded");
}

void InterpreterSingleton::cleanupSWIG(const char* TypeName)
{
PyGILStateLocker locker;
#if (defined(HAVE_SWIG) && (HAVE_SWIG == 1))
namespace Swig_python { extern void cleanupSWIG_T(const char* TypeName); }
Swig_python::cleanupSWIG_T(TypeName);
#else
(void)TypeName;
#endif
}

void InterpreterSingleton::cleanupSWIG(const char* TypeName)
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))
Swig_python::cleanupSWIG_T(TypeName);
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");
}
1 change: 1 addition & 0 deletions src/Base/Interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ class BaseExport InterpreterSingleton
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
Expand Down
28 changes: 28 additions & 0 deletions src/Base/swigpyrun.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
}

0 comments on commit 82da006

Please sign in to comment.