diff options
author | Mark Wiebe <mwwiebe@gmail.com> | 2011-09-07 18:22:54 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2011-10-01 09:43:09 -0600 |
commit | f0d8fede3ae93c5e07257d176d275393fc66abd7 (patch) | |
tree | 0c5ed665f9ad2b6d0da5c6659230a82dbe57a60f | |
parent | a1b9a1aa76a76570224161316d7b20e7fc8464a2 (diff) | |
download | numpy-f0d8fede3ae93c5e07257d176d275393fc66abd7.tar.gz |
BUG: datetime: Fix str() function of datetime arrays
Done by making the unit given by datetime_data be a string instead of
bytes in Python 3.
-rw-r--r-- | numpy/core/src/multiarray/datetime.c | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/numpy/core/src/multiarray/datetime.c b/numpy/core/src/multiarray/datetime.c index 2c95ab282..294912821 100644 --- a/numpy/core/src/multiarray/datetime.c +++ b/numpy/core/src/multiarray/datetime.c @@ -1929,7 +1929,7 @@ convert_datetime_metadata_to_tuple(PyArray_DatetimeMetaData *meta) } PyTuple_SET_ITEM(dt_tuple, 0, - PyBytes_FromString(_datetime_strings[meta->base])); + PyUString_FromString(_datetime_strings[meta->base])); PyTuple_SET_ITEM(dt_tuple, 1, PyInt_FromLong(meta->num)); @@ -1948,6 +1948,7 @@ convert_datetime_metadata_tuple_to_datetime_metadata(PyObject *tuple, char *basestr = NULL; Py_ssize_t len = 0, tuple_size; int den = 1; + PyObject *unit_str = NULL; if (!PyTuple_Check(tuple)) { PyObject_Print(tuple, stderr, 0); @@ -1965,16 +1966,30 @@ convert_datetime_metadata_tuple_to_datetime_metadata(PyObject *tuple, return -1; } - if (PyBytes_AsStringAndSize(PyTuple_GET_ITEM(tuple, 0), - &basestr, &len) < 0) { + unit_str = PyTuple_GET_ITEM(tuple, 0); + Py_INCREF(unit_str); + if (PyUnicode_Check(unit_str)) { + /* Allow unicode format strings: convert to bytes */ + PyObject *tmp = PyUnicode_AsASCIIString(unit_str); + Py_DECREF(unit_str); + if (tmp == NULL) { + return -1; + } + unit_str = tmp; + } + if (PyBytes_AsStringAndSize(unit_str, &basestr, &len) < 0) { + Py_DECREF(unit_str); return -1; } out_meta->base = parse_datetime_unit_from_string(basestr, len, NULL); if (out_meta->base == -1) { + Py_DECREF(unit_str); return -1; } + Py_DECREF(unit_str); + /* Convert the values to longs */ out_meta->num = PyInt_AsLong(PyTuple_GET_ITEM(tuple, 1)); if (out_meta->num == -1 && PyErr_Occurred()) { |