473,569 Members | 2,761 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3200
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******@netap p.com>
declaimed the following in comp.lang.pytho n:
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7991
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
9
2539
by: timothy.williams | last post by:
Hi. I trying to write an extension module to call some C libraries so I can use them in Python. Several of the library functions pass pointers to structures as arguments. I was thinking that I could create a class for each structure, but I'm not sure how to get the data back and forth. The example the "Extending and Embedding the Python...
0
2011
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application called gramps (http://www.gramps-project.org) for MAC OS X.
2
1875
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version: $Revision: 42333 $
5
6752
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would cause. If I could create a large file that could be encrypted, and maybe add files to it by appending them and putting in some kind of delimiter...
0
1107
by: Sudheer Gupta | last post by:
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;
2
1991
by: Pieter Rautenbach | last post by:
Hallo, I have a 64 bit server with CentOS 4.3 installed, running Python. $ uname -a Linux lutetium.mxit.co.za 2.6.9-34.ELsmp #1 SMP Thu Mar 9 06:23:23 GMT 2006 x86_64 x86_64 x86_64 GNU/Linux Consider the following two snippets, issuing a struct.unpack(...) using Python 2.3.4 and Python 2.5 respectively.
2
10597
by: Jansson Christer | last post by:
Hi all, I have discovered that in my Python 2.4.1 installation (on Solaris 8), struct.pack handles things in a way that seems inconsistent to me. I haven't found any comprehensible documentation over known issues with Python 2.4.1 so I try this... Here's the thing:
1
3171
by: jmalone | last post by:
I have a python script that I need to freeze on AIX 5.1 (customer has AIX and does not want to install Python). The python script is pretty simple (the only things it imports are sys and socket). The README file in the Tools/freeze directory of the Python-2.4.4 distribution says the following (and many other things): Previous...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.