Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there objects for which it is impossible to create a deep copy?

I get an error message when I execute the following line:

img = copy.deepcopy(img_file_obj)

The img_file_obj has the following type:

<class 'werkzeug.datastructures.FileStorage'>

Is it not allowed to create a deep copy of a file storage object?

ADDED

I probably need to explain why I am trying to create a copy of a file-storage object. In the end of my code I execute:

img_obj.save(fname)

But before that I check how big the file is. I do it in the following way:

img_obj.seek(0, os.SEEK_END)
size = img.tell()

The problem is that the checking of the size "destroys" the file. If I check the file size and then save it, I get an empty file on the disk. It is why I wanted to create a copy of the file-object, check the size of the copy and, if the size is acceptable, save the original file-object on the disk.

like image 333
Roman Avatar asked Sep 06 '14 04:09

Roman


People also ask

What objects Cannot be copied in Python?

Any object with sub-elements (whether selected by the standard __deepcopy__ or its own implementation) that cannot be deep-copied cannot be deep-copied.

Does object assign deep copy?

Object. assign does not copy prototype properties and methods. This method does not create a deep copy of Source Object, it makes a shallow copy of the data. For the properties containing reference or complex data, the reference is copied to the destination object, instead of creating a separate object.

What are deep copies?

A deep copy of an object is a copy whose properties do not share the same references (point to the same underlying values) as those of the source object from which the copy was made.


1 Answers

Are there objects for which it is impossible to create a deep copy?

Yes.

Any object whose type overrides the standard __deepcopy__ with (or registers with copyreg) a function that raises an exception can't be deep-copied.

Any object whose type uses the standard __deepcopy__ but can't be (shallow-)copied cannot be deep-copied.

Any object with sub-elements (whether selected by the standard __deepcopy__ or its own implementation) that cannot be deep-copied cannot be deep-copied.

And there are many kinds of objects that can't be even shallow-copied. The documentation for the copy module gives some examples:

This module does not copy types like module, method, stack trace, stack frame, file, socket, window, array, or any similar types.

Not that it would be impossible to copy a file, but what it would mean is pretty ambiguous (should it dup the file handle, reopen the file, or share the handle? should it start with the same file pointer? does its buffer or stateful encoder have to be in the same state?), so it would be more misleading than helpful to make them copyable.

Of course the Werkzeug framework could have chosen to make its FileStorage objects copyable even though standard Python file objects aren't, but they presumably had the same reasons not to.

like image 53
abarnert Avatar answered Nov 11 '22 14:11

abarnert