From a4d778c546138f1dd638cb04cc9e26619d6a7443 Mon Sep 17 00:00:00 2001 From: ApamNapat Date: Mon, 2 Jul 2018 21:32:44 +0200 Subject: [PATCH 1/5] Fix unclosed resource in validators --- jsonschema/validators.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jsonschema/validators.py b/jsonschema/validators.py index a47c3aefa..9d4ed3cf2 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -753,7 +753,8 @@ def resolve_remote(self, uri): result = requests.get(uri).json else: # Otherwise, pass off to urllib and assume utf-8 - result = json.loads(urlopen(uri).read().decode("utf-8")) + with urlopen(uri) as url: + result = json.loads(url.read().decode("utf-8")) if self.cache_remote: self.store[uri] = result From 86dfbd4c9f38037a6abc996cf49f0a46533b048f Mon Sep 17 00:00:00 2001 From: ApamNapat Date: Sun, 8 Jul 2018 15:26:58 +0200 Subject: [PATCH 2/5] Extended existing test with checks for context manager calls --- jsonschema/tests/test_validators.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index f2f5a1f8b..6485b2ed0 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1186,14 +1186,23 @@ def test_it_retrieves_unstored_refs_via_requests(self): def test_it_retrieves_unstored_refs_via_urlopen(self): ref = "http://bar#baz" schema = {"baz": 12} + resource_manager_mock = mock.MagicMock() + (resource_manager_mock. + __enter__.return_value. + read.return_value. + decode.return_value) = json.dumps(schema).encode("utf8") + urlopen_mock = mock.MagicMock(return_value=resource_manager_mock) with MockImport("requests", None): - with mock.patch("jsonschema.validators.urlopen") as urlopen: - urlopen.return_value.read.return_value = ( - json.dumps(schema).encode("utf8")) + with mock.patch("jsonschema.validators.urlopen", urlopen_mock): with self.resolver.resolving(ref) as resolved: self.assertEqual(resolved, 12) - urlopen.assert_called_once_with("http://bar") + + urlopen_mock.assert_called_once_with("http://bar") + self.assertIn(mock.call.__enter__(), + resource_manager_mock.mock_calls) + self.assertIn(mock.call.__exit__(None, None, None), + resource_manager_mock.mock_calls) def test_it_can_construct_a_base_uri_from_a_schema(self): schema = {"id": "foo"} From febd30e7eac97bdc3cce0375d8901b29f3678675 Mon Sep 17 00:00:00 2001 From: ApamNapat Date: Sun, 8 Jul 2018 15:44:57 +0200 Subject: [PATCH 3/5] Refactored call asserts to pass in python 3.4 and 3.5 --- jsonschema/tests/test_validators.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 6485b2ed0..3eaac0532 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1199,10 +1199,8 @@ def test_it_retrieves_unstored_refs_via_urlopen(self): self.assertEqual(resolved, 12) urlopen_mock.assert_called_once_with("http://bar") - self.assertIn(mock.call.__enter__(), - resource_manager_mock.mock_calls) - self.assertIn(mock.call.__exit__(None, None, None), - resource_manager_mock.mock_calls) + resource_manager_mock.__enter__.assert_called_once() + resource_manager_mock.__exit__.assert_called_once() def test_it_can_construct_a_base_uri_from_a_schema(self): schema = {"id": "foo"} From 5998a1d87553a1293189ea6b9ddf390d8866f6f0 Mon Sep 17 00:00:00 2001 From: ApamNapat Date: Sun, 8 Jul 2018 15:54:41 +0200 Subject: [PATCH 4/5] Yet another assert variant to satisfy all python versions --- jsonschema/tests/test_validators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 3eaac0532..d28d94d1d 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1199,8 +1199,8 @@ def test_it_retrieves_unstored_refs_via_urlopen(self): self.assertEqual(resolved, 12) urlopen_mock.assert_called_once_with("http://bar") - resource_manager_mock.__enter__.assert_called_once() - resource_manager_mock.__exit__.assert_called_once() + self.assertEqual(resource_manager_mock.__enter__.call_count, 1) + self.assertEqual(resource_manager_mock.__exit__.call_count, 1) def test_it_can_construct_a_base_uri_from_a_schema(self): schema = {"id": "foo"} From 30bd2bc4aed14d40e5a48a82f21753c45b5eeeac Mon Sep 17 00:00:00 2001 From: ApamNapat Date: Sun, 8 Jul 2018 17:41:42 +0200 Subject: [PATCH 5/5] Changed asserts order to rerun CI --- jsonschema/tests/test_validators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index d28d94d1d..8ae04f3f1 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1199,8 +1199,8 @@ def test_it_retrieves_unstored_refs_via_urlopen(self): self.assertEqual(resolved, 12) urlopen_mock.assert_called_once_with("http://bar") - self.assertEqual(resource_manager_mock.__enter__.call_count, 1) self.assertEqual(resource_manager_mock.__exit__.call_count, 1) + self.assertEqual(resource_manager_mock.__enter__.call_count, 1) def test_it_can_construct_a_base_uri_from_a_schema(self): schema = {"id": "foo"}