[New-bugs-announce] [issue28701] Replace PyUnicode_CompareWithASCIIString with _PyUnicode_EqualToASCIIString

Serhiy Storchaka report at bugs.python.org
Tue Nov 15 14:50:05 EST 2016


New submission from Serhiy Storchaka:

Proposed patch replaces calls of public function PyUnicode_CompareWithASCIIString() with new private function _PyUnicode_EqualToASCIIString(). The problem with  PyUnicode_CompareWithASCIIString() is that it returns -1 for the result "less than" and error, but the error case is never checked. The patch is purposed for following purposes:

1. Readability. ``_PyUnicode_EqualToASCIIString(...)`` looks more readable than ``PyUnicode_CompareWithASCIIString(...) == 0`` or ``!PyUnicode_CompareWithASCIIString(...)``, especially in large expression. I always have to make an effort to understand correctly the meaning of the latter expression.

2. Efficiency. If the strings are not equal, _PyUnicode_EqualToASCIIString() can quickly return false, but PyUnicode_CompareWithASCIIString() needs to check whether what string is larger.

3. Correctness. Since no caller checks the error of PyUnicode_CompareWithASCIIString(), it is incorrectly interpreted as "less then". Exception set by PyUnicode_CompareWithASCIIString() can be leaked in following code causing mystical error or crash in debug build. There are too many callers to add error checking for them all. These would be non-trivial error-prone changes that add new lines of the code, new variables and new returns or gotos. On other hand replacing PyUnicode_CompareWithASCIIString() with _PyUnicode_EqualToASCIIString() is done by simple script.

_PyUnicode_EqualToASCIIString() returns true value (1) if strings are equal, false value (0) if they are different, and doesn't raise exceptions. Unlike to PyUnicode_CompareWithASCIIString() it works only with ASCII characters and returns false if any string contains non-ASCII characters.

The patch also documents the return value of PyUnicode_CompareWithASCIIString() in case of error.

See issue21449 for similar issue with _PyUnicode_CompareWithId().

----------
components: Interpreter Core, Unicode
files: _PyUnicode_EqualToASCIIString.patch
keywords: patch
messages: 280882
nosy: ezio.melotti, haypo, josh.r, serhiy.storchaka, xiang.zhang
priority: normal
severity: normal
stage: patch review
status: open
title: Replace PyUnicode_CompareWithASCIIString with _PyUnicode_EqualToASCIIString
type: behavior
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7
Added file: http://bugs.python.org/file45490/_PyUnicode_EqualToASCIIString.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28701>
_______________________________________


More information about the New-bugs-announce mailing list