Python中“inspect”和“interactive”命令行标志之间的区别

时间:2021-09-03 23:49:13

What is the difference between "inspect" and "interactive" flags? The sys.flags function prints both of them.

“inspect”和“interactive”标志有什么区别? sys.flags函数打印它们。

How can they both have "-i" flag according to the documentation of sys.flags?

根据sys.flags的文档,它们如何都有“-i”标志?

How can I set them separately? If I use "python -i", both of them will be set to 1.

我该如何单独设置它们?如果我使用“python -i”,它们都将设置为1。

Related:

2 个解决方案

#1


According to pythonrun.c corresponding Py_InspectFlag and Py_InteractiveFlag are used as follows:

根据pythonrun.c对应的Py_InspectFlag和Py_InteractiveFlag使用如下:

int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
/* snip */
static void
handle_system_exit(void)
{
    PyObject *exception, *value, *tb;
    int exitcode = 0;

    if (Py_InspectFlag)
        /* Don't exit if -i flag was given. This flag is set to 0
         * when entering interactive mode for inspecting. */
        return;
    /* snip */
}

Python doesn't exit on SystemExit if "inspect" flag is true.

如果“inspect”标志为true,Python不会在SystemExit上退出。

int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
/* snip */
/*
 * The file descriptor fd is considered ``interactive'' if either
 *   a) isatty(fd) is TRUE, or
 *   b) the -i flag was given, and the filename associated with
 *      the descriptor is NULL or "<stdin>" or "???".
 */
int
Py_FdIsInteractive(FILE *fp, const char *filename)
{
    if (isatty((int)fileno(fp)))
        return 1;
    if (!Py_InteractiveFlag)
        return 0;
    return (filename == NULL) ||
           (strcmp(filename, "<stdin>") == 0) ||
           (strcmp(filename, "???") == 0);
}

If "interactive" flag is false and current input is not associated with a terminal then python doesn't bother entering "interactive" mode (unbuffering stdout, printing version, showing prompt, etc).

如果“interactive”标志为false并且当前输入未与终端关联,则python不会打扰进入“交互”模式(unbuffering stdout,打印版本,显示提示等)。

-i option turns on both flags. "inspect" flag is also on if PYTHONINSPECT environment variable is not empty (see main.c).

-i选项打开两个标志。如果PYTHONINSPECT环境变量不为空,则“inspect”标志也会打开(请参阅main.c)。

Basically it means if you set PYTHONINSPECT variable and run your module then python doesn't exit on SystemExit (e.g., at the end of the script) and shows you an interactive prompt instead of (allowing you to inspect your module state (thus "inspect" name for the flag)).

基本上它意味着如果您设置PYTHONINSPECT变量并运行您的模块然后python不会退出SystemExit(例如,在脚本的末尾)并显示交互式提示而不是(允许您检查您的模块状态(因此“检查”) “国旗的名称))。

#2


man python says about the -i flag:

man python说关于-i标志:

When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a script raises an exception.

当脚本作为第一个参数传递或使用-c选项时,请在执行脚本或命令后进入交互模式。它不会读取$ PYTHONSTARTUP文件。当脚本引发异常时,这对于检查全局变量或堆栈跟踪非常有用。

Hence -i allows inspection of a script in interactive mode. -i implies both of these things. You can be interactive without inspecting (namely by just calling python, without arguments), but not vice versa.

因此-i允许以交互模式检查脚本。 -i意味着这两件事。你可以在没有检查的情况下进行交互(即只调用python,不带参数),但反之亦然。

#1


According to pythonrun.c corresponding Py_InspectFlag and Py_InteractiveFlag are used as follows:

根据pythonrun.c对应的Py_InspectFlag和Py_InteractiveFlag使用如下:

int Py_InspectFlag; /* Needed to determine whether to exit at SystemError */
/* snip */
static void
handle_system_exit(void)
{
    PyObject *exception, *value, *tb;
    int exitcode = 0;

    if (Py_InspectFlag)
        /* Don't exit if -i flag was given. This flag is set to 0
         * when entering interactive mode for inspecting. */
        return;
    /* snip */
}

Python doesn't exit on SystemExit if "inspect" flag is true.

如果“inspect”标志为true,Python不会在SystemExit上退出。

int Py_InteractiveFlag; /* Needed by Py_FdIsInteractive() below */
/* snip */
/*
 * The file descriptor fd is considered ``interactive'' if either
 *   a) isatty(fd) is TRUE, or
 *   b) the -i flag was given, and the filename associated with
 *      the descriptor is NULL or "<stdin>" or "???".
 */
int
Py_FdIsInteractive(FILE *fp, const char *filename)
{
    if (isatty((int)fileno(fp)))
        return 1;
    if (!Py_InteractiveFlag)
        return 0;
    return (filename == NULL) ||
           (strcmp(filename, "<stdin>") == 0) ||
           (strcmp(filename, "???") == 0);
}

If "interactive" flag is false and current input is not associated with a terminal then python doesn't bother entering "interactive" mode (unbuffering stdout, printing version, showing prompt, etc).

如果“interactive”标志为false并且当前输入未与终端关联,则python不会打扰进入“交互”模式(unbuffering stdout,打印版本,显示提示等)。

-i option turns on both flags. "inspect" flag is also on if PYTHONINSPECT environment variable is not empty (see main.c).

-i选项打开两个标志。如果PYTHONINSPECT环境变量不为空,则“inspect”标志也会打开(请参阅main.c)。

Basically it means if you set PYTHONINSPECT variable and run your module then python doesn't exit on SystemExit (e.g., at the end of the script) and shows you an interactive prompt instead of (allowing you to inspect your module state (thus "inspect" name for the flag)).

基本上它意味着如果您设置PYTHONINSPECT变量并运行您的模块然后python不会退出SystemExit(例如,在脚本的末尾)并显示交互式提示而不是(允许您检查您的模块状态(因此“检查”) “国旗的名称))。

#2


man python says about the -i flag:

man python说关于-i标志:

When a script is passed as first argument or the -c option is used, enter interactive mode after executing the script or the command. It does not read the $PYTHONSTARTUP file. This can be useful to inspect global variables or a stack trace when a script raises an exception.

当脚本作为第一个参数传递或使用-c选项时,请在执行脚本或命令后进入交互模式。它不会读取$ PYTHONSTARTUP文件。当脚本引发异常时,这对于检查全局变量或堆栈跟踪非常有用。

Hence -i allows inspection of a script in interactive mode. -i implies both of these things. You can be interactive without inspecting (namely by just calling python, without arguments), but not vice versa.

因此-i允许以交互模式检查脚本。 -i意味着这两件事。你可以在没有检查的情况下进行交互(即只调用python,不带参数),但反之亦然。