classification
Title: Fixed the documentation of the mapping codec APIs
Type: behavior Stage: resolved
Components: Documentation, Unicode Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: docs@python, ezio.melotti, haypo, lemburg, serhiy.storchaka, xiang.zhang
Priority: normal Keywords: patch

Created on 2016-11-20 10:29 by serhiy.storchaka, last changed 2017-03-24 22:09 by serhiy.storchaka. This issue is now closed.

Files
File name Uploaded Description Edit
docs-PyUnicode_Translate.patch serhiy.storchaka, 2016-11-20 10:29 review
docs-PyUnicode_Translate-2.patch serhiy.storchaka, 2016-11-24 11:38 review
docs-PyUnicode_Translate-3.patch serhiy.storchaka, 2016-11-28 11:25 review
docs-PyUnicode_Translate-4.patch serhiy.storchaka, 2017-01-23 11:24 review
Pull Requests
URL Status Linked Edit
PR 487 merged serhiy.storchaka, 2017-03-05 22:01
PR 714 merged serhiy.storchaka, 2017-03-19 06:32
PR 715 merged serhiy.storchaka, 2017-03-19 06:37
Messages (14)
msg281259 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-20 10:29
Proposed patch adds the documentation of PyUnicode_Translate() and fixes the documentation of other mapping codec APIs (it is incorrect in Python 3): PyUnicode_DecodeCharmap(), PyUnicode_AsCharmapString(), PyUnicode_EncodeCharmap(), and PyUnicode_TranslateCharmap().
msg281629 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-24 11:38
Updated patch addresses Xiang's and Victor's comments.
msg281677 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-11-25 03:34
v2 LGTM.
msg281857 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2016-11-28 10:28
Why are you removing the introductory section on how mappings work ?

Also, this wording needs to be corrected: "bytes (integers in the range from 0 to 255)". Bytes are not integers. I'd suggest to use the more correct wording "bytes strings of length 1".
msg281864 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-28 11:24
Thanks Xiang. I forgot about comments in headers, the updated path updates them too.

> Why are you removing the introductory section on how mappings work ?

Because it is not correct. I copied it to descriptions of concrete functions with correcting it according to the peculiarity of particular function.

> Also, this wording needs to be corrected: "bytes (integers in the range from 0 to 255)". Bytes are not integers. I'd suggest to use the more correct wording "bytes strings of length 1".

The word "bytes" means here not Python bytes object, but is used in more common meaning: an integer in the range from 0 to 255.
msg281866 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2016-11-28 11:54
On 28.11.2016 12:24, Serhiy Storchaka wrote:
>> Why are you removing the introductory section on how mappings work ?
> 
> Because it is not correct. I copied it to descriptions of concrete functions with correcting it according to the peculiarity of particular function.

The only part that is not correct is "single string characters".
This should read "single bytes" or "bytes strings of length 1".

I also don't see where you copied the description. Without some
description of what "mappings" are in the context of the charmap
codec, it's not easy to understand what the purpose of these
APIs is. Please just fix the bytes wording instead of removing the
whole intro.

>> Also, this wording needs to be corrected: "bytes (integers in the range from 0 to 255)". Bytes are not integers. I'd suggest to use the more correct wording "bytes strings of length 1".
> 
> The word "bytes" means here not Python bytes object, but is used in more common meaning: an integer in the range from 0 to 255.

That's confusing, since we use the term "bytes" as referring
to the bytes object in Python. Please use "integers in the range
0-255".

