summaryrefslogtreecommitdiff
path: root/numpy/core/src/arrayobject.c
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2006-02-11 10:31:32 +0000
committerTravis Oliphant <oliphant@enthought.com>2006-02-11 10:31:32 +0000
commit8ee6df7668b3ae0568573620153e71e36c9c399a (patch)
treeaf89f3e25fda05fff5de0dfdb98f31ae94f9957e /numpy/core/src/arrayobject.c
parent6fc0e44e5daab7dcaae4a3250b7545a263d6bfcb (diff)
downloadnumpy-8ee6df7668b3ae0568573620153e71e36c9c399a.tar.gz
Fixed UNICODE functions to handle misaligned and/or byte-swapped arrays.
Diffstat (limited to 'numpy/core/src/arrayobject.c')
-rw-r--r--numpy/core/src/arrayobject.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c
index 76be69702..3e996d338 100644
--- a/numpy/core/src/arrayobject.c
+++ b/numpy/core/src/arrayobject.c
@@ -868,8 +868,9 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
else if (type_num == PyArray_UNICODE) {
PyUnicodeObject *uni = (PyUnicodeObject*)obj;
int length = itemsize >> 2;
-
#ifndef Py_UNICODE_WIDE
+ char *buffer;
+ int alloc=1;
length *= 2;
#endif
/* Need an extra slot and need to use
@@ -887,15 +888,25 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base)
uni->hash = -1;
uni->defenc = NULL;
#ifndef Py_UNICODE_WIDE
+ /* need aligned data buffer */
+ if (!PyArray_ISBEHAVED(base)) {
+ buffer = _pya_malloc(itemsize);
+ if (buffer == NULL)
+ return PyErr_NoMemory();
+ alloc = 1;
+ memcpy(buffer, data, itemsize);
+ if (!PyArray_ISNOTSWAPPED(base)) {
+ byte_swap_vector(buffer, itemsize >> 2, 4);
+ }
+ }
+ else buffer = data;
+
/* Allocated enough for 2-characters per itemsize.
Now convert from the data-buffer
*/
- if (!PyArray_ISNBO(descr->byteorder)) {
- /* byteswap the data */
- byte_swap_vector(data, itemsize >> 2, 4);
- }
- length = PyUCS2Buffer_FromUCS4(uni->str, (PyArray_UCS4 *)data,
+ length = PyUCS2Buffer_FromUCS4(uni->str, (PyArray_UCS4 *)buffer,
itemsize >> 2);
+ if (alloc) _pya_free(buffer);
/* Resize the unicode result */
if (MyPyUnicode_Resize(uni, length) < 0) {
Py_DECREF(obj);