borrow is_nonstr_iter from pyramid and use it instead of simple isinstance check

This commit is contained in:
Chris McDonough
2012-02-07 21:59:42 -05:00
parent 0fd52a4c23
commit cb10f1404b
2 changed files with 13 additions and 3 deletions

View File

@@ -11,6 +11,7 @@ from .compat import (
text_type,
string_types,
xrange,
is_nonstr_iter,
)
from . import iso8601
@@ -75,9 +76,9 @@ class Invalid(Exception):
``msg`` attribute is iterable, it is returned. If it is not
iterable, a single-element list containing the ``msg`` value
is returned."""
if isinstance(self.msg, string_types):
return [self.msg]
return self.msg
if is_nonstr_iter(self.msg):
return self.msg
return [self.msg]
def add(self, exc, pos=None):
""" Add a child exception; ``exc`` must be an instance of

View File

@@ -16,6 +16,15 @@ def text_(s, encoding='latin-1', errors='strict'):
return s.decode(encoding, errors)
return s # pragma: no cover
if PY3: # pragma: no cover
def is_nonstr_iter(v):
if isinstance(v, str):
return False
return hasattr(v, '__iter__')
else:
def is_nonstr_iter(v):
return hasattr(v, '__iter__')
try:
xrange = xrange
except NameError: # pragma: no cover