Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Use of exit() or quit()

ID: py/use-of-exit-or-quitKind: problemSecurity severity: Severity: warningPrecision: very-highTags:   - quality   - reliability   - correctnessQuery suites:   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

Theexit andquit “functions” are actuallysite.Quitter objects and are loaded, at interpreter start up, fromsite.py. However, if the interpreter is started with the-S flag, or a customsite.py is used thenexit andquit may not be present.

Recommendation

Replace uses ofexit() andquit() withsys.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.

defmain():try:process()exceptExceptionasex:print(ex)exit(1)

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

importsysdefmain():try:process()exceptExceptionasex:print(ex)sys.exit(1)

References


[8]ページ先頭

©2009-2025 Movatter.jp