Py_INCREF() incomprehension

Ervin Hegedüs airween at gmail.com
Tue Apr 26 11:48:15 CEST 2011


Hello Python users,

I'm working on a Python module in C - that's a cryptographic module,
which uses a 3rd-party lib from a provider (a bank).
This module will encrypt and decrypt the messages for the provider web service.

Here is a part of source:

static PyObject*
mycrypt_encrypt(PyObject *self, PyObject *args)
{
    int cRes = 0;
    int OutLen = 0;

    char *  url;
    char *  path;

    if (!PyArg_ParseTuple(args, "ss", &url, &path)) {
        return NULL;
    }

    OutLen = strlen(url)*4;
    outdata=calloc(OutLen, sizeof(char));

    if (!outdata) {
        handle_err(UER_NOMEM);
        return NULL;
    }
    cRes = ekiEncodeUrl (url, strlen(url)+1, outdata, &OutLen, 1, path);

    if (cRes == 0) {
        return Py_BuildValue("s", outdata);
    } else {
        handle_err(cRes);
        return NULL;
    }

    return Py_None;
}

where ekiEncodeUrl is in a 3rd-party library.
I should call this function from Python like this:

import mycrypt

message = "PID=IEB0001&MSGT=10&TRID=000000012345678"
crypted = mycrypt(mymessage, "/path/to/key");

Everything works fine, but sorry for the recurrent question: where
should I use the Py_INCREF()/Py_DECREF() in code above?


Thank you,

cheers:

a.



More information about the Python-list mailing list