Skip to content
Closed
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
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ New Modules
Improved Modules
================

builtins
--------

* In the interactive REPL, typing "exit" or "quit" will directly exit the
interpreter session instead of printing a message suggesting to use "exit()" or
"quit()" instead. Contributed by Pablo Galindo in :issue:`44603`.


fractions
---------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In the interactive REPL, typing "exit" or "quit" will directly exit the
interpreter session instead of printing a message suggesting to use "exit()"
or "quit()" instead. Patch by Pablo Galindo
27 changes: 27 additions & 0 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,25 @@ PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flag

}

static int
PyRun_IsInteractiveExitCommand(mod_ty mod) {
if (asdl_seq_LEN(mod->v.Module.body) != 1) {
return 0;
}
stmt_ty statement = asdl_seq_GET(mod->v.Module.body, 0);
if (statement->kind != Expr_kind) {
return 0;
}
expr_ty expr = statement->v.Expr.value;
if (expr->kind != Name_kind) {
return 0;
}
if (expr->v.Name.ctx != Load) {
return 0;
}
return PyUnicode_CompareWithASCIIString(expr->v.Name.id, "exit") == 0 ||
PyUnicode_CompareWithASCIIString(expr->v.Name.id, "quit") == 0;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests of the object associated with the names is needed to not turn them into semi-keywords, as in the the following.

>>> exit = 3
>>> exit

f:\dev\3x>


/* A PyRun_InteractiveOneObject() auxiliary function that does not print the
* error on failure. */
Expand Down Expand Up @@ -267,6 +286,14 @@ PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
}
return -1;
}

if (PyRun_IsInteractiveExitCommand(mod)) {
Py_INCREF(Py_None);
PyErr_SetObject(PyExc_SystemExit, Py_None);
_PyArena_Free(arena);
return -1;
}

m = PyImport_AddModuleObject(mod_name);
if (m == NULL) {
_PyArena_Free(arena);
Expand Down