annotate genshi/_speedups.c @ 1034:e02843c0fecc trunk

Add missing boolean attributes to XHTML and HTML serializers (fixes #570).
author hodgestar
date Wed, 19 Mar 2014 14:22:22 +0000
parents 657f9ecffa3c
children accc8a0cf486
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};
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
245 PyObject *seq = NULL, *seq2, *tmp, *tmp2;
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
246 char quotes = 1;
795
f5076e2a9786 Apply some patches for build from #165.
cmlenz
parents: 737
diff changeset
247 Py_ssize_t n;
f5076e2a9786 Apply some patches for build from #165.
cmlenz
parents: 737
diff changeset
248 int i;
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
249
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
250 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|b", kwlist, &seq, &quotes)) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
251 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
252 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
253 if (!PySequence_Check(seq)) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
254 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
255 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
256 n = PySequence_Size(seq);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
257 if (n < 0) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
258 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
259 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
260 seq2 = PyTuple_New(n);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
261 if (seq2 == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
262 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
263 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
264 for (i = 0; i < n; i++) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
265 tmp = PySequence_GetItem(seq, i);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
266 if (tmp == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
267 Py_DECREF(seq2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
268 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
269 }
665
3ee92ec99ad9 Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents: 547
diff changeset
270 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
271 if (tmp2 == NULL) {
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
272 Py_DECREF(seq2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
273 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
274 }
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
275 PyTuple_SET_ITEM(seq2, i, tmp2);
3ee92ec99ad9 Applied patch to fix a memory leak in the C implementation of the `Markup.escape()` function. Thanks to Christian Boos for reporting and figuring out the problem. Closes #166.
cmlenz
parents: 547
diff changeset
276 Py_DECREF(tmp);
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
277 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
278 tmp = PyUnicode_Join(self, seq2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
279 Py_DECREF(seq2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
280 if (tmp == NULL)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
281 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
282 args = PyTuple_New(1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
283 if (args == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
284 Py_DECREF(tmp);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
285 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
286 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
287 PyTuple_SET_ITEM(args, 0, tmp);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
288 tmp = MarkupType.tp_new(&MarkupType, args, NULL);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
289 Py_DECREF(args);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
290 return tmp;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
291 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
292
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
293 static PyObject *
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
294 Markup_add(PyObject *self, PyObject *other)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
295 {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
296 PyObject *tmp, *tmp2, *args, *ret;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
297 if (PyObject_TypeCheck(self, &MarkupType)) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
298 tmp = escape(other, 1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
299 if (tmp == NULL)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
300 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
301 tmp2 = PyUnicode_Concat(self, tmp);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
302 } else { // __radd__
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
303 tmp = escape(self, 1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
304 if (tmp == NULL)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
305 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
306 tmp2 = PyUnicode_Concat(tmp, other);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
307 }
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
308 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
309 if (tmp2 == NULL)
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
310 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
311 args = PyTuple_New(1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
312 if (args == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
313 Py_DECREF(tmp2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
314 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
315 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
316 PyTuple_SET_ITEM(args, 0, tmp2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
317 ret = MarkupType.tp_new(&MarkupType, args, NULL);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
318 Py_DECREF(args);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
319 return ret;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
320 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
321
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
322 static PyObject *
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
323 Markup_mod(PyObject *self, PyObject *args)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
324 {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
325 PyObject *tmp, *tmp2, *ret, *args2;
795
f5076e2a9786 Apply some patches for build from #165.
cmlenz
parents: 737
diff changeset
326 int i;
f5076e2a9786 Apply some patches for build from #165.
cmlenz
parents: 737
diff changeset
327 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
328 PyObject *kwds = NULL;
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
329
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
330 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
331 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
332 }
5420fe9d99a9 The `Markup` class now supports mappings for right hand of the `%` (modulo) operator in the same way the Python string classes do, except that the substituted values are escape. Also, the special constructor which took positional arguments that would be substituted was removed. Thus the `Markup` class now supports the same arguments as that of its `unicode` base class. Closes #211. Many thanks to Christian Boos for the patch!
cmlenz
parents: 681
diff changeset
333 if (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
334 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
335 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
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 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
338 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
339 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
340 }
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 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
342 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
343 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
344 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
345 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
346 }
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 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
348 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
349 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
350 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
351 }
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 }
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 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
354 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
355 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
356 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
357 }
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
358 } else if (PyTuple_Check(args)) {
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
359 nargs = PyTuple_GET_SIZE(args);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
360 args2 = PyTuple_New(nargs);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
361 if (args2 == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
362 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
363 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
364 for (i = 0; i < nargs; i++) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
365 tmp = escape(PyTuple_GET_ITEM(args, i), 1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
366 if (tmp == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
367 Py_DECREF(args2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
368 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
369 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
370 PyTuple_SET_ITEM(args2, i, tmp);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
371 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
372 tmp = PyUnicode_Format(self, args2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
373 Py_DECREF(args2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
374 if (tmp == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
375 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
376 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
377 } else {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
378 tmp2 = escape(args, 1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
379 if (tmp2 == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
380 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
381 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
382 tmp = PyUnicode_Format(self, tmp2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
383 Py_DECREF(tmp2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
384 if (tmp == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
385 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
386 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
387 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
388 args = PyTuple_New(1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
389 if (args == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
390 Py_DECREF(tmp);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
391 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
392 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
393 PyTuple_SET_ITEM(args, 0, tmp);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
394 ret = PyUnicode_Type.tp_new(&MarkupType, args, NULL);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
395 Py_DECREF(args);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
396 return ret;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
397 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
398
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
399 static PyObject *
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
400 Markup_mul(PyObject *self, PyObject *num)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
401 {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
402 PyObject *unicode, *result, *args;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
403
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
404 if (PyObject_TypeCheck(self, &MarkupType)) {
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
405 unicode = PyObject_Str(self);
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, num);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
408 } else { // __rmul__
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
409 unicode = PyObject_Str(num);
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
410 if (unicode == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
411 result = PyNumber_Multiply(unicode, self);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
412 }
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
413 Py_DECREF(unicode);
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
414
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
415 if (result == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
416 args = PyTuple_New(1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
417 if (args == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
418 Py_DECREF(result);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
419 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
420 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
421 PyTuple_SET_ITEM(args, 0, result);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
422 result = PyUnicode_Type.tp_new(&MarkupType, args, NULL);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
423 Py_DECREF(args);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
424
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
425 return result;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
426 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
427
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
428 static PyObject *
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
429 Markup_repr(PyObject *self)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
430 {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
431 PyObject *format, *result, *args;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
432
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
433 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
434 format = PyUnicode_FromString("<Markup %r>");
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
435 #else
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
436 format = PyString_FromString("<Markup %r>");
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
437 #endif
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
438 if (format == NULL) return NULL;
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
439 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
440 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
441 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
442 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
443 }
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
444 args = PyTuple_New(1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
445 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
446 Py_DECREF(format);
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
447 Py_DECREF(result);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
448 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
449 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
450 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
451 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
452 result = PyUnicode_Format(format, args);
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
453 #else
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
454 result = PyString_Format(format, args);
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
455 #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
456 Py_DECREF(format);
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
457 Py_DECREF(args);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
458 return result;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
459 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
460
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
461 PyDoc_STRVAR(unescape__doc__,
547
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
462 "Reverse-escapes &, <, >, and \" and returns a `unicode` object.\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
463 \n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
464 >>> Markup('1 &lt; 2').unescape()\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
465 u'1 < 2'\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
466 \n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
467 :return: the unescaped string\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
468 :rtype: `unicode`\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
469 :see: `genshi.core.unescape`\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
470 ");
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
471
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
472 static PyObject *
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
473 Markup_unescape(PyObject* self)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
474 {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
475 PyObject *tmp, *tmp2;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
476
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
477 tmp = PyUnicode_Replace(self, qt2, qt1, -1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
478 if (tmp == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
479 tmp2 = PyUnicode_Replace(tmp, gt2, gt1, -1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
480 Py_DECREF(tmp);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
481 if (tmp2 == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
482 tmp = PyUnicode_Replace(tmp2, lt2, lt1, -1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
483 Py_DECREF(tmp2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
484 if (tmp == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
485 tmp2 = PyUnicode_Replace(tmp, amp2, amp1, -1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
486 Py_DECREF(tmp);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
487 return tmp2;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
488 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
489
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
490 PyDoc_STRVAR(stripentities__doc__,
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
491 "Return a copy of the text with any character or numeric entities\n\
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
492 replaced by the equivalent UTF-8 characters.\n\
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
493 \n\
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
494 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
495 the core XML entities (``&amp;``, ``&apos;``, ``&gt;``, ``&lt;`` and\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
496 ``&quot;``) are not stripped.\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
497 \n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
498 :return: a `Markup` instance with entities removed\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
499 :rtype: `Markup`\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
500 :see: `genshi.util.stripentities`\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
501 ");
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
502
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
503 static PyObject *
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
504 Markup_stripentities(PyObject* self, PyObject *args, PyObject *kwds)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
505 {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
506 static char *kwlist[] = {"keepxmlentities", 0};
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
507 PyObject *result, *args2;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
508 char keepxml = 0;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
509
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
510 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|b", kwlist, &keepxml)) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
511 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
512 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
513
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
514 if (stripentities == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
515 result = PyObject_CallFunction(stripentities, "Ob", self, keepxml);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
516 if (result == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
517 args2 = PyTuple_New(1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
518 if (args2 == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
519 Py_DECREF(result);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
520 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
521 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
522 PyTuple_SET_ITEM(args2, 0, result);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
523 result = MarkupType.tp_new(&MarkupType, args2, NULL);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
524 Py_DECREF(args2);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
525 return result;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
526 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
527
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
528 PyDoc_STRVAR(striptags__doc__,
547
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
529 """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
530 \n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
531 :return: a `Markup` instance with all tags removed\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
532 :rtype: `Markup`\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
533 :see: `genshi.util.striptags`\n\
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
534 ");
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
535
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
536 static PyObject *
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
537 Markup_striptags(PyObject* self)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
538 {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
539 PyObject *result, *args;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
540
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
541 if (striptags == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
542 result = PyObject_CallFunction(striptags, "O", self);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
543 if (result == NULL) return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
544 args = PyTuple_New(1);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
545 if (args == NULL) {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
546 Py_DECREF(result);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
547 return NULL;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
548 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
549 PyTuple_SET_ITEM(args, 0, result);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
550 result = MarkupType.tp_new(&MarkupType, args, NULL);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
551 Py_DECREF(args);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
552 return result;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
553 }
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
554
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
555 typedef struct {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
556 PyUnicodeObject HEAD;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
557 } MarkupObject;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
558
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
559 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
560 {"__html__", (PyCFunction) Markup_html, METH_NOARGS, NULL},
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
561 {"escape", (PyCFunction) Markup_escape,
547
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
562 METH_VARARGS|METH_CLASS|METH_KEYWORDS, escape__doc__},
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
563 {"join", (PyCFunction)Markup_join, METH_VARARGS|METH_KEYWORDS, join__doc__},
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
564 {"unescape", (PyCFunction)Markup_unescape, METH_NOARGS, unescape__doc__},
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
565 {"stripentities", (PyCFunction) Markup_stripentities,
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
566 METH_VARARGS|METH_KEYWORDS, stripentities__doc__},
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
567 {"striptags", (PyCFunction) Markup_striptags, METH_NOARGS,
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
568 striptags__doc__},
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
569 {NULL} /* Sentinel */
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
570 };
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
571
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
572 static PyNumberMethods Markup_as_number = {
547
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
573 Markup_add, /*nb_add*/
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
574 0, /*nb_subtract*/
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
575 Markup_mul, /*nb_multiply*/
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
576 #ifndef IS_PY3K
547
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
577 0, /*nb_divide*/
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
578 #endif
547
93b2a5792cfc Port docstrings to C version of `Markup` class.
cmlenz
parents: 541
diff changeset
579 Markup_mod, /*nb_remainder*/
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
580 };
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
581
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
582 PyTypeObject MarkupType = {
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
583 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
584 PyVarObject_HEAD_INIT(NULL, 0)
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
585 #else
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
586 PyObject_HEAD_INIT(NULL)
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
587 0,
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
588 #endif
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
589 "genshi._speedups.Markup",
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
590 sizeof(MarkupObject),
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
591 0,
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
592 0, /*tp_dealloc*/
722
1ec766e4f753 Fix trailing whitespace.
jruigrok
parents: 719
diff changeset
593 0, /*tp_print*/
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
594 0, /*tp_getattr*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
595 0, /*tp_setattr*/
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
596 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
597 0, /*tp_reserved*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
598 #else
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
599 0, /*tp_compare*/
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
600 #endif
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
601 Markup_repr, /*tp_repr*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
602 &Markup_as_number, /*tp_as_number*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
603 0, /*tp_as_sequence*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
604 0, /*tp_as_mapping*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
605 0, /*tp_hash */
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
606
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
607 0, /*tp_call*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
608 0, /*tp_str*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
609 0, /*tp_getattro*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
610 0, /*tp_setattro*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
611 0, /*tp_as_buffer*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
612
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
613 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
614 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
615 #elif defined(Py_TPFLAGS_UNICODE_SUBCLASS)
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
616 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
617 #else
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
618 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
619 #endif
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
620
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
621 Markup__doc__,/*tp_doc*/
722
1ec766e4f753 Fix trailing whitespace.
jruigrok
parents: 719
diff changeset
622
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
623 0, /*tp_traverse*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
624 0, /*tp_clear*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
625
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
626 0, /*tp_richcompare*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
627 0, /*tp_weaklistoffset*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
628
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
629 0, /*tp_iter*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
630 0, /*tp_iternext*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
631
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
632 /* Attribute descriptor and subclassing stuff */
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
633
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
634 Markup_methods,/*tp_methods*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
635 0, /*tp_members*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
636 0, /*tp_getset*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
637 0, /*tp_base*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
638 0, /*tp_dict*/
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_descr_get*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
641 0, /*tp_descr_set*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
642 0, /*tp_dictoffset*/
722
1ec766e4f753 Fix trailing whitespace.
jruigrok
parents: 719
diff changeset
643
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
644 0, /*tp_init*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
645 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
646 0, /*tp_new*/
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
647 0, /*tp_free Low-level free-memory routine */
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
648 0, /*tp_is_gc For PyObject_IS_GC */
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
649 0, /*tp_bases*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
650 0, /*tp_mro method resolution order */
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
651 0, /*tp_cache*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
652 0, /*tp_subclasses*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
653 0 /*tp_weaklist*/
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
654 };
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
655
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
656 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
657 struct PyModuleDef module_def = {
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
658 PyModuleDef_HEAD_INIT, /*m_base*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
659 "_speedups", /*m_name*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
660 NULL, /*m_doc*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
661 -1, /*m_size*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
662 NULL, /*m_methods*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
663 NULL, /*m_reload*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
664 NULL, /*m_traverse*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
665 NULL, /*m_clear*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
666 NULL /*m_free*/
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
667 };
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
668
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
669 PyObject *
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
670 PyInit__speedups(void)
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
671 #else
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
672 PyMODINIT_FUNC
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
673 init_speedups(void)
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
674 #endif
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
675 {
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
676 PyObject *module;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
677
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
678 /* Workaround for quirk in Visual Studio, see
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
679 <http://www.python.it/faq/faq-3.html#3.24> */
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
680 MarkupType.tp_base = &PyUnicode_Type;
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
681
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
682 if (PyType_Ready(&MarkupType) < 0)
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
683 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
684 return NULL;
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
685 #else
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
686 return;
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
687 #endif
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
688
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
689 init_constants();
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
690
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
691 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
692 module = PyModule_Create(&module_def);
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
693 #else
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
694 module = Py_InitModule("_speedups", NULL);
930
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
695 #endif
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
696 Py_INCREF(&MarkupType);
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
697 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
698
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
699 #ifdef IS_PY3K
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
700 return module;
7b88d0773fd0 Merge r1138 from py3k: add python 3 support to _speedups C extension
hodgestar
parents: 795
diff changeset
701 #endif
541
773d8c470e82 Merged cspeedups branch into trunk.
cmlenz
parents:
diff changeset
702 }
Copyright (C) 2012-2017 Edgewall Software