Fix bare except clause in setup.py - #5051
Conversation
|
Thanks for the PR, please fix the commit validity check. |
|
Thank you for the review, @polybassa! Could you clarify what the commit validity check requires? I see the "Check the validity of the commits" CI check is failing — is this a DCO sign-off ( |
|
itzzdev09
left a comment
There was a problem hiding this comment.
Confirmed correct, and I dug into why this one survived — which seems worth recording, since the fix alone doesn't stop it recurring.
E722 is not in the project's ignore list (ignore = E203, E731, W504, W503), so flake8 does flag bare excepts. Running it directly on the file:
$ flake8 setup.py
setup.py:19:1: E722 do not use bare 'except'
setup.py:97:89: E501 line too long (93 > 88 characters)
The reason CI never saw it is the lint target:
[testenv:flake8]
commands = flake8 scapy/setup.py sits at the repo root, outside scapy/, so it's never linted. That also explains why this is the only bare except: left in the tree — grep finds exactly one occurrence repo-wide, and it happens to be in the one Python file the linter doesn't cover.
After the change in this PR, flake8 setup.py reports only the pre-existing E501 on line 97.
On correctness: from setuptools import ... raises ImportError when setuptools is missing (ModuleNotFoundError subclasses it, so that's covered too), and the narrowing does the intended job of no longer converting a KeyboardInterrupt during import into "setuptools is required to install scapy !".
Two things maintainers might want to decide alongside this:
- Whether to widen the lint target to catch regressions — something like
flake8 scapy/ setup.py. That would also surface theE501on line 97, so it isn't a zero-diff change. - Whether the
raise ImportError(...)should beraise ImportError(...) from None. As written the original traceback is chained in as__context__, so the user sees both the underlying import failure and the friendly message. That's arguably useful here, so it may be intentional — just noting it since the PR is already touching this handler.
Neither blocks the change; the PR is a strict improvement as-is.
Disclosure: reviewed with AI assistance (Claude Code). The flake8 output above is from actually running it against this file, before and after the change.
Replace bare
except:withexcept ImportError:insetup.py.The guarded block imports from
setuptools— specificallyfrom setuptools import .... The only exception that can reasonably be raised here isImportError(when setuptools is not installed). Usingexcept ImportError:makes the intent explicit and avoids accidentally swallowingKeyboardInterruptorSystemExit.