You Are Here:

Community: Developer Discussion Boards

#1 Old Problem in PyArg_ParseTuple - 2005-09-20, 06:31

Join Date: Mar 2003
Posts: 18
varunc
Offline
Registered User
Hi,

I am writing an extension module in C++. I am facing a problem with PyArg_ParseTuple. Here is the relevant portion of my code:
TInt type = -1;
char *s_key, *s_value;//to address, data
s_key=NULL;
s_value=NULL;
int l_key, l_value; //lengths of strings
long int l_valuel;//from serviceid, to serviceid
int i_value; //protocol
double d_value1;
double d_value2;
float f_value;
Py_complex c;

/* Possible types are:
EIntType,
ELongType,
EStringType,
EFloatType,
EComplexType

Determine type:
*/
if(PyArg_ParseTuple(args, "u#s#", &s_key, &l_key, &s_value, &l_value)) /* type-checked */
type=CAppConfig::EStringType;
else if(PyArg_ParseTuple(args, "u#f", &s_key, &l_key, &f_value))
{
d_value1 = f_value;
type=CAppConfig::EFloatType;
}
else if(PyArg_ParseTuple(args, "u#d", &s_key, &l_key, &d_value1))
type=CAppConfig::EFloatType;
else if(PyArg_ParseTuple(args, "u#i", &s_key, &l_key, &i_value))
type=CAppConfig::EIntType;
else if(PyArg_ParseTuple(args, "u#l", &s_key, &l_key, &l_valuel))
type=CAppConfig::ELongType;
else if(PyArg_ParseTuple(args, "u#D", &s_key, &l_key, &c))
{
type=CAppConfig::EComplexType;
d_value1 = c.real;
d_value2 = c.imag;
}
else
{
PyErr_BadArgument();
return NULL;
}

This code always works with strings and ints with no problems. However, when i pass in a float like 12.22, it detects it as int. For complex type is always branches to PyErr_BadArgument(); same applies for long int's

Any info/help in this regard will be much appreciated!!!!

Regards,
Varun
Reply With Quote

#2 Old Re: Problem in PyArg_ParseTuple - 2005-09-20, 10:06

Join Date: Dec 2004
Posts: 646
jplauril's Avatar
jplauril
Offline
Forum Nokia Expert
First of all, note that PyArg_ParseTuple will raise an exception if the conversion fails. Your code will thus raise an exception for all argument types except string.

Also, the Python "long" type cannot be stored in a C long int variable nor can it be parsed with the "l" format specifier.

What is the Python script you use for testing that code?
Reply With Quote

#3 Old Re: Problem in PyArg_ParseTuple - 2005-09-22, 09:07

Join Date: Mar 2003
Posts: 18
varunc
Offline
Registered User
Hi,
The code I am using to test this is something like this:
>>>> import ghostConfig
>>>> ghostConfig.set(u'mykey', 'myvalue')
>>>> ghostConfig.get(u'mykey')
'myvalue'
>>>> ghostConfig.set(u'mykey', 12)
>>>> ghostConfig.get(u'mykey')
12
>>>> ghostConfig.set(u'mykey', 14.56)
>>>> ghostConfig.get(u'mykey')
14 <---- Problem why treated as int???
>>>> ghostConfig.set(u'mykey', 3+4j)
Error type mismatch....

-------------

This is really a toy project that I am using to understand value passing between python and c++. It is a simple key value registry...

"PyArg_ParseTuple will raise an exception if the conversion fails"
>> I thought PyArg_ParseTuple would return 0 if the conversion fails??
Hence this kind of nested ifs should be ok???
if(PyArg_ParseTuple(args, "u#s#", &s_key, &l_key, &s_value, &l_value)) /* type-checked */
type=CAppConfig::EStringType;
else if(PyArg_ParseTuple(args, "u#f", &s_key, &l_key, &f_value))
{
d_value1 = f_value;
type=CAppConfig::EFloatType;
}

Whats going on here?
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
problem when stop playing video on 3650 and try to play again niko86 Mobile Java General 5 2007-08-03 08:40
J2ME: Problem in Nokia Solution for a software problem mte01 Mobile Java Tools & SDKs 2 2006-05-16 09:15
J2ME: Problem in Nokia Solution for a media problem mte01 Mobile Java Tools & SDKs 0 2005-08-12 12:30
Nokia 6310i & Mac 17" powerbook bluetooth connection problem scarylittlevoices Bluetooth Technology 1 2003-08-21 15:48
Web services on Series 60 emulator problem paulgrimwood Mobile Java Networking & Messaging & Security 2 2003-08-05 11:05

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d81449X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZuserE5ftagQSxpythonX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