Skip to content

Commit 367e3df

Browse files
committed
BUG: Fixes python-jsonschema#126: generator objects leak on Python 3
1 parent dca5999 commit 367e3df

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

jsonschema/validators.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ def __init__(
7272

7373
@classmethod
7474
def check_schema(cls, schema):
75-
for error in cls(cls.META_SCHEMA).iter_errors(schema):
75+
for error in list(cls(cls.META_SCHEMA).iter_errors(
76+
schema, _first_only=True)):
7677
raise SchemaError.create_from(error)
7778

78-
def iter_errors(self, instance, _schema=None):
79+
def iter_errors(self, instance, _schema=None, _first_only=False):
7980
if _schema is None:
8081
_schema = self.schema
8182

@@ -104,6 +105,9 @@ def iter_errors(self, instance, _schema=None):
104105
error.schema_path.appendleft(k)
105106
yield error
106107

108+
if _first_only:
109+
return
110+
107111
def descend(self, instance, schema, path=None, schema_path=None):
108112
for error in self.iter_errors(instance, schema):
109113
if path is not None:
@@ -113,7 +117,8 @@ def descend(self, instance, schema, path=None, schema_path=None):
113117
yield error
114118

115119
def validate(self, *args, **kwargs):
116-
for error in self.iter_errors(*args, **kwargs):
120+
kwargs['_first_only'] = True
121+
for error in list(self.iter_errors(*args, **kwargs)):
117122
raise error
118123

119124
def is_type(self, instance, type):
@@ -132,8 +137,8 @@ def is_type(self, instance, type):
132137
return isinstance(instance, pytypes)
133138

134139
def is_valid(self, instance, _schema=None):
135-
error = next(self.iter_errors(instance, _schema), None)
136-
return error is None
140+
errors = list(self.iter_errors(instance, _schema, _first_only=True))
141+
return not len(errors)
137142

138143
if version is not None:
139144
Validator = validates(version)(Validator)

0 commit comments

Comments
 (0)