added macro for char* to Py_UNICODE conversion
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent c0d40d3 · Browse files · 2 files·+13 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+13 added−14 removed
- other:+13 added−14 removed
Provenance
- Commit
9a1e33c- Tree
3cf047d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
c0d40d3
2 files changed+13 added−14 removed
modified:jinja2/_speedups.c+12 added-13 removed
11 unmodified lines12 */13 14 #include <Python.h>15 16-static PyObject* markup;17-16 #define ESCAPED_CHARS_TABLE_SIZE 6317+#define UNICHR(x) (((PyUnicodeObject*)PyUnicode_DecodeASCII(x, strlen(x), NULL))->str);18 19+static PyObject* markup;20 static Py_ssize_t escaped_chars_delta_len[ESCAPED_CHARS_TABLE_SIZE];21 static Py_UNICODE *escaped_chars_repl[ESCAPED_CHARS_TABLE_SIZE];22 23 static int24 init_constants(void)25 {26 memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len));27- /* memset(escaped_chars_repl, 0, sizeof (escaped_chars_repl)); */27 28 escaped_chars_delta_len['"'] = 5;30- escaped_chars_repl['"'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII(""", 6, NULL))->str;29+ escaped_chars_repl['"'] = UNICHR(""");30 31 escaped_chars_delta_len['&'] = 3;33- escaped_chars_repl['&'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&", 5, NULL))->str;32+ escaped_chars_repl['&'] = UNICHR("&");33 34 escaped_chars_delta_len['<'] = 3;36- escaped_chars_repl['<'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("<", 4, NULL))->str;35+ escaped_chars_repl['<'] = UNICHR("<");36 37 escaped_chars_delta_len['>'] = 3;39- escaped_chars_repl['>'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII(">", 4, NULL))->str;38+ escaped_chars_repl['>'] = UNICHR(">");39 40 PyObject *module = PyImport_ImportModule("jinja2.utils");41 if (!module)42 return 0;34 unmodified lines77 78 outp = out->str;79 inp = in->str;80 while (erepl-- > 0) {82- /* look for the next sustitution */81+ /* look for the next substitution */82 next_escp = inp;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 ++delta_len;87 break;88 }89 ++next_escp;10 unmodified lines100 outp += delta_len;101 102 inp = next_escp + 1;103 }104- if (inp < inp_end) {104+ if (inp < inp_end)105 Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str));106- }106 107 return (PyObject*)out;108 }109 74 unmodified lines184 185 186 static PyMethodDef module_methods[] = {187 {"escape", (PyCFunction)escape, METH_O,189- "escape(s) -> string\n\n"188+ "escape(s) -> markup\n\n"189 "Convert the characters &, <, >, and \" in string s to HTML-safe\n"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."},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."},17 unmodified linesmodified:jinja2/utils.py+1 added-1 removed
478 unmodified lines479 except ImportError:480 def escape(s):481 """Convert the characters &, <, >, and " in string s to HTML-safe482 sequences. Use this if you need to display text that might contain483- such characters in HTML.483+ such characters in HTML. Marks return value as markup string.484 """485 if hasattr(s, '__html__'):486 return s.__html__()487 return Markup(unicode(s)27 unmodified lines