Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. Where is this defined?

like image 656
MattDMo Avatar asked May 02 '13 03:05

MattDMo


Video Answer


1 Answers

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.
>>> 
like image 138
Dietrich Epp Avatar answered Sep 21 '22 03:09

Dietrich Epp