Skip to content

Commit a27725e

Browse files
authored
Merge pull request #125 from certik/sys_exit
Implement sys.exit()
2 parents e3e580f + e435d09 commit a27725e

File tree

11 files changed

+38
-12
lines changed

11 files changed

+38
-12
lines changed

integration_tests/exit_01.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from sys import exit
2+
3+
def main0():
4+
print("Before")
5+
exit(0)
6+
print("After")
7+
8+
main0()

integration_tests/run_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55

66
tests = [
7+
"exit_01.py",
78
"expr_01.py",
89
"expr_02.py",
910
"expr_03.py",

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,8 +1785,16 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
17851785
args.p, args.size());
17861786
return;
17871787

1788-
} else if (call_name == "exit" || call_name == "quit") {
1789-
ASR::expr_t *code = nullptr;
1788+
} else if (call_name == "quit") {
1789+
ASR::expr_t *code;
1790+
if (args.size() == 0) {
1791+
code = nullptr;
1792+
} else if (args.size() == 1) {
1793+
code = args[0];
1794+
} else {
1795+
throw SemanticError("The function quit() requires 0 or 1 arguments",
1796+
x.base.base.loc);
1797+
}
17901798
tmp = ASR::make_Stop_t(al, x.base.base.loc, code);
17911799
return;
17921800
}

src/runtime/sys.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from ltypes import i32
2+
13
def exit(error_code: i32):
24
"""
35
Exits the program with an error code `error_code`.
46
"""
57

6-
print("Not implemented yet.")
8+
quit(error_code)

tests/loop2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from sys import exit
2+
13
def test_for():
24
i: i32
35
for i in range(0, 10):
@@ -7,4 +9,4 @@ def test_for():
79
break
810
if i == 3:
911
quit()
10-
exit()
12+
exit(0)

tests/reference/asr-loop2-e874469.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "asr-loop2-e874469",
33
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
44
"infile": "tests/loop2.py",
5-
"infile_hash": "8bc96fe7a628ac6571893b80a53dc45785f08e770c335319ad3e2cbc",
5+
"infile_hash": "0f1af9370fbae5de0ed9e612e933d7745598fc9c74b036496f4ae8ca",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "asr-loop2-e874469.stdout",
9-
"stdout_hash": "5c7a6323cbd8137a7b82feb7e6d4f0dfb976c28229af3e2244a2597c",
9+
"stdout_hash": "d1dc630006e0a7a21748d0a8bd26f3c99b277d5c208ed6d8077ac9e9",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(TranslationUnit (SymbolTable 1 {main_program: (Program (SymbolTable 3 {}) main_program [] []), test_for: (Subroutine (SymbolTable 2 {i: (Variable 2 i Local () () Default (Integer 4 []) Source Public Required .false.)}) test_for [] [(DoLoop ((Var 2 i) (ConstantInteger 0 (Integer 4 [])) (BinOp (ConstantInteger 10 (Integer 4 [])) Sub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger 9 (Integer 4 [])) ()) (ConstantInteger 1 (Integer 4 []))) [(If (Compare (Var 2 i) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) () ()) [(Cycle)] []) (If (Compare (Var 2 i) Gt (ConstantInteger 5 (Integer 4 [])) (Logical 4 []) () ()) [(Exit)] []) (If (Compare (Var 2 i) Eq (ConstantInteger 3 (Integer 4 [])) (Logical 4 []) () ()) [(Stop ())] [])]) (Stop ())] Source Public Implementation () .false. .false.)}) [])
1+
(TranslationUnit (SymbolTable 1 {exit: (ExternalSymbol 1 exit 3 exit sys [] exit Public), main_program: (Program (SymbolTable 6 {}) main_program [] []), sys: (Module (SymbolTable 3 {exit: (Subroutine (SymbolTable 4 {error_code: (Variable 4 error_code In () () Default (Integer 4 []) Source Public Required .false.)}) exit [(Var 4 error_code)] [(Stop (Var 4 error_code))] Source Public Implementation () .false. .false.)}) sys [] .false.), test_for: (Subroutine (SymbolTable 5 {i: (Variable 5 i Local () () Default (Integer 4 []) Source Public Required .false.)}) test_for [] [(DoLoop ((Var 5 i) (ConstantInteger 0 (Integer 4 [])) (BinOp (ConstantInteger 10 (Integer 4 [])) Sub (ConstantInteger 1 (Integer 4 [])) (Integer 4 []) (ConstantInteger 9 (Integer 4 [])) ()) (ConstantInteger 1 (Integer 4 []))) [(If (Compare (Var 5 i) Eq (ConstantInteger 0 (Integer 4 [])) (Logical 4 []) () ()) [(Cycle)] []) (If (Compare (Var 5 i) Gt (ConstantInteger 5 (Integer 4 [])) (Logical 4 []) () ()) [(Exit)] []) (If (Compare (Var 5 i) Eq (ConstantInteger 3 (Integer 4 [])) (Logical 4 []) () ()) [(Stop ())] [])]) (SubroutineCall 1 exit () [(ConstantInteger 0 (Integer 4 []))] ())] Source Public Implementation () .false. .false.)}) [])

