FreeCAD

Форк
0
/
swigpyrun.inl 
144 строки · 4.9 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2009 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23

24
int createSWIGPointerObj_T(const char* TypeName, void* obj, PyObject** ptr, int own)
25
{
26
    swig_module_info* module = SWIG_GetModule(nullptr);
27
    if (!module) {
28
        return 1;
29
    }
30

31
    swig_type_info* swig_type = nullptr;
32
    swig_type = SWIG_TypeQuery(TypeName);
33
    if (!swig_type) {
34
        std::stringstream str;
35
        str << "SWIG: Cannot find type information for requested type: " << TypeName;
36
        throw Base::RuntimeError(str.str());
37
    }
38

39
    *ptr = SWIG_NewPointerObj(obj, swig_type, own);
40
    if (!*ptr) {
41
        throw Base::RuntimeError("Cannot convert into requested type");
42
    }
43

44
    // success
45
    return 0;
46
}
47

48
int convertSWIGPointerObj_T(const char* TypeName, PyObject* obj, void** ptr, int flags)
49
{
50
    swig_module_info* module = SWIG_GetModule(nullptr);
51
    if (!module) {
52
        return 1;
53
    }
54

55
    swig_type_info* swig_type = nullptr;
56
    swig_type = SWIG_TypeQuery(TypeName);
57

58
    if (!swig_type) {
59
        throw Base::RuntimeError("Cannot find type information for requested type");
60
    }
61

62
    // return value of 0 is on success
63
    if (SWIG_ConvertPtr(obj, ptr, swig_type, flags)) {
64
        throw Base::RuntimeError("Cannot convert into requested type");
65
    }
66

67
    // success
68
    return 0;
69
}
70

71
void cleanupSWIG_T(const char* TypeName)
72
{
73
    swig_module_info* swig_module = SWIG_GetModule(nullptr);
74
    if (!swig_module) {
75
        return;
76
    }
77

78
    swig_type_info* swig_type = nullptr;
79
    swig_type = SWIG_TypeQuery(TypeName);
80
    if (!swig_type) {
81
        return;
82
    }
83

84
    PyObject *module {}, *dict {};
85
    PyObject* modules = PyImport_GetModuleDict();
86
    module = PyDict_GetItemString(modules, "__builtin__");
87
    if (module && PyModule_Check(module)) {
88
        dict = PyModule_GetDict(module);
89
        PyDict_SetItemString(dict, "_", Py_None);
90
    }
91

92
    module = PyDict_GetItemString(modules, "__main__");
93
    if (module && PyModule_Check(module)) {
94
        PyObject* dict = PyModule_GetDict(module);
95
        if (!dict) {
96
            return;
97
        }
98

99
        Py_ssize_t pos {};
100
        PyObject *key {}, *value {};
101
        pos = 0;
102
        while (PyDict_Next(dict, &pos, &key, &value)) {
103
            if (value != Py_None && PyUnicode_Check(key)) {
104
                void* ptr = nullptr;
105
                if (SWIG_ConvertPtr(value, &ptr, nullptr, 0) == 0) {
106
                    PyDict_SetItem(dict, key, Py_None);
107
                }
108
            }
109
        }
110
    }
111

112
    // Run garbage collector
113
    PyGC_Collect();
114
}
115

116
int getSWIGPointerTypeObj_T(const char* TypeName, PyTypeObject** ptr)
117
{
118
    swig_module_info* module = SWIG_GetModule(nullptr);
119
    if (!module) {
120
        return 1;
121
    }
122

123
    swig_type_info* swig_type = nullptr;
124
    SwigPyClientData* clientData = nullptr;
125
    PyTypeObject* pyType = nullptr;
126
    swig_type = SWIG_TypeQuery(TypeName);
127
    if (swig_type) {
128
        clientData = static_cast<SwigPyClientData*>(swig_type->clientdata);
129
    }
130

131
    if (clientData) {
132
        pyType = reinterpret_cast<PyTypeObject*>(clientData->newargs);
133
    }
134

135
    if (!pyType) {
136
        std::stringstream str;
137
        str << "SWIG: Cannot find type information for requested type: " << TypeName;
138
        throw Base::RuntimeError(str.str());
139
    }
140

141
    *ptr = pyType;
142

143
    return 0;
144
}
145

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.