Extention Woes

T

Tuvas

I am in the process of writing an extention module, and am coming up
with lots of problems. Perhaps someone could be of use. I keep getting
data that isn't what I'm sending the program. Let me give the line of C
code and Python Code and output to illistrate the problem.

write_can(can_han,0x0140,'abcd')

if (PyArg_ParseTuple(args("iiz#",&can_han,&com,&data,&len)
return NULL;

Okay, so I put in a print statement to print out the variables can_han,
com, data, and length. The results were more or less this (I don't have
copy/paste at the moment, so...

1357 com->0 len->4 str->,/p.

Anways, the variable can_han was correct, the length is correct, but
the com and the string are garbage. Any ideas as to why this may be?
Thanks!
 
F

Fredrik Lundh

Tuvas said:
I am in the process of writing an extention module, and am coming up
with lots of problems. Perhaps someone could be of use. I keep getting
data that isn't what I'm sending the program. Let me give the line of C
code and Python Code and output to illistrate the problem.

write_can(can_han,0x0140,'abcd')

if (PyArg_ParseTuple(args("iiz#",&can_han,&com,&data,&len)
return NULL;

that doesn't look entirely correct. maybe you should switch to a com-
puter with working cut and paste, so we don't have to guess...
Okay, so I put in a print statement to print out the variables can_han,
com, data, and length. The results were more or less this (I don't have
copy/paste at the moment, so...

1357 com->0 len->4 str->,/p.

Anways, the variable can_han was correct, the length is correct, but
the com and the string are garbage. Any ideas as to why this may be?

it would help if you included the variable declarations and the exact printf
statement you used to print them.

</F>
 
D

Dave Brueck

Tuvas said:
I am in the process of writing an extention module, and am coming up
with lots of problems.

This isn't a direct answer to your question, but what sort of extension is it?
Do you need to actually write a Python extension?

I ask because I've all but stopped writing Python extension modules and just use
ctypes instead (i.e. write your module as a normal .dll or .so that exports some
functions, and then use ctypes to call them).

There are probably some cases where this approach isn't a good fit, but it has
worked really well for me for a lot of different applications.

Best of luck,
-Dave
 
T

Tuvas

I tried Ctypes, but it was giving me tons of problems in the install
phase, and so I decided it'd just be better to use an extention module.
It's the type of stuff that could be perfectly done with ctypes, but...
Oh well. Also, I've done all but this last little piece with
extentions, might as well finish it.

Exact stuff.
C Code (Just parsing and printing)

if(!PyArg_ParseTuple(args,"iiz#",&can_han,&com,&dat,&len))
return NULL; //This command will translate
python data to C data
printf("%i com->%i len->%i string->%s\n",can_han,com,len,dat);

Python (Just command to this function)
print write_can(can_han,0x0140,'abcd')

Output
135769704 com->0 len->4 string->t=Ø·

Output expected values for length and the number, not for the string
nor com. Ideas?
 
N

Neal Norwitz

Tuvas said:
Forgot, var declartions

int can_han;
int com;
char len;
char dat[8];

That should probably be:

int len;
char *dat;

IIRC, "z" returns the internal string pointer. "#" is definitely not
going to return a char. I'm pretty sure it returns an int and not a
long.

n
 
T

Tuvas

Well, the point of declaring it as a char was to have it as an 8 bit
integer, as is requested later on in the program. Anyways, I tried
making the changes, similar results. The len value was always right,
the problem seems to be in this com value. BTW, it doesn't give me one
single warning, so I don't think it's a casting problem... I did a full
check, and this can_han variable is right. Hmmm... Other ideas?
 
F

Fredrik Lundh

Tuvas said:
Well, the point of declaring it as a char was to have it as an 8 bit
integer, as is requested later on in the program.

since ParseTuple writes an integer to the address you pass in,
that's likely to overwrite some random stuff on the stack. like-
wise, passing in a character buffer where ParseTuple expects
a pointer to a char pointer isn't going to work either (but that
only writes garbage into the buffer).
Anyways, I tried making the changes, similar results.

post the new code and the output it's giving you.
BTW, it doesn't give me one single warning, so I don't think it's a
casting problem...

the C compiler doesn't understand the ParseTuple format string,
so that doesn't mean anything. it's up to you to make sure that
the format specifiers and the pointers you pass in match.

</F>
 
T

Tuvas

Never mind, problem solved. The other problem I was having was I forgot
to put the .so at the end of a file, so it didn't change anything.
Thanks for all of the help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top