tests/reference/ast-loop2-63bf329.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "ast-loop2-63bf329",
33
"cmd": "lpython --show-ast --no-color {infile} -o {outfile}",
44
"infile": "tests/loop2.py",
5-
"infile_hash": "8bc96fe7a628ac6571893b80a53dc45785f08e770c335319ad3e2cbc",
5+
"infile_hash": "0f1af9370fbae5de0ed9e612e933d7745598fc9c74b036496f4ae8ca",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "ast-loop2-63bf329.stdout",
9-
"stdout_hash": "087c70fedf810add24d5c170994e52aee61ff6e6e91f0dabfe4f49ce",
9+
"stdout_hash": "a5cd7c1a9f0bdb94b4ae8a8ec509111aede5651ea109408828ce40c8",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(Module [(FunctionDef test_for ([] [] [] [] [] [] []) [(AnnAssign (Name i Store) (Name i32 Load) () 1) (For (Name i Store) (Call (Name range Load) [(ConstantInt 0 ()) (ConstantInt 10 ())] []) [(If (Compare (Name i Load) Eq [(ConstantInt 0 ())]) [(Continue)] []) (If (Compare (Name i Load) Gt [(ConstantInt 5 ())]) [(Break)] []) (If (Compare (Name i Load) Eq [(ConstantInt 3 ())]) [(Expr (Call (Name quit Load) [] []))] [])] [] ()) (Expr (Call (Name exit Load) [] []))] [] () ())] [])
1+
(Module [(ImportFrom sys [(exit ())] 0) (FunctionDef test_for ([] [] [] [] [] [] []) [(AnnAssign (Name i Store) (Name i32 Load) () 1) (For (Name i Store) (Call (Name range Load) [(ConstantInt 0 ()) (ConstantInt 10 ())] []) [(If (Compare (Name i Load) Eq [(ConstantInt 0 ())]) [(Continue)] []) (If (Compare (Name i Load) Gt [(ConstantInt 5 ())]) [(Break)] []) (If (Compare (Name i Load) Eq [(ConstantInt 3 ())]) [(Expr (Call (Name quit Load) [] []))] [])] [] ()) (Expr (Call (Name exit Load) [(ConstantInt 0 ())] []))] [] () ())] [])

tests/reference/cpp-loop2-0686fc4.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"basename": "cpp-loop2-0686fc4",
33
"cmd": "lpython --no-color --show-cpp {infile}",
44
"infile": "tests/loop2.py",
5-
"infile_hash": "8bc96fe7a628ac6571893b80a53dc45785f08e770c335319ad3e2cbc",
5+
"infile_hash": "0f1af9370fbae5de0ed9e612e933d7745598fc9c74b036496f4ae8ca",
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "cpp-loop2-0686fc4.stdout",
9-
"stdout_hash": "1b3ba76abf9c0f99b17c6b8293a0b73a0a6765c7716654832212aa9e",
9+
"stdout_hash": "eef4f1ff5b5b8880a2e873cd82808099e1f3612117a10667599f5d62",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/cpp-loop2-0686fc4.stdout

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ void test_for()
3333
exit(0);
3434
}
3535

36+
void exit(int error_code)
37+
{
38+
exit(0);
39+
}
40+
3641
namespace {
3742

3843
void main2() {

0 commit comments

Comments
 (0)