Blame _dbus_bindings/signature.c

Packit 130fc8
/* Implementation of Signature type for D-Bus bindings.
Packit 130fc8
 *
Packit 130fc8
 * Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
Packit 130fc8
 *
Packit 130fc8
 * Permission is hereby granted, free of charge, to any person
Packit 130fc8
 * obtaining a copy of this software and associated documentation
Packit 130fc8
 * files (the "Software"), to deal in the Software without
Packit 130fc8
 * restriction, including without limitation the rights to use, copy,
Packit 130fc8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
Packit 130fc8
 * of the Software, and to permit persons to whom the Software is
Packit 130fc8
 * furnished to do so, subject to the following conditions:
Packit 130fc8
 *
Packit 130fc8
 * The above copyright notice and this permission notice shall be
Packit 130fc8
 * included in all copies or substantial portions of the Software.
Packit 130fc8
 *
Packit 130fc8
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 130fc8
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 130fc8
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 130fc8
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Packit 130fc8
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
Packit 130fc8
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Packit 130fc8
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit 130fc8
 * DEALINGS IN THE SOFTWARE.
Packit 130fc8
 */
Packit 130fc8
Packit 130fc8
#include "dbus_bindings-internal.h"
Packit 130fc8
Packit 130fc8
#include <Python.h>
Packit 130fc8
#include <structmember.h>
Packit 130fc8
Packit 130fc8
#include "types-internal.h"
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(Signature_tp_doc,
Packit 130fc8
"A string subclass whose values are restricted to valid D-Bus\n"
Packit 130fc8
"signatures. When iterated over, instead of individual characters it\n"
Packit 130fc8
"produces Signature instances representing single complete types.\n"
Packit 130fc8
"\n"
Packit 130fc8
"Constructor::\n"
Packit 130fc8
"\n"
Packit 130fc8
"    ``Signature(value: str or unicode[, variant_level: int]) -> Signature``\n"
Packit 130fc8
"\n"
Packit 130fc8
"``value`` must be a valid D-Bus signature (zero or more single complete\n"
Packit 130fc8
"types).\n"
Packit 130fc8
"\n"
Packit 130fc8
"``variant_level`` must be non-negative; the default is 0.\n"
Packit 130fc8
"\n"
Packit 130fc8
":IVariables:\n"
Packit 130fc8
"  `variant_level` : int\n"
Packit 130fc8
"    Indicates how many nested Variant containers this object\n"
Packit 130fc8
"    is contained in: if a message's wire format has a variant containing a\n"
Packit 130fc8
"    variant containing a signature, this is represented in Python by a\n"
Packit 130fc8
"    Signature with variant_level==2.\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
typedef struct {
Packit 130fc8
    PyObject_HEAD
Packit 130fc8
    PyObject *bytes;
Packit 130fc8
    DBusSignatureIter iter;
Packit 130fc8
} SignatureIter;
Packit 130fc8
Packit 130fc8
static void
Packit 130fc8
SignatureIter_tp_dealloc (SignatureIter *self)
Packit 130fc8
{
Packit 130fc8
    Py_CLEAR(self->bytes);
Packit 130fc8
    PyObject_Del(self);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
SignatureIter_tp_iternext (SignatureIter *self)
Packit 130fc8
{
Packit 130fc8
    char *sig;
Packit 130fc8
    PyObject *obj;
Packit 130fc8
Packit 130fc8
    /* Stop immediately if finished or not correctly initialized */
Packit 130fc8
    if (!self->bytes) return NULL;
Packit 130fc8
Packit 130fc8
    sig = dbus_signature_iter_get_signature(&(self->iter));
Packit 130fc8
    if (!sig) return PyErr_NoMemory();
Packit 130fc8
    obj = PyObject_CallFunction((PyObject *)&DBusPySignature_Type, "s", sig);
Packit 130fc8
    dbus_free(sig);
Packit 130fc8
    if (!obj) return NULL;
Packit 130fc8
Packit 130fc8
    if (!dbus_signature_iter_next(&(self->iter))) {
Packit 130fc8
        /* mark object as having been finished with */
Packit 130fc8
        Py_CLEAR(self->bytes);
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    return obj;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
SignatureIter_tp_iter(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    Py_INCREF(self);
Packit 130fc8
    return self;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyTypeObject SignatureIterType = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "_dbus_bindings._SignatureIter",
Packit 130fc8
    sizeof(SignatureIter),
Packit 130fc8
    0,
Packit 130fc8
    (destructor)SignatureIter_tp_dealloc,   /* tp_dealloc */
Packit 130fc8
    0,                                      /* tp_print */
Packit 130fc8
    0,                                      /* tp_getattr */
Packit 130fc8
    0,                                      /* tp_setattr */
Packit 130fc8
    0,                                      /* tp_compare */
Packit 130fc8
    0,                                      /* tp_repr */
Packit 130fc8
    0,                                      /* tp_as_number */
Packit 130fc8
    0,                                      /* tp_as_sequence */
Packit 130fc8
    0,                                      /* tp_as_mapping */
Packit 130fc8
    0,                                      /* tp_hash */
Packit 130fc8
    0,                                      /* tp_call */
Packit 130fc8
    0,                                      /* tp_str */
Packit 130fc8
    0,                                      /* tp_getattro */
Packit 130fc8
    0,                                      /* tp_setattro */
Packit 130fc8
    0,                                      /* tp_as_buffer */
Packit 130fc8
    Py_TPFLAGS_DEFAULT,                     /* tp_flags */
Packit 130fc8
    0,                                      /* tp_doc */
Packit 130fc8
    0,                                      /* tp_traverse */
Packit 130fc8
    0,                                      /* tp_clear */
Packit 130fc8
    0,                                      /* tp_richcompare */
Packit 130fc8
    0,                                      /* tp_weaklistoffset */
Packit 130fc8
    SignatureIter_tp_iter,                  /* tp_iter */
Packit 130fc8
    (iternextfunc)SignatureIter_tp_iternext,  /* tp_iternext */
Packit 130fc8
    0,                                      /* tp_methods */
Packit 130fc8
    0,                                      /* tp_members */
Packit 130fc8
    0,                                      /* tp_getset */
Packit 130fc8
    0,                                      /* tp_base */
Packit 130fc8
    0,                                      /* tp_dict */
Packit 130fc8
    0,                                      /* tp_descr_get */
Packit 130fc8
    0,                                      /* tp_descr_set */
Packit 130fc8
    0,                                      /* tp_dictoffset */
Packit 130fc8
    0,                                      /* tp_init */
Packit 130fc8
    0,                                      /* tp_alloc */
Packit 130fc8
    /* deliberately not callable! Use iter(Signature) instead */
Packit 130fc8
    0,                                      /* tp_new */
Packit 130fc8
    0,                                      /* tp_free */
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
Signature_tp_iter(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    SignatureIter *iter = PyObject_New(SignatureIter, &SignatureIterType);
Packit 130fc8
    PyObject *self_as_bytes;
Packit 130fc8
Packit 130fc8
    if (!iter) return NULL;
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
    self_as_bytes = PyUnicode_AsUTF8String(self);
Packit 130fc8
    if (!self_as_bytes) {
Packit 130fc8
        Py_CLEAR(iter);
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
#else
Packit 130fc8
    self_as_bytes = self;
Packit 130fc8
    Py_INCREF(self_as_bytes);
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
    if (PyBytes_GET_SIZE(self_as_bytes) > 0) {
Packit 130fc8
        iter->bytes = self_as_bytes;
Packit 130fc8
        dbus_signature_iter_init(&(iter->iter),
Packit 130fc8
                                 PyBytes_AS_STRING(self_as_bytes));
Packit 130fc8
    }
Packit 130fc8
    else {
Packit 130fc8
        /* this is a null string, make a null iterator */
Packit 130fc8
        iter->bytes = NULL;
Packit 130fc8
        Py_CLEAR(self_as_bytes);
Packit 130fc8
    }
Packit 130fc8
    return (PyObject *)iter;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
Signature_tp_new (PyTypeObject *cls, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    const char *str = NULL;
Packit 130fc8
    PyObject *ignored;
Packit 130fc8
    static char *argnames[] = {"object_path", "variant_level", NULL};
Packit 130fc8
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|O:__new__", argnames,
Packit 130fc8
                                     &str, &ignored)) return NULL;
Packit 130fc8
    if (!dbus_signature_validate(str, NULL)) {
Packit 130fc8
        PyErr_SetString(PyExc_ValueError, "Corrupt type signature");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    return (DBusPyStrBase_Type.tp_new)(cls, args, kwargs);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyTypeObject DBusPySignature_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "dbus.Signature",
Packit 130fc8
    0,
Packit 130fc8
    0,
Packit 130fc8
    0,                                      /* tp_dealloc */
Packit 130fc8
    0,                                      /* tp_print */
Packit 130fc8
    0,                                      /* tp_getattr */
Packit 130fc8
    0,                                      /* tp_setattr */
Packit 130fc8
    0,                                      /* tp_compare */
Packit 130fc8
    0,                                      /* tp_repr */
Packit 130fc8
    0,                                      /* tp_as_number */
Packit 130fc8
    0,                                      /* tp_as_sequence */
Packit 130fc8
    0,                                      /* tp_as_mapping */
Packit 130fc8
    0,                                      /* tp_hash */
Packit 130fc8
    0,                                      /* tp_call */
Packit 130fc8
    0,                                      /* tp_str */
Packit 130fc8
    0,                                      /* tp_getattro */
Packit 130fc8
    0,                                      /* tp_setattro */
Packit 130fc8
    0,                                      /* tp_as_buffer */
Packit 130fc8
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Packit 130fc8
    Signature_tp_doc,                       /* tp_doc */
Packit 130fc8
    0,                                      /* tp_traverse */
Packit 130fc8
    0,                                      /* tp_clear */
Packit 130fc8
    0,                                      /* tp_richcompare */
Packit 130fc8
    0,                                      /* tp_weaklistoffset */
Packit 130fc8
    Signature_tp_iter,                      /* tp_iter */
Packit 130fc8
    0,                                      /* tp_iternext */
Packit 130fc8
    0,                                      /* tp_methods */
Packit 130fc8
    0,                                      /* tp_members */
Packit 130fc8
    0,                                      /* tp_getset */
Packit 130fc8
    DEFERRED_ADDRESS(&DBusPyStrBase_Type),  /* tp_base */
Packit 130fc8
    0,                                      /* tp_dict */
Packit 130fc8
    0,                                      /* tp_descr_get */
Packit 130fc8
    0,                                      /* tp_descr_set */
Packit 130fc8
    0,                                      /* tp_dictoffset */
Packit 130fc8
    0,                                      /* tp_init */
Packit 130fc8
    0,                                      /* tp_alloc */
Packit 130fc8
    Signature_tp_new,                       /* tp_new */
Packit 130fc8
    0,                                      /* tp_free */
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
dbus_bool_t
Packit 130fc8
dbus_py_init_signature(void)
Packit 130fc8
{
Packit 130fc8
    if (PyType_Ready(&SignatureIterType) < 0) return 0;
Packit 130fc8
Packit 130fc8
    DBusPySignature_Type.tp_base = &DBusPyStrBase_Type;
Packit 130fc8
    if (PyType_Ready(&DBusPySignature_Type) < 0) return 0;
Packit 130fc8
    DBusPySignature_Type.tp_print = NULL;
Packit 130fc8
Packit 130fc8
    return 1;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
dbus_bool_t
Packit 130fc8
dbus_py_insert_signature(PyObject *this_module)
Packit 130fc8
{
Packit 130fc8
    /* PyModule_AddObject steals a ref */
Packit 130fc8
    Py_INCREF(&DBusPySignature_Type);
Packit 130fc8
    if (PyModule_AddObject(this_module, "Signature",
Packit 130fc8
                           (PyObject *)&DBusPySignature_Type) < 0) return 0;
Packit 130fc8
    Py_INCREF(&SignatureIterType);
Packit 130fc8
    if (PyModule_AddObject(this_module, "_SignatureIter",
Packit 130fc8
                           (PyObject *)&SignatureIterType) < 0) return 0;
Packit 130fc8
Packit 130fc8
    return 1;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
/* vim:set ft=c cino< sw=4 sts=4 et: */