Skip to content

Commit

Permalink
Added support for the __index__ protocol.
Browse files Browse the repository at this point in the history
PyNIO now works with __index__, so that numpy integers can be used as indexes. Also fixed issues in setup.py that were
causing opendap and netcdf4 to not be linked properly. Fixes NCAR#15.
  • Loading branch information
Bill Ladwig committed Apr 4, 2018
1 parent 05806a2 commit a220065
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 13 deletions.
31 changes: 31 additions & 0 deletions build_py2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

export HAS_NETCDF4=1
export HAS_HDFEOS=1
export HAS_HDFEOS5=1
export HAS_GDAL=1
export HAS_GRIB2=1
export NETCDF_PREFIX=${CONDA_PREFIX}
export F2CLIBS=gfortran
export HAS_SZIP=0
export HAS_HDF4=1
export HAS_HDF5=1
export HAS_GDAL=1

export CXXFLAGS="-fPIC $CXXFLAGS"
export LDFLAGS="-L$CONDA_PREFIX/lib $LDFLAGS"
export CPPFLAGS="-I$CONDA_PREFIX/include $CPPFLAGS"
export CFLAGS="-D_BSD_SOURCE -D_XOPEN_SOURCE -I$PREFIX/include $CFLAGS"

if [[ $(uname) == Darwin ]]; then
export CC=gcc
export CXX=g++
export MACOSX_DEPLOYMENT_TARGET="10.9"
export CXXFLAGS="-stdlib=libc++ $CXXFLAGS"
export CXXFLAGS="$CXXFLAGS -stdlib=libc++"
export LDFLAGS="-headerpad_max_install_names $LDFLAGS"
fi

python setup.py build
pip install .

34 changes: 34 additions & 0 deletions build_py3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

export HAS_NETCDF4=1
export HAS_HDFEOS=1
export HAS_HDFEOS5=1
export HAS_GDAL=1
export HAS_GRIB2=1
export NETCDF_PREFIX=${CONDA_PREFIX}
export F2CLIBS=gfortran
export HAS_SZIP=0
export HAS_HDF4=1
export HAS_HDF5=1
export HAS_GDAL=1

export CXXFLAGS="-g -O0 -fPIC $CXXFLAGS"
export LDFLAGS="-L$CONDA_PREFIX/lib $LDFLAGS"
export CPPFLAGS="-I$CONDA_PREFIX/include $CPPFLAGS"
export CFLAGS="-D_BSD_SOURCE -D_XOPEN_SOURCE -I$CONDA_PREFIX/include $CFLAGS"

if [[ $(uname) == Darwin ]]; then
export CC=gcc
export CXX=g++
export MACOSX_DEPLOYMENT_TARGET="10.9"
export CXXFLAGS="-stdlib=libc++ $CXXFLAGS"
export CXXFLAGS="$CXXFLAGS -stdlib=libc++"
export LDFLAGS="-headerpad_max_install_names $LDFLAGS"
fi

env

python setup.py build

pip install .