Aside: The deprecation of PyUnicode_EncodeCharmap() also seems misplaced
in this context, since only the Py_UNICODE version of the API is
deprecated. The functionality still exists and is useful. An API
similar to the _PyUnicode_EncodeCharmap() API should be made publicly
available to accommodate for the deprecation, since the mentioned
PyUnicode_AsCharmapString() and PyUnicode_AsEncodedString()
APIs are not suitable as replacement. PyUnicode_AsCharmapString()
doesn't support error handling (strange, BTW) and
PyUnicode_AsEncodedString() has a completely unrelated meaning (no
idea why it's mentioned here at all).
msg281872 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-28 12:51
> The only part that is not correct is "single string characters".
> This should read "single bytes" or "bytes strings of length 1".

This is not correct. Decoding mappings map not bytes strings, but integers. 
And this is not the only incorrect part. Decoding mappings can map to 
multicharacter Unicode strings, not to single Unicode	 characters. Not just 
None, but the integer 0xfffe and Unicode string '\ufffe' mean "undefined 
mapping".

There are similar incorrectnesses about encoding mappings.

> I also don't see where you copied the description. Without some
> description of what "mappings" are in the context of the charmap
> codec, it's not easy to understand what the purpose of these
> APIs is. Please just fix the bytes wording instead of removing the
> whole intro.

Decoding mappings were desribed in the introduction and in the description of 
PyUnicode_DecodeCharmap() (both are outdated and incomplete). I merged and 
corrected descriptions and left it only in one place, since 
PyUnicode_DecodeCharmap() is the only function that needs this. Same for 
encoding mappings. Both decoding and encoding mappings do not have a relation 
to PyUnicode_Translate(). The paragraph about a LookupError in the 
introduction was totally wrong. I left in the introduction only common part. 
Other details are too different in decoding, encoding and translation mappings.

> >> Also, this wording needs to be corrected: "bytes (integers in the range
> >> from 0 to 255)". Bytes are not integers. I'd suggest to use the more
> >> correct wording "bytes strings of length 1".> 
> > The word "bytes" means here not Python bytes object, but is used in more
> > common meaning: an integer in the range from 0 to 255.
> That's confusing, since we use the term "bytes" as referring
> to the bytes object in Python. Please use "integers in the range
> 0-255".

Okay, I'll remove the word "bytes" here.  But how would you formulate the 
following sentence: "Unmapped bytes (ones which cause a :exc:`LookupError`) as 
well as mapped to ``None``, ``0xFFFE`` or ``'\ufffe'`` are treated as "undefined 
mapping" and cause an error."?

> Aside: The deprecation of PyUnicode_EncodeCharmap() also seems misplaced
> in this context, since only the Py_UNICODE version of the API is
> deprecated. The functionality still exists and is useful. An API
> similar to the _PyUnicode_EncodeCharmap() API should be made publicly
> available to accommodate for the deprecation, since the mentioned
> PyUnicode_AsCharmapString() and PyUnicode_AsEncodedString()
> APIs are not suitable as replacement. PyUnicode_AsCharmapString()
> doesn't support error handling (strange, BTW) and
> PyUnicode_AsEncodedString() has a completely unrelated meaning (no
> idea why it's mentioned here at all).

Only PyUnicode_EncodeCharmap() is deprecated, PyUnicode_AsCharmapString() is 
not deprecated. I placed the deprecated function just after its non-deprecated 
counerpart following the pattern for other deprecated functions. If you prefer 
I'll move both deprecated functions (PyUnicode_EncodeCharmap and 
PyUnicode_TranslateCharmap) together at the end of this section.

I don't know why PyUnicode_AsCharmapString() don't support the errors 
argument. I added PyUnicode_AsEncodedString() as a replacement (issue19569) 
because this is the only public non-deprecated way to do a charmap encoding 
with errors handling. There is no exact equivalent, but 
PyUnicode_AsCharmapString() and PyUnicode_AsEncodedString() cover different 
areas of using PyUnicode_EncodeCharmap().
msg286027 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-22 19:42
How can we move this issue forward? Marc-Andre, have I answered to your objections?
msg286064 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2017-01-23 09:14
>> The only part that is not correct is "single string characters".
>> This should read "single bytes" or "bytes strings of length 1".
>
> This is not correct. Decoding mappings map not bytes strings, but
integers.

Looking at the implementation, you're right. AFAIR, the first
incarnation of the charmap codec used single chars in Python 2.
I guess the documentation was never updated when the change was
made to use integers instead.

