Skip to content

Commit

Permalink
BUG: Fix numpy#1388. Return NULL after setting error. Also simplify c…
Browse files Browse the repository at this point in the history
…typeslib prep_simple function.
  • Loading branch information
teoliphant committed Feb 4, 2010
1 parent 1473800 commit 58b5fb9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
7 changes: 5 additions & 2 deletions numpy/core/src/multiarray/ctors.c
Original file line number Diff line number Diff line change
Expand Up @@ -3121,8 +3121,11 @@ PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type,

if ((offset < 0) || (offset >= ts)) {
PyErr_Format(PyExc_ValueError,
"offset must be positive and smaller than %"
INTP_FMT, (intp)ts);
"offset must be non-negative and smaller than buffer "\
"lenth (%" INTP_FMT ")", (intp)ts);
Py_DECREF(buf);
Py_DECREF(type);
return NULL;
}

data += offset;
Expand Down
12 changes: 4 additions & 8 deletions numpy/ctypeslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,15 @@ def ndpointer(dtype=None, ndim=None, shape=None, flags=None):
# c_double. Filled in by prep_simple.
_typecodes = {}

def prep_simple(simple_type, typestr):
def prep_simple(simple_type, dtype):
"""Given a ctypes simple type, construct and attach an
__array_interface__ property to it if it does not yet have one.
"""
try: simple_type.__array_interface__
except AttributeError: pass
else: return


typestr = _dtype(dtype).str
_typecodes[typestr] = simple_type

def __array_interface__(self):
Expand All @@ -316,11 +317,6 @@ def __array_interface__(self):

simple_type.__array_interface__ = property(__array_interface__)

if sys.byteorder == "little":
TYPESTR = "<%c%d"
else:
TYPESTR = ">%c%d"

simple_types = [
((ct.c_byte, ct.c_short, ct.c_int, ct.c_long, ct.c_longlong), "i"),
((ct.c_ubyte, ct.c_ushort, ct.c_uint, ct.c_ulong, ct.c_ulonglong), "u"),
Expand All @@ -330,7 +326,7 @@ def __array_interface__(self):
# Prep that numerical ctypes types:
for types, code in simple_types:
for tp in types:
prep_simple(tp, TYPESTR % (code, ct.sizeof(tp)))
prep_simple(tp, "%c%d" % (code, ct.sizeof(tp)))

################################################################
# array types
Expand Down

0 comments on commit 58b5fb9

Please sign in to comment.