8000 bpo-33954: Fix _PyUnicode_InsertThousandsGrouping() by vstinner · Pull Request #10623 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

8000 bpo-33954: Fix _PyUnicode_InsertThousandsGrouping() #10623

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

Merged
merged 2 commits into from
Nov 26, 2018
Merged

bpo-33954: Fix _PyUnicode_InsertThousandsGrouping() #10623

merged 2 commits into from
Nov 26, 2018

Conversation

vstinner
Copy link
Member
@vstinner vstinner commented Nov 20, 2018

For str.format(), float.__format__() and complex.__format__() methods
for non-ASCII decimal point when using the "n" formatter.

Changes:

  • Rewrite _PyUnicode_InsertThousandsGrouping(): it now requires
    a _PyUnicodeWriter object for the buffer and a Python str
    for digits.
  • Remove Objects/stringlib/localeutil.h:
    _PyUnicode_InsertThousandsGrouping() implementation moved
    to unicodeobject.c.

https://bugs.python.org/issue33954

@vstinner
Copy link
Member Author

@serhiy-storchaka, @methane: Would you mind to review this change?

Since I moved the implementation of the function, the diff is not easy to read :-( I also had to move the FILL() macro. Changes:

  • handle PyUnicode_READY() error in calc_number_widths()
  • InsertThousandsGrouping_fill() is now responsible to compute maxchar

I hesitate to move the function to Python/formatter_unicode.c since it's only called there.

_PyUnicode_InsertThousandsGrouping() function is really ugly. It should be called twice with different parameters, and the second call requires writer to be properly initialized (prepared with the correct 'kind' and enough buffer).

I don't understand why _PyUnicode_InsertThousandsGrouping() has a "min_width" parameter. It looks like an optimization specific to Python/formatter_unicode.c.

This change is a bugfix and should be backported to Python 3.6 and 3.7. Currently, it's possible to crash Python, see https://bugs.python.org/issue33954

@vstinner
Copy link
Member Author

I plan to merge this PR next week.

@serhiy-storchaka serhiy-storchaka added type-bug An unexpected behavior, bug, or error needs backport to 3.6 labels Nov 21, 2018
@serhiy-storchaka serhiy-storchaka self-requested a review November 21, 2018 17:56
@serhiy-storchaka
Copy link
Member

unicodeobject.c is already large, so I prefer to move these function to other file.

What if keep them in Objects/stringlib/localeutil.h, but make this file not template?

@vstinner
Copy link
Member Author

unicodeobject.c is already large, so I prefer to move these function to other file.

What about moving this file to Python/formatter_unicode.c?

My previous attempt to split the giant Objects/unicodeobject.c:
https://mail.python.org/pipermail/python-dev/2012-October/122365.html

I'm also fine with leaving the code in localeutil.h, but rename it to localeutil.c and remove STRINGLIB().

@vstinner
Copy link
Member Author

I rebased my patch, squashed commited and moved back code into (a new file) localeutil.c.

My PR now renames localeutil.h to localeutil.c.

Fix str.format(), float.__format__() and complex.__format__() methods
for non-ASCII decimal point when using the "n" formatter.

Changes:

* Rewrite _PyUnicode_InsertThousandsGrouping(): it now requires
  a _PyUnicodeWriter object for the buffer and a Python str
  for digits.
* FILL() macro now also checks "0 <= start".
@vstinner
Copy link
Member Author

My PR now renames localeutil.h to localeutil.c.

Hum. GitHub (Git) failed to detect that the file has been renamed, so I decide instead of keep the original filename.

I modified again my change to no longer move any code. The change should be simpler to review and better keep the Git history.

@vstinner
Copy link
Member Author

@serhiy-storchaka: Would you mind to review the updated PR?

* Convert FILL() macro to static inline function
* Rename the function to unicode_fill().
* Replace "i" iterator with end pointer for the end condition.
@vstinner vstinner merged commit 59423e3 into python:master Nov 26, 2018
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

@vstinner vstinner deleted the insert_thousands branch November 26, 2018 12:40
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 26, 2018
Fix str.format(), float.__format__() and complex.__format__() methods
for non-ASCII decimal point when using the "n" formatter.

Changes:

* Rewrite _PyUnicode_InsertThousandsGrouping(): it now requires
  a _PyUnicodeWriter object for the buffer and a Python str object
  for digits.
* Rename FILL() macro to unicode_fill(), convert it to static inline function,
  add "assert(0 <= start);" and rework its code.
(cherry picked from commit 59423e3)

Co-authored-by: Victor Stinner <vstinner@redhat.com>
@bedevere-bot
Copy link

GH-10717 is a backport of this pull request to the 3.7 branch.

@miss-islington
Copy link
Contributor

Sorry, @vstinner, I could not cleanly backport this to 3.6 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 59423e3ddd736387cef8f7632c71954c1859bed0 3.6

vstinner added a commit that referenced this pull request Nov 26, 2018
)

Fix str.format(), float.__format__() and complex.__format__() methods
for non-ASCII decimal point when using the "n" formatter.

Rewrite _PyUnicode_InsertThousandsGrouping(): it now requires
a _PyUnicodeWriter object for the buffer and a Python str object
for digits.

(cherry picked from commit 59423e3)
vstinner added a commit that referenced this pull request Nov 26, 2018
) (GH-10720)

Fix str.format(), float.__format__() and complex.__format__() methods
for non-ASCII decimal point when using the "n" formatter.

Rewrite _PyUnicode_InsertThousandsGrouping(): it now requires
a _PyUnicodeWriter object for the buffer and a Python str object
for digits.

(cherry picked from commit 59423e3)
(cherry picked from commit 6f5fa1b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0