This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: fix_idioms.py generates bad code
Type: Stage:
Components: 2to3 (2.x to 3.x conversion tool) Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: benjamin.peterson Nosy List: aronacher, benjamin.peterson, collinwinter, hawking, joe.amenta, terry.reedy
Priority: normal Keywords: patch

Created on 2008-08-16 07:03 by hawking, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
indentation_test.diff benjamin.peterson, 2008-08-16 15:06
fix_idioms.patch joe.amenta, 2009-10-07 02:23 Patch that should fix this bug and test that this bug has been fixed.
Messages (7)
msg71199 - (view) Author: Ali Polatel (hawking) Date: 2008-08-16 07:03
fix_idioms.py generates bad code for conversions in try/except blocks.
Example:
s=(1, 2, 3)
try:
    t = list(s)
    t.sort()
except TypeError:
    pass

fix_idioms.py generates this diff:
--- test.py (original)
+++ test.py (refactored)
@@ -7,8 +7,7 @@
 
 s=(1, 2, 3)
 try:
-    t = list(s)
-    t.sort()
-except TypeError:
+    t = sorted(s)
+    except TypeError:
     pass
 
except TypeError is indented wrongly.
msg71217 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-08-16 15:06
It's due to these lines in fix_idioms:

if next_stmt:
     next_stmt[0].set_prefix(sort_stmt.get_prefix())

I'm not sure what the correct way to deal with this is. Anyway I'm
attaching a test case.
msg71777 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-08-22 21:05
Why is the statement, whatever it is, even being touched?
Would not the same problem arise with any following outdented line?

IOW, why not delete that pair of lines from fix_idioms.py?
Does that break anything else in test_fixers?
msg77298 - (view) Author: Armin Ronacher (aronacher) * (Python committer) Date: 2008-12-08 11:39
I would drop the prefix in that case or attach it to the sorted() call.

So from this code:

    x = foo()
    # perform sorting
    x.sort()

to

    # perform sorting
    x = sorted(foo())

Makes more sense than sticking it after the sorted() call like it
happens currently.  This should also fix the problem with outdented
statements such as except/finally.
msg93672 - (view) Author: Joe Amenta (joe.amenta) Date: 2009-10-07 02:21
Attached a patch that implements more thoroughly what appears to be the
intended behavior.
msg93673 - (view) Author: Joe Amenta (joe.amenta) Date: 2009-10-07 02:23
Missed a paren in the last one... re-uploading it.
msg93725 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-10-07 21:26
Thanks very much for the patch; Committed in r75278.
History
Date User Action Args
2022-04-11 14:56:37adminsetgithub: 47813
2009-10-07 21:26:21benjamin.petersonsetstatus: open -> closed
resolution: fixed
messages: + msg93725
2009-10-07 03:28:23benjamin.petersonsetassignee: collinwinter -> benjamin.peterson
2009-10-07 02:23:40joe.amentasetfiles: + fix_idioms.patch

messages: + msg93673
2009-10-07 02:23:05joe.amentasetfiles: - fix_idioms.patch
2009-10-07 02:21:39joe.amentasetfiles: + fix_idioms.patch
nosy: + joe.amenta
messages: + msg93672

2008-12-08 11:39:51aronachersetnosy: + aronacher
messages: + msg77298
2008-08-22 21:05:54terry.reedysetnosy: + terry.reedy
messages: + msg71777
2008-08-16 15:06:24benjamin.petersonsetfiles: + indentation_test.diff
nosy: + benjamin.peterson
messages: + msg71217
keywords: + patch
2008-08-16 07:03:07hawkingcreate