473,394 Members | 1,752 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,394 software developers and data experts.

Extention Woes

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!

Oct 19 '05 #1
8 1443
"Tuvas" wrote:
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>

Oct 19 '05 #2
Tuvas wrote:
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
Oct 19 '05 #3
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,&da t,&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?

Oct 20 '05 #4
Forgot, var declartions

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

Oct 20 '05 #5
Tuvas wrote:
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

Oct 20 '05 #6
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?

Oct 20 '05 #7
"Tuvas" wrote:
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>

Oct 20 '05 #8
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!

Oct 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jesper Olsen | last post by:
I have a python extention implemented in C/C++ - the extention itself is internally implemented in C++, but the interface is pure C, so that it can easily be called from a python C-wrapper. The...
0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
1
by: Carl Ogawa | last post by:
How do I make .cgi extention work? I installed ActivePerl 5.8. My CGI scripts work fine with .PL extention but not .CGI extention although I associated CGI extention as exactly same as PL...
1
by: MFA | last post by:
Hi all I have installed Front page server Extention on IIS 5.0 it was working fine. I was able to load project from Visual Interdav and doing well. Yesterday I installed a security update from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...

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.