From 91f87e1f613630ff0ad9864017f059afcd6e57f1 Mon Sep 17 00:00:00 2001 From: Bryan Van de Ven Date: Mon, 20 Feb 2012 11:32:57 -0600 Subject: BUG: Fix ticket #1990. When an array is created from a sequence of numeric (boolean, int, float, complex) and string (bytes, str, unicode) values, the resulting array type is string, but only the string values were being used to choose the string length, leading to truncation of data. --- numpy/compat/py3k.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'numpy/compat/py3k.py') diff --git a/numpy/compat/py3k.py b/numpy/compat/py3k.py index 001455de5..0a03929be 100644 --- a/numpy/compat/py3k.py +++ b/numpy/compat/py3k.py @@ -13,32 +13,45 @@ if sys.version_info[0] >= 3: import io bytes = bytes unicode = str - asunicode = str + + def asunicode(s): + if isinstance(s, bytes): + return s.decode('latin1') + return str(s) + def asbytes(s): if isinstance(s, bytes): return s - return s.encode('latin1') + return str(s).encode('latin1') + def asstr(s): - if isinstance(s, str): - return s - return s.decode('latin1') + if isinstance(s, bytes): + return s.decode('latin1') + return str(s) + def isfileobj(f): return isinstance(f, (io.FileIO, io.BufferedReader)) + def open_latin1(filename, mode='r'): return open(filename, mode=mode, encoding='iso-8859-1') + strchar = 'U' + else: bytes = str unicode = unicode asbytes = str asstr = str strchar = 'S' + def isfileobj(f): return isinstance(f, file) + def asunicode(s): if isinstance(s, unicode): return s - return s.decode('ascii') + return str(s).decode('ascii') + def open_latin1(filename, mode='r'): return open(filename, mode=mode) -- cgit v1.2.1