no-exit

Try in Playground
python-best-practicesBest PracticeInformational

0

No tags

No CWE or CVE

Use sys.exit() instead of exit(). Exit is a builtin and done mostly for the console. sys.exit() is done for program with a proper return argument (see documentation).

Ast Rule: function call


no-exit

How to write a rule
function visit(node) {
  if((!node.moduleOrObject || node.moduleOrObject.value !== "sys") && node.functionName.value === "exit"){
    const hasOneArgument = node.arguments.values && node.arguments.values.length === 1;

    const error = buildError(node.start.line, node.start.col, node.end.line, node.end.col, "do not use exit()", "CRITICAL", "SAFETY");

    const argumentValue = node.arguments.values[0].value.str;
    const newFunctionCall = `sys.exit(${argumentValue})`;
    const editReplaceFunctionCall = buildEditUpdate(node.start.line, node.start.col, node.end.line, node.end.col, newFunctionCall)

    const editAddImport = buildEditAdd(1, 1, "import sys\n");

    const fix = buildFix("replace with sys.exit()", [editReplaceFunctionCall, editAddImport]);
    addError(error.addFix(fix));
  }
}

use-sys-exit.py

Expected test result: no error

import sys
print("bla")
sys.exit(0)

use-exit.py

Expected test result: has error

print("bla")
exit(0)
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
Legal
  • Security
  • Privacy Policy
  • Code Privacy
  • Terms of Service
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.