pythontypeerrordeep-copywerkzeugfile-storage

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.


Solution

  • 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.