Bug report
Bug description:
Hey!
I'm not sure this is a bug, exactly, or just behaviour that is not documented well (or at all, despite me looking I couldn't really find anything on it), but I recently ran into it and was scratching my head for a good hour.
The "issue" if it even is one, is that the default warnings.showwarning handler is always called when the main scope for the interpreter is getting cleaned up, even if there is a custom showwarning hook.
I noticed this by having some resource leak messages in the __del__ calls for some objects, and noticed that they only trigger the proper handler if they are triggered in any scope under the main scope, but if done so in the main scope they always use the default handler.
The following code reproduces the behaviour:
import warnings
def _warning_handler(message, category, filename, lineno, output_file, line):
warn = warnings.formatwarning(message, category, filename, lineno, line)
print(f'😺: {warn}', file = output_file)
warnings.showwarning = _warning_handler
def meow():
print(f'Warning handler is: {warnings.showwarning.__code__.co_filename}')
warnings.warn('meow')
class Cat:
def __del__(self):
print(f'Warning handler is: {warnings.showwarning.__code__.co_filename}')
warnings.warn('nya')
meow()
cat = Cat()
The output of this code is:
Warning handler is: /tmp/mvp.py
😺: /tmp/mvp.py:11: UserWarning: meow
warnings.warn('meow')
Warning handler is: /tmp/mvp.py
/tmp/mvp.py:16: UserWarning: nya
CPython versions tested on:
3.14
Operating systems tested on:
Linux
Bug report
Bug description:
Hey!
I'm not sure this is a bug, exactly, or just behaviour that is not documented well (or at all, despite me looking I couldn't really find anything on it), but I recently ran into it and was scratching my head for a good hour.
The "issue" if it even is one, is that the default
warnings.showwarninghandler is always called when the main scope for the interpreter is getting cleaned up, even if there is a customshowwarninghook.I noticed this by having some resource leak messages in the
__del__calls for some objects, and noticed that they only trigger the proper handler if they are triggered in any scope under the main scope, but if done so in the main scope they always use the default handler.The following code reproduces the behaviour:
The output of this code is:
CPython versions tested on:
3.14
Operating systems tested on:
Linux