[Python-Dev] a possible bug (or something I don't understand)? (original) (raw)

Ognen Duzlevski maketo at sdf.lonestar.org
Tue Jan 13 14:05:48 EST 2004


Hi,

Python 2.3.3 (#1, Dec 24 2003, 00:21:04) [GCC 3.3] on linux2

Please forgive me if I am bold in the assumption of a bug. It probably is not a bug but sure has me puzzled.

I have written a function using PyInline to modify three matrices (or, lists of lists) of integers. The function appears to run fine.

After the function has been called, I am using the following assignment:

minscore = min(scoremat[tlen][qlen],delmat[tlen][qlen],insertmat[teln][qlen])

However, the call breaks with: TypeError: an integer is required

Then I went a step further and here is the sequence of calls:

def SeqMapping(tseq,qseq,seqmap,invmap,tlen,qlen): # determine each energy components given the alignments # modifies seqmap, invmap # returns (minscore,identical) global LARGESCORE,OPENGAPPENALTY,ONEINDELPENALTY,EMPTY

opengappenalty = OPENGAPPENALTY
oneindelpenalty = ONEINDELPENALTY
largescore = LARGESCORE
empty = EMPTY

minscore = largescore
scoremat = []
insertmat = []
delmat = []

st = time.clock()

DynAlign(scoremat,insertmat,delmat,''.join(tseq),''.join(qseq),tlen,qlen)

et = time.clock()
print "time in nested loop: %f" % (et-st)

m1 = int(delmat[tlen][qlen])
print m1
m2 = int(insertmat[tlen][qlen])
print m2
m3 = int(scoremat[tlen][qlen])
print m3
#minscore = min(int(delmat[tlen][qlen]), int(insertmat[tlen][qlen]), \
#int(scoremat[tlen][qlen]))
print min(m1,m2,m3)

This is the output:

time in nested loop: 0.250000 403 394 402 Traceback (most recent call last): File "./primegens_old.py", line 1653, in ? GetSegment() File "./primegens_old.py", line 1496, in GetSegment tmp_score,sim = SeqMapping(seq[ndx1],seq[0],seqmapping,inversemapping,length[ndx1],length[0]) File "./primegens_old.py", line 676, in SeqMapping print min(m1,m2,m3) TypeError: an integer is required

Cleary scoremat[tlen][qlen], delmat[tlen][qlen] and insertmat[tlen][qlen] are integers (I even had type() show me they are 'int'). I am even using int() to make sure it is all right. Still the output is as above and I am really puzzled. Is it something really trivial I am missing?

Thank you, Ognen

p.s. just in case, here is the inlined C function:

from PyInline import C import main C.Builder(code=""" void DynAlign(PyObject *scoremat,PyObject *insertmat,PyObject *delmat,PyObject *tseq,PyObject *qseq,PyObject *tlen,PyObject$ {

#define MIN(a,b) ((a) < (b) ? (a):(b))

int opengappenalty;
int oneindelpenalty;
int largescore;
int cnst;
int ndx1, ndx2;
int tl,ql;
char *tsq;
char *qsq;

opengappenalty = 4;
oneindelpenalty = 2;
largescore = 999999;
tl = PyInt_AsLong(tlen);
ql = PyInt_AsLong(qlen);
tsq = PyString_AsString(tseq);
qsq = PyString_AsString(qseq);

for (ndx1=0; ndx1<=tl; ndx1++)
{
    PyList_Append(scoremat,PyList_New(ql+1));
    PyList_Append(insertmat,PyList_New(ql+1));
    PyList_Append(delmat,PyList_New(ql+1));
}
for (ndx1=1; ndx1<=tl; ndx1++)
{
    long num;
    PyObject* inner_list = NULL;
    PyObject* item;

    num = opengappenalty + (ndx1 * oneindelpenalty);
    inner_list = PyList_GetItem(scoremat,ndx1);
    item = PyInt_FromLong(num);
    PyList_SetItem(inner_list,0,PyNumber_Int(item));
    inner_list = PyList_GetItem(insertmat,ndx1);
       PyList_SetItem(inner_list,0,PyNumber_Int(item));
    inner_list = PyList_GetItem(delmat,ndx1);
    item = PyInt_FromLong(largescore);
    PyList_SetItem(inner_list,0,PyNumber_Int(item));
}
for (ndx1=1; ndx1<=ql; ndx1++)
{
    long num;
    PyObject* inner_list = NULL;
    PyObject* item;

    num = opengappenalty + (ndx1 * oneindelpenalty);

    inner_list = PyList_GetItem(scoremat,0);
    item = PyInt_FromLong(num);
    PyList_SetItem(inner_list,ndx1,PyNumber_Int(item));
    inner_list = PyList_GetItem(delmat,0);
    PyList_SetItem(inner_list,ndx1,PyNumber_Int(item));

    inner_list = PyList_GetItem(insertmat,0);
    item = PyInt_FromLong(largescore);
    PyList_SetItem(inner_list,ndx1,PyNumber_Int(item));
}

cnst = oneindelpenalty + opengappenalty;

for (ndx1=1; ndx1<=tl; ndx1++)
{
    for (ndx2=1; ndx2<=ql; ndx2++)
    {
        PyObject *delm1 = NULL;
        PyObject *delm2 = NULL;
        PyObject *scmat1 = NULL;
       PyObject *scmat2 = NULL;
        PyObject *insmat1 = NULL;
        PyObject *insmat2 = NULL;
        PyObject *item = NULL;
        long n1,n2,n3,minm,add;

        delm1 = PyList_GetItem(delmat,ndx1);
        delm2 = PyList_GetItem(delmat,ndx1-1);
        scmat1 = PyList_GetItem(scoremat,ndx1);
        scmat2 = PyList_GetItem(scoremat,ndx1-1);
        insmat1 = PyList_GetItem(insertmat,ndx1);
        insmat2 = PyList_GetItem(insertmat,ndx1-1);

        item = PyList_GetItem(delm2,ndx2);
        n1 = PyInt_AsLong(item) + oneindelpenalty;
        item = PyList_GetItem(scmat2,ndx2);
        n2 = PyInt_AsLong(item) + cnst;
        item = PyList_GetItem(insmat2,ndx2);
        n3 = PyInt_AsLong(item) + cnst;

        minm = MIN(n1,MIN(n2,n3));
        item = PyInt_FromLong(minm);
        PyList_SetItem(delm1,ndx2,PyNumber_Int(item));

        item = PyList_GetItem(insmat1,ndx2-1);
        n1 = PyInt_AsLong(item) + oneindelpenalty;
        item = PyList_GetItem(scmat1,ndx2-1);
        n2 = PyInt_AsLong(item) + cnst;
        item = PyList_GetItem(delm1,ndx2-1);
        n3 = PyInt_AsLong(item) + cnst;

        minm = MIN(n1,MIN(n2,n3));
        item = PyInt_FromLong(minm);
        PyList_SetItem(insmat1,ndx2,PyNumber_Int(item));


        if (tsq[ndx1-1] == qsq[ndx2-1])
            add = -1;
        else
            add = 1;

        item = PyList_GetItem(scmat2,ndx2-1);
        n1 = PyInt_AsLong(item);
        item = PyList_GetItem(delm2,ndx2-1);
        n2 = PyInt_AsLong(item);
        item = PyList_GetItem(insmat2,ndx2-1);
        n3 = PyInt_AsLong(item) + cnst;

        minm = MIN(n1,MIN(n2,n3)) + add;
        item = PyInt_FromLong(minm);
        PyList_SetItem(scmat1,ndx2,PyNumber_Int(item));
    }
}

} """,targetmodule=main).build()



More information about the Python-Dev mailing list