Controlling copying and pickling of objects written in C

Adam Bregenzer adam_no_spam at no_s.p.a.m.bregenzer.net
Sun Apr 13 19:47:32 EDT 2008


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 at no_s.p.a.m.bregenzer.net> escribió:
> 
>> 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



More information about the Python-list mailing list