[Python-Dev] Showstopper in import?

Michael Hudson mwh21@cam.ac.uk
02 Feb 2001 13:15:14 +0000


"Tim Peters" <tim.one@home.com> writes:

> > Percolator has "from x import *" code. This is what is causing the
> > exception.
> 
> Woo hoo!  The traceback bamboozled me:  it doesn't show any code from
> Percolator.py, just the import in EditorWindow.py.  So I'll call *that* the
> bug <0.7 wink>.
> 
> > I think it has already been fixed in CVS though, so should
> > work again.
> 
> Doesn't work for me.  If someone does patch Percolator.py, though, it will
> just blow up again at
> 
>     from IOBinding import IOBinding
> 
> .  Guido was apparently fond of this trick.

I apologise if I'm explaining things people already know here, but I
can explain the wierdo tracebacks.

Try this:

>>> def f():
...  from string import *
...  pass
... 
SyntaxError: 'from ... import *' may only occur in a module scope
>>>

you see?  No traceback at all!  This is a general feature of
exceptions raised by the compiler (as opposed to the parser).

>>> 21323124912094230491
OverflowError: integer literal too large
>>>

(also using some name other than "as" in an "import as" statement,
invalid unicode \N{names}, various arglist nogos (eg. "def
f(a=1,e):"), assigning to an expression, ... the list goes on & is
getting longer).

So what's happening is module A imports module B, which fails to
copmile due to a non-module level "import *", but doesn't set up a
traceback, so the traceback points at the import statement in module
A.  And as the exception message mentions import statements, everyone
gets confused.

The fix?  Presumably rigging compile.c:com_error to set up tracebacks
properly?  It looks like it *tries* to, but I don't know this area of
the code well enough to understand why it doesn't work.  Anyone?

Cheers,
M.

-- 
  same software, different verbosity settings (this one goes to
  eleven)                             -- the effbot on the martellibot