annotate genshi/_speedups.c @ 913:263919318d52 experimental-py3k

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