73 changes: 69 additions & 4 deletions niomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,9 @@ NioFileObject_new_dimension(NioFileObject *self, PyObject *args) {
#endif
else if (PyLong_Check(size_ob))
size = (Py_ssize_t) PyLong_AsSsize_t(size_ob);
else if (PyIndex_Check(size_ob)) {
size = PyNumber_AsSsize_t(size_ob, PyExc_OverflowError);
}
else {
PyErr_SetString(PyExc_TypeError, "size must be None or integer");
return NULL;
Expand Down Expand Up @@ -1933,6 +1936,9 @@ static PyObject *NioFileObject_new_chunk_dimension(NioFileObject *self,
#endif
else if (PyLong_Check(size_ob))
size = (Py_ssize_t) PyLong_AsSsize_t(size_ob);
else if (PyIndex_Check(size_ob)) {
size = PyNumber_AsSsize_t(size_ob, PyExc_OverflowError);
}
else {
PyErr_SetString(PyExc_TypeError, "size must be None or integer");
return NULL;
Expand Down Expand Up @@ -2884,6 +2890,9 @@ static PyObject *NioFileObject_new_compound_type(NioFileObject *self,
memb_sizes[i] = (int) PyInt_AsLong(item2);
}
#endif
else if (PyIndex_Check(item2)) {
memb_sizes[i] = (int) PyNumber_AsSsize_t(item2, PyExc_OverflowError);
}
else if (is_string_type(item2)) {
typestr = as_utf8_char(item2);
sscanf(typestr, "%d", &(memb_sizes[i]));
Expand Down Expand Up @@ -3069,6 +3078,9 @@ static PyObject *NioFileObject_new_compound(NioFileObject *self, PyObject *args)
memb_sizes[i] = (int) PyInt_AsLong(item2);
}
#endif
else if (PyIndex_Check(item2)) {
memb_sizes[i] = (int) PyNumber_AsSsize_t(item2, PyExc_OverflowError);
}
else if (is_string_type(item2)) {
typestr = as_utf8_char(item2);
sscanf(typestr, "%d", &(memb_sizes[i]));
Expand Down Expand Up @@ -6376,6 +6388,11 @@ NioVariableObject_subscript(NioVariableObject *self, PyObject *index) {
return NioVariableObject_item(self, i);
}
#endif
else if (PyIndex_Check(index)) {
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_OverflowError);
return NioVariableObject_item(self, i);
}

if (self->nd == 0) {
PyErr_SetString(PyExc_TypeError, "Not a sequence");
return NULL;
Expand Down Expand Up @@ -6442,6 +6459,21 @@ NioVariableObject_subscript(NioVariableObject *self, PyObject *index) {
d++;
}
#endif
else if (PyIndex_Check(subscript)) {
Py_ssize_t n = PyNumber_AsSsize_t(subscript, PyExc_OverflowError);
if (n >= self->dimensions[d]
|| n < -self->dimensions[d]) {
PyErr_Format(PyExc_IndexError,
"index %d is out of bounds for axis %d with size %ld",
(int) n, d, self->dimensions[d]);
free(indices);
return NULL;
}
indices[d].start = n;
indices[d].stop = n + 1;
indices[d].item = 1;
d++;
}
else if (PySlice_Check(subscript)) {
Py_ssize_t slicelen;
PySliceObject *slice = (PySliceObject *) subscript;
Expand Down Expand Up @@ -6547,6 +6579,11 @@ static int NioVariableObject_ass_subscript(NioVariableObject *self,
return NioVariableObject_ass_item(self, i, value);
}
#endif
else if (PyIndex_Check(index)) {
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_OverflowError);
return NioVariableObject_ass_item(self, i, value);
}

if (value == NULL) {
PyErr_SetString(PyExc_ValueError, "Can't delete elements.");
return -1;
Expand All @@ -6563,7 +6600,7 @@ static int NioVariableObject_ass_subscript(NioVariableObject *self,
#if PY_MAJOR_VERSION < 3
if (PySlice_GetIndicesEx((PySliceObject *) index,
#else
if (PySlice_GetIndicesEx(index,
if (PySlice_GetIndicesEx(index,
#endif
self->dimensions[0], &indices->start, &indices->stop,
&indices->stride, &slicelen) < 0) {
Expand All @@ -6585,6 +6622,9 @@ static int NioVariableObject_ass_subscript(NioVariableObject *self,
indices->start = PyInt_AsLong(slice->start);
}
#endif
else if (PyIndex_Check(slice->start)) {
indices->start = PyNumber_AsSsize_t(slice->start, PyExc_OverflowError);
}
if (indices->start < PY_SSIZE_T_MIN * 100)
indices->no_start = 1;
}
Expand All @@ -6595,10 +6635,14 @@ static int NioVariableObject_ass_subscript(NioVariableObject *self,
indices->stop = PyLong_AsLong(slice->stop);
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(slice->stop)) {
else if (PyInt_Check(slice->stop)) {
indices->stop = PyInt_AsLong(slice->stop);
}
#endif
else if (PyIndex_Check(slice->stop)) {
indices->stop = PyNumber_AsSsize_t(slice->stop, PyExc_OverflowError);
}

if (indices->stop > PY_SSIZE_T_MAX / 100)
indices->no_stop = 1;
}
Expand Down Expand Up @@ -6627,13 +6671,20 @@ static int NioVariableObject_ass_subscript(NioVariableObject *self,
d++;
}
#endif
else if (PyIndex_Check(subscript)) {
Py_ssize_t n = PyNumber_AsSsize_t(subscript, PyExc_OverflowError);
indices[d].start = n;
indices[d].stop = n + 1;
indices[d].item = 1;
d++;
}
else if (PySlice_Check(subscript)) {
Py_ssize_t slicelen;
PySliceObject *slice = (PySliceObject *) subscript;
#if PY_MAJOR_VERSION < 3
if (PySlice_GetIndicesEx((PySliceObject *) subscript,
#else
if (PySlice_GetIndicesEx(subscript,
if (PySlice_GetIndicesEx(subscript,
#endif
self->dimensions[d], &indices[d].start,
&indices[d].stop, &indices[d].stride, &slicelen)
Expand All @@ -6653,10 +6704,13 @@ static int NioVariableObject_ass_subscript(NioVariableObject *self,
indices[d].start = PyLong_AsLong(slice->start);
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(slice->start)) {
else if (PyInt_Check(slice->start)) {
indices[d].start = PyInt_AsLong(slice->start);
}
#endif
else if (PyIndex_Check(slice->start)) {
indices[d].start = PyNumber_AsSsize_t(slice->start, PyExc_OverflowError);
}
}
if (slice->stop == Py_None)
indices[d].no_stop = 1;
Expand All @@ -6669,6 +6723,9 @@ static int NioVariableObject_ass_subscript(NioVariableObject *self,
indices[d].stop = PyInt_AsLong(slice->stop);
}
#endif
else if (PyIndex_Check(slice->stop)) {
indices[d].stop = PyNumber_AsSsize_t(slice->stop, PyExc_OverflowError);
}
}
d++;
} else if (subscript == Py_Ellipsis) {
Expand Down Expand Up @@ -7295,6 +7352,14 @@ void SetNioOptions(NrmQuark extq, int mode, PyObject *options,
/* printf("%s %ld\n",keystr,PyLong_AsLong(value));*/
}
#endif
else if (PyIndex_Check(value)) {
int* ival = (int *) malloc(sizeof(int));
*ival = (int) PyNumber_AsSsize_t(value, PyExc_OverflowError);
md = _NclCreateMultiDVal(NULL, NULL, Ncl_MultiDValData, 0,
(void*) ival, NULL, 1, &len_dims, TEMPORARY, NULL,
(NclTypeClass) nclTypeintClass);
/* printf("%s %ld\n",keystr,PyLong_AsLong(value));*/
}
else if (PyFloat_Check(value)) {
float *fval = (float *) malloc(sizeof(float));
*fval = (float) PyFloat_AsDouble(value);
Expand Down
16 changes: 8 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# may be required. See the following comments.
#
from __future__ import print_function
from sys import version_info
import os
import sys
import subprocess
Expand Down Expand Up @@ -88,12 +89,14 @@
def set_curl_libs():
#curl_libs = commands.getstatusoutput('curl-config --libs')

p = subprocess.Popen(["curl-config ", "--libs"], stdout=PIPE, stderr=STDOUT)
p = subprocess.Popen(["curl-config", "--libs"], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
output, _ = p.communicate()

if p.returncode == 0:

output = output.decode("utf-8")

if version_info >= (3,):
output = output.decode("utf-8")
#
# Split into individual lines so we can loop through each one.
#
Expand All @@ -102,9 +105,9 @@ def set_curl_libs():
# Check if this is a -L or -l string and do the appropriate thing.
#
for clibstr in clibs:
if bytes(clibstr[0:2]) == "-L":
if clibstr[0:2] == "-L":
LIB_DIRS.append(clibstr.split("-L")[1])
elif bytes(clibstr[0:2]) == "-l":
elif clibstr[0:2] == "-l":
LIBRARIES.append(clibstr.split("-l")[1])
else:
#
Expand Down Expand Up @@ -482,9 +485,6 @@ def configuration(parent_package='',top_path=None):
for file in LIB_EXCLUDE_SOURCES:
sources.remove(file)
sources = [ join('libsrc', file) for file in sources ]
#print (len(sources))
#print (sources)
#raise RuntimeError("out")
config.add_library('nio',sources,
include_dirs=INC_DIRS,
macros=LIB_MACROS,
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.5.0
1.5.1

0 comments on commit a220065

Please sign in to comment.