PyObject * convertArrayOfIntToPython(const Teuchos::Array<int> & tai)
{
    static
    PyObject * classTupleOfInt = NULL;
    PyObject * args            = NULL;
    PyObject * result          = NULL;
    int        res             = 0;
    int        size            = 0;

    if (!classTupleOfInt)
    {
        classTupleOfInt = getClassFromModule(PyTrilinosEpetraExt, "tuple_of_int");
        if (!classTupleOfInt) goto fail;
    }
    size = tai.size();
    args = PyTuple_New(size);
    for (int i=0; i < size; ++i)
    {
        res = PyTuple_SetItem(args, Py_ssize_t(i), PyInt_FromLong(long(tai[i])));
        if (res) goto fail;
    }
    result = PyObject_CallObject(classTupleOfInt, args);
    Py_DECREF(args);
    args = NULL;
    if (!result) goto fail;

    return result;

fail:
    Py_XDECREF(args);
    return NULL;
}
Example #2
0
/*
#<pydoc>
def dbg_read_memory(ea, sz):
    """
    Reads from the debugee's memory at the specified ea
    @return:
        - The read buffer (as a string)
        - Or None on failure
    """
    pass
#</pydoc>
*/
static PyObject *dbg_read_memory(PyObject *py_ea, PyObject *py_sz)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();

  uint64 ea, sz;
  if ( !dbg_can_query() || !PyW_GetNumber(py_ea, &ea) || !PyW_GetNumber(py_sz, &sz) )
    Py_RETURN_NONE;

  // Create a Python string
  PyObject *ret = PyString_FromStringAndSize(NULL, Py_ssize_t(sz));
  if ( ret == NULL )
    Py_RETURN_NONE;

  // Get the internal buffer
  Py_ssize_t len;
  char *buf;
  PyString_AsStringAndSize(ret, &buf, &len);

  if ( (size_t)read_dbg_memory(ea_t(ea), buf, size_t(sz)) != sz )
  {
    // Release the string on failure
    Py_DECREF(ret);
    // Return None on failure
    Py_RETURN_NONE;
  }
  return ret;
}
Example #3
0
  void idaapi get_row(
          qstrvec_t *cols,
          int *icon_,
          chooser_item_attrs_t *attrs,
          size_t n) const
  {
    PYW_GIL_GET;

    // Call Python
    PYW_GIL_CHECK_LOCKED_SCOPE();
    pycall_res_t list(
            PyObject_CallMethod(
                    self, (char *)S_ON_GET_LINE,
                    "i", int(n)));
    if ( list.result != NULL )
    {
      // Go over the List returned by Python and convert to C strings
      for ( int i = chobj->columns - 1; i >= 0; --i )
      {
        borref_t item(PyList_GetItem(list.result.o, Py_ssize_t(i)));
        if ( item == NULL )
          continue;

        const char *str = PyString_AsString(item.o);
        if ( str != NULL )
          (*cols)[i] = str;
      }
    }

    *icon_ = chobj->icon;
    if ( (cb_flags & CHOOSE_HAVE_GETICON) != 0 )
    {
      pycall_res_t pyres(
              PyObject_CallMethod(
                      self, (char *)S_ON_GET_ICON,
                      "i", int(n)));
      if ( pyres.result != NULL )
        *icon_ = PyInt_AsLong(pyres.result.o);
    }

    if ( (cb_flags & CHOOSE_HAVE_GETATTR) != 0 )
    {
      pycall_res_t pyres(
              PyObject_CallMethod(
                      self, (char *)S_ON_GET_LINE_ATTR,
                      "i", int(n)));
      if ( pyres.result != NULL && PyList_Check(pyres.result.o) )
      {
        PyObject *item;
        if ( (item = PyList_GetItem(pyres.result.o, 0)) != NULL )
          attrs->color = PyInt_AsLong(item);
        if ( (item = PyList_GetItem(pyres.result.o, 1)) != NULL )
          attrs->flags = PyInt_AsLong(item);
      }
    }
  }
