[Tutor] Python Extensions in C

James Reynolds eire1130 at gmail.com
Wed Jun 1 21:49:44 CEST 2011


So I've been continuing with my experiment above, and I've made some good
progress and learned some new things as I've been going. I've expanded it
out a little bit, and you can see the code here:

http://pastebin.com/gnW4xQNv

<http://pastebin.com/iCNJVyKr>I've tried to incorporate most of your
suggestions (although, the code itself looks more condensed in my editor
than what it appears there).

Unfortunately, I am stuck and I have no clue how to free myself from the
mud.


On line 370, you will notice I have "*return_varp" as the return value and
not "skew". This is because I am trying to figure out why the variance
function is not working properly from this function call.

Earlier today it was working fine, so I know it works at least in theory.

When variance is called from stats.var it works just fine and the output is
as expected.

so for example,

a = [1,2,3,4,4,5]
stats.var(a) = 2.16 (correct)

stats.skew(a) = -0.74 (not correct)

Again, for this I have the return value of skew set to show me the the
second moment. Skew is calling the same exact function as var and with the
same exact same inputs as var(), or should be.

I've looked at every single variable in variance to see what is going on and
here is what I know for now:

1. sumall is adding up to the wrong number when called from skew (or kurt).
2. avg, count, and moment all are what they should be.
3. The sequence passed is always the same from Python (a = [1,2,3,4,4,5])

I'm guessing this is some nuance of C code dealing static variables or
something to do with memory, but again, I no even less about C than I do
about Python (which is my objective here - learning some of this should make
me better at both, hopefully)

A couple other points: There are three parameters now, not one. The later
two are optional. they are (list, avg, and sample) sample is True by
default.

So you can pass it an average already.

Lastly, any other pointers would be greatly appreciated.

James















On Thu, May 26, 2011 at 7:50 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "James Reynolds" <eire1130 at gmail.com> wrote
>
>
>  As far as the null point goes, it shouldn't be null at all once it gets to
>> the point Alan pointed out. The pointer is set in (for example) stat_avg
>>
>>   seq = PySequence_Fast(obj, "Expected a Sequence");
>>
>
> Can the function fail? If so what does it return? That was
> my point. Can seq ever still be NULL after the function call?
> For example if the function is allocating memory it could fail
> to grab enough and then return a NULL....
>
> But it depends on how reliable the Python function is in
> its return value...
>
>
>  But maybe I am missing something that you have seen?
>>
>
> Don't rely on functio  returns being valid values.
> It is common practice in industrial strength C to return a NULL
> and expect the user to check. Manyb of the standard library
> functions work that way too.
>
> So you often see code like
>
> if (foo = someFunction() ){   // notice it is an assignment not equality
> test
>     process(foo);
> }
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110601/afc6ef1c/attachment.html>


More information about the Tutor mailing list