The Wayback Machine - https://web.archive.org/web/20221223072041/https://github.com/python/cpython/pull/5615
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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

larryhastings
Copy link
Contributor

@larryhastings larryhastings commented Feb 11, 2018

Fix textwrap.wrap() so it's stable. In certain fiddly circumstances,
textwrap.wrap(x) wasn't the same as textwrap.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 word would have
fit if that whitespace blob was only 1 character long.

https://bugs.python.org/issue32397

Copy link
Member

@ned-deily ned-deily left a comment

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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

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()
Copy link
Member

@serhiy-storchaka serhiy-storchaka Feb 11, 2018

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] = spacer
cur_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)
Copy link
Member

@serhiy-storchaka serhiy-storchaka Feb 11, 2018

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.

# original is 31 characters long:
# 0 1 2 3
# 1234567890123456789012345678901
original = "xxxx xxxx xxxx xxxx xxxx. xxxx"
Copy link
Member

@serhiy-storchaka serhiy-storchaka Feb 11, 2018

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):
Copy link
Member

@serhiy-storchaka serhiy-storchaka Feb 11, 2018

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

ned-deily commented Jun 4, 2018

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

@vstinner
Copy link
Member

vstinner commented Jan 10, 2019

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

@iritkatriel iritkatriel Sep 4, 2020

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

@iritkatriel iritkatriel left a comment

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

@github-actions
Copy link

github-actions bot commented Aug 15, 2022

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

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting merge stale Stale PR or inactive for long period of time. type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants