[Python-Dev] cpython: Optimize _PyUnicode_FastCopyCharacters() when maxchar(from) > maxchar(to)

Antoine Pitrou solipsis at pitrou.net
Sat Jun 16 10:23:30 CEST 2012


On Sat, 16 Jun 2012 02:29:09 +0200
victor.stinner <python-checkins at python.org> wrote:
> +    if (from_kind == to_kind) {
> +        if (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) {
> +            /* Writing Latin-1 characters into an ASCII string requires to
> +               check that all written characters are pure ASCII */
> +#ifndef Py_DEBUG
> +            if (check_maxchar) {
> +                Py_UCS4 max_char;
> +                max_char = ucs1lib_find_max_char(from_data,
> +                                                 (char*)from_data + how_many);
> +                if (max_char >= 128)
> +                    return -1;
> +            }
> +#else
> +            const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to);
> +            Py_UCS4 ch;
> +            Py_ssize_t i;
> +            for (i=0; i < how_many; i++) {
> +                ch = PyUnicode_READ(from_kind, from_data, from_start + i);
> +                assert(ch <= to_maxchar);
> +            }
> +#endif

So you're returning -1 in release mode but you're crashing (assert())
in debug mode? Why that?

> +#ifndef Py_DEBUG
> +        if (!check_maxchar) {
[...]

This means the optimizations are not exercised in debug mode?
That sounds like a bad idea.

Regards

Antoine.




More information about the Python-Dev mailing list