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.

classification
Title: pprint fails in transformming non-breaking space
Type: behavior Stage: resolved
Components: Unicode Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, steven.daprano, xxm
Priority: normal Keywords:

Created on 2021-01-07 06:47 by xxm, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg384564 - (view) Author: Xinmeng Xia (xxm) Date: 2021-01-07 06:47
"pprint" can transform unicode like "print". However, pprint fails to transform non-breaking space('\240') . See the following example:

Python 3.10.0a2 (default, Nov 24 2020, 14:18:46) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Example 1(Results as expected):
---------------------------------------------------------------------
>>> import pprint
>>> print(u'\041 hello')
! hello
>>> pprint.pprint(u'\041 hello')
'! hello'

Example 2(Results not as expected):
---------------------------------------------------------------------
>>> print(u'\240 hello')
  hello
>>> pprint.pprint(u'\240 hello')
'\xa0 hello'

Expected output: the output of pprint.pprint(u'\240 hello') should be consistent with output of print(u'\240 hello')
msg384583 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2021-01-07 11:59
By the way, there is no need to use the u'' prefix on strings in Python 3.

Unfortunately, I think this is a case where pprint doesn't meet your expectations. pprint is designed to print the repr of a string, while regular print prints the human-visible str:

>>> print(repr('\240 hello'))
'\xa0 hello'
>>> print('\240 hello')
  hello

Its not just non-breaking space, it also includes ASCII characters:


>>> print(repr('\01 hello'))
'\x01 hello'
>>> print('\01 hello')
 hello
>>> pprint.pprint('\01 hello')
'\x01 hello'


So this is intentional behaviour, not a bug. If you want to change the behaviour, it will probably require a re-design of the way strings are printed by the pprint module.
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 87018
2021-01-07 14:35:12serhiy.storchakasetresolution: fixed -> not a bug
2021-01-07 14:35:00serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: resolved
2021-01-07 11:59:56steven.dapranosetnosy: + steven.daprano
messages: + msg384583
2021-01-07 08:56:24vstinnersetnosy: - vstinner
2021-01-07 06:47:34xxmcreate