0

Hi,
Below is the code I am compiling to a .dll & .lib but when I try to import the "Rand" module I get ImportError: No module named Rand, I renamed the .dll to .pyd then this error is removed but when I call the sub module Rand.myRand(10,30) it crashes with a message pythonw.exe has stopped working
I am using Python2.6 on windows 7 machine.

#include <Python.h>
#define RANDMAX 32767

#ifdef __cplusplus
	extern "C" PyMODINIT_FUNC initRand(void);
#endif 

time_t Tval = 0;

int myRand(int lower,int upper)
{
	int temp = 0;
	int b = 0 ;
	if (upper == lower)
		return  lower;
	temp = rand();
	b = lower + (temp / RANDMAX) * (upper - lower);
	return b;
}
static PyObject* py_myRand(PyObject* self, PyObject* args)
{
	int lower = 0;
	int upper = 0;
	int value = 0;
	if (!PyArg_ParseTuple(args, "ii", lower,upper))
        return NULL;
	value = myRand(lower,upper);
	return Py_BuildValue("i", value);
}
static PyMethodDef RandMethods[] = {
    {"myRand",  py_myRand, METH_VARARGS,"Generate Random Number"},
    {NULL, NULL, 0, NULL} 
};

PyMODINIT_FUNC initRand(void)
{
    (void) Py_InitModule("Rand", RandMethods);
}

int main(int argc, char *argv[])
{
    Py_SetProgramName(argv[0]);
    Py_Initialize();
    initRand();
    return 0;
}

Thanks in advance.....

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by EMT
0

The worst looking line is line 25: PyArg_ParseTuple takes addresses. It should be

if (!PyArg_ParseTuple(args, "ii", &lower, &upper))

See if it runs this way.

Edited by Gribouillis: n/a

0

The worst looking line is line 25: PyArg_ParseTuple takes addresses. It should be

if (!PyArg_ParseTuple(args, "ii", &lower, &upper))

See if it runs this way.

GREAT ! thanks a ton.....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.