Blame _dbus_bindings/abstract.c

Packit 130fc8
/* Subclasses of built-in Python types supporting extra D-Bus functionality.
Packit 130fc8
 *
Packit 130fc8
 * Copyright (C) 2006-2007 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
/* Dict indexed by object IDs, whose values are nonzero variant levels
Packit 130fc8
 * for immutable variable-sized D-Bus data types (_LongBase, _StrBase, Struct).
Packit 130fc8
 *
Packit 130fc8
 * This is a strange way to store them, but adding a __dict__ to the offending
Packit 130fc8
 * objects seems even more error-prone, given that their sizes are variable!
Packit 130fc8
 */
Packit 130fc8
PyObject *_dbus_py_variant_levels = NULL;
Packit 130fc8
Packit 130fc8
long
Packit 130fc8
dbus_py_variant_level_get(PyObject *obj)
Packit 130fc8
{
Packit 130fc8
    PyObject *vl_obj;
Packit 130fc8
    PyObject *key = PyLong_FromVoidPtr(obj);
Packit 130fc8
    long variant_level;
Packit 130fc8
Packit 130fc8
    if (!key) {
Packit 130fc8
        return -1;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    vl_obj = PyDict_GetItem(_dbus_py_variant_levels, key);
Packit 130fc8
    Py_CLEAR(key);
Packit 130fc8
Packit 130fc8
    if (!vl_obj) {
Packit 130fc8
        /* PyDict_GetItem() does not set an exception when the key is missing.
Packit 130fc8
         * In our case, it just means that there was no entry in the variant
Packit 130fc8
         * dictionary for this object.  Semantically, this is equivalent to a
Packit 130fc8
         * variant level of 0.
Packit 130fc8
         */
Packit 130fc8
        return 0;
Packit 130fc8
    }
Packit 130fc8
    variant_level = NATIVEINT_ASLONG(vl_obj);
Packit 130fc8
    if (variant_level == -1 && PyErr_Occurred()) {
Packit 130fc8
        /* variant_level < 0 can never be inserted into the dictionary; see
Packit 130fc8
         * dbus_py_variant_level_set() below.  The semantics of setting
Packit 130fc8
         * variant_level < 0 is to delete it from the dictionary.
Packit 130fc8
         */
Packit 130fc8
        return -1;
Packit 130fc8
    }
Packit 130fc8
    assert(variant_level >= 0);
Packit 130fc8
    return variant_level;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
dbus_bool_t
Packit 130fc8
dbus_py_variant_level_set(PyObject *obj, long variant_level)
Packit 130fc8
{
Packit 130fc8
    /* key is the object's ID (= pointer) to avoid referencing it */
Packit 130fc8
    PyObject *key = PyLong_FromVoidPtr(obj);
Packit 130fc8
Packit 130fc8
    if (!key) {
Packit 130fc8
        return FALSE;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    if (variant_level <= 0) {
Packit 130fc8
        if (PyDict_GetItem (_dbus_py_variant_levels, key)) {
Packit 130fc8
            if (PyDict_DelItem (_dbus_py_variant_levels, key) < 0) {
Packit 130fc8
                Py_CLEAR(key);
Packit 130fc8
                return FALSE;
Packit 130fc8
            }
Packit 130fc8
        }
Packit 130fc8
    }
Packit 130fc8
    else {
Packit 130fc8
        PyObject *vl_obj = NATIVEINT_FROMLONG(variant_level);
Packit 130fc8
        if (!vl_obj) {
Packit 130fc8
            Py_CLEAR(key);
Packit 130fc8
            return FALSE;
Packit 130fc8
        }
Packit 130fc8
        if (PyDict_SetItem(_dbus_py_variant_levels, key, vl_obj) < 0) {
Packit 130fc8
            Py_CLEAR(vl_obj);
Packit 130fc8
            Py_CLEAR(key);
Packit 130fc8
            return FALSE;
Packit 130fc8
        }
Packit 130fc8
        Py_CLEAR(vl_obj);
Packit 130fc8
    }
Packit 130fc8
    Py_CLEAR(key);
Packit 130fc8
    return TRUE;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyObject *
Packit 130fc8
dbus_py_variant_level_getattro(PyObject *obj, PyObject *name)
Packit 130fc8
{
Packit 130fc8
    PyObject *key, *value;
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
    if (PyUnicode_CompareWithASCIIString(name, "variant_level"))
Packit 130fc8
        return PyObject_GenericGetAttr(obj, name);
Packit 130fc8
#else
Packit 130fc8
    if (PyBytes_Check(name)) {
Packit 130fc8
        Py_INCREF(name);
Packit 130fc8
    }
Packit 130fc8
    else if (PyUnicode_Check(name)) {
Packit 130fc8
        name = PyUnicode_AsEncodedString(name, NULL, NULL);
Packit 130fc8
        if (!name) {
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
    }
Packit 130fc8
    else {
Packit 130fc8
        PyErr_SetString(PyExc_TypeError, "attribute name must be string");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    if (strcmp(PyBytes_AS_STRING(name), "variant_level")) {
Packit 130fc8
        value = PyObject_GenericGetAttr(obj, name);
Packit 130fc8
        Py_CLEAR(name);
Packit 130fc8
        return value;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    Py_CLEAR(name);
Packit 130fc8
#endif  /* PY3 */
Packit 130fc8
Packit 130fc8
    key = PyLong_FromVoidPtr(obj);
Packit 130fc8
Packit 130fc8
    if (!key) {
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    value = PyDict_GetItem(_dbus_py_variant_levels, key);
Packit 130fc8
    Py_CLEAR(key);
Packit 130fc8
Packit 130fc8
    if (!value)
Packit 130fc8
        return NATIVEINT_FROMLONG(0);
Packit 130fc8
    Py_INCREF(value);
Packit 130fc8
    return value;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
/* To be invoked by destructors. Clear the variant level without touching the
Packit 130fc8
 * exception state */
Packit 130fc8
void
Packit 130fc8
dbus_py_variant_level_clear(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    PyObject *et, *ev, *etb;
Packit 130fc8
Packit 130fc8
    /* avoid clobbering any pending exception */
Packit 130fc8
    PyErr_Fetch(&et, &ev, &etb;;
Packit 130fc8
    if (!dbus_py_variant_level_set(self, 0)) {
Packit 130fc8
        /* should never happen */
Packit 130fc8
        PyErr_WriteUnraisable(self);
Packit 130fc8
    }
Packit 130fc8
    PyErr_Restore(et, ev, etb);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
#ifndef PY3
Packit 130fc8
/* Support code for int subclasses. ================================== */
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(DBusPythonInt_tp_doc,\
Packit 130fc8
"Base class for int subclasses with a ``variant_level`` attribute.\n"
Packit 130fc8
"Do not rely on the existence of this class outside dbus-python.\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
static PyMemberDef DBusPythonInt_tp_members[] = {
Packit 130fc8
    {"variant_level", T_LONG, offsetof(DBusPyIntBase, variant_level),
Packit 130fc8
     READONLY,
Packit 130fc8
     "The number of nested variants wrapping the real data. "
Packit 130fc8
     "0 if not in a variant."},
Packit 130fc8
    {NULL},
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonInt_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    PyObject *self;
Packit 130fc8
    long variantness = 0;
Packit 130fc8
    static char *argnames[] = {"variant_level", NULL};
Packit 130fc8
Packit 130fc8
    if (PyTuple_Size(args) > 1) {
Packit 130fc8
        PyErr_SetString(PyExc_TypeError,
Packit 130fc8
                        "__new__ takes at most one positional parameter");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple, kwargs,
Packit 130fc8
                                     "|l:__new__", argnames,
Packit 130fc8
                                     &variantness)) return NULL;
Packit 130fc8
    if (variantness < 0) {
Packit 130fc8
        PyErr_SetString(PyExc_ValueError,
Packit 130fc8
                        "variant_level must be non-negative");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    self = (PyInt_Type.tp_new)(cls, args, NULL);
Packit 130fc8
    if (self) {
Packit 130fc8
        ((DBusPyIntBase *)self)->variant_level = variantness;
Packit 130fc8
    }
Packit 130fc8
    return self;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonInt_tp_repr(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    PyObject *parent_repr = (PyInt_Type.tp_repr)(self);
Packit 130fc8
    long variant_level = ((DBusPyIntBase *)self)->variant_level;
Packit 130fc8
    PyObject *my_repr;
Packit 130fc8
Packit 130fc8
    if (!parent_repr) return NULL;
Packit 130fc8
    if (variant_level > 0) {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V, variant_level=%ld)",
Packit 130fc8
                                       Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr),
Packit 130fc8
                                       variant_level);
Packit 130fc8
    }
Packit 130fc8
    else {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V)", Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr));
Packit 130fc8
    }
Packit 130fc8
    /* whether my_repr is NULL or not: */
Packit 130fc8
    Py_CLEAR(parent_repr);
Packit 130fc8
    return my_repr;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyTypeObject DBusPyIntBase_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "_dbus_bindings._IntBase",
Packit 130fc8
    sizeof(DBusPyIntBase),
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
    DBusPythonInt_tp_repr,                  /* 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
    DBusPythonInt_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
    0,                                      /* tp_iter */
Packit 130fc8
    0,                                      /* tp_iternext */
Packit 130fc8
    0,                                      /* tp_methods */
Packit 130fc8
    DBusPythonInt_tp_members,               /* tp_members */
Packit 130fc8
    0,                                      /* tp_getset */
Packit 130fc8
    DEFERRED_ADDRESS(&PyInt_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
    PyType_GenericAlloc,                    /* tp_alloc */
Packit 130fc8
    DBusPythonInt_tp_new,                   /* tp_new */
Packit 130fc8
    PyObject_Del,                           /* tp_free */
Packit 130fc8
};
Packit 130fc8
#endif  /* !PY3 */
Packit 130fc8
Packit 130fc8
/* Support code for float subclasses. ================================ */
Packit 130fc8
Packit 130fc8
/* There's only one subclass at the moment (Double) but these are factored
Packit 130fc8
out to make room for Float later. (Float is implemented and #if'd out) */
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(DBusPythonFloat_tp_doc,\
Packit 130fc8
"Base class for float subclasses with a ``variant_level`` attribute.\n"
Packit 130fc8
"Do not rely on the existence of this class outside dbus-python.\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
static PyMemberDef DBusPythonFloat_tp_members[] = {
Packit 130fc8
    {"variant_level", T_LONG, offsetof(DBusPyFloatBase, variant_level),
Packit 130fc8
     READONLY,
Packit 130fc8
     "The number of nested variants wrapping the real data. "
Packit 130fc8
     "0 if not in a variant."},
Packit 130fc8
    {NULL},
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonFloat_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    PyObject *self;
Packit 130fc8
    long variantness = 0;
Packit 130fc8
    static char *argnames[] = {"variant_level", NULL};
Packit 130fc8
Packit 130fc8
    if (PyTuple_Size(args) > 1) {
Packit 130fc8
        PyErr_SetString(PyExc_TypeError,
Packit 130fc8
                        "__new__ takes at most one positional parameter");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple, kwargs,
Packit 130fc8
                                     "|l:__new__", argnames,
Packit 130fc8
                                     &variantness)) return NULL;
Packit 130fc8
    if (variantness < 0) {
Packit 130fc8
        PyErr_SetString(PyExc_ValueError,
Packit 130fc8
                        "variant_level must be non-negative");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    self = (PyFloat_Type.tp_new)(cls, args, NULL);
Packit 130fc8
    if (self) {
Packit 130fc8
        ((DBusPyFloatBase *)self)->variant_level = variantness;
Packit 130fc8
    }
Packit 130fc8
    return self;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonFloat_tp_repr(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    PyObject *parent_repr = (PyFloat_Type.tp_repr)(self);
Packit 130fc8
    long variant_level = ((DBusPyFloatBase *)self)->variant_level;
Packit 130fc8
    PyObject *my_repr;
Packit 130fc8
Packit 130fc8
    if (!parent_repr) return NULL;
Packit 130fc8
    if (variant_level > 0) {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V, variant_level=%ld)",
Packit 130fc8
                                       Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr),
Packit 130fc8
                                       variant_level);
Packit 130fc8
    }
Packit 130fc8
    else {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V)", Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr));
Packit 130fc8
    }
Packit 130fc8
    /* whether my_repr is NULL or not: */
Packit 130fc8
    Py_CLEAR(parent_repr);
Packit 130fc8
    return my_repr;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyTypeObject DBusPyFloatBase_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "_dbus_bindings._FloatBase",
Packit 130fc8
    sizeof(DBusPyFloatBase),
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
    DBusPythonFloat_tp_repr,                /* 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
    DBusPythonFloat_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
    0,                                      /* tp_iter */
Packit 130fc8
    0,                                      /* tp_iternext */
Packit 130fc8
    0,                                      /* tp_methods */
Packit 130fc8
    DBusPythonFloat_tp_members,             /* tp_members */
Packit 130fc8
    0,                                      /* tp_getset */
Packit 130fc8
    DEFERRED_ADDRESS(&PyFloat_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
    DBusPythonFloat_tp_new,                 /* tp_new */
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
/* Support code for bytes subclasses ================================== */
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(DBusPythonBytes_tp_doc,\
Packit 130fc8
"Base class for bytes subclasses with a ``variant_level`` attribute.\n"
Packit 130fc8
"Do not rely on the existence of this class outside dbus-python.\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonBytes_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    PyObject *self;
Packit 130fc8
    long variantness = 0;
Packit 130fc8
    static char *argnames[] = {"variant_level", NULL};
Packit 130fc8
Packit 130fc8
    if (PyTuple_Size(args) > 1) {
Packit 130fc8
        PyErr_SetString(PyExc_TypeError,
Packit 130fc8
                        "__new__ takes at most one positional parameter");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple, kwargs,
Packit 130fc8
                                     "|l:__new__", argnames,
Packit 130fc8
                                     &variantness))
Packit 130fc8
        return NULL;
Packit 130fc8
Packit 130fc8
    if (variantness < 0) {
Packit 130fc8
        PyErr_SetString(PyExc_ValueError,
Packit 130fc8
                       "variant_level must be non-negative");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    self = (PyBytes_Type.tp_new)(cls, args, NULL);
Packit 130fc8
    if (self) {
Packit 130fc8
        if (!dbus_py_variant_level_set(self, variantness)) {
Packit 130fc8
            Py_CLEAR(self);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
    }
Packit 130fc8
    return self;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonBytes_tp_repr(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    PyObject *parent_repr = (PyBytes_Type.tp_repr)(self);
Packit 130fc8
    PyObject *vl_obj;
Packit 130fc8
    PyObject *my_repr;
Packit 130fc8
    long variant_level;
Packit 130fc8
Packit 130fc8
    if (!parent_repr) return NULL;
Packit 130fc8
    vl_obj = PyObject_GetAttr(self, dbus_py_variant_level_const);
Packit 130fc8
    if (!vl_obj) {
Packit 130fc8
        Py_CLEAR(parent_repr);
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    variant_level = NATIVEINT_ASLONG(vl_obj);
Packit 130fc8
    Py_CLEAR(vl_obj);
Packit 130fc8
    if (variant_level == -1 && PyErr_Occurred()) {
Packit 130fc8
        Py_CLEAR(parent_repr);
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (variant_level > 0) {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V, variant_level=%ld)",
Packit 130fc8
                                       Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr),
Packit 130fc8
                                       variant_level);
Packit 130fc8
    }
Packit 130fc8
    else {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V)", Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr));
Packit 130fc8
    }
Packit 130fc8
    /* whether my_repr is NULL or not: */
Packit 130fc8
    Py_CLEAR(parent_repr);
Packit 130fc8
    return my_repr;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static void
Packit 130fc8
DBusPyBytesBase_tp_dealloc(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    dbus_py_variant_level_clear(self);
Packit 130fc8
    (PyBytes_Type.tp_dealloc)(self);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyTypeObject DBusPyBytesBase_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "_dbus_bindings._BytesBase",
Packit 130fc8
    0,
Packit 130fc8
    0,
Packit 130fc8
    DBusPyBytesBase_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
    DBusPythonBytes_tp_repr,                /* 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
    dbus_py_variant_level_getattro,         /* tp_getattro */
Packit 130fc8
    dbus_py_immutable_setattro,             /* tp_setattro */
Packit 130fc8
    0,                                      /* tp_as_buffer */
Packit 130fc8
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Packit 130fc8
    DBusPythonBytes_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
    0,                                      /* 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(&PyBytes_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
    DBusPythonBytes_tp_new,                 /* tp_new */
Packit 130fc8
};
Packit 130fc8
#endif  /* PY3 */
Packit 130fc8
Packit 130fc8
/* Support code for str subclasses ================================== */
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(DBusPythonString_tp_doc,\
Packit 130fc8
"Base class for str subclasses with a ``variant_level`` attribute.\n"
Packit 130fc8
"Do not rely on the existence of this class outside dbus-python.\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonString_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    PyObject *self;
Packit 130fc8
    long variantness = 0;
Packit 130fc8
    static char *argnames[] = {"variant_level", NULL};
Packit 130fc8
Packit 130fc8
    if (PyTuple_Size(args) > 1) {
Packit 130fc8
        PyErr_SetString(PyExc_TypeError,
Packit 130fc8
                        "__new__ takes at most one positional parameter");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple, kwargs,
Packit 130fc8
                                     "|l:__new__", argnames,
Packit 130fc8
                                     &variantness)) return NULL;
Packit 130fc8
    if (variantness < 0) {
Packit 130fc8
        PyErr_SetString(PyExc_ValueError,
Packit 130fc8
                        "variant_level must be non-negative");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    self = (NATIVESTR_TYPE.tp_new)(cls, args, NULL);
Packit 130fc8
    if (self) {
Packit 130fc8
        if (!dbus_py_variant_level_set(self, variantness)) {
Packit 130fc8
            Py_CLEAR(self);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
    }
Packit 130fc8
    return self;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonString_tp_repr(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    PyObject *parent_repr = (NATIVESTR_TYPE.tp_repr)(self);
Packit 130fc8
    PyObject *vl_obj;
Packit 130fc8
    PyObject *my_repr;
Packit 130fc8
    long variant_level;
Packit 130fc8
Packit 130fc8
    if (!parent_repr) return NULL;
Packit 130fc8
    vl_obj = PyObject_GetAttr(self, dbus_py_variant_level_const);
Packit 130fc8
    if (!vl_obj) {
Packit 130fc8
        Py_CLEAR(parent_repr);
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    variant_level = NATIVEINT_ASLONG(vl_obj);
Packit 130fc8
    Py_CLEAR(vl_obj);
Packit 130fc8
    if (variant_level == -1 && PyErr_Occurred()) {
Packit 130fc8
        Py_CLEAR(parent_repr);
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    if (variant_level > 0) {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V, variant_level=%ld)",
Packit 130fc8
                                       Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr),
Packit 130fc8
                                       variant_level);
Packit 130fc8
    }
Packit 130fc8
    else {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V)", Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr));
Packit 130fc8
    }
Packit 130fc8
    /* whether my_repr is NULL or not: */
Packit 130fc8
    Py_CLEAR(parent_repr);
Packit 130fc8
    return my_repr;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static void
Packit 130fc8
DBusPyStrBase_tp_dealloc(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    dbus_py_variant_level_clear(self);
Packit 130fc8
    (NATIVESTR_TYPE.tp_dealloc)(self);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyTypeObject DBusPyStrBase_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "_dbus_bindings._StrBase",
Packit 130fc8
    0,
Packit 130fc8
    0,
Packit 130fc8
    DBusPyStrBase_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
    DBusPythonString_tp_repr,               /* 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
    dbus_py_variant_level_getattro,         /* tp_getattro */
Packit 130fc8
    dbus_py_immutable_setattro,             /* tp_setattro */
Packit 130fc8
    0,                                      /* tp_as_buffer */
Packit 130fc8
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Packit 130fc8
    DBusPythonString_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
    0,                                      /* 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(&NATIVESTR_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
    DBusPythonString_tp_new,                /* tp_new */
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
/* Support code for long subclasses ================================= */
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(DBusPythonLong_tp_doc,\
Packit 130fc8
"Base class for ``long`` subclasses with a ``variant_level`` attribute.\n"
Packit 130fc8
"Do not rely on the existence of this class outside dbus-python.\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonLong_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    PyObject *self;
Packit 130fc8
    long variantness = 0;
Packit 130fc8
    static char *argnames[] = {"variant_level", NULL};
Packit 130fc8
Packit 130fc8
    if (PyTuple_Size(args) > 1) {
Packit 130fc8
        PyErr_SetString(PyExc_TypeError,
Packit 130fc8
                        "__new__ takes at most one positional parameter");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple, kwargs,
Packit 130fc8
                                     "|l:__new__", argnames,
Packit 130fc8
                                     &variantness)) return NULL;
Packit 130fc8
    if (variantness < 0) {
Packit 130fc8
        PyErr_SetString(PyExc_ValueError,
Packit 130fc8
                        "variant_level must be non-negative");
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    self = (PyLong_Type.tp_new)(cls, args, NULL);
Packit 130fc8
    if (self) {
Packit 130fc8
        if (!dbus_py_variant_level_set(self, variantness)) {
Packit 130fc8
            Py_CLEAR(self);
Packit 130fc8
            return NULL;
Packit 130fc8
        }
Packit 130fc8
    }
Packit 130fc8
    return self;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
DBusPythonLong_tp_repr(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    PyObject *parent_repr = (PyLong_Type.tp_repr)(self);
Packit 130fc8
    PyObject *vl_obj;
Packit 130fc8
    PyObject *my_repr;
Packit 130fc8
    long variant_level;
Packit 130fc8
Packit 130fc8
    if (!parent_repr) return NULL;
Packit 130fc8
    vl_obj = PyObject_GetAttr(self, dbus_py_variant_level_const);
Packit 130fc8
    if (!vl_obj) {
Packit 130fc8
        Py_CLEAR(parent_repr);
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    variant_level = NATIVEINT_ASLONG(vl_obj);
Packit 130fc8
    Py_CLEAR(vl_obj);
Packit 130fc8
    if (variant_level < 0 && PyErr_Occurred()) {
Packit 130fc8
        Py_CLEAR(parent_repr);
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
Packit 130fc8
    if (variant_level) {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V, variant_level=%ld)",
Packit 130fc8
                                       Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr),
Packit 130fc8
                                       variant_level);
Packit 130fc8
    }
Packit 130fc8
    else {
Packit 130fc8
        my_repr = PyUnicode_FromFormat("%s(%V)", Py_TYPE(self)->tp_name,
Packit 130fc8
                                       REPRV(parent_repr));
Packit 130fc8
    }
Packit 130fc8
    /* whether my_repr is NULL or not: */
Packit 130fc8
    Py_CLEAR(parent_repr);
Packit 130fc8
    return my_repr;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static void
Packit 130fc8
DBusPyLongBase_tp_dealloc(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    dbus_py_variant_level_clear(self);
Packit 130fc8
    (PyLong_Type.tp_dealloc)(self);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyTypeObject DBusPyLongBase_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "_dbus_bindings._LongBase",
Packit 130fc8
    0,
Packit 130fc8
    0,
Packit 130fc8
    DBusPyLongBase_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
    DBusPythonLong_tp_repr,                 /* 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
    dbus_py_variant_level_getattro,         /* tp_getattro */
Packit 130fc8
    dbus_py_immutable_setattro,             /* tp_setattro */
Packit 130fc8
    0,                                      /* tp_as_buffer */
Packit 130fc8
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Packit 130fc8
    DBusPythonLong_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
    0,                                      /* 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(&PyLong_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
    DBusPythonLong_tp_new,                  /* tp_new */
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
PyObject *dbus_py_variant_level_const = NULL;
Packit 130fc8
PyObject *dbus_py_signature_const = NULL;
Packit 130fc8
PyObject *dbus_py__dbus_object_path__const = NULL;
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
#define INTERN (PyUnicode_InternFromString)
Packit 130fc8
#else
Packit 130fc8
/* Neither Python 2.6 nor 2.7 define the expected PyBytes_InternFromString
Packit 130fc8
 * alias in bytesobject.h.
Packit 130fc8
 */
Packit 130fc8
#define INTERN (PyString_InternFromString)
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
dbus_bool_t
Packit 130fc8
dbus_py_init_abstract(void)
Packit 130fc8
{
Packit 130fc8
    _dbus_py_variant_levels = PyDict_New();
Packit 130fc8
    if (!_dbus_py_variant_levels) return 0;
Packit 130fc8
Packit 130fc8
    dbus_py__dbus_object_path__const = INTERN("__dbus_object_path__");
Packit 130fc8
    if (!dbus_py__dbus_object_path__const) return 0;
Packit 130fc8
Packit 130fc8
    dbus_py_variant_level_const = INTERN("variant_level");
Packit 130fc8
    if (!dbus_py_variant_level_const) return 0;
Packit 130fc8
Packit 130fc8
    dbus_py_signature_const = INTERN("signature");
Packit 130fc8
    if (!dbus_py_signature_const) return 0;
Packit 130fc8
Packit 130fc8
#ifdef PY3
Packit 130fc8
    DBusPyBytesBase_Type.tp_base = &PyBytes_Type;
Packit 130fc8
    if (PyType_Ready(&DBusPyBytesBase_Type) < 0) return 0;
Packit 130fc8
    DBusPyBytesBase_Type.tp_print = NULL;
Packit 130fc8
#else
Packit 130fc8
    DBusPyIntBase_Type.tp_base = &PyInt_Type;
Packit 130fc8
    if (PyType_Ready(&DBusPyIntBase_Type) < 0) return 0;
Packit 130fc8
    /* disable the tp_print copied from PyInt_Type, so tp_repr gets called as
Packit 130fc8
    desired */
Packit 130fc8
    DBusPyIntBase_Type.tp_print = NULL;
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
    DBusPyFloatBase_Type.tp_base = &PyFloat_Type;
Packit 130fc8
    if (PyType_Ready(&DBusPyFloatBase_Type) < 0) return 0;
Packit 130fc8
    DBusPyFloatBase_Type.tp_print = NULL;
Packit 130fc8
Packit 130fc8
    DBusPyLongBase_Type.tp_base = &PyLong_Type;
Packit 130fc8
    if (PyType_Ready(&DBusPyLongBase_Type) < 0) return 0;
Packit 130fc8
    DBusPyLongBase_Type.tp_print = NULL;
Packit 130fc8
Packit 130fc8
    DBusPyStrBase_Type.tp_base = &NATIVESTR_TYPE;
Packit 130fc8
    if (PyType_Ready(&DBusPyStrBase_Type) < 0) return 0;
Packit 130fc8
    DBusPyStrBase_Type.tp_print = NULL;
Packit 130fc8
Packit 130fc8
    return 1;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
dbus_bool_t
Packit 130fc8
dbus_py_insert_abstract_types(PyObject *this_module)
Packit 130fc8
{
Packit 130fc8
    /* PyModule_AddObject steals a ref */
Packit 130fc8
#ifdef PY3
Packit 130fc8
    Py_INCREF(&DBusPyBytesBase_Type);
Packit 130fc8
    if (PyModule_AddObject(this_module, "_BytesBase",
Packit 130fc8
                           (PyObject *)&DBusPyBytesBase_Type) < 0) return 0;
Packit 130fc8
#else
Packit 130fc8
    Py_INCREF(&DBusPyIntBase_Type);
Packit 130fc8
    if (PyModule_AddObject(this_module, "_IntBase",
Packit 130fc8
                           (PyObject *)&DBusPyIntBase_Type) < 0) return 0;
Packit 130fc8
#endif
Packit 130fc8
    Py_INCREF(&DBusPyLongBase_Type);
Packit 130fc8
    Py_INCREF(&DBusPyStrBase_Type);
Packit 130fc8
    Py_INCREF(&DBusPyFloatBase_Type);
Packit 130fc8
    if (PyModule_AddObject(this_module, "_LongBase",
Packit 130fc8
                           (PyObject *)&DBusPyLongBase_Type) < 0) return 0;
Packit 130fc8
    if (PyModule_AddObject(this_module, "_StrBase",
Packit 130fc8
                           (PyObject *)&DBusPyStrBase_Type) < 0) return 0;
Packit 130fc8
    if (PyModule_AddObject(this_module, "_FloatBase",
Packit 130fc8
                           (PyObject *)&DBusPyFloatBase_Type) < 0) return 0;
Packit 130fc8
Packit 130fc8
    return 1;
Packit 130fc8
}