<br><br><div class="gmail_quote">On Sat, Dec 3, 2011 at 5:28 PM, Geoffrey Irving <span dir="ltr"><<a href="mailto:irving@naml.us">irving@naml.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
When attempting to cast to a user defined type, PyArray_GetCast looks<br>
up the cast function in the dictionary but doesn't check if the entry<br>
exists.  This causes segfaults.  Here's a patch.<br>
<br>
Geoffrey<br>
<br>
diff --git a/numpy/core/src/multiarray/convert_datatype.c<br>
b/numpy/core/src/multiarray/convert_datatype.c<br>
index 818d558..4b8f38b 100644<br>
--- a/numpy/core/src/multiarray/convert_datatype.c<br>
+++ b/numpy/core/src/multiarray/convert_datatype.c<br>
@@ -81,7 +81,7 @@ PyArray_GetCastFunc(PyArray_Descr *descr, int type_num)<br>
             key = PyInt_FromLong(type_num);<br>
             cobj = PyDict_GetItem(obj, key);<br>
             Py_DECREF(key);<br>
-            if (NpyCapsule_Check(cobj)) {<br>
+            if (cobj && NpyCapsule_Check(cobj)) {<br>
                 castfunc = NpyCapsule_AsVoidPtr(cobj);<br>
             }<br>
         }<br>
__</blockquote><div><br>I'm thinking NpyCapsule_Check should catch this. From the documentation it probably should:<br><br>int <tt class="descname">PyCObject_Check</tt><big>(</big><a title="PyObject" class="reference external" href="http://docs.python.org/release/2.7/c-api/structures.html#PyObject">PyObject</a><em> *p</em><big>)</big><br>
Return true if its argument is a <a title="PyCObject" class="reference internal" href="http://docs.python.org/release/2.7/c-api/cobject.html#PyCObject"><tt class="xref docutils literal"><span class="pre">PyCObject</span></tt></a> <br>
<br>I don't think NULL is a valid PyCObject ;) However, it should be easy to add the NULL check to the numpy version of the function. I'll do that.<br><br>Chuck <br></div><br></div>