Example #4
0
//-------------------------------------------------------------------------
static bool py_do_get_bytes(
        PyObject **out_py_bytes,
        PyObject **out_py_mask,
        ea_t ea,
        unsigned int size,
        int gmb_flags)
{
  PYW_GIL_CHECK_LOCKED_SCOPE();
  bool has_mask = out_py_mask != NULL;
  do
  {
    if ( size <= 0 )
      break;

    // Allocate memory via Python
    newref_t py_bytes(PyString_FromStringAndSize(NULL, Py_ssize_t(size)));
    if ( py_bytes == NULL )
      break;

    bytevec_t mask;
    if ( has_mask )
      mask.resize((size + 7) / 8, 0);

    // Read bytes
    int code = get_bytes(PyString_AsString(py_bytes.o),
                         size,
                         ea,
                         gmb_flags,
                         has_mask ? mask.begin() : NULL);
    if ( code < 0 )
      break;

    // note: specify size, as '0' bytes would otherwise cut the mask short
    if ( has_mask )
    {
      newref_t py_mask(PyString_FromStringAndSize(
                               (const char *) mask.begin(),
                               mask.size()));
      if ( py_mask == NULL )
        break;
      py_mask.incref();
      *out_py_mask = py_mask.o;
    }

    py_bytes.incref();
    *out_py_bytes = py_bytes.o;
    return true;
  } while ( false );
  return false;
}
Example #5
0
  static bool idaapi s_print(       // convert to colored string
    void *ud,                       // user-defined data
    qstring *out,                   // output buffer. may be NULL
    const void *value,              // value to print. may not be NULL
    asize_t size,                   // size of value in bytes
    ea_t current_ea,                // current address (BADADDR if unknown)
    int operand_num,                // current operand number
    int dtid)                       // custom data type id
  {
    PYW_GIL_GET;

    // Build a string from the buffer
    newref_t py_value(PyString_FromStringAndSize(
                              (const char *)value,
                              Py_ssize_t(size)));
    if ( py_value == NULL )
      return false;

    py_custom_data_format_t *_this = (py_custom_data_format_t *) ud;
    newref_t py_result(PyObject_CallMethod(
                               _this->py_self,
                               (char *)S_PRINTF,
                               "O" PY_FMT64 "ii",
                               py_value.o,
                               pyul_t(current_ea),
                               operand_num,
                               dtid));

    // Error while calling the function?
    if ( PyW_ShowCbErr(S_PRINTF) || py_result == NULL )
      return false;

    bool ok = false;
    if ( PyString_Check(py_result.o) )
    {
      Py_ssize_t len;
      char *buf;
      if ( out != NULL && PyString_AsStringAndSize(py_result.o, &buf, &len) != -1 )
      {
        out->qclear();
        out->append(buf, len);
      }
      ok = true;
    }
    return ok;
  }
Example #6
0
  void on_get_line(int lineno, char * const *line_arr)
  {
    // Called from s_getl, which itself can be called from the kernel. Ensure GIL
    PYW_GIL_GET;

    // Get headers?
    if ( lineno == 0 )
    {
      // Copy the pre-parsed columns
      for ( size_t i=0; i < cols.size(); i++ )
        qstrncpy(line_arr[i], cols[i].c_str(), MAXSTR);
      return;
    }

    // Clear buffer
    int ncols = int(cols.size());
    for ( int i=ncols-1; i>=0; i-- )
      line_arr[i][0] = '\0';

    // Call Python
    PYW_GIL_CHECK_LOCKED_SCOPE();
    newref_t list(PyObject_CallMethod(self, (char *)S_ON_GET_LINE, "i", lineno - 1));
    if ( list == NULL )
      return;

    // Go over the List returned by Python and convert to C strings
    for ( int i=ncols-1; i>=0; i-- )
    {
      borref_t item(PyList_GetItem(list.o, Py_ssize_t(i)));
      if ( item == NULL )
        continue;

      const char *str = PyString_AsString(item.o);
      if ( str != NULL )
        qstrncpy(line_arr[i], str, MAXSTR);
    }
  }
