Mercurial > genshi > mirror
annotate genshi/_speedups.c @ 1034:e02843c0fecc trunk
Add missing boolean attributes to XHTML and HTML serializers (fixes #570).
author | hodgestar |
---|---|
date | Wed, 19 Mar 2014 14:22:22 +0000 |
parents | 657f9ecffa3c |
children | accc8a0cf486 |
rev | line source |
---|---|
541 | 1 /* |
719 | 2 * Copyright (C) 2006-2008 Edgewall Software |
541 | 3 * All rights reserved. |
4 * | |
5 * This software is licensed as described in the file COPYING, which | |
6 * you should have received as part of this distribution. The terms | |
7 * are also available at http://genshi.edgewall.org/wiki/License. | |
8 * | |
9 * This software consists of voluntary contributions made by many | |
10 * individuals. For the exact contribution history, see the revision | |
11 * history and logs, available at http://genshi.edgewall.org/log/. | |
12 */ | |
13 | |
14 #include <Python.h> | |
15 #include <structmember.h> | |
16 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
17 #if PY_MAJOR_VERSION > 2 |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
18 # define IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
19 #elif PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
20 typedef int Py_ssize_t; |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
21 # define PY_SSIZE_T_MAX INT_MAX |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
22 # define PY_SSIZE_T_MIN INT_MIN |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
23 #endif |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
24 |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
25 /* We only use Unicode Strings in this module */ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
26 #ifndef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
27 # define PyObject_Str PyObject_Unicode |
723 | 28 #endif |
29 | |
541 | 30 static PyObject *amp1, *amp2, *lt1, *lt2, *gt1, *gt2, *qt1, *qt2; |
31 static PyObject *stripentities, *striptags; | |
32 | |
33 static void | |
34 init_constants(void) | |
35 { | |
36 PyObject *util = PyImport_ImportModule("genshi.util"); | |
37 stripentities = PyObject_GetAttrString(util, "stripentities"); | |
38 striptags = PyObject_GetAttrString(util, "striptags"); | |
39 Py_DECREF(util); | |
40 | |
41 amp1 = PyUnicode_DecodeASCII("&", 1, NULL); | |
42 amp2 = PyUnicode_DecodeASCII("&", 5, NULL); | |
43 lt1 = PyUnicode_DecodeASCII("<", 1, NULL); | |
44 lt2 = PyUnicode_DecodeASCII("<", 4, NULL); | |
45 gt1 = PyUnicode_DecodeASCII(">", 1, NULL); | |
46 gt2 = PyUnicode_DecodeASCII(">", 4, NULL); | |
47 qt1 = PyUnicode_DecodeASCII("\"", 1, NULL); | |
48 qt2 = PyUnicode_DecodeASCII(""", 5, NULL); | |
49 } | |
50 | |
51 /* Markup class */ | |
52 | |
795 | 53 PyTypeObject MarkupType; /* declared later */ |
541 | 54 |
55 PyDoc_STRVAR(Markup__doc__, | |
56 "Marks a string as being safe for inclusion in HTML/XML output without\n\ | |
57 needing to be escaped."); | |
58 | |
59 static PyObject * | |
60 escape(PyObject *text, int quotes) | |
61 { | |
62 PyObject *args, *ret; | |
63 PyUnicodeObject *in, *out; | |
64 Py_UNICODE *inp, *outp; | |
65 int len, inn, outn; | |
66 | |
67 if (PyObject_TypeCheck(text, &MarkupType)) { | |
68 Py_INCREF(text); | |
69 return text; | |
70 } | |
737
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
71 if (PyObject_HasAttrString(text, "__html__")) { |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
72 ret = PyObject_CallMethod(text, "__html__", NULL); |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
73 args = PyTuple_New(1); |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
74 if (args == NULL) { |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
75 Py_DECREF(ret); |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
76 return NULL; |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
77 } |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
78 PyTuple_SET_ITEM(args, 0, ret); |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
79 ret = MarkupType.tp_new(&MarkupType, args, NULL); |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
80 Py_DECREF(args); |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
81 return ret; |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
82 } |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
83 in = (PyUnicodeObject *) PyObject_Str(text); |
541 | 84 if (in == NULL) { |
85 return NULL; | |
86 } | |
87 /* First we need to figure out how long the escaped string will be */ | |
88 len = inn = 0; | |
89 inp = in->str; | |
90 while (*(inp) || in->length > inp - in->str) { | |
91 switch (*inp++) { | |
92 case '&': len += 5; inn++; break; | |
93 case '"': len += quotes ? 5 : 1; inn += quotes ? 1 : 0; break; | |
94 case '<': | |
95 case '>': len += 4; inn++; break; | |
96 default: len++; | |
97 } | |
98 } | |
99 | |
100 /* Do we need to escape anything at all? */ | |
101 if (!inn) { | |
102 args = PyTuple_New(1); | |
103 if (args == NULL) { | |
104 Py_DECREF((PyObject *) in); | |
105 return NULL; | |
106 } | |
107 PyTuple_SET_ITEM(args, 0, (PyObject *) in); | |
108 ret = MarkupType.tp_new(&MarkupType, args, NULL); | |
109 Py_DECREF(args); | |
110 return ret; | |
111 } | |
112 | |
113 out = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, len); | |
114 if (out == NULL) { | |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
115 Py_DECREF((PyObject *) in); |
541 | 116 return NULL; |
117 } | |
118 | |
119 outn = 0; | |
120 inp = in->str; | |
121 outp = out->str; | |
122 while (*(inp) || in->length > inp - in->str) { | |
123 if (outn == inn) { | |
124 /* copy rest of string if we have already replaced everything */ | |
125 Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str)); | |
126 break; | |
127 } | |
128 switch (*inp) { | |
129 case '&': | |
130 Py_UNICODE_COPY(outp, ((PyUnicodeObject *) amp2)->str, 5); | |
131 outp += 5; | |
132 outn++; | |
133 break; | |
134 case '"': | |
135 if (quotes) { | |
136 Py_UNICODE_COPY(outp, ((PyUnicodeObject *) qt2)->str, 5); | |
137 outp += 5; | |
138 outn++; | |
139 } else { | |
140 *outp++ = *inp; | |
141 } | |
142 break; | |
143 case '<': | |
144 Py_UNICODE_COPY(outp, ((PyUnicodeObject *) lt2)->str, 4); | |
145 outp += 4; | |
146 outn++; | |
147 break; | |
148 case '>': | |
149 Py_UNICODE_COPY(outp, ((PyUnicodeObject *) gt2)->str, 4); | |
150 outp += 4; | |
151 outn++; | |
152 break; | |
153 default: | |
154 *outp++ = *inp; | |
155 } | |
156 inp++; | |
157 } | |
158 | |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
159 Py_DECREF((PyObject *) in); |
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
160 |
541 | 161 args = PyTuple_New(1); |
162 if (args == NULL) { | |
163 Py_DECREF((PyObject *) out); | |
164 return NULL; | |
165 } | |
166 PyTuple_SET_ITEM(args, 0, (PyObject *) out); | |
167 ret = MarkupType.tp_new(&MarkupType, args, NULL); | |
168 Py_DECREF(args); | |
169 return ret; | |
170 } | |
171 | |
172 PyDoc_STRVAR(escape__doc__, | |
173 "Create a Markup instance from a string and escape special characters\n\ | |
174 it may contain (<, >, & and \").\n\ | |
175 \n\ | |
547 | 176 >>> escape('\"1 < 2\"')\n\ |
177 <Markup u'"1 < 2"'>\n\ | |
178 \n\ | |
541 | 179 If the `quotes` parameter is set to `False`, the \" character is left\n\ |
180 as is. Escaping quotes is generally only required for strings that are\n\ | |
547 | 181 to be used in attribute values.\n\ |
182 \n\ | |
183 >>> escape('\"1 < 2\"', quotes=False)\n\ | |
184 <Markup u'\"1 < 2\"'>\n\ | |
185 \n\ | |
186 :param text: the text to escape\n\ | |
187 :param quotes: if ``True``, double quote characters are escaped in\n\ | |
188 addition to the other special characters\n\ | |
189 :return: the escaped `Markup` string\n\ | |
190 :rtype: `Markup`\n\ | |
191 "); | |
541 | 192 |
193 static PyObject * | |
194 Markup_escape(PyTypeObject* type, PyObject *args, PyObject *kwds) | |
195 { | |
196 static char *kwlist[] = {"text", "quotes", 0}; | |
197 PyObject *text = NULL; | |
198 char quotes = 1; | |
199 | |
200 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|b", kwlist, &text, "es)) { | |
201 return NULL; | |
202 } | |
203 if (PyObject_Not(text)) { | |
943
657f9ecffa3c
Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents:
930
diff
changeset
|
204 args = PyTuple_New(0); |
657f9ecffa3c
Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents:
930
diff
changeset
|
205 if (args == NULL) |
657f9ecffa3c
Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents:
930
diff
changeset
|
206 return NULL; |
657f9ecffa3c
Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents:
930
diff
changeset
|
207 text = type->tp_new(type, args, NULL); |
657f9ecffa3c
Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents:
930
diff
changeset
|
208 Py_DECREF(args); |
657f9ecffa3c
Fix bug in _speedups where it differed from behaviour of Python implementation and add a test for this case (fixes #439). Fix and test contributed by cboos.
hodgestar
parents:
930
diff
changeset
|
209 return text; |
541 | 210 } |
211 if (PyObject_TypeCheck(text, type)) { | |
212 Py_INCREF(text); | |
213 return text; | |
214 } | |
215 return escape(text, quotes); | |
216 } | |
217 | |
737
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
218 static PyObject * |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
219 Markup_html(PyObject *self) |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
220 { |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
221 Py_INCREF(self); |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
222 return self; |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
223 } |
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
224 |
547 | 225 PyDoc_STRVAR(join__doc__, |
226 "Return a `Markup` object which is the concatenation of the strings\n\ | |
227 in the given sequence, where this `Markup` object is the separator\n\ | |
228 between the joined elements.\n\ | |
229 \n\ | |
230 Any element in the sequence that is not a `Markup` instance is\n\ | |
231 automatically escaped.\n\ | |
232 \n\ | |
233 :param seq: the sequence of strings to join\n\ | |
234 :param escape_quotes: whether double quote characters in the elements\n\ | |
235 should be escaped\n\ | |
236 :return: the joined `Markup` object\n\ | |
237 :rtype: `Markup`\n\ | |
238 :see: `escape`\n\ | |
239 "); | |
240 | |
541 | 241 static PyObject * |
242 Markup_join(PyObject *self, PyObject *args, PyObject *kwds) | |
243 { | |
244 static char *kwlist[] = {"seq", "escape_quotes", 0}; | |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
245 PyObject *seq = NULL, *seq2, *tmp, *tmp2; |
541 | 246 char quotes = 1; |
795 | 247 Py_ssize_t n; |
248 int i; | |
541 | 249 |
250 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|b", kwlist, &seq, "es)) { | |
251 return NULL; | |
252 } | |
253 if (!PySequence_Check(seq)) { | |
254 return NULL; | |
255 } | |
256 n = PySequence_Size(seq); | |
257 if (n < 0) { | |
258 return NULL; | |
259 } | |
260 seq2 = PyTuple_New(n); | |
261 if (seq2 == NULL) { | |
262 return NULL; | |
263 } | |
264 for (i = 0; i < n; i++) { | |
265 tmp = PySequence_GetItem(seq, i); | |
266 if (tmp == NULL) { | |
267 Py_DECREF(seq2); | |
268 return NULL; | |
269 } | |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
270 tmp2 = escape(tmp, quotes); |
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
271 if (tmp2 == NULL) { |
541 | 272 Py_DECREF(seq2); |
273 return NULL; | |
274 } | |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
275 PyTuple_SET_ITEM(seq2, i, tmp2); |
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
276 Py_DECREF(tmp); |
541 | 277 } |
278 tmp = PyUnicode_Join(self, seq2); | |
279 Py_DECREF(seq2); | |
280 if (tmp == NULL) | |
281 return NULL; | |
282 args = PyTuple_New(1); | |
283 if (args == NULL) { | |
284 Py_DECREF(tmp); | |
285 return NULL; | |
286 } | |
287 PyTuple_SET_ITEM(args, 0, tmp); | |
288 tmp = MarkupType.tp_new(&MarkupType, args, NULL); | |
289 Py_DECREF(args); | |
290 return tmp; | |
291 } | |
292 | |
293 static PyObject * | |
294 Markup_add(PyObject *self, PyObject *other) | |
295 { | |
296 PyObject *tmp, *tmp2, *args, *ret; | |
297 if (PyObject_TypeCheck(self, &MarkupType)) { | |
298 tmp = escape(other, 1); | |
299 if (tmp == NULL) | |
300 return NULL; | |
301 tmp2 = PyUnicode_Concat(self, tmp); | |
302 } else { // __radd__ | |
303 tmp = escape(self, 1); | |
304 if (tmp == NULL) | |
305 return NULL; | |
306 tmp2 = PyUnicode_Concat(tmp, other); | |
307 } | |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
308 Py_DECREF(tmp); |
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
309 if (tmp2 == NULL) |
541 | 310 return NULL; |
311 args = PyTuple_New(1); | |
312 if (args == NULL) { | |
313 Py_DECREF(tmp2); | |
314 return NULL; | |
315 } | |
316 PyTuple_SET_ITEM(args, 0, tmp2); | |
317 ret = MarkupType.tp_new(&MarkupType, args, NULL); | |
318 Py_DECREF(args); | |
319 return ret; | |
320 } | |
321 | |
322 static PyObject * | |
323 Markup_mod(PyObject *self, PyObject *args) | |
324 { | |
325 PyObject *tmp, *tmp2, *ret, *args2; | |
795 | 326 int i; |
327 Py_ssize_t nargs = 0; | |
713
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
328 PyObject *kwds = NULL; |
541 | 329 |
713
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
330 if (PyDict_Check(args)) { |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
331 kwds = args; |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
332 } |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
333 if (kwds && PyDict_Size(kwds)) { |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
334 PyObject *kwcopy, *key, *value; |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
335 Py_ssize_t pos = 0; |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
336 |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
337 kwcopy = PyDict_Copy( kwds ); |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
338 if (kwcopy == NULL) { |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
339 return NULL; |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
340 } |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
341 while (PyDict_Next(kwcopy, &pos, &key, &value)) { |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
342 tmp = escape(value, 1); |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
343 if (tmp == NULL) { |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
344 Py_DECREF(kwcopy); |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
345 return NULL; |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
346 } |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
347 if (PyDict_SetItem(kwcopy, key, tmp) < 0) { |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
348 Py_DECREF(tmp); |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
349 Py_DECREF(kwcopy); |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
350 return NULL; |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
351 } |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
352 } |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
353 tmp = PyUnicode_Format(self, kwcopy); |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
354 Py_DECREF(kwcopy); |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
355 if (tmp == NULL) { |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
356 return NULL; |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
357 } |
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
358 } else if (PyTuple_Check(args)) { |
541 | 359 nargs = PyTuple_GET_SIZE(args); |
360 args2 = PyTuple_New(nargs); | |
361 if (args2 == NULL) { | |
362 return NULL; | |
363 } | |
364 for (i = 0; i < nargs; i++) { | |
365 tmp = escape(PyTuple_GET_ITEM(args, i), 1); | |
366 if (tmp == NULL) { | |
367 Py_DECREF(args2); | |
368 return NULL; | |
369 } | |
370 PyTuple_SET_ITEM(args2, i, tmp); | |
371 } | |
372 tmp = PyUnicode_Format(self, args2); | |
373 Py_DECREF(args2); | |
374 if (tmp == NULL) { | |
375 return NULL; | |
376 } | |
377 } else { | |
378 tmp2 = escape(args, 1); | |
379 if (tmp2 == NULL) { | |
380 return NULL; | |
381 } | |
382 tmp = PyUnicode_Format(self, tmp2); | |
383 Py_DECREF(tmp2); | |
384 if (tmp == NULL) { | |
385 return NULL; | |
386 } | |
387 } | |
388 args = PyTuple_New(1); | |
389 if (args == NULL) { | |
390 Py_DECREF(tmp); | |
391 return NULL; | |
392 } | |
393 PyTuple_SET_ITEM(args, 0, tmp); | |
394 ret = PyUnicode_Type.tp_new(&MarkupType, args, NULL); | |
395 Py_DECREF(args); | |
396 return ret; | |
397 } | |
398 | |
399 static PyObject * | |
400 Markup_mul(PyObject *self, PyObject *num) | |
401 { | |
402 PyObject *unicode, *result, *args; | |
403 | |
404 if (PyObject_TypeCheck(self, &MarkupType)) { | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
405 unicode = PyObject_Str(self); |
541 | 406 if (unicode == NULL) return NULL; |
407 result = PyNumber_Multiply(unicode, num); | |
408 } else { // __rmul__ | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
409 unicode = PyObject_Str(num); |
541 | 410 if (unicode == NULL) return NULL; |
411 result = PyNumber_Multiply(unicode, self); | |
412 } | |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
413 Py_DECREF(unicode); |
541 | 414 |
415 if (result == NULL) return NULL; | |
416 args = PyTuple_New(1); | |
417 if (args == NULL) { | |
418 Py_DECREF(result); | |
419 return NULL; | |
420 } | |
421 PyTuple_SET_ITEM(args, 0, result); | |
422 result = PyUnicode_Type.tp_new(&MarkupType, args, NULL); | |
423 Py_DECREF(args); | |
424 | |
425 return result; | |
426 } | |
427 | |
428 static PyObject * | |
429 Markup_repr(PyObject *self) | |
430 { | |
431 PyObject *format, *result, *args; | |
432 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
433 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
434 format = PyUnicode_FromString("<Markup %r>"); |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
435 #else |
541 | 436 format = PyString_FromString("<Markup %r>"); |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
437 #endif |
541 | 438 if (format == NULL) return NULL; |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
439 result = PyObject_Str(self); |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
440 if (result == NULL) { |
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
441 Py_DECREF(format); |
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
442 return NULL; |
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
443 } |
541 | 444 args = PyTuple_New(1); |
445 if (args == NULL) { | |
665
3ee92ec99ad9
Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents:
547
diff
changeset
|
446 Py_DECREF(format); |
541 | 447 Py_DECREF(result); |
448 return NULL; | |
449 } | |
450 PyTuple_SET_ITEM(args, 0, result); | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
451 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
452 result = PyUnicode_Format(format, args); |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
453 #else |
541 | 454 result = PyString_Format(format, args); |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
455 #endif |
681
3e7cd32c9411
Fix another memory leak in the C speedups code. Thanks to Erik Bray for finding this one and providing a patch. Closes #166 (again).
cmlenz
parents:
665
diff
changeset
|
456 Py_DECREF(format); |
541 | 457 Py_DECREF(args); |
458 return result; | |
459 } | |
460 | |
461 PyDoc_STRVAR(unescape__doc__, | |
547 | 462 "Reverse-escapes &, <, >, and \" and returns a `unicode` object.\n\ |
463 \n\ | |
464 >>> Markup('1 < 2').unescape()\n\ | |
465 u'1 < 2'\n\ | |
466 \n\ | |
467 :return: the unescaped string\n\ | |
468 :rtype: `unicode`\n\ | |
469 :see: `genshi.core.unescape`\n\ | |
470 "); | |
541 | 471 |
472 static PyObject * | |
473 Markup_unescape(PyObject* self) | |
474 { | |
475 PyObject *tmp, *tmp2; | |
476 | |
477 tmp = PyUnicode_Replace(self, qt2, qt1, -1); | |
478 if (tmp == NULL) return NULL; | |
479 tmp2 = PyUnicode_Replace(tmp, gt2, gt1, -1); | |
480 Py_DECREF(tmp); | |
481 if (tmp2 == NULL) return NULL; | |
482 tmp = PyUnicode_Replace(tmp2, lt2, lt1, -1); | |
483 Py_DECREF(tmp2); | |
484 if (tmp == NULL) return NULL; | |
485 tmp2 = PyUnicode_Replace(tmp, amp2, amp1, -1); | |
486 Py_DECREF(tmp); | |
487 return tmp2; | |
488 } | |
489 | |
490 PyDoc_STRVAR(stripentities__doc__, | |
491 "Return a copy of the text with any character or numeric entities\n\ | |
492 replaced by the equivalent UTF-8 characters.\n\ | |
493 \n\ | |
494 If the `keepxmlentities` parameter is provided and evaluates to `True`,\n\ | |
547 | 495 the core XML entities (``&``, ``'``, ``>``, ``<`` and\n\ |
496 ``"``) are not stripped.\n\ | |
497 \n\ | |
498 :return: a `Markup` instance with entities removed\n\ | |
499 :rtype: `Markup`\n\ | |
500 :see: `genshi.util.stripentities`\n\ | |
501 "); | |
541 | 502 |
503 static PyObject * | |
504 Markup_stripentities(PyObject* self, PyObject *args, PyObject *kwds) | |
505 { | |
506 static char *kwlist[] = {"keepxmlentities", 0}; | |
507 PyObject *result, *args2; | |
508 char keepxml = 0; | |
509 | |
510 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|b", kwlist, &keepxml)) { | |
511 return NULL; | |
512 } | |
513 | |
514 if (stripentities == NULL) return NULL; | |
515 result = PyObject_CallFunction(stripentities, "Ob", self, keepxml); | |
516 if (result == NULL) return NULL; | |
517 args2 = PyTuple_New(1); | |
518 if (args2 == NULL) { | |
519 Py_DECREF(result); | |
520 return NULL; | |
521 } | |
522 PyTuple_SET_ITEM(args2, 0, result); | |
523 result = MarkupType.tp_new(&MarkupType, args2, NULL); | |
524 Py_DECREF(args2); | |
525 return result; | |
526 } | |
527 | |
528 PyDoc_STRVAR(striptags__doc__, | |
547 | 529 """Return a copy of the text with all XML/HTML tags removed.\n\ |
530 \n\ | |
531 :return: a `Markup` instance with all tags removed\n\ | |
532 :rtype: `Markup`\n\ | |
533 :see: `genshi.util.striptags`\n\ | |
534 "); | |
541 | 535 |
536 static PyObject * | |
537 Markup_striptags(PyObject* self) | |
538 { | |
539 PyObject *result, *args; | |
540 | |
541 if (striptags == NULL) return NULL; | |
542 result = PyObject_CallFunction(striptags, "O", self); | |
543 if (result == NULL) return NULL; | |
544 args = PyTuple_New(1); | |
545 if (args == NULL) { | |
546 Py_DECREF(result); | |
547 return NULL; | |
548 } | |
549 PyTuple_SET_ITEM(args, 0, result); | |
550 result = MarkupType.tp_new(&MarkupType, args, NULL); | |
551 Py_DECREF(args); | |
552 return result; | |
553 } | |
554 | |
555 typedef struct { | |
556 PyUnicodeObject HEAD; | |
557 } MarkupObject; | |
558 | |
559 static PyMethodDef Markup_methods[] = { | |
737
ca72e3dc443d
Implement the `__html__` protocol as suggested in #202. This would allow Genshi to be used in combination with other markup generating tools, as long as they support the same protocol.
cmlenz
parents:
723
diff
changeset
|
560 {"__html__", (PyCFunction) Markup_html, METH_NOARGS, NULL}, |
541 | 561 {"escape", (PyCFunction) Markup_escape, |
547 | 562 METH_VARARGS|METH_CLASS|METH_KEYWORDS, escape__doc__}, |
563 {"join", (PyCFunction)Markup_join, METH_VARARGS|METH_KEYWORDS, join__doc__}, | |
541 | 564 {"unescape", (PyCFunction)Markup_unescape, METH_NOARGS, unescape__doc__}, |
565 {"stripentities", (PyCFunction) Markup_stripentities, | |
566 METH_VARARGS|METH_KEYWORDS, stripentities__doc__}, | |
567 {"striptags", (PyCFunction) Markup_striptags, METH_NOARGS, | |
568 striptags__doc__}, | |
569 {NULL} /* Sentinel */ | |
570 }; | |
571 | |
572 static PyNumberMethods Markup_as_number = { | |
547 | 573 Markup_add, /*nb_add*/ |
574 0, /*nb_subtract*/ | |
575 Markup_mul, /*nb_multiply*/ | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
576 #ifndef IS_PY3K |
547 | 577 0, /*nb_divide*/ |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
578 #endif |
547 | 579 Markup_mod, /*nb_remainder*/ |
541 | 580 }; |
581 | |
582 PyTypeObject MarkupType = { | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
583 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
584 PyVarObject_HEAD_INIT(NULL, 0) |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
585 #else |
541 | 586 PyObject_HEAD_INIT(NULL) |
587 0, | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
588 #endif |
541 | 589 "genshi._speedups.Markup", |
590 sizeof(MarkupObject), | |
591 0, | |
592 0, /*tp_dealloc*/ | |
722 | 593 0, /*tp_print*/ |
541 | 594 0, /*tp_getattr*/ |
595 0, /*tp_setattr*/ | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
596 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
597 0, /*tp_reserved*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
598 #else |
541 | 599 0, /*tp_compare*/ |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
600 #endif |
541 | 601 Markup_repr, /*tp_repr*/ |
602 &Markup_as_number, /*tp_as_number*/ | |
603 0, /*tp_as_sequence*/ | |
604 0, /*tp_as_mapping*/ | |
605 0, /*tp_hash */ | |
606 | |
607 0, /*tp_call*/ | |
608 0, /*tp_str*/ | |
609 0, /*tp_getattro*/ | |
610 0, /*tp_setattro*/ | |
611 0, /*tp_as_buffer*/ | |
612 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
613 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
614 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
615 #elif defined(Py_TPFLAGS_UNICODE_SUBCLASS) |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
616 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
617 #else |
541 | 618 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
619 #endif |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
620 |
541 | 621 Markup__doc__,/*tp_doc*/ |
722 | 622 |
541 | 623 0, /*tp_traverse*/ |
624 0, /*tp_clear*/ | |
625 | |
626 0, /*tp_richcompare*/ | |
627 0, /*tp_weaklistoffset*/ | |
628 | |
629 0, /*tp_iter*/ | |
630 0, /*tp_iternext*/ | |
631 | |
632 /* Attribute descriptor and subclassing stuff */ | |
633 | |
634 Markup_methods,/*tp_methods*/ | |
635 0, /*tp_members*/ | |
636 0, /*tp_getset*/ | |
637 0, /*tp_base*/ | |
638 0, /*tp_dict*/ | |
722 | 639 |
541 | 640 0, /*tp_descr_get*/ |
641 0, /*tp_descr_set*/ | |
642 0, /*tp_dictoffset*/ | |
722 | 643 |
541 | 644 0, /*tp_init*/ |
645 0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/ | |
713
5420fe9d99a9
The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents:
681
diff
changeset
|
646 0, /*tp_new*/ |
541 | 647 0, /*tp_free Low-level free-memory routine */ |
648 0, /*tp_is_gc For PyObject_IS_GC */ | |
649 0, /*tp_bases*/ | |
650 0, /*tp_mro method resolution order */ | |
651 0, /*tp_cache*/ | |
652 0, /*tp_subclasses*/ | |
653 0 /*tp_weaklist*/ | |
654 }; | |
655 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
656 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
657 struct PyModuleDef module_def = { |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
658 PyModuleDef_HEAD_INIT, /*m_base*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
659 "_speedups", /*m_name*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
660 NULL, /*m_doc*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
661 -1, /*m_size*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
662 NULL, /*m_methods*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
663 NULL, /*m_reload*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
664 NULL, /*m_traverse*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
665 NULL, /*m_clear*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
666 NULL /*m_free*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
667 }; |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
668 |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
669 PyObject * |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
670 PyInit__speedups(void) |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
671 #else |
541 | 672 PyMODINIT_FUNC |
673 init_speedups(void) | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
674 #endif |
541 | 675 { |
676 PyObject *module; | |
677 | |
678 /* Workaround for quirk in Visual Studio, see | |
679 <http://www.python.it/faq/faq-3.html#3.24> */ | |
680 MarkupType.tp_base = &PyUnicode_Type; | |
681 | |
682 if (PyType_Ready(&MarkupType) < 0) | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
683 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
684 return NULL; |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
685 #else |
541 | 686 return; |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
687 #endif |
541 | 688 |
689 init_constants(); | |
690 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
691 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
692 module = PyModule_Create(&module_def); |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
693 #else |
541 | 694 module = Py_InitModule("_speedups", NULL); |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
695 #endif |
541 | 696 Py_INCREF(&MarkupType); |
697 PyModule_AddObject(module, "Markup", (PyObject *) &MarkupType); | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
698 |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
699 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
700 return module; |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
701 #endif |
541 | 702 } |