Where is the Python startup banner defined?

后端 未结 2 562
小蘑菇
小蘑菇 2021-01-05 19:10

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 versio

2条回答
  •  情书的邮戳
    2021-01-05 19:39

    Let's use grep to get in the ballpark. 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. If it's a C string, the quotes will be escaped. We should look for C strings first and Python strings later.

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

    It's in Modules/main.c, in Py_Main(). 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...

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

    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. By examining the makefiles, we can figure out that this comes from the macro HGTAG. A makefile variable named HGTAG produces the variable, and that variable is run as a command. So,

    Simple solution

    When building 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.
    >>> 
    

提交回复
热议问题