-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy_studxml.cpp
More file actions
209 lines (170 loc) · 6.97 KB
/
py_studxml.cpp
File metadata and controls
209 lines (170 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (c) 2017, Dan Eicher
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "py_studxml.h"
PyTypeObject *PyXmlParsingError_Type;
PyTypeObject *PyXmlSerializationError_Type;
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef studxml_content_moduledef = {
PyModuleDef_HEAD_INIT,
"studxml.content",
NULL,
-1,
NULL,
};
#endif
static PyObject *
initstudxml_content(void)
{
PyObject *m;
#if PY_VERSION_HEX >= 0x03000000
m = PyModule_Create(&studxml_content_moduledef);
#else
m = Py_InitModule3((char *) "studxml.content", NULL, NULL);
#endif
if (m == NULL) {
return NULL;
}
PyModule_AddIntConstant(m, (char *) "empty", xml::content::empty);
PyModule_AddIntConstant(m, (char *) "simple", xml::content::simple);
PyModule_AddIntConstant(m, (char *) "complex", xml::content::complex);
PyModule_AddIntConstant(m, (char *) "mixed", xml::content::mixed);
return m;
}
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef studxml_moduledef = {
PyModuleDef_HEAD_INIT,
"studxml",
NULL,
-1,
NULL,
};
#endif
#if PY_VERSION_HEX >= 0x03000000
#define MOD_ERROR NULL
#define MOD_INIT(name) PyObject * PyInit_##name(void)
#define MOD_RETURN(val) val
#else
#define MOD_ERROR
#define MOD_INIT(name) void init##name(void)
#define MOD_RETURN(val)
#endif
#if defined(__cplusplus)
extern "C"
#endif
#if defined(__GNUC__) && __GNUC__ >= 4
__attribute__ ((visibility("default")))
#endif
MOD_INIT(studxml)
{
PyObject *tmp_value, *submodule, *m;
#if PY_VERSION_HEX >= 0x03000000
m = PyModule_Create(&studxml_moduledef);
#else
m = Py_InitModule3((char *) "studxml", NULL, NULL);
#endif
if (m == NULL) {
return MOD_ERROR;
}
/* Register the 'xml::parser' class */
PyXmlParserMeta_Type.tp_base = Py_TYPE(&PyBaseObject_Type);
/* Some fields need to be manually inheritted from the parent metaclass */
PyXmlParserMeta_Type.tp_traverse = Py_TYPE(&PyBaseObject_Type)->tp_traverse;
PyXmlParserMeta_Type.tp_clear = Py_TYPE(&PyBaseObject_Type)->tp_clear;
PyXmlParserMeta_Type.tp_is_gc = Py_TYPE(&PyBaseObject_Type)->tp_is_gc;
/* PyType tp_setattro is too restrictive */
PyXmlParserMeta_Type.tp_setattro = PyObject_GenericSetAttr;
PyType_Ready(&PyXmlParserMeta_Type);
Py_TYPE(&PyXmlParser_Type) = &PyXmlParserMeta_Type;
if (PyType_Ready(&PyXmlParser_Type)) {
return MOD_ERROR;
}
PyModule_AddObject(m, (char *) "Parser", (PyObject *) &PyXmlParser_Type);
/* Register the 'xml::serializer' class */
if (PyType_Ready(&PyXmlSerializer_Type)) {
return MOD_ERROR;
}
PyModule_AddObject(m, (char *) "Serializer", (PyObject *) &PyXmlSerializer_Type);
/* Register the 'xml::qname' class */
if (PyType_Ready(&PyXmlQname_Type)) {
return MOD_ERROR;
}
PyModule_AddObject(m, (char *) "Qname", (PyObject *) &PyXmlQname_Type);
if (PyType_Ready(&PyXmlParserIter_Type)) {
return MOD_ERROR;
}
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, (char *) "ParserIter", (PyObject *) &PyXmlParserIter_Type);
/* Register the 'xml::parsing' exception */
if ((PyXmlParsingError_Type = (PyTypeObject*) PyErr_NewException((char*)"studxml.ParsingError", NULL, NULL)) == NULL) {
return MOD_ERROR;
}
Py_INCREF((PyObject *) PyXmlParsingError_Type);
PyModule_AddObject(m, (char *) "ParsingError", (PyObject *) PyXmlParsingError_Type);
/* Register the 'xml::serialization' exception */
if ((PyXmlSerializationError_Type = (PyTypeObject*) PyErr_NewException((char*)"studxml.SerializationError", NULL, NULL)) == NULL) {
return MOD_ERROR;
}
Py_INCREF((PyObject *) PyXmlSerializationError_Type);
PyModule_AddObject(m, (char *) "SerializationError", (PyObject *) PyXmlSerializationError_Type);
// xml::parser::start_element
tmp_value = PyLong_FromLong(xml::parser::start_element);
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, "start_element", tmp_value);
Py_DECREF(tmp_value);
// xml::parser::end_element
tmp_value = PyLong_FromLong(xml::parser::end_element);
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, "end_element", tmp_value);
Py_DECREF(tmp_value);
// xml::parser::start_attribute
tmp_value = PyLong_FromLong(xml::parser::start_attribute);
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, "start_attribute", tmp_value);
Py_DECREF(tmp_value);
// xml::parser::end_attribute
tmp_value = PyLong_FromLong(xml::parser::end_attribute);
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, "end_attribute", tmp_value);
Py_DECREF(tmp_value);
// xml::parser::characters
tmp_value = PyLong_FromLong(xml::parser::characters);
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, "characters", tmp_value);
Py_DECREF(tmp_value);
// xml::parser::start_namespace_decl
tmp_value = PyLong_FromLong(xml::parser::start_namespace_decl);
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, "start_namespace_decl", tmp_value);
Py_DECREF(tmp_value);
// xml::parser::end_namespace_decl
tmp_value = PyLong_FromLong(xml::parser::end_namespace_decl);
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, "end_namespace_decl", tmp_value);
Py_DECREF(tmp_value);
// xml::parser::eof
tmp_value = PyLong_FromLong(xml::parser::eof);
PyDict_SetItemString((PyObject *) PyXmlParser_Type.tp_dict, "eof", tmp_value);
Py_DECREF(tmp_value);
submodule = initstudxml_content();
if (submodule == NULL) {
return MOD_ERROR;
}
Py_INCREF(submodule);
PyModule_AddObject(m, (char *) "content", submodule);
return MOD_RETURN(m);
}