python - Python 中 "inspect"和 "interactive"命令行标志的区别

标签 python command-line interpreter

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

根据 sys.flags 的文档,它们怎么会有“-i”标志?

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

相关:

最佳答案

根据 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 */
}

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

如果“交互”标志为 false 且当前输入与终端无关,则 python 不会费心进入“交互”模式(无缓冲标准输出、打印版本、显示提示等)。

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

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

关于python - Python 中 "inspect"和 "interactive"命令行标志的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1145428/

相关文章:

Java 脚本解释器

python - 如何使用过滤器获取python数据框列名

c++ - 创建命令并通过 Linux 终端执行它们

python - PuLP 编程输出

command-line - Zsh:从下划线更改行编辑光标?

java 检查 jar 是否从命令行或资源管理器启动

解释器中的 Python 解释器

embedded - 是否有可用的 FreeRTOS 解释语言库?

python - 如何从 jupyter 笔记本中删除停用的 conda 环境名称?

python - Django REST 框架 : Customize data dict on JSONRenderer