New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyUnicode_AsDecodedObject can only return unicode now #72612
Comments
PyUnicode_AsDecodedObject was added in f46d49e2e0f0 and became an API in 2284fa89ab08. It seems its intention is to return a Python object. But during evolution, with commits 5f11621a6f51 and 123f2dc08b3e, it can only return unicode now, becoming another version of PyUnicode_AsDecodedUnicode. Is this the wanted behaviour? |
Indeed. The only difference is that PyUnicode_AsDecodedUnicode fails for most encodings (except rot13), but PyUnicode_AsDecodedObject just crashes in debug build. It seems to me that these functions (as well as PyUnicode_AsEncodedUnicode) shouldn't exist it Python 3. None of these functions are documented. PyUnicode_AsDecodedObject emits Py3k warning in 2.7. PyUnicode_AsDecodedUnicode and PyUnicode_AsEncodedUnicode were added in Python 3 (2284fa89ab08), and the purpose of this is not clear to me. They work only with rot13, but general PyCodec_Decode and PyCodec_Encode can be used instead. Could you please explain Marc-André? |
PyUnicode_AsDecodedObject() and PyUnicode_AsEncodedObject() were meant as C API implementations of the unicode.decode() and unicode.encode() methods in Python2. Not having PyUnicode_AsDecodedObject() documented was likely an oversight on my part. In Python2, unicode.decode() and unicode.encode() were more or less direct interfaces to the codec registry. In Python 2.7 this was changed to issue a warning for porting to Python 3. In Python3, the methods were changed to only return unicode objects and to reflect this change without breaking the C API, the new PyUnicode_AsDecodedUnicode() and PyUnicode_AsEncodedUnicode() were added. I guess the more recent changes simply didn't pay attention to this difference anymore and put restrictions on the output of PyUnicode_AsDecodedObject() and PyUnicode_AsEncodedObject() which were not originally intended, hence the crash you are seeing, Serhiy. Going forward, C extensions in Python3 could indeed use the PyCodec_*() APIs directly. |
Shouldn't all these function be deprecated in favour of PyCodec_*() APIs? |
On 13.10.2016 10:10, Serhiy Storchaka wrote:
Not all of them, since you still want to have a C API for As for the others, I don't know how much use they get. |
I propose following:
|
+1 for 2). Patch looks good. |
Marc-Andre, what are your thoughts about this? |
As I already mentioned, PyUnicode_AsEncodedUnicode() needs to stay, since it's the C API for unicode.encode(). The others can be deprecated. |
Marc-Andre, shouldn't the C API of unicode.encode() be PyUnicode_AsEncodedString instead of PyUnicode_AsEncodedUnicode now? BTW Serhiy, how about PyUnicode_AsEncodedObject? Not see it in your deprecate list. |
On 25.10.2016 04:16, Xiang Zhang wrote:
You're right. I got confused with all the slight variations.
Let's see what we have: PyUnicode_AsEncodedString(): encode to bytes (unicode.encode()) codecs.encode() can be used for the last two. |
New changeset 71dce630dc02 by Serhiy Storchaka in branch '3.4': New changeset 74569ecd67e4 by Serhiy Storchaka in branch '3.5': New changeset 6af1a26e655f by Serhiy Storchaka in branch '3.6': New changeset 1ab1fd00e9d6 by Serhiy Storchaka in branch 'default': |
Indeed. It is not such useless as other functions (that support only the rot13 encoding), but it can be replaced with either PyUnicode_AsEncodedString or PyCodec_Encode (both exists in 2.7). Updated patch deprecates PyUnicode_AsEncodedObject too. Please make a review of warning messages, I'm not sure about the wording. |
LGTM. I can understand the wording. |
New changeset 15a494886c5a by Serhiy Storchaka in branch '3.6': New changeset 50c28727d91c by Serhiy Storchaka in branch 'default': |
Thanks Xiang. |
s/that encode form str/that encode from str/ in Include/unicodeobject.h |
Misc/NEWS
so that it is managed by towncrier #552Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: