blob: 61858ef84ccc4ca4ccedc143cda8453eb25f3acd [file] [log] [blame]
Armin Ronacherbd33f112008-04-18 09:17:32 +02001/**
2 * jinja2._speedups
3 * ~~~~~~~~~~~~~~~~
4 *
Armin Ronacher5411ce72008-05-25 11:36:22 +02005 * This module implements functions for automatic escaping in C for better
6 * performance. Additionally it defines a `tb_set_next` function to patch the
7 * debug traceback. If the speedups module is not compiled a ctypes
8 * implementation of `tb_set_next` and Python implementations of the other
9 * functions are used.
Armin Ronacherbd33f112008-04-18 09:17:32 +020010 *
Armin Ronacherb2178862008-05-05 22:06:21 +020011 * :copyright: 2008 by Armin Ronacher, Mickaël Guérin.
Armin Ronacherbd33f112008-04-18 09:17:32 +020012 * :license: BSD.
13 */
14
15#include <Python.h>
16
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020017#define ESCAPED_CHARS_TABLE_SIZE 63
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020018#define UNICHR(x) (((PyUnicodeObject*)PyUnicode_DecodeASCII(x, strlen(x), NULL))->str);
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020019
Armin Ronacherf4e1fb42008-06-09 18:54:56 +020020#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
21typedef int Py_ssize_t;
22#define PY_SSIZE_T_MAX INT_MAX
23#define PY_SSIZE_T_MIN INT_MIN
24#endif
25
26
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020027static PyObject* markup;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020028static Py_ssize_t escaped_chars_delta_len[ESCAPED_CHARS_TABLE_SIZE];
29static Py_UNICODE *escaped_chars_repl[ESCAPED_CHARS_TABLE_SIZE];
Armin Ronacherbd33f112008-04-18 09:17:32 +020030
31static int
32init_constants(void)
33{
Armin Ronacherf35e2812008-05-06 16:04:10 +020034 /* happing of characters to replace */
35 escaped_chars_repl['"'] = UNICHR("&#34;");
36 escaped_chars_repl['\''] = UNICHR("&#39;");
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020037 escaped_chars_repl['&'] = UNICHR("&amp;");
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020038 escaped_chars_repl['<'] = UNICHR("&lt;");
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020039 escaped_chars_repl['>'] = UNICHR("&gt;");
Armin Ronacherf35e2812008-05-06 16:04:10 +020040
41 /* lengths of those characters when replaced - 1 */
42 memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len));
43 escaped_chars_delta_len['"'] = escaped_chars_delta_len['\''] = \
44 escaped_chars_delta_len['&'] = 4;
45 escaped_chars_delta_len['<'] = escaped_chars_delta_len['>'] = 3;
Armin Ronacherbd33f112008-04-18 09:17:32 +020046
Armin Ronacherf35e2812008-05-06 16:04:10 +020047 /* import markup type so that we can mark the return value */
Armin Ronacherbd33f112008-04-18 09:17:32 +020048 PyObject *module = PyImport_ImportModule("jinja2.utils");
49 if (!module)
50 return 0;
51 markup = PyObject_GetAttrString(module, "Markup");
52 Py_DECREF(module);
53
54 return 1;
55}
56
57static PyObject*
58escape_unicode(PyUnicodeObject *in)
59{
60 PyUnicodeObject *out;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020061 Py_UNICODE *inp = in->str;
62 const Py_UNICODE *inp_end = in->str + in->length;
63 Py_UNICODE *next_escp;
Armin Ronacherbd33f112008-04-18 09:17:32 +020064 Py_UNICODE *outp;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020065 Py_ssize_t delta=0, erepl=0, delta_len=0;
Armin Ronacherbd33f112008-04-18 09:17:32 +020066
67 /* First we need to figure out how long the escaped string will be */
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020068 while (*(inp) || inp < inp_end) {
69 if (*inp < ESCAPED_CHARS_TABLE_SIZE && escaped_chars_delta_len[*inp]) {
70 delta += escaped_chars_delta_len[*inp];
Armin Ronacherf59bac22008-04-20 13:11:43 +020071 ++erepl;
Armin Ronacherbd33f112008-04-18 09:17:32 +020072 }
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020073 ++inp;
74 }
Armin Ronacherbd33f112008-04-18 09:17:32 +020075
76 /* Do we need to escape anything at all? */
77 if (!erepl) {
78 Py_INCREF(in);
79 return (PyObject*)in;
80 }
81
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020082 out = (PyUnicodeObject*)PyUnicode_FromUnicode(NULL, in->length + delta);
Armin Ronacherbd33f112008-04-18 09:17:32 +020083 if (!out)
84 return NULL;
85
86 outp = out->str;
87 inp = in->str;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020088 while (erepl-- > 0) {
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020089 /* look for the next substitution */
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020090 next_escp = inp;
91 while (next_escp < inp_end) {
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020092 if (*next_escp < ESCAPED_CHARS_TABLE_SIZE &&
93 (delta_len = escaped_chars_delta_len[*next_escp])) {
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020094 ++delta_len;
95 break;
96 }
97 ++next_escp;
Armin Ronacherbd33f112008-04-18 09:17:32 +020098 }
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020099
100 if (next_escp > inp) {
101 /* copy unescaped chars between inp and next_escp */
102 Py_UNICODE_COPY(outp, inp, next_escp-inp);
Armin Ronachere62b7ef2008-05-24 20:47:29 +0200103 outp += next_escp - inp;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +0200104 }
105
106 /* escape 'next_escp' */
107 Py_UNICODE_COPY(outp, escaped_chars_repl[*next_escp], delta_len);
108 outp += delta_len;
109
110 inp = next_escp + 1;
111 }
Armin Ronacher9a1e33c2008-05-05 22:00:46 +0200112 if (inp < inp_end)
Mickaël Guérinc0d40d32008-05-05 17:08:51 +0200113 Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str));
Armin Ronacherbd33f112008-04-18 09:17:32 +0200114
115 return (PyObject*)out;
116}
117
118
119static PyObject*
Armin Ronacherf59bac22008-04-20 13:11:43 +0200120escape(PyObject *self, PyObject *text)
121{
122 PyObject *s = NULL, *rv = NULL;
Armin Ronacherbd33f112008-04-18 09:17:32 +0200123
124 /* we don't have to escape integers, bools or floats */
125 if (PyInt_CheckExact(text) || PyLong_CheckExact(text) ||
126 PyFloat_CheckExact(text) || PyBool_Check(text) ||
Armin Ronacher7ceced52008-05-03 10:15:31 +0200127 text == Py_None)
128 return PyObject_CallFunctionObjArgs(markup, text, NULL);
Armin Ronacherbd33f112008-04-18 09:17:32 +0200129
130 /* if the object has an __html__ method that performs the escaping */
131 PyObject *html = PyObject_GetAttrString(text, "__html__");
132 if (html) {
133 rv = PyObject_CallObject(html, NULL);
134 Py_DECREF(html);
135 return rv;
136 }
137
138 /* otherwise make the object unicode if it isn't, then escape */
139 PyErr_Clear();
140 if (!PyUnicode_Check(text)) {
141 PyObject *unicode = PyObject_Unicode(text);
142 if (!unicode)
143 return NULL;
144 s = escape_unicode((PyUnicodeObject*)unicode);
145 Py_DECREF(unicode);
146 }
147 else
148 s = escape_unicode((PyUnicodeObject*)text);
149
150 /* convert the unicode string into a markup object. */
Armin Ronacher7ceced52008-05-03 10:15:31 +0200151 rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL);
152 Py_DECREF(s);
153 return rv;
Armin Ronacherbd33f112008-04-18 09:17:32 +0200154}
155
156
Armin Ronacherf35e2812008-05-06 16:04:10 +0200157static PyObject*
158soft_unicode(PyObject *self, PyObject *s)
159{
160 if (!PyUnicode_Check(s))
161 return PyObject_Unicode(s);
162 Py_INCREF(s);
163 return s;
164}
165
166
Armin Ronacherd71fff02008-05-26 23:57:07 +0200167static PyObject*
Armin Ronacherbd33f112008-04-18 09:17:32 +0200168tb_set_next(PyObject *self, PyObject *args)
169{
170 PyTracebackObject *tb, *old;
171 PyObject *next;
172
173 if (!PyArg_ParseTuple(args, "O!O:tb_set_next", &PyTraceBack_Type, &tb, &next))
174 return NULL;
175 if (next == Py_None)
176 next = NULL;
177 else if (!PyTraceBack_Check(next)) {
178 PyErr_SetString(PyExc_TypeError,
179 "tb_set_next arg 2 must be traceback or None");
180 return NULL;
181 }
182 else
183 Py_INCREF(next);
184
185 old = tb->tb_next;
186 tb->tb_next = (PyTracebackObject*)next;
187 Py_XDECREF(old);
188
189 Py_INCREF(Py_None);
190 return Py_None;
191}
192
193
194static PyMethodDef module_methods[] = {
Armin Ronacherf59bac22008-04-20 13:11:43 +0200195 {"escape", (PyCFunction)escape, METH_O,
Armin Ronacher9a1e33c2008-05-05 22:00:46 +0200196 "escape(s) -> markup\n\n"
Armin Ronacher9bb7e472008-05-28 11:26:59 +0200197 "Convert the characters &, <, >, ', and \" in string s to HTML-safe\n"
Armin Ronacherf35e2812008-05-06 16:04:10 +0200198 "sequences. Use this if you need to display text that might contain\n"
Armin Ronacher9a1e33c2008-05-05 22:00:46 +0200199 "such characters in HTML. Marks return value as markup string."},
Armin Ronacherf59bac22008-04-20 13:11:43 +0200200 {"soft_unicode", (PyCFunction)soft_unicode, METH_O,
201 "soft_unicode(object) -> string\n\n"
202 "Make a string unicode if it isn't already. That way a markup\n"
203 "string is not converted back to unicode."},
Armin Ronacherbd33f112008-04-18 09:17:32 +0200204 {"tb_set_next", (PyCFunction)tb_set_next, METH_VARARGS,
205 "Set the tb_next member of a traceback object."},
206 {NULL, NULL, 0, NULL} /* Sentinel */
207};
208
209
210#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
211#define PyMODINIT_FUNC void
212#endif
213PyMODINIT_FUNC
214init_speedups(void)
215{
216 if (!init_constants())
217 return;
218
219 Py_InitModule3("jinja2._speedups", module_methods, "");
220}