Using C struct in Python

S

Sudheer Gupta

Hi,

I am having trouble using C struct in python. Hope anyone can help me
out ...

Say, I have my C struct as

typedef struct call
{
struct call *next;
// .....

} call_t;

I have a global variable, namely call_pool, which is of type call_t *


My python program:

cp = call_pool # no error doing this, means that call_pool is accessable

while cp:
print cp
print cp.next


This is giving me error: " There is no member or method name c_next"

Now, If I just do:

print cp
print cp.next

there is no problem. But I am seeing a difference in the way python is
looking at the struct:

print cp -> (call_t*) 0xb0...
print cp.next -> (struct call *) 0xb0...


Is python not intelligent enough to diagnose the next pointer ??

Responses appreciated.

Thanks
Sudheer
 
J

John Machin


Hi.

Your later "correction" doesn't clear up the confusion below. Quick
eye-balling revealed no difference. If you have to correct a minor typo
in a posting, please consider saying "change X to Y" instead of
reposting the whole thing.
I am having trouble using C struct in python. Hope anyone can help me
out ...

Say, I have my C struct as

typedef struct call
{
struct call *next;
// .....

} call_t;

I have a global variable, namely call_pool, which is of type call_t *

You really need to explain what sort of glue you have between your
Python code and your C code.

1. Are you the author of the glue? If not, better ask the author.
2. Are you extending Python with a module written in C, or are you
embedding Python in a C program?
3. If extending, what's your glue? SWIG? something else? hand-crafted?
My python program:

cp = call_pool # no error doing this, means that call_pool is accessable

Acessible from where? How do you bind the name "call_pool" to an object?
while cp:
print cp
print cp.next


This is giving me error: " There is no member or method name c_next"

Not a Python error message. Must be coming from inside your extension
module.
Now, If I just do:

print cp
print cp.next

there is no problem.

Sorry, I don't understand. Above you said it was "giving me error".
But I am seeing a difference in the way python is
looking at the struct:

print cp -> (call_t*) 0xb0...
print cp.next -> (struct call *) 0xb0...

If they are actual results from a Python print statement, then I can
only assume the extension module defines types which have (in effect)
str() and/or repr() methods which produce such output.

Python does not know that the C type of cp is (call_t*) and that of
cp.next is (struct call *). In fact it doesn't care, and it shouldn't
care. The extension module could be written in assembly language or APL
or even INTERCAL if it obeys the conventions, which don't include
exposing a C type for each object. Another way of looking at it: methods
in extension modules are mostly expected to behave like methods written
in Python.

I get the impression that you are using some "superglue" that parses C
declarations and writes (parts of) Python extension modules in C. Do you
think you could possibly tell us what the name of this superglue is?
Is python not intelligent enough to diagnose the next pointer ??


To the extent to which I can understand what your question means, the
answer is: It by design makes no attempt to be what you are calling
intelligent.

Python does only limited inspection of the tables of methods that an
extension type says it supports. It uses only the very basic
information: is the pointer to method X NULL?

I hope some of the above helps you amplify your question.

Cheers,
John
 
S

Sudheer Gupta

Hi,

Thanks a lot for your responses. It cleared up a lot for me !!

Its a superglue developed and used in house and cannot be revealed ..
sorry for that !!

I am only extending to the existing glue. Have contacted the author
regarding the problem.

Sorry for confusion

-Sudheer
 
D

Dennis Lee Bieber

print cp
print cp.next
What happens if you keep adding
print cp.next
statements (say 50 or more of them).

Your error message might indicate that you ran out of "cp.next"
/values/ (ie: the last valid item in the chain of cp's does not have a
next link at all, rather than a next link that contains null)
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Sudheer Gupta

There is a typo in this. Second statement was suppose to be cp =
cp.next.
I corrected it latter with the second email.
 
D

Dennis Lee Bieber

There is a typo in this. Second statement was suppose to be cp =
cp.next.
I corrected it latter with the second email.

Within the while loop it could still be, as I mentioned, that you
are running into some invalid end-of-chain condition, which a single
pair of statements does not encounter.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top