Mercurial > genshi > mirror
annotate genshi/_speedups.c @ 1041:cddcb39ce5dd stable-0.7.x
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
author | hodgestar |
---|---|
date | Thu, 20 Mar 2014 11:44:48 +0000 |
parents | 657f9ecffa3c |
children |
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}; | |
1041
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
245 PyObject *seq = NULL, *seq2, *it, *tmp, *tmp2; |
541 | 246 char quotes = 1; |
247 | |
248 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|b", kwlist, &seq, "es)) { | |
249 return NULL; | |
250 } | |
1041
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
251 it = PyObject_GetIter(seq); |
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
252 if (it == NULL) |
541 | 253 return NULL; |
1041
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
254 seq2 = PyList_New(0); |
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
255 if (seq2 == NULL) { |
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
256 Py_DECREF(it); |
541 | 257 return NULL; |
258 } | |
1041
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
259 while ((tmp = PyIter_Next(it))) { |
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
|
260 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
|
261 if (tmp2 == NULL) { |
541 | 262 Py_DECREF(seq2); |
1041
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
263 Py_DECREF(it); |
541 | 264 return NULL; |
265 } | |
1041
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
266 PyList_Append(seq2, tmp2); |
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
|
267 Py_DECREF(tmp); |
541 | 268 } |
1041
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
269 Py_DECREF(it); |
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
270 if (PyErr_Occurred()) { |
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
271 Py_DECREF(seq2); |
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
272 return NULL; |
cddcb39ce5dd
Merge r1266 from trunk (support for iterators in _speedups.Markup.join).
hodgestar
parents:
943
diff
changeset
|
273 } |
541 | 274 tmp = PyUnicode_Join(self, seq2); |
275 Py_DECREF(seq2); | |
276 if (tmp == NULL) | |
277 return NULL; | |
278 args = PyTuple_New(1); | |
279 if (args == NULL) { | |
280 Py_DECREF(tmp); | |
281 return NULL; | |
282 } | |
283 PyTuple_SET_ITEM(args, 0, tmp); | |
284 tmp = MarkupType.tp_new(&MarkupType, args, NULL); | |
285 Py_DECREF(args); | |
286 return tmp; | |
287 } | |
288 | |
289 static PyObject * | |
290 Markup_add(PyObject *self, PyObject *other) | |
291 { | |
292 PyObject *tmp, *tmp2, *args, *ret; | |
293 if (PyObject_TypeCheck(self, &MarkupType)) { | |
294 tmp = escape(other, 1); | |
295 if (tmp == NULL) | |
296 return NULL; | |
297 tmp2 = PyUnicode_Concat(self, tmp); | |
298 } else { // __radd__ | |
299 tmp = escape(self, 1); | |
300 if (tmp == NULL) | |
301 return NULL; | |
302 tmp2 = PyUnicode_Concat(tmp, other); | |
303 } | |
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
|
304 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
|
305 if (tmp2 == NULL) |
541 | 306 return NULL; |
307 args = PyTuple_New(1); | |
308 if (args == NULL) { | |
309 Py_DECREF(tmp2); | |
310 return NULL; | |
311 } | |
312 PyTuple_SET_ITEM(args, 0, tmp2); | |
313 ret = MarkupType.tp_new(&MarkupType, args, NULL); | |
314 Py_DECREF(args); | |
315 return ret; | |
316 } | |
317 | |
318 static PyObject * | |
319 Markup_mod(PyObject *self, PyObject *args) | |
320 { | |
321 PyObject *tmp, *tmp2, *ret, *args2; | |
795 | 322 int i; |
323 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
|
324 PyObject *kwds = NULL; |
541 | 325 |
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
|
326 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
|
327 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
|
328 } |
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
|
329 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
|
330 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
|
331 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
|
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 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
|
334 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
|
335 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
|
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 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
|
338 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
|
339 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
|
340 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
|
341 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
|
342 } |
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 (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
|
344 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
|
345 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
|
346 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
|
347 } |
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 } |
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 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
|
350 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
|
351 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
|
352 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
|
353 } |
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 } else if (PyTuple_Check(args)) { |
541 | 355 nargs = PyTuple_GET_SIZE(args); |
356 args2 = PyTuple_New(nargs); | |
357 if (args2 == NULL) { | |
358 return NULL; | |
359 } | |
360 for (i = 0; i < nargs; i++) { | |
361 tmp = escape(PyTuple_GET_ITEM(args, i), 1); | |
362 if (tmp == NULL) { | |
363 Py_DECREF(args2); | |
364 return NULL; | |
365 } | |
366 PyTuple_SET_ITEM(args2, i, tmp); | |
367 } | |
368 tmp = PyUnicode_Format(self, args2); | |
369 Py_DECREF(args2); | |
370 if (tmp == NULL) { | |
371 return NULL; | |
372 } | |
373 } else { | |
374 tmp2 = escape(args, 1); | |
375 if (tmp2 == NULL) { | |
376 return NULL; | |
377 } | |
378 tmp = PyUnicode_Format(self, tmp2); | |
379 Py_DECREF(tmp2); | |
380 if (tmp == NULL) { | |
381 return NULL; | |
382 } | |
383 } | |
384 args = PyTuple_New(1); | |
385 if (args == NULL) { | |
386 Py_DECREF(tmp); | |
387 return NULL; | |
388 } | |
389 PyTuple_SET_ITEM(args, 0, tmp); | |
390 ret = PyUnicode_Type.tp_new(&MarkupType, args, NULL); | |
391 Py_DECREF(args); | |
392 return ret; | |
393 } | |
394 | |
395 static PyObject * | |
396 Markup_mul(PyObject *self, PyObject *num) | |
397 { | |
398 PyObject *unicode, *result, *args; | |
399 | |
400 if (PyObject_TypeCheck(self, &MarkupType)) { | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
401 unicode = PyObject_Str(self); |
541 | 402 if (unicode == NULL) return NULL; |
403 result = PyNumber_Multiply(unicode, num); | |
404 } else { // __rmul__ | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
405 unicode = PyObject_Str(num); |
541 | 406 if (unicode == NULL) return NULL; |
407 result = PyNumber_Multiply(unicode, self); | |
408 } | |
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
|
409 Py_DECREF(unicode); |
541 | 410 |
411 if (result == NULL) return NULL; | |
412 args = PyTuple_New(1); | |
413 if (args == NULL) { | |
414 Py_DECREF(result); | |
415 return NULL; | |
416 } | |
417 PyTuple_SET_ITEM(args, 0, result); | |
418 result = PyUnicode_Type.tp_new(&MarkupType, args, NULL); | |
419 Py_DECREF(args); | |
420 | |
421 return result; | |
422 } | |
423 | |
424 static PyObject * | |
425 Markup_repr(PyObject *self) | |
426 { | |
427 PyObject *format, *result, *args; | |
428 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
429 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
430 format = PyUnicode_FromString("<Markup %r>"); |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
431 #else |
541 | 432 format = PyString_FromString("<Markup %r>"); |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
433 #endif |
541 | 434 if (format == NULL) return NULL; |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
435 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
|
436 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
|
437 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
|
438 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
|
439 } |
541 | 440 args = PyTuple_New(1); |
441 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
|
442 Py_DECREF(format); |
541 | 443 Py_DECREF(result); |
444 return NULL; | |
445 } | |
446 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
|
447 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
448 result = PyUnicode_Format(format, args); |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
449 #else |
541 | 450 result = PyString_Format(format, args); |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
451 #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
|
452 Py_DECREF(format); |
541 | 453 Py_DECREF(args); |
454 return result; | |
455 } | |
456 | |
457 PyDoc_STRVAR(unescape__doc__, | |
547 | 458 "Reverse-escapes &, <, >, and \" and returns a `unicode` object.\n\ |
459 \n\ | |
460 >>> Markup('1 < 2').unescape()\n\ | |
461 u'1 < 2'\n\ | |
462 \n\ | |
463 :return: the unescaped string\n\ | |
464 :rtype: `unicode`\n\ | |
465 :see: `genshi.core.unescape`\n\ | |
466 "); | |
541 | 467 |
468 static PyObject * | |
469 Markup_unescape(PyObject* self) | |
470 { | |
471 PyObject *tmp, *tmp2; | |
472 | |
473 tmp = PyUnicode_Replace(self, qt2, qt1, -1); | |
474 if (tmp == NULL) return NULL; | |
475 tmp2 = PyUnicode_Replace(tmp, gt2, gt1, -1); | |
476 Py_DECREF(tmp); | |
477 if (tmp2 == NULL) return NULL; | |
478 tmp = PyUnicode_Replace(tmp2, lt2, lt1, -1); | |
479 Py_DECREF(tmp2); | |
480 if (tmp == NULL) return NULL; | |
481 tmp2 = PyUnicode_Replace(tmp, amp2, amp1, -1); | |
482 Py_DECREF(tmp); | |
483 return tmp2; | |
484 } | |
485 | |
486 PyDoc_STRVAR(stripentities__doc__, | |
487 "Return a copy of the text with any character or numeric entities\n\ | |
488 replaced by the equivalent UTF-8 characters.\n\ | |
489 \n\ | |
490 If the `keepxmlentities` parameter is provided and evaluates to `True`,\n\ | |
547 | 491 the core XML entities (``&``, ``'``, ``>``, ``<`` and\n\ |
492 ``"``) are not stripped.\n\ | |
493 \n\ | |
494 :return: a `Markup` instance with entities removed\n\ | |
495 :rtype: `Markup`\n\ | |
496 :see: `genshi.util.stripentities`\n\ | |
497 "); | |
541 | 498 |
499 static PyObject * | |
500 Markup_stripentities(PyObject* self, PyObject *args, PyObject *kwds) | |
501 { | |
502 static char *kwlist[] = {"keepxmlentities", 0}; | |
503 PyObject *result, *args2; | |
504 char keepxml = 0; | |
505 | |
506 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|b", kwlist, &keepxml)) { | |
507 return NULL; | |
508 } | |
509 | |
510 if (stripentities == NULL) return NULL; | |
511 result = PyObject_CallFunction(stripentities, "Ob", self, keepxml); | |
512 if (result == NULL) return NULL; | |
513 args2 = PyTuple_New(1); | |
514 if (args2 == NULL) { | |
515 Py_DECREF(result); | |
516 return NULL; | |
517 } | |
518 PyTuple_SET_ITEM(args2, 0, result); | |
519 result = MarkupType.tp_new(&MarkupType, args2, NULL); | |
520 Py_DECREF(args2); | |
521 return result; | |
522 } | |
523 | |
524 PyDoc_STRVAR(striptags__doc__, | |
547 | 525 """Return a copy of the text with all XML/HTML tags removed.\n\ |
526 \n\ | |
527 :return: a `Markup` instance with all tags removed\n\ | |
528 :rtype: `Markup`\n\ | |
529 :see: `genshi.util.striptags`\n\ | |
530 "); | |
541 | 531 |
532 static PyObject * | |
533 Markup_striptags(PyObject* self) | |
534 { | |
535 PyObject *result, *args; | |
536 | |
537 if (striptags == NULL) return NULL; | |
538 result = PyObject_CallFunction(striptags, "O", self); | |
539 if (result == NULL) return NULL; | |
540 args = PyTuple_New(1); | |
541 if (args == NULL) { | |
542 Py_DECREF(result); | |
543 return NULL; | |
544 } | |
545 PyTuple_SET_ITEM(args, 0, result); | |
546 result = MarkupType.tp_new(&MarkupType, args, NULL); | |
547 Py_DECREF(args); | |
548 return result; | |
549 } | |
550 | |
551 typedef struct { | |
552 PyUnicodeObject HEAD; | |
553 } MarkupObject; | |
554 | |
555 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
|
556 {"__html__", (PyCFunction) Markup_html, METH_NOARGS, NULL}, |
541 | 557 {"escape", (PyCFunction) Markup_escape, |
547 | 558 METH_VARARGS|METH_CLASS|METH_KEYWORDS, escape__doc__}, |
559 {"join", (PyCFunction)Markup_join, METH_VARARGS|METH_KEYWORDS, join__doc__}, | |
541 | 560 {"unescape", (PyCFunction)Markup_unescape, METH_NOARGS, unescape__doc__}, |
561 {"stripentities", (PyCFunction) Markup_stripentities, | |
562 METH_VARARGS|METH_KEYWORDS, stripentities__doc__}, | |
563 {"striptags", (PyCFunction) Markup_striptags, METH_NOARGS, | |
564 striptags__doc__}, | |
565 {NULL} /* Sentinel */ | |
566 }; | |
567 | |
568 static PyNumberMethods Markup_as_number = { | |
547 | 569 Markup_add, /*nb_add*/ |
570 0, /*nb_subtract*/ | |
571 Markup_mul, /*nb_multiply*/ | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
572 #ifndef IS_PY3K |
547 | 573 0, /*nb_divide*/ |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
574 #endif |
547 | 575 Markup_mod, /*nb_remainder*/ |
541 | 576 }; |
577 | |
578 PyTypeObject MarkupType = { | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
579 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
580 PyVarObject_HEAD_INIT(NULL, 0) |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
581 #else |
541 | 582 PyObject_HEAD_INIT(NULL) |
583 0, | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
584 #endif |
541 | 585 "genshi._speedups.Markup", |
586 sizeof(MarkupObject), | |
587 0, | |
588 0, /*tp_dealloc*/ | |
722 | 589 0, /*tp_print*/ |
541 | 590 0, /*tp_getattr*/ |
591 0, /*tp_setattr*/ | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
592 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
593 0, /*tp_reserved*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
594 #else |
541 | 595 0, /*tp_compare*/ |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
596 #endif |
541 | 597 Markup_repr, /*tp_repr*/ |
598 &Markup_as_number, /*tp_as_number*/ | |
599 0, /*tp_as_sequence*/ | |
600 0, /*tp_as_mapping*/ | |
601 0, /*tp_hash */ | |
602 | |
603 0, /*tp_call*/ | |
604 0, /*tp_str*/ | |
605 0, /*tp_getattro*/ | |
606 0, /*tp_setattro*/ | |
607 0, /*tp_as_buffer*/ | |
608 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
609 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
610 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
|
611 #elif defined(Py_TPFLAGS_UNICODE_SUBCLASS) |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
612 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
|
613 #else |
541 | 614 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
|
615 #endif |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
616 |
541 | 617 Markup__doc__,/*tp_doc*/ |
722 | 618 |
541 | 619 0, /*tp_traverse*/ |
620 0, /*tp_clear*/ | |
621 | |
622 0, /*tp_richcompare*/ | |
623 0, /*tp_weaklistoffset*/ | |
624 | |
625 0, /*tp_iter*/ | |
626 0, /*tp_iternext*/ | |
627 | |
628 /* Attribute descriptor and subclassing stuff */ | |
629 | |
630 Markup_methods,/*tp_methods*/ | |
631 0, /*tp_members*/ | |
632 0, /*tp_getset*/ | |
633 0, /*tp_base*/ | |
634 0, /*tp_dict*/ | |
722 | 635 |
541 | 636 0, /*tp_descr_get*/ |
637 0, /*tp_descr_set*/ | |
638 0, /*tp_dictoffset*/ | |
722 | 639 |
541 | 640 0, /*tp_init*/ |
641 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
|
642 0, /*tp_new*/ |
541 | 643 0, /*tp_free Low-level free-memory routine */ |
644 0, /*tp_is_gc For PyObject_IS_GC */ | |
645 0, /*tp_bases*/ | |
646 0, /*tp_mro method resolution order */ | |
647 0, /*tp_cache*/ | |
648 0, /*tp_subclasses*/ | |
649 0 /*tp_weaklist*/ | |
650 }; | |
651 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
652 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
653 struct PyModuleDef module_def = { |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
654 PyModuleDef_HEAD_INIT, /*m_base*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
655 "_speedups", /*m_name*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
656 NULL, /*m_doc*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
657 -1, /*m_size*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
658 NULL, /*m_methods*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
659 NULL, /*m_reload*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
660 NULL, /*m_traverse*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
661 NULL, /*m_clear*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
662 NULL /*m_free*/ |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
663 }; |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
664 |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
665 PyObject * |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
666 PyInit__speedups(void) |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
667 #else |
541 | 668 PyMODINIT_FUNC |
669 init_speedups(void) | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
670 #endif |
541 | 671 { |
672 PyObject *module; | |
673 | |
674 /* Workaround for quirk in Visual Studio, see | |
675 <http://www.python.it/faq/faq-3.html#3.24> */ | |
676 MarkupType.tp_base = &PyUnicode_Type; | |
677 | |
678 if (PyType_Ready(&MarkupType) < 0) | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
679 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
680 return NULL; |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
681 #else |
541 | 682 return; |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
683 #endif |
541 | 684 |
685 init_constants(); | |
686 | |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
687 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
688 module = PyModule_Create(&module_def); |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
689 #else |
541 | 690 module = Py_InitModule("_speedups", NULL); |
930
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
691 #endif |
541 | 692 Py_INCREF(&MarkupType); |
693 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
|
694 |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
695 #ifdef IS_PY3K |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
696 return module; |
7b88d0773fd0
Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
697 #endif |
541 | 698 } |