[issue9386] Bad indentation in urllib import fixer with multipe imports

Dave Malcolm report at bugs.python.org
Mon Jul 26 19:28:27 CEST 2010


New submission from Dave Malcolm <dmalcolm at redhat.com>:

Running 2to3 on lxml-2.2.6 failed with broken indentation.

This fragment from lxml/html/__init__.py:
> def open_http_urllib(method, url, values):
>     ## FIXME: should test that it's not a relative URL or something
>     try:
>        from urllib import urlencode, urlopen
>     except ImportError: # Python 3

became:
> def open_http_urllib(method, url, values):
>     ## FIXME: should test that it's not a relative URL or something
>     try:
>         from urllib.parse import urlencode
> from urllib.request import urlopen
>     except ImportError: # Python 3

which is syntactically invalid: note the invalid indentation for the second "import" line.

Seems to work when there's a single name imported per line; fails when more than one name is imported from urlib.


Am attaching a patch to test_fixers (for the 2.7 branch) which adds a reproducer.  I don't have a fix.

test test_lib2to3 failed -- Traceback (most recent call last):
  File "/home/david/coding/python-svn/2.7-2to3/Lib/lib2to3/tests/test_fixers.py", line 1858, in test_import_indentation
    self.check(b, a)
  File "/home/david/coding/python-svn/2.7-2to3/Lib/lib2to3/tests/test_fixers.py", line 37, in check
    tree = self._check(before, after)
  File "/home/david/coding/python-svn/2.7-2to3/Lib/lib2to3/tests/test_fixers.py", line 33, in _check
    self.assertEqual(after, unicode(tree))
AssertionError: u"\ndef foo():\n    from urllib.parse import urlencode\n    from urllib.parse im [truncated]... != u"\ndef foo():\n    from urllib.parse import urlencode\nfrom urllib.request impo [truncated]...
  
  def foo():
      from urllib.parse import urlencode
-     from urllib.parse import urlopen
? ----            --  ^
+ from urllib.request import urlopen
?              ++++ ^
      print('got here')
  
(Note to self: tracking this downstream in Fedora for lxml as https://bugzilla.redhat.com/show_bug.cgi?id=600036 )

----------
components: 2to3 (2.x to 3.0 conversion tool)
files: urllib-indentation-issue.patch
keywords: patch
messages: 111649
nosy: dmalcolm
priority: normal
severity: normal
stage: needs patch
status: open
title: Bad indentation in urllib import fixer with multipe imports
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file18212/urllib-indentation-issue.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9386>
_______________________________________


More information about the Python-bugs-list mailing list