Python C extension: Value different if passed as list than if passed as number

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anand

    #1

    Python C extension: Value different if passed as list than if passed as number

    I have a C extension to a dll function.

    static PyObject *_wrap_SetTxCom mandRegister(Py Object *self, PyObject
    *args) {
    PyObject *resultobj;
    int arg1 ;
    int arg2 ;
    int arg3 ;
    char *arg4 = (char *)"Python" ;
    err result;

    if(!PyArg_Parse Tuple(args,(cha r
    *)"iii|s:SetTxC ommandRegister" ,&arg1,&arg2,&a rg3,&arg4)) goto fail;
    result = (err)SetTxComma ndRegister(arg1 ,arg2,arg3,(cha r const
    *)arg4);
    resultobj = PyInt_FromLong( (long)result);
    return resultobj;
    fail:
    return NULL;
    }

    Now here is what i pass from python
    test = [0xff,0xaf,0xf0]
    newint = test[0]
    SetTxCommandReg ister(0xff,test[1],test[2])
    print test

    arg1 = 0xaf if i pass either test[0] or newint
    arg1 = 0xff if i pass constant like what i have shown in the code

    What am i doing wrong?

    Thanks in advance
    Anand
  • Anand

    #2
    Re: Python C extension: Value different if passed as list than if passed as number

    My bad. I wanted to a inplace memory write for an int. so

    i = 0;
    func(i);
    print i;

    ======
    output = 1.

    The funny thing is python doesnt use 0 as 0. instead it points to a
    memory location whose value = 0.

    Since in my inplace replace, i changed that value from 0 to 1,
    test[0] actually became test[1] :))

    so i had tons of strange behavior happening. I shouldnt have made an
    immutable object as mutable.

    Comment

    • Anand

      #3
      Re: Python C extension: Value different if passed as list than if passed as number

      My bad. I wanted to do a inplace memory write for an int. so

      i = 0;
      func(i);
      print i;

      ======
      output = 1.

      The funny thing is python doesnt use 0 as 0. instead it points to a
      memory location whose value = 0.

      Since in my inplace replace, i changed that value from 0 to 1,
      test[0] actually became test[1] :))

      so i had tons of strange behavior happening. I shouldnt have made an
      immutable object as mutable.

      Comment

      Working...