Blame _dbus_bindings/string.c

Packit 130fc8
/* Simple D-Bus types: ObjectPath and other string types.
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 "types-internal.h"
Packit 130fc8
#include <structmember.h>
Packit 130fc8
Packit 130fc8
#ifndef PY3
Packit 130fc8
/* UTF-8 string representation ====================================== */
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(UTF8String_tp_doc,
Packit 130fc8
"A string represented using UTF-8 - a subtype of `str`.\n"
Packit 130fc8
"\n"
Packit 130fc8
"All strings on D-Bus are required to be valid Unicode; in the \"wire\n"
Packit 130fc8
"protocol\" they're transported as UTF-8.\n"
Packit 130fc8
"\n"
Packit 130fc8
"By default, when byte arrays are converted from D-Bus to Python, they\n"
Packit 130fc8
"come out as a `dbus.String`, which is a subtype of `unicode`.\n"
Packit 130fc8
"If you prefer to get UTF-8 strings (as instances of this class) or you\n"
Packit 130fc8
"want to avoid the conversion overhead of going from UTF-8 to Python's\n"
Packit 130fc8
"internal Unicode representation, you can pass the ``utf8_strings=True``\n"
Packit 130fc8
"keyword argument to any of these methods:\n"
Packit 130fc8
"\n"
Packit 130fc8
"* any D-Bus method proxy, or ``connect_to_signal``, on the objects returned\n"
Packit 130fc8
"  by `Bus.get_object`\n"
Packit 130fc8
"* any D-Bus method on a `dbus.Interface`\n"
Packit 130fc8
"* `dbus.Interface.connect_to_signal`\n"
Packit 130fc8
"* `Bus.add_signal_receiver`\n"
Packit 130fc8
"\n"
Packit 130fc8
"\n"
Packit 130fc8
"Constructor::\n"
Packit 130fc8
"\n"
Packit 130fc8
"    dbus.UTF8String(value: str or unicode[, variant_level: int]) -> UTF8String\n"
Packit 130fc8
"\n"
Packit 130fc8
"If value is a str object it must be valid UTF-8.\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 string, this is represented in Python by a\n"
Packit 130fc8
"    String or UTF8String with variant_level==2.\n"
Packit 130fc8
":Since: 0.80 (in older versions, use dbus.String)\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
UTF8String_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    const char *str = NULL;
Packit 130fc8
    long variantness = 0;
Packit 130fc8
    static char *argnames[] = {"value", "variant_level", NULL};
Packit 130fc8
    PyObject *unicode;
Packit 130fc8
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|l:__new__", argnames,
Packit 130fc8
                                     &str, &variantness)) return NULL;
Packit 130fc8
    unicode = PyUnicode_DecodeUTF8(str, strlen(str), NULL);
Packit 130fc8
    if (!unicode) return NULL;
Packit 130fc8
    Py_CLEAR(unicode);
Packit 130fc8
    return (DBusPyStrBase_Type.tp_new)(cls, args, kwargs);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyTypeObject DBusPyUTF8String_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "dbus.UTF8String",
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
    UTF8String_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(&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
    UTF8String_tp_new,                      /* tp_new */
Packit 130fc8
};
Packit 130fc8
#endif  /* !PY3 */
Packit 130fc8
Packit 130fc8
/* Object path ====================================================== */
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(ObjectPath_tp_doc,
Packit 130fc8
"A D-Bus object path, such as '/com/example/MyApp/Documents/abc'.\n"
Packit 130fc8
"\n"
Packit 130fc8
"ObjectPath is a subtype of str, and object-paths behave like strings.\n"
Packit 130fc8
"\n"
Packit 130fc8
"Constructor::\n"
Packit 130fc8
"\n"
Packit 130fc8
"    dbus.ObjectPath(path: str, variant_level: int) -> ObjectPath\n"
Packit 130fc8
"\n"
Packit 130fc8
"path must be an ASCII string following the syntax of object paths.\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 an object path, this is represented in Python by an\n"
Packit 130fc8
"    ObjectPath with variant_level==2.\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
ObjectPath_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
Packit 130fc8
{
Packit 130fc8
    const char *str = NULL;
Packit 130fc8
    long variantness = 0;
Packit 130fc8
    static char *argnames[] = {"object_path", "variant_level", NULL};
Packit 130fc8
Packit 130fc8
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|l:__new__", argnames,
Packit 130fc8
                                     &str, &variantness)) return NULL;
