pythonserializationpickledumpdill

Python dill.dump fmode parameter seems to have no effect


I'm experimenting with dill, specifically different modes of dill.dump(). The documentation for the fmode parameter indicates that using FILE_FMODE or CONTENTS_FMODE will pickle the file contents, which would be useful when serializing in one location and then deserializing and executing in another location later.

However, none of the fmode values seems to have any effect - all the pickle files I generate are identical. Sample code to reproduce is as follows:

import dill as pickle
from dill import CONTENTS_FMODE, FILE_FMODE, HANDLE_FMODE

from test.pickle_classes import AddTwoIntegers

with open('add_two_ints_fmode_none.pkl', 'wb') as f:
    pickle.dump(AddTwoIntegers, f)

with open('add_two_ints_fmode_contents.pkl', 'wb') as f:
    pickle.dump(AddTwoIntegers, f, fmode=CONTENTS_FMODE)

with open('add_two_ints_fmode_file.pkl', 'wb') as f:
    pickle.dump(AddTwoIntegers, f, fmode=FILE_FMODE)

with open('add_two_ints_fmode_handle.pkl', 'wb') as f:
    pickle.dump(AddTwoIntegers, f, fmode=HANDLE_FMODE)

The folder in which this code is located contains a folder named 'test', within which is a file named 'pickle_classes.py' containing the following code:

class AddTwoIntegers:

    @staticmethod
    def add(a: int, b: int) -> int:
        return a + b

But like I said - all four pickle files I create are identical. Am I doing something wrong? Am I misunderstanding how fmode is supposed to work? I've traipsed through the dill source code and I see how fmode is set, but I don't see where it's being used at all.

I'm using dill 0.3.4 with python 3.9.12. Any advice is appreciated -TIA


Solution

  • I'm the dill author. Sorry the docs aren't clear. So the only place that fmode is used is when you are pickling a file handle (i.e. an instance of a FileType, TextWrapperType, BufferedReaderType, or a similar object), like this:

    >>> import dill
    >>> f = open('foo.txt', 'w')
    >>> g = dill.copy(f, fmode=dill.FILE_FMODE)
    >>> g
    <_io.TextIOWrapper name='foo.txt' mode='w' encoding='UTF-8'>
    

    For several examples see: https://github.com/uqfoundation/dill/blob/master/dill/tests/test_file.py