-
-
Save dutc/d2ff9f17a0520d233b2d197d1dafa1a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from platform import python_implementation | |
if python_implementation() not in ('CPython', 'PyPy'): | |
raise NotImplementedError('unsupported') | |
from types import FunctionType | |
def cell_set(obj, val): | |
def _(val, cell = None): | |
def _(val): | |
nonlocal cell | |
cell = val | |
return _ | |
_ = _(val) | |
FunctionType(_.__code__, _.__globals__, _.__name__, _.__defaults__, (obj,))(val) | |
def f(): | |
a = 'ayy' | |
def g(): | |
cell_set((lambda: a).__closure__[0], 'lmao') | |
g() | |
return a | |
assert f() == 'lmao' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SHELL = /bin/zsh | |
.PHONY: test | |
.ONESHELL: | |
test: cell_set.py | |
@for interp in pypy pypy3 python2 python lua; do | |
printf "%-8s %s\n" "$$interp" "$$($$interp $^ >/dev/null 2>&1 && echo "✓" || echo "✗")" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment