[2to3] Bug converting import
Christian Heimes
lists at cheimes.de
Mon Jun 23 17:24:12 EDT 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
More information about the Python-list
mailing list