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
Dead code should be removed #61718
Comments
Using my astoptimizer project and the hook proposed in the issue bpo-17515, I found the following dead code. Some code is not dead or unreachable in the general case, but only on a specific platform. I ran astoptimizer with all optimizations enabled, so os.name is replaced with "posix" for example. So "return os.name" is seen as dead code in the following function: def _get_default_scheme():
if os.name == 'posix':
# the default scheme for posix is posix_prefix
return 'posix_prefix'
return os.name Unreachable Lib/_pyio.py:2057: Assign(targets=[Name(id='initial_value', ctx=Store())], value=Call(func=Name(id='str', ctx=Load()), ... Dead code Lib/datetime.py:2128: Expr(value=Str(s='\nSome time zone algebra. For a datetime x, let\n x.n = x stripped of its time... Almost Dead code Lib/sqlite3/test/dump.py:44: Expr(value=ListComp(elt=Call(func=Attribute(value=Attribute(value=Name(id='self', ctx=Load()), attr=... |
"Almost Dead code" is code generally considered as useless, but many Python unit tests use such code. Dummy example: try: I don't think that we should touch this code, it's just for information. |
Your "dead code" looks like multiline comments (at least it is in the email case). I thought those were optimized away when the pyc is written. What is "almost dead code"? If it is the stuff that is only applicable to a given platform, then presumably it is needed for that platform and why it it worth mentioning? |
Here is a concrete patch.
Oh, it looks like you are right: useless strings are already removed during compilation. But it looks a little bit surprising to me to use a multiline string for a comment. I prefer classic # comments.
It's just for information, just ignore these "warnings" :-) |
The code in test_peepholer should be removed. That code isn't run directly. Instead, it is tested for a correct disassembly. |
I was surprised by this as well. I think the comment in _header_value_parser was originally a docstring and got moved around during editing. It should probably get changed to a block comment. I could have sworn that when I learned about the optimization it was in the context of documentation that specifically said the optimization was done so that triple quoted strings could be used as multi-line comments, but I cannot find it through google or guessing where I might have seen it in our docs, so perhaps I was imagining things. |
For the dead code in test_posix.py, see issue bpo-9246. |
The instance in reindent.py uses the "docstring for attributes" feature that some documentation tools supports, which is a unused string *after* the attribute. When you turn that into a comment, put the comment *before* the attribute. |
test_create_autospec_unbound_methods() of Lib/unittest/test/testmock/testhelpers.py contains dead code because of a known issue. I created issue bpo-17548 to track this function. |
New changeset 313bcff51900 by Victor Stinner in branch 'default': New changeset 33bdd0a985b9 by Victor Stinner in branch 'default': New changeset 9dba3bc7c2a6 by Victor Stinner in branch 'default': |
Thanks for the different reviews. |
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: