Issue 3563: fix_idioms.py generates bad code (original) (raw)

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:37 admin set github: 47813
2009-10-07 21:26:21 benjamin.peterson set status: open -> closedresolution: fixedmessages: +
2009-10-07 03:28:23 benjamin.peterson set assignee: collinwinter -> benjamin.peterson
2009-10-07 02:23:40 joe.amenta set files: + fix_idioms.patchmessages: +
2009-10-07 02:23:05 joe.amenta set files: - fix_idioms.patch
2009-10-07 02:21:39 joe.amenta set files: + fix_idioms.patchnosy: + joe.amentamessages: +
2008-12-08 11:39:51 aronacher set nosy: + aronachermessages: +
2008-08-22 21:05:54 terry.reedy set nosy: + terry.reedymessages: +
2008-08-16 15:06:24 benjamin.peterson set files: + indentation_test.diffnosy: + benjamin.petersonmessages: + keywords: + patch
2008-08-16 07:03:07 hawking create