From: Armin Ronacher Date: Mon, 5 May 2008 20:00:46 +0000 (+0200) Subject: added macro for char* to Py_UNICODE conversion X-Git-Tag: 2.0rc1~94 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=9a1e33c2ef323ed38a9fdd0cbb175d4304e08e1f;p=jinja2.git added macro for char* to Py_UNICODE conversion --HG-- branch : trunk --- diff --git a/jinja2/_speedups.c b/jinja2/_speedups.c index ca3497e..d8df390 100644 --- a/jinja2/_speedups.c +++ b/jinja2/_speedups.c @@ -13,10 +13,10 @@ #include -static PyObject* markup; - #define ESCAPED_CHARS_TABLE_SIZE 63 +#define UNICHR(x) (((PyUnicodeObject*)PyUnicode_DecodeASCII(x, strlen(x), NULL))->str); +static PyObject* markup; static Py_ssize_t escaped_chars_delta_len[ESCAPED_CHARS_TABLE_SIZE]; static Py_UNICODE *escaped_chars_repl[ESCAPED_CHARS_TABLE_SIZE]; @@ -24,19 +24,18 @@ static int init_constants(void) { memset(escaped_chars_delta_len, 0, sizeof (escaped_chars_delta_len)); - /* memset(escaped_chars_repl, 0, sizeof (escaped_chars_repl)); */ escaped_chars_delta_len['"'] = 5; - escaped_chars_repl['"'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII(""", 6, NULL))->str; + escaped_chars_repl['"'] = UNICHR("""); escaped_chars_delta_len['&'] = 3; - escaped_chars_repl['&'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("&", 5, NULL))->str; + escaped_chars_repl['&'] = UNICHR("&"); escaped_chars_delta_len['<'] = 3; - escaped_chars_repl['<'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII("<", 4, NULL))->str; + escaped_chars_repl['<'] = UNICHR("<"); escaped_chars_delta_len['>'] = 3; - escaped_chars_repl['>'] = ((PyUnicodeObject*)PyUnicode_DecodeASCII(">", 4, NULL))->str; + escaped_chars_repl['>'] = UNICHR(">"); PyObject *module = PyImport_ImportModule("jinja2.utils"); if (!module) @@ -79,10 +78,11 @@ escape_unicode(PyUnicodeObject *in) outp = out->str; inp = in->str; while (erepl-- > 0) { - /* look for the next sustitution */ + /* look for the next substitution */ next_escp = inp; while (next_escp < inp_end) { - if (*next_escp < ESCAPED_CHARS_TABLE_SIZE && (delta_len = escaped_chars_delta_len[*next_escp])) { + if (*next_escp < ESCAPED_CHARS_TABLE_SIZE && + (delta_len = escaped_chars_delta_len[*next_escp])) { ++delta_len; break; } @@ -101,9 +101,8 @@ escape_unicode(PyUnicodeObject *in) inp = next_escp + 1; } - if (inp < inp_end) { + if (inp < inp_end) Py_UNICODE_COPY(outp, inp, in->length - (inp - in->str)); - } return (PyObject*)out; } @@ -186,10 +185,10 @@ tb_set_next(PyObject *self, PyObject *args) static PyMethodDef module_methods[] = { {"escape", (PyCFunction)escape, METH_O, - "escape(s) -> string\n\n" + "escape(s) -> markup\n\n" "Convert the characters &, <, >, and \" in string s to HTML-safe\n" "sequences. Use this if you need to display text that might contain\n" - "such characters in HTML."}, + "such characters in HTML. Marks return value as markup string."}, {"soft_unicode", (PyCFunction)soft_unicode, METH_O, "soft_unicode(object) -> string\n\n" "Make a string unicode if it isn't already. That way a markup\n" diff --git a/jinja2/utils.py b/jinja2/utils.py index 9437023..13b67f9 100644 --- a/jinja2/utils.py +++ b/jinja2/utils.py @@ -480,7 +480,7 @@ except ImportError: def escape(s): """Convert the characters &, <, >, and " in string s to HTML-safe sequences. Use this if you need to display text that might contain - such characters in HTML. + such characters in HTML. Marks return value as markup string. """ if hasattr(s, '__html__'): return s.__html__()