Personal website https://benkurtovic.com/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2014-06-01-obfuscating-hello-world.md 24 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. ---
  2. layout: post
  3. title: Obfuscating "Hello world!"
  4. tags: Python
  5. description: Fun with functional programming in Python
  6. ---
  7. __Update__ (November 1, 2017): Added [Python 3 support](#addendum-python-3-support).
  8. A few months ago, I got first place in
  9. [this Code Golf contest](//codegolf.stackexchange.com/q/22533) to create the
  10. weirdest obfuscated program that prints the string "Hello world!". I decided to
  11. write up an explanation of how the hell it works. So, here's the entry, in
  12. Python 2.7:
  13. {% highlight python linenos=table %}
  14. (lambda _, __, ___, ____, _____, ______, _______, ________:
  15. getattr(
  16. __import__(True.__class__.__name__[_] + [].__class__.__name__[__]),
  17. ().__class__.__eq__.__class__.__name__[:__] +
  18. ().__iter__().__class__.__name__[_____:________]
  19. )(
  20. _, (lambda _, __, ___: _(_, __, ___))(
  21. lambda _, __, ___:
  22. chr(___ % __) + _(_, __, ___ // __) if ___ else
  23. (lambda: _).func_code.co_lnotab,
  24. _ << ________,
  25. (((_____ << ____) + _) << ((___ << _____) - ___)) + (((((___ << __)
  26. - _) << ___) + _) << ((_____ << ____) + (_ << _))) + (((_______ <<
  27. __) - _) << (((((_ << ___) + _)) << ___) + (_ << _))) + (((_______
  28. << ___) + _) << ((_ << ______) + _)) + (((_______ << ____) - _) <<
  29. ((_______ << ___))) + (((_ << ____) - _) << ((((___ << __) + _) <<
  30. __) - _)) - (_______ << ((((___ << __) - _) << __) + _)) + (_______
  31. << (((((_ << ___) + _)) << __))) - ((((((_ << ___) + _)) << __) +
  32. _) << ((((___ << __) + _) << _))) + (((_______ << __) - _) <<
  33. (((((_ << ___) + _)) << _))) + (((___ << ___) + _) << ((_____ <<
  34. _))) + (_____ << ______) + (_ << ___)
  35. )
  36. )
  37. )(
  38. *(lambda _, __, ___: _(_, __, ___))(
  39. (lambda _, __, ___:
  40. [__(___[(lambda: _).func_code.co_nlocals])] +
  41. _(_, __, ___[(lambda _: _).func_code.co_nlocals:]) if ___ else []
  42. ),
  43. lambda _: _.func_code.co_argcount,
  44. (
  45. lambda _: _,
  46. lambda _, __: _,
  47. lambda _, __, ___: _,
  48. lambda _, __, ___, ____: _,
  49. lambda _, __, ___, ____, _____: _,
  50. lambda _, __, ___, ____, _____, ______: _,
  51. lambda _, __, ___, ____, _____, ______, _______: _,
  52. lambda _, __, ___, ____, _____, ______, _______, ________: _
  53. )
  54. )
  55. )
  56. {% endhighlight %}
  57. String literals weren't allowed, but I set some other restrictions for fun: it
  58. had to be a single expression (so no `print` statement) with minimal builtin
  59. usage and no integer literals.
  60. ## Getting started
  61. Since we can't use `print`, we can write to the `stdout` file object:
  62. {% highlight python %}
  63. import sys
  64. sys.stdout.write("Hello world!\n")
  65. {% endhighlight %}
  66. But let's use something lower-level:
  67. [`os.write()`](//docs.python.org/2/library/os.html#os.write). We need
  68. `stdout`'s [file descriptor](//en.wikipedia.org/wiki/File_descriptor), which is
  69. `1` (you can check with `print sys.stdout.fileno()`).
  70. {% highlight python %}
  71. import os
  72. os.write(1, "Hello world!\n")
  73. {% endhighlight %}
  74. We want a single expression, so we'll use
  75. [`__import__()`](//docs.python.org/2/library/functions.html#__import__):
  76. {% highlight python %}
  77. __import__("os").write(1, "Hello world!\n")
  78. {% endhighlight %}
  79. We also want to be able to obfuscate the `write()`, so we'll throw in a
  80. `getattr()`:
  81. {% highlight python %}
  82. getattr(__import__("os"), "write")(1, "Hello world!\n")
  83. {% endhighlight %}
  84. This is the starting point. Everything from now on will be obfuscating the
  85. three strings and the int.
  86. ## Stringing together strings
  87. `"os"` and `"write"` are fairly simple, so we'll create them by joining parts
  88. of the names of various built-in classes. There are many different ways to do
  89. this, but I chose the following:
  90. - `"o"` from the second letter of `bool`: `True.__class__.__name__[1]`
  91. - `"s"` from the third letter of `list`: `[].__class__.__name__[2]`
  92. - `"wr"` from the first two letters of `wrapper_descriptor`, an implementation
  93. detail in CPython found as the type of some builtin classes' methods (more on
  94. that
  95. [here](http://utcc.utoronto.ca/~cks/space/blog/python/SlotWrapperObjects)):
  96. `().__class__.__eq__.__class__.__name__[:2]`
  97. - `"ite"` from the sixth through eighth letters of `tupleiterator`, the type of
  98. object returned by calling `iter()` on a tuple:
  99. `().__iter__().__class__.__name__[5:8]`
  100. We're starting to make some progress!
  101. {% highlight python linenos=table %}
  102. getattr(
  103. __import__(True.__class__.__name__[1] + [].__class__.__name__[2]),
  104. ().__class__.__eq__.__class__.__name__[:2] +
  105. ().__iter__().__class__.__name__[5:8]
  106. )(1, "Hello world!\n")
  107. {% endhighlight %}
  108. `"Hello world!\n"` is more complicated. We're going to encode it as a big
  109. integer, which will be formed of the ASCII code of each character multiplied by
  110. 256 to the power of the character's index in the string. In other words, the
  111. following sum:
  112. <div>$$\sum_{n=0}^{L-1} c_n(256^n)$$</div>
  113. where <span>\\(L\\)</span> is the length of the string and
  114. <span>\\(c_n\\)</span> is the ASCII code of the
  115. <span>\\(n\\)</span><sup>th</sup> character in the string. To create the
  116. number:
  117. {% highlight pycon %}
  118. >>> codes = [ord(c) for c in "Hello world!\n"]
  119. >>> num = sum(codes[i] * 256 ** i for i in xrange(len(codes)))
  120. >>> print num
  121. 802616035175250124568770929992
  122. {% endhighlight %}
  123. Now we need the code to convert this number back into a string. We use a simple
  124. recursive algorithm:
  125. {% highlight pycon %}
  126. >>> def convert(num):
  127. ... if num:
  128. ... return chr(num % 256) + convert(num // 256)
  129. ... else:
  130. ... return ""
  131. ...
  132. >>> convert(802616035175250124568770929992)
  133. 'Hello world!\n'
  134. {% endhighlight %}
  135. Rewriting in one line with `lambda`:
  136. {% highlight python %}
  137. convert = lambda num: chr(num % 256) + convert(num // 256) if num else ""
  138. {% endhighlight %}
  139. Now we use
  140. [anonymous recursion](//en.wikipedia.org/wiki/Anonymous_recursion) to turn this
  141. into a single expression. This requires a
  142. [combinator](//en.wikipedia.org/wiki/Combinatory_logic). Start with this:
  143. {% highlight pycon %}
  144. >>> comb = lambda f, n: f(f, n)
  145. >>> convert = lambda f, n: chr(n % 256) + f(f, n // 256) if n else ""
  146. >>> comb(convert, 802616035175250124568770929992)
  147. 'Hello world!\n'
  148. {% endhighlight %}
  149. Now we just substitute the two definitions into the expression, and we have our
  150. function:
  151. {% highlight pycon %}
  152. >>> (lambda f, n: f(f, n))(
  153. ... lambda f, n: chr(n % 256) + f(f, n // 256) if n else "",
  154. ... 802616035175250124568770929992)
  155. 'Hello world!\n'
  156. {% endhighlight %}
  157. Now we can stick this into our code from before, replacing some variable names
  158. along the way (`f` &rarr; `_`, `n` &rarr; `__`):
  159. {% highlight python linenos=table %}
  160. getattr(
  161. __import__(True.__class__.__name__[1] + [].__class__.__name__[2]),
  162. ().__class__.__eq__.__class__.__name__[:2] +
  163. ().__iter__().__class__.__name__[5:8]
  164. )(
  165. 1, (lambda _, __: _(_, __))(
  166. lambda _, __: chr(__ % 256) + _(_, __ // 256) if __ else "",
  167. 802616035175250124568770929992
  168. )
  169. )
  170. {% endhighlight %}
  171. ## Function internals
  172. We're left with a `""` in the body of our convert function (remember: no string
  173. literals!), and a large number that we'll have to hide somehow. Let's start
  174. with the empty string. We can make one on the fly by examining the internals of
  175. some random function:
  176. {% highlight pycon %}
  177. >>> (lambda: 0).func_code.co_lnotab
  178. ''
  179. {% endhighlight %}
  180. What we're _really_ doing here is looking at the
  181. [line number table](http://svn.python.org/projects/python/branches/pep-0384/Objects/lnotab_notes.txt)
  182. of the `code` object contained within the function. Since it's anonymous, there
  183. are no line numbers, so the string is empty. Replace the `0` with `_` to make
  184. it more confusing (it doesn't matter, since the function's not being called),
  185. and stick it in. We'll also refactor out the `256` into an argument that gets
  186. passed to our obfuscated `convert()` along with the number. This requires
  187. adding an argument to the combinator:
  188. {% highlight python linenos=table %}
  189. getattr(
  190. __import__(True.__class__.__name__[1] + [].__class__.__name__[2]),
  191. ().__class__.__eq__.__class__.__name__[:2] +
  192. ().__iter__().__class__.__name__[5:8]
  193. )(
  194. 1, (lambda _, __, ___: _(_, __, ___))(
  195. lambda _, __, ___:
  196. chr(___ % __) + _(_, __, ___ // __) if ___ else
  197. (lambda: _).func_code.co_lnotab,
  198. 256,
  199. 802616035175250124568770929992
  200. )
  201. )
  202. {% endhighlight %}
  203. ## A detour
  204. Let's tackle a different problem for a bit. We want a way to obfuscate the
  205. numbers in our code, but it'll be cumbersome (and not particularly interesting)
  206. to recreate them each time they're used. If we can implement, say,
  207. `range(1, 9) == [1, 2, 3, 4, 5, 6, 7, 8]`, then we can wrap our current work in
  208. a function that takes variables containing the numbers from 1 to 8, and replace
  209. occurrences of integer literals in the body with these variables:
  210. {% highlight python linenos=table %}
  211. (lambda n1, n2, n3, n4, n5, n6, n7, n8:
  212. getattr(
  213. __import__(True.__class__.__name__[n1] + [].__class__.__name__[n2]),
  214. ...
  215. )(
  216. ...
  217. )
  218. )(*range(1, 9))
  219. {% endhighlight %}
  220. Even though we need to form `256` and `802616035175250124568770929992` as well,
  221. these can be created using arithmetic operations on these eight "fundamental"
  222. numbers. The choice of 1–8 is arbitrary, but seems to be a good middle ground.
  223. We can get the number of arguments a function takes via its `code` object:
  224. {% highlight pycon %}
  225. >>> (lambda a, b, c: 0).func_code.co_argcount
  226. 3
  227. {% endhighlight %}
  228. Build a tuple of functions with argcounts between 1 and 8:
  229. {% highlight python linenos=table %}
  230. funcs = (
  231. lambda _: _,
  232. lambda _, __: _,
  233. lambda _, __, ___: _,
  234. lambda _, __, ___, ____: _,
  235. lambda _, __, ___, ____, _____: _,
  236. lambda _, __, ___, ____, _____, ______: _,
  237. lambda _, __, ___, ____, _____, ______, _______: _,
  238. lambda _, __, ___, ____, _____, ______, _______, ________: _
  239. )
  240. {% endhighlight %}
  241. Using a recursive algorithm, we can turn this into the output of `range(1, 9)`:
  242. {% highlight pycon %}
  243. >>> def convert(L):
  244. ... if L:
  245. ... return [L[0].func_code.co_argcount] + convert(L[1:])
  246. ... else:
  247. ... return []
  248. ...
  249. >>> convert(funcs)
  250. [1, 2, 3, 4, 5, 6, 7, 8]
  251. {% endhighlight %}
  252. As before, we convert this into `lambda` form:
  253. {% highlight python %}
  254. convert = lambda L: [L[0].func_code.co_argcount] + convert(L[1:]) if L else []
  255. {% endhighlight %}
  256. Then, into anonymous-recursive form:
  257. {% highlight pycon %}
  258. >>> (lambda f, L: f(f, L))(
  259. ... lambda f, L: [L[0].func_code.co_argcount] + f(f, L[1:]) if L else [],
  260. ... funcs)
  261. [1, 2, 3, 4, 5, 6, 7, 8]
  262. {% endhighlight %}
  263. For fun, we'll factor out the argcount operation into an additional function
  264. argument, and obfuscate some variable names:
  265. {% highlight python linenos=table %}
  266. (lambda _, __, ___: _(_, __, ___))(
  267. (lambda _, __, ___:
  268. [__(___[0])] + _(_, __, ___[1:]) if ___ else []
  269. ),
  270. lambda _: _.func_code.co_argcount,
  271. funcs
  272. )
  273. {% endhighlight %}
  274. There's a new problem now: we still need a way to hide `0` and `1`. We can get
  275. these by examining the number of local variables within arbitrary functions:
  276. {% highlight pycon %}
  277. >>> (lambda: _).func_code.co_nlocals
  278. 0
  279. >>> (lambda _: _).func_code.co_nlocals
  280. 1
  281. {% endhighlight %}
  282. Even though the function bodies look the same, `_` in the first function is not
  283. an argument, nor is it defined in the function, so Python interprets it as a
  284. global variable:
  285. {% highlight pycon %}
  286. >>> import dis
  287. >>> dis.dis(lambda: _)
  288. 1 0 LOAD_GLOBAL 0 (_)
  289. 3 RETURN_VALUE
  290. >>> dis.dis(lambda _: _)
  291. 1 0 LOAD_FAST 0 (_)
  292. 3 RETURN_VALUE
  293. {% endhighlight %}
  294. This happens regardless of whether `_` is actually defined in the global scope.
  295. Putting this into practice:
  296. {% highlight python linenos=table %}
  297. (lambda _, __, ___: _(_, __, ___))(
  298. (lambda _, __, ___:
  299. [__(___[(lambda: _).func_code.co_nlocals])] +
  300. _(_, __, ___[(lambda _: _).func_code.co_nlocals:]) if ___ else []
  301. ),
  302. lambda _: _.func_code.co_argcount,
  303. funcs
  304. )
  305. {% endhighlight %}
  306. Now we can substitute the value of `funcs` in, and then using `*` to pass the
  307. resulting list of integers as eight separate variables, we get this:
  308. {% highlight python linenos=table %}
  309. (lambda n1, n2, n3, n4, n5, n6, n7, n8:
  310. getattr(
  311. __import__(True.__class__.__name__[n1] + [].__class__.__name__[n2]),
  312. ().__class__.__eq__.__class__.__name__[:n2] +
  313. ().__iter__().__class__.__name__[n5:n8]
  314. )(
  315. n1, (lambda _, __, ___: _(_, __, ___))(
  316. lambda _, __, ___:
  317. chr(___ % __) + _(_, __, ___ // __) if ___ else
  318. (lambda: _).func_code.co_lnotab,
  319. 256,
  320. 802616035175250124568770929992
  321. )
  322. )
  323. )(
  324. *(lambda _, __, ___: _(_, __, ___))(
  325. (lambda _, __, ___:
  326. [__(___[(lambda: _).func_code.co_nlocals])] +
  327. _(_, __, ___[(lambda _: _).func_code.co_nlocals:]) if ___ else []
  328. ),
  329. lambda _: _.func_code.co_argcount,
  330. (
  331. lambda _: _,
  332. lambda _, __: _,
  333. lambda _, __, ___: _,
  334. lambda _, __, ___, ____: _,
  335. lambda _, __, ___, ____, _____: _,
  336. lambda _, __, ___, ____, _____, ______: _,
  337. lambda _, __, ___, ____, _____, ______, _______: _,
  338. lambda _, __, ___, ____, _____, ______, _______, ________: _
  339. )
  340. )
  341. )
  342. {% endhighlight %}
  343. ## Shifting bits
  344. Almost there! We'll replace the `n{1..8}` variables with `_`, `__`, `___`,
  345. `____`, etc., since it creates confusion with the variables used in our inner
  346. functions. This doesn't cause actual problems, since scoping rules mean the
  347. right ones will be used. This is also one of the reasons why we refactored
  348. `256` out to where `_` refers to `1` instead of our obfuscated `convert()`
  349. function. It's getting long, so I'll paste only the first half:
  350. {% highlight python linenos=table %}
  351. (lambda _, __, ___, ____, _____, ______, _______, ________:
  352. getattr(
  353. __import__(True.__class__.__name__[_] + [].__class__.__name__[__]),
  354. ().__class__.__eq__.__class__.__name__[:__] +
  355. ().__iter__().__class__.__name__[_____:________]
  356. )(
  357. _, (lambda _, __, ___: _(_, __, ___))(
  358. lambda _, __, ___:
  359. chr(___ % __) + _(_, __, ___ // __) if ___ else
  360. (lambda: _).func_code.co_lnotab,
  361. 256,
  362. 802616035175250124568770929992
  363. )
  364. )
  365. )
  366. {% endhighlight %}
  367. Only two more things are left. We'll start with the easy one: `256`.
  368. <span>\\(256 = 2^8\\)</span>, so we can rewrite it as `1 << 8` (using a
  369. [left bit shift](//stackoverflow.com/a/141873)), or `_ << ________` with our
  370. obfuscated variables.
  371. We'll use the same idea with `802616035175250124568770929992`. A simple
  372. divide-and-conquer algorithm can break it up into sums of numbers which are
  373. themselves sums of numbers that are shifted together, and so on. For example,
  374. if we had `112`, we could break it up into `96 + 16` and then
  375. `(3 << 5) + (2 << 3)`. I like using bit shifts because the `<<` reminds me of
  376. `std::cout << "foo"` in C++, or
  377. [`print` chevron](//docs.python.org/2/reference/simple_stmts.html#the-print-statement)
  378. (`print >>`) in Python, both of which are red herrings involving other ways of
  379. doing I/O.
  380. The number can be decomposed in a variety of ways; no one method is correct
  381. (after all, we could just break it up into `(1 << 0) + (1 << 0) + ...`, but
  382. that's not interesting). We should have some substantial amount of nesting, but
  383. still use most of our numerical variables. Obviously, doing this by hand isn't
  384. fun, so we'll come up with an algorithm. In pseudocode:
  385. {% highlight text linenos=table %}
  386. func encode(num):
  387. if num <= 8:
  388. return "_" * num
  389. else:
  390. return "(" + convert(num) + ")"
  391. func convert(num):
  392. base = shift = 0
  393. diff = num
  394. span = ...
  395. for test_base in range(span):
  396. for test_shift in range(span):
  397. test_diff = |num| - (test_base << test_shift)
  398. if |test_diff| < |diff|:
  399. diff = test_diff
  400. base = test_base
  401. shift = test_shift
  402. encoded = "(" + encode(base) + " << " + encode(shift) + ")"
  403. if diff == 0:
  404. return encoded
  405. else:
  406. return encoded + " + " + convert(diff)
  407. convert(802616035175250124568770929992)
  408. {% endhighlight %}
  409. The basic idea here is that we test various combinations of numbers in a
  410. certain range until we come up with two numbers, `base` and `shift`,
  411. such that `base << shift` is as closest to `num` as possible (i.e. we minimize
  412. their absolute difference, `diff`). We then use our divide-and-conquer
  413. algorithm to break up `best_base` and `best_shift`, and then repeat the
  414. procedure on `diff` until it reaches zero, summing the terms along the way.
  415. The argument to `range()`, `span`, represents the width of the search space.
  416. This can't be too large, or we'll end getting `num` as our `base` and `0` as
  417. our `shift` (because `diff` is zero), and since `base` can't be represented as
  418. a single variable, it'll repeat, recursing infinitely. If it's too small, we'll
  419. end up with something like the `(1 << 0) + (1 << 0) + ...` mentioned above. In
  420. practice, we want `span` to get smaller as the recursion depth increases.
  421. Through trial and error, I found this equation to work well:
  422. <div>$$\mathit{span} = \lceil\log_{1.5} \lvert{\mathit{num}}\lvert\rceil + \lfloor2^{4-\mathit{depth}}\rfloor$$</div>
  423. Translating the pseudocode into Python and making some tweaks (support for the
  424. `depth` argument, and some caveats involving negative numbers), we get this:
  425. {% highlight python linenos=table %}
  426. from math import ceil, log
  427. def encode(num, depth):
  428. if num == 0:
  429. return "_ - _"
  430. if num <= 8:
  431. return "_" * num
  432. return "(" + convert(num, depth + 1) + ")"
  433. def convert(num, depth=0):
  434. result = ""
  435. while num:
  436. base = shift = 0
  437. diff = num
  438. span = int(ceil(log(abs(num), 1.5))) + (16 >> depth)
  439. for test_base in xrange(span):
  440. for test_shift in xrange(span):
  441. test_diff = abs(num) - (test_base << test_shift)
  442. if abs(test_diff) < abs(diff):
  443. diff = test_diff
  444. base = test_base
  445. shift = test_shift
  446. if result:
  447. result += " + " if num > 0 else " - "
  448. elif num < 0:
  449. base = -base
  450. if shift == 0:
  451. result += encode(base, depth)
  452. else:
  453. result += "(%s << %s)" % (encode(base, depth),
  454. encode(shift, depth))
  455. num = diff if num > 0 else -diff
  456. return result
  457. {% endhighlight %}
  458. Now, when we call `convert(802616035175250124568770929992)`, we get a nice
  459. decomposition:
  460. {% highlight pycon %}
  461. >>> convert(802616035175250124568770929992)
  462. (((_____ << ____) + _) << ((___ << _____) - ___)) + (((((___ << __) - _) << ___) + _) << ((_____ << ____) + (_ << _))) + (((_______ << __) - _) << (((((_ << ___) + _)) << ___) + (_ << _))) + (((_______ << ___) + _) << ((_ << ______) + _)) + (((_______ << ____) - _) << ((_______ << ___))) + (((_ << ____) - _) << ((((___ << __) + _) << __) - _)) - (_______ << ((((___ << __) - _) << __) + _)) + (_______ << (((((_ << ___) + _)) << __))) - ((((((_ << ___) + _)) << __) + _) << ((((___ << __) + _) << _))) + (((_______ << __) - _) << (((((_ << ___) + _)) << _))) + (((___ << ___) + _) << ((_____ << _))) + (_____ << ______) + (_ << ___)
  463. {% endhighlight %}
  464. Stick this in as a replacement for `802616035175250124568770929992`, and put
  465. all the parts together:
  466. {% highlight python linenos=table %}
  467. (lambda _, __, ___, ____, _____, ______, _______, ________:
  468. getattr(
  469. __import__(True.__class__.__name__[_] + [].__class__.__name__[__]),
  470. ().__class__.__eq__.__class__.__name__[:__] +
  471. ().__iter__().__class__.__name__[_____:________]
  472. )(
  473. _, (lambda _, __, ___: _(_, __, ___))(
  474. lambda _, __, ___:
  475. chr(___ % __) + _(_, __, ___ // __) if ___ else
  476. (lambda: _).func_code.co_lnotab,
  477. _ << ________,
  478. (((_____ << ____) + _) << ((___ << _____) - ___)) + (((((___ << __)
  479. - _) << ___) + _) << ((_____ << ____) + (_ << _))) + (((_______ <<
  480. __) - _) << (((((_ << ___) + _)) << ___) + (_ << _))) + (((_______
  481. << ___) + _) << ((_ << ______) + _)) + (((_______ << ____) - _) <<
  482. ((_______ << ___))) + (((_ << ____) - _) << ((((___ << __) + _) <<
  483. __) - _)) - (_______ << ((((___ << __) - _) << __) + _)) + (_______
  484. << (((((_ << ___) + _)) << __))) - ((((((_ << ___) + _)) << __) +
  485. _) << ((((___ << __) + _) << _))) + (((_______ << __) - _) <<
  486. (((((_ << ___) + _)) << _))) + (((___ << ___) + _) << ((_____ <<
  487. _))) + (_____ << ______) + (_ << ___)
  488. )
  489. )
  490. )(
  491. *(lambda _, __, ___: _(_, __, ___))(
  492. (lambda _, __, ___:
  493. [__(___[(lambda: _).func_code.co_nlocals])] +
  494. _(_, __, ___[(lambda _: _).func_code.co_nlocals:]) if ___ else []
  495. ),
  496. lambda _: _.func_code.co_argcount,
  497. (
  498. lambda _: _,
  499. lambda _, __: _,
  500. lambda _, __, ___: _,
  501. lambda _, __, ___, ____: _,
  502. lambda _, __, ___, ____, _____: _,
  503. lambda _, __, ___, ____, _____, ______: _,
  504. lambda _, __, ___, ____, _____, ______, _______: _,
  505. lambda _, __, ___, ____, _____, ______, _______, ________: _
  506. )
  507. )
  508. )
  509. {% endhighlight %}
  510. And there you have it.
  511. ## Addendum: Python 3 support
  512. Since writing this post, several people have asked about Python 3 support.
  513. I didn't think of it at the time, but as Python 3 continues to gain traction
  514. (and thank you for that!), this post is clearly long overdue for an update.
  515. Fortunately, Python 3 (as of writing, 3.6) doesn't require us to change much:
  516. - The `func_code` function object attribute has been renamed to `__code__`.
  517. Easy fix with a find-and-replace.
  518. - The `tupleiterator` type name has been changed to `tuple_iterator`. Since we
  519. use this to extract the substring `"ite"`, we can get around this by changing
  520. our indexing in `().__iter__().__class__.__name__` from `[_____:________]` to
  521. `[_:][_____:________]`.
  522. - `os.write()` requires `bytes` now instead of a `str`, so `chr(...)` needs to
  523. be changed to `bytes([...])`.
  524. Here is the full Python 3 version:
  525. {% highlight python linenos=table %}
  526. (lambda _, __, ___, ____, _____, ______, _______, ________:
  527. getattr(
  528. __import__(True.__class__.__name__[_] + [].__class__.__name__[__]),
  529. ().__class__.__eq__.__class__.__name__[:__] +
  530. ().__iter__().__class__.__name__[_:][_____:________]
  531. )(
  532. _, (lambda _, __, ___: _(_, __, ___))(
  533. lambda _, __, ___:
  534. bytes([___ % __]) + _(_, __, ___ // __) if ___ else
  535. (lambda: _).__code__.co_lnotab,
  536. _ << ________,
  537. (((_____ << ____) + _) << ((___ << _____) - ___)) + (((((___ << __)
  538. - _) << ___) + _) << ((_____ << ____) + (_ << _))) + (((_______ <<
  539. __) - _) << (((((_ << ___) + _)) << ___) + (_ << _))) + (((_______
  540. << ___) + _) << ((_ << ______) + _)) + (((_______ << ____) - _) <<
  541. ((_______ << ___))) + (((_ << ____) - _) << ((((___ << __) + _) <<
  542. __) - _)) - (_______ << ((((___ << __) - _) << __) + _)) + (_______
  543. << (((((_ << ___) + _)) << __))) - ((((((_ << ___) + _)) << __) +
  544. _) << ((((___ << __) + _) << _))) + (((_______ << __) - _) <<
  545. (((((_ << ___) + _)) << _))) + (((___ << ___) + _) << ((_____ <<
  546. _))) + (_____ << ______) + (_ << ___)
  547. )
  548. )
  549. )(
  550. *(lambda _, __, ___: _(_, __, ___))(
  551. (lambda _, __, ___:
  552. [__(___[(lambda: _).__code__.co_nlocals])] +
  553. _(_, __, ___[(lambda _: _).__code__.co_nlocals:]) if ___ else []
  554. ),
  555. lambda _: _.__code__.co_argcount,
  556. (
  557. lambda _: _,
  558. lambda _, __: _,
  559. lambda _, __, ___: _,
  560. lambda _, __, ___, ____: _,
  561. lambda _, __, ___, ____, _____: _,
  562. lambda _, __, ___, ____, _____, ______: _,
  563. lambda _, __, ___, ____, _____, ______, _______: _,
  564. lambda _, __, ___, ____, _____, ______, _______, ________: _
  565. )
  566. )
  567. )
  568. {% endhighlight %}
  569. Thank you for reading! I continue to be amazed by this post's popularity.