Skip to content

Fix py2 urlopen #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2018
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
6 changes: 5 additions & 1 deletion jsonschema/compat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import operator
import sys

Expand Down Expand Up @@ -27,7 +28,10 @@
urljoin, urlunsplit, SplitResult, urlsplit as _urlsplit # noqa
)
from urllib import unquote # noqa
from urllib2 import urlopen # noqa
import urllib2 # noqa
def urlopen(*args, **kwargs):
return contextlib.closing(urllib2.urlopen(*args, **kwargs))

str_types = basestring
int_types = int, long
iteritems = operator.methodcaller("iteritems")
Expand Down
17 changes: 17 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from io import BytesIO
from unittest import TestCase
import json
import os
import sys
import tempfile
import unittest

from twisted.trial.unittest import SynchronousTestCase
Expand All @@ -22,6 +24,12 @@
from jsonschema.tests.compat import mock


if PY3:
from urllib.request import pathname2url
else:
from urllib import pathname2url


def startswith(validator, startswith, instance, schema):
if not instance.startswith(startswith):
yield ValidationError(u"Whoops!")
Expand Down Expand Up @@ -1341,6 +1349,15 @@ def fake_urlopen(url):
pass
self.assertEqual(resolved, 12)

def test_it_retrieves_local_refs_via_urlopen(self):
with tempfile.NamedTemporaryFile(delete=False, mode='wt') as tempf:
self.addCleanup(os.remove, tempf.name)
json.dump({'foo': 'bar'}, tempf)

ref = "file://{}#foo".format(pathname2url(tempf.name))
with self.resolver.resolving(ref) as resolved:
self.assertEqual(resolved, 'bar')

def test_it_can_construct_a_base_uri_from_a_schema(self):
schema = {"id": "foo"}
resolver = validators.RefResolver.from_schema(
Expand Down