From 4efcb4cb4cebc87cec1bbaa87409ed2315eeea7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20Sch=C3=A4fer?= Date: Tue, 19 Jan 2016 15:02:06 +0100 Subject: [PATCH] Use pickle instead of marshal --- kiwi/result.py | 13 +++++++------ test/unit/result_test.py | 33 ++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/kiwi/result.py b/kiwi/result.py index d47f5926..8a919383 100644 --- a/kiwi/result.py +++ b/kiwi/result.py @@ -15,7 +15,7 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # -import marshal +import pickle import os # project @@ -48,11 +48,11 @@ class Result(object): def dump(self, filename): try: - with open(filename, 'w') as result: - marshal.dump(self, result) + with open(filename, 'wb') as result: + pickle.dump(self, result) except Exception as e: raise KiwiResultError( - 'Failed to marshal dump results: %s' % format(e) + 'Failed to pickle dump results: %s' % format(e) ) @classmethod @@ -62,8 +62,9 @@ class Result(object): 'No result information %s found' % filename ) try: - return marshal.load(filename) + with open(filename, 'rb') as result: + return pickle.load(result) except Exception as e: raise KiwiResultError( - 'Failed to marshal load results: %s' % format(e) + 'Failed to pickle load results: %s' % format(e) ) diff --git a/test/unit/result_test.py b/test/unit/result_test.py index 6915af7d..440df85b 100644 --- a/test/unit/result_test.py +++ b/test/unit/result_test.py @@ -36,31 +36,38 @@ class TestResult(object): self.result.print_results() assert mock_info.called - @patch('marshal.dump') + @patch('pickle.dump') @patch('__builtin__.open') - def test_dump(self, mock_open, mock_marshal_dump): + def test_dump(self, mock_open, mock_pickle_dump): mock_open.return_value = self.context_manager_mock self.result.dump('kiwi.result') mock_open.assert_called_once_with( - 'kiwi.result', 'w' + 'kiwi.result', 'wb' ) - mock_marshal_dump.assert_called_once_with( + mock_pickle_dump.assert_called_once_with( self.result, self.file_mock ) - @patch('marshal.dump') + @patch('pickle.dump') @patch('__builtin__.open') @raises(KiwiResultError) - def test_dump_failed(self, mock_open, mock_marshal_dump): - mock_marshal_dump.side_effect = Exception + def test_dump_failed(self, mock_open, mock_pickle_dump): + mock_pickle_dump.side_effect = Exception self.result.dump('kiwi.result') - @patch('marshal.load') + @patch('pickle.load') @patch('os.path.exists') - def test_load(self, mock_exists, mock_marshal_load): + @patch('__builtin__.open') + def test_load(self, mock_open, mock_exists, mock_pickle_load): + mock_open.return_value = self.context_manager_mock mock_exists.return_value = True Result.load('kiwi.result') - mock_marshal_load.assert_called_once_with('kiwi.result') + mock_open.assert_called_once_with( + 'kiwi.result', 'rb' + ) + mock_pickle_load.assert_called_once_with( + self.file_mock + ) @patch('os.path.exists') @raises(KiwiResultError) @@ -68,10 +75,10 @@ class TestResult(object): mock_exists.return_value = False Result.load('kiwi.result') - @patch('marshal.load') + @patch('pickle.load') @patch('os.path.exists') @raises(KiwiResultError) - def test_load_failed(self, mock_exists, mock_marshal_load): + def test_load_failed(self, mock_exists, mock_pickle_load): mock_exists.return_value = True - mock_marshal_load.side_effect = Exception + mock_pickle_load.side_effect = Exception Result.load('kiwi.result')