Using C struct in Python
John Machin
sjmachin at lexicon.net
Wed Jun 7 19:28:58 EDT 2006
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
More information about the Python-list
mailing list