160 points · by mercapto · 8y ago · 116 comments
def invite_accepted(sender, email, **kwargs):
import sys
caller = sys._getframe(2)
request = caller.f_locals['request']
request.session['invite_accepted'] = TrueI was monkey patching an external library in JavaScript today, rather than having to fork the thing for one slight modification, and thought about this philosophy again.
Monkey patching is how you get into situations where the order in which libraries get loaded causes software to unpredictably work or break. Or, worse yet, to work reliably on the developer's machine and break in the wild. (Because the external library on someone else's server loads slower for the developer than their local library, and faster for some users.)
I think you learn when not to do it, which is most of the time.
Your beef is likely with the reality of doing business and shipping a real product.
exec(
code_fragment,
frame.f_globals,
frame.f_locals
)
# update locals
ctypes.pythonapi.PyFrame_LocalsToFast(ctypes.py_object(frame), ctypes.c_int(0))
This sets the values back into the right place on the frame you were operating on.https://pydev.blogspot.jp/2014/02/changing-locals-of-frame-f...
See: https://gist.github.com/llllllllll/b1ac68a6b77535a64c0b13bfd... https://gist.github.com/dutc/d2ff9f17a0520d233b2d197d1dafa1a... https://twitter.com/__qualname__/status/867928378040438784
But then again, I was playing around with the function bytecode to transfer functions between instances in some of my for-fun hacks (you can access the byte code of a function and manually create functions from byte code in Python through "code objects").
In that example, subclasses of Runnable can be transferred. However, only the corpus of the "execute" method and the "properties" object is transferred. If you need to access modules, you'd have to import them inside the execute method.
But if you actually plan to use this code in production, I would strongly advise against it :). There is a better solution available that will survive django upgrades:
Add a middleware that stores the session object in a thread-local variable for the duration of that request. You can then access that variable from your event handler.
def create_callback(request):
def invite_accepted(sender, email, **kwargs):
request.session['invite_accepted'] = True
return invite_accepted
You would then pass create_callback(request_object) as the callback, and would have access to 'request'.I thought, preventing Abuse in all ways was the biggest point behind Python.
This is abuse: https://github.com/ajalt/fuckitpy
Heh.
[0] https://msdn.microsoft.com/en-us/library/aa266173(v=vs.60).a...
"On Error Resume Next specifies that when a run-time error occurs, control goes to the statement immediately following the statement where the error occurred where execution continues. Use this form rather than On Error GoTo when accessing objects."
https://gist.github.com/llllllllll/b1ac68a6b77535a64c0b13bfd...
(Python's LOAD_FAST bytecode does `fastlocals[i]`: how can we abuse the lack of bounds checking on this array access?)
(We've also discussed potential extensions this to approach "lift" C-extension code in bytestrings into interpreter objects. This would be useful to escalate existing interpreter attacks in environments that try to lock things down.)
t = (0, 1)
tuple_setitem(t, 0, 99)
print(t)
# (0, 1)
EDIT: Ah, it seems to work in 3.6.3Is there a use case for something like this?
If you want an iterative function, write one. It's not harder than writing a recursive one (although sometimes a bit less readable).
Writing an iterative function is also both faster than the recursive function, and much faster than the hack.
Still a neat hack, though.
Was more of an interesting challenge personally.
We can do it for general JS programs. If you are willing to compile Python to JS, you can transparently use our compiler to get heap bounded stacks.
A cool result of doing this in JavaScript is that any language that compiles to JavaScript (python, ocaml, Scala, c++, clojure...) can transparently get heap bounded stacks by just using our compiler.
For some more examples, look at pyret (pyret.org) that also supports heap bounded stacks. (In fact, we were able to strip out the Pyret compiler and just use Stopify to get all these benefits)
>>> def recurse():
... return recurse
...
>>> recurse
<function recurse at 0x1083b5f28>
>>> recurse()
<function recurse at 0x1083b5f28>
>>> recurse()()()
<function recurse at 0x1083b5f28>Reminds me of the fact that Python doesn't have a char type and it's strings all the way down:
"a"[0][0][0][0] == "a"
Also, what does `recurse()()()` mean? What are we looking for exactly?
recurse == recurse()()()()...
It's more of an interesting observation than anything usefulThe syntax can occasionally be useful if you have a function the generates function, but then you'd be calling initial function with some argument like this:
f_x = f(x)()
f_y = f(y)()its just abusing a core mechanic.
a=[]
a.append(a)
When a recursive call happens, it aborts the top-level call, runs the recursive call directly, memoizes the result, and then re-executes the original call. Hence the warning about this only being usable for pure functions.
EDIT: also I just noticed that the map keys aren't the parameters themselves, but their string representations. So heaven help you if you try this with a data type whose str() method isn't one-to-one.