> And this is not the only incorrect part. Decoding mappings can map to
> multicharacter Unicode strings, not to single Unicode	 characters. Not
just
> None, but the integer 0xfffe and Unicode string '\ufffe' mean "undefined
> mapping".

Yes, this was added later on as well. Apparently the docs
were never updated.

> There are similar incorrectnesses about encoding mappings.

Ok, fair enough, let's remove the two paragraphs.

>> I also don't see where you copied the description. Without some
>> description of what "mappings" are in the context of the charmap
>> codec, it's not easy to understand what the purpose of these
>> APIs is. Please just fix the bytes wording instead of removing the
>> whole intro.
>
> Decoding mappings were desribed in the introduction and in the
description of
> PyUnicode_DecodeCharmap() (both are outdated and incomplete). I merged
and
> corrected descriptions and left it only in one place, since
> PyUnicode_DecodeCharmap() is the only function that needs this. Same for
> encoding mappings. Both decoding and encoding mappings do not have a
relation
> to PyUnicode_Translate(). The paragraph about a LookupError in the
> introduction was totally wrong. I left in the introduction only common
part.
> Other details are too different in decoding, encoding and translation
mappings.
>
>> >> Also, this wording needs to be corrected: "bytes (integers in the
range
>> >> from 0 to 255)". Bytes are not integers. I'd suggest to use the more
>> >> correct wording "bytes strings of length 1".>
>> > The word "bytes" means here not Python bytes object, but is used in
more
>> > common meaning: an integer in the range from 0 to 255.
>> That's confusing, since we use the term "bytes" as referring
>> to the bytes object in Python. Please use "integers in the range
>> 0-255".
>
> Okay, I'll remove the word "bytes" here. But how would you formulate the
> following sentence: "Unmapped bytes (ones which cause a
:exc:`LookupError`) as
> well as mapped to ``None``, ``0xFFFE`` or ``'\ufffe'`` are treated as
"undefined
> mapping" and cause an error."?

Better:

"""
If *mapping* is *NULL*, Latin-1 decoding will be applied.  Else
*mapping* must map bytes ordinals (integers in the range from 0 to 255)
to Unicode strings, integers (which are then interpreted as Unicode
ordinals) or ``None``. Unmapped data bytes - ones which cause a
:exc:`LookupError`, as well as ones which get mapped to ``None``,
``0xFFFE`` or ``'\ufffe'``, are treated as undefined mappings and cause
an error.
"""

>> Aside: The deprecation of PyUnicode_EncodeCharmap() also seems misplaced
>> in this context, since only the Py_UNICODE version of the API is
>> deprecated. The functionality still exists and is useful. An API
>> similar to the _PyUnicode_EncodeCharmap() API should be made publicly
>> available to accommodate for the deprecation, since the mentioned
>> PyUnicode_AsCharmapString() and PyUnicode_AsEncodedString()
>> APIs are not suitable as replacement. PyUnicode_AsCharmapString()
>> doesn't support error handling (strange, BTW) and
>> PyUnicode_AsEncodedString() has a completely unrelated meaning (no
>> idea why it's mentioned here at all).
>
> Only PyUnicode_EncodeCharmap() is deprecated,
PyUnicode_AsCharmapString() is
> not deprecated. I placed the deprecated function just after its
non-deprecated
> counerpart following the pattern for other deprecated functions. If
you prefer
> I'll move both deprecated functions (PyUnicode_EncodeCharmap and
> PyUnicode_TranslateCharmap) together at the end of this section.

No, I'd prefer this deprecation to be undone as long as we
don't have a proper alternative for the API.

Looking at the various deprecations for the Py_UNICODE APIs,
I find that the Unicode API symmetry was severely broken.
In the Python 2 API, we always have an PyUnicode_Encode...() and
corresponding PyUnicode_Decode...() API for every codec.

