Example #1
0
static int
TDI_RawNodeType_setitem(rawnodeobject *self, PyObject *key, PyObject *value)
{
    PyObject *tmp, *normkey;
    tdi_attr_t *item;
    int subresult;

    if (!(key = ENCODE_NAME(self->node, key)))
        return -1;

    if (!PyString_CheckExact(key) && !PyString_Check(key)) {
        PyErr_SetString(TDI_E_ModelError, "attribute key must be a string");
        Py_DECREF(key);
        return -1;
    }
    if (!(normkey = DECODER_NORMALIZE(self->node, key))) {
        Py_DECREF(key);
        return -1;
    }

    if (!value) {
        subresult = PyDict_DelItem(self->node->attr, normkey);
        if (subresult == -1) {
            if (PyErr_ExceptionMatches(PyExc_KeyError)) {
                PyErr_Clear();
                subresult = 0;
            }
        }
        Py_DECREF(normkey);
        Py_DECREF(key);
        return subresult;
    }

    if (PyUnicode_CheckExact(value) || PyUnicode_Check(value)) {
        if (!(value = ENCODE_UNICODE(self->node, value))) {
            Py_DECREF(normkey);
            Py_DECREF(key);
            return -1;
        }
    }
    else if (!(value = PyObject_Str(value))) {
        Py_DECREF(normkey);
        Py_DECREF(key);
        return -1;
    }

    item = (tdi_attr_t *)PyDict_GetItem(self->node->attr, normkey);
    tmp = tdi_attr_new(item ? item->key : key, value);
    Py_DECREF(value);
    Py_DECREF(key);
    if (!tmp) {
        Py_DECREF(normkey);
        return -1;
    }
    subresult = PyDict_SetItem(self->node->attr, normkey, tmp);
    Py_DECREF(tmp);
    Py_DECREF(normkey);

    return subresult;
}
Example #2
0
File: myson.c Project: S-YOU/myson
void encode_object(PyObject *arg) {
	Py_ssize_t size, i;
	char *p;

	if (PyString_CheckExact(arg)) {
		uint8_t *s, *sEnd;
		*d++ = '"';
		ENCODE_STR(arg, s, sEnd)
		*d++ = '"';
	} else if (PyUnicode_CheckExact(arg)) {
		*d++ = '"';
		Py_UNICODE *u, *uEnd;
		ENCODE_UNICODE(arg, u, uEnd)
		*d++ = '"';
	} else if (PyInt_CheckExact(arg) || PyLong_CheckExact(arg)) {
		i = PyInt_AsSsize_t(arg);
		uint64_t val = i < 0 ? *d++ = '-', (uint64_t)(~i + 1) : i;
		ITOA(val)
	} else if (PyList_CheckExact(arg)) {
Example #3
0
static int
TDI_RawNodeType_setcontent(rawnodeobject *self, PyObject *value,
                           void *closure)
{
    tdi_content_t *tmp;

    if (!value) {
        PyErr_SetString(PyExc_TypeError,
                        "Cannot delete the content attribute");
        return -1;
    }

    if (PyUnicode_CheckExact(value) || PyUnicode_Check(value)) {
        if (!(value = ENCODE_UNICODE(self->node, value)))
            return -1;
    }
    else if (!(value = PyObject_Str(value))) {
        return -1;
    }

    Py_INCREF(tdi_g_empty_dict);
    Py_CLEAR(self->node->namedict);
    self->node->namedict = tdi_g_empty_dict;
    tmp = self->node->content;
    self->node->content = NULL;
    if (!tmp) {
        tmp = tdi_content_new();
    }
    else {
        Py_CLEAR(tmp->clean);
        Py_CLEAR(tmp->with_escapes);
    }
    tmp->clean = value;
    Py_INCREF(value);
    tmp->with_escapes = value;
    self->node->content = tmp;

    return 0;
}