CodeQL documentation

Use of exit() or quit()

ID: py/use-of-exit-or-quit
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

The exit and quit “functions” are actually site.Quitter objects and are loaded, at interpreter start up, from site.py. However, if the interpreter is started with the -S flag, or a custom site.py is used then exit and quit may not be present.

Recommendation

Replace uses of exit() and quit() with sys.exit() which is built into the interpreter and is guaranteed to be present.

Example

In this example, exit() is used and will fail if the interpreter is passed the -S option.


def main():
    try:
        process()
    except Exception as ex:
        print(ex)
        exit(1)

In this example, sys.exit() is used and will behave the same regardless of the interpreter options.

import sys

def main():
    try:
        process()
    except Exception as ex:
        print(ex)
        sys.exit(1)

References

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy