470,572 Members | 2,099 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,572 developers. It's quick & easy.

Controlling copying and pickling of objects written in C

I am writing an extension and have "hidden" data included in the object's
C structure that is not visible to python. I am unsure what would happen
to that data if the python object were copied or pickled and would prefer
to raise an exception whenever code tries to copy/deep copy/pickle or
marshal the object since it would not make sense. Where would I look to
control that?

Thanks,
Adam
Jun 27 '08 #1
2 790
En Sun, 13 Apr 2008 01:57:42 -0300, Adam Bregenzer
<adam_no_spam@no_s.p.a.m.bregenzer.netescribió:
I am writing an extension and have "hidden" data included in the object's
C structure that is not visible to python. I am unsure what would happen
to that data if the python object were copied or pickled and would prefer
to raise an exception whenever code tries to copy/deep copy/pickle or
marshal the object since it would not make sense. Where would I look to
control that?
You could raise an exception in __getstate__ - that would make pickle
fail, and probably copy too but I'm not sure of that.

--
Gabriel Genellina

Jun 27 '08 #2
On Sun, 13 Apr 2008 16:49:51 -0300, Gabriel Genellina wrote:
En Sun, 13 Apr 2008 01:57:42 -0300, Adam Bregenzer
<adam_no_spam@no_s.p.a.m.bregenzer.netescribió:
>I am writing an extension and have "hidden" data included in the
object's C structure that is not visible to python. I am unsure what
would happen to that data if the python object were copied or pickled
and would prefer to raise an exception whenever code tries to copy/deep
copy/pickle or marshal the object since it would not make sense. Where
would I look to control that?

You could raise an exception in __getstate__ - that would make pickle
fail, and probably copy too but I'm not sure of that.
Thank you for pointing me in the right direction. Implementing
__getstate__ does trigger the exception for copy, however I seem to get
the most helpful error messages by implementing __reduce__ and
__getstate__ with a reduce related exception message and implementing
__copy__ with a copy related exception message.

Final question: What is the best exception to use. I am using
NotImplementedError since it is deliberately not implemented. Is that
correct or would a TypeError exception be more appropriate?

Thanks,
Adam
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hans Georg Krauthaeuser | last post: by
2 posts views Thread by Kirk Strauser | last post: by
reply views Thread by Shahin Saadati | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.