C extension question

C

chris.cenotti

I am currently trying to port some Python and Python C extension code
to C#, and am having trouble understanding what is happening in a
piece of the code.

The pertinent pieces of code are below, and my question follows the
snippets:

in foo.py:
(mgrid,xgrid,ygrid,zgrid,ngrids) = IP.CreateGridCell()

in IP.c:
static PyObject *PyCreateGridCell()
{
GRIDCELL *grid = alloc(...);
for(i=0; i<length; i++)
{
grid[cnt] = .... //etc fill in grid array
cnt++;
}

PyObject *GRIDRET = PyCObject_FromVoidPtr((void *)grid,NULL);
return Py_BuildValue("Oi",GRIDRET,cnt-1);

typedef struct {
XYZ p[8];
double val[8];
} GRIDCELL;

typedef struct {
double x,y,z;
} XYZ;

So my question is (and I hope it's not too silly a question), what is
happening to the grid array pointer in the C code that it maps to
multiple variables (which all are arrays) on the python side. Is the
grid variable being assigned to mgrid, xgrid, ygrid and zgrid whole,
or is it being split somehow (and if so, how)? Thanks for the help.
 
M

Matimus

I am currently trying to port some Python and Python C extension code
to C#, and am having trouble understanding what is happening in a
piece of the code.

The pertinent pieces of code are below, and my question follows the
snippets:

in foo.py:
(mgrid,xgrid,ygrid,zgrid,ngrids) = IP.CreateGridCell()

in IP.c:
static PyObject *PyCreateGridCell()
{
GRIDCELL *grid = alloc(...);
for(i=0; i<length; i++)
{
grid[cnt] = .... //etc fill in grid array
cnt++;
}

PyObject *GRIDRET = PyCObject_FromVoidPtr((void *)grid,NULL);
return Py_BuildValue("Oi",GRIDRET,cnt-1);

typedef struct {
XYZ p[8];
double val[8];
} GRIDCELL;

typedef struct {
double x,y,z;
} XYZ;

So my question is (and I hope it's not too silly a question), what is
happening to the grid array pointer in the C code that it maps to
multiple variables (which all are arrays) on the python side. Is the
grid variable being assigned to mgrid, xgrid, ygrid and zgrid whole,
or is it being split somehow (and if so, how)? Thanks for the help.

PyCreateGridCell uses [1]Py_BuildValue to generate a tuple containing
a Object (its not really clear to me exactly what this object is) and
an integer. From the looks of it I would say that PyCreateGridCell
does not return something that can be unpacked into 5 things like the
python code suggests. Hoever, there is no guarantee that
PyCreateGridCell is the function that gets called by Python when
IP.CreateGridCell is called. You have to look at the method table to
see what it maps to. I would read the introduction [2] at least to the
Extending and Embedding documentation.

Why do you need to do this anyway. If you _need_ to use .NET you could
save time by using IronPython instead of rewriting the whole thing in
C#.

[1] http://docs.python.org/api/arg-parsing.html#l2h-216
[2] http://docs.python.org/ext/intro.html

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top