Skip to content

Commit b0911cf

Browse files
committed
#782: Global URI store for resolver
1 parent 5599eb3 commit b0911cf

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

jsonschema/tests/test_validators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from jsonschema import FormatChecker, TypeChecker, exceptions, validators
1717
from jsonschema.tests._helpers import bug
18+
from jsonschema.validators import remove_store_item
1819

1920

2021
def startswith(validator, startswith, instance, schema):
@@ -1527,6 +1528,9 @@ def handler(url):
15271528
with resolver.resolving(ref) as resolved:
15281529
self.assertEqual(resolved, schema)
15291530

1531+
# Remove test item from store
1532+
remove_store_item(ref)
1533+
15301534
def test_cache_remote_on(self):
15311535
response = [object()]
15321536

@@ -1545,6 +1549,9 @@ def handler(url):
15451549
with resolver.resolving(ref):
15461550
pass
15471551

1552+
# Remove test item from store
1553+
remove_store_item(ref)
1554+
15481555
def test_cache_remote_off(self):
15491556
response = [object()]
15501557

jsonschema/validators.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,29 @@ def extend(validator, validators=(), version=None, type_checker=None):
496496
)
497497

498498
_LATEST_VERSION = Draft202012Validator
499+
_STORE = None
500+
501+
502+
def get_store():
503+
"""
504+
Get store or initialize if not None
505+
"""
506+
global _STORE
507+
if _STORE is None:
508+
_STORE = _utils.URIDict(
509+
(id, validator.META_SCHEMA)
510+
for id, validator in meta_schemas.items()
511+
)
512+
return _STORE
513+
514+
515+
def remove_store_item(key):
516+
"""
517+
Remove a given key from the store
518+
"""
519+
global _STORE
520+
if _STORE is not None:
521+
del _STORE[key]
499522

500523

501524
class RefResolver(object):
@@ -562,10 +585,7 @@ def __init__(
562585
self.handlers = dict(handlers)
563586

564587
self._scopes_stack = [base_uri]
565-
self.store = _utils.URIDict(
566-
(id, validator.META_SCHEMA)
567-
for id, validator in meta_schemas.items()
568-
)
588+
self.store = get_store()
569589
self.store.update(store)
570590
self.store[base_uri] = referrer
571591

0 commit comments

Comments
 (0)