예제 #1
0
PyObject *
PyObject_GetItem(PyObject *o, PyObject *key)
{
	PyMappingMethods *m;

	if (o == NULL || key == NULL)
		return null_error();

	m = o->ob_type->tp_as_mapping;
	if (m && m->mp_subscript)
		return m->mp_subscript(o, key);

	if (o->ob_type->tp_as_sequence) {
		if (PyInt_Check(key))
			return PySequence_GetItem(o, PyInt_AsLong(key));
		else if (PyLong_Check(key)) {
			long key_value = PyLong_AsLong(key);
			if (key_value == -1 && PyErr_Occurred())
				return NULL;
			return PySequence_GetItem(o, key_value);
		}
		return type_error("sequence index must be integer");
	}

	return type_error("unsubscriptable object");
}
예제 #2
0
int
PyObject_DelItem(PyObject *o, PyObject *key)
{
	PyMappingMethods *m;

	if (o == NULL || key == NULL) {
		null_error();
		return -1;
	}
	m = o->ob_type->tp_as_mapping;
	if (m && m->mp_ass_subscript)
		return m->mp_ass_subscript(o, key, (PyObject*)NULL);

	if (o->ob_type->tp_as_sequence) {
		if (PyInt_Check(key))
			return PySequence_DelItem(o, PyInt_AsLong(key));
		else if (PyLong_Check(key)) {
			long key_value = PyLong_AsLong(key);
			if (key_value == -1 && PyErr_Occurred())
				return -1;
			return PySequence_DelItem(o, key_value);
		}
		type_error("sequence index must be integer");
		return -1;
	}

	type_error("object does not support item deletion");
	return -1;
}
예제 #3
0
파일: key.c 프로젝트: dw/acid
/**
 * Fetch a slice of the Key.
 */
static PyObject *
key_subscript(Key *self, PyObject *key)
{
    if(PySlice_Check(key)) {
        // TODO: make this more efficient
        PyObject *tup = PySequence_Tuple((PyObject *) self);
        PyObject *slice = NULL;
        PyObject *newkey = NULL;
        if(tup) {
            PyMappingMethods *funcs = tup->ob_type->tp_as_mapping;
            slice = funcs->mp_subscript((PyObject *) tup, key);
            Py_DECREF(tup);
        }
        if(slice) {
            newkey = key_new(&KeyType, slice, NULL);
            Py_DECREF(slice);
        }
        return newkey;
    } else {
        // Fetch the `i`th item from the Key.
        Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_OverflowError);
        if(i == -1 && PyErr_Occurred()) {
            return NULL;
        }
        struct reader rdr;
        rdr.p = Key_DATA(self);
        rdr.e = Key_SIZE(self) + rdr.p;
        int eof = rdr.p == rdr.e;

        if(i < 0) {
            // TODO: make this more efficient
            Py_ssize_t len = key_length(self);
            i += len;
            eof |= i < 0;
        }
        while(i-- && !eof) {
            if(acid_skip_element(&rdr, &eof)) {
                return NULL;
            }
        }
        if(eof) {
            PyErr_SetString(PyExc_IndexError, "Key index out of range");
            return NULL;
        }
        return acid_read_element(&rdr);
    }
}
예제 #4
0
int
PyMapping_Size(PyObject *o)
{
	PyMappingMethods *m;

	if (o == NULL) {
		null_error();
		return -1;
	}

	m = o->ob_type->tp_as_mapping;
	if (m && m->mp_length)
		return m->mp_length(o);

	type_error("len() of unsized object");
	return -1;
}