[New-bugs-announce] [issue39331] 2to3 mishandles indented imports

Guy Galun report at bugs.python.org
Tue Jan 14 10:47:38 EST 2020


New submission from Guy Galun <galun.guy at gmail.com>:

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.

----------
components: 2to3 (2.x to 3.x conversion tool)
files: ptypes.py
messages: 359978
nosy: galun.guy
priority: normal
severity: normal
status: open
title: 2to3 mishandles indented imports
type: crash
versions: Python 3.9
Added file: https://bugs.python.org/file48839/ptypes.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39331>
_______________________________________


More information about the New-bugs-announce mailing list