简体   繁体   English

Python启动横幅在哪里定义?

[英]Where is the Python startup banner defined?

I'm compiling several different versions of Python for my system, and I'd like to know where in the source the startup banner is defined so I can change it for each version. 我正在为我的系统编译几个不同版本的Python,我想知道源在哪里定义了启动横幅,所以我可以为每个版本更改它。 For example, when the interpreter starts it displays 例如,当解释器启动时,它会显示

Python 3.3.1 (default, Apr 28 2013, 10:19:42) 
[GCC 4.7.2 20121109 (Red Hat 4.7.2-8)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

I'd like to change the string default to other things to signal which version I'm using, but I'm also interested in how the whole shebang is assembled. 我想把字符串default改为其他东西来表示我正在使用哪个版本,但我也对整个shebang是如何组装感兴趣。 Where is this defined? 这定义在哪里?

Let's use grep to get in the ballpark. 让我们用grep进入球场。 I'm not going to bother searching for default because I'll get too many results, but I'll try Type "Help" , which should not appear too many times. 我不打算搜索default因为我会得到太多结果,但我会尝试Type "Help" ,这不应该出现太多次。 If it's a C string, the quotes will be escaped. 如果它是C字符串,则引号将被转义。 We should look for C strings first and Python strings later. 我们应该首先查找C字符串,然后查找Python字符串。

Python $ grep 'Type \\"help\\"' . -Ir
./Modules/main.c:    "Type \"help\", \"copyright\", \"credits\" or \"license\" " \

It's in Modules/main.c , in Py_Main() . 它位于Py_Main()中的Modules/main.c中。 More digging gives us this line: 更多的挖掘为我们提供了这条线:

fprintf(stderr, "Python %s on %s\n",
    Py_GetVersion(), Py_GetPlatform());

Because "on" is in the format string, Py_GetPlatform() must be linux and Py_GetVersion() must give the string we want... 因为“on”是格式字符串, Py_GetPlatform()必须是linuxPy_GetVersion()必须给出我们想要的字符串...

Python $ grep Py_GetVersion . -Irl
...
./Python/getversion.c
...

That looks promising... 看起来很有希望......

PyOS_snprintf(version, sizeof(version), "%.80s (%.80s) %.80s",
              PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler());

We must want Py_GetBuildInfo() , because it's inside the parentheses... 我们必须要Py_GetBuildInfo() ,因为它在括号内...

Python $ grep Py_GetBuildInfo . -Irl
...
./Modules/getbuildinfo.c
...

That looks a little too obvious. 这看起来有点太明显了。

const char *
Py_GetBuildInfo(void)
{
    static char buildinfo[50 + sizeof(HGVERSION) +
                          ((sizeof(HGTAG) > sizeof(HGBRANCH)) ?
                           sizeof(HGTAG) : sizeof(HGBRANCH))];
    const char *revision = _Py_hgversion();
    const char *sep = *revision ? ":" : "";
    const char *hgid = _Py_hgidentifier();
    if (!(*hgid))
        hgid = "default";
    PyOS_snprintf(buildinfo, sizeof(buildinfo),
                  "%s%s%s, %.20s, %.9s", hgid, sep, revision,
                  DATE, TIME);
    return buildinfo;
}

So, default is the name of the Mercurial branch. 因此, default是Mercurial分支的名称。 By examining the makefiles, we can figure out that this comes from the macro HGTAG . 通过检查makefile,我们可以发现它来自宏HGTAG A makefile variable named HGTAG produces the variable, and that variable is run as a command. 名为HGTAG的makefile变量生成变量,该变量作为命令运行。 So, 所以,

Simple solution 简单解决方案

When building Python, 在构建Python时,

Python $ ./configure
Python $ make HGTAG='echo awesome'
Python $ ./python
Python 3.2.3 (awesome, May  1 2013, 21:33:27) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Looks like if you add a mercurial tag before you build, then default will be replaced with the name of your tag (source: Modules/getbuildinfo.c : _Py_hgidentifier() ) 看起来如果在构建之前添加mercurial标记,则default将替换为标记的名称(source: Modules/getbuildinfo.c_Py_hgidentifier()

Basically seems like it chooses the name default because that is the name of the branch. 基本上它似乎选择名称default因为这是分支的名称。 Looks like the interpreter is built with the tag name, if one exists, or the name of the branch if no tag (besides tip ) exists on the current working copy. 看起来解释器是使用标记名称(如果存在)构建的,或者如果当前工作副本上没有标记(除了tip )之外的分支名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM