Skip to content

Commit

Permalink
FIX python3.4/3.5 compability issues in mock library
Browse files Browse the repository at this point in the history
  • Loading branch information
mfeurer committed Feb 13, 2017
1 parent fba5a7d commit 73d8d71
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion autosklearn/util/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ def save_model(self, model, idx, seed):
pickle.dump(model, zipfile, -1)
zipfile.close()
tempname = fh.name
except RecursionError:
# Actually I would like to catch a RecursionError here, but it turns out
# that it was added in python3.5 and cannot be used in python3.4. But
# since it is a subclass of RuntimeError this works fine as well
except RuntimeError:
filepath = os.path.join(self.get_model_dir(),
'%s.%s.model' % (seed, idx))

Expand Down
8 changes: 6 additions & 2 deletions test/test_util/test_backend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: utf-8 -*-
import sys
import unittest
import unittest.mock

Expand Down Expand Up @@ -49,7 +50,7 @@ def test_load_model_by_seed_and_id_gz(self, exists_mock, openMock,
def test_load_model_by_seed_and_id(self, exists_mock, pickleLoadMock):
exists_mock.return_value = False
open_mock = unittest.mock.mock_open(read_data='Data')
with unittest.mock.patch('autosklearn.util.backend.open', open_mock) as m:
with unittest.mock.patch('autosklearn.util.backend.open', open_mock, create=True):
seed = 13
idx = 17
expected_model = self._setup_load_model_mocks(open_mock,
Expand Down Expand Up @@ -100,7 +101,10 @@ def __init__(self):
def side_effect(self, *args):
self.num_calls += 1
if self.num_calls == 1:
raise RecursionError
if sys.version_info >= (3, 5):
raise RecursionError
else:
raise RuntimeError

dump_mock.side_effect = SideEffect().side_effect
model = sklearn.tree.DecisionTreeClassifier()
Expand Down

0 comments on commit 73d8d71

Please sign in to comment.