annotate genshi/_speedups.c @ 784:67d324a62cc0 experimental-match-fastpaths

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