Skip to content

Instantly share code, notes, and snippets.

@jcrist
Last active May 26, 2017 06:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcrist/1e77dfe0c96bccc9c3de7043c205f25f to your computer and use it in GitHub Desktop.
Save jcrist/1e77dfe0c96bccc9c3de7043c205f25f to your computer and use it in GitHub Desktop.
Solution to Joe Jevnik's awful puzzle
import platform
import ctypes
print("Implementation: %s" % platform.python_implementation())
def cell_set(cell, target):
offset = 2 if platform.python_implementation() == 'PyPy' else 4
def inner():
return (lambda: target).__closure__[0]
p_cell = ctypes.cast(id(cell), ctypes.POINTER(ctypes.c_int))
p_target_cell = ctypes.cast(id(inner()), ctypes.POINTER(ctypes.c_int))
import pdb;pdb.Pdb() # prevent pypy from optimizing out
p_cell[offset] = p_target_cell[offset]
def f():
a = 'ayy'
def g():
cell_set((lambda: a).__closure__[0], 'lmao')
g()
return a
print(f())
@jcrist
Copy link
Author

jcrist commented May 26, 2017

One possible solution to this awful nerd snipe.

Running this on both cpython and pypy:

jcrist Code $ python test.py
Implementation: CPython
lmao
jcrist Code $ pypy test.py
Implementation: PyPy
lmao

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment