Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quit and Exit #523

Merged
merged 2 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions mathics/builtin/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,34 +1012,6 @@ def apply(self, expr, evaluation):
return Symbol('Null')


class Quit(Builtin):
"""
<dl>
<dt>'Quit'[]
<dd>removes all user-defined definitions.
</dl>

>> a = 3
= 3
>> Quit[]
>> a
= a

'Quit' even removes the definitions of protected and locked symbols:
>> x = 5;
>> Attributes[x] = {Locked, Protected};
>> Quit[]
>> x
= x
"""

def apply(self, evaluation):
'Quit[]'

evaluation.definitions.set_user_definitions({})
return Symbol('Null')


def get_symbol_values(symbol, func_name, position, evaluation):
name = symbol.get_name()
if not name:
Expand Down
39 changes: 39 additions & 0 deletions mathics/builtin/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,42 @@ class Out(Builtin):
' f:StandardForm|TraditionalForm|InputForm|OutputForm]':
r'"%%" <> ToString[k]',
}


class Exit(Builtin):
'''
<dl>
<dt>'Exit[]'
<dd>terminates the Mathics session.
<dt>'Exit[n]'
<dd>terminates with exit code $n$.
</dl>

Exit is an alias for Quit.
'''

def apply(self, evaluation):
'Exit[]'
sys.exit()

def apply_n(self, n, evaluation):
'Exit[n_Integer]'
sys.exit(n.get_int_value())


class Quit(Builtin):
'''
<dl>
<dt>'Quit[]'
<dd>terminates the Mathics session.
<dt>'Quit[n]'
<dd>terminates with exit code $n$.
</dl>

Quit is an alias for Exit.
'''

rules = {
'Quit[n_Integer]': 'Exit[n]',
'Quit[]': 'Exit[]',
}
6 changes: 5 additions & 1 deletion mathics/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,13 @@ def main():
shell.print_result(result)
except (KeyboardInterrupt):
print('\nKeyboardInterrupt')
except (SystemExit, EOFError):
except EOFError:
print("\n\nGoodbye!\n")
break
except SystemExit:
print("\n\nGoodbye!\n")
# raise to pass the error code on, e.g. Quit[1]
raise
finally:
shell.reset_lineno()

Expand Down