Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [2to3] Bug converting import

Reply
Thread Tools

[2to3] Bug converting import

 
 
Helmut Jarausch
Guest
Posts: n/a
 
      06-23-2008
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
 
Reply With Quote
 
 
 
 
Christian Heimes
Guest
Posts: n/a
 
      06-23-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
to use import java.lang.* or import java.lang.Math or none at all? JPractitioner Java 13 02-24-2006 08:48 PM
XML Schema question - does "import" import elements? Vitali Gontsharuk XML 2 08-25-2005 07:33 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM
import/from import question Artur M. Piwko Python 1 07-02-2003 07:04 PM



Advertisments