blob: 112e6001c2c1c3b75137a7ea0d40e7254f4dbff9 [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 Ronacher9a1e33c2008-05-05 22:00:46 +020020static PyObject* markup;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020021static Py_ssize_t escaped_chars_delta_len[ESCAPED_CHARS_TABLE_SIZE];
22static Py_UNICODE *escaped_chars_repl[ESCAPED_CHARS_TABLE_SIZE];
Armin Ronacherbd33f112008-04-18 09:17:32 +020023
24static int
25init_constants(void)
26{
Armin Ronacherf35e2812008-05-06 16:04:10 +020027 /* happing of characters to replace */
28 escaped_chars_repl['"'] = UNICHR("&#34;");
29 escaped_chars_repl['\''] = UNICHR("&#39;");
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020030 escaped_chars_repl['&'] = UNICHR("&amp;");
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020031 escaped_chars_repl['<'] = UNICHR("&lt;");
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020032 escaped_chars_repl['>'] = UNICHR("&gt;");
Armin Ronacherf35e2812008-05-06 16:04:10 +020033
34 /* lengths of those characters when replaced - 1 */
35 memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len));
36 escaped_chars_delta_len['"'] = escaped_chars_delta_len['\''] = \
37 escaped_chars_delta_len['&'] = 4;
38 escaped_chars_delta_len['<'] = escaped_chars_delta_len['>'] = 3;
Armin Ronacherbd33f112008-04-18 09:17:32 +020039
Armin Ronacherf35e2812008-05-06 16:04:10 +020040 /* import markup type so that we can mark the return value */
Armin Ronacherbd33f112008-04-18 09:17:32 +020041 PyObject *module = PyImport_ImportModule("jinja2.utils");
42 if (!module)
43 return 0;
44 markup = PyObject_GetAttrString(module, "Markup");
45 Py_DECREF(module);
46
47 return 1;
48}
49
50static PyObject*
51escape_unicode(PyUnicodeObject *in)
52{
53 PyUnicodeObject *out;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020054 Py_UNICODE *inp = in->str;
55 const Py_UNICODE *inp_end = in->str + in->length;
56 Py_UNICODE *next_escp;
Armin Ronacherbd33f112008-04-18 09:17:32 +020057 Py_UNICODE *outp;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020058 Py_ssize_t delta=0, erepl=0, delta_len=0;
Armin Ronacherbd33f112008-04-18 09:17:32 +020059
60 /* First we need to figure out how long the escaped string will be */
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020061 while (*(inp) || inp < inp_end) {
62 if (*inp < ESCAPED_CHARS_TABLE_SIZE && escaped_chars_delta_len[*inp]) {
63 delta += escaped_chars_delta_len[*inp];
Armin Ronacherf59bac22008-04-20 13:11:43 +020064 ++erepl;
Armin Ronacherbd33f112008-04-18 09:17:32 +020065 }
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020066 ++inp;
67 }
Armin Ronacherbd33f112008-04-18 09:17:32 +020068
69 /* Do we need to escape anything at all? */
70 if (!erepl) {
71 Py_INCREF(in);
72 return (PyObject*)in;
73 }
74
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020075 out = (PyUnicodeObject*)PyUnicode_FromUnicode(NULL, in->length + delta);
Armin Ronacherbd33f112008-04-18 09:17:32 +020076 if (!out)
77 return NULL;
78
79 outp = out->str;
80 inp = in->str;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020081 while (erepl-- > 0) {
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020082 /* look for the next substitution */
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020083 next_escp = inp;
84 while (next_escp < inp_end) {
Armin Ronacher9a1e33c2008-05-05 22:00:46 +020085 if (*next_escp < ESCAPED_CHARS_TABLE_SIZE &&
86 (delta_len = escaped_chars_delta_len[*next_escp])) {
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020087 ++delta_len;
88 break;
89 }
90 ++next_escp;
Armin Ronacherbd33f112008-04-18 09:17:32 +020091 }
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020092
93 if (next_escp > inp) {
94 /* copy unescaped chars between inp and next_escp */
95 Py_UNICODE_COPY(outp, inp, next_escp-inp);
Armin Ronachere62b7ef2008-05-24 20:47:29 +020096 outp += next_escp - inp;
Mickaël Guérinc0d40d32008-05-05 17:08:51 +020097 }
98
99 /* escape 'next_escp' */
100 Py_UNICODE_COPY(outp, escaped_chars_repl[*next_escp], delta_len);
101 outp += delta_len;
102
103 inp = next_escp + 1;
104 }
Armin Ronacher9a1e33c2008-05-05 22:00:46 +0200105 if (inp < inp_end)
Mickaël Guérinc0d40d32008-05-05 17:08:51 +0200106 Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str));
Armin Ronacherbd33f112008-04-18 09:17:32 +0200107
108 return (PyObject*)out;
109}
110
111
112static PyObject*
Armin Ronacherf59bac22008-04-20 13:11:43 +0200113escape(PyObject *self, PyObject *text)
114{
115 PyObject *s = NULL, *rv = NULL;
Armin Ronacherbd33f112008-04-18 09:17:32 +0200116
117 /* we don't have to escape integers, bools or floats */
118 if (PyInt_CheckExact(text) || PyLong_CheckExact(text) ||
119 PyFloat_CheckExact(text) || PyBool_Check(text) ||
Armin Ronacher7ceced52008-05-03 10:15:31 +0200120 text == Py_None)
121 return PyObject_CallFunctionObjArgs(markup, text, NULL);
Armin Ronacherbd33f112008-04-18 09:17:32 +0200122
123 /* if the object has an __html__ method that performs the escaping */
124 PyObject *html = PyObject_GetAttrString(text, "__html__");
125 if (html) {
126 rv = PyObject_CallObject(html, NULL);
127 Py_DECREF(html);
128 return rv;
129 }
130
131 /* otherwise make the object unicode if it isn't, then escape */
132 PyErr_Clear();
133 if (!PyUnicode_Check(text)) {
134 PyObject *unicode = PyObject_Unicode(text);
135 if (!unicode)
136 return NULL;
137 s = escape_unicode((PyUnicodeObject*)unicode);
138 Py_DECREF(unicode);
139 }
140 else
141 s = escape_unicode((PyUnicodeObject*)text);
142
143 /* convert the unicode string into a markup object. */
Armin Ronacher7ceced52008-05-03 10:15:31 +0200144 rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL);
145 Py_DECREF(s);
146 return rv;
Armin Ronacherbd33f112008-04-18 09:17:32 +0200147}
148
149
Armin Ronacherf35e2812008-05-06 16:04:10 +0200150static PyObject*
151soft_unicode(PyObject *self, PyObject *s)
152{
153 if (!PyUnicode_Check(s))
154 return PyObject_Unicode(s);
155 Py_INCREF(s);
156 return s;
157}
158
159
Armin Ronacherbd33f112008-04-18 09:17:32 +0200160static PyObject *
161tb_set_next(PyObject *self, PyObject *args)
162{
163 PyTracebackObject *tb, *old;
164 PyObject *next;
165
166 if (!PyArg_ParseTuple(args, "O!O:tb_set_next", &PyTraceBack_Type, &tb, &next))
167 return NULL;
168 if (next == Py_None)
169 next = NULL;
170 else if (!PyTraceBack_Check(next)) {
171 PyErr_SetString(PyExc_TypeError,
172 "tb_set_next arg 2 must be traceback or None");
173 return NULL;
174 }
175 else
176 Py_INCREF(next);
177
178 old = tb->tb_next;
179 tb->tb_next = (PyTracebackObject*)next;
180 Py_XDECREF(old);
181
182 Py_INCREF(Py_None);
183 return Py_None;
184}
185
186
187static PyMethodDef module_methods[] = {
Armin Ronacherf59bac22008-04-20 13:11:43 +0200188 {"escape", (PyCFunction)escape, METH_O,
Armin Ronacher9a1e33c2008-05-05 22:00:46 +0200189 "escape(s) -> markup\n\n"
Armin Ronacherbd33f112008-04-18 09:17:32 +0200190 "Convert the characters &, <, >, and \" in string s to HTML-safe\n"
Armin Ronacherf35e2812008-05-06 16:04:10 +0200191 "sequences. Use this if you need to display text that might contain\n"
Armin Ronacher9a1e33c2008-05-05 22:00:46 +0200192 "such characters in HTML. Marks return value as markup string."},
Armin Ronacherf59bac22008-04-20 13:11:43 +0200193 {"soft_unicode", (PyCFunction)soft_unicode, METH_O,
194 "soft_unicode(object) -> string\n\n"
195 "Make a string unicode if it isn't already. That way a markup\n"
196 "string is not converted back to unicode."},
Armin Ronacherbd33f112008-04-18 09:17:32 +0200197 {"tb_set_next", (PyCFunction)tb_set_next, METH_VARARGS,
198 "Set the tb_next member of a traceback object."},
199 {NULL, NULL, 0, NULL} /* Sentinel */
200};
201
202
203#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
204#define PyMODINIT_FUNC void
205#endif
206PyMODINIT_FUNC
207init_speedups(void)
208{
209 if (!init_constants())
210 return;
211
212 Py_InitModule3("jinja2._speedups", module_methods, "");
213}