Exemplo n.º 1
0
static PyObject *
Struct_tp_repr(PyObject *self)
{
    PyObject *parent_repr = (PyTuple_Type.tp_repr)((PyObject *)self);
    PyObject *sig;
    PyObject *sig_repr = NULL;
    PyObject *key;
    long variant_level;
    PyObject *my_repr = NULL;

    if (!parent_repr) goto finally;
    key = PyLong_FromVoidPtr(self);
    if (!key) goto finally;
    sig = PyDict_GetItem(struct_signatures, key);
    Py_CLEAR(key);
    if (!sig) sig = Py_None;
    sig_repr = PyObject_Repr(sig);
    if (!sig_repr) goto finally;

    variant_level = dbus_py_variant_level_get(self);
    if (variant_level < 0)
        goto finally;

    if (variant_level > 0) {
        my_repr = PyUnicode_FromFormat("%s(%V, signature=%V, "
                                       "variant_level=%ld)",
                                       Py_TYPE(self)->tp_name,
                                       REPRV(parent_repr),
                                       REPRV(sig_repr),
                                       variant_level);
    }
    else {
        my_repr = PyUnicode_FromFormat("%s(%V, signature=%V)",
                                       Py_TYPE(self)->tp_name,
                                       REPRV(parent_repr),
                                       REPRV(sig_repr));
    }

finally:
    Py_CLEAR(parent_repr);
    Py_CLEAR(sig_repr);
    return my_repr;
}
Exemplo n.º 2
0
static PyObject *
Byte_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
    PyObject *obj;
    PyObject *tuple;
    long variantness = 0;
    static char *argnames[] = {"variant_level", NULL};

    if (PyTuple_Size(args) > 1) {
        PyErr_SetString(PyExc_TypeError, "Byte constructor takes no more "
                        "than one positional argument");
        return NULL;
    }
    if (!PyArg_ParseTupleAndKeywords(dbus_py_empty_tuple, kwargs,
                                     "|l:__new__", argnames,
                                     &variantness)) return NULL;
    if (variantness < 0) {
        PyErr_SetString(PyExc_ValueError,
                        "variant_level must be non-negative");
        return NULL;
    }

    /* obj is a borrowed reference.  It gets turned into an owned reference on
     * the good-path of the if-statements below.
     */
    obj = PyTuple_GetItem(args, 0);

    if (PyBytes_Check(obj)) {
        /* string of length 1, we hope */
        if (PyBytes_GET_SIZE(obj) != 1) {
            goto bad_arg;
        }
        obj = NATIVEINT_FROMLONG((unsigned char)(PyBytes_AS_STRING(obj)[0]));
        if (!obj)
            goto bad_arg;
    }
    else if (INTORLONG_CHECK(obj)) {
        /* on Python 2 this accepts either int or long */
        long i = PyLong_AsLong(obj);
        long my_variant_level;

        if (i == -1 && PyErr_Occurred())
            goto bad_arg;

#ifdef PY3
        my_variant_level = dbus_py_variant_level_get(obj);
        if (my_variant_level < 0)
            return NULL;
#else
        my_variant_level = ((DBusPyIntBase *)obj)->variant_level;
#endif
        if (Py_TYPE(obj) == cls && my_variant_level == variantness) {
            Py_INCREF(obj);
            return obj;
        }
        if (i < 0 || i > 255) goto bad_range;
        /* else make it a new reference */
        Py_INCREF(obj);
    }
    else {
        goto bad_arg;
    }

    /* The tuple steals the reference to obj. */
    tuple = Py_BuildValue("(N)", obj);
    if (!tuple) return NULL;

    obj = DBUS_PY_BYTE_BASE.tp_new(cls, tuple, kwargs);
    Py_CLEAR(tuple);
    return obj;

bad_arg:
    PyErr_SetString(PyExc_TypeError, "Expected a bytes or str of length 1, "
                    "or an int in the range 0-255");
    return NULL;
bad_range:
    PyErr_SetString(PyExc_ValueError, "Integer outside range 0-255");
    return NULL;
}