469,926 Members | 2,124 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

Using C struct in Python

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
Jun 7 '06 #1
3 3013
On 8/06/2006 7:35 AM, Sudheer Gupta wrote:
Hi,
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
Jun 7 '06 #2
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
John Machin wrote:
On 8/06/2006 7:35 AM, Sudheer Gupta wrote:
Hi,


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

Jun 7 '06 #3
Dennis Lee Bieber wrote:
On Wed, 07 Jun 2006 17:35:58 -0400, Sudheer Gupta <su******@netapp.com>
declaimed the following in comp.lang.python:
print cp
print cp.next
There is a typo in this. Second statement was suppose to be cp =
cp.next.
I corrected it latter with the second email. 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)

Jun 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by John F Dutcher | last post: by
9 posts views Thread by timothy.williams | last post: by
reply views Thread by Richard Taylor | last post: by
2 posts views Thread by Martin v. L÷wis | last post: by
5 posts views Thread by Michael Sperlle | last post: by
reply views Thread by Sudheer Gupta | last post: by
2 posts views Thread by Pieter Rautenbach | last post: by
2 posts views Thread by Jansson Christer | last post: by
1 post views Thread by jmalone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.