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
31 changes: 31 additions & 0 deletions jsonschema/benchmarks/import_benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
A benchmark which measures the import time of jsonschema
"""

import subprocess
import sys


def import_time(loops):
total_us = 0
for _ in range(loops):
p = subprocess.run( # noqa: S603 (arguments are static)
[sys.executable, "-X", "importtime", "-c", "import jsonschema"],
stderr=subprocess.PIPE,
stdout=subprocess.DEVNULL,
check=True,
)

line = p.stderr.splitlines()[-1]
field = line.split(b"|")[-2].strip()
us = int(field) # microseconds
total_us += us

# pyperf expects seconds
return total_us / 1_000_000.0

if __name__ == "__main__":
from pyperf import Runner
runner = Runner()

runner.bench_time_func("Import time (cumulative)", import_time)
8 changes: 3 additions & 5 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2379,11 +2379,9 @@ def fake_urlopen(url):
self.assertEqual(url, "http://bar")
yield BytesIO(json.dumps(schema).encode("utf8"))

self.addCleanup(setattr, validators, "urlopen", validators.urlopen)
validators.urlopen = fake_urlopen

with self.resolver.resolving(ref) as resolved:
pass
with mock.patch("urllib.request.urlopen", new=fake_urlopen): # noqa: SIM117
with self.resolver.resolving(ref) as resolved:
pass
self.assertEqual(resolved, 12)

def test_it_retrieves_local_refs_via_urlopen(self):
Expand Down
2 changes: 1 addition & 1 deletion jsonschema/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from operator import methodcaller
from typing import TYPE_CHECKING
from urllib.parse import unquote, urldefrag, urljoin, urlsplit
from urllib.request import urlopen
from warnings import warn
import contextlib
import json
Expand Down Expand Up @@ -1225,6 +1224,7 @@ def resolve_remote(self, uri):
result = requests.get(uri).json()
else:
# Otherwise, pass off to urllib and assume utf-8
from urllib.request import urlopen
with urlopen(uri) as url: # noqa: S310
result = json.loads(url.read().decode("utf-8"))

Expand Down
Loading