Visitar URL original
Reversing an encoding with unicode-escape returns a different result · Issue #65530 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20231124042307/https://github.com/python/cpython/issues/65530
Skip to content
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

Reversing an encoding with unicode-escape returns a different result #65530

Open
Sworddragon mannequin opened this issue Apr 22, 2014 · 14 comments
Open

Reversing an encoding with unicode-escape returns a different result #65530

Sworddragon mannequin opened this issue Apr 22, 2014 · 14 comments
Labels
topic-unicode type-bug An unexpected behavior, bug, or error

Comments

@Sworddragon
Copy link
Mannequin

Sworddragon mannequin commented Apr 22, 2014

BPO 21331
Nosy @malemburg, @loewis, @ncoghlan, @vstinner, @ezio-melotti, @bitdancer, @serhiy-storchaka

Note: 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:

assignee = None
closed_at = None
created_at = <Date 2014-04-22.20:58:23.698>
labels = ['type-bug', 'expert-unicode']
title = 'Reversing an encoding with unicode-escape returns a different result'
updated_at = <Date 2014-06-24.10:08:35.712>
user = 'https://bugs.python.org/Sworddragon'

bugs.python.org fields:

activity = <Date 2014-06-24.10:08:35.712>
actor = 'lemburg'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Unicode']
creation = <Date 2014-04-22.20:58:23.698>
creator = 'Sworddragon'
dependencies = []
files = []
hgrepos = []
issue_num = 21331
keywords = []
message_count = 14.0
messages = ['217021', '217024', '217033', '217055', '217094', '217095', '217096', '218519', '221191', '221198', '221204', '221308', '221442', '221447']
nosy_count = 8.0
nosy_names = ['lemburg', 'loewis', 'ncoghlan', 'vstinner', 'ezio.melotti', 'r.david.murray', 'Sworddragon', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue21331'
versions = ['Python 3.4']

@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Apr 22, 2014

I have made some tests with encoding/decoding in conjunction with unicode-escape and got some strange results:

>>> print('ä')
ä
>>> print('ä'.encode('utf-8'))
b'\xc3\xa4'
>>> print('ä'.encode('utf-8').decode('unicode-escape'))
ä
>>> print('ä'.encode('utf-8').decode('unicode-escape').encode('unicode-escape'))
b'\\xc3\\xa4'
>>> print('ä'.encode('utf-8').decode('unicode-escape').encode('unicode-escape').decode('utf-8'))
\xc3\xa4

Shouldn't .decode('unicode-escape').encode('unicode-escape') nullify itself and so "'ä'.encode('utf-8').decode('unicode-escape').encode('unicode-escape')" return the same result as 'ä'.encode('utf-8')?

@Sworddragon Sworddragon mannequin added topic-unicode type-bug An unexpected behavior, bug, or error labels Apr 22, 2014
@bitdancer
Copy link
Member

No. x.encode('unicode-escape').decode('unicode-escape') should return the same result, and it does.

The bug, I think, is that bytes.decode('unicode-escape') is not objecting to the non-ascii characters. It appears to be treating them as latin1, and that strikes me as broken.

@vstinner
Copy link
Member

unicode_escape codec is deprecated since Python 3.3. Please use UTF-8 or
something else.

@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Apr 23, 2014

The documentation says that unicode_internal is deprecated since Python 3.3 but not unicode_escape. Also, isn't unicode_escape different from utf-8? For example my original intention was to convert 2 byte string characters to their control characters. For example the file test.txt contains the 17 byte utf-8 raw content "---a---\n---ä---". Now I want to convert '\\n' to '\n':

>>> file = open('test.txt', 'r')
>>> content = file.read()
>>> file.close()
>>> content = content.encode('utf-8').decode('unicode-escape')
>>> print(content)
---a

---ä---

I'm getting now successfully 2 lines but I have noticed not getting the ä anymore. After that I have made a deeper look and opened this ticket.

If unicode_escape gets really deprecated maybe I could simply replace the characters 0-31 and 127 to achieve practically the same behavior.

@bitdancer
Copy link
Member

Using unicode_escape to decode non-ascii is simply wrong. It can't work.

@bitdancer
Copy link
Member

To understand why, understand that a byte string has no encoding inherent. So when you call b'utf8string'.decode('unicode_escape'), python has no way to know how to interpret the non-ascii characters in that bytestring. If you want the unicode_escape representation of something, you want to do 'string'.encode('unicode_escape'). If you then want that as a python string, you can do:

'mystring'.encode('unicode_escape').decode('ascii')

In theory there ought to be a way to use the codecs module to go directly from unicode string to unicode-escaped string, but I don't know how to do it, since the proposal for the 'transform' method was rejected :)

Just to bend your brain a bit further, note that this does work:

>>> codecs.decode(codecs.encode('ä', 'unicode-escape').decode('ascii'), 'unicode-escape')
'ä'

@bitdancer
Copy link
Member

Also, I'm not sure what this should do, but what it does do doesn't look right:

>>> codecs.decode('ä', 'unicode-escape')
'ä'

@serhiy-storchaka
Copy link
Member

Sworddragon, try to use content.encode('ascii', 'backslashreplace').decode('unicode-escape').

It is too late to change the unicode-escape encoding.

@Sworddragon
Copy link
Mannequin Author

Sworddragon mannequin commented Jun 21, 2014

It is too late to change the unicode-escape encoding.

So it will stay at ISO-8859-1? If yes I think this ticket can be closed as wont fix.

@loewis
Copy link
Mannequin

loewis mannequin commented Jun 21, 2014

I disagree. The current decoder implementation is clearly incorrect: the unicode-escape encoding only uses bytes < 128. So decoding non-ascii bytes should fail. So the examples in msg217021 should all give UnicodeDecodeErrors.

As this is an incompatible change, we need to deprecate the current behavior for 3.5, and change it in 3.6.

@malemburg
Copy link
Member

The unicode-escape codec was used in Python 2 to convert Unicode literals in source code to Unicode objects. Before PEP-263, Unicode literals in source code were interpreted as Latin-1. See http://legacy.python.org/dev/peps/pep-0263/ for details.

The implementation is correct, but doesn't necessarily match today's realities anymore.

@loewis
Copy link
Mannequin

loewis mannequin commented Jun 22, 2014

As you say, the unicode-escape codec is tied to the Python language definition. So if the language changes, the codec needs to change as well.

A Unicode literal in source code might be using any encoding, so to be on the safe side, restricting it to ASCII is meaningful. Or else, if we want to use the default source encoding (as it did in 2.x), we should assume UTF-8 (per PEP-3120). Using ISO-8859-1 is clearly wrong for 3.x.

@serhiy-storchaka
Copy link
Member

Note that 'raw-unicode-escape' is used in pickle protocol 0. Changing it can break compatibility.

@malemburg
Copy link
Member

On 24.06.2014 11:44, Serhiy Storchaka wrote:

Note that 'raw-unicode-escape' is used in pickle protocol 0. Changing it can break compatibility.

Indeed. unicode-escape was also designed to be able to read back
raw-unicode-escape encoded data, so changing the decoder to not
accept Latin-1 code points would break that as well.

It may be better to simply create a new codec that rejects
non-ASCII encoded bytes when decoding and perhaps call
that 'unicode-repr'.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-unicode type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

4 participants