示例#1
0
/* Helper function for typy_fields which converts a single field to a
   dictionary.  Returns NULL on error.  */
static PyObject *
convert_field (struct type *type, int field)
{
    PyObject *result = field_new ();
    PyObject *arg;

    if (!result)
        return NULL;

    if (!field_is_static (&TYPE_FIELD (type, field)))
    {
        arg = PyLong_FromLong (TYPE_FIELD_BITPOS (type, field));
        if (!arg)
            goto fail;

        if (PyObject_SetAttrString (result, "bitpos", arg) < 0)
            goto failarg;
    }

    if (TYPE_FIELD_NAME (type, field))
        arg = PyString_FromString (TYPE_FIELD_NAME (type, field));
    else
    {
        arg = Py_None;
        Py_INCREF (arg);
    }
    if (!arg)
        goto fail;
    if (PyObject_SetAttrString (result, "name", arg) < 0)
        goto failarg;

    arg = TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False;
    Py_INCREF (arg);
    if (PyObject_SetAttrString (result, "artificial", arg) < 0)
        goto failarg;

    if (TYPE_CODE (type) == TYPE_CODE_CLASS)
        arg = field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False;
    else
        arg = Py_False;
    Py_INCREF (arg);
    if (PyObject_SetAttrString (result, "is_base_class", arg) < 0)
        goto failarg;

    arg = PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field));
    if (!arg)
        goto fail;
    if (PyObject_SetAttrString (result, "bitsize", arg) < 0)
        goto failarg;

    /* A field can have a NULL type in some situations.  */
    if (TYPE_FIELD_TYPE (type, field) == NULL)
    {
        arg = Py_None;
        Py_INCREF (arg);
    }
    else
        arg = type_to_type_object (TYPE_FIELD_TYPE (type, field));
    if (!arg)
        goto fail;
    if (PyObject_SetAttrString (result, "type", arg) < 0)
        goto failarg;

    return result;

failarg:
    Py_DECREF (arg);
fail:
    Py_DECREF (result);
    return NULL;
}
示例#2
0
文件: py-type.c 项目: ibuclaw/gdb
static PyObject *
convert_field (struct type *type, int field)
{
  gdbpy_ref<> result (field_new ());

  if (result == NULL)
    return NULL;

  gdbpy_ref<> arg (type_to_type_object (type));
  if (arg == NULL)
    return NULL;
  if (PyObject_SetAttrString (result.get (), "parent_type", arg.get ()) < 0)
    return NULL;

  if (!field_is_static (&TYPE_FIELD (type, field)))
    {
      const char *attrstring;

      if (TYPE_CODE (type) == TYPE_CODE_ENUM)
	{
	  arg.reset (gdb_py_long_from_longest (TYPE_FIELD_ENUMVAL (type,
								   field)));
	  attrstring = "enumval";
	}
      else
	{
	  arg.reset (gdb_py_long_from_longest (TYPE_FIELD_BITPOS (type,
								  field)));
	  attrstring = "bitpos";
	}

      if (arg == NULL)
	return NULL;

      /* At least python-2.4 had the second parameter non-const.  */
      if (PyObject_SetAttrString (result.get (), (char *) attrstring,
				  arg.get ()) < 0)
	return NULL;
    }

  arg.reset (NULL);
  if (TYPE_FIELD_NAME (type, field))
    {
      const char *field_name = TYPE_FIELD_NAME (type, field);

      if (field_name[0] != '\0')
	{
	  arg.reset (PyString_FromString (TYPE_FIELD_NAME (type, field)));
	  if (arg == NULL)
	    return NULL;
	}
    }
  if (arg == NULL)
    {
      arg.reset (Py_None);
      Py_INCREF (arg.get ());
    }
  if (PyObject_SetAttrString (result.get (), "name", arg.get ()) < 0)
    return NULL;

  arg.reset (TYPE_FIELD_ARTIFICIAL (type, field) ? Py_True : Py_False);
  Py_INCREF (arg.get ());
  if (PyObject_SetAttrString (result.get (), "artificial", arg.get ()) < 0)
    return NULL;

  if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
    arg.reset (field < TYPE_N_BASECLASSES (type) ? Py_True : Py_False);
  else
    arg.reset (Py_False);
  Py_INCREF (arg.get ());
  if (PyObject_SetAttrString (result.get (), "is_base_class", arg.get ()) < 0)
    return NULL;

  arg.reset (PyLong_FromLong (TYPE_FIELD_BITSIZE (type, field)));
  if (arg == NULL)
    return NULL;
  if (PyObject_SetAttrString (result.get (), "bitsize", arg.get ()) < 0)
    return NULL;

  /* A field can have a NULL type in some situations.  */
  if (TYPE_FIELD_TYPE (type, field) == NULL)
    {
      arg.reset (Py_None);
      Py_INCREF (arg.get ());
    }
  else
    arg.reset (type_to_type_object (TYPE_FIELD_TYPE (type, field)));
  if (arg == NULL)
    return NULL;
  if (PyObject_SetAttrString (result.get (), "type", arg.get ()) < 0)
    return NULL;

  return result.release ();
}