473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 3190
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
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.,...
9
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...
0
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...
2
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:...
5
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...
0
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
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...
2
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...
1
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)....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.