py-execute-region maintains indent making making it impossible to execute indented code

Bug #828314 reported by Gennady N. Uraltsev
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-mode.el
Fix Released
High
Andreas Roehler

Bug Description

Consider this file:

#! /usr/bin/env python2.7
import os

if __name__ == "__main__":
    print "hello"

selecting the line
>print "hello"

and running py-execute-region

results in this error:

>>> ## executing temporary file /tmp/Python2.728520nnd.py...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/Python2.728520nnd.py", line 3
    print "hello"
    ^
IndentationError: unexpected indent

Apparently py-execute-region maintains indentation and so this makes any indented code not executable by this means.
1)Is this meant this way or maybe py-indent-region should outdent as far as possible for the region selected?
2)Maybe py-execute-region could read a C-u supplied argument about how many indent positions to do when sending to python interpreter.

Thanks!

Changed in python-mode:
assignee: nobody → Andreas Roehler (a-roehler)
Changed in python-mode:
importance: Undecided → Medium
Changed in python-mode:
milestone: none → 6.0.2
importance: Medium → High
status: New → In Progress
Revision history for this message
Andreas Roehler (a-roehler) wrote :

checked in a fix,

could you try again?

Thanks,

Andreas

Changed in python-mode:
status: In Progress → Fix Committed
Changed in python-mode:
status: Fix Committed → Fix Released
Revision history for this message
Gennady N. Uraltsev (gennady-uraltsev) wrote :

Dear Andreas,

I have looked at the latest development version and the bug is still there... At least for me..

The same file above, selecting the line with

    print "hello"

and executing the region gives the exact same region

Revision history for this message
Gennady N. Uraltsev (gennady-uraltsev) wrote :

Sorry I meant "same region" ----> same python interpreter error.

Revision history for this message
Andreas Roehler (a-roehler) wrote : Re: [Bug 828314] Re: py-execute-region maintains indent making making it impossible to execute indented code

Am 30.08.2011 20:40, schrieb Gennady N. Uraltsev:
> Dear Andreas,
>
> I have looked at the latest development version and the bug is still
> there... At least for me..
>
> The same file above, selecting the line with
>
> print "hello"
>
> and executing the region gives the exact same region
>

Hmm, think you can't send the line to the python interpreter in this
case. As the indent until "print" raises an error - correctly IMHO.

Sending a region beginning at "print" should work.

BTW py-execute-statement should DTRT and being fastest in these cases.

Cheers,

Andreas

Revision history for this message
Georg Brandl (gbrandl) wrote :

How complicated would it be to remove all indentation common to all lines before sending to the interpreter?

Revision history for this message
Gennady N. Uraltsev (gennady-uraltsev) wrote :

I agree. Some time ago I used this feature (I cannot recall, maybe it was in python.el or an old verison of python-mode) to prototype code in a very quick way, possibly from inside function definitions etc.... What do you think about it? Maybe implementing it as a behaviour selected by C-u?

Revision history for this message
Barry Warsaw (barry) wrote :

I could have sworn py-execute-region inserted a preceding line such as

if True:

when the region to execute was indented. Shouldn't that be enough to avoid indentation errors?

Revision history for this message
Andreas Roehler (a-roehler) wrote :

Am 30.08.2011 22:04, schrieb Barry Warsaw:
> I could have sworn py-execute-region inserted a preceding line such as
>
> if True:
>

think you would swear righteously :)

> when the region to execute was indented. Shouldn't that be enough to
> avoid indentation errors?
>

Didn't like that "needs if", as it will work in some, maybe most cases,
but fail in others.

It only deals with a possible one-step indent, but fails with more
indents AFAIU.

Will re-install it the way, it removes all indent from the first line,
dedenting the remaining accordingly.

Cheers,

Andreas

Changed in python-mode:
status: Fix Released → In Progress
Revision history for this message
Barry Warsaw (barry) wrote :

On Aug 31, 2011, at 05:48 AM, Andreas Roehler wrote:

>Am 30.08.2011 22:04, schrieb Barry Warsaw:
>> I could have sworn py-execute-region inserted a preceding line such as
>>
>> if True:
>>
>
>think you would swear righteously :)

No question, it's a hack... but one that's pretty much guaranteed to work. :)

>> when the region to execute was indented. Shouldn't that be enough to
>> avoid indentation errors?
>
>Didn't like that "needs if", as it will work in some, maybe most cases,
>but fail in others.
>
>It only deals with a possible one-step indent, but fails with more
>indents AFAIU.

Actually, no, it should always work. Python only cares that the indentation
is consistent within a block. E.g. this is perfectly valid:

-----snip snip-----
if True:
                             print 'hello world'
-----snip snip-----

Revision history for this message
Andreas Roehler (a-roehler) wrote :

Am 31.08.2011 15:22, schrieb Barry Warsaw:
> On Aug 31, 2011, at 05:48 AM, Andreas Roehler wrote:
>
>> Am 30.08.2011 22:04, schrieb Barry Warsaw:
>>> I could have sworn py-execute-region inserted a preceding line such as
>>>
>>> if True:
>>>
>>
>> think you would swear righteously :)
>
> No question, it's a hack... but one that's pretty much guaranteed to
> work. :)
>
>>> when the region to execute was indented. Shouldn't that be enough to
>>> avoid indentation errors?
>>
>> Didn't like that "needs if", as it will work in some, maybe most cases,
>> but fail in others.
>>
>> It only deals with a possible one-step indent, but fails with more
>> indents AFAIU.
>
> Actually, no, it should always work. Python only cares that the indentation
> is consistent within a block. E.g. this is perfectly valid:
>
> -----snip snip-----
> if True:
> print 'hello world'
> -----snip snip-----
>

Ah, thanks, so my arguments shrink a little bit :)

remains saying: when implementing a left-shift of the code delivered
it's more obvious whats on.

Revision history for this message
Andreas Roehler (a-roehler) wrote :

Hi Gennady,

could you try again?

Thanks,

Andreas

Changed in python-mode:
status: In Progress → Fix Committed
Revision history for this message
Gennady N. Uraltsev (gennady-uraltsev) wrote :

Sorry for the delay.

Works perfectly now!

Changed in python-mode:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.