Пример #1
0
int
PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
{
	PyMappingMethods *m;

	if (o == NULL || key == NULL || value == 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, value);

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

	type_error("object does not support item assignment");
	return -1;
}
static PyObject *
Affine_itransform(polypaths_planar_overrideAffineObject *self, PyObject *seq)
{
    Py_ssize_t i;
    Py_ssize_t len;
    PyObject *point;
    polypaths_planar_overrideSeq2Object *varray;
    double x, y, a, b, c, d, e, f;

    a = self->a;
    b = self->b;
    c = self->c;
    d = self->d;
    e = self->e;
    f = self->f;
    assert(polypaths_planar_overrideAffine_Check(self));
    if (polypaths_planar_overrideSeq2_Check(seq)) {
	/* Optimized code path for Seq2s */
	varray = (polypaths_planar_overrideSeq2Object *)seq;
	for (i = 0; i < Py_SIZE(seq); i++) {
	    x = varray->vec[i].x;
	    y = varray->vec[i].y;
	    varray->vec[i].x = x*a + y*d + c;
	    varray->vec[i].y = x*b + y*e + f;
	}
    } else {
		/* General vector sequence */
		len = PySequence_Length(seq);
		if (len == -1) {
			PyErr_SetString(PyExc_TypeError, 
			"Affine.itransform(): Cannot transform non-sequence");
			return NULL;
		}
		for (i = 0; i < len; i++) {
			point = PySequence_GetItem(seq, i);
			if (point == NULL) {
				return NULL;
			}
			if (!polypaths_planar_overrideVec2_Parse(point, &x, &y)) {
				Py_DECREF(point);
				PyErr_Format(PyExc_TypeError, 
					"Affine.itransform(): "
					"Element at position %lu is not a valid vector", i);
				return NULL;
			}
			Py_DECREF(point);
			point = (PyObject *)polypaths_planar_overrideVec2_FromDoubles(x*a + y*d + c, x*b + y*e + f);
			if (point == NULL) {
				return NULL;
			}
			if (PySequence_SetItem(seq, i, point) < 0) {
			Py_DECREF(point);
				return NULL;
			}
			Py_DECREF(point);
		}
    }
    Py_INCREF(Py_None);
    return Py_None;
}
Пример #3
0
int
set_unit_list(
    PyObject* owner,
    const char* propname,
    PyObject* value,
    Py_ssize_t len,
    char (*dest)[72]) {

  PyObject*  unit  = NULL;
  PyObject*  proxy = NULL;
  Py_ssize_t i        = 0;

  if (check_delete(propname, value)) {
    return -1;
  }

  if (!PySequence_Check(value)) {
    PyErr_Format(
        PyExc_TypeError,
        "'%s' must be a sequence of strings",
        propname);
    return -1;
  }

  if (PySequence_Size(value) != len) {
    PyErr_Format(
        PyExc_ValueError,
        "len(%s) must be %u",
        propname,
        (unsigned int)len);
    return -1;
  }

  proxy = PyUnitListProxy_New(owner, len, dest);
  if (proxy == NULL) {
      return -1;
  }

  for (i = 0; i < len; ++i) {
    unit = PySequence_GetItem(value, i);
    if (unit == NULL) {
      Py_DECREF(proxy);
      return -1;
    }

    if (PySequence_SetItem(proxy, i, unit) == -1) {
      Py_DECREF(proxy);
      Py_DECREF(unit);
      return -1;
    }

    Py_DECREF(unit);
  }

  Py_DECREF(proxy);

  return 0;
}
Пример #4
0
static PyObject* mqttv3_subscribeMany(PyObject* self, PyObject *args)
{
	MQTTClient c;
	PyObject* topicList;
	PyObject* qosList;

	int count;
	char** topics;
	int* qoss;

	int i, rc = 0;

	if (!PyArg_ParseTuple(args, "kOO", &c, &topicList, &qosList))
		return NULL;

	if (!PySequence_Check(topicList) || !PySequence_Check(qosList))
	{
		PyErr_SetString(PyExc_TypeError,
				"3rd and 4th parameters must be sequences");
		return NULL;
	}

	if ((count = PySequence_Length(topicList)) != PySequence_Length(qosList))
	{
		PyErr_SetString(PyExc_TypeError,
				"3rd and 4th parameters must be sequences of the same length");
		return NULL;
	}

	topics = malloc(count * sizeof(char*));
	for (i = 0; i < count; ++i)
		topics[i] = PyString_AsString(PySequence_GetItem(topicList, i));

	qoss = malloc(count * sizeof(int));
	for (i = 0; i < count; ++i)
		qoss[i] = (int) PyInt_AsLong(PySequence_GetItem(qosList, i));

	Py_BEGIN_ALLOW_THREADS rc = MQTTClient_subscribeMany(c, count, topics,
			qoss);
	Py_END_ALLOW_THREADS

	for (i = 0; i < count; ++i)
		PySequence_SetItem(qosList, i, PyInt_FromLong((long) qoss[i]));

	free(topics);
	free(qoss);

	if (rc == MQTTCLIENT_SUCCESS)
		return Py_BuildValue("iO", rc, qosList);
	else
		return Py_BuildValue("i", rc);
}
Пример #5
0
static int __Pyx_SetItemInt(PyObject *o, Py_ssize_t i, PyObject *v) {
    PyTypeObject *t = o->ob_type;
    int r;
    if (t->tp_as_sequence && t->tp_as_sequence->sq_item)
        r = PySequence_SetItem(o, i, v);
    else {
        PyObject *j = PyInt_FromLong(i);
        if (!j)
            return -1;
        r = PyObject_SetItem(o, j, v);
        Py_DECREF(j);
    }
    return r;
}
Пример #6
0
static int t_sequence_seq_set(t_sequence *self, Py_ssize_t i, PyObject *value)
{
    int result;

    if (value == NULL)
        result = PySequence_DelItem(self->sequence, i);
    else if (self->itemvalue.flags & V_PURE)
        result = PySequence_SetItem(self->sequence, i, value);
    else
    {
        value = _prepareValue(self, value);
        if (!value)
            return -1;

        result = PySequence_SetItem(self->sequence, i, value);
        Py_DECREF(value);
    }

    if (result >= 0)
        result = _t_itemvalue__setDirty((t_itemvalue *) self, 0);

    return result;
}
Пример #7
0
static int _restoreValues(t_sequence *self, PyObject *values)
{
    int size = PySequence_Size(values);
    int i, isTuple;

    if (size < 0)
        return -1;

    isTuple = PyTuple_Check(values);

    for (i = 0; i < size; i++) {
        PyObject *value = PySequence_GetItem(values, i);
        PyObject *v;

        if (!value)
            return -1;

        v = _restoreValue(self, value);
        Py_DECREF(value);
        if (!v)
            return -1;

        if (v == value)
            Py_DECREF(v);
        else if (isTuple)
        {
            if (PyTuple_SetItem(values, i, v) < 0)
            {
                Py_DECREF(v);
                return -1;
            }
        }
        else if (PySequence_SetItem(values, i, v) < 0)
        {
            Py_DECREF(v);
            return -1;
        }
        else
            Py_DECREF(v);
    }

    return 0;
}
static PyObject *
insert_powers1(PyObject *self, PyObject *args)
{
   PyObject *numbers;
   int n;
   
   if (!PyArg_ParseTuple(args, "Oi", &numbers, &n)) {
      return NULL;
   }
   
   PyObject *powers = Py_BuildValue("(iii)", n, n*n, n*n*n);
   
   // Equivalent to Python: numbers[n] = powers
   if (PySequence_SetItem(numbers, n, powers) < 0) {
      return NULL;
   }

   return powers;
}
static PyObject *
insert_powers2(PyObject *self, PyObject *args)
{
   PyObject *numbers;
   int n;
   
   if (!PyArg_ParseTuple(args, "Oi", &numbers, &n)) {
      return NULL;
   }
   
   PyObject *powers = Py_BuildValue("(iii)", n, n*n, n*n*n);
   
   if (PySequence_SetItem(numbers, n, powers) < 0) {
      // Because we won't return powers, we have to discard it.
      Py_DECREF(powers);
      return NULL;
   }

   return powers;
}
Пример #10
0
static PyObject *
SpiDev_xfer2(SpiDevObject *self, PyObject *args)
{
	int status;
	uint16_t delay_usecs = 0;
	uint32_t speed_hz = 0;
	uint8_t bits_per_word = 0;
	uint16_t ii, len;
	PyObject *obj;
	PyObject *seq;
	struct spi_ioc_transfer xfer;
	Py_BEGIN_ALLOW_THREADS
	memset(&xfer, 0, sizeof(xfer));
	Py_END_ALLOW_THREADS
	uint8_t *txbuf, *rxbuf;
	char	wrmsg_text[4096];

	if (!PyArg_ParseTuple(args, "O|IHB:xfer2", &obj, &speed_hz, &delay_usecs, &bits_per_word))
		return NULL;

	seq = PySequence_Fast(obj, "expected a sequence");
	len = PySequence_Fast_GET_SIZE(seq);
	if (!seq || len <= 0) {
		PyErr_SetString(PyExc_TypeError, wrmsg_list0);
		return NULL;
	}

	if (len > SPIDEV_MAXPATH) {
		snprintf(wrmsg_text, sizeof(wrmsg_text) - 1, wrmsg_listmax, SPIDEV_MAXPATH);
		PyErr_SetString(PyExc_OverflowError, wrmsg_text);
		return NULL;
	}

	Py_BEGIN_ALLOW_THREADS
	txbuf = malloc(sizeof(__u8) * len);
	rxbuf = malloc(sizeof(__u8) * len);
	Py_END_ALLOW_THREADS

	for (ii = 0; ii < len; ii++) {
		PyObject *val = PySequence_Fast_GET_ITEM(seq, ii);
#if PY_MAJOR_VERSION < 3
		if (PyInt_Check(val)) {
			txbuf[ii] = (__u8)PyInt_AS_LONG(val);
		} else
#endif
		{
			if (PyLong_Check(val)) {
				txbuf[ii] = (__u8)PyLong_AS_LONG(val);
			} else {
				snprintf(wrmsg_text, sizeof (wrmsg_text) - 1, wrmsg_val, val);
				PyErr_SetString(PyExc_TypeError, wrmsg_text);
				free(txbuf);
				free(rxbuf);
				return NULL;
			}
		}
	}

	if (PyTuple_Check(obj)) {
		Py_DECREF(seq);
		seq = PySequence_List(obj);
	}

	Py_BEGIN_ALLOW_THREADS
	xfer.tx_buf = (unsigned long)txbuf;
	xfer.rx_buf = (unsigned long)rxbuf;
	xfer.len = len;
	xfer.delay_usecs = delay_usecs;
	xfer.speed_hz = speed_hz ? speed_hz : self->max_speed_hz;
	xfer.bits_per_word = bits_per_word ? bits_per_word : self->bits_per_word;

	status = ioctl(self->fd, SPI_IOC_MESSAGE(1), &xfer);
	Py_END_ALLOW_THREADS
	if (status < 0) {
		PyErr_SetFromErrno(PyExc_IOError);
		free(txbuf);
		free(rxbuf);
		return NULL;
	}

	for (ii = 0; ii < len; ii++) {
		PyObject *val = Py_BuildValue("l", (long)rxbuf[ii]);
		PySequence_SetItem(seq, ii, val);
	}
	// WA:
	// in CS_HIGH mode CS isnt pulled to low after transfer
	// reading 0 bytes doesn't really matter but brings CS down
	// tomdean:
	// Stop generating an extra CS except in mode CS_HOGH
	if (self->mode & SPI_CS_HIGH) status = read(self->fd, &rxbuf[0], 0);

	Py_BEGIN_ALLOW_THREADS
	free(txbuf);
	free(rxbuf);
	Py_END_ALLOW_THREADS


	if (PyTuple_Check(obj)) {
		PyObject *old = seq;
		seq = PySequence_Tuple(seq);
		Py_DECREF(old);
	}

	return seq;
}
 void Sequence::setItem(int index, const Object &obj)
 {
     PySequence_SetItem(mPtr, index, obj.borrowReference());
     PW_PyExcept_Check("Sequence::setItem");
 }