-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
bpo-29564: warnings suggests to enable tracemalloc #10486
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
Conversation
The warnings module now suggests to enable tracemalloc if the source is specified, tracemalloc module is available, but tracemalloc is not tracing memory allocations.
@@ -77,6 +83,9 @@ def _formatwarnmsg_impl(msg): | |||
if line: | |||
line = line.strip() | |||
s += ' %s\n' % line | |||
elif not tracing: | |||
s += (f'{category}: Enable tracemalloc to get the object ' | |||
f'allocation traceback\n') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Would be useful to add to this sentence how to enable tracemalloc quickly? For example:
s += (f'{category}: Enable tracemalloc to get the object '
f'allocation traceback (i.e. -X tracemalloc=N_FRAMES)\n')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are multiple ways to enable tracemalloc: PYTHONTRACEMALLOC=nframe and python3 -X tracemalloc=nframe. It's non-obvious where -X tracemalloc comes from, nor what is nframe, etc.
"ResourceWarning: Enable tracemalloc to get the object allocation traceback" is already 74 characters long, and I like to fit into 80 columns :-)
IMHO giving the hint "enable tracemalloc" should be enough to someone motivated to fix a ResourceWarning bug :)
@pablogsal: What do you think? Is the current PR good now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup :)
I am convinced by your 80 columns argument :D
Lib/warnings.py
Outdated
except Exception: | ||
# When a warning is logged during Python shutdown, tracemalloc | ||
# and the import machinery don't work anymore | ||
except ImportError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the CI is failing because other exceptions can be propagated from import tracemalloc
(if you cange this except ImportError
to except Exception
as it was before the CI passes).
The error is a bit cryptic because any exception that happens during _formatwarnmsg_impl
will trigger an infinite recursion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"except Exception:" is already used multiple times in the function, so yeah, I'm ok to replace "except ImportError: "with "except Exception:". I added a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7. |
GH-10509 is a backport of this pull request to the 3.7 branch. |
The warnings module now suggests to enable tracemalloc if the source is specified, tracemalloc module is available, but tracemalloc is not tracing memory allocations. (cherry picked from commit 2c07c49) Co-authored-by: Victor Stinner <vstinner@redhat.com>
The warnings module now suggests to enable tracemalloc if the source is specified, tracemalloc module is available, but tracemalloc is not tracing memory allocations. (cherry picked from commit 2c07c49) Co-authored-by: Victor Stinner <vstinner@redhat.com>
For some reason, I expected that thefe's an optimizations for modules already found in cache, hence that respective file descriptors would not be duplicated needlessly. I was wrong, as fortunately Python can figure out on its own these days, and moreover is capable to indicate precisely where the problem occurred. Initial messages (./run-tests filter): /clufter/format.py:807: ResourceWarning: unclosed file <_io.TextIOWrapper name='/clufter/tests/XMLFormat-walk/cluster/quorumd/heuristic.py' mode='r' encoding='utf-8'> mfile, mpath, mdesc = find_module(name, [root]) ResourceWarning: Enable tracemalloc to get the object allocation traceback /clufter/filter.py:1066: ResourceWarning: unclosed file <_io.TextIOWrapper name='/clufter/tests/XMLFormat-walk/cluster/rm/failoverdomains/failoverdomain.py' mode='r' encoding='utf-8'> walk = in_obj.walk_schema(root_dir, preprocess=cls._xslt_preprocess, ResourceWarning: Enable tracemalloc to get the object allocation traceback Sample message when tracemalloc enabled: Object allocated at (most recent call last): File "/usr/lib64/python3.8/runpy.py", lineno 192 return _run_code(code, main_globals, None, [14 frames skipped] File "/clufter/tests/filter.py", lineno 66 r = flt.get_template(in_obj, symbol='direct_xslt_test', File "/clufter/filter.py", lineno 1066 walk = in_obj.walk_schema(root_dir, preprocess=cls._xslt_preprocess, File "/clufter/format.py", lineno 807 mfile, mpath, mdesc = find_module(name, [root]) File "/usr/lib64/python3.8/imp.py", lineno 302 file = open(file_path, mode, encoding=encoding) References: https://docs.python.org/3/whatsnew/3.6.html#warnings python/cpython#10486 Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
For some reason, I expected that thefe's an optimizations for modules already found in cache, hence that respective file descriptors would not be duplicated needlessly. I was wrong, as fortunately Python can figure out on its own these days, and moreover is capable to indicate precisely where the problem occurred. Initial messages (./run-tests filter): /clufter/format.py:807: ResourceWarning: unclosed file <_io.TextIOWrapper name='/clufter/tests/XMLFormat-walk/cluster/quorumd/heuristic.py' mode='r' encoding='utf-8'> mfile, mpath, mdesc = find_module(name, [root]) ResourceWarning: Enable tracemalloc to get the object allocation traceback /clufter/filter.py:1066: ResourceWarning: unclosed file <_io.TextIOWrapper name='/clufter/tests/XMLFormat-walk/cluster/rm/failoverdomains/failoverdomain.py' mode='r' encoding='utf-8'> walk = in_obj.walk_schema(root_dir, preprocess=cls._xslt_preprocess, ResourceWarning: Enable tracemalloc to get the object allocation traceback Sample message when tracemalloc enabled: Object allocated at (most recent call last): File "/usr/lib64/python3.8/runpy.py", lineno 192 return _run_code(code, main_globals, None, [14 frames skipped] File "/clufter/tests/filter.py", lineno 66 r = flt.get_template(in_obj, symbol='direct_xslt_test', File "/clufter/filter.py", lineno 1066 walk = in_obj.walk_schema(root_dir, preprocess=cls._xslt_preprocess, File "/clufter/format.py", lineno 807 mfile, mpath, mdesc = find_module(name, [root]) File "/usr/lib64/python3.8/imp.py", lineno 302 file = open(file_path, mode, encoding=encoding) References: https://docs.python.org/3/whatsnew/3.6.html#warnings python/cpython#10486 Signed-off-by: Jan Pokorný <jpokorny@redhat.com>
The warnings module now suggests to enable tracemalloc if the source
is specified, tracemalloc module is available, but tracemalloc is not
tracing memory allocations.
https://bugs.python.org/issue29564