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

only strip continuation prompts if regular prompts seen first #4060

Merged
merged 2 commits into from
Aug 29, 2013

Conversation

minrk
Copy link
Member

@minrk minrk commented Aug 19, 2013

should be discussed before merge (ping @takluyver), as it is a significant and sensitive change.

I'm also not sure that it's the right fix, but worth a try.

closes #4059

should be discussed before merge (ping @takluyver),
as it is a significant and sensitive change.

I'm also not sure that it's the right fix, but worth a try.

closes ipython#4059
return _strip_prompts(prompt_re)
prompt_re = re.compile(r'^(In \[\d+\]: )')
continuation_re = re.compile(r'^(In \[\d+\]: |\ \ \ \.\.\.+: )')
return _strip_prompts(prompt_re, continuation_re)
Copy link
Member

Choose a reason for hiding this comment

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

ipy prompt does seem to be unambiguous as they avoid not to end with : and that will help in case like when you can select the first line without leaving the first prompt not unselected. So would you like to call return _strip_prompts(continuation_re, continuation_re) ?

Copy link
Member Author

Choose a reason for hiding this comment

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

That might be preferable. I'd like feedback from @takluyver on whether this seems like the right approach.
Keeping the existing behavior just means making the same call, so I can leave ipy_prompt unchanged if that's the way we want to go.

@takluyver
Copy link
Member

I think this is closer to what we were doing before, but we changed it because quite a few people, faced with an example like this:

>>> def f(a):
...     b = a * 2
...     return b + 1

will copy from immediately after the initial prompt, so we get something like this pasted:

def f(a):
...     b = a * 2
...     return b + 1

I think this case is rather more common than having the ellipsis singleton as the first element on the second line of the block (we already leave it if there isn't something that looks like a prompt in the first two lines).

We could make it slightly more specific by checking the first line only for the initial >>> prompt, not the ... continuation prompt. But there are also times when you want to copy and paste code from the middle of a function definition or a loop, in which case it may start with a ... prompt.

- initial-only prompt is only stripped from the first line (only change from master)
- any prompt is stripped from the second line
- if a prompt is found in the first two lines,
  strip from the rest of the block
@minrk
Copy link
Member Author

minrk commented Aug 27, 2013

Logic updated after discussion with @takluyver:

  1. two expressions: prompt_re and initial_re, with initial being optional, and defaulting to the same as prompt
  2. prompt should match any prompt, not just continuation (unchanged from master)
  3. initial is checked against the first line only, so that continuation prompts will not be stripped from the first line of the cell.
  4. Any prompt will be stripped from the second line of the cell
  5. If any prompt is found on the first two lines of the cell, prompts will be stripped from the rest of the cell.

Mostly the logic is unchanged - the only change being the addition of the smaller initial_re for checking only the first line, which allows us to avoid stripping ... in the first line.

There are still some cases that will fail, which I think is inevitable given the ambiguity. But I think the affected cases are rare enough and easy enough to workaround that this is at least an improvement over master:

  1. paste a block only consisting of continuation prompts (works in master, fails with this PR)
  2. line starting with intentional ellipsis only on the second line of the cell (no change from master)

workaround for 1. is to use initial prompt or no prompt on the first line.

@takluyver
Copy link
Member

👍 I agree with the behaviour, and the implementation looks good. We'll give it a day or two in case anyone wants to discuss the behaviour more.

@asmeurer
Copy link
Contributor

This seems like as good of a workaround as I can think of. You can probably be smarter in the qtconsole, but that's your decision if you want to implement the different logic there.

takluyver added a commit that referenced this pull request Aug 29, 2013
only strip continuation prompts if regular prompts seen first
@takluyver takluyver merged commit 11babdf into ipython:master Aug 29, 2013
takluyver added a commit that referenced this pull request Aug 29, 2013
… seen first

should be discussed before merge (ping @takluyver), as it is a significant and sensitive change.

I'm also not sure that it's the right fix, but worth a try.

closes #4059
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
only strip continuation prompts if regular prompts seen first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issues with Ellipsis literal in Python 3
4 participants