python - How can I check if a warning was raised but still not to turn it into an exception -
i have method get_font
can raise defaultfontwarning
. want check if warning raised , run piece of code if it's case.
my current solution is
def set_font(self): warnings.catch_warnings(): warnings.filterwarnings("error") try: self.font = get_font(self.path, self.size) except defaultfontwarning: self.is_default = true
this attaches is_default
self
, silences warning user cannot see it.
how can check if warning raised still not turn exception , output it?
raise warning
(or type; such userwarning
), retain original traceback.
def set_font(self): warnings.catch_warnings(): try: self.font = get_font(self.path, self.size) except defaultfontwarning e: raise warning(*e.args).with_traceback(e.__traceback__) none
using from none
rid of during handling ...
message. can use own message such 'bad font'
replacing *e.args
:
raise warning('bad font').with_traceback(e.__traceback__) none
Comments
Post a Comment