mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
_copy_characters(): move debug code at the top to avoid noisy #ifdef
And don't use assert() anymore if check_maxchar is set: return -1 on error instead.
This commit is contained in:
parent
6319e0fa20
commit
f185226244
1 changed files with 23 additions and 26 deletions
|
@ -1148,27 +1148,31 @@ _copy_characters(PyObject *to, Py_ssize_t to_start,
|
||||||
to_kind = PyUnicode_KIND(to);
|
to_kind = PyUnicode_KIND(to);
|
||||||
to_data = PyUnicode_DATA(to);
|
to_data = PyUnicode_DATA(to);
|
||||||
|
|
||||||
|
#ifdef Py_DEBUG
|
||||||
|
if (!check_maxchar
|
||||||
|
&& PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to))
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
||||||
if (from_kind == to_kind) {
|
if (from_kind == to_kind) {
|
||||||
if (!PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to)) {
|
if (check_maxchar
|
||||||
|
&& !PyUnicode_IS_ASCII(from) && PyUnicode_IS_ASCII(to))
|
||||||
|
{
|
||||||
/* Writing Latin-1 characters into an ASCII string requires to
|
/* Writing Latin-1 characters into an ASCII string requires to
|
||||||
check that all written characters are pure ASCII */
|
check that all written characters are pure ASCII */
|
||||||
#ifndef Py_DEBUG
|
Py_UCS4 max_char;
|
||||||
if (check_maxchar) {
|
max_char = ucs1lib_find_max_char(from_data,
|
||||||
Py_UCS4 max_char;
|
(Py_UCS1*)from_data + how_many);
|
||||||
max_char = ucs1lib_find_max_char(from_data,
|
if (max_char >= 128)
|
||||||
(Py_UCS1*)from_data + how_many);
|
return -1;
|
||||||
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
|
|
||||||
}
|
}
|
||||||
Py_MEMCPY((char*)to_data + to_kind * to_start,
|
Py_MEMCPY((char*)to_data + to_kind * to_start,
|
||||||
(char*)from_data + from_kind * from_start,
|
(char*)from_data + from_kind * from_start,
|
||||||
|
@ -1207,7 +1211,6 @@ _copy_characters(PyObject *to, Py_ssize_t to_start,
|
||||||
else {
|
else {
|
||||||
assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to));
|
assert (PyUnicode_MAX_CHAR_VALUE(from) > PyUnicode_MAX_CHAR_VALUE(to));
|
||||||
|
|
||||||
#ifndef Py_DEBUG
|
|
||||||
if (!check_maxchar) {
|
if (!check_maxchar) {
|
||||||
if (from_kind == PyUnicode_2BYTE_KIND
|
if (from_kind == PyUnicode_2BYTE_KIND
|
||||||
&& to_kind == PyUnicode_1BYTE_KIND)
|
&& to_kind == PyUnicode_1BYTE_KIND)
|
||||||
|
@ -1244,21 +1247,15 @@ _copy_characters(PyObject *to, Py_ssize_t to_start,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
#endif
|
|
||||||
{
|
|
||||||
const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to);
|
const Py_UCS4 to_maxchar = PyUnicode_MAX_CHAR_VALUE(to);
|
||||||
Py_UCS4 ch;
|
Py_UCS4 ch;
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
|
|
||||||
for (i=0; i < how_many; i++) {
|
for (i=0; i < how_many; i++) {
|
||||||
ch = PyUnicode_READ(from_kind, from_data, from_start + i);
|
ch = PyUnicode_READ(from_kind, from_data, from_start + i);
|
||||||
#ifndef Py_DEBUG
|
|
||||||
if (ch > to_maxchar)
|
if (ch > to_maxchar)
|
||||||
return -1;
|
return -1;
|
||||||
#else
|
|
||||||
assert(ch <= to_maxchar);
|
|
||||||
#endif
|
|
||||||
PyUnicode_WRITE(to_kind, to_data, to_start + i, ch);
|
PyUnicode_WRITE(to_kind, to_data, to_start + i, ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue