Exemplo n.º 1
0
// Create a new Element
    PyObject *
    MakeElement(PyObject *self, PyObject *args)
    {
        long elmid;
        PyObject *lst;
        // (char*) needed to avoid compiler warning (python's fault)
        int ok = PyArg_ParseTuple(args, (char*) "lO", &elmid, &lst);


        if (!ok)
            return NULL;
        Scm code(lst);

        // add factory call
        Element *elm = g_elementFactory.Create(elmid);

        if (elm == NULL || !elm->Build(elmid, code)) {
            if (elm)
                delete elm;
            Error("error constructing element");
            SetException();
            return NULL;
        }
        elm->IncRef();


        // return element address
        PyObject *addr = PyInt_FromLong(Element2Id(elm));
        return addr;
    }
Exemplo n.º 2
0
Element *GetElementFromObject(PyObject *obj)
{
    if (PyObject_HasAttrString(obj, (char*) "ptr")) {
        PyObject* ptr = PyObject_GetAttrString(obj, (char*) "ptr");
        long addr = PyLong_AsLong(ptr);
        Py_DECREF(ptr);
        
        return Id2Element(addr); //(Element*) addr;
    } else {
        // python code
        // NOTE: I think the only element that uses this code is a color
        // construct outside any graphic
        
        // do nothing if not a tuple
        // and check that header is int
        if (!PyTuple_Check(obj) ||
            PyTuple_GET_SIZE(obj) < 1 ||
            !PyInt_Check(PyTuple_GET_ITEM(obj, 0)))
            return NULL;
        
        // build element based on header
        int elmid = (int) PyInt_AsLong(PyTuple_GET_ITEM(obj, 0));

        Element *elm = g_elementFactory.Create(elmid);
        if (elm == NULL) {
            // invalid elmid, return error
            return NULL;
        }
        
        Scm code = Py2ScmTake(PyTuple_GetSlice(obj, 1, PyTuple_GET_SIZE(obj)));
        
        if (!elm->Build(elmid, code)) {
            delete elm;
            return NULL;
        }
        
        // return the built element
        return elm;
    }
}