| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own) |
| 27 |
{ |
| 28 |
swig_module_info* module = SWIG_GetModule(nullptr); |
| 29 |
if (!module) { |
| 30 |
return 1; |
| 31 |
} |
| 32 |
|
| 33 |
swig_type_info* swig_type = nullptr; |
| 34 |
swig_type = SWIG_TypeQuery(TypeName); |
| 35 |
if (!swig_type) { |
| 36 |
std::stringstream str; |
| 37 |
str << "SWIG: Cannot find type information for requested type: " << TypeName; |
| 38 |
throw Base::RuntimeError(str.str()); |
| 39 |
} |
| 40 |
|
| 41 |
*ptr = SWIG_NewPointerObj(obj, swig_type, own); |
| 42 |
if (!*ptr) { |
| 43 |
throw Base::RuntimeError("Cannot convert into requested type"); |
| 44 |
} |
| 45 |
|
| 46 |
// success |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags) |
| 51 |
{ |
| 52 |
swig_module_info* module = SWIG_GetModule(nullptr); |
| 53 |
if (!module) { |
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
swig_type_info* swig_type = nullptr; |
| 58 |
swig_type = SWIG_TypeQuery(TypeName); |
| 59 |
|
| 60 |
if (!swig_type) { |
| 61 |
throw Base::RuntimeError("Cannot find type information for requested type"); |
| 62 |
} |
| 63 |
|
| 64 |
// return value of 0 is on success |
| 65 |
if (SWIG_ConvertPtr(obj, ptr, swig_type, flags)) { |
| 66 |
throw Base::RuntimeError("Cannot convert into requested type"); |
| 67 |
} |
| 68 |
|
| 69 |
// success |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
void cleanupSWIG_T(const char* TypeName) |
| 74 |
{ |
| 75 |
swig_module_info* swig_module = SWIG_GetModule(nullptr); |
| 76 |
if (!swig_module) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
swig_type_info* swig_type = nullptr; |
| 81 |
swig_type = SWIG_TypeQuery(TypeName); |
| 82 |
if (!swig_type) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
PyObject *module {}, *dict {}; |
| 87 |
PyObject* modules = PyImport_GetModuleDict(); |
| 88 |
module = PyDict_GetItemString(modules, "__builtin__"); |
| 89 |
if (module && PyModule_Check(module)) { |
| 90 |
dict = PyModule_GetDict(module); |
| 91 |
PyDict_SetItemString(dict, "_", Py_None); |
| 92 |
} |
| 93 |
|
| 94 |
module = PyDict_GetItemString(modules, "__main__"); |
| 95 |
if (module && PyModule_Check(module)) { |
| 96 |
PyObject* dict = PyModule_GetDict(module); |
| 97 |
if (!dict) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
Py_ssize_t pos {}; |
| 102 |
PyObject *key {}, *value {}; |
| 103 |
pos = 0; |
| 104 |
while (PyDict_Next(dict, &pos, &key, &value)) { |
| 105 |
if (value != Py_None && PyUnicode_Check(key)) { |
| 106 |
void* ptr = nullptr; |
| 107 |
if (SWIG_ConvertPtr(value, &ptr, nullptr, 0) == 0) { |
| 108 |
PyDict_SetItem(dict, key, Py_None); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// Run garbage collector |
| 115 |
PyGC_Collect(); |
| 116 |
} |
| 117 |
|
| 118 |
int getSWIGPointerTypeObj_T(const char* TypeName, PyTypeObject** ptr) |
| 119 |
{ |
| 120 |
swig_module_info* module = SWIG_GetModule(nullptr); |
| 121 |
if (!module) { |
| 122 |
return 1; |
| 123 |
} |
| 124 |
|
| 125 |
swig_type_info* swig_type = nullptr; |
| 126 |
SwigPyClientData* clientData = nullptr; |
| 127 |
PyTypeObject* pyType = nullptr; |
| 128 |
swig_type = SWIG_TypeQuery(TypeName); |
| 129 |
if (swig_type) { |
| 130 |
clientData = static_cast<SwigPyClientData*>(swig_type->clientdata); |
| 131 |
} |
| 132 |
|
| 133 |
if (clientData) { |
| 134 |
pyType = reinterpret_cast<PyTypeObject*>(clientData->newargs); |
| 135 |
} |
| 136 |
|
| 137 |
if (!pyType) { |
| 138 |
std::stringstream str; |
| 139 |
str << "SWIG: Cannot find type information for requested type: " << TypeName; |
| 140 |
throw Base::RuntimeError(str.str()); |
| 141 |
} |
| 142 |
|
| 143 |
*ptr = pyType; |
| 144 |
|
| 145 |
return 0; |
| 146 |
} |