Python internals. Easter eggs

    Hello! On Habré there were quite a lot of articles about the python easter eggs, but it seems that nowhere was it mentioned about how it all works from the inside.

    I think that it will be interesting, first of all, for novice pythonists. This is what will be discussed under the cut!

    What is the easter egg python first comes to mind?

    Of course, Zen of Python (import this)

    Go to the folder with the source of Python or open github .
    Here is its source:

    s = """Gur Mra bs Clguba, ol Gvz Crgref
    Ornhgvshy vf orggre guna htyl.
    Rkcyvpvg vf orggre guna vzcyvpvg.
    Fvzcyr vf orggre guna pbzcyrk.
    Pbzcyrk vf orggre guna pbzcyvpngrq.
    Syng vf orggre guna arfgrq.
    Fcnefr vf orggre guna qrafr.
    Ernqnovyvgl pbhagf.
    Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
    Nygubhtu cenpgvpnyvgl orngf chevgl.
    Reebef fubhyq arire cnff fvyragyl.
    Hayrff rkcyvpvgyl fvyraprq.
    Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
    Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
    Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
    Abj vf orggre guna arire.
    Nygubhtu arire vf bsgra orggre guna *evtug* abj.
    Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
    Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
    Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""
    d = {}
    for c in (65, 97):
        for i in range(26):
            d[chr(i+c)] = chr((i+13) % 26 + c)
    print("".join([d.get(c, c) for c in s]))

    Well, how? Some kind of perl porridge ... Why was it necessary to bother with getting a character by code? Unclear.

    Go ahead. We are looking for antigravity on githaba. We find this:


    As we see, there is no module.

    What does it mean? It's time to try the configure script, perhaps it will generate the desired file. Run ./configure in the Python source folder.

    Check the Lib folder and find there.

    Its contents are:

    import webbrowser
    import hashlib"")
    defgeohash(latitude, longitude, datedow):'''Compute geohash() using the Munroe algorithm.
        >>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
        37.857713 -122.544543
        h = hashlib.md5(datedow).hexdigest()
        p, q = [('%f' % float.fromhex('0.' + x)) for x in (h[:16], h[16:32])]
        print('%d%s %d%s' % (latitude, p[1:], longitude, q[1:]))

    When importing, we see how our default browser opens and there is a page
    with a comic about python in it.

    By the way, as you see in antigravity, there is also a geohash function, which, as it is easy to guess,
    generates a hash from latitude, longitude and date.

    What else do we have? That's right, __future__ module. A separate c file in the source / Python folder is already selected for it. If someone did not know when you are writing from __future__ import feature
    you do not import this feature, but tell the interpreter to use it. For this reason, this instruction should be at the beginning of the file. Here are the most interesting places in the code of the module on C, I do not see the whole point of citing.

    C code
    //сначала куча нужных инклудов#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"#define ERR_LATE_FUTURE \
    "from __future__ imports must occur at the beginning of the file"//определяем сообщения об ошибкахstaticintfuture_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename){
        int i;
        asdl_seq *names;
        assert(s->kind == ImportFrom_kind);
        names = s->v.ImportFrom.names;
        for (i = 0; i < asdl_seq_LEN(names); i++) {
            alias_ty name = (alias_ty)asdl_seq_GET(names, i);
            constchar *feature = PyUnicode_AsUTF8(name->name);
            if (!feature)
                return0; //если не нашли фичу, то выходимif (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
            } elseif (strcmp(feature, FUTURE_GENERATORS) == 0) {
                continue; //ищим поддержку генераторов, если есть продолжаем
           //дальше идет список фич и везде проверка выглядит как в случае с генераторами//только названия фич другое
            } elseif (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) {
                ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL;
            } elseif (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) {
            } elseif (strcmp(feature, FUTURE_ANNOTATIONS) == 0) {
                ff->ff_features |= CO_FUTURE_ANNOTATIONS;
            } elseif (strcmp(feature, "braces") == 0) {
                                "not a chance");
               //а если импортируем фигурные скобки,то питон выбросит такую ошибку
                PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
            } else {
                             UNDEFINED_FUTURE_FEATURE, feature);
                //какая-то непонятная фича, выбрасываем ошибку
                PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
           //вырезанный не очень интересный кодif (s->kind == ImportFrom_kind) {
                identifier modname = s->v.ImportFrom.module;
                if (modname &&
                    _PyUnicode_EqualToASCIIString(modname, "__future__")) {
                   //задаем название модуляif (done) {
                      //а вот здесь выбрасываем ошибку если импортируем не в начале файла
                        PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset);
                    if (!future_check_features(ff, s, filename))
                    ff->ff_lineno = s->lineno;
                else {
                    done = 1;
            else {
                done = 1;

    Well, it seems that all :-)

    But at the end of the meme about the python, this is positive!

    PS: I hope none of the coders on the pearl is not offended, it has a lot of cool written on it.

    Also popular now: