Skip to content
This repository
Browse code

added macro for char* to Py_UNICODE conversion

--HG--
branch : trunk
  • Loading branch information...
commit 9a1e33c2ef323ed38a9fdd0cbb175d4304e08e1f 1 parent c0d40d3
Armin Ronacher authored

Showing 2 changed files with 13 additions and 14 deletions. Show diff stats Hide diff stats

  1. +12 13 jinja2/_speedups.c
  2. +1 1  jinja2/utils.py
25 jinja2/_speedups.c
@@ -13,10 +13,10 @@
13 13
14 14 #include <Python.h>
15 15
16   -static PyObject* markup;
17   -
18 16 #define ESCAPED_CHARS_TABLE_SIZE 63
  17 +#define UNICHR(x) (((PyUnicodeObject*)PyUnicode_DecodeASCII(x, strlen(x), NULL))->str);
19 18
  19 +static PyObject* markup;
20 20 static Py_ssize_t escaped_chars_delta_len[ESCAPED_CHARS_TABLE_SIZE];
21 21 static Py_UNICODE *escaped_chars_repl[ESCAPED_CHARS_TABLE_SIZE];
22 22
@@ -24,19 +24,18 @@ static int
24 24 init_constants(void)
25 25 {
26 26 memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len));
27   - /* memset(escaped_chars_repl, 0, sizeof (escaped_chars_repl)); */
28 27
29 28 escaped_chars_delta_len['"'] = 5;
30   - escaped_chars_repl['"'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&quot;", 6, NULL))->str;
  29 + escaped_chars_repl['"'] = UNICHR("&quot;");
31 30
32 31 escaped_chars_delta_len['&'] = 3;
33   - escaped_chars_repl['&'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&amp;", 5, NULL))->str;
  32 + escaped_chars_repl['&'] = UNICHR("&amp;");
34 33
35 34 escaped_chars_delta_len['<'] = 3;
36   - escaped_chars_repl['<'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&lt;", 4, NULL))->str;
  35 + escaped_chars_repl['<'] = UNICHR("&lt;");
37 36
38 37 escaped_chars_delta_len['>'] = 3;
39   - escaped_chars_repl['>'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&gt;", 4, NULL))->str;
  38 + escaped_chars_repl['>'] = UNICHR("&gt;");
40 39
41 40 PyObject *module = PyImport_ImportModule("jinja2.utils");
42 41 if (!module)
@@ -79,10 +78,11 @@ escape_unicode(PyUnicodeObject *in)
79 78 outp = out->str;
80 79 inp = in->str;
81 80 while (erepl-- > 0) {
82   - /* look for the next sustitution */
  81 + /* look for the next substitution */
83 82 next_escp = inp;
84 83 while (next_escp < inp_end) {
85   - if (*next_escp < ESCAPED_CHARS_TABLE_SIZE && (delta_len = escaped_chars_delta_len[*next_escp])) {
  84 + if (*next_escp < ESCAPED_CHARS_TABLE_SIZE &&
  85 + (delta_len = escaped_chars_delta_len[*next_escp])) {
86 86 ++delta_len;
87 87 break;
88 88 }
@@ -101,9 +101,8 @@ escape_unicode(PyUnicodeObject *in)
101 101
102 102 inp = next_escp + 1;
103 103 }
104   - if (inp < inp_end) {
  104 + if (inp < inp_end)
105 105 Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str));
106   - }
107 106
108 107 return (PyObject*)out;
109 108 }
@@ -186,10 +185,10 @@ tb_set_next(PyObject *self, PyObject *args)
186 185
187 186 static PyMethodDef module_methods[] = {
188 187 {"escape", (PyCFunction)escape, METH_O,
189   - "escape(s) -> string\n\n"
  188 + "escape(s) -> markup\n\n"
190 189 "Convert the characters &, <, >, and \" in string s to HTML-safe\n"
191 190 "sequences. Use this if you need to display text that might contain\n"
192   - "such characters in HTML."},
  191 + "such characters in HTML. Marks return value as markup string."},
193 192 {"soft_unicode", (PyCFunction)soft_unicode, METH_O,
194 193 "soft_unicode(object) -> string\n\n"
195 194 "Make a string unicode if it isn't already. That way a markup\n"
2  jinja2/utils.py
@@ -480,7 +480,7 @@ def __reversed__(self):
480 480 def escape(s):
481 481 """Convert the characters &, <, >, and " in string s to HTML-safe
482 482 sequences. Use this if you need to display text that might contain
483   - such characters in HTML.
  483 + such characters in HTML. Marks return value as markup string.
484 484 """
485 485 if hasattr(s, '__html__'):
486 486 return s.__html__()

0 comments on commit 9a1e33c

Please sign in to comment.
Something went wrong with that request. Please try again.