From 56c161ab00bc1fe3fbac6777576105770d024581 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 6 Oct 2011 02:47:11 +0200 Subject: [PATCH] _copy_characters() fails more quickly in debug mode on inconsistent state --- Objects/unicodeobject.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 21cafb32515..a64f795fe38 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1052,20 +1052,32 @@ _copy_characters(PyObject *to, Py_ssize_t to_start, Py_UCS4 ch; Py_ssize_t i; +#ifdef Py_DEBUG for (i=0; i < how_many; i++) { ch = PyUnicode_READ(from_kind, from_data, from_start + i); - if (check_maxchar) { - if (ch > to_maxchar) - return 1; - } - else { - assert(ch <= to_maxchar); - } + assert(ch <= to_maxchar); PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); } +#else + if (!check_maxchar) { + for (i=0; i < how_many; i++) { + ch = PyUnicode_READ(from_kind, from_data, from_start + i); + PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); + } + } + else { + for (i=0; i < how_many; i++) { + ch = PyUnicode_READ(from_kind, from_data, from_start + i); + if (ch > to_maxchar) + return 1; + PyUnicode_WRITE(to_kind, to_data, to_start + i, ch); + } + } +#endif } else { - return -1; + assert(0 && "inconsistent state"); + return 1; } } return 0;