@@ -79,30 +79,38 @@ def iter_errors(self, instance, _schema=None):
79
79
if _schema is None :
80
80
_schema = self .schema
81
81
82
- with self .resolver .in_scope (_schema .get (u"id" , u"" )):
83
- ref = _schema .get (u"$ref" )
84
- if ref is not None :
85
- validators = [(u"$ref" , ref )]
86
- else :
87
- validators = iteritems (_schema )
88
-
89
- for k , v in validators :
90
- validator = self .VALIDATORS .get (k )
91
- if validator is None :
92
- continue
93
-
94
- errors = validator (self , v , instance , _schema ) or ()
95
- for error in errors :
96
- # set details if not already set by the called fn
97
- error ._set (
98
- validator = k ,
99
- validator_value = v ,
100
- instance = instance ,
101
- schema = _schema ,
102
- )
103
- if k != u"$ref" :
104
- error .schema_path .appendleft (k )
105
- yield error
82
+ scope = _schema .get (u"id" )
83
+ has_scope = scope
84
+ if has_scope :
85
+ self .resolver .push_scope (scope )
86
+
87
+
88
+ ref = _schema .get (u"$ref" )
89
+ if ref is not None :
90
+ validators = [(u"$ref" , ref )]
91
+ else :
92
+ validators = iteritems (_schema )
93
+
94
+ for k , v in validators :
95
+ validator = self .VALIDATORS .get (k )
96
+ if validator is None :
97
+ continue
98
+
99
+ errors = validator (self , v , instance , _schema ) or ()
100
+ for error in errors :
101
+ # set details if not already set by the called fn
102
+ error ._set (
103
+ validator = k ,
104
+ validator_value = v ,
105
+ instance = instance ,
106
+ schema = _schema ,
107
+ )
108
+ if k != u"$ref" :
109
+ error .schema_path .appendleft (k )
110
+ yield error
111
+
112
+ if has_scope :
113
+ self .resolver .pop_scope ()
106
114
107
115
def descend (self , instance , schema , path = None , schema_path = None ):
108
116
for error in self .iter_errors (instance , schema ):
@@ -240,6 +248,8 @@ def __init__(
240
248
self .cache_remote = cache_remote
241
249
self .handlers = dict (handlers )
242
250
251
+
252
+ self .old_scopes = []
243
253
self .store = _utils .URIDict (
244
254
(id , validator .META_SCHEMA )
245
255
for id , validator in iteritems (meta_schemas )
@@ -259,14 +269,13 @@ def from_schema(cls, schema, *args, **kwargs):
259
269
260
270
return cls (schema .get (u"id" , u"" ), schema , * args , ** kwargs )
261
271
262
- @contextlib .contextmanager
263
- def in_scope (self , scope ):
272
+ def push_scope (self , resoultion_scope ):
264
273
old_scope = self .resolution_scope
265
- self .resolution_scope = urljoin (old_scope , scope )
266
- try :
267
- yield
268
- finally :
269
- self .resolution_scope = old_scope
274
+ self .old_scopes . append (old_scope )
275
+ self . resolution_scope = urljoin ( old_scope , resoultion_scope )
276
+
277
+ def pop_scope ( self ) :
278
+ self .resolution_scope = self . old_scopes . pop ()
270
279
271
280
@contextlib .contextmanager
272
281
def resolving (self , ref ):
@@ -293,8 +302,9 @@ def resolving(self, ref):
293
302
294
303
old_base_uri , self .base_uri = self .base_uri , uri
295
304
try :
296
- with self .in_scope (uri ):
297
- yield self .resolve_fragment (document , fragment )
305
+ self .push_scope (uri )
306
+ yield self .resolve_fragment (document , fragment )
307
+ self .pop_scope ()
298
308
finally :
299
309
self .base_uri = old_base_uri
300
310
0 commit comments