In Python 3, the encode APIs were apparently all deprecated
due to their use of Py_UNICODE and only the the much less useful
PyUnicode_As...String() APIs were left, which intentionally do not
have an error argument, because they were intended as quick
replacement for PyString_AsString() uses in Python 2.

> I don't know why PyUnicode_AsCharmapString() don't support the errors
> argument. I added PyUnicode_AsEncodedString() as a replacement
(issue19569)
> because this is the only public non-deprecated way to do a charmap
encoding
> with errors handling. There is no exact equivalent, but
> PyUnicode_AsCharmapString() and PyUnicode_AsEncodedString() cover
different
> areas of using PyUnicode_EncodeCharmap().

The only way around this is to have new APIs to reestablish
the previous encoding functionality (only based on
PyObject *unicode instead of Py_UNICODE) at the C API level.
msg286075 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-01-23 11:24
"bytes ordinals" is good term. Thank you. Here is an updated patch.

> No, I'd prefer this deprecation to be undone as long as we
> don't have a proper alternative for the API.

This is different issue.
msg289484 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-12 08:09
Do you still have objections Marc-Andre?
msg290155 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-24 22:06
New changeset 88b32eb7b317dd7c7943433f980e17e34e50f8f8 by Serhiy Storchaka in branch '3.5':
bpo-28749: Fixed the documentation of the mapping codec APIs. (#487) (#715)
https://github.com/python/cpython/commit/88b32eb7b317dd7c7943433f980e17e34e50f8f8
msg290156 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-24 22:06
New changeset 69eab3123ed1de4bed4b7dedecabe415f6139bb6 by Serhiy Storchaka in branch '3.6':
bpo-28749: Fixed the documentation of the mapping codec APIs. (#487) (#714)
https://github.com/python/cpython/commit/69eab3123ed1de4bed4b7dedecabe415f6139bb6
msg290166 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-24 22:09
New changeset c85a26628ceb9624c96c3064e8b99033c026d8a3 by Serhiy Storchaka in branch 'master':
bpo-28749: Fixed the documentation of the mapping codec APIs. (#487)
https://github.com/python/cpython/commit/c85a26628ceb9624c96c3064e8b99033c026d8a3
History
Date User Action Args
2017-03-24 22:09:26serhiy.storchakasetmessages: + msg290166
2017-03-24 22:06:26serhiy.storchakasetmessages: + msg290156
2017-03-24 22:06:17serhiy.storchakasetmessages: + msg290155
2017-03-21 08:14:55serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-03-19 06:37:41serhiy.storchakasetpull_requests: + pull_request635
2017-03-19 06:32:38serhiy.storchakasetpull_requests: + pull_request634
2017-03-12 08:09:08serhiy.storchakasetmessages: + msg289484
2017-03-05 22:01:42serhiy.storchakasetpull_requests: + pull_request400
2017-02-02 19:10:40serhiy.storchakasetassignee: docs@python -> serhiy.storchaka
2017-01-23 11:24:38serhiy.storchakasetfiles: + docs-PyUnicode_Translate-4.patch

messages: + msg286075
2017-01-23 09:14:32lemburgsetmessages: + msg286064
2017-01-22 19:42:48serhiy.storchakasetmessages: + msg286027
2016-11-28 12:51:42serhiy.storchakasetmessages: + msg281872
2016-11-28 11:54:04lemburgsetmessages: + msg281866
2016-11-28 11:25:11serhiy.storchakasetfiles: + docs-PyUnicode_Translate-3.patch
2016-11-28 11:24:57serhiy.storchakasetmessages: + msg281864
2016-11-28 10:28:34lemburgsetnosy: + lemburg
messages: + msg281857
2016-11-25 03:34:51xiang.zhangsetnosy: + xiang.zhang
messages: + msg281677
2016-11-24 11:38:50serhiy.storchakasetfiles: + docs-PyUnicode_Translate-2.patch

messages: + msg281629
2016-11-20 10:29:58serhiy.storchakacreate