summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Bourque <jay.bourque@continuum.io>2013-04-25 13:38:16 -0500
committerJay Bourque <jay.bourque@continuum.io>2013-04-25 13:38:16 -0500
commitc6df6b1071fb1077dc0e116cdcd20a604221a9c6 (patch)
tree7769273804944c303d98562140e3e33d9af12dde
parentf231558bab5be194f3a7e57ba4c9738056d3a04d (diff)
downloadnumpy-c6df6b1071fb1077dc0e116cdcd20a604221a9c6.tar.gz
Add support for unicode strings
-rw-r--r--numpy/core/src/multiarray/convert_datatype.c35
-rw-r--r--numpy/core/tests/test_api.py7
2 files changed, 29 insertions, 13 deletions
diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c
index 457844618..cb1caec08 100644
--- a/numpy/core/src/multiarray/convert_datatype.c
+++ b/numpy/core/src/multiarray/convert_datatype.c
@@ -226,22 +226,33 @@ PyArray_AdaptFlexibleDType(PyObject *data_obj, PyArray_Descr *data_dtype,
break;
case NPY_OBJECT:
size = 64;
- /* If we're adapting a string dtype for an array of string
- objects, call GetArrayParamsFromObject to figure out
- maximum string size, and use that as new dtype size. */
- if (flex_type_num == NPY_STRING && data_obj != NULL) {
- /* Convert data array to list of objects since
- GetArrayParamsFromObject won't iterator through
- items in an array. */
+ /*
+ * If we're adapting a string dtype for an array of string
+ * objects, call GetArrayParamsFromObject to figure out
+ * maximum string size, and use that as new dtype size.
+ */
+ if ((flex_type_num == NPY_STRING ||
+ flex_type_num == NPY_UNICODE) &&
+ data_obj != NULL) {
+ /*
+ * Convert data array to list of objects since
+ * GetArrayParamsFromObject won't iterator through
+ * items in an array.
+ */
list = PyArray_ToList(data_obj);
if (list != NULL) {
result = PyArray_GetArrayParamsFromObject(
- list,
- flex_dtype,
- 0, &dtype,
- &ndim, dims, &arr, NULL);
+ list,
+ flex_dtype,
+ 0, &dtype,
+ &ndim, dims, &arr, NULL);
if (result == 0 && dtype != NULL) {
- size = dtype->elsize;
+ if (flex_type_num == NPY_UNICODE) {
+ size = dtype->elsize / 4;
+ }
+ else {
+ size = dtype->elsize;
+ }
}
Py_DECREF(list);
}
diff --git a/numpy/core/tests/test_api.py b/numpy/core/tests/test_api.py
index 388c23d3a..ce4658fd4 100644
--- a/numpy/core/tests/test_api.py
+++ b/numpy/core/tests/test_api.py
@@ -6,6 +6,7 @@ import numpy as np
from numpy.testing import *
from numpy.testing.utils import WarningManager
import warnings
+from numpy.compat import sixu
# Switch between new behaviour when NPY_RELAXED_STRIDES_CHECKING is set.
NPY_RELAXED_STRIDES_CHECKING = np.ones((10,1), order='C').flags.f_contiguous
@@ -91,10 +92,14 @@ def test_array_astype():
# Make sure converting from string object to fixed length string
# does not truncate.
- a = np.array(['a'*100], dtype='O')
+ a = np.array([b'a'*100], dtype='O')
b = a.astype('S')
assert_equal(a, b)
assert_equal(b.dtype, np.dtype('S100'))
+ a = np.array([sixu('a')*100], dtype='O')
+ b = a.astype('U')
+ assert_equal(a, b)
+ assert_equal(b.dtype, np.dtype('U100'))
def test_copyto_fromscalar():
a = np.arange(6, dtype='f4').reshape(2,3)