This question is related to another question I posted yesterday, although it is much more general in nature.
Because of the thread I mentionned, I have been trying to determine what objects can be copied, pickled, marshaled and what objects cannot.
While doing that, I stumbled on this puzzle:
new_obj = copy.deepcopy(my_obj)
function_that_uses_my_new_obj(new_obj)
throws:
function_that_uses_my_new_obj(new_obj)RuntimeError: Internal C++ object (Pyside.QtGui.QWidget) already deleted
Now, since my_obj is a C++ object, that error I can understand. And the reason for that particular problem is the main topic of the other thread.
However, when I try:
function_that_uses_my_new_obj(copy.deepcopy(my_obj))
I don't get anything at all. The program runs normally to this line, stops there for a few seconds and the execution is stopped, the code after that line is not run, no exception/error/warning is thrown and the Python prompt is ready to accept any new command.
EDIT
For some reason, using the copy() method instead of deepcopy() like so:
function_that_uses_my_new_obj(copy.copy(my_obj))
results in the same exception being thrown. So there has to be some point at which deepcopy decides to stop or is being stopped and that triggers the end of the execution. What I don't get is why nothing is raised to inform the user...
Copy and pickle will try to do their job in all cases, but as for many things in python, the responsibility of the result is placed on the programmer only.
In order of rising difficulty:
The objects that really can be safely copied or pickled are basically the primitive types, strings, lists, dictionaries, numbers, etc.
Then there are many objects that explicitly support the magic methods (like __copy__ and __deepcopy__)
Then there are the objects on which copy will do its best and succeed. For simple objects, that contain only references to objects in the previous levels.
Finally the really unsafe to copy:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With