Mercurial > genshi > genshi-test
annotate genshi/_speedups.c @ 913:263919318d52 experimental-py3k
py3k branch: add python 3 support to _speedups C extension
author | hodgestar |
---|---|
date | Sun, 24 Oct 2010 21:16:11 +0000 |
parents | d282c6c0133e |
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 | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
17 #if PY_MAJOR_VERSION > 2 |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
18 # define IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
19 #elif PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN) |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
20 typedef int Py_ssize_t; |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
21 # define PY_SSIZE_T_MAX INT_MAX |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
22 # define PY_SSIZE_T_MIN INT_MIN |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
23 #endif |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
24 |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
25 /* We only use Unicode Strings in this module */ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
26 #ifndef IS_PY3K |
263919318d52
py3k branch: 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
686bbeecb9ac
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__")) { |
686bbeecb9ac
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); |
686bbeecb9ac
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); |
686bbeecb9ac
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) { |
686bbeecb9ac
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); |
686bbeecb9ac
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; |
686bbeecb9ac
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 } |
686bbeecb9ac
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); |
686bbeecb9ac
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); |
686bbeecb9ac
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); |
686bbeecb9ac
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; |
686bbeecb9ac
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 } |
913
263919318d52
py3k branch: 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
281ab9fb4b04
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
281ab9fb4b04
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); |
281ab9fb4b04
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)) { | |
204 return type->tp_new(type, args, NULL); | |
205 } | |
206 if (PyObject_TypeCheck(text, type)) { | |
207 Py_INCREF(text); | |
208 return text; | |
209 } | |
210 return escape(text, quotes); | |
211 } | |
212 | |
737
686bbeecb9ac
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
|
213 static PyObject * |
686bbeecb9ac
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
|
214 Markup_html(PyObject *self) |
686bbeecb9ac
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
|
215 { |
686bbeecb9ac
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
|
216 Py_INCREF(self); |
686bbeecb9ac
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
|
217 return self; |
686bbeecb9ac
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 } |
686bbeecb9ac
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 |
547 | 220 PyDoc_STRVAR(join__doc__, |
221 "Return a `Markup` object which is the concatenation of the strings\n\ | |
222 in the given sequence, where this `Markup` object is the separator\n\ | |
223 between the joined elements.\n\ | |
224 \n\ | |
225 Any element in the sequence that is not a `Markup` instance is\n\ | |
226 automatically escaped.\n\ | |
227 \n\ | |
228 :param seq: the sequence of strings to join\n\ | |
229 :param escape_quotes: whether double quote characters in the elements\n\ | |
230 should be escaped\n\ | |
231 :return: the joined `Markup` object\n\ | |
232 :rtype: `Markup`\n\ | |
233 :see: `escape`\n\ | |
234 "); | |
235 | |
541 | 236 static PyObject * |
237 Markup_join(PyObject *self, PyObject *args, PyObject *kwds) | |
238 { | |
239 static char *kwlist[] = {"seq", "escape_quotes", 0}; | |
665
281ab9fb4b04
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
|
240 PyObject *seq = NULL, *seq2, *tmp, *tmp2; |
541 | 241 char quotes = 1; |
795 | 242 Py_ssize_t n; |
243 int i; | |
541 | 244 |
245 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|b", kwlist, &seq, "es)) { | |
246 return NULL; | |
247 } | |
248 if (!PySequence_Check(seq)) { | |
249 return NULL; | |
250 } | |
251 n = PySequence_Size(seq); | |
252 if (n < 0) { | |
253 return NULL; | |
254 } | |
255 seq2 = PyTuple_New(n); | |
256 if (seq2 == NULL) { | |
257 return NULL; | |
258 } | |
259 for (i = 0; i < n; i++) { | |
260 tmp = PySequence_GetItem(seq, i); | |
261 if (tmp == NULL) { | |
262 Py_DECREF(seq2); | |
263 return NULL; | |
264 } | |
665
281ab9fb4b04
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
|
265 tmp2 = escape(tmp, quotes); |
281ab9fb4b04
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
|
266 if (tmp2 == NULL) { |
541 | 267 Py_DECREF(seq2); |
268 return NULL; | |
269 } | |
665
281ab9fb4b04
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 PyTuple_SET_ITEM(seq2, i, tmp2); |
281ab9fb4b04
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 Py_DECREF(tmp); |
541 | 272 } |
273 tmp = PyUnicode_Join(self, seq2); | |
274 Py_DECREF(seq2); | |
275 if (tmp == NULL) | |
276 return NULL; | |
277 args = PyTuple_New(1); | |
278 if (args == NULL) { | |
279 Py_DECREF(tmp); | |
280 return NULL; | |
281 } | |
282 PyTuple_SET_ITEM(args, 0, tmp); | |
283 tmp = MarkupType.tp_new(&MarkupType, args, NULL); | |
284 Py_DECREF(args); | |
285 return tmp; | |
286 } | |
287 | |
288 static PyObject * | |
289 Markup_add(PyObject *self, PyObject *other) | |
290 { | |
291 PyObject *tmp, *tmp2, *args, *ret; | |
292 if (PyObject_TypeCheck(self, &MarkupType)) { | |
293 tmp = escape(other, 1); | |
294 if (tmp == NULL) | |
295 return NULL; | |
296 tmp2 = PyUnicode_Concat(self, tmp); | |
297 } else { // __radd__ | |
298 tmp = escape(self, 1); | |
299 if (tmp == NULL) | |
300 return NULL; | |
301 tmp2 = PyUnicode_Concat(tmp, other); | |
302 } | |
665
281ab9fb4b04
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
|
303 Py_DECREF(tmp); |
281ab9fb4b04
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 if (tmp2 == NULL) |
541 | 305 return NULL; |
306 args = PyTuple_New(1); | |
307 if (args == NULL) { | |
308 Py_DECREF(tmp2); | |
309 return NULL; | |
310 } | |
311 PyTuple_SET_ITEM(args, 0, tmp2); | |
312 ret = MarkupType.tp_new(&MarkupType, args, NULL); | |
313 Py_DECREF(args); | |
314 return ret; | |
315 } | |
316 | |
317 static PyObject * | |
318 Markup_mod(PyObject *self, PyObject *args) | |
319 { | |
320 PyObject *tmp, *tmp2, *ret, *args2; | |
795 | 321 int i; |
322 Py_ssize_t nargs = 0; | |
713
a58a50e89d04
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
|
323 PyObject *kwds = NULL; |
541 | 324 |
713
a58a50e89d04
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
|
325 if (PyDict_Check(args)) { |
a58a50e89d04
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 kwds = args; |
a58a50e89d04
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 } |
a58a50e89d04
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 if (kwds && PyDict_Size(kwds)) { |
a58a50e89d04
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 PyObject *kwcopy, *key, *value; |
a58a50e89d04
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 Py_ssize_t pos = 0; |
a58a50e89d04
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 |
a58a50e89d04
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 kwcopy = PyDict_Copy( kwds ); |
a58a50e89d04
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 (kwcopy == NULL) { |
a58a50e89d04
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 return NULL; |
a58a50e89d04
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 } |
a58a50e89d04
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 while (PyDict_Next(kwcopy, &pos, &key, &value)) { |
a58a50e89d04
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 tmp = escape(value, 1); |
a58a50e89d04
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 (tmp == NULL) { |
a58a50e89d04
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 Py_DECREF(kwcopy); |
a58a50e89d04
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 return NULL; |
a58a50e89d04
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 } |
a58a50e89d04
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 if (PyDict_SetItem(kwcopy, key, tmp) < 0) { |
a58a50e89d04
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 Py_DECREF(tmp); |
a58a50e89d04
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); |
a58a50e89d04
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; |
a58a50e89d04
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 } |
a58a50e89d04
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 } |
a58a50e89d04
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 tmp = PyUnicode_Format(self, kwcopy); |
a58a50e89d04
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); |
a58a50e89d04
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 if (tmp == NULL) { |
a58a50e89d04
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 return NULL; |
a58a50e89d04
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 } |
a58a50e89d04
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 } else if (PyTuple_Check(args)) { |
541 | 354 nargs = PyTuple_GET_SIZE(args); |
355 args2 = PyTuple_New(nargs); | |
356 if (args2 == NULL) { | |
357 return NULL; | |
358 } | |
359 for (i = 0; i < nargs; i++) { | |
360 tmp = escape(PyTuple_GET_ITEM(args, i), 1); | |
361 if (tmp == NULL) { | |
362 Py_DECREF(args2); | |
363 return NULL; | |
364 } | |
365 PyTuple_SET_ITEM(args2, i, tmp); | |
366 } | |
367 tmp = PyUnicode_Format(self, args2); | |
368 Py_DECREF(args2); | |
369 if (tmp == NULL) { | |
370 return NULL; | |
371 } | |
372 } else { | |
373 tmp2 = escape(args, 1); | |
374 if (tmp2 == NULL) { | |
375 return NULL; | |
376 } | |
377 tmp = PyUnicode_Format(self, tmp2); | |
378 Py_DECREF(tmp2); | |
379 if (tmp == NULL) { | |
380 return NULL; | |
381 } | |
382 } | |
383 args = PyTuple_New(1); | |
384 if (args == NULL) { | |
385 Py_DECREF(tmp); | |
386 return NULL; | |
387 } | |
388 PyTuple_SET_ITEM(args, 0, tmp); | |
389 ret = PyUnicode_Type.tp_new(&MarkupType, args, NULL); | |
390 Py_DECREF(args); | |
391 return ret; | |
392 } | |
393 | |
394 static PyObject * | |
395 Markup_mul(PyObject *self, PyObject *num) | |
396 { | |
397 PyObject *unicode, *result, *args; | |
398 | |
399 if (PyObject_TypeCheck(self, &MarkupType)) { | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
400 unicode = PyObject_Str(self); |
541 | 401 if (unicode == NULL) return NULL; |
402 result = PyNumber_Multiply(unicode, num); | |
403 } else { // __rmul__ | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
404 unicode = PyObject_Str(num); |
541 | 405 if (unicode == NULL) return NULL; |
406 result = PyNumber_Multiply(unicode, self); | |
407 } | |
665
281ab9fb4b04
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
|
408 Py_DECREF(unicode); |
541 | 409 |
410 if (result == NULL) return NULL; | |
411 args = PyTuple_New(1); | |
412 if (args == NULL) { | |
413 Py_DECREF(result); | |
414 return NULL; | |
415 } | |
416 PyTuple_SET_ITEM(args, 0, result); | |
417 result = PyUnicode_Type.tp_new(&MarkupType, args, NULL); | |
418 Py_DECREF(args); | |
419 | |
420 return result; | |
421 } | |
422 | |
423 static PyObject * | |
424 Markup_repr(PyObject *self) | |
425 { | |
426 PyObject *format, *result, *args; | |
427 | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
428 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
429 format = PyUnicode_FromString("<Markup %r>"); |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
430 #else |
541 | 431 format = PyString_FromString("<Markup %r>"); |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
432 #endif |
541 | 433 if (format == NULL) return NULL; |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
434 result = PyObject_Str(self); |
665
281ab9fb4b04
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
|
435 if (result == NULL) { |
281ab9fb4b04
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 Py_DECREF(format); |
281ab9fb4b04
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 return NULL; |
281ab9fb4b04
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 } |
541 | 439 args = PyTuple_New(1); |
440 if (args == NULL) { | |
665
281ab9fb4b04
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); |
541 | 442 Py_DECREF(result); |
443 return NULL; | |
444 } | |
445 PyTuple_SET_ITEM(args, 0, result); | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
446 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
447 result = PyUnicode_Format(format, args); |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
448 #else |
541 | 449 result = PyString_Format(format, args); |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
450 #endif |
681
ae5378f81398
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
|
451 Py_DECREF(format); |
541 | 452 Py_DECREF(args); |
453 return result; | |
454 } | |
455 | |
456 PyDoc_STRVAR(unescape__doc__, | |
547 | 457 "Reverse-escapes &, <, >, and \" and returns a `unicode` object.\n\ |
458 \n\ | |
459 >>> Markup('1 < 2').unescape()\n\ | |
460 u'1 < 2'\n\ | |
461 \n\ | |
462 :return: the unescaped string\n\ | |
463 :rtype: `unicode`\n\ | |
464 :see: `genshi.core.unescape`\n\ | |
465 "); | |
541 | 466 |
467 static PyObject * | |
468 Markup_unescape(PyObject* self) | |
469 { | |
470 PyObject *tmp, *tmp2; | |
471 | |
472 tmp = PyUnicode_Replace(self, qt2, qt1, -1); | |
473 if (tmp == NULL) return NULL; | |
474 tmp2 = PyUnicode_Replace(tmp, gt2, gt1, -1); | |
475 Py_DECREF(tmp); | |
476 if (tmp2 == NULL) return NULL; | |
477 tmp = PyUnicode_Replace(tmp2, lt2, lt1, -1); | |
478 Py_DECREF(tmp2); | |
479 if (tmp == NULL) return NULL; | |
480 tmp2 = PyUnicode_Replace(tmp, amp2, amp1, -1); | |
481 Py_DECREF(tmp); | |
482 return tmp2; | |
483 } | |
484 | |
485 PyDoc_STRVAR(stripentities__doc__, | |
486 "Return a copy of the text with any character or numeric entities\n\ | |
487 replaced by the equivalent UTF-8 characters.\n\ | |
488 \n\ | |
489 If the `keepxmlentities` parameter is provided and evaluates to `True`,\n\ | |
547 | 490 the core XML entities (``&``, ``'``, ``>``, ``<`` and\n\ |
491 ``"``) are not stripped.\n\ | |
492 \n\ | |
493 :return: a `Markup` instance with entities removed\n\ | |
494 :rtype: `Markup`\n\ | |
495 :see: `genshi.util.stripentities`\n\ | |
496 "); | |
541 | 497 |
498 static PyObject * | |
499 Markup_stripentities(PyObject* self, PyObject *args, PyObject *kwds) | |
500 { | |
501 static char *kwlist[] = {"keepxmlentities", 0}; | |
502 PyObject *result, *args2; | |
503 char keepxml = 0; | |
504 | |
505 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|b", kwlist, &keepxml)) { | |
506 return NULL; | |
507 } | |
508 | |
509 if (stripentities == NULL) return NULL; | |
510 result = PyObject_CallFunction(stripentities, "Ob", self, keepxml); | |
511 if (result == NULL) return NULL; | |
512 args2 = PyTuple_New(1); | |
513 if (args2 == NULL) { | |
514 Py_DECREF(result); | |
515 return NULL; | |
516 } | |
517 PyTuple_SET_ITEM(args2, 0, result); | |
518 result = MarkupType.tp_new(&MarkupType, args2, NULL); | |
519 Py_DECREF(args2); | |
520 return result; | |
521 } | |
522 | |
523 PyDoc_STRVAR(striptags__doc__, | |
547 | 524 """Return a copy of the text with all XML/HTML tags removed.\n\ |
525 \n\ | |
526 :return: a `Markup` instance with all tags removed\n\ | |
527 :rtype: `Markup`\n\ | |
528 :see: `genshi.util.striptags`\n\ | |
529 "); | |
541 | 530 |
531 static PyObject * | |
532 Markup_striptags(PyObject* self) | |
533 { | |
534 PyObject *result, *args; | |
535 | |
536 if (striptags == NULL) return NULL; | |
537 result = PyObject_CallFunction(striptags, "O", self); | |
538 if (result == NULL) return NULL; | |
539 args = PyTuple_New(1); | |
540 if (args == NULL) { | |
541 Py_DECREF(result); | |
542 return NULL; | |
543 } | |
544 PyTuple_SET_ITEM(args, 0, result); | |
545 result = MarkupType.tp_new(&MarkupType, args, NULL); | |
546 Py_DECREF(args); | |
547 return result; | |
548 } | |
549 | |
550 typedef struct { | |
551 PyUnicodeObject HEAD; | |
552 } MarkupObject; | |
553 | |
554 static PyMethodDef Markup_methods[] = { | |
737
686bbeecb9ac
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
|
555 {"__html__", (PyCFunction) Markup_html, METH_NOARGS, NULL}, |
541 | 556 {"escape", (PyCFunction) Markup_escape, |
547 | 557 METH_VARARGS|METH_CLASS|METH_KEYWORDS, escape__doc__}, |
558 {"join", (PyCFunction)Markup_join, METH_VARARGS|METH_KEYWORDS, join__doc__}, | |
541 | 559 {"unescape", (PyCFunction)Markup_unescape, METH_NOARGS, unescape__doc__}, |
560 {"stripentities", (PyCFunction) Markup_stripentities, | |
561 METH_VARARGS|METH_KEYWORDS, stripentities__doc__}, | |
562 {"striptags", (PyCFunction) Markup_striptags, METH_NOARGS, | |
563 striptags__doc__}, | |
564 {NULL} /* Sentinel */ | |
565 }; | |
566 | |
567 static PyNumberMethods Markup_as_number = { | |
547 | 568 Markup_add, /*nb_add*/ |
569 0, /*nb_subtract*/ | |
570 Markup_mul, /*nb_multiply*/ | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
571 #ifndef IS_PY3K |
547 | 572 0, /*nb_divide*/ |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
573 #endif |
547 | 574 Markup_mod, /*nb_remainder*/ |
541 | 575 }; |
576 | |
577 PyTypeObject MarkupType = { | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
578 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
579 PyVarObject_HEAD_INIT(NULL, 0) |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
580 #else |
541 | 581 PyObject_HEAD_INIT(NULL) |
582 0, | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
583 #endif |
541 | 584 "genshi._speedups.Markup", |
585 sizeof(MarkupObject), | |
586 0, | |
587 0, /*tp_dealloc*/ | |
722 | 588 0, /*tp_print*/ |
541 | 589 0, /*tp_getattr*/ |
590 0, /*tp_setattr*/ | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
591 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
592 0, /*tp_reserved*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
593 #else |
541 | 594 0, /*tp_compare*/ |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
595 #endif |
541 | 596 Markup_repr, /*tp_repr*/ |
597 &Markup_as_number, /*tp_as_number*/ | |
598 0, /*tp_as_sequence*/ | |
599 0, /*tp_as_mapping*/ | |
600 0, /*tp_hash */ | |
601 | |
602 0, /*tp_call*/ | |
603 0, /*tp_str*/ | |
604 0, /*tp_getattro*/ | |
605 0, /*tp_setattro*/ | |
606 0, /*tp_as_buffer*/ | |
607 | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
608 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
609 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
610 #elif defined(Py_TPFLAGS_UNICODE_SUBCLASS) |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
611 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_UNICODE_SUBCLASS, /*tp_flags*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
612 #else |
541 | 613 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_CHECKTYPES, /*tp_flags*/ |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
614 #endif |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
615 |
541 | 616 Markup__doc__,/*tp_doc*/ |
722 | 617 |
541 | 618 0, /*tp_traverse*/ |
619 0, /*tp_clear*/ | |
620 | |
621 0, /*tp_richcompare*/ | |
622 0, /*tp_weaklistoffset*/ | |
623 | |
624 0, /*tp_iter*/ | |
625 0, /*tp_iternext*/ | |
626 | |
627 /* Attribute descriptor and subclassing stuff */ | |
628 | |
629 Markup_methods,/*tp_methods*/ | |
630 0, /*tp_members*/ | |
631 0, /*tp_getset*/ | |
632 0, /*tp_base*/ | |
633 0, /*tp_dict*/ | |
722 | 634 |
541 | 635 0, /*tp_descr_get*/ |
636 0, /*tp_descr_set*/ | |
637 0, /*tp_dictoffset*/ | |
722 | 638 |
541 | 639 0, /*tp_init*/ |
640 0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/ | |
713
a58a50e89d04
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
|
641 0, /*tp_new*/ |
541 | 642 0, /*tp_free Low-level free-memory routine */ |
643 0, /*tp_is_gc For PyObject_IS_GC */ | |
644 0, /*tp_bases*/ | |
645 0, /*tp_mro method resolution order */ | |
646 0, /*tp_cache*/ | |
647 0, /*tp_subclasses*/ | |
648 0 /*tp_weaklist*/ | |
649 }; | |
650 | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
651 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
652 struct PyModuleDef module_def = { |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
653 PyModuleDef_HEAD_INIT, /*m_base*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
654 "_speedups", /*m_name*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
655 NULL, /*m_doc*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
656 -1, /*m_size*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
657 NULL, /*m_methods*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
658 NULL, /*m_reload*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
659 NULL, /*m_traverse*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
660 NULL, /*m_clear*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
661 NULL /*m_free*/ |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
662 }; |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
663 |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
664 PyObject * |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
665 PyInit__speedups(void) |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
666 #else |
541 | 667 PyMODINIT_FUNC |
668 init_speedups(void) | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
669 #endif |
541 | 670 { |
671 PyObject *module; | |
672 | |
673 /* Workaround for quirk in Visual Studio, see | |
674 <http://www.python.it/faq/faq-3.html#3.24> */ | |
675 MarkupType.tp_base = &PyUnicode_Type; | |
676 | |
677 if (PyType_Ready(&MarkupType) < 0) | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
678 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
679 return NULL; |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
680 #else |
541 | 681 return; |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
682 #endif |
541 | 683 |
684 init_constants(); | |
685 | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
686 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
687 module = PyModule_Create(&module_def); |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
688 #else |
541 | 689 module = Py_InitModule("_speedups", NULL); |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
690 #endif |
541 | 691 Py_INCREF(&MarkupType); |
692 PyModule_AddObject(module, "Markup", (PyObject *) &MarkupType); | |
913
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
693 |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
694 #ifdef IS_PY3K |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
695 return module; |
263919318d52
py3k branch: add python 3 support to _speedups C extension
hodgestar
parents:
795
diff
changeset
|
696 #endif |
541 | 697 } |