I have a simple function I want to call in two separate program executions. The first time I run the program it saves the function pointer into a file:
import cPickle
def test():
pass
def main():
a = test
pFile = open('test.txt', 'wb')
cPickle.dump(a, pFile)
pFile.close()
The second time I want to load the file and execute the function saved in the object:
import cPickle
def test():
pass
def main():
pFile = open('test.txt', 'rb')
a = cPickle.load(pFile)
pFile.close()
a()
Note that with cPickle, this works, which is a bit odd, since I figured this is basically a pointer to the function and that this would change at runtime? When I print ‘a’, it gives me something like :function test at 0x0351C170:.
So I guess my first question is, why does this work, even though the reference address should change at runtime?
And my second question would be, if it does not change/or it’s irrelevant somehow, how can I execute the function just from having the string: :function test at 0x0351C170:. e.g. something like:
def test():
print 'test'
a = test
a() #outputs: 'test'
b = str(a) #<function test at 0x03509170>
eval(b)() #error
See the
pickledocumentation:So the
picklemodule actually writes out the Python bytecode in the pickle file. When the pickle file is reloaded, the function is recreated from the code object.To answer the second part of your question, no, it is not possible to directly call Python code just by knowing the internal address value.