Skip to content

Instantly share code, notes, and snippets.

@dutc
Forked from llllllllll/cell_set.py
Last active May 26, 2017 16:06
Show Gist options
  • Save dutc/d2ff9f17a0520d233b2d197d1dafa1a2 to your computer and use it in GitHub Desktop.
Save dutc/d2ff9f17a0520d233b2d197d1dafa1a2 to your computer and use it in GitHub Desktop.
#!/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'
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