Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 1 | /** |
| 2 | * jinja2._speedups |
| 3 | * ~~~~~~~~~~~~~~~~ |
| 4 | * |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 5 | * This module implements a few functions in C for better performance. It |
| 6 | * also defines a `tb_set_next` function that is used to patch the debug |
| 7 | * traceback. If the speedups module is not compiled a ctypes implementation |
| 8 | * is used. |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 9 | * |
Armin Ronacher | b217886 | 2008-05-05 22:06:21 +0200 | [diff] [blame] | 10 | * :copyright: 2008 by Armin Ronacher, Mickaël Guérin. |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 11 | * :license: BSD. |
| 12 | */ |
| 13 | |
| 14 | #include <Python.h> |
| 15 | |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 16 | #define ESCAPED_CHARS_TABLE_SIZE 63 |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 17 | #define UNICHR(x) (((PyUnicodeObject*)PyUnicode_DecodeASCII(x, strlen(x), NULL))->str); |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 18 | |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 19 | static PyObject* markup; |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 20 | static Py_ssize_t escaped_chars_delta_len[ESCAPED_CHARS_TABLE_SIZE]; |
| 21 | static Py_UNICODE *escaped_chars_repl[ESCAPED_CHARS_TABLE_SIZE]; |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 22 | |
| 23 | static int |
| 24 | init_constants(void) |
| 25 | { |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame^] | 26 | /* happing of characters to replace */ |
| 27 | escaped_chars_repl['"'] = UNICHR("""); |
| 28 | escaped_chars_repl['\''] = UNICHR("'"); |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 29 | escaped_chars_repl['&'] = UNICHR("&"); |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 30 | escaped_chars_repl['<'] = UNICHR("<"); |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 31 | escaped_chars_repl['>'] = UNICHR(">"); |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame^] | 32 | |
| 33 | /* lengths of those characters when replaced - 1 */ |
| 34 | memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len)); |
| 35 | escaped_chars_delta_len['"'] = escaped_chars_delta_len['\''] = \ |
| 36 | escaped_chars_delta_len['&'] = 4; |
| 37 | escaped_chars_delta_len['<'] = escaped_chars_delta_len['>'] = 3; |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 38 | |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame^] | 39 | /* import markup type so that we can mark the return value */ |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 40 | PyObject *module = PyImport_ImportModule("jinja2.utils"); |
| 41 | if (!module) |
| 42 | return 0; |
| 43 | markup = PyObject_GetAttrString(module, "Markup"); |
| 44 | Py_DECREF(module); |
| 45 | |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | static PyObject* |
| 50 | escape_unicode(PyUnicodeObject *in) |
| 51 | { |
| 52 | PyUnicodeObject *out; |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 53 | Py_UNICODE *inp = in->str; |
| 54 | const Py_UNICODE *inp_end = in->str + in->length; |
| 55 | Py_UNICODE *next_escp; |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 56 | Py_UNICODE *outp; |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 57 | Py_ssize_t delta=0, erepl=0, delta_len=0; |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 58 | |
| 59 | /* First we need to figure out how long the escaped string will be */ |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 60 | while (*(inp) || inp < inp_end) { |
| 61 | if (*inp < ESCAPED_CHARS_TABLE_SIZE && escaped_chars_delta_len[*inp]) { |
| 62 | delta += escaped_chars_delta_len[*inp]; |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 63 | ++erepl; |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 64 | } |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 65 | ++inp; |
| 66 | } |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 67 | |
| 68 | /* Do we need to escape anything at all? */ |
| 69 | if (!erepl) { |
| 70 | Py_INCREF(in); |
| 71 | return (PyObject*)in; |
| 72 | } |
| 73 | |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 74 | out = (PyUnicodeObject*)PyUnicode_FromUnicode(NULL, in->length + delta); |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 75 | if (!out) |
| 76 | return NULL; |
| 77 | |
| 78 | outp = out->str; |
| 79 | inp = in->str; |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 80 | while (erepl-- > 0) { |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 81 | /* look for the next substitution */ |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 82 | next_escp = inp; |
| 83 | while (next_escp < inp_end) { |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 84 | if (*next_escp < ESCAPED_CHARS_TABLE_SIZE && |
| 85 | (delta_len = escaped_chars_delta_len[*next_escp])) { |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 86 | ++delta_len; |
| 87 | break; |
| 88 | } |
| 89 | ++next_escp; |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 90 | } |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 91 | |
| 92 | if (next_escp > inp) { |
| 93 | /* copy unescaped chars between inp and next_escp */ |
| 94 | Py_UNICODE_COPY(outp, inp, next_escp-inp); |
| 95 | outp += next_escp-inp; |
| 96 | } |
| 97 | |
| 98 | /* escape 'next_escp' */ |
| 99 | Py_UNICODE_COPY(outp, escaped_chars_repl[*next_escp], delta_len); |
| 100 | outp += delta_len; |
| 101 | |
| 102 | inp = next_escp + 1; |
| 103 | } |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 104 | if (inp < inp_end) |
Mickaël Guérin | c0d40d3 | 2008-05-05 17:08:51 +0200 | [diff] [blame] | 105 | Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str)); |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 106 | |
| 107 | return (PyObject*)out; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static PyObject* |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 112 | escape(PyObject *self, PyObject *text) |
| 113 | { |
| 114 | PyObject *s = NULL, *rv = NULL; |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 115 | |
| 116 | /* we don't have to escape integers, bools or floats */ |
| 117 | if (PyInt_CheckExact(text) || PyLong_CheckExact(text) || |
| 118 | PyFloat_CheckExact(text) || PyBool_Check(text) || |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 119 | text == Py_None) |
| 120 | return PyObject_CallFunctionObjArgs(markup, text, NULL); |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 121 | |
| 122 | /* if the object has an __html__ method that performs the escaping */ |
| 123 | PyObject *html = PyObject_GetAttrString(text, "__html__"); |
| 124 | if (html) { |
| 125 | rv = PyObject_CallObject(html, NULL); |
| 126 | Py_DECREF(html); |
| 127 | return rv; |
| 128 | } |
| 129 | |
| 130 | /* otherwise make the object unicode if it isn't, then escape */ |
| 131 | PyErr_Clear(); |
| 132 | if (!PyUnicode_Check(text)) { |
| 133 | PyObject *unicode = PyObject_Unicode(text); |
| 134 | if (!unicode) |
| 135 | return NULL; |
| 136 | s = escape_unicode((PyUnicodeObject*)unicode); |
| 137 | Py_DECREF(unicode); |
| 138 | } |
| 139 | else |
| 140 | s = escape_unicode((PyUnicodeObject*)text); |
| 141 | |
| 142 | /* convert the unicode string into a markup object. */ |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 143 | rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL); |
| 144 | Py_DECREF(s); |
| 145 | return rv; |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame^] | 149 | static PyObject* |
| 150 | soft_unicode(PyObject *self, PyObject *s) |
| 151 | { |
| 152 | if (!PyUnicode_Check(s)) |
| 153 | return PyObject_Unicode(s); |
| 154 | Py_INCREF(s); |
| 155 | return s; |
| 156 | } |
| 157 | |
| 158 | |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 159 | static PyObject * |
| 160 | tb_set_next(PyObject *self, PyObject *args) |
| 161 | { |
| 162 | PyTracebackObject *tb, *old; |
| 163 | PyObject *next; |
| 164 | |
| 165 | if (!PyArg_ParseTuple(args, "O!O:tb_set_next", &PyTraceBack_Type, &tb, &next)) |
| 166 | return NULL; |
| 167 | if (next == Py_None) |
| 168 | next = NULL; |
| 169 | else if (!PyTraceBack_Check(next)) { |
| 170 | PyErr_SetString(PyExc_TypeError, |
| 171 | "tb_set_next arg 2 must be traceback or None"); |
| 172 | return NULL; |
| 173 | } |
| 174 | else |
| 175 | Py_INCREF(next); |
| 176 | |
| 177 | old = tb->tb_next; |
| 178 | tb->tb_next = (PyTracebackObject*)next; |
| 179 | Py_XDECREF(old); |
| 180 | |
| 181 | Py_INCREF(Py_None); |
| 182 | return Py_None; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | static PyMethodDef module_methods[] = { |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 187 | {"escape", (PyCFunction)escape, METH_O, |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 188 | "escape(s) -> markup\n\n" |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 189 | "Convert the characters &, <, >, and \" in string s to HTML-safe\n" |
Armin Ronacher | f35e281 | 2008-05-06 16:04:10 +0200 | [diff] [blame^] | 190 | "sequences. Use this if you need to display text that might contain\n" |
Armin Ronacher | 9a1e33c | 2008-05-05 22:00:46 +0200 | [diff] [blame] | 191 | "such characters in HTML. Marks return value as markup string."}, |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 192 | {"soft_unicode", (PyCFunction)soft_unicode, METH_O, |
| 193 | "soft_unicode(object) -> string\n\n" |
| 194 | "Make a string unicode if it isn't already. That way a markup\n" |
| 195 | "string is not converted back to unicode."}, |
Armin Ronacher | bd33f11 | 2008-04-18 09:17:32 +0200 | [diff] [blame] | 196 | {"tb_set_next", (PyCFunction)tb_set_next, METH_VARARGS, |
| 197 | "Set the tb_next member of a traceback object."}, |
| 198 | {NULL, NULL, 0, NULL} /* Sentinel */ |
| 199 | }; |
| 200 | |
| 201 | |
| 202 | #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ |
| 203 | #define PyMODINIT_FUNC void |
| 204 | #endif |
| 205 | PyMODINIT_FUNC |
| 206 | init_speedups(void) |
| 207 | { |
| 208 | if (!init_constants()) |
| 209 | return; |
| 210 | |
| 211 | Py_InitModule3("jinja2._speedups", module_methods, ""); |
| 212 | } |