This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients ezio.melotti, josh.r, pitrou, serhiy.storchaka, vstinner
Date 2016-11-13.16:21:13
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1479054073.91.0.614064396979.issue21449@psf.upfronthosting.co.za>
In-reply-to
Content
This issue is not just about readability or performance. It is about correctness, since none of callers check for failure of _PyUnicode_CompareWithId.

I just came to the same problem from other side (replacing PyUnicode_CompareWithASCII with PyUnicode_EqualToASCII or _PyUnicode_CompareWithId). Josh's idea in general LGTM, but there are few details:

1. None of callers check for failure of new functions as well. In boolean context the failure is interpreted as true. What is even worse, an exception is set, and this can cause a crash in debug build.

If we don't want to rewrite all the uses of _PyUnicode_CompareWithId by adding the code for checking return value for error, we should make new function never failing. _PyUnicode_CompareWithId can fail if the first argument is not valid string or if there is no memory for allocating a Unicode object for identifier. I think it is better to return false value in these circumstances.

If the first argument is not string, it isn't equal to an identifier. If it is an invalid string, it can't be equal too. If there is no memory for allocating a Unicode object for identifier, we should fallback to comparing the raw content (like in PyUnicode_CompareWithASCII).

2. It may be worth to replace _PyUnicode_FromId with the macro:

#define _PyUnicode_FROM_ID(id) ((id)->object ? (id)->object : _PyUnicode_FromId(id))

Or rename _PyUnicode_FromId to _PyUnicode_FromIdInternal and _PyUnicode_FROM_ID to _PyUnicode_FromId? This would save us from rewriting a lot of code that uses _PyUnicode_FromId.

3. The name _PyUnicode_CompareWithIdEqual looks too long to me. What about _PyUnicode_EqualToId?

4. Pointers could be checked for equality before checking PyUnicode_CHECK_INTERNED(left). The former check is much cheaper than the latter.
History
Date User Action Args
2016-11-13 16:21:14serhiy.storchakasetrecipients: + serhiy.storchaka, pitrou, vstinner, ezio.melotti, josh.r
2016-11-13 16:21:13serhiy.storchakasetmessageid: <1479054073.91.0.614064396979.issue21449@psf.upfronthosting.co.za>
2016-11-13 16:21:13serhiy.storchakalinkissue21449 messages
2016-11-13 16:21:13serhiy.storchakacreate