diff options
Diffstat (limited to 'numpy/compat/py3k.py')
-rw-r--r-- | numpy/compat/py3k.py | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/numpy/compat/py3k.py b/numpy/compat/py3k.py index b6113b661..534ae4d29 100644 --- a/numpy/compat/py3k.py +++ b/numpy/compat/py3k.py @@ -4,23 +4,25 @@ Python 3 compatibility tools. """ __all__ = ['bytes', 'asbytes', 'isfileobj', 'getexception', 'strchar', - 'asunicode'] + 'unicode', 'asunicode', 'asbytes_nested', 'asunicode_nested'] import sys if sys.version_info[0] >= 3: import io bytes = bytes + unicode = str + asunicode = str def asbytes(s): if isinstance(s, bytes): return s return s.encode('iso-8859-1') - asunicode = str def isfileobj(f): return isinstance(f, io.IOBase) strchar = 'U' else: bytes = str + unicode = unicode asbytes = str strchar = 'S' def isfileobj(f): @@ -33,3 +35,14 @@ else: def getexception(): return sys.exc_info()[1] +def asbytes_nested(x): + if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)): + return [asbytes_nested(y) for y in x] + else: + return asbytes(x) + +def asunicode_nested(x): + if hasattr(x, '__iter__') and not isinstance(x, (bytes, unicode)): + return [asunicode_nested(y) for y in x] + else: + return asunicode(x) |