Skip to content

Fix bare except clauses: use specific exception types#410

Open
koteshyelamati wants to merge 2 commits into
pythonprofilers:masterfrom
koteshyelamati:master
Open

Fix bare except clauses: use specific exception types#410
koteshyelamati wants to merge 2 commits into
pythonprofilers:masterfrom
koteshyelamati:master

Conversation

@koteshyelamati

Copy link
Copy Markdown

Fix bare except: clauses that catch BaseException (including SystemExit and KeyboardInterrupt). Replace with specific exception types:

  • memory_profiler.py: except:except (ValueError, IndexError): — the try block parses float(out[1].split()[vsz_index]) / 1024, which can raise ValueError (bad float) or IndexError (missing field)
  • mprof.py (line ~251): except:except Exception: — wraps proc.cmdline() which can raise various psutil exceptions
  • mprof.py (line ~759): except:except (ValueError, AttributeError): — the try block calls float(x) (raises ValueError) and value.split(',') (raises AttributeError if not a string)

Using bare except: is unsafe because it silences KeyboardInterrupt and SystemExit, making the program unresponsive to Ctrl+C or interpreter shutdown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant