# Path: src/formencode/api.py
# class NoDefault(object):
#     """A dummy value used for parameters with no default."""
# 
# class Invalid(Exception):
# 
#     """
#     This is raised in response to invalid input.  It has several
#     public attributes:
# 
#     ``msg``:
#         The message, *without* values substituted.  For instance, if
#         you want HTML quoting of values, you can apply that.
#     ``substituteArgs``:
#         The arguments (a dictionary) to go with ``msg``.
#     ``str(self)``:
#         The message describing the error, with values substituted.
#     ``value``:
#         The offending (invalid) value.
#     ``state``:
#         The state that went with this validator.  This is an
#         application-specific object.
#     ``error_list``:
#         If this was a compound validator that takes a repeating value,
#         and sub-validator(s) had errors, then this is a list of those
#         exceptions.  The list will be the same length as the number of
#         values -- valid values will have None instead of an exception.
#     ``error_dict``:
#         Like ``error_list``, but for dictionary compound validators.
#     """
# 
#     def __init__(self, msg,
#                  value, state, error_list=None, error_dict=None):
#         Exception.__init__(self, msg, value, state, error_list, error_dict)
#         self.msg = msg
#         self.value = value
#         self.state = state
#         self.error_list = error_list
#         self.error_dict = error_dict
#         assert (not self.error_list or not self.error_dict), (
#                 "Errors shouldn't have both error dicts and lists "
#                 "(error %s has %s and %s)"
#                 % (self, self.error_list, self.error_dict))
# 
#     def __str__(self):
#         val = self.msg
#         return val
# 
#     if six.text_type is not str:  # Python 2
# 
#         def __unicode__(self):
#             if isinstance(self.msg, six.text_type):
#                 return self.msg
#             elif isinstance(self.msg, str):
#                 return self.msg.decode('utf8')
#             else:
#                 return six.text_type(self.msg)
# 
#     def unpack_errors(self, encode_variables=False, dict_char='.',
#                       list_char='-'):
#         """
#         Returns the error as a simple data structure -- lists,
#         dictionaries, and strings.
# 
#         If ``encode_variables`` is true, then this will return a flat
#         dictionary, encoded with variable_encode
#         """
#         if self.error_list:
#             assert not encode_variables, (
#                 "You can only encode dictionary errors")
#             assert not self.error_dict
#             return [item.unpack_errors() if item else item
#                 for item in self.error_list]
#         if self.error_dict:
#             result = {}
#             for name, item in six.iteritems(self.error_dict):
#                 result[name] = item if isinstance(
#                     item, six.string_types) else item.unpack_errors()
#             if encode_variables:
#                 from . import variabledecode
#                 result = variabledecode.variable_encode(
#                     result, add_repetitions=False,
#                     dict_char=dict_char, list_char=list_char)
#                 for key in list(result.keys()):
#                     if not result[key]:
#                         del result[key]
#             return result
#         assert not encode_variables, (
#             "You can only encode dictionary errors")
#         return self.msg
# 
# Path: src/formencode/compound.py
# class CompoundValidator(FancyValidator):
#     """Base class for all compound validators."""
# 
#     if_invalid = NoDefault
#     accept_iterator = False
# 
#     validators = []
# 
#     __unpackargs__ = ('*', 'validatorArgs')
# 
#     __mutableattributes__ = ('validators',)
# 
#     _deprecated_methods = (
#         ('attempt_convert', '_attempt_convert'),)
# 
#     @staticmethod
#     def __classinit__(cls, new_attrs):
#         FancyValidator.__classinit__(cls, new_attrs)
#         toAdd = []
#         for name, value in six.iteritems(new_attrs):
#             if is_validator(value) and value is not Identity:
#                 toAdd.append((name, value))
#                 # @@: Should we really delete too?
#                 delattr(cls, name)
#         toAdd.sort()
#         cls.validators.extend([value for _name, value in toAdd])
# 
#     def __init__(self, *args, **kw):
#         Validator.__init__(self, *args, **kw)
#         self.validators = self.validators[:]
#         self.validators.extend(self.validatorArgs)
# 
#     @staticmethod
#     def _repr_vars(names):
#         return [n for n in Validator._repr_vars(names)
#                 if n != 'validatorArgs']
# 
#     def _attempt_convert(self, value, state, convertFunc):
#         raise NotImplementedError("Subclasses must implement _attempt_convert")
# 
#     def _convert_to_python(self, value, state=None):
#         return self._attempt_convert(value, state,
#                                     to_python)
# 
#     def _convert_from_python(self, value, state=None):
#         return self._attempt_convert(value, state,
#                                     from_python)
# 
#     def subvalidators(self):
#         return self.validators
# 
# def from_python(validator, value, state):
#     return validator.from_python(value, state)


from .api import NoDefault, Invalid
from .compound import CompoundValidator, from_python
import six
                    state.index = index
                    index += 1
                good_pass = True
                for validator in self.validators:
                    try:
                        sub_value = validate(validator, sub_value, state)
                    except Invalid as e:
                        errors.append(e)
                        all_good = False
                        good_pass = False
                        break
                if good_pass:
                    errors.append(None)
                new_list.append(sub_value)
            if all_good:
                if is_set:
                    new_list = set(new_list)
                return new_list
            else:
                raise Invalid('Errors:\n%s' % '\n'.join(six.text_type(e)
                    for e in errors if e), value, state, error_list=errors)
        finally:
            if state is not None:
                if previous_index is NoDefault:
                    try:
                        del state.index
                    except AttributeError:
                        pass
                else:
                    state.index = previous_index

# Path: src/formencode/api.py
# class NoDefault(object):
#     """A dummy value used for parameters with no default."""
# 
# class Invalid(Exception):
# 
#     """
#     This is raised in response to invalid input.  It has several
#     public attributes:
# 
#     ``msg``:
#         The message, *without* values substituted.  For instance, if
#         you want HTML quoting of values, you can apply that.
#     ``substituteArgs``:
#         The arguments (a dictionary) to go with ``msg``.
#     ``str(self)``:
#         The message describing the error, with values substituted.
#     ``value``:
#         The offending (invalid) value.
#     ``state``:
#         The state that went with this validator.  This is an
#         application-specific object.
#     ``error_list``:
#         If this was a compound validator that takes a repeating value,
#         and sub-validator(s) had errors, then this is a list of those
#         exceptions.  The list will be the same length as the number of
#         values -- valid values will have None instead of an exception.
#     ``error_dict``:
#         Like ``error_list``, but for dictionary compound validators.
#     """
# 
#     def __init__(self, msg,
#                  value, state, error_list=None, error_dict=None):
#         Exception.__init__(self, msg, value, state, error_list, error_dict)
#         self.msg = msg
#         self.value = value
#         self.state = state
#         self.error_list = error_list
#         self.error_dict = error_dict
#         assert (not self.error_list or not self.error_dict), (
#                 "Errors shouldn't have both error dicts and lists "
#                 "(error %s has %s and %s)"
#                 % (self, self.error_list, self.error_dict))
# 
#     def __str__(self):
#         val = self.msg
#         return val
# 
#     if six.text_type is not str:  # Python 2
# 
#         def __unicode__(self):
#             if isinstance(self.msg, six.text_type):
#                 return self.msg
#             elif isinstance(self.msg, str):
#                 return self.msg.decode('utf8')
#             else:
#                 return six.text_type(self.msg)
# 
#     def unpack_errors(self, encode_variables=False, dict_char='.',
#                       list_char='-'):
#         """
#         Returns the error as a simple data structure -- lists,
#         dictionaries, and strings.
# 
#         If ``encode_variables`` is true, then this will return a flat
#         dictionary, encoded with variable_encode
#         """
#         if self.error_list:
#             assert not encode_variables, (
#                 "You can only encode dictionary errors")
#             assert not self.error_dict
#             return [item.unpack_errors() if item else item
#                 for item in self.error_list]
#         if self.error_dict:
#             result = {}
#             for name, item in six.iteritems(self.error_dict):
#                 result[name] = item if isinstance(
#                     item, six.string_types) else item.unpack_errors()
#             if encode_variables:
#                 from . import variabledecode
#                 result = variabledecode.variable_encode(
#                     result, add_repetitions=False,
#                     dict_char=dict_char, list_char=list_char)
#                 for key in list(result.keys()):
#                     if not result[key]:
#                         del result[key]
#             return result
#         assert not encode_variables, (
#             "You can only encode dictionary errors")
#         return self.msg
# 
# Path: src/formencode/compound.py
# class CompoundValidator(FancyValidator):
#     """Base class for all compound validators."""
# 
#     if_invalid = NoDefault
#     accept_iterator = False
# 
#     validators = []
# 
#     __unpackargs__ = ('*', 'validatorArgs')
# 
#     __mutableattributes__ = ('validators',)
# 
#     _deprecated_methods = (
#         ('attempt_convert', '_attempt_convert'),)
# 
#     @staticmethod
#     def __classinit__(cls, new_attrs):
#         FancyValidator.__classinit__(cls, new_attrs)
#         toAdd = []
#         for name, value in six.iteritems(new_attrs):
#             if is_validator(value) and value is not Identity:
#                 toAdd.append((name, value))
#                 # @@: Should we really delete too?
#                 delattr(cls, name)
#         toAdd.sort()
#         cls.validators.extend([value for _name, value in toAdd])
# 
#     def __init__(self, *args, **kw):
#         Validator.__init__(self, *args, **kw)
#         self.validators = self.validators[:]
#         self.validators.extend(self.validatorArgs)
# 
#     @staticmethod
#     def _repr_vars(names):
#         return [n for n in Validator._repr_vars(names)
#                 if n != 'validatorArgs']
# 
#     def _attempt_convert(self, value, state, convertFunc):
#         raise NotImplementedError("Subclasses must implement _attempt_convert")
# 
#     def _convert_to_python(self, value, state=None):
#         return self._attempt_convert(value, state,
#                                     to_python)
# 
#     def _convert_from_python(self, value, state=None):
#         return self._attempt_convert(value, state,
#                                     from_python)
# 
#     def subvalidators(self):
#         return self.validators
# 
# def from_python(validator, value, state):
#     return validator.from_python(value, state)


# Path: src/formencode/foreach.py
from .api import NoDefault, Invalid
from .compound import CompoundValidator, from_python
import six

                    state.index = index
                    index += 1
                good_pass = True
                for validator in self.validators:
                    try:
                        sub_value = validate(validator, sub_value, state)
                    except Invalid as e:
                        errors.append(e)
                        all_good = False
                        good_pass = False
                        break
                if good_pass:
                    errors.append(None)
                new_list.append(sub_value)
            if all_good:
                if is_set:
                    new_list = set(new_list)
                return new_list
            else:
                raise Invalid('Errors:\n%s' % '\n'.join(six.text_type(e)
                    for e in errors if e), value, state, error_list=errors)
        finally:
            if state is not None:
                if previous_index is NoDefault:
                    try:
                        del state.index
                    except AttributeError:
                        pass
                else:
                    state.index = previous_index

                if previous_full_list is NoDefault: