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

str.format(): calculates string-length based on bytes instead on chars #8171

Open
bablokb opened this issue Jul 14, 2023 · 2 comments
Open

Comments

@bablokb
Copy link

bablokb commented Jul 14, 2023

CircuitPython version

Adafruit CircuitPython 8.2.0 on 2023-07-05; Raspberry Pi Pico with rp2040

Code/REPL

>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== b_label = "Bat V"
=== b_value = "4.86"
=== t_label = "T/AHT °C"
=== t_value = "23.6"
=== "{label:<8.8}:{value:>4.4}".format(label=b_label,value=b_value)
=== "{label:<8.8}:{value:>4.4}".format(label=t_label,value=t_value) 
'Bat V   :4.86'
'T/AHT °:23.6'
>>>

Behavior

n.a.

Description

The format-specifier {label:<8.8} forces a field-length of 8 and truncates longer strings (and left justifies the string, but this is not of relevance here).

t_label in the example above has a string length of 8 but an UTF-8 byte-length of 9 (the degree-sign ° is a single char with two bytes). This results in t_label being truncated even though it would fit into the field.

Additional information

Standard python gets this right:

Python 3.9.15 (main, Oct 28 2022, 17:28:38) [GCC] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> b_label = "Bat V"
>>> b_value = "4.86"
>>> t_label = "T/AHT °C"
>>> t_value = "23.6"
>>> "{label:<8.8}:{value:>4.4}".format(label=b_label,value=b_value)
'Bat V   :4.86'
>>> "{label:<8.8}:{value:>4.4}".format(label=t_label,value=t_value)
'T/AHT °C:23.6'
>>>
@bablokb bablokb added the bug label Jul 14, 2023
@tannewt tannewt added this to the Long term milestone Jul 14, 2023
@tannewt
Copy link
Member

tannewt commented Jul 14, 2023

Would you mind testing the latest MicroPython? We inherit most of the relevant code for this issue from them and we're working to update CP to newer MP. Thanks!

@bablokb
Copy link
Author

bablokb commented Jul 14, 2023

The current rp2 micropython gives me

=== b_label = "Bat V"
=== b_value = "4.86"
=== t_label = "T/AHT °C"
=== t_value = "23.6"
=== "{label:<8.8}:{value:>4.4}".format(label=b_label,value=b_value)
=== "{label:<8.8}:{value:>4.4}".format(label=t_label,value=t_value)
=== 
'Bat V   :4.86'
'T/AHT \xb0:23.6'
>>> 

b0 is the extended ascii value of °, so it seems this bug is also present in micropython.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants