annotate genshi/_speedups.c @ 719:4bc6741b2811 trunk

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