[Tutor] Get the structure values from a c file

Dave Angel d at davea.name
Mon Dec 17 05:48:46 CET 2012


On 12/16/2012 10:12 PM, Marefe Serentas wrote:
> Hi, I need help.
>
> I want to create a python script that will store the value of the
> structure /MainStruct/ from the c file.
> Suppose my c file looks like this:
> ----------------------------------------------------------
> #define max (3)
>
> typedef struct A
> {
>     int a;
>     char b;
> }A;
>
> typedef struct MainStruct
> {
>     A var1;
>     int var2;
>     int var3[max];
> }Main;
>
> void generate()
> {
>     MainStruct myMain = {0};
>     myMain.var1.a = 1
>     myMain.var1.b = 'a'
>     myMain.var2 = 3
>     myMain.var3[0] = -3
>     myMain.var3[1] = 6
>     myMain.var3[2] = 18
> }
> -------------------------------------------------------------
> My python script accepts a c file as input.
> How can I get the values of the structure?
> Can I call the function generate() in python script, but it doesn't
> have a return statement, how can I get the values by calling the
> function?
> Help me, I'm really new in python.
> All I could think is just parse the c file. But it seems that it's not
> a good idea.

Please use plain-text for your message.  This time you seem to have
gotten away with it, but html messages can cause serious formatting
problems, among others garbling your sourcecode.  They also inflate the
message size by having two copies, only one of which is necessarily
visible to the bulk of the mailing list.

I can't begin to figure out what your constraints are, or why you are
even attempting this problem.  So let's see if I can clarify things a bit.

First, is it an assignment?  If so, could you quote it, so we might get
the teacher's perspective?
Or is it a work-problem, in which case you should be able to tell us why
your customers need it?
Or is it a personal challenge, in which case, we should know more about
your background, and rationale for going this particular direction?

Next, tell us about yourself.  Are you an experienced C programmer, but
new to Python?  Are you experienced at some other language (Lisp ?) and
trying to learn some C and more Python?  Or what?  What OS are you
running on, what version of C do you have available, and what version(s)
of Python are you going to use?

You said you wanted to write a program "that will store the value of the
structure /MainStruct/ from the c file"

But MainStruct has no values, and never can.  It's a structure
definition, not a variable.  The only values are in the instance myMain,
which is instantiated in the function, and tossed at the end.  So if you
want to write a "Python program that will retrieve (not store) the
values of myMain at some particular point in time," then it begins to
make sense.

Is this C file a single file, that looks exactly like you show us?  In
that case, simply call a meatware function (human brain) to translate
the data according to whatever your rules are.

If you're still here, perhaps you're saying that there are thousands of
such C files, differing in some way,. and you want the Python code to
somehow extract something from a particular one of them.  Is there a C
library module generated from each such file, so you could call it from
Python?  Do you have the tools and experience to rebuild it with some
changes to the file (like changing the return type of the function)?  Do
you want help automating those changes and launching the gcc compile?

How much are you permitted to change the source before compiling it? 
Are you allowed to rename each such file to a header file, after editing
it?  Can you use C++, or are you stuck in C?

If someone is generating these thousands of source files, what is the
commonality?  Do they all define the function generate_and_return() or
might some of them call it something else?  Are the fields fixed, are
the number of nestings fixed, are the rhs of the assignments inside
generate() always literals?  Are they constrained to either ints or chars?

Is the code that's generating these files available to be customized? 
Perhaps it could just use some other format.  Maybe you could write C
code that includes one such file and when it's run, it generates a
Python module.

And finally, what do you mean by "storing" the value?  Do you need to
custom generate a set of classes that mimic the C structs, and give them
similar names (unless you come up with a symbol that's legal in C, but
not in Python), or are you looking for something that in some sense
represents the concepts?  For this example, I see no value in keeping
the two structs separate if you're never using them anywhere else.  I'd
probably just coalesce them.  What about types? A C int is constrained
to 16 bit (or 32 or 64, depends on compiler version).  But a Python int
has no upper limit at all (in version 3).  A char is not the same
between C and Python 3, and even without considering Python, a C char is
a compiler-specific type.  Default seems to be a signed 8-bit value, but
I  believe the standard permits unsigned and/or other bit sizes.

What about alignment?  Quite relevant in C, but very seldom meaningful
for Python.  But since this may be just a fraction of some larger hunk
of code, perhaps those C structs are intended to map to disk formats,
where alignment, sizes and byte-sex are very important.

Perhaps you want to look at ctypes.


-- 

DaveA




More information about the Tutor mailing list