-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
In developer mode (-X dev), ResourceWarning is only emited once per line numbers #76270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The -X dev mode currently *hides* some ResourceWarning warnings: $ cat x.py
def func():
open('/etc/issue') func()
func() $ ./python x.py
x.py:2: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/issue' mode='r' encoding='UTF-8'>
open('/etc/issue')
x.py:2: ResourceWarning: unclosed file <_io.TextIOWrapper name='/etc/issue' mode='r' encoding='UTF-8'>
open('/etc/issue') haypo@selma$ ./python -X dev x.py The problem is that the "-W default" inserted by -X dev overrides the final filter on ResourceWarning: $ ./python -X dev -c 'import warnings, pprint; pprint.pprint(warnings.filters)'
[('default',
re.compile('', re.IGNORECASE),
<class 'Warning'>,
re.compile(''),
0),
('ignore', None, <class 'BytesWarning'>, None, 0),
('always', None, <class 'ResourceWarning'>, None, 0)] |
Attached PR 4482 fixes warnings filters: haypo@selma$ ./python -X dev -c 'import warnings, pprint; pprint.pprint(warnings.filters)' |
I don't agree with this. You're comparing "-X dev" with a pydebug build of Python, not with a normal Python. Your example shows the problem. If a program calls func() 1000 times in a row, they will show the same warning 1000 times. What does it bring to have the exact same warning (and line number) displayed 1000 times instead of once? Nothing. There is a reason the filter "default" isn't the same as "always", and it's that "always" can be completely overwhelming. |
I don't know the rationale of the "always" action rather than "default". The choice was made 7 years ago, when Georg Brandl implemented the new ResourceWarning category: commit 08be72d. The problem of ResourceWarning is that the warning logs where the last reference to the resource was cleared, or where the a garbage collection was triggered. Multiple resources can be created from different places, but all "die" at the same place. In Python 3.6, I enhanced ResourceWarning to log also the traceback where the leaked resource has been created, if the tracemalloc is tracing memory allocations. When tracemalloc is used, it's useful to log ResourceWarning multiple times even if the warning itself is logged multipletimes. Well, that's an unusual usage of the ResourceWarning, we can also suggest to use -W always::ResourceWarning in that case.
In general, I would like to offer the same experience in "development mode" (-X dev) and with a pydebug build. For ResourceWarning, your rationale only concerns pydebug build, no? |
Le 21/11/2017 à 14:46, STINNER Victor a écrit :
Neither do I. But I don't think it matters a lot. pydebug builds are
If they have different names, they will be logged separately. >>> def f(fname):
... open(fname)
...
>>> f('setup.py')
__main__:2: ResourceWarning: unclosed file <_io.TextIOWrapper
name='setup.py' mode='r' encoding='UTF-8'>
>>> f('setup.py')
>>> f('LICENSE.txt')
__main__:2: ResourceWarning: unclosed file <_io.TextIOWrapper
name='LICENSE.txt' mode='r' encoding='UTF-8'>
>>> f('LICENSE.txt')
>>>
Why? I'm talking about "-X dev", not pydebug builds. |
I forgot: the snippet above was made with "python -Wdefault::ResourceWarning". |
See also bpo-27535. |
Antoine:
Oh wow, nice behaviour, I like it :-) Antoine:
Oops sorry, typo (brain error), I mean: "also concerns". Serhiy:
Oh, not leaking memory in warnings registies is a nice side effect of the "always" action :-) -- It's much more complicated than what I expected to get the "correct behaviour" for warnings :-/ |
Antoine: What do you think of using the "default" action for pydebug build and -X dev? |
That would be fine with me. |
+1 from me for using "default" instead of "always" for ResourceWarning. Folks can always combine "-X tracemalloc" with "-W always::ResourceWarning" if want to ensure they see absolutely every resource warning, rather than only representative ones. |
The "default warning filters" are documented, but only for Python compiled in release mode: So I didn't touch this documentation. The initial issue is now fixed: the development mode (-X dev) now behaves as the debug mode (pydebug build) for the default warning filters. The second issue, Antoine's complain about the "always" action, was also fixed. I close the issue. At the end, we should get a better debug experience ;-) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: