Skip to content

Commit f0a15ac

Browse files
author
georg.brandl
committed
Variant of patch #697613: don't exit the interpreter on a SystemExit
exception if the -i command line option or PYTHONINSPECT environment variable is given, but break into the interactive interpreter just like on other exceptions or normal program exit. (backport) git-svn-id: http://svn.python.org/projects/python/trunk@54188 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent b3c8e8c commit f0a15ac

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

Include/pydebug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
PyAPI_DATA(int) Py_DebugFlag;
99
PyAPI_DATA(int) Py_VerboseFlag;
1010
PyAPI_DATA(int) Py_InteractiveFlag;
11+
PyAPI_DATA(int) Py_InspectFlag;
1112
PyAPI_DATA(int) Py_OptimizeFlag;
1213
PyAPI_DATA(int) Py_NoSiteFlag;
1314
PyAPI_DATA(int) Py_UseClassExceptionsFlag;

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ What's New in Python 2.6 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Variant of patch #697613: don't exit the interpreter on a SystemExit
16+
exception if the -i command line option or PYTHONINSPECT environment
17+
variable is given, but break into the interactive interpreter just like
18+
on other exceptions or normal program exit.
19+
1520
- Patch #1638879: don't accept strings with embedded NUL bytes in long().
1621

1722
- Bug #1674503: close the file opened by execfile() in an error condition.

Modules/main.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,11 @@ Py_Main(int argc, char **argv)
216216
char *module = NULL;
217217
FILE *fp = stdin;
218218
char *p;
219-
int inspect = 0;
220219
int unbuffered = 0;
221220
int skipfirstline = 0;
222221
int stdin_is_interactive = 0;
223222
int help = 0;
224223
int version = 0;
225-
int saw_inspect_flag = 0;
226224
int saw_unbuffered_flag = 0;
227225
PyCompilerFlags cf;
228226

@@ -297,8 +295,7 @@ Py_Main(int argc, char **argv)
297295
/* NOTREACHED */
298296

299297
case 'i':
300-
inspect++;
301-
saw_inspect_flag = 1;
298+
Py_InspectFlag++;
302299
Py_InteractiveFlag++;
303300
break;
304301

@@ -369,9 +366,9 @@ Py_Main(int argc, char **argv)
369366
return 0;
370367
}
371368

372-
if (!saw_inspect_flag &&
369+
if (!Py_InspectFlag &&
373370
(p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
374-
inspect = 1;
371+
Py_InspectFlag = 1;
375372
if (!saw_unbuffered_flag &&
376373
(p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0')
377374
unbuffered = 1;
@@ -499,7 +496,7 @@ Py_Main(int argc, char **argv)
499496

500497
PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);
501498

502-
if ((inspect || (command == NULL && filename == NULL && module == NULL)) &&
499+
if ((Py_InspectFlag || (command == NULL && filename == NULL && module == NULL)) &&
503500
isatty(fileno(stdin))) {
504501
PyObject *v;
505502
v = PyImport_ImportModule("readline");
@@ -518,6 +515,7 @@ Py_Main(int argc, char **argv)
518515
}
519516
else {
520517
if (filename == NULL && stdin_is_interactive) {
518+
Py_InspectFlag = 0; /* do exit on SystemExit */
521519
RunStartupFile(&cf);
522520
}
523521
/* XXX */
@@ -530,16 +528,18 @@ Py_Main(int argc, char **argv)
530528
/* Check this environment variable at the end, to give programs the
531529
* opportunity to set it from Python.
532530
*/
533-
if (!saw_inspect_flag &&
531+
if (!Py_InspectFlag &&
534532
(p = Py_GETENV("PYTHONINSPECT")) && *p != '\0')
535533
{
536-
inspect = 1;
534+
Py_InspectFlag = 1;
537535
}
538536

539-
if (inspect && stdin_is_interactive &&
540-
(filename != NULL || command != NULL || module != NULL))
537+
if (Py_InspectFlag && stdin_is_interactive &&
538+
(filename != NULL || command != NULL || module != NULL)) {
539+
Py_InspectFlag = 0;
541540
/* XXX */
542541
sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
542+
}
543543

544544
WaitForThreadShutdown();
545545

Python/pythonrun.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ extern void _PyGILState_Fini(void);
6969
int Py_DebugFlag; /* Needed by parser.c */
7070
int Py_VerboseFlag; /* Needed by import.c */
7171
int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
72+
int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
7273
int Py_NoSiteFlag; /* Suppress 'import site' */
7374
int Py_UseClassExceptionsFlag = 1; /* Needed by bltinmodule.c: deprecated */
7475
int Py_FrozenFlag; /* Needed by getpath.c */
@@ -1019,6 +1020,11 @@ handle_system_exit(void)
10191020
PyObject *exception, *value, *tb;
10201021
int exitcode = 0;
10211022

1023+
if (Py_InspectFlag)
1024+
/* Don't exit if -i flag was given. This flag is set to 0
1025+
* when entering interactive mode for inspecting. */
1026+
return;
1027+
10221028
PyErr_Fetch(&exception, &value, &tb);
10231029
if (Py_FlushLine())
10241030
PyErr_Clear();

0 commit comments

Comments
 (0)