segfault in extension module

Nat Echols echols at
Thu Dec 4 06:02:40 CET 2003

> Post some code.

may god have mercy on my soul:

#include <Python.h>
#include "nw.h"

static PyObject *nw_align (PyObject *self, PyObject *args) {
    char *seq1, *seq2, *mfile;
    char *out1, *out2, *match;
    int penalty, status, score;
    PyObject *results;

    if (! PyArg_ParseTuple(args, "sssi", &seq1, &seq2, &mfile, &penalty)) {
        return NULL;

    status = nw(seq1, seq2, mfile, penalty, &out1, &out2, &match, &score);

    if (status == -1) {
        return NULL;

    results = Py_BuildValue("(sssi)", out1, out2, match, score);
    return results;

static PyMethodDef nwMethods[] = {
    {"align",  nw_align, METH_VARARGS,
     "Perform Needleman-Wunsch alignment of two protein sequences."},
    {NULL, NULL, 0, NULL} /* This is required!  No idea why. */

void initnw (void) {
    (void) Py_InitModule("nw", nwMethods);

This is just the wrapper; the function that it calls is defined such:
int nw (char *seq1, char *seq2, char *matrixfile, int penalty,
    char **_out1, char **_out2, char **_match, int *_score);
(I can supply this too, but I already know this works fine in a standalone
C program.)

In Python, I simply do this:
(out1, out2, match, score) = nw.align(seq1, seq2, "BLOSUM62", 0)

This is it; should be simple to fix, no?

More information about the Python-list mailing list