Packit 130fc8
    if (!dbus_py_validate_object_path(str)) {
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    return (DBusPyStrBase_Type.tp_new)(cls, args, kwargs);
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
PyTypeObject DBusPyObjectPath_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "dbus.ObjectPath",
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
    ObjectPath_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(&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
    ObjectPath_tp_new,                      /* tp_new */
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
/* Unicode string representation ==================================== */
Packit 130fc8
Packit 130fc8
PyDoc_STRVAR(String_tp_doc,
Packit 130fc8
"A string represented using Unicode - a subtype of `unicode`.\n"
Packit 130fc8
"\n"
Packit 130fc8
"All strings on D-Bus are required to be valid Unicode; in the \"wire\n"
Packit 130fc8
"protocol\" they're transported as UTF-8.\n"
Packit 130fc8
"\n"
Packit 130fc8
"By default, when strings are converted from D-Bus to Python, they\n"
Packit 130fc8
"come out as this class. If you prefer to get UTF-8 strings (as instances\n"
Packit 130fc8
"of a subtype of `str`) or you want to avoid the conversion overhead of\n"
Packit 130fc8
"going from UTF-8 to Python's internal Unicode representation, see the\n"
Packit 130fc8
"documentation for `dbus.UTF8String`.\n"
Packit 130fc8
"\n"
Packit 130fc8
"Constructor::\n"
Packit 130fc8
"\n"
Packit 130fc8
"    String(value: str or unicode[, variant_level: int]) -> String\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 string, this is represented in Python by a\n"
Packit 130fc8
"    String or UTF8String with variant_level==2.\n"
Packit 130fc8
);
Packit 130fc8
Packit 130fc8
static PyMemberDef String_tp_members[] = {
Packit 130fc8
    {"variant_level", T_LONG, offsetof(DBusPyString, 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
String_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
    self = (PyUnicode_Type.tp_new)(cls, args, NULL);
Packit 130fc8
    if (self) {
Packit 130fc8
        ((DBusPyString *)self)->variant_level = variantness;
Packit 130fc8
    }
Packit 130fc8
    return self;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
static PyObject *
Packit 130fc8
String_tp_repr(PyObject *self)
Packit 130fc8
{
Packit 130fc8
    PyObject *parent_repr = (PyUnicode_Type.tp_repr)(self);
Packit 130fc8
    PyObject *my_repr;
Packit 130fc8
Packit 130fc8
    if (!parent_repr) {
Packit 130fc8
        return NULL;
Packit 130fc8
    }
Packit 130fc8
    if (((DBusPyString *)self)->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
                                       ((DBusPyString *)self)->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 DBusPyString_Type = {
Packit 130fc8
    PyVarObject_HEAD_INIT(DEFERRED_ADDRESS(&PyType_Type), 0)
Packit 130fc8
    "dbus.String",
Packit 130fc8
    sizeof(DBusPyString),
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
    String_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
    PyObject_GenericGetAttr,                /* 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
    String_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
    String_tp_members,                      /* tp_members */
Packit 130fc8
    0,                                      /* tp_getset */
Packit 130fc8
    DEFERRED_ADDRESS(&PyUnicode_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
    String_tp_new,                          /* tp_new */
Packit 130fc8
};
Packit 130fc8
Packit 130fc8
dbus_bool_t
Packit 130fc8
dbus_py_init_string_types(void)
Packit 130fc8
{
Packit 130fc8
    /* don't need to do strange contortions for unicode, since it's not a
Packit 130fc8
     * "variable-size" object (it has a pointer to its data instead)
Packit 130fc8
     */
Packit 130fc8
    if (PyUnicode_Type.tp_itemsize != 0) {
Packit 130fc8
        fprintf(stderr, "dbus-python is not compatible with this version of "
Packit 130fc8
                "Python (unicode objects are assumed to be fixed-size)");
Packit 130fc8
        return 0;
Packit 130fc8
    }
Packit 130fc8
    DBusPyString_Type.tp_base = &PyUnicode_Type;
Packit 130fc8
    if (PyType_Ready(&DBusPyString_Type) < 0) return 0;
Packit 130fc8
    DBusPyString_Type.tp_print = NULL;
Packit 130fc8
Packit 130fc8
#ifndef PY3
Packit 130fc8
    DBusPyUTF8String_Type.tp_base = &DBusPyStrBase_Type;
Packit 130fc8
    if (PyType_Ready(&DBusPyUTF8String_Type) < 0) return 0;
Packit 130fc8
    DBusPyUTF8String_Type.tp_print = NULL;
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
    DBusPyObjectPath_Type.tp_base = &DBusPyStrBase_Type;
Packit 130fc8
    if (PyType_Ready(&DBusPyObjectPath_Type) < 0) return 0;
Packit 130fc8
    DBusPyObjectPath_Type.tp_print = NULL;
Packit 130fc8
Packit 130fc8
    return 1;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
dbus_bool_t
Packit 130fc8
dbus_py_insert_string_types(PyObject *this_module)
Packit 130fc8
{
Packit 130fc8
    /* PyModule_AddObject steals a ref */
Packit 130fc8
    Py_INCREF(&DBusPyObjectPath_Type);
Packit 130fc8
    Py_INCREF(&DBusPyString_Type);
Packit 130fc8
    if (PyModule_AddObject(this_module, "ObjectPath",
Packit 130fc8
                           (PyObject *)&DBusPyObjectPath_Type) < 0) return 0;
Packit 130fc8
    if (PyModule_AddObject(this_module, "String",
Packit 130fc8
                           (PyObject *)&DBusPyString_Type) < 0) return 0;
Packit 130fc8
Packit 130fc8
#ifndef PY3
Packit 130fc8
    Py_INCREF(&DBusPyUTF8String_Type);
Packit 130fc8
    if (PyModule_AddObject(this_module, "UTF8String",
Packit 130fc8
                           (PyObject *)&DBusPyUTF8String_Type) < 0) return 0;
Packit 130fc8
#endif
Packit 130fc8
Packit 130fc8
    return 1;
Packit 130fc8
}
Packit 130fc8
Packit 130fc8
/* vim:set ft=c cino< sw=4 sts=4 et: */