annotate genshi/_speedups.c @ 1041:cddcb39ce5dd stable-0.7.x

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