Python中“检查”和“交互”命令行标志之间的区别

发布于 2021-01-29 16:35:31

“检查”和“交互”标志之间有什么区别?该sys.flags功能两者的打印。

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

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

有关:

关注者
0
被浏览
52
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    根据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如果“检查”标志为true,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);
    }
    

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

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

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看