how can i pickle an application-level object from interpreter code?

Hi all, I'm brand-new to PyPy, and here is my question: I want to selectively pickle Python objects that appear in application-level code, from *within* the interpreter. e.g., let's say i'm using the PyPy Python interpreter to run this program: import foo x = foo.ComplicatedObject() right after the interpreter creates an instance of foo.ComplicatedObject(), I want to serialize that instance to disk, in effect simulating this statement: pickle.dump(x, open('data.pickle')) What is the easiest way for me to do this from within the interpreter code? It seems like an instance object is represented within the interpreter as of type "class W_InstanceObject(Wrappable)", and I can't directly pickle that object. If I could just call the Python standard library's pickle on 'x' from within the interpreter, that would be great. Thanks in advance, Philip

Hi, 2009/10/2 Philip Guo <pg@cs.stanford.edu>:
Hi all,
I'm brand-new to PyPy, and here is my question: I want to selectively pickle Python objects that appear in application-level code, from *within* the interpreter. e.g., let's say i'm using the PyPy Python interpreter to run this program:
import foo x = foo.ComplicatedObject()
right after the interpreter creates an instance of foo.ComplicatedObject(), I want to serialize that instance to disk, in effect simulating this statement:
pickle.dump(x, open('data.pickle'))
What is the easiest way for me to do this from within the interpreter code?
It seems like an instance object is represented within the interpreter as of type "class W_InstanceObject(Wrappable)", and I can't directly pickle that object. If I could just call the Python standard library's pickle on 'x' from within the interpreter, that would be great.
This should be possible by using gateway.applevel, like the following code. Be careful however that pickle.dump is itself a python function which creates objects. Modifying W_InstanceObject.__init__ this way is probably not a good idea... Note: I wrote this quickly to get an idea; I did not test at all. I took the idea from some other code in pypy/module/__builtin__/operation.py from pypy.interpreter import gateway dump_object = gateway.applevel(r''' def dump_object(x): import pickle pickle.dump(x, open('data.pickle', 'w')) ''', filename =__file__).interphook('dump_object') then you should be able to call it this way: dump_object(space, w_instanceObject) -- Amaury Forgeot d'Arc

On Fri, Oct 2, 2009 at 10:32 AM, Amaury Forgeot d'Arc <amauryfa@gmail.com>wrote:
Hi,
Hi all,
I'm brand-new to PyPy, and here is my question: I want to selectively
Python objects that appear in application-level code, from *within* the interpreter. e.g., let's say i'm using the PyPy Python interpreter to run this program:
import foo x = foo.ComplicatedObject()
right after the interpreter creates an instance of foo.ComplicatedObject(), I want to serialize that instance to disk, in effect simulating this statement:
pickle.dump(x, open('data.pickle'))
What is the easiest way for me to do this from within the interpreter code?
It seems like an instance object is represented within the interpreter as of type "class W_InstanceObject(Wrappable)", and I can't directly pickle
2009/10/2 Philip Guo <pg@cs.stanford.edu>: pickle that
object. If I could just call the Python standard library's pickle on 'x' from within the interpreter, that would be great.
This should be possible by using gateway.applevel, like the following code. Be careful however that pickle.dump is itself a python function which creates objects. Modifying W_InstanceObject.__init__ this way is probably not a good idea...
Note: I wrote this quickly to get an idea; I did not test at all. I took the idea from some other code in pypy/module/__builtin__/operation.py
from pypy.interpreter import gateway
dump_object = gateway.applevel(r''' def dump_object(x): import pickle pickle.dump(x, open('data.pickle', 'w')) ''', filename =__file__).interphook('dump_object')
then you should be able to call it this way: dump_object(space, w_instanceObject)
Thanks so much for your quick reply, Amaury! This works extremely well. I'm still trying to wrap my head around app-level vs. interpreter-level, especially how to serialize data on both levels; your code snippet helped a lot! Philip

Hi Philip, On Fri, Oct 02, 2009 at 09:35:30AM -0700, Philip Guo wrote:
I'm brand-new to PyPy, and here is my question: I want to selectively pickle Python objects that appear in application-level code, from *within* the interpreter.
What about writing a metaclass and keeping it all as portable, regular Python code? E.g. class MetaPickle(type): def __call__(self, *args, **kwds): x = type.__call__(self, *args, **kwds) pickle.dump(x, f) return x class X(object): __metaclass__ = MetaPickle There are also ways to do it by patching the classes without changing anything to their definition. If you need more information about these solutions, please ask in a general Python list. A bientot, Armin
participants (3)
-
Amaury Forgeot d'Arc
-
Armin Rigo
-
Philip Guo