By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,454 Members | 3,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,454 IT Pros & Developers. It's quick & easy.

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

P: n/a
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....e460a0ee8b03a5

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?

May 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
funkyj wrote:
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
May 19 '06 #2

P: n/a

Peter Otten wrote:
funkyj wrote:
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).
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!

May 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.