Strange problem with C API

Amrit amrit040 at yahoo.com
Tue Feb 26 08:13:20 EST 2002


Hi 

I'm trying to write a C function callable from python. I'm getting
some strange results :

Here's a typical run:
"""
Python 2.0 (#2, Jun 18 2001, 15:29:44) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import lt
>>> lt.__file__
'lt.so'
>>> dir(lt)
['__doc__', '__file__', '__name__', 'fmm']
>>> lt.fmm(range(15),-41)
(0.0, -1.0)
>>> lt.fmm(range(15),-41)
TypeError: function requires at least one argument
"""

On calling the same function again with the same argument gives me a
type error. This alternates. I get a typeerror and then a correct
output and so on. Any ideas what could be wrong. My C code is pasted
below

Thanks 
Amrit
/*

File: lt.c 

I Compile using 
gcc -c lt.c -I/usr/local/include/python2.0/
ld -shared lt.o -o lt.so
*/

#include "Python.h"
#include <stdio.h>
#define N 20 // Maximum size of the input 

static PyObject * findminmax(PyObject * self, PyObject * args);

static PyMethodDef TestMethods[] = {
  {"fmm",findminmax,METH_VARARGS}, 
  {NULL,NULL}
};

void initlt(){
  (void) Py_InitModule("lt",TestMethods);
} 


static PyObject *  findminmax (PyObject * self, PyObject * args)
{
  /*
    A function to find the maximum and minimum of a LT function
    Call from python as (a,b) = fmm(range(15),-41) 
  */
  PyObject *vec, *obj[N], *newobj,*retvec;
  float x[N];
  int a[N];
  int f[N+1];
  int i, n,j;
  float Thr;
  float sum;
  float  max,min;
  
  if (!PyArg_ParseTuple (args, "Of",&vec,&Thr)){
    printf("Error in arg parse\n");
    return NULL;
  }
  if (!PySequence_Check (vec)){
    printf("Error in arg parse as a vec\n");
    return NULL;
  }
  n = PyObject_Length (vec);
  if (n>N) {
    /* This shouldn't be happening */
    /* so who cares what we do now */
    /* look up a better way of gracefully returning */
    return(NULL);
  }
  //  printf ("Starting with thr=%f and n=%d\n",Thr,n);
  for (i = 0; i < n; i++)
    {
      obj[i] = PySequence_GetItem (vec, i+1);
      PyArg_Parse (obj[i], "f", &x[i]);
      a[i]=0;
      f[i]=i;
    }

  /* End Parsing code ... start computation */

  for (i=n;i<N;i++) f[i]=i;
  
  f[n]=n;
  sum=Thr;
  j=0;
  min=1<<30; // min is the min positive value
  max=1<<31; // max is the max negative value
  /* Go through all 2^n possible combinations of a[i] and compute the
max and min
My python implemtation of the same algo works just fine !
*/
  while(1){
    if (sum>=0) min=sum<min?sum:min;
    else max=sum>max?sum:max;
    j=f[0];
    if (j==n)
      break;
    f[0]=0;
    f[j]=f[j+1];
    f[j+1]=j+1;
    if (a[j]==0){
      a[j]=1;
      sum+=x[j];
    }
    else{
      a[j]=0;
      sum-=x[j];
    }
  }
  /*  printf("Done Computation\n"); */
  /*  Make the output tuple */
  retvec=(PyObject *) malloc(2*sizeof(PyObject));
  retvec=Py_BuildValue("(ff)",min,max);
  return(retvec);
}
/* End lt.c */



More information about the Python-list mailing list