<br><br><div class="gmail_quote">On Sun, Dec 4, 2011 at 6:30 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;">
<div class="HOEnZb"><div class="h5">On Sun, Dec 4, 2011 at 10:02 AM, Charles R Harris<br>
<<a href="mailto:charlesr.harris@gmail.com">charlesr.harris@gmail.com</a>> wrote:<br>
><br>
><br>
> On Sat, Dec 3, 2011 at 5:28 PM, Geoffrey Irving <<a href="mailto:irving@naml.us">irving@naml.us</a>> wrote:<br>
>><br>
>> 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>
>> __<br>
><br>
><br>
> I'm thinking NpyCapsule_Check should catch this. From the documentation it<br>
> probably should:<br>
><br>
> int PyCObject_Check(PyObject *p)<br>
> Return true if its argument is a PyCObject<br>
><br>
> I don't think NULL is a valid PyCObject ;) However, it should be easy to add<br>
> the NULL check to the numpy version of the function. I'll do that.<br>
<br>
</div></div>That would work, but I think would match the rest of the Python API<br>
better if NpyCapsule_Check required a nonnull argument.<br>
PyCapsule_Check and essentially every other Python API function have<br>
documented undefined behavior if you pass in null, so it might be<br>
surprising one numpy macro violates this.  Incidentally, every other<br>
use of NpyCapsule_Check correctly tests for null.<br>
<span class="HOEnZb"><font color="#888888"><br></font></span></blockquote><div><br>Good points. I may change it back ;)<br><br>Chuck <br></div><br></div>