diff --git a/.gitignore b/.gitignore index b84d8de9d..d7a442815 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,6 @@ target/ # IPython .ipynb_checkpoints Untitled.ipynb + +# Dev tools +.idea diff --git a/cloudpickle/cloudpickle.py b/cloudpickle/cloudpickle.py index 30d90516c..d15769721 100644 --- a/cloudpickle/cloudpickle.py +++ b/cloudpickle/cloudpickle.py @@ -541,30 +541,27 @@ def save_file(self, obj): return self.save_reduce(getattr, (sys,'stderr'), obj=obj) if obj is sys.stdin: raise pickle.PicklingError("Cannot pickle standard input") - if not obj.closed: - if hasattr(obj, 'isatty') and obj.isatty(): - raise pickle.PicklingError("Cannot pickle files that map to tty objects") - if 'r' not in obj.mode and '+' not in obj.mode: - raise pickle.PicklingError("Cannot pickle files that are not opened for reading: %s" % obj.mode) + if obj.closed: + raise pickle.PicklingError("Cannot pickle closed files") + if hasattr(obj, 'isatty') and obj.isatty(): + raise pickle.PicklingError("Cannot pickle files that map to tty objects") + if 'r' not in obj.mode and '+' not in obj.mode: + raise pickle.PicklingError("Cannot pickle files that are not opened for reading: %s" % obj.mode) name = obj.name retval = pystringIO.StringIO() - if obj.closed: - #create an empty closed string io - retval.close() - else: - try: - # Read the whole file - curloc = obj.tell() - obj.seek(0) - contents = obj.read() - obj.seek(curloc) - except IOError: - raise pickle.PicklingError("Cannot pickle file %s as it cannot be read" % name) - retval.write(contents) - retval.seek(curloc) + try: + # Read the whole file + curloc = obj.tell() + obj.seek(0) + contents = obj.read() + obj.seek(curloc) + except IOError: + raise pickle.PicklingError("Cannot pickle file %s as it cannot be read" % name) + retval.write(contents) + retval.seek(curloc) retval.name = name self.save(retval) diff --git a/tests/cloudpickle_file_test.py b/tests/cloudpickle_file_test.py index 7a1a0dafc..a5b13fcce 100644 --- a/tests/cloudpickle_file_test.py +++ b/tests/cloudpickle_file_test.py @@ -35,17 +35,13 @@ def test_empty_file(self): self.assertEquals('', pickle.loads(cloudpickle.dumps(f)).read()) os.remove(self.tmpfilepath) - # XXX: investigate why this is a problem under Python 3 - @pytest.mark.skipif(sys.version_info > (2, 7), - reason="only works on Python 2.x") def test_closed_file(self): # Write & close with open(self.tmpfilepath, 'w') as f: f.write(self.teststring) - # Cloudpickle returns an empty (& closed!) StringIO if the file was - # closed... - unpickled = pickle.loads(cloudpickle.dumps(f)) - self.assertTrue(unpickled.closed) + with pytest.raises(pickle.PicklingError) as excinfo: + cloudpickle.dumps(f) + assert "Cannot pickle closed files" in str(excinfo.value) os.remove(self.tmpfilepath) def test_r_mode(self):