annotate genshi/_speedups.c @ 909:585fdbd30e05

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