bool convertFromPythonExisting(PyObject *lst, TExample &example)
{
  PDomain dom=example.domain;

  if (PyOrExample_Check(lst)) {
    const TExample &orex = PyExample_AS_ExampleReference(lst);
    if (orex.domain != dom)
      dom->convert(example, orex);
    else
      example = orex;
    return true;
  }

  if (!PyList_Check(lst)) {
    PyErr_Format(PyExc_TypeError, "invalid argument type (expected list, got '%s)", lst ? lst->ob_type->tp_name : "None");
    return false;
  }

  int const nvars = dom->variables->size() + dom->classVars->size();
  if (Py_ssize_t(nvars) != PyList_Size(lst)) {
    PyErr_Format(PyExc_IndexError, "invalid list size (got %i, expected %i items)",
        PyList_Size(lst), nvars);
    return false;
  }

  Py_ssize_t pos = 0;
  TExample::iterator ei(example.begin());
  TVarList::iterator vi(dom->variables->begin());
  TVarList::const_iterator const ve(dom->variables->end());
  TVarList::const_iterator const ce(dom->classVars->end());
  while(vi != ce && vi != ve) {
    PyObject *li=PyList_GetItem(lst, pos++);
    if (!li)
      PYERROR(PyExc_SystemError, "can't read the list", false);

    if (PyOrValue_Check(li))
      if (PyValue_AS_Variable(li) ? (PyValue_AS_Variable(li) != *vi) : (PyValue_AS_Value(li).varType=!(*vi)->varType) ) {
        PyErr_Format(PyExc_TypeError, "wrong value type for attribute no. %i (%s)", pos, (*vi)->get_name().c_str());
        return false;
      }
      else
        *(ei++)=PyValue_AS_Value(li);

    else {
      if (li == Py_None) {
        *(ei++) = (*vi)->DK();
      }
        
      else if (PyString_Check(li))
          (*vi)->str2val(string(PyString_AsString(li)), *(ei++));

      else if ((*vi)->varType==TValue::INTVAR) {
        if (PyInt_Check(li))
          *(ei++)=TValue(int(PyInt_AsLong(li)));
        else {
          PyErr_Format(PyExc_TypeError, "attribute no. %i (%s) is ordinal, string value expected", pos, (*vi)->get_name().c_str());
          return false;
        }
      }
      else if ((*vi)->varType==TValue::FLOATVAR) {
        float f;
        if (PyNumber_ToFloat(li, f))
          *(ei++) = TValue(f);
        else {
          PyErr_Format(PyExc_TypeError, "attribute no. %i (%s) is continuous, float value expected", pos, (*vi)->get_name().c_str());
          return false;
        }
      }
      else
        ei++;
    }
    if (++vi == ve) {
        vi = dom->classVars->begin();
    }
  }

  return true;
}
PyObject *
convertOutArgsToPython(const EpetraExt::ModelEvaluator::OutArgs & outArgs)
{
    static
    PyObject * classOutArgs 	    = NULL;
    static
    PyObject * classTupleOfEvaluation = NULL;
    PyObject * obj          	    = NULL;
    PyObject * tupleOfEval            = NULL;
    PyObject * outArgsObj   	    = NULL;
    int        res	            = 0;
    int        Ng 	            = 0;
    Teuchos::RCP< Epetra_Operator > WPtr;

    // Python class objects
    if (!classOutArgs)
    {
        classOutArgs = getClassFromModule(PyTrilinosEpetraExt, "OutArgs");
        if (!classOutArgs) goto fail;
    }
    if (!classTupleOfEvaluation)
    {
        classTupleOfEvaluation = getClassFromModule(PyTrilinosEpetraExt,
                                 "tuple_of_Evaluation");
        if (!classTupleOfEvaluation) goto fail;
    }

    // Create an instance
    outArgsObj = PyObject_CallObject(classOutArgs, NULL);
    if (!outArgsObj) goto fail;

    // description attribute
    obj = PyString_FromString(outArgs.modelEvalDescription().c_str());
    if (!obj) goto fail;
    res = PyObject_SetAttrString(outArgsObj, "description", obj);
    Py_DECREF(obj);
    obj = NULL;
    if (res < 0) goto fail;

    // g attribute
    Ng = outArgs.Ng();
    if (Ng > 0)
    {
        obj = PyTuple_New(Ng);
        for (int i=0; i < Ng; ++i)
        {
            res = PyTuple_SetItem(obj, Py_ssize_t(i),
                                  convertEvaluationToPython(outArgs.get_g(i)));
            if (res) goto fail;
        }
        tupleOfEval = PyObject_CallObject(classTupleOfEvaluation, obj);
        res = PyObject_SetAttrString(outArgsObj, "g", tupleOfEval);
        Py_DECREF(tupleOfEval);
        Py_DECREF(obj);
        obj = NULL;
        if (res) goto fail;
    }

    // f attribute
    if (outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_f))
    {
        obj = convertEvaluationToPython(outArgs.get_f());
        if (!obj) goto fail;
        res = PyObject_SetAttrString(outArgsObj, "f", obj);
        Py_DECREF(obj);
        obj = NULL;
        if (res) goto fail;
    }

    // W attribute
    if (outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_W))
    {
        WPtr = outArgs.get_W();
        if (!WPtr.is_null())
        {
            obj = convertEpetraOperatorToPython(&WPtr);
            if (!obj) goto fail;
            res = PyObject_SetAttrString(outArgsObj, "W", obj);
            Py_DECREF(obj);
            obj = NULL;
            if (res) goto fail;
        }
    }

    // W_properties attribute
    if (outArgs.supports(EpetraExt::ModelEvaluator::OUT_ARG_W))
    {
        if (!WPtr.is_null())
        {
            obj = convertDerivativePropertiesToPython(outArgs.get_W_properties());
            if (!obj) goto fail;
            res = PyObject_SetAttrString(outArgsObj, "W_properties", obj);
            Py_DECREF(obj);
            obj = NULL;
            if (res) goto fail;
        }
    }

    return outArgsObj;

