annotate genshi/_speedups.c @ 745:74b5c5476ddb trunk

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