From a23939f9c404dcd5b8aa006e959646c2b00374fb Mon Sep 17 00:00:00 2001 From: Miel Vander Sande Date: Wed, 20 May 2026 14:37:01 +0200 Subject: [PATCH] Add option to set MAX_CONTEXT_URLS in ContextResolver --- lib/pyld/context_resolver.py | 21 ++++++++++++++++++--- lib/pyld/jsonld.py | 3 --- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/pyld/context_resolver.py b/lib/pyld/context_resolver.py index 343d3663..dac646fa 100644 --- a/lib/pyld/context_resolver.py +++ b/lib/pyld/context_resolver.py @@ -15,6 +15,7 @@ from .resolved_context import ResolvedContext +# Restraints MAX_CONTEXT_URLS = 10 @@ -23,14 +24,28 @@ class ContextResolver: Resolves and caches remote contexts. """ - def __init__(self, shared_cache, document_loader): + def __init__(self, shared_cache, document_loader, max_context_urls = None): """ Creates a ContextResolver. + + :param max_context_urls: the maximum number of times to recusively fetch contexts. + (default MAX_CONTEXT_URLS). """ # processor-specific RDF parsers self.per_op_cache = {} self.shared_cache = shared_cache self.document_loader = document_loader + self._max_context_urls: int = max_context_urls if isinstance(max_context_urls, int) else MAX_CONTEXT_URLS + + @property + def max_context_urls(self) -> int: + return self._max_context_urls + + @max_context_urls.setter + def max_context_urls(self, max_context_urls: int) -> None: + if not isinstance(max_context_urls, int): + raise TypeError("Value for max_context_urls is not a number") + self._max_context_urls = max_context_urls def resolve(self, active_ctx, context, base, cycles=None): """ @@ -125,11 +140,11 @@ def _resolve_remote_context(self, active_ctx, url, base, cycles): def _fetch_context(self, active_ctx, url, cycles): # check for max context URLs fetched during a resolve operation - if len(cycles) > MAX_CONTEXT_URLS: + if len(cycles) > self.max_context_urls: raise jsonld.JsonLdError( 'Maximum number of @context URLs exceeded.', 'jsonld.ContextUrlError', - {'max': MAX_CONTEXT_URLS}, + {'max': self.max_context_urls}, code=( 'loading remote context failed' if active_ctx.get('processingMode') == 'json-ld-1.0' diff --git a/lib/pyld/jsonld.py b/lib/pyld/jsonld.py index 94899b40..0226403d 100644 --- a/lib/pyld/jsonld.py +++ b/lib/pyld/jsonld.py @@ -127,9 +127,6 @@ # Default base IRI if none is provided through input or options DEFAULT_BASE_IRI = 'http://example.org/base/' -# Restraints -MAX_CONTEXT_URLS = 10 - # resolved context cache # TODO: consider basing max on context size rather than number RESOLVED_CONTEXT_CACHE_MAX_SIZE = 100