[Python-Dev] syntax warnings don't go through warnings.warn?
Robert Kern
robert.kern at gmail.com
Mon Jun 1 19:44:22 CEST 2009
On 2009-06-01 11:50, Dino Viehland wrote:
> I’m just a little surprised by this - Is there a reason why syntax
> warnings are special and untrappable via warnings.warn?
>
> >>> import warnings
>
> >>> def mywarn(*args): print 'xx', args
>
> ...
>
> >>> warnings.warn = mywarn
>
> >>> compile("def f():\n a = 1\n global a\n", "", "exec")
>
> :3: SyntaxWarning: name 'a' is assigned to before global declaration
>
> <code object <module> at 012DB410, file "", line 1>
symtable.c uses PyErr_WarnExplicit() to provide file and line number
information. You need to stub warnings.warn_explicit().
>>> import warnings
>>> def mywarn_explicit(*args):
... print 'xx', args
...
>>> warnings.warn_explicit = mywarn_explicit
>>> compile("def f():\n a = 1\n global a\n", "", "exec")
xx ("name 'a' is assigned to before global declaration", <type
'exceptions.SyntaxWarning'>, '', 3, None, None)
<code object <module> at 0x39e9f8, file "", line 1>
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-Dev
mailing list