[2to3] Bug converting import

Discussion in 'Python' started by Helmut Jarausch, Jun 23, 2008.

  1. Hi

    Given the following two files in the same directory

    Master.py:
    ----------
    #!/usr/bin/python
    import Slave
    Slave.main()

    and
    Slave.py:
    ---------
    def main() :
    print "Hello World"

    Invoking Master.py under python-2.5.2
    works just fine.

    2to3 converts these to

    Master.py:
    ----------
    from . import Slave
    Slave.main()

    I have added the first line
    #!/usr/local/bin/python3.0
    manually

    Slave.py:
    ---------
    def main() :
    print("Hello World")


    Now, when I invoke Master.py I get

    Traceback (most recent call last):
    File "Master.py", line 2, in <module>
    from . import Slave
    ValueError: Attempted relative import in non-package


    thanks for looking into it,

    Helmut Jarausch

    Lehrstuhl fuer Numerische Mathematik
    RWTH - Aachen University
    D 52056 Aachen, Germany
    Helmut Jarausch, Jun 23, 2008
    #1
    1. Advertising

  2. Helmut Jarausch wrote:
    > Now, when I invoke Master.py I get
    >
    > Traceback (most recent call last):
    > File "Master.py", line 2, in <module>
    > from . import Slave
    > ValueError: Attempted relative import in non-package
    >
    >
    > thanks for looking into it,


    The cause of the bug is in fixes/fix_import.py
    probably_a_local_import(). The function doesn't check if
    dirname(file_path) is a package.

    This patch should fix the bug:

    Index: Lib/lib2to3/fixes/fix_import.py
    ===================================================================
    --- Lib/lib2to3/fixes/fix_import.py (Revision 64490)
    +++ Lib/lib2to3/fixes/fix_import.py (Arbeitskopie)
    @@ -53,8 +53,13 @@
    # Must be stripped because the right space is included by the parser
    imp_name = imp_name.split('.', 1)[0].strip()
    base_path = dirname(file_path)
    - base_path = join(base_path, imp_name)
    - for ext in ['.py', pathsep, '.pyc', '.so', '.sl', '.pyd']:
    - if exists(base_path + ext):
    + base_name = join(base_path, imp_name)
    + base_init = join(base_path, "__init__")
    + exts = ['.py', pathsep, '.pyc', 'pyo', '.so', '.sl', '.pyd']
    + if not any(exists(base_init + ext) for ext in exts):
    + # not a package
    + return False
    + if any(exists(base_name + ext) for ext in exts):
    return True
    return False
    Christian Heimes, Jun 23, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robin Becker

    2to3.py download

    Robin Becker, Feb 12, 2008, in forum: Python
    Replies:
    0
    Views:
    647
    Robin Becker
    Feb 12, 2008
  2. Guilherme Polo

    Re: 2to3.py download

    Guilherme Polo, Feb 12, 2008, in forum: Python
    Replies:
    0
    Views:
    944
    Guilherme Polo
    Feb 12, 2008
  3. Helmut Jarausch

    2to3 bug and question

    Helmut Jarausch, Jun 23, 2008, in forum: Python
    Replies:
    2
    Views:
    239
    Terry Reedy
    Jun 23, 2008
  4. Replies:
    4
    Views:
    456
    Isaac Gouy
    Jan 2, 2009
  5. 2to3 Help?

    , Jan 15, 2009, in forum: Python
    Replies:
    14
    Views:
    2,812
Loading...

Share This Page