Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User varunc's Avatar
    Join Date
    Mar 2003
    Posts
    18
    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

  2. #2
    Super Contributor jplauril's Avatar
    Join Date
    Dec 2004
    Posts
    643
    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?

  3. #3
    Registered User varunc's Avatar
    Join Date
    Mar 2003
    Posts
    18
    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?

Similar Threads

  1. J2ME: Problem in Nokia Solution for a software problem
    By mte01 in forum Mobile Java Tools & SDKs
    Replies: 4
    Last Post: 2011-06-08, 05:52
  2. problem when stop playing video on 3650 and try to play again
    By niko86 in forum Mobile Java General
    Replies: 5
    Last Post: 2007-08-03, 07:40
  3. J2ME: Problem in Nokia Solution for a media problem
    By mte01 in forum Mobile Java Tools & SDKs
    Replies: 0
    Last Post: 2005-08-12, 11:30
  4. Nokia 6310i & Mac 17" powerbook bluetooth connection problem
    By scarylittlevoices in forum Bluetooth Technology
    Replies: 1
    Last Post: 2003-08-21, 14:48
  5. Web services on Series 60 emulator problem
    By paulgrimwood in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2003-08-05, 10:05

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved