📜 ⬆️ ⬇️

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 code Python or open github .
Here is its source:

Cheesecakes
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:

image

As you can 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 antigravity.py

Its contents are:

antigravity
 import webbrowser import hashlib webbrowser.open("https://xkcd.com/353/") def geohash(latitude, longitude, datedow): '''Compute geohash() using the Munroe algorithm. >>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68') 37.857713 -122.544543 ''' # https://xkcd.com/426/ 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 the page in it
with comic about python.

https://xkcd.com/353/

By the way, in antigravity, as you can see, there is also a function geohash, which is not hard to guess
generates a hash of 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" //    static int future_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); const char *feature = PyUnicode_AsUTF8(name->name); if (!feature) return 0; //   ,   if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) { continue; } else if (strcmp(feature, FUTURE_GENERATORS) == 0) { continue; //  ,    } //             //    } else if (strcmp(feature, FUTURE_BARRY_AS_BDFL) == 0) { ff->ff_features |= CO_FUTURE_BARRY_AS_BDFL; } else if (strcmp(feature, FUTURE_GENERATOR_STOP) == 0) { continue; } else if (strcmp(feature, FUTURE_ANNOTATIONS) == 0) { ff->ff_features |= CO_FUTURE_ANNOTATIONS; } else if (strcmp(feature, "braces") == 0) { PyErr_SetString(PyExc_SyntaxError, "not a chance"); //    ,     PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1); return 0; } else { PyErr_Format(PyExc_SyntaxError, UNDEFINED_FUTURE_FEATURE, feature); //-  ,   PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1); return 0; } } return 1; } //     if (s->kind == ImportFrom_kind) { identifier modname = s->v.ImportFrom.module; if (modname && _PyUnicode_EqualToASCIIString(modname, "__future__")) { //   if (done) { PyErr_SetString(PyExc_SyntaxError, ERR_LATE_FUTURE); //           PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset); return 0; } if (!future_check_features(ff, s, filename)) return 0; ff->ff_lineno = s->lineno; } else { done = 1; } } else { done = 1; } } return 1; } 


Well, that seems to be all :-)

But at the end of the meme pro 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.

Source: https://habr.com/ru/post/430308/


All Articles