➜

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
标题: float.__format__('n') fails with _PyUnicode_CheckConsistency assertion error for locales with non-ascii thousands separator
类型: Stage: resolved
Components: Unicode Versions: Python 3.8, Python 3.7, Python 3.6
process
状态: closed Resolution: fixed
Dependencies: 后续:
分配给: 抄送列表: eric.smith, ezio.melotti, mark.dickinson, serhiy.storchaka, vstinner, xtreak
优先级: normal 关键字: patch

Created on 2018-06-25 08:23 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 10623 merged vstinner, 2018-11-20 21:35
PR 10717 closed miss-islington, 2018-11-26 12:40
PR 10718 merged vstinner, 2018-11-26 12:45
PR 10720 merged vstinner, 2018-11-26 14:16
PR 10737 merged vstinner, 2018-11-27 11:12
PR 10738 merged vstinner, 2018-11-27 11:20
PR 10740 merged vstinner, 2018-11-27 11:23
Messages (14)
msg320403 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-06-25 08:23
Example:

vstinner@apu$ ./python
Python 3.8.0a0 (heads/master-dirty:bcd3a1a18d, Jun 23 2018, 10:31:03) 
[GCC 8.1.1 20180502 (Red Hat 8.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.str(2.5)
'2.5'
>>> '{:n}'.format(2.5)
'2.5'
>>> locale.setlocale(locale.LC_ALL, '')
'fr_FR.UTF-8'
>>> locale.str(2.5)
'2,5'
>>> '{:n}'.format(2.5)
python: Objects/unicodeobject.c:474: _PyUnicode_CheckConsistency: Assertion `maxchar < 128' failed.
Aborted (core dumped)

Another example:

vstinner@apu$ ./python
Python 3.8.0a0 (heads/master-dirty:bcd3a1a18d, Jun 23 2018, 10:31:03) 
>>> import locale; locale.setlocale(locale.LC_ALL, '')
'fr_FR.UTF-8'
>>> (2.5).__format__('n')
python: Objects/unicodeobject.c:474: _PyUnicode_CheckConsistency: Assertion `maxchar < 128' failed.
Aborted (core dumped)


Result of my system Python 3.6 of Fedora 28:

vstinner@apu$ python3
Python 3.6.5 (default, Mar 29 2018, 18:20:46) 
[GCC 8.0.1 20180317 (Red Hat 8.0.1-0.19)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.str(2.5)
'2.5'
>>> '{:n}'.format(2.5)
'2.5'
>>> locale.setlocale(locale.LC_ALL, '')
'fr_FR.UTF-8'
>>> locale.str(2.5)
'2,5'
>>> '{:n}'.format(2.5)
'°,5'
>>> '{:n}'.format(3.5)
'°,5'
>>> '{:n}'.format(33.5)
'°\x18,5'
>>> '{:n}'.format(333.5)
'°\x186,5'
msg320635 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-06-27 22:35
Aha, the problem occurs when the thousands separator code point is greater than 255.

On my Fedora 28 (glibc 2.27), it's U+202f:

vstinner@apu$ ./python
Python 3.8.0a0 (heads/master-dirty:492572715a, Jun 28 2018, 00:18:54) 
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'fr_FR.UTF-8'
>>> locale.localeconv()['thousands_sep']
'\u202f'

The bug is in _PyUnicode_InsertThousandsGrouping(): if thousands_sep kind is different than unicode kind, "data = _PyUnicode_AsKind(unicode, thousands_sep_kind);" is used, but later this memory is released. So the function writes into a temporary buffer which is then released. It doesn't work...

It seems like I introduced the regression 6 years ago in bpo-13706:

commit 90f50d4df9e21093f006427fd7ed11a0d704f792
Author: Victor Stinner <victor.stinner@haypocalc.com>
Date:   Fri Feb 24 01:44:47 2012 +0100

    Issue #13706: Fix format(float, "n") for locale with non-ASCII decimal point (e.g. ps_aF)
msg320669 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-06-28 14:45
> The bug is in _PyUnicode_InsertThousandsGrouping()

This function should take a _PyUnicodeWriter as input, not a PyUnicodeObject.
msg330157 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-20 22:00
Minimum reproducer, on Fedora 29:

import locale
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
print(ascii('{:n}'.format(1.5)))

Current result:

'H,5'

Output with PR 10623:

'1,5'
msg330211 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-21 17:19
My private test suite for locales:
https://github.com/vstinner/misc/blob/master/python/test_all_locales.py

Since each platform uses a different locale database, it's hard to write reliable portable and future-proof tests :-(

My tests only work on specific Windows, macOS, FreeBSD and Linux versions.
msg330429 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-26 12:40
New changeset 59423e3ddd736387cef8f7632c71954c1859bed0 by Victor Stinner in branch 'master':
bpo-33954: Fix _PyUnicode_InsertThousandsGrouping() (GH-10623)
https://github.com/python/cpython/commit/59423e3ddd736387cef8f7632c71954c1859bed0
msg330431 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-26 13:17
New changeset 6f5fa1b4be735159e964906ab608dc467476e47c by Victor Stinner in branch '3.7':
bpo-33954: Fix _PyUnicode_InsertThousandsGrouping() (GH-10623) (GH-10718)
https://github.com/python/cpython/commit/6f5fa1b4be735159e964906ab608dc467476e47c
msg330444 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-26 16:03
New changeset fc4a44b0c3a69390eca4680d89c2ae5fe967f882 by Victor Stinner in branch '3.6':
bpo-33954: Fix _PyUnicode_InsertThousandsGrouping() (GH-10623) (GH-10718) (GH-10720)
https://github.com/python/cpython/commit/fc4a44b0c3a69390eca4680d89c2ae5fe967f882
msg330445 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-26 16:06
Python 3.6, 3.7 and master have been fixed.
msg330494 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 日期: 2018-11-27 07:10
Objects/unicodeobject.c: In function ‘_PyUnicode_FastFill’:
Objects/unicodeobject.c:10126:24: warning: passing argument 2 of ‘unicode_fill’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     unicode_fill(kind, data, fill_char, start, length);
                        ^~~~
Objects/unicodeobject.c:224:1: note: expected ‘void *’ but argument is of type ‘const void *’
 unicode_fill(enum PyUnicode_Kind kind, void *data, Py_UCS4 value,
 ^~~~~~~~~~~~
In file included from /usr/include/wchar.h:850:0,
                 from ./Include/unicodeobject.h:97,
                 from ./Include/Python.h:87,
                 from ./Modules/getpath.c:3:
msg330510 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-27 11:41
New changeset 163403a63e9272fcd14707e344122c2e3c5e0244 by Victor Stinner in branch 'master':
bpo-33954: Fix compiler warning in _PyUnicode_FastFill() (GH-10737)
https://github.com/python/cpython/commit/163403a63e9272fcd14707e344122c2e3c5e0244
msg330511 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-27 11:42
New changeset 7f9fb0f34555641722be5468b7fbd53dd3c0f1e2 by Victor Stinner in branch '3.7':
bpo-33954: Rewrite FILL() macro of unicodeobject.c (GH-10738)
https://github.com/python/cpython/commit/7f9fb0f34555641722be5468b7fbd53dd3c0f1e2
msg330517 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-27 13:31
New changeset 54fa83e0a3f3b077763cb50705d7a7dbe4a40a4a by Victor Stinner in branch '3.6':
bpo-33954: Rewrite FILL() macro of unicodeobject.c (GH-10740)
https://github.com/python/cpython/commit/54fa83e0a3f3b077763cb50705d7a7dbe4a40a4a
msg330518 - (view) Author: STINNER Victor (vstinner) * (Python committer) 日期: 2018-11-27 13:32
Serhiy Storchaka:
"""
Objects/unicodeobject.c: In function ‘_PyUnicode_FastFill’:
Objects/unicodeobject.c:10126:24: warning: passing argument 2 of ‘unicode_fill’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
"""

Yeah, I was aware of the warning. I had a local branch but I started to make many unrelated changes and so I lost track of the issue :-)

The warning should now be fixed. Thanks for the report.

Note: it was a real bug which exists since at least Python 3.6 ;-)
历史
日期 用户 动作 参数
2022-04-11 14:59:02admin修改github: 78135
2020-01-12 02:29:51cheryl.sabella链接issue33471 superseder
2019-01-04 23:31:03vstinner链接issue35432 superseder
2018-11-27 13:32:20vstinner修改消息: + msg330518
2018-11-27 13:31:00vstinner修改消息: + msg330517
2018-11-27 11:42:08vstinner修改消息: + msg330511
2018-11-27 11:41:20vstinner修改消息: + msg330510
2018-11-27 11:23:06vstinner修改pull_requests: + pull_request9987
2018-11-27 11:20:29vstinner修改pull_requests: + pull_request9985
2018-11-27 11:12:10vstinner修改pull_requests: + pull_request9984
2018-11-27 07:10:08serhiy.storchaka修改抄送: + serhiy.storchaka
消息: + msg330494
2018-11-26 16:06:00vstinner修改状态: open -> closed
resolution: fixed
消息: + msg330445

stage: patch review -> resolved
2018-11-26 16:03:39vstinner修改消息: + msg330444
2018-11-26 14:16:34vstinner修改pull_requests: + pull_request9968
2018-11-26 13:17:06vstinner修改消息: + msg330431
2018-11-26 12:45:29vstinner修改pull_requests: + pull_request9967
2018-11-26 12:40:18miss-islington修改pull_requests: + pull_request9966
2018-11-26 12:40:03vstinner修改消息: + msg330429
2018-11-21 17:19:07vstinner修改消息: + msg330211
2018-11-20 22:00:43vstinner修改消息: + msg330157
2018-11-20 21:35:49vstinner修改keywords: + patch
stage: patch review
pull_requests: + pull_request9871
2018-08-05 17:54:55ppperry修改标题: float.__format__('n') fails with _PyUnicode_CheckConsistency assertion error -> float.__format__('n') fails with _PyUnicode_CheckConsistency assertion error for locales with non-ascii thousands separator
2018-08-05 12:45:57xtreak修改抄送: + xtreak
2018-06-28 14:45:48vstinner修改消息: + msg320669
2018-06-28 13:57:20mark.dickinson修改抄送: + mark.dickinson, eric.smith
2018-06-27 22:35:49vstinner修改消息: + msg320635
2018-06-25 08:32:07vstinner修改versions: + Python 3.6, Python 3.7
2018-06-25 08:23:46vstinner创建