Skip to content

Datetime cleanup (mostly for Python 3) #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2005-2009, NumPy Developers.
Copyright (c) 2005-2011, NumPy Developers.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/code_generators/generate_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ def english_upper(s):
TD(intfltcmplx),
[TypeDescription('m', FullTypeDescr, 'mq', 'm'),
TypeDescription('m', FullTypeDescr, 'md', 'm'),
#TypeDescription('m', FullTypeDescr, 'mm', 'd'),
],
TD(O, f='PyNumber_FloorDivide'),
),
Expand All @@ -299,6 +300,7 @@ def english_upper(s):
TD(flts+cmplx),
[TypeDescription('m', FullTypeDescr, 'mq', 'm'),
TypeDescription('m', FullTypeDescr, 'md', 'm'),
TypeDescription('m', FullTypeDescr, 'mm', 'd'),
],
TD(O, f='PyNumber_TrueDivide'),
),
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/include/numpy/npy_3kcompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#if PY_VERSION_HEX >= 0x03000000
#ifndef NPY_PY3K
#define NPY_PY3K
#define NPY_PY3K 1
#endif
#endif

Expand Down
7 changes: 1 addition & 6 deletions numpy/core/src/multiarray/arrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,9 @@ PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object)

/* If there's one dest element and src is a Python scalar */
if (PyArray_IsScalar(src_object, Generic)) {
PyArray_Descr *dtype;
char *value;
int retcode;

dtype = PyArray_DescrFromScalar(src_object);
if (dtype == NULL) {
Py_DECREF(src_object);
return -1;
}
value = scalar_value(src_object, dtype);
if (value == NULL) {
Py_DECREF(dtype);
Expand All @@ -235,6 +229,7 @@ PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object)
/* Assigning NA affects the mask if it exists */
else if (na != NULL) {
if (PyArray_AssignNA(dest, na, NULL, 0, NULL) < 0) {
Py_DECREF(dtype);
Py_DECREF(na);
Py_DECREF(src_object);
return -1;
Expand Down
3 changes: 1 addition & 2 deletions numpy/core/src/multiarray/arraytypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,6 @@ VOID_getitem(char *ip, PyArrayObject *ap)
*/
{
intp dims[1], strides[1];
PyArray_Descr *descr;
dims[0] = itemsize;
strides[0] = 1;
descr = PyArray_DescrNewFromType(NPY_BYTE);
Expand All @@ -672,7 +671,7 @@ VOID_getitem(char *ip, PyArrayObject *ap)
PyArray_ISWRITEABLE(ap) ? NPY_ARRAY_WRITEABLE : 0,
NULL);
Py_INCREF(ap);
if (PyArray_SetBaseObject(u, ap) < 0) {
if (PyArray_SetBaseObject(u, (PyObject *)ap) < 0) {
Py_DECREF(u);
return NULL;
}
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/src/multiarray/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

/* removed multiple segment interface */

#if !defined(NPY_PY3K)
static Py_ssize_t
array_getsegcount(PyArrayObject *self, Py_ssize_t *lenp)
{
Expand Down Expand Up @@ -72,6 +73,7 @@ array_getcharbuf(PyArrayObject *self, Py_ssize_t segment, constchar **ptrptr)
{
return array_getreadbuf(self, segment, (void **) ptrptr);
}
#endif /* !defined(NPY_PY3K) */


/*************************************************************************
Expand Down
16 changes: 8 additions & 8 deletions numpy/core/src/multiarray/calculation.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out)
PyArrayObject *ap = NULL, *rp = NULL;
PyArray_ArgFunc* arg_func;
char *ip;
intp *rptr;
intp i, n, m;
npy_intp *rptr;
npy_intp i, n, m;
int elsize;
NPY_BEGIN_THREADS_DEF;

Expand All @@ -56,13 +56,13 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out)
*/
if (axis != PyArray_NDIM(ap)-1) {
PyArray_Dims newaxes;
intp dims[MAX_DIMS];
int i;
npy_intp dims[MAX_DIMS];
int j;

newaxes.ptr = dims;
newaxes.len = PyArray_NDIM(ap);
for (i = 0; i < axis; i++) dims[i] = i;
for (i = axis; i < PyArray_NDIM(ap) - 1; i++) dims[i] = i + 1;
for (j = 0; j < axis; j++) dims[j] = j;
for (j = axis; j < PyArray_NDIM(ap) - 1; j++) dims[j] = j + 1;
dims[PyArray_NDIM(ap) - 1] = axis;
op = (PyArrayObject *)PyArray_Transpose(ap, &newaxes);
Py_DECREF(ap);
Expand Down Expand Up @@ -120,7 +120,7 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out)

NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(ap));
n = PyArray_SIZE(ap)/m;
rptr = (intp *)PyArray_DATA(rp);
rptr = (npy_intp *)PyArray_DATA(rp);
for (ip = PyArray_DATA(ap), i = 0; i < n; i++, ip += elsize*m) {
arg_func(ip, m, rptr, ap);
rptr += 1;
Expand Down Expand Up @@ -273,7 +273,7 @@ __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out,
PyArrayObject *arr1 = NULL, *arr2 = NULL, *arrnew = NULL;
PyObject *ret = NULL, *newshape = NULL;
int i, n;
intp val;
npy_intp val;

arrnew = (PyArrayObject *)PyArray_CheckAxis(self, &axis, NPY_ARRAY_ALLOWNA);
if (arrnew == NULL) {
Expand Down
2 changes: 2 additions & 0 deletions numpy/core/src/multiarray/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ _array_find_python_scalar_type(PyObject *op)
return NULL;
}

#if !defined(NPY_PY3K)
static PyArray_Descr *
_use_default_type(PyObject *op)
{
Expand All @@ -65,6 +66,7 @@ _use_default_type(PyObject *op)
}
return PyArray_DescrFromType(typenum);
}
#endif

/*
* Recursively examines the object to determine an appropriate dtype
Expand Down
8 changes: 4 additions & 4 deletions numpy/core/src/multiarray/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format)
NPY_NO_EXPORT PyObject *
PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
{
intp numbytes;
intp index;
npy_intp numbytes;
npy_intp i;
char *dptr;
int elsize;
PyObject *ret;
Expand Down Expand Up @@ -293,9 +293,9 @@ PyArray_ToString(PyArrayObject *self, NPY_ORDER order)
return NULL;
}
dptr = PyBytes_AS_STRING(ret);
index = it->size;
i = it->size;
elsize = PyArray_DESCR(self)->elsize;
while (index--) {
while (i--) {
memcpy(dptr, it->dataptr, elsize);
dptr += elsize;
PyArray_ITER_NEXT(it);
Expand Down
25 changes: 20 additions & 5 deletions numpy/core/src/multiarray/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -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);
Expand All @@ -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()) {
Expand Down Expand Up @@ -2441,7 +2456,7 @@ convert_pydatetime_to_datetimestruct(PyObject *obj, npy_datetimestruct *out,
* the Python datetime.tzinfo object.
*/
NPY_NO_EXPORT int
get_tzoffset_from_pytzinfo(PyObject *timezone, npy_datetimestruct *dts)
get_tzoffset_from_pytzinfo(PyObject *timezone_obj, npy_datetimestruct *dts)
{
PyObject *dt, *loc_dt;
npy_datetimestruct loc_dts;
Expand All @@ -2454,7 +2469,7 @@ get_tzoffset_from_pytzinfo(PyObject *timezone, npy_datetimestruct *dts)
}

/* Convert the datetime from UTC to local time */
loc_dt = PyObject_CallMethod(timezone, "fromutc", "O", dt);
loc_dt = PyObject_CallMethod(timezone_obj, "fromutc", "O", dt);
Py_DECREF(dt);
if (loc_dt == NULL) {
return -1;
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/src/multiarray/datetime_busdaycal.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ PyArray_WeekMaskConverter(PyObject *weekmask_in, npy_bool *weekmask)
static int
qsort_datetime_compare(const void *elem1, const void *elem2)
{
npy_datetime e1 = *(npy_datetime *)elem1;
npy_datetime e2 = *(npy_datetime *)elem2;
npy_datetime e1 = *(const npy_datetime *)elem1;
npy_datetime e2 = *(const npy_datetime *)elem2;

return (e1 < e2) ? -1 : (e1 == e2) ? 0 : 1;
}
Expand Down
Loading