Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
recursive-include srsly *.h *.pyx *.pxd *.cc *.c *.cpp *.json
include LICENSE
include README.md
recursive-include srsly/tests/cloudpickle/mock_local_folder *.py
3 changes: 3 additions & 0 deletions srsly/tests/cloudpickle/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pytest

pytest.register_assert_rewrite("tests.testutils")
32 changes: 15 additions & 17 deletions srsly/tests/cloudpickle/cloudpickle_file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import sys
import tempfile
import unittest
import pickle

import pytest

import srsly.cloudpickle as cloudpickle
from srsly.cloudpickle.compat import pickle


class CloudPickleFileTests(unittest.TestCase):
Expand All @@ -16,22 +16,22 @@ class CloudPickleFileTests(unittest.TestCase):

def setUp(self):
self.tmpdir = tempfile.mkdtemp()
self.tmpfilepath = os.path.join(self.tmpdir, 'testfile')
self.teststring = 'Hello world!'
self.tmpfilepath = os.path.join(self.tmpdir, "testfile")
self.teststring = "Hello world!"

def tearDown(self):
shutil.rmtree(self.tmpdir)

def test_empty_file(self):
# Empty file
open(self.tmpfilepath, 'w').close()
with open(self.tmpfilepath, 'r') as f:
self.assertEqual('', pickle.loads(cloudpickle.dumps(f)).read())
open(self.tmpfilepath, "w").close()
with open(self.tmpfilepath) as f:
self.assertEqual("", pickle.loads(cloudpickle.dumps(f)).read())
os.remove(self.tmpfilepath)

def test_closed_file(self):
# Write & close
with open(self.tmpfilepath, 'w') as f:
with open(self.tmpfilepath, "w") as f:
f.write(self.teststring)
with pytest.raises(pickle.PicklingError) as excinfo:
cloudpickle.dumps(f)
Expand All @@ -40,25 +40,24 @@ def test_closed_file(self):

def test_r_mode(self):
# Write & close
with open(self.tmpfilepath, 'w') as f:
with open(self.tmpfilepath, "w") as f:
f.write(self.teststring)
# Open for reading
with open(self.tmpfilepath, 'r') as f:
with open(self.tmpfilepath) as f:
new_f = pickle.loads(cloudpickle.dumps(f))
self.assertEqual(self.teststring, new_f.read())
os.remove(self.tmpfilepath)

def test_w_mode(self):
with open(self.tmpfilepath, 'w') as f:
with open(self.tmpfilepath, "w") as f:
f.write(self.teststring)
f.seek(0)
self.assertRaises(pickle.PicklingError,
lambda: cloudpickle.dumps(f))
self.assertRaises(pickle.PicklingError, lambda: cloudpickle.dumps(f))
os.remove(self.tmpfilepath)

def test_plus_mode(self):
# Write, then seek to 0
with open(self.tmpfilepath, 'w+') as f:
with open(self.tmpfilepath, "w+") as f:
f.write(self.teststring)
f.seek(0)
new_f = pickle.loads(cloudpickle.dumps(f))
Expand All @@ -67,7 +66,7 @@ def test_plus_mode(self):

def test_seek(self):
# Write, then seek to arbitrary position
with open(self.tmpfilepath, 'w+') as f:
with open(self.tmpfilepath, "w+") as f:
f.write(self.teststring)
f.seek(4)
unpickled = pickle.loads(cloudpickle.dumps(f))
Expand All @@ -84,9 +83,8 @@ def test_pickling_special_file_handles(self):
# Warning: if you want to run your tests with nose, add -s option
for out in sys.stdout, sys.stderr: # Regression test for SPARK-3415
self.assertEqual(out, pickle.loads(cloudpickle.dumps(out)))
self.assertRaises(pickle.PicklingError,
lambda: cloudpickle.dumps(sys.stdin))
self.assertRaises(pickle.PicklingError, lambda: cloudpickle.dumps(sys.stdin))


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
Loading
Loading