how to suppress the "source code echo" output by warnings.warn("x")?

F

funkyj

I've been googling around trying to find the answer to this question
but all I've managed to turn up is a 2 year old post of someone else
asking the same question (no answer though).

http://groups.google.com/group/comp...=python+warnings+echo&rnum=6#81e460a0ee8b03a5

jh> In the following
jh>
jh> import warnings
jh> warnings.warn('change me')
jh>
jh> The warning is issued:
jh>
jh> hunter:~/python/test> python test.py
jh> test.py:3: UserWarning: change me
jh> warnings.warn('change me')
jh>
jh> I want to supress the line echo. Eg, I just want
jh>
jh> hunter:~/python/test> python test.py
jh> test.py:3: UserWarning: change me
jh>
jh> How do I configure warnings to do this?

Perhaps this can't be done without rewriting the warning module?
 
P

Peter Otten

funkyj said:
I've been googling around trying to find the answer to this question
but all I've managed to turn up is a 2 year old post of someone else
asking the same question (no answer though).
jh> In the following
jh>
jh> import warnings
jh> warnings.warn('change me')
jh>
jh> The warning is issued:
jh>
jh> hunter:~/python/test> python test.py
jh> test.py:3: UserWarning: change me
jh> warnings.warn('change me')
jh>
jh> I want to supress the line echo. Eg, I just want
jh>
jh> hunter:~/python/test> python test.py
jh> test.py:3: UserWarning: change me
jh>
jh> How do I configure warnings to do this?

Perhaps this can't be done without rewriting the warning module?

How about monkey-patching?

import warnings

def formatwarning(message, category, filename, lineno):
return "%s:%s: %s: %s\n" % (filename, lineno,
category.__name__, message)

warnings.formatwarning = formatwarning

warnings.warn("so what")

Peter
 
F

funkyj

How about monkey-patching?

import warnings

def formatwarning(message, category, filename, lineno):
return "%s:%s: %s: %s\n" % (filename, lineno,
category.__name__, message)

warnings.formatwarning = formatwarning

warnings.warn("so what")

Thanks, that did the trick!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top