Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-76578: Fix textwrap.wrap() so it's stable if run twice.#5615

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
larryhastings wants to merge2 commits intopython:main
base:main
Choose a base branch
Loading
fromlarryhastings:larry_textwrap_stability

Conversation

larryhastings
Copy link
Contributor

@larryhastingslarryhastings commentedFeb 11, 2018
edited by bedevere-bot
Loading

Fixtextwrap.wrap() so it's stable. In certain fiddly circumstances,
textwrap.wrap(x) wasn't the same astextwrap.wrap(textwrap.wrap(x)),
which was surprising. This happened when a line was wrapped at a whitespace
blob that was longer than 1 character, but the following wordwould have
fit if that whitespace blob was only 1 character long.

https://bugs.python.org/issue32397

Copy link
Member

@ned-deilyned-deily left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This seems like a bug to me so there should be no issue in fixing it for 3.7 and probably should be OK for 3.6.x.

@serhiy-storchakaserhiy-storchaka added type-bugAn unexpected behavior, bug, or error needs backport to 3.6 labelsFeb 11, 2018
Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I have few minor style suggestions, but in any case LGTM.

spacer = " "
new_len = cur_len - len(cur_line[-1]) + len(spacer) + len(chunks[-1])
if new_len <= width:
cur_line.pop()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I would write this as

cur_line[-1]=spacercur_line.append(chunks.pop())

but this is a matter of style.

and cur_line and cur_line[-1].strip() == ''):
spacer = " "
if (self.fix_sentence_endings
and (len(cur_line) > 1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I think this would look better without parenthesis.

merwok reacted with thumbs up emoji
# original is 31 characters long:
# 0 1 2 3
# 1234567890123456789012345678901
original = "xxxx xxxx xxxx xxxx xxxx. xxxx"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Could you please use more meaningful example? If the test will fail it more convenient to see different words in the report rather of repeated 'xxxx'.

wrapped2 = wrap(wrapped)
self.assertEqual(wrapped, wrapped2)

def test_wrap_stability_with_fix_sentence_endings(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Seems this case is already covered by test_fix_sentence_endings. test_fix_sentence_endings fails if disable the correction for fix_sentence_endings.

@ned-deily
Copy link
Member

@larryhastings What's the status of this PR? It looks like@serhiy-storchaka had a few minor review comments

@vstinner
Copy link
Member

I removed the " needs backport to 3.6" label, the 3.6 branch no longer accept bugfixes (only security fixes are accepted):https://devguide.python.org/#status-of-python-branches

# This is all complicated slightly by fix_sentence_endings,
# where the chunk we add back in might need to be two spaces
# instead of one.
if (chunks and self.drop_whitespace
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

According to the doc, drop_whitespace means "whitespace at the beginning and ending of every line (after wrapping but before indenting) is dropped".

But when you do below
new_len = cur_len - len(cur_line[-1]) + len(spacer) + len(chunks[-1])
doesn't that amount to dropping whitespace within the line?

I see what you're trying to solve here, but I think that the definition of drop_whitespace, if I understand it correctly, makes wrap inherently non-idempotent.

Copy link
Member

@iritkatrieliritkatriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

As per my comment, I think this might break drop_whitespace. Please check.

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsbot added the staleStale PR or inactive for long period of time. labelAug 15, 2022
@smontanaro
Copy link
Contributor

It appears this change is going in adifferent direction (adding a new feature instead of considering this a bug). If so, shouldn't this PR (and its underlying issue) be closed/rejected?

erlend-aasland reacted with thumbs up emoji

@github-actionsgithub-actionsbot removed the staleStale PR or inactive for long period of time. labelMay 2, 2023
@smontanaro
Copy link
Contributor

It appears this change is going in adifferent direction (adding a new feature instead of considering this a bug). If so, shouldn't this PR (and its underlying issue) be closed/rejected?

No answer. Is this truly still a bug fix or is it now a feature request?

@erlend-aaslanderlend-aasland changed the titlebpo-32397: Fix textwrap.wrap() so it's stable if run twice.gh-76578: Fix textwrap.wrap() so it's stable if run twice.May 5, 2023
@github-actionsGitHub Actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsbot added the staleStale PR or inactive for long period of time. labelJul 7, 2024
@python-cla-bot
Copy link

python-cla-botbot commentedApr 18, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@github-actionsgithub-actionsbot removed the staleStale PR or inactive for long period of time. labelApr 18, 2025
@github-actionsGitHub Actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsbot added the staleStale PR or inactive for long period of time. labelMay 19, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@iritkatrieliritkatrieliritkatriel requested changes

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

@ned-deilyned-deilyned-deily approved these changes

Assignees
No one assigned
Labels
awaiting core reviewstaleStale PR or inactive for long period of time.type-bugAn unexpected behavior, bug, or error
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

10 participants
@larryhastings@ned-deily@vstinner@smontanaro@iritkatriel@serhiy-storchaka@the-knights-who-say-ni@ezio-melotti@bedevere-bot@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp