gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:24:07 25 4
gpt4 key购买 nike

“检查”和“交互”标志有什么区别?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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com