Python C API, building a module

MRAB python at mrabarnett.plus.com
Fri Nov 27 22:25:15 EST 2009


P.F.C. wrote:
> Hello, I'm new to the mailing list but have been using python for a while.
> 
> I am now attempting to embed the interpreter into a C application but am 
> having an issue when building a module exposing my application's 
> functions to python.
> I do not know if this is the right place to ask for help with it, if it 
> isn't then please let me know where to go.
> 
> the problem I'm having is with making a PyMethodDef array
> When I make the array like this it works:
> 
> static PyMethodDef ge_methods[]={
>   {"test",ge_test,METH_NOARGS,"Test returns 123L"},
>   {NULL,NULL}
> };
> 
> but as soon as I try to build the array from constant variables like this:
> 
> const int ge_test_args = METH_NOARGS;                       //The flag 
> for this function
> const char* ge_test_doc = "Test\nwill print \"test\"";        //The 
> docstring
> static PyMethodDef ge_methods[]={
>    {"test",ge_test, ge_test_args, ge_test_doc},                //simply 
> replacing the flag and the docstring with a constant variable
>    {NULL,NULL}
> };
> 
> the compiler then gives the following errors:
> ./test1.c:74: error: initializer element is not constant
> ./test1.c:74: error: (near initialization for ‘ge_methods[0].ml_flags’)
> ./test1.c:74: error: initializer element is not constant
> ./test1.c:74: error: (near initialization for ‘ge_methods[0].ml_doc’)
> 
> I'm using the gcc compiler
> 
> This may well be because of my lack of understanding the C language but 
> I was hoping someone could help me out, or at least point me in the 
> right direction
> 
> I also posted about this at 
> http://talk.christiandevs.com/viewtopic.php?f=13&t=2521 
> <http://talk.christiandevs.com/viewtopic.php?f=13&t=2521>
> 
I think it's because C 'const' objects aren't true constants, but are
more like read-only variables; you can initialise them in the
declaration but not assign to them otherwise. Thus what you're actually
trying to do is initialise from a variable, not a constant.



More information about the Python-list mailing list