fail:
    Py_XDECREF(obj       );
    Py_XDECREF(outArgsObj);
    return NULL;
}
PyObject *
convertInArgsToPython(const EpetraExt::ModelEvaluator::InArgs & inArgs)
{
    static
    PyObject * classInArgs 	= NULL;
    static
    PyObject * classTupleOfVector = NULL;
    PyObject * inArgsObj   	= NULL;
    PyObject * obj         	= NULL;
    PyObject * tupleOfVector      = NULL;
    int        res                = 0;
    int        Np                 = 0;
    Teuchos::RCP<const Epetra_Vector> xPtr;
    Teuchos::RCP<const Epetra_Vector> x_dotPtr;
    Teuchos::RCP<const Epetra_Vector> pPtr;

    // Python class objects
    if (!classInArgs)
    {
        classInArgs = getClassFromModule(PyTrilinosEpetraExt, "InArgs");
        if (!classInArgs) goto fail;
    }
    if (!classTupleOfVector)
    {
        classTupleOfVector = getClassFromModule(PyTrilinosEpetraExt, "tuple_of_Vector");
        if (!classTupleOfVector) goto fail;
    }

    // Create an instance
    inArgsObj = PyObject_CallObject(classInArgs, NULL);
    if (!inArgsObj) goto fail;

    // description attribute
    obj = PyString_FromString(inArgs.modelEvalDescription().c_str());
    if (!obj) goto fail;
    res = PyObject_SetAttrString(inArgsObj, "description", obj);
    Py_DECREF(obj);
    obj = NULL;
    if (res < 0) goto fail;

    // t attribute
    if (inArgs.supports(EpetraExt::ModelEvaluator::IN_ARG_t))
    {
        obj = PyFloat_FromDouble(inArgs.get_t());
        if (!obj) goto fail;
        res = PyObject_SetAttrString(inArgsObj, "t", obj);
        Py_DECREF(obj);
        obj = NULL;
        if (res < 0) goto fail;
    }

    // alpha attribute
    if (inArgs.supports(EpetraExt::ModelEvaluator::IN_ARG_alpha))
    {
        obj = PyFloat_FromDouble(inArgs.get_alpha());
        if (!obj) goto fail;
        res = PyObject_SetAttrString(inArgsObj, "alpha", obj);
        Py_DECREF(obj);
        obj = NULL;
        if (res < 0) goto fail;
    }

    // beta attribute
    if (inArgs.supports(EpetraExt::ModelEvaluator::IN_ARG_beta))
    {
        obj = PyFloat_FromDouble(inArgs.get_beta());
        if (!obj) goto fail;
        res = PyObject_SetAttrString(inArgsObj, "beta", obj);
        Py_DECREF(obj);
        obj = NULL;
        if (res < 0) goto fail;
    }

    // x attribute
    if (inArgs.supports(EpetraExt::ModelEvaluator::IN_ARG_x))
    {
        xPtr = inArgs.get_x();
        obj  = convertEpetraVectorToPython(&xPtr);
        if (!obj) goto fail;
        res  = PyObject_SetAttrString(inArgsObj, "x", obj);
        Py_DECREF(obj);
        obj = NULL;
        if (res < 0) goto fail;
    }

    // x_dot attribute
    if (inArgs.supports(EpetraExt::ModelEvaluator::IN_ARG_x_dot))
    {
        x_dotPtr = inArgs.get_x_dot();
        obj      = convertEpetraVectorToPython(&x_dotPtr);
        if (!obj) goto fail;
        res      = PyObject_SetAttrString(inArgsObj, "x_dot", obj);
        Py_DECREF(obj);
        obj = NULL;
        if (res < 0) goto fail;
    }

    // p attribute
    Np = inArgs.Np();
    if (Np > 0)
    {
        obj = PyTuple_New(Np);
        for (int i=0; i < Np; ++i)
        {
            pPtr = inArgs.get_p(i);
            res  = PyTuple_SetItem(obj, Py_ssize_t(i),
                                   convertEpetraVectorToPython(&pPtr));
            if (res) goto fail;
        }
        tupleOfVector = PyObject_CallObject(classTupleOfVector, obj);
        res = PyObject_SetAttrString(inArgsObj, "p", tupleOfVector);
        Py_DECREF(tupleOfVector);
        Py_DECREF(obj);
        obj = NULL;
        if (res) goto fail;
    }

    return inArgsObj;

fail:
    if (PyErr_Occurred()) PyErr_Print();
    Py_XDECREF(obj               );
    Py_XDECREF(inArgsObj         );
    Py_XDECREF(classTupleOfVector);
    return NULL;
}