classification
Title: 2to3 mishandles indented imports
Type: crash Stage: resolved
Components: 2to3 (2.x to 3.x conversion tool) Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder: 2to3 Conversion Result using BlankLine() can be Syntactically Incorrect
View: 38681
Assigned To: Nosy List: cheryl.sabella, galun.guy
Priority: normal Keywords:

Created on 2020-01-14 15:47 by galun.guy, last changed 2020-01-18 03:15 by cheryl.sabella. This issue is now closed.

Files
File name Uploaded Description Edit
ptypes.py galun.guy, 2020-01-14 15:47
Messages (2)
msg359978 - (view) Author: Guy Galun (galun.guy) Date: 2020-01-14 15:47
When encountering an import that should be removed in Python 3 (e.g. "from itertools import izip"), 2to3 changes it a blank line, which may cause a runtime error if that import was indented:

error: module importing failed: expected an indented block (ptypes.py, line 10)
  File "temp.py", line 1, in <module>
  File "./lldbmacros/xnu.py", line 771, in <module>
    from memory import *
  File "./lldbmacros/memory.py", line 11, in <module>
    import macho
  File "./lldbmacros/macho.py", line 3, in <module>
    from macholib import MachO as macho
  File "./lldbmacros/macholib/MachO.py", line 10, in <module>
    from .mach_o import MH_FILETYPE_SHORTNAMES, LC_DYSYMTAB, LC_SYMTAB
  File "./lldbmacros/macholib/mach_o.py", line 16, in <module>
    from macholib.ptypes import p_uint32, p_uint64, Structure, p_long, pypackable

Relevant section before 2to3:

try:
    from itertools import izip, imap
except ImportError:
    izip, imap = zip, map
from itertools import chain, starmap

And after 2to3:

try:
    
except ImportError:
    izip, imap = zip, map
from itertools import chain, starmap


* Side note:
This specific case may only be problematic with scripts that are partially aware of Python 3, otherwise they wouldn't try-catch that import.

* Proposed solution:
In case of that kind of import being the single line of an indented block, change it to "pass" instead of a blank line.
msg360230 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2020-01-18 03:15
Thank you for the report.  This is a duplicate of #38681.
History
Date User Action Args
2020-01-18 03:15:58cheryl.sabellasetstatus: open -> closed

superseder: 2to3 Conversion Result using BlankLine() can be Syntactically Incorrect

nosy: + cheryl.sabella
messages: + msg360230
resolution: duplicate
stage: resolved
2020-01-14 15:47:38galun.guycreate