Changeset 1482:600fb1c435a9


Ignore:
Timestamp:
Apr 19, 2014, 7:13:39 PM (5 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Deliberately don't support pickling for `FTPHost` and `FTPFile`.

Problems with pickling (from ticket #75):

- To re-connect from the pickle file, you'd need the username and
  password, and I would understand if someone was mad at me if I
  didn't just use their password in memory but also wrote it to a
  file. ;-)

- The pickle file might be unpickled in a completely different
  environment. It might not be possible to connect to the host from
  there.

- The state of the FTP server might have changed between pickling and
  unpickling. For example, your last current directory might no longer
  exist. Of course, this can also happen during a session, but the
  more time passes between two logins (before pickling and when
  unpickling), the more likely a change on the remote side is.

- It gets much more complicated if there are open remote files when
  the FTPHost instance should be pickled.
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ftputil/file.py

    r1459 r1482  
    1 # Copyright (C) 2003-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2003-2014, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# Copyright (C) 2008, Roger Demetrescu <roger.demetrescu@gmail.com>
    33# See the file LICENSE for licensing terms.
     
    1111
    1212import io
     13import pickle
    1314
    1415import ftputil.compat
     
    301302            # purposes.
    302303            self.closed = True
     304
     305    def __getstate__(self):
     306        raise pickle.PicklingError("pickling is deliberately unsupported")
  • ftputil/host.py

    r1465 r1482  
    1 # Copyright (C) 2002-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     1# Copyright (C) 2002-2014, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    22# Copyright (C) 2008, Roger Demetrescu <roger.demetrescu@gmail.com>
    33# See the file LICENSE for licensing terms.
     
    1313
    1414import ftplib
     15import pickle
    1516import stat
    1617import sys
     
    898899        self.stat_cache.invalidate(path)
    899900
     901    def __getstate__(self):
     902        raise pickle.PicklingError("pickling is deliberately unsupported")
     903
    900904    #
    901905    # Context manager methods
  • test/test_host.py

    r1435 r1482  
    11# encoding: utf-8
    2 # Copyright (C) 2002-2013, Stefan Schwarzer <sschwarzer@sschwarzer.net>
     2# Copyright (C) 2002-2014, Stefan Schwarzer <sschwarzer@sschwarzer.net>
    33# See the file LICENSE for licensing terms.
    44
     
    88import itertools
    99import os
     10import pickle
    1011import posixpath
    1112import random
     
    568569
    569570
     571class TestFailingPickling(unittest.TestCase):
     572
     573    def test_failing_pickling(self):
     574        """Test if pickling (intentionally) isn't supported."""
     575        with test_base.ftp_host_factory() as host:
     576            self.assertRaises(pickle.PicklingError, pickle.dumps, host)
     577            with host.open("/home/sschwarzer/index.html") as file_obj:
     578                self.assertRaises(pickle.PicklingError, pickle.dumps, file_obj)
     579
     580
    570581if __name__ == "__main__":
    571582    unittest.main()
Note: See TracChangeset for help on using the changeset viewer.