6

“检查”和“交互式”标志有什么区别?sys.flags函数打印它们。

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

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

有关的:

4

2 回答 2

9

根据pythonrun.c对应Py_InspectFlagPy_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 */
}

SystemExit如果“检查”标志为真,Python 不会退出。

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);
}

如果“交互”标志为假并且当前输入未与终端关联,则 python 不会打扰进入“交互”模式(取消缓冲标准输出、打印版本、显示提示等)。

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

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

于 2009-07-17T21:28:21.637 回答
0

man python谈到-i国旗:

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

因此-i允许在交互模式下检查脚本。暗示了这两件事。您可以在不检查的情况下进行交互(即只调用,不带参数),但反之则不行。-i python

于 2009-07-17T20:14:52.130 回答