summaryrefslogtreecommitdiff
path: root/pyflakes/api.py
diff options
context:
space:
mode:
authorPhil Frost <indigo@bitglue.com>2015-04-17 09:01:28 -0400
committerPhil Frost <indigo@bitglue.com>2015-04-17 09:07:53 -0400
commit7b7f50a0810b343be3623c141cca50a487bdec31 (patch)
tree3408da2f2f5f038d242243a835813c8c6b853451 /pyflakes/api.py
parentc5d8e427fe07c1edc286d74d5bbc1d5659783452 (diff)
downloadpyflakes-7b7f50a0810b343be3623c141cca50a487bdec31.tar.gz
Don't crash with an AttributeError on Windows
Depending on the C library, some constants (like signal.SIGINT) might not be defined. The previous code attempted to avoid failures on invalid signals by catching the ValueError raised by signal.signal when given an invalid signal to handle, however failed to account for the possiblity of the signal not being defined at all.
Diffstat (limited to 'pyflakes/api.py')
-rw-r--r--pyflakes/api.py35
1 files changed, 29 insertions, 6 deletions
diff --git a/pyflakes/api.py b/pyflakes/api.py
index 379fc20..3bc2330 100644
--- a/pyflakes/api.py
+++ b/pyflakes/api.py
@@ -130,17 +130,40 @@ def checkRecursive(paths, reporter):
return warnings
+def _exitOnSignal(sigName, message):
+ """Handles a signal with sys.exit.
+
+ Some of these signals (SIGPIPE, for example) don't exist or are invalid on
+ Windows. So, ignore errors that might arise.
+ """
+ import signal
+
+ try:
+ sigNumber = getattr(signal, sigName)
+ except AttributeError:
+ # the signal constants defined in the signal module are defined by
+ # whether the C library supports them or not. So, SIGPIPE might not
+ # even be defined.
+ return
+
+ def handler(sig, f):
+ sys.exit(message)
+
+ try:
+ signal.signal(sigNumber, handler)
+ except ValueError:
+ # It's also possible the signal is defined, but then it's invalid. In
+ # this case, signal.signal raises ValueError.
+ pass
+
+
def main(prog=None):
"""Entry point for the script "pyflakes"."""
import optparse
- import signal
# Handle "Keyboard Interrupt" and "Broken pipe" gracefully
- try:
- signal.signal(signal.SIGINT, lambda sig, f: sys.exit('... stopped'))
- signal.signal(signal.SIGPIPE, lambda sig, f: sys.exit(1))
- except ValueError:
- pass # SIGPIPE is not supported on Windows
+ _exitOnSignal('SIGINT', '... stopped')
+ _exitOnSignal('SIGPIPE', 1)
parser = optparse.OptionParser(prog=prog, version=__version__)
(__, args) = parser.parse_args()