By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,482 Members | 1,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,482 IT Pros & Developers. It's quick & easy.

Controlling copying and pickling of objects written in C

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.