“检查”和“交互式” Python中的命令行标志 [英] Difference between "inspect" and "interactive" command line flags in Python

查看:241
本文介绍了“检查”和“交互式” Python中的命令行标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

inspect和interactive标志有什么区别?
sys.flags函数打印这两个函数。

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

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

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

我可以单独设置吗?如果我使用python -i,他们都将
设置为1。

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

  • tell whether python is in -i mode

推荐答案

根据 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不退出 SystemExit 如果inspect标志为真。

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

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标志为假,且当前输入未与终端关联那么python不会进入交互模式(不缓冲stdout,打印版本,显示提示等)。

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).

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

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

基本上,这意味着如果你设置了<$ c的模块/ main.c?view = markuprel =nofollow noreferrer $ c> PYTHONINSPECT 变量并运行你的模块,然后python不退出SystemExit(例如,在脚本的结尾),并显示一个交互式提示,而不是(允许您检查模块状态(因此检查标志的名称))。

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)).

这篇关于“检查”和“交互式” Python中的命令行标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