Ejemplo n.º 1
0
QVariant
QPython::getattr(QVariant obj, QString attr) {
    if (!SINCE_API_VERSION(1, 4)) {
        emitError(QString("Import PyOtherSide 1.4 or newer to use getattr()"));
        return QVariant();
    }

    ENSURE_GIL_STATE;

    PyObjectRef pyobj(convertQVariantToPyObject(obj), true);

    if (!pyobj) {
        emitError(QString("Failed to convert %1 to python object: '%1' (%2)").arg(obj.toString()).arg(priv->formatExc()));
        return QVariant();
    }

    QByteArray byteArray = attr.toUtf8();
    const char *attrStr = byteArray.data();

    PyObjectRef o(PyObject_GetAttrString(pyobj.borrow(), attrStr), true);

    if (!o) {
        emitError(QString("Attribute not found: '%1' (%2)").arg(attr).arg(priv->formatExc()));
        return QVariant();
    }

    return convertPyObjectToQVariant(o.borrow());
}
Ejemplo n.º 2
0
/**
 * @brief Constructor specifying an axes subplot spec and optional parent.
 * An Axes with the given subplot specification is created on construction
 * See
 * https://matplotlib.org/2.2.3/api/_as_gen/matplotlib.figure.Figure.html?highlight=add_subplot#matplotlib.figure.Figure.add_subplot
 * @param subplotspec A matplotlib subplot spec defined as a 3-digit integer
 * @param facecolor String denoting the figure's facecolor
 * @param parent The owning parent widget
 */
FigureCanvasQt::FigureCanvasQt(int subplotspec, QWidget *parent)
    : QWidget(parent), InstanceHolder(createPyCanvas(subplotspec), "draw"),
      m_figure(Figure(Python::Object(pyobj().attr("figure")))) {
  // Cannot use delegating constructor here as InstanceHolder needs to be
  // initialized before the axes can be created
  m_mplCanvas = initLayout(this);
}
Ejemplo n.º 3
0
int main()
{
  setup_classes();

  // c = C()
  pyobj c = create_object(C);

  // d = D()
  pyobj d = create_object(D);

  // TODO: call the __init__ method if it exists

#ifdef TAGGING
  pyobj one = inject_int(1);
  pyobj three = inject_int(3);
#else
  pyobj one = create_int(1);
  pyobj three = create_int(3);
#endif

  // c.f = 1
  set_attr(c, "f", one);

  // d.f = 1
  set_attr(d, "f", one);

  pyobj i, j, k, h;

  // i = c.m()
  {
    pyobj meth = get_attr(c, "m");
    pyobj fun = get_function(meth);
    void *fp = get_fun_ptr(fun);
    pyobj (*f)(pyobj) = (pyobj (*)(pyobj)) fp; // cast to a function pointer type
    i = f(get_receiver(meth));
  }

  // j = d.m()
  {
    pyobj meth = get_attr(d, "m");
    pyobj fun = get_function(meth);
    void *fp = get_fun_ptr(fun);
    pyobj (*f)(pyobj) = (pyobj (*)(pyobj)) fp; // cast to a function pointer type
    j = f(get_receiver(meth));
  }

  // d.n(3)
  {
    pyobj (*f)(pyobj, pyobj) = (pyobj (*)(pyobj, pyobj)) get_fun_ptr_from_attr(d, "n");
    f(d, three);
  }

  // k = d.m()
  {
    pyobj meth = get_attr(d, "m");
    pyobj fun = get_function(meth);
    void *fp = get_fun_ptr(fun);
    pyobj (*f)(pyobj) = (pyobj (*)(pyobj)) fp; // cast to a function pointer type
    k = f(get_receiver(meth));
  }

  // h = i + j + k
  {
#ifdef TAGGING
    // optimized, but assumes i and j are integers
    // h = i + j + k

    // unoptimized, but checks that i and j are integers
    h = inject_int(project_int(i) + project_int(j) + project_int(k));
#else
    h = create_int(project_int(i) + project_int(j) + project_int(k));
#endif
  }

  // print i, j, k
  print_any(i);
  print_any(j);
  print_any(k);
  print_any(h);
  return 0;
}