Exemplo n.º 1
0
RCP<const Number> PyNumber::rsub(const Number &other) const {
    PyObject *other_p, *result;
    if (is_a<PyNumber>(other)) {
        other_p = static_cast<const PyNumber &>(other).pyobject_;
        result = PyNumber_Subtract(other_p, pyobject_);
    } else {
        other_p = pymodule_->to_py_(other.rcp_from_this_cast<const Basic>());
        result = PyNumber_Subtract(other_p, pyobject_);
        Py_XDECREF(other_p);
    }
    return make_rcp<PyNumber>(result, pymodule_);
}
Exemplo n.º 2
0
 /** op -.
  * @throw type_err
  */
 set operator - (const obj& o)const
 {
     PyObject* r = PyNumber_Subtract(_p, o.p());
     if(r)
         return r;
     throw type_err("op - failed");        
 }
Exemplo n.º 3
0
static PyObject *Proxy_subtract(PyObject *o1, PyObject *o2)
{
    Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o1);
    Proxy__WRAPPED_REPLACE_OR_RETURN_NULL(o2);

    return PyNumber_Subtract(o1, o2);
}
Exemplo n.º 4
0
static void *PyDateTimeToINT64(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen)
{
  PyObject *obj = (PyObject *) _obj;
  PyObject *date, *ord, *utcoffset;
  int y, m, d, h, mn, s, days;

  utcoffset = PyObject_CallMethod(obj, "utcoffset", NULL);
  if(utcoffset != Py_None){
    obj = PyNumber_Subtract(obj, utcoffset);
  }

  y = PyDateTime_GET_YEAR(obj);
  m = PyDateTime_GET_MONTH(obj);
  d = PyDateTime_GET_DAY(obj);
  h = PyDateTime_DATE_GET_HOUR(obj);
  mn = PyDateTime_DATE_GET_MINUTE(obj);
  s = PyDateTime_DATE_GET_SECOND(obj);

  date = PyDate_FromDate(y, m, 1);
  ord = PyObject_CallMethod(date, "toordinal", NULL);
  days = PyInt_AS_LONG(ord) - EPOCH_ORD + d - 1;
  Py_DECREF(date);
  Py_DECREF(ord);
  *( (JSINT64 *) outValue) = (((JSINT64) ((days * 24 + h) * 60 + mn)) * 60 + s);
  return NULL;
}
Exemplo n.º 5
0
static PyObject *
range_index(rangeobject *r, PyObject *ob)
{
    int contains;

    if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) {
        Py_ssize_t index;
        index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX);
        if (index == -1)
            return NULL;
        return PyLong_FromSsize_t(index);
    }

    contains = range_contains_long(r, ob);
    if (contains == -1)
        return NULL;

    if (contains) {
        PyObject *idx, *tmp = PyNumber_Subtract(ob, r->start);
        if (tmp == NULL)
            return NULL;
        /* idx = (ob - r.start) // r.step */
        idx = PyNumber_FloorDivide(tmp, r->step);
        Py_DECREF(tmp);
        return idx;
    }

    /* object is not in the range */
    PyErr_Format(PyExc_ValueError, "%R is not in range", ob);
    return NULL;
}
Exemplo n.º 6
0
inline CObject operator- (const CObject& a, const CObject& b)
{
    PyObject* tmp_obj = PyNumber_Subtract(a.Get(), b.Get());
    if ( !tmp_obj ) {
        throw CArithmeticError("PyNumber_Subtract");
    }
    return CObject(tmp_obj, eTakeOwnership);
}
Exemplo n.º 7
0
static PyObject *
range_iter(PyObject *seq)
{
    rangeobject *r = (rangeobject *)seq;
    longrangeiterobject *it;
    PyObject *tmp, *len;
    long lstart, lstop, lstep;

    assert(PyRange_Check(seq));

    /* If all three fields convert to long, use the int version */
    lstart = PyLong_AsLong(r->start);
    if (lstart != -1 || !PyErr_Occurred()) {
	lstop = PyLong_AsLong(r->stop);
	if (lstop != -1 || !PyErr_Occurred()) {
	    lstep = PyLong_AsLong(r->step);
	    if (lstep != -1 || !PyErr_Occurred())
		return int_range_iter(lstart, lstop, lstep);
	}
    }
    /* Some conversion failed, so there is an error set. Clear it,
       and try again with a long range. */
    PyErr_Clear();

    it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
    if (it == NULL)
        return NULL;

    /* Do all initialization here, so we can DECREF on failure. */
    it->start = r->start;
    it->step = r->step;
    Py_INCREF(it->start);
    Py_INCREF(it->step);

    it->len = it->index = NULL;

    /* Calculate length: (r->stop - r->start) / r->step */
    tmp = PyNumber_Subtract(r->stop, r->start);
    if (!tmp)
        goto create_failure;
    len = PyNumber_FloorDivide(tmp, r->step);
    Py_DECREF(tmp);
    if (!len)
        goto create_failure;
    it->len = len;
    it->index = PyLong_FromLong(0);
    if (!it->index)
        goto create_failure;

    return (PyObject *)it;

create_failure:
    Py_DECREF(it);
    return NULL;
}
Exemplo n.º 8
0
static PyObject *
range_index(rangeobject *r, PyObject *ob)
{
    PyObject *idx, *tmp;
    int contains;
    PyObject *format_tuple, *err_string;
    static PyObject *err_format = NULL;

    if (!PyLong_CheckExact(ob) && !PyBool_Check(ob)) {
        Py_ssize_t index;
        index = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_INDEX);
        if (index == -1)
            return NULL;
        return PyLong_FromSsize_t(index);
    }

    contains = range_contains_long(r, ob);
    if (contains == -1)
        return NULL;

    if (!contains)
        goto value_error;

    tmp = PyNumber_Subtract(ob, r->start);
    if (tmp == NULL)
        return NULL;

    /* idx = (ob - r.start) // r.step */
    idx = PyNumber_FloorDivide(tmp, r->step);
    Py_DECREF(tmp);
    return idx;

value_error:

    /* object is not in the range */
    if (err_format == NULL) {
        err_format = PyUnicode_FromString("%r is not in range");
        if (err_format == NULL)
            return NULL;
    }
    format_tuple = PyTuple_Pack(1, ob);
    if (format_tuple == NULL)
        return NULL;
    err_string = PyUnicode_Format(err_format, format_tuple);
    Py_DECREF(format_tuple);
    if (err_string == NULL)
        return NULL;
    PyErr_SetObject(PyExc_ValueError, err_string);
    Py_DECREF(err_string);
    return NULL;
}
Exemplo n.º 9
0
/* Assumes (PyLong_CheckExact(ob) || PyBool_Check(ob)) */
static int
range_contains_long(rangeobject *r, PyObject *ob)
{
    int cmp1, cmp2, cmp3;
    PyObject *tmp1 = NULL;
    PyObject *tmp2 = NULL;
    PyObject *zero = NULL;
    int result = -1;

    zero = PyLong_FromLong(0);
    if (zero == NULL) /* MemoryError in int(0) */
        goto end;

    /* Check if the value can possibly be in the range. */

    cmp1 = PyObject_RichCompareBool(r->step, zero, Py_GT);
    if (cmp1 == -1)
        goto end;
    if (cmp1 == 1) { /* positive steps: start <= ob < stop */
        cmp2 = PyObject_RichCompareBool(r->start, ob, Py_LE);
        cmp3 = PyObject_RichCompareBool(ob, r->stop, Py_LT);
    }
    else { /* negative steps: stop < ob <= start */
        cmp2 = PyObject_RichCompareBool(ob, r->start, Py_LE);
        cmp3 = PyObject_RichCompareBool(r->stop, ob, Py_LT);
    }

    if (cmp2 == -1 || cmp3 == -1) /* TypeError */
        goto end;
    if (cmp2 == 0 || cmp3 == 0) { /* ob outside of range */
        result = 0;
        goto end;
    }

    /* Check that the stride does not invalidate ob's membership. */
    tmp1 = PyNumber_Subtract(ob, r->start);
    if (tmp1 == NULL)
        goto end;
    tmp2 = PyNumber_Remainder(tmp1, r->step);
    if (tmp2 == NULL)
        goto end;
    /* result = (int(ob) - start % step) == 0 */
    result = PyObject_RichCompareBool(tmp2, zero, Py_EQ);
  end:
    Py_XDECREF(tmp1);
    Py_XDECREF(tmp2);
    Py_XDECREF(zero);
    return result;
}
Exemplo n.º 10
0
/// takes a Python list of spheres with position and velocity vectors and returns the Lennard-Jones potential of the system
static PyObject *
total_pe(PyObject *self, PyObject *args)
{
	/*
	def total_pe():
		pe = 0.0
		for m1 in range(len(molecules)):
			mol1 = molecules[m1]
			for m2 in range(m1 + 1, len(molecules)):
				mol2 = molecules[m2]
				r12 = abs(mol1.pos - mol2.pos)
				assert abs(mol2.pos - mol1.pos) == r12
				pe += r12 ** -6 * (r12 ** -6 - 2)
		return pe 
	*/
	double pe = 0.0;
	PyObject *mol_list;
	if (!PyArg_ParseTuple(args, "O", &mol_list)) return NULL;
	if (!PyList_Check(mol_list)) return NULL;
	long size = PyList_Size(mol_list);
	for (long m1 = 0; m1 < size; m1++) {
		PyObject *mol1, *mol1pos;
		if (!(mol1 = PyList_GetItem(mol_list, m1))) return NULL;
		if (!(mol1pos = PyObject_GetAttrString(mol1, "pos"))) return NULL;
		for (long m2 = m1 + 1; m2 < size; m2++) {
			PyObject *mol2, *mol2pos, *r12vec, *pysep;
			if (!(mol2 = PyList_GetItem(mol_list, m2))) return NULL;
			if (!(mol2pos = PyObject_GetAttrString(mol2, "pos"))) return NULL;
			if (!(r12vec = PyNumber_Subtract(mol1pos, mol2pos))) return NULL;
			if (!(pysep = PyNumber_Absolute(r12vec))) return NULL;
			double r12 = PyFloat_AsDouble(pysep);
			pe += pow(r12, -6) * (pow(r12, -6) - 2);
			Py_DECREF(mol2pos);
			Py_DECREF(r12vec);
			Py_DECREF(pysep);
		}
		Py_DECREF(mol1pos);
	}	
	return Py_BuildValue("d", pe);
}
Exemplo n.º 11
0
/*NUMPY_API
 * Ptp
 */
NPY_NO_EXPORT PyObject *
PyArray_Ptp(PyArrayObject *ap, int axis, PyArrayObject *out)
{
    PyArrayObject *arr;
    PyObject *ret;
    PyObject *obj1 = NULL, *obj2 = NULL;

    arr=(PyArrayObject *)PyArray_CheckAxis(ap, &axis, 0);
    if (arr == NULL) {
        return NULL;
    }
    obj1 = PyArray_Max(arr, axis, out);
    if (obj1 == NULL) {
        goto fail;
    }
    obj2 = PyArray_Min(arr, axis, NULL);
    if (obj2 == NULL) {
        goto fail;
    }
    Py_DECREF(arr);
    if (out) {
        ret = PyObject_CallFunction(n_ops.subtract, "OOO", out, obj2, out);
    }
    else {
        ret = PyNumber_Subtract(obj1, obj2);
    }
    Py_DECREF(obj1);
    Py_DECREF(obj2);
    return ret;

 fail:
    Py_XDECREF(arr);
    Py_XDECREF(obj1);
    Py_XDECREF(obj2);
    return NULL;
}
Exemplo n.º 12
0
  /*else*/ {
    __pyx_2 = PyObject_GetAttr(__pyx_v_self, __pyx_n___purgeCache); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; goto __pyx_L1;}
    __pyx_3 = PyObject_CallObject(__pyx_2, 0); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; goto __pyx_L1;}
    Py_DECREF(__pyx_2); __pyx_2 = 0;
    Py_DECREF(__pyx_3); __pyx_3 = 0;
  }
  __pyx_L2:;

  __pyx_r = 0;
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_2);
  Py_XDECREF(__pyx_3);
  __Pyx_AddTraceback("Stemmer.Stemmer.maxCacheSize.__set__");
  __pyx_r = -1;
  __pyx_L0:;
  Py_DECREF(__pyx_v_self);
  return __pyx_r;
}

static PyObject *__pyx_f_7Stemmer_7Stemmer_12maxCacheSize___get__(PyObject *__pyx_v_self); /*proto*/
static PyObject *__pyx_f_7Stemmer_7Stemmer_12maxCacheSize___get__(PyObject *__pyx_v_self) {
  PyObject *__pyx_r;
  PyObject *__pyx_1 = 0;
  Py_INCREF(__pyx_v_self);
  __pyx_1 = PyInt_FromLong(((struct __pyx_obj_7Stemmer_Stemmer *)__pyx_v_self)->max_cache_size); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 146; goto __pyx_L1;}
  __pyx_r = __pyx_1;
  __pyx_1 = 0;
  goto __pyx_L0;

  __pyx_r = Py_None; Py_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_1);
  __Pyx_AddTraceback("Stemmer.Stemmer.maxCacheSize.__get__");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_self);
  return __pyx_r;
}

static PyObject *__pyx_f_7Stemmer_7Stemmer___purgeCache(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/
static PyObject *__pyx_f_7Stemmer_7Stemmer___purgeCache(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyObject *__pyx_v_newcache;
  PyObject *__pyx_v_mincounter;
  PyObject *__pyx_v_word;
  PyObject *__pyx_v_cacheditem;
  PyObject *__pyx_r;
  Py_ssize_t __pyx_1;
  int __pyx_2;
  PyObject *__pyx_3 = 0;
  PyObject *__pyx_4 = 0;
  PyObject *__pyx_5 = 0;
  static char *__pyx_argnames[] = {0};
  if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "", __pyx_argnames)) return 0;
  Py_INCREF(__pyx_v_self);
  __pyx_v_newcache = Py_None; Py_INCREF(Py_None);
  __pyx_v_mincounter = Py_None; Py_INCREF(Py_None);
  __pyx_v_word = Py_None; Py_INCREF(Py_None);
  __pyx_v_cacheditem = Py_None; Py_INCREF(Py_None);

  /* "/home/richard/private/Working/snowball/pystemmer/src/Stemmer.pyx":149 */
  __pyx_1 = PyObject_Length(((struct __pyx_obj_7Stemmer_Stemmer *)__pyx_v_self)->cache); if (__pyx_1 == -1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 149; goto __pyx_L1;}
  __pyx_2 = (__pyx_1 < ((struct __pyx_obj_7Stemmer_Stemmer *)__pyx_v_self)->max_cache_size);
  if (__pyx_2) {
    __pyx_r = Py_None; Py_INCREF(Py_None);
    goto __pyx_L0;
    goto __pyx_L2;
  }
  __pyx_L2:;

  /* "/home/richard/private/Working/snowball/pystemmer/src/Stemmer.pyx":151 */
  __pyx_3 = PyDict_New(); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; goto __pyx_L1;}
  Py_DECREF(__pyx_v_newcache);
  __pyx_v_newcache = __pyx_3;
  __pyx_3 = 0;

  /* "/home/richard/private/Working/snowball/pystemmer/src/Stemmer.pyx":152 */
  __pyx_3 = PyInt_FromLong(((((struct __pyx_obj_7Stemmer_Stemmer *)__pyx_v_self)->max_cache_size * 8) / 10)); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; goto __pyx_L1;}
  __pyx_4 = PyTuple_New(1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; goto __pyx_L1;}
  PyTuple_SET_ITEM(__pyx_4, 0, __pyx_3);
  __pyx_3 = 0;
  __pyx_3 = PyObject_CallObject(((PyObject *)(&PyInt_Type)), __pyx_4); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; goto __pyx_L1;}
  Py_DECREF(__pyx_4); __pyx_4 = 0;
  __pyx_4 = PyNumber_Subtract(((struct __pyx_obj_7Stemmer_Stemmer *)__pyx_v_self)->counter, __pyx_3); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 152; goto __pyx_L1;}
  Py_DECREF(__pyx_3); __pyx_3 = 0;
  Py_DECREF(__pyx_v_mincounter);
  __pyx_v_mincounter = __pyx_4;
  __pyx_4 = 0;

  /* "/home/richard/private/Working/snowball/pystemmer/src/Stemmer.pyx":153 */
  __pyx_3 = PyObject_GetAttr(((struct __pyx_obj_7Stemmer_Stemmer *)__pyx_v_self)->cache, __pyx_n_iteritems); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; goto __pyx_L1;}
  __pyx_4 = PyObject_CallObject(__pyx_3, 0); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; goto __pyx_L1;}
  Py_DECREF(__pyx_3); __pyx_3 = 0;
  __pyx_3 = PyObject_GetIter(__pyx_4); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; goto __pyx_L1;}
  Py_DECREF(__pyx_4); __pyx_4 = 0;
  for (;;) {
    __pyx_4 = PyIter_Next(__pyx_3);
    if (!__pyx_4) {
      if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; goto __pyx_L1;}
      break;
    }
    __pyx_5 = PyObject_GetIter(__pyx_4); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; goto __pyx_L1;}
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    __pyx_4 = __Pyx_UnpackItem(__pyx_5); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; goto __pyx_L1;}
    Py_DECREF(__pyx_v_word);
    __pyx_v_word = __pyx_4;
    __pyx_4 = 0;
    __pyx_4 = __Pyx_UnpackItem(__pyx_5); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; goto __pyx_L1;}
    Py_DECREF(__pyx_v_cacheditem);
    __pyx_v_cacheditem = __pyx_4;
    __pyx_4 = 0;
    if (__Pyx_EndUnpack(__pyx_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 153; goto __pyx_L1;}
    Py_DECREF(__pyx_5); __pyx_5 = 0;
    __pyx_4 = __Pyx_GetItemInt(__pyx_v_cacheditem, 1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 154; goto __pyx_L1;}
    if (PyObject_Cmp(__pyx_4, __pyx_v_mincounter, &__pyx_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 154; goto __pyx_L1;}
    __pyx_2 = __pyx_2 > 0;
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    if (__pyx_2) {
      if (PyObject_SetItem(__pyx_v_newcache, __pyx_v_word, __pyx_v_cacheditem) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 155; goto __pyx_L1;}
      goto __pyx_L5;
    }
    __pyx_L5:;
  }
  Py_DECREF(__pyx_3); __pyx_3 = 0;

  /* "/home/richard/private/Working/snowball/pystemmer/src/Stemmer.pyx":156 */
  Py_INCREF(__pyx_v_newcache);
  Py_DECREF(((struct __pyx_obj_7Stemmer_Stemmer *)__pyx_v_self)->cache);
  ((struct __pyx_obj_7Stemmer_Stemmer *)__pyx_v_self)->cache = __pyx_v_newcache;

  __pyx_r = Py_None; Py_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_3);
  Py_XDECREF(__pyx_4);
  Py_XDECREF(__pyx_5);
  __Pyx_AddTraceback("Stemmer.Stemmer.__purgeCache");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_newcache);
  Py_DECREF(__pyx_v_mincounter);
  Py_DECREF(__pyx_v_word);
  Py_DECREF(__pyx_v_cacheditem);
  Py_DECREF(__pyx_v_self);
  return __pyx_r;
}
Exemplo n.º 13
0
/* Return number of items in range (lo, hi, step) as a PyLong object,
 * when arguments are PyLong objects.  Arguments MUST return 1 with
 * PyLong_Check().  Return NULL when there is an error.
 */
static PyObject*
compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
{
    /* -------------------------------------------------------------
    Algorithm is equal to that of get_len_of_range(), but it operates
    on PyObjects (which are assumed to be PyLong objects).
    ---------------------------------------------------------------*/
    int cmp_result;
    PyObject *lo, *hi;
    PyObject *diff = NULL;
    PyObject *one = NULL;
    PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
                /* holds sub-expression evaluations */

    PyObject *zero = PyLong_FromLong(0);
    if (zero == NULL)
        return NULL;
    cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
    Py_DECREF(zero);
    if (cmp_result == -1)
        return NULL;

    if (cmp_result == 1) {
        lo = start;
        hi = stop;
        Py_INCREF(step);
    } else {
        lo = stop;
        hi = start;
        step = PyNumber_Negative(step);
        if (!step)
            return NULL;
    }

    /* if (lo >= hi), return length of 0. */
    cmp_result = PyObject_RichCompareBool(lo, hi, Py_GE);
    if (cmp_result != 0) {
        Py_XDECREF(step);
        if (cmp_result < 0)
            return NULL;
        return PyLong_FromLong(0);
    }

    if ((one = PyLong_FromLong(1L)) == NULL)
        goto Fail;

    if ((tmp1 = PyNumber_Subtract(hi, lo)) == NULL)
        goto Fail;

    if ((diff = PyNumber_Subtract(tmp1, one)) == NULL)
        goto Fail;

    if ((tmp2 = PyNumber_FloorDivide(diff, step)) == NULL)
        goto Fail;

    if ((result = PyNumber_Add(tmp2, one)) == NULL)
        goto Fail;

    Py_DECREF(tmp2);
    Py_DECREF(diff);
    Py_DECREF(step);
    Py_DECREF(tmp1);
    Py_DECREF(one);
    return result;

  Fail:
    Py_XDECREF(tmp2);
    Py_XDECREF(diff);
    Py_XDECREF(step);
    Py_XDECREF(tmp1);
    Py_XDECREF(one);
    return NULL;
}
Exemplo n.º 14
0
static PyObject *
range_reverse(PyObject *seq)
{
    rangeobject *range = (rangeobject*) seq;
    longrangeiterobject *it;
    PyObject *one, *sum, *diff, *product;
    long lstart, lstop, lstep, new_start, new_stop;
    unsigned long ulen;

    assert(PyRange_Check(seq));

    /* reversed(range(start, stop, step)) can be expressed as
       range(start+(n-1)*step, start-step, -step), where n is the number of
       integers in the range.

       If each of start, stop, step, -step, start-step, and the length
       of the iterator is representable as a C long, use the int
       version.  This excludes some cases where the reversed range is
       representable as a range_iterator, but it's good enough for
       common cases and it makes the checks simple. */

    lstart = PyLong_AsLong(range->start);
    if (lstart == -1 && PyErr_Occurred()) {
        PyErr_Clear();
        goto long_range;
    }
    lstop = PyLong_AsLong(range->stop);
    if (lstop == -1 && PyErr_Occurred()) {
        PyErr_Clear();
        goto long_range;
    }
    lstep = PyLong_AsLong(range->step);
    if (lstep == -1 && PyErr_Occurred()) {
        PyErr_Clear();
        goto long_range;
    }
    /* check for possible overflow of -lstep */
    if (lstep == LONG_MIN)
        goto long_range;

    /* check for overflow of lstart - lstep:

       for lstep > 0, need only check whether lstart - lstep < LONG_MIN.
       for lstep < 0, need only check whether lstart - lstep > LONG_MAX

       Rearrange these inequalities as:

           lstart - LONG_MIN < lstep  (lstep > 0)
           LONG_MAX - lstart < -lstep  (lstep < 0)

       and compute both sides as unsigned longs, to avoid the
       possibility of undefined behaviour due to signed overflow. */

    if (lstep > 0) {
         if ((unsigned long)lstart - LONG_MIN < (unsigned long)lstep)
            goto long_range;
    }
    else {
        if (LONG_MAX - (unsigned long)lstart < 0UL - lstep)
            goto long_range;
    }

    ulen = get_len_of_range(lstart, lstop, lstep);
    if (ulen > (unsigned long)LONG_MAX)
        goto long_range;

    new_stop = lstart - lstep;
    new_start = (long)(new_stop + ulen * lstep);
    return fast_range_iter(new_start, new_stop, -lstep);

long_range:
    it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
    if (it == NULL)
        return NULL;

    /* start + (len - 1) * step */
    it->len = range->length;
    Py_INCREF(it->len);

    one = PyLong_FromLong(1);
    if (!one)
        goto create_failure;

    diff = PyNumber_Subtract(it->len, one);
    Py_DECREF(one);
    if (!diff)
        goto create_failure;

    product = PyNumber_Multiply(diff, range->step);
    Py_DECREF(diff);
    if (!product)
        goto create_failure;

    sum = PyNumber_Add(range->start, product);
    Py_DECREF(product);
    it->start = sum;
    if (!it->start)
        goto create_failure;

    it->step = PyNumber_Negative(range->step);
    if (!it->step)
        goto create_failure;

    it->index = PyLong_FromLong(0);
    if (!it->index)
        goto create_failure;

    return (PyObject *)it;

create_failure:
    Py_DECREF(it);
    return NULL;
}
Exemplo n.º 15
0
static PyObject *__pyx_f_11closestpair_closest_pair(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {
  PyListObject *__pyx_v_points = 0;
  float __pyx_v_min_d;
  PyObject *__pyx_v_min_p1;
  PyObject *__pyx_v_min_p2;
  PyObject *__pyx_v_i;
  PyObject *__pyx_v_point;
  PyObject *__pyx_v_point2;
  PyObject *__pyx_v_d;
  PyObject *__pyx_v_split;
  PyObject *__pyx_v_d1;
  PyObject *__pyx_v_p11;
  PyObject *__pyx_v_p12;
  PyObject *__pyx_v_d2;
  PyObject *__pyx_v_p21;
  PyObject *__pyx_v_p22;
  PyObject *__pyx_v_points_in_strip;
  PyObject *__pyx_v_split_at;
  PyObject *__pyx_v_max_i;
  PyObject *__pyx_v_sd;
  PyObject *__pyx_r;
  Py_ssize_t __pyx_1;
  int __pyx_2;
  PyObject *__pyx_3 = 0;
  PyObject *__pyx_4 = 0;
  PyObject *__pyx_5 = 0;
  PyObject *__pyx_6 = 0;
  PyObject *__pyx_7 = 0;
  PyObject *__pyx_8 = 0;
  PyObject *__pyx_9 = 0;
  PyObject *__pyx_10 = 0;
  float __pyx_11;
  int __pyx_12;
  Py_ssize_t __pyx_13;
  static char *__pyx_argnames[] = {"points",0};
  if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "O", __pyx_argnames, &__pyx_v_points)) return 0;
  Py_INCREF(__pyx_v_points);
  __pyx_v_min_p1 = Py_None; Py_INCREF(Py_None);
  __pyx_v_min_p2 = Py_None; Py_INCREF(Py_None);
  __pyx_v_i = Py_None; Py_INCREF(Py_None);
  __pyx_v_point = Py_None; Py_INCREF(Py_None);
  __pyx_v_point2 = Py_None; Py_INCREF(Py_None);
  __pyx_v_d = Py_None; Py_INCREF(Py_None);
  __pyx_v_split = Py_None; Py_INCREF(Py_None);
  __pyx_v_d1 = Py_None; Py_INCREF(Py_None);
  __pyx_v_p11 = Py_None; Py_INCREF(Py_None);
  __pyx_v_p12 = Py_None; Py_INCREF(Py_None);
  __pyx_v_d2 = Py_None; Py_INCREF(Py_None);
  __pyx_v_p21 = Py_None; Py_INCREF(Py_None);
  __pyx_v_p22 = Py_None; Py_INCREF(Py_None);
  __pyx_v_points_in_strip = Py_None; Py_INCREF(Py_None);
  __pyx_v_split_at = Py_None; Py_INCREF(Py_None);
  __pyx_v_max_i = Py_None; Py_INCREF(Py_None);
  __pyx_v_sd = Py_None; Py_INCREF(Py_None);
  if (!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_points), (&PyList_Type), 1, "points")) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; goto __pyx_L1;}

  /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":15 */
  __pyx_1 = PyObject_Length(((PyObject *)__pyx_v_points)); if (__pyx_1 == -1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; goto __pyx_L1;}
  __pyx_2 = (__pyx_1 < 2);
  if (__pyx_2) {
    __pyx_3 = PyTuple_New(1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;}
    Py_INCREF(__pyx_k1p);
    PyTuple_SET_ITEM(__pyx_3, 0, __pyx_k1p);
    __pyx_4 = PyObject_CallObject(PyExc_ValueError, __pyx_3); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    __Pyx_Raise(__pyx_4, 0, 0);
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;}
    goto __pyx_L2;
  }
  __pyx_1 = PyObject_Length(((PyObject *)__pyx_v_points)); if (__pyx_1 == -1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; goto __pyx_L1;}
  __pyx_2 = 2 <= __pyx_1;
  if (__pyx_2) {
    __pyx_2 = __pyx_1 <= 6;
  }
  if (__pyx_2) {

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":18 */
    __pyx_v_min_d = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":19 */
    Py_INCREF(Py_None);
    Py_DECREF(__pyx_v_min_p1);
    __pyx_v_min_p1 = Py_None;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":20 */
    Py_INCREF(Py_None);
    Py_DECREF(__pyx_v_min_p2);
    __pyx_v_min_p2 = Py_None;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":21 */
    __pyx_3 = PyTuple_New(1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; goto __pyx_L1;}
    Py_INCREF(((PyObject *)__pyx_v_points));
    PyTuple_SET_ITEM(__pyx_3, 0, ((PyObject *)__pyx_v_points));
    __pyx_4 = PyObject_CallObject(((PyObject *)(&PyEnum_Type)), __pyx_3); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    __pyx_3 = PyObject_GetIter(__pyx_4); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; goto __pyx_L1;}
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    for (;;) {
      __pyx_4 = PyIter_Next(__pyx_3);
      if (!__pyx_4) {
        if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; goto __pyx_L1;}
        break;
      }
      __pyx_5 = PyObject_GetIter(__pyx_4); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; goto __pyx_L1;}
      Py_DECREF(__pyx_4); __pyx_4 = 0;
      __pyx_4 = __Pyx_UnpackItem(__pyx_5); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; goto __pyx_L1;}
      Py_DECREF(__pyx_v_i);
      __pyx_v_i = __pyx_4;
      __pyx_4 = 0;
      __pyx_4 = __Pyx_UnpackItem(__pyx_5); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; goto __pyx_L1;}
      Py_DECREF(__pyx_v_point);
      __pyx_v_point = __pyx_4;
      __pyx_4 = 0;
      if (__Pyx_EndUnpack(__pyx_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; goto __pyx_L1;}
      Py_DECREF(__pyx_5); __pyx_5 = 0;
      __pyx_4 = PyInt_FromLong(1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; goto __pyx_L1;}
      __pyx_5 = PyNumber_Add(__pyx_v_i, __pyx_4); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; goto __pyx_L1;}
      Py_DECREF(__pyx_4); __pyx_4 = 0;
      __pyx_1 = PyInt_AsSsize_t(__pyx_5); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; goto __pyx_L1;}
      Py_DECREF(__pyx_5); __pyx_5 = 0;
      __pyx_4 = PySequence_GetSlice(((PyObject *)__pyx_v_points), __pyx_1, PY_SSIZE_T_MAX); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; goto __pyx_L1;}
      __pyx_5 = PyObject_GetIter(__pyx_4); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; goto __pyx_L1;}
      Py_DECREF(__pyx_4); __pyx_4 = 0;
      for (;;) {
        __pyx_4 = PyIter_Next(__pyx_5);
        if (!__pyx_4) {
          if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; goto __pyx_L1;}
          break;
        }
        Py_DECREF(__pyx_v_point2);
        __pyx_v_point2 = __pyx_4;
        __pyx_4 = 0;

        /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":23 */
        __pyx_4 = __Pyx_GetName(__pyx_m, __pyx_n_distance); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 23; goto __pyx_L1;}
        __pyx_6 = PySequence_GetItem(__pyx_v_point, 0); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 23; goto __pyx_L1;}
        __pyx_7 = PySequence_GetItem(__pyx_v_point, 1); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 23; goto __pyx_L1;}
        __pyx_8 = PySequence_GetItem(__pyx_v_point2, 0); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 23; goto __pyx_L1;}
        __pyx_9 = PySequence_GetItem(__pyx_v_point2, 1); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 23; goto __pyx_L1;}
        __pyx_10 = PyTuple_New(4); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 23; goto __pyx_L1;}
        PyTuple_SET_ITEM(__pyx_10, 0, __pyx_6);
        PyTuple_SET_ITEM(__pyx_10, 1, __pyx_7);
        PyTuple_SET_ITEM(__pyx_10, 2, __pyx_8);
        PyTuple_SET_ITEM(__pyx_10, 3, __pyx_9);
        __pyx_6 = 0;
        __pyx_7 = 0;
        __pyx_8 = 0;
        __pyx_9 = 0;
        __pyx_6 = PyObject_CallObject(__pyx_4, __pyx_10); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 23; goto __pyx_L1;}
        Py_DECREF(__pyx_4); __pyx_4 = 0;
        Py_DECREF(__pyx_10); __pyx_10 = 0;
        Py_DECREF(__pyx_v_d);
        __pyx_v_d = __pyx_6;
        __pyx_6 = 0;

        /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":24 */
        __pyx_2 = (__pyx_v_min_d == 0);
        if (!__pyx_2) {
          __pyx_7 = PyFloat_FromDouble(__pyx_v_min_d); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; goto __pyx_L1;}
          if (PyObject_Cmp(__pyx_7, __pyx_v_d, &__pyx_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; goto __pyx_L1;}
          __pyx_2 = __pyx_2 > 0;
          Py_DECREF(__pyx_7); __pyx_7 = 0;
        }
        if (__pyx_2) {

          /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":25 */
          __pyx_11 = PyFloat_AsDouble(__pyx_v_d); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; goto __pyx_L1;}
          __pyx_v_min_d = __pyx_11;

          /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":26 */
          Py_INCREF(__pyx_v_point);
          Py_DECREF(__pyx_v_min_p1);
          __pyx_v_min_p1 = __pyx_v_point;

          /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":27 */
          Py_INCREF(__pyx_v_point2);
          Py_DECREF(__pyx_v_min_p2);
          __pyx_v_min_p2 = __pyx_v_point2;
          goto __pyx_L7;
        }
        __pyx_L7:;
      }
      Py_DECREF(__pyx_5); __pyx_5 = 0;
    }
    Py_DECREF(__pyx_3); __pyx_3 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":28 */
    __pyx_8 = PyFloat_FromDouble(__pyx_v_min_d); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; goto __pyx_L1;}
    __pyx_9 = PyTuple_New(3); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_9, 0, __pyx_8);
    Py_INCREF(__pyx_v_min_p1);
    PyTuple_SET_ITEM(__pyx_9, 1, __pyx_v_min_p1);
    Py_INCREF(__pyx_v_min_p2);
    PyTuple_SET_ITEM(__pyx_9, 2, __pyx_v_min_p2);
    __pyx_8 = 0;
    __pyx_r = __pyx_9;
    __pyx_9 = 0;
    goto __pyx_L0;
    goto __pyx_L2;
  }
  /*else*/ {

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":30 */
    __pyx_12 = PyList_Sort(((PyObject *)__pyx_v_points)); if (__pyx_12 == -1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; goto __pyx_L1;}

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":32 */
    __pyx_1 = PyObject_Length(((PyObject *)__pyx_v_points)); if (__pyx_1 == -1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;}
    __pyx_4 = PyInt_FromSsize_t((__pyx_1 / 2)); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;}
    __pyx_10 = PyTuple_New(1); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_10, 0, __pyx_4);
    __pyx_4 = 0;
    __pyx_6 = PyObject_CallObject(((PyObject *)(&PyInt_Type)), __pyx_10); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;}
    Py_DECREF(__pyx_10); __pyx_10 = 0;
    Py_DECREF(__pyx_v_split);
    __pyx_v_split = __pyx_6;
    __pyx_6 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":33 */
    __pyx_7 = __Pyx_GetName(__pyx_m, __pyx_n_closest_pair); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    __pyx_1 = PyInt_AsSsize_t(__pyx_v_split); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    __pyx_5 = PySequence_GetSlice(((PyObject *)__pyx_v_points), 0, __pyx_1); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    __pyx_3 = PyTuple_New(1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_3, 0, __pyx_5);
    __pyx_5 = 0;
    __pyx_8 = PyObject_CallObject(__pyx_7, __pyx_3); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    Py_DECREF(__pyx_7); __pyx_7 = 0;
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    __pyx_9 = PyObject_GetIter(__pyx_8); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    Py_DECREF(__pyx_8); __pyx_8 = 0;
    __pyx_4 = __Pyx_UnpackItem(__pyx_9); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    Py_DECREF(__pyx_v_d1);
    __pyx_v_d1 = __pyx_4;
    __pyx_4 = 0;
    __pyx_10 = __Pyx_UnpackItem(__pyx_9); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    Py_DECREF(__pyx_v_p11);
    __pyx_v_p11 = __pyx_10;
    __pyx_10 = 0;
    __pyx_6 = __Pyx_UnpackItem(__pyx_9); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    Py_DECREF(__pyx_v_p12);
    __pyx_v_p12 = __pyx_6;
    __pyx_6 = 0;
    if (__Pyx_EndUnpack(__pyx_9) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; goto __pyx_L1;}
    Py_DECREF(__pyx_9); __pyx_9 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":34 */
    __pyx_5 = __Pyx_GetName(__pyx_m, __pyx_n_closest_pair); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    __pyx_1 = PyInt_AsSsize_t(__pyx_v_split); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    __pyx_7 = PySequence_GetSlice(((PyObject *)__pyx_v_points), __pyx_1, PY_SSIZE_T_MAX); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    __pyx_3 = PyTuple_New(1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    PyTuple_SET_ITEM(__pyx_3, 0, __pyx_7);
    __pyx_7 = 0;
    __pyx_8 = PyObject_CallObject(__pyx_5, __pyx_3); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    Py_DECREF(__pyx_5); __pyx_5 = 0;
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    __pyx_4 = PyObject_GetIter(__pyx_8); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    Py_DECREF(__pyx_8); __pyx_8 = 0;
    __pyx_10 = __Pyx_UnpackItem(__pyx_4); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    Py_DECREF(__pyx_v_d2);
    __pyx_v_d2 = __pyx_10;
    __pyx_10 = 0;
    __pyx_6 = __Pyx_UnpackItem(__pyx_4); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    Py_DECREF(__pyx_v_p21);
    __pyx_v_p21 = __pyx_6;
    __pyx_6 = 0;
    __pyx_9 = __Pyx_UnpackItem(__pyx_4); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    Py_DECREF(__pyx_v_p22);
    __pyx_v_p22 = __pyx_9;
    __pyx_9 = 0;
    if (__Pyx_EndUnpack(__pyx_4) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;}
    Py_DECREF(__pyx_4); __pyx_4 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":35 */
    __pyx_7 = __Pyx_GetName(__pyx_b, __pyx_n_min); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 35; goto __pyx_L1;}
    __pyx_5 = PyTuple_New(2); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 35; goto __pyx_L1;}
    Py_INCREF(__pyx_v_d1);
    PyTuple_SET_ITEM(__pyx_5, 0, __pyx_v_d1);
    Py_INCREF(__pyx_v_d2);
    PyTuple_SET_ITEM(__pyx_5, 1, __pyx_v_d2);
    __pyx_3 = PyObject_CallObject(__pyx_7, __pyx_5); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 35; goto __pyx_L1;}
    Py_DECREF(__pyx_7); __pyx_7 = 0;
    Py_DECREF(__pyx_5); __pyx_5 = 0;
    Py_DECREF(__pyx_v_d);
    __pyx_v_d = __pyx_3;
    __pyx_3 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":37 */
    __pyx_8 = PyList_New(0); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 37; goto __pyx_L1;}
    Py_DECREF(__pyx_v_points_in_strip);
    __pyx_v_points_in_strip = __pyx_8;
    __pyx_8 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":38 */
    __pyx_10 = PyInt_FromLong(1); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    __pyx_6 = PyNumber_Subtract(__pyx_v_split, __pyx_10); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    Py_DECREF(__pyx_10); __pyx_10 = 0;
    __pyx_9 = PyObject_GetItem(((PyObject *)__pyx_v_points), __pyx_6); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    Py_DECREF(__pyx_6); __pyx_6 = 0;
    __pyx_4 = PySequence_GetItem(__pyx_9, 0); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    Py_DECREF(__pyx_9); __pyx_9 = 0;
    __pyx_7 = PyObject_GetItem(((PyObject *)__pyx_v_points), __pyx_v_split); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    __pyx_5 = PySequence_GetItem(__pyx_7, 0); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    Py_DECREF(__pyx_7); __pyx_7 = 0;
    __pyx_3 = PyNumber_Add(__pyx_4, __pyx_5); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    Py_DECREF(__pyx_5); __pyx_5 = 0;
    __pyx_8 = PyFloat_FromDouble(2.0); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    __pyx_10 = PyNumber_Divide(__pyx_3, __pyx_8); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; goto __pyx_L1;}
    Py_DECREF(__pyx_3); __pyx_3 = 0;
    Py_DECREF(__pyx_8); __pyx_8 = 0;
    Py_DECREF(__pyx_v_split_at);
    __pyx_v_split_at = __pyx_10;
    __pyx_10 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":39 */
    __pyx_6 = PyObject_GetIter(((PyObject *)__pyx_v_points)); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 39; goto __pyx_L1;}
    for (;;) {
      __pyx_9 = PyIter_Next(__pyx_6);
      if (!__pyx_9) {
        if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 39; goto __pyx_L1;}
        break;
      }
      Py_DECREF(__pyx_v_point);
      __pyx_v_point = __pyx_9;
      __pyx_9 = 0;

      /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":40 */
      __pyx_7 = PySequence_GetItem(__pyx_v_point, 0); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; goto __pyx_L1;}
      __pyx_4 = PyNumber_Subtract(__pyx_v_split_at, __pyx_v_d); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; goto __pyx_L1;}
      if (PyObject_Cmp(__pyx_7, __pyx_4, &__pyx_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; goto __pyx_L1;}
      __pyx_2 = __pyx_2 < 0;
      Py_DECREF(__pyx_7); __pyx_7 = 0;
      Py_DECREF(__pyx_4); __pyx_4 = 0;
      if (__pyx_2) {
        goto __pyx_L8;
        goto __pyx_L10;
      }
      __pyx_5 = PySequence_GetItem(__pyx_v_point, 0); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; goto __pyx_L1;}
      __pyx_3 = PyNumber_Add(__pyx_v_split_at, __pyx_v_d); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; goto __pyx_L1;}
      if (PyObject_Cmp(__pyx_5, __pyx_3, &__pyx_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; goto __pyx_L1;}
      __pyx_2 = __pyx_2 > 0;
      Py_DECREF(__pyx_5); __pyx_5 = 0;
      Py_DECREF(__pyx_3); __pyx_3 = 0;
      if (__pyx_2) {
        goto __pyx_L9;
        goto __pyx_L10;
      }
      __pyx_L10:;

      /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":44 */
      __pyx_8 = PyObject_GetAttr(__pyx_v_points_in_strip, __pyx_n_append); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
      __pyx_10 = PySequence_GetItem(__pyx_v_point, 1); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
      __pyx_9 = PySequence_GetItem(__pyx_v_point, 0); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
      __pyx_7 = PySequence_GetItem(__pyx_v_point, 2); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
      __pyx_4 = PyTuple_New(3); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
      PyTuple_SET_ITEM(__pyx_4, 0, __pyx_10);
      PyTuple_SET_ITEM(__pyx_4, 1, __pyx_9);
      PyTuple_SET_ITEM(__pyx_4, 2, __pyx_7);
      __pyx_10 = 0;
      __pyx_9 = 0;
      __pyx_7 = 0;
      __pyx_5 = PyTuple_New(1); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
      PyTuple_SET_ITEM(__pyx_5, 0, __pyx_4);
      __pyx_4 = 0;
      __pyx_3 = PyObject_CallObject(__pyx_8, __pyx_5); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; goto __pyx_L1;}
      Py_DECREF(__pyx_8); __pyx_8 = 0;
      Py_DECREF(__pyx_5); __pyx_5 = 0;
      Py_DECREF(__pyx_3); __pyx_3 = 0;
      __pyx_L8:;
    }
    __pyx_L9:;
    Py_DECREF(__pyx_6); __pyx_6 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":46 */
    __pyx_10 = PyObject_GetAttr(__pyx_v_points_in_strip, __pyx_n_sort); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; goto __pyx_L1;}
    __pyx_9 = PyObject_CallObject(__pyx_10, 0); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; goto __pyx_L1;}
    Py_DECREF(__pyx_10); __pyx_10 = 0;
    Py_DECREF(__pyx_9); __pyx_9 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":48 */
    __pyx_v_min_d = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":49 */
    Py_INCREF(Py_None);
    Py_DECREF(__pyx_v_min_p1);
    __pyx_v_min_p1 = Py_None;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":50 */
    Py_INCREF(Py_None);
    Py_DECREF(__pyx_v_min_p2);
    __pyx_v_min_p2 = Py_None;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":51 */
    __pyx_1 = PyObject_Length(__pyx_v_points_in_strip); if (__pyx_1 == -1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 51; goto __pyx_L1;}
    __pyx_7 = PyInt_FromSsize_t(__pyx_1); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 51; goto __pyx_L1;}
    Py_DECREF(__pyx_v_max_i);
    __pyx_v_max_i = __pyx_7;
    __pyx_7 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":52 */
    __pyx_4 = PyTuple_New(1); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; goto __pyx_L1;}
    Py_INCREF(__pyx_v_points_in_strip);
    PyTuple_SET_ITEM(__pyx_4, 0, __pyx_v_points_in_strip);
    __pyx_8 = PyObject_CallObject(((PyObject *)(&PyEnum_Type)), __pyx_4); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; goto __pyx_L1;}
    Py_DECREF(__pyx_4); __pyx_4 = 0;
    __pyx_5 = PyObject_GetIter(__pyx_8); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; goto __pyx_L1;}
    Py_DECREF(__pyx_8); __pyx_8 = 0;
    for (;;) {
      __pyx_3 = PyIter_Next(__pyx_5);
      if (!__pyx_3) {
        if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; goto __pyx_L1;}
        break;
      }
      __pyx_6 = PyObject_GetIter(__pyx_3); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; goto __pyx_L1;}
      Py_DECREF(__pyx_3); __pyx_3 = 0;
      __pyx_10 = __Pyx_UnpackItem(__pyx_6); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; goto __pyx_L1;}
      Py_DECREF(__pyx_v_i);
      __pyx_v_i = __pyx_10;
      __pyx_10 = 0;
      __pyx_9 = __Pyx_UnpackItem(__pyx_6); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; goto __pyx_L1;}
      Py_DECREF(__pyx_v_point);
      __pyx_v_point = __pyx_9;
      __pyx_9 = 0;
      if (__Pyx_EndUnpack(__pyx_6) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; goto __pyx_L1;}
      Py_DECREF(__pyx_6); __pyx_6 = 0;
      __pyx_7 = PyInt_FromLong(1); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      __pyx_4 = PyNumber_Add(__pyx_v_i, __pyx_7); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      Py_DECREF(__pyx_7); __pyx_7 = 0;
      __pyx_1 = PyInt_AsSsize_t(__pyx_4); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      Py_DECREF(__pyx_4); __pyx_4 = 0;
      __pyx_8 = __Pyx_GetName(__pyx_b, __pyx_n_min); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      __pyx_3 = PyInt_FromLong(7); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      __pyx_10 = PyNumber_Add(__pyx_v_i, __pyx_3); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      Py_DECREF(__pyx_3); __pyx_3 = 0;
      __pyx_9 = PyTuple_New(2); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      Py_INCREF(__pyx_v_max_i);
      PyTuple_SET_ITEM(__pyx_9, 0, __pyx_v_max_i);
      PyTuple_SET_ITEM(__pyx_9, 1, __pyx_10);
      __pyx_10 = 0;
      __pyx_6 = PyObject_CallObject(__pyx_8, __pyx_9); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      Py_DECREF(__pyx_8); __pyx_8 = 0;
      Py_DECREF(__pyx_9); __pyx_9 = 0;
      __pyx_13 = PyInt_AsSsize_t(__pyx_6); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      Py_DECREF(__pyx_6); __pyx_6 = 0;
      __pyx_7 = PySequence_GetSlice(__pyx_v_points_in_strip, __pyx_1, __pyx_13); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      __pyx_4 = PyObject_GetIter(__pyx_7); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
      Py_DECREF(__pyx_7); __pyx_7 = 0;
      for (;;) {
        __pyx_3 = PyIter_Next(__pyx_4);
        if (!__pyx_3) {
          if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; goto __pyx_L1;}
          break;
        }
        Py_DECREF(__pyx_v_point2);
        __pyx_v_point2 = __pyx_3;
        __pyx_3 = 0;

        /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":54 */
        __pyx_10 = __Pyx_GetName(__pyx_m, __pyx_n_distance); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
        __pyx_8 = PySequence_GetItem(__pyx_v_point, 0); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
        __pyx_9 = PySequence_GetItem(__pyx_v_point, 1); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
        __pyx_6 = PySequence_GetItem(__pyx_v_point2, 0); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
        __pyx_7 = PySequence_GetItem(__pyx_v_point2, 1); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
        __pyx_3 = PyTuple_New(4); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
        PyTuple_SET_ITEM(__pyx_3, 0, __pyx_8);
        PyTuple_SET_ITEM(__pyx_3, 1, __pyx_9);
        PyTuple_SET_ITEM(__pyx_3, 2, __pyx_6);
        PyTuple_SET_ITEM(__pyx_3, 3, __pyx_7);
        __pyx_8 = 0;
        __pyx_9 = 0;
        __pyx_6 = 0;
        __pyx_7 = 0;
        __pyx_8 = PyObject_CallObject(__pyx_10, __pyx_3); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; goto __pyx_L1;}
        Py_DECREF(__pyx_10); __pyx_10 = 0;
        Py_DECREF(__pyx_3); __pyx_3 = 0;
        Py_DECREF(__pyx_v_sd);
        __pyx_v_sd = __pyx_8;
        __pyx_8 = 0;

        /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":55 */
        __pyx_2 = (__pyx_v_min_d == 0);
        if (!__pyx_2) {
          __pyx_9 = PyFloat_FromDouble(__pyx_v_min_d); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; goto __pyx_L1;}
          if (PyObject_Cmp(__pyx_9, __pyx_v_sd, &__pyx_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; goto __pyx_L1;}
          __pyx_2 = __pyx_2 > 0;
          Py_DECREF(__pyx_9); __pyx_9 = 0;
        }
        if (__pyx_2) {

          /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":56 */
          __pyx_11 = PyFloat_AsDouble(__pyx_v_sd); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; goto __pyx_L1;}
          __pyx_v_min_d = __pyx_11;

          /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":57 */
          __pyx_6 = PySequence_GetItem(__pyx_v_point, 1); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
          __pyx_7 = PySequence_GetItem(__pyx_v_point, 0); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
          __pyx_10 = PySequence_GetItem(__pyx_v_point, 2); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
          __pyx_3 = PyTuple_New(3); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; goto __pyx_L1;}
          PyTuple_SET_ITEM(__pyx_3, 0, __pyx_6);
          PyTuple_SET_ITEM(__pyx_3, 1, __pyx_7);
          PyTuple_SET_ITEM(__pyx_3, 2, __pyx_10);
          __pyx_6 = 0;
          __pyx_7 = 0;
          __pyx_10 = 0;
          Py_DECREF(__pyx_v_min_p1);
          __pyx_v_min_p1 = __pyx_3;
          __pyx_3 = 0;

          /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":58 */
          __pyx_8 = PySequence_GetItem(__pyx_v_point2, 1); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; goto __pyx_L1;}
          __pyx_9 = PySequence_GetItem(__pyx_v_point2, 0); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; goto __pyx_L1;}
          __pyx_6 = PySequence_GetItem(__pyx_v_point2, 2); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; goto __pyx_L1;}
          __pyx_7 = PyTuple_New(3); if (!__pyx_7) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; goto __pyx_L1;}
          PyTuple_SET_ITEM(__pyx_7, 0, __pyx_8);
          PyTuple_SET_ITEM(__pyx_7, 1, __pyx_9);
          PyTuple_SET_ITEM(__pyx_7, 2, __pyx_6);
          __pyx_8 = 0;
          __pyx_9 = 0;
          __pyx_6 = 0;
          Py_DECREF(__pyx_v_min_p2);
          __pyx_v_min_p2 = __pyx_7;
          __pyx_7 = 0;
          goto __pyx_L15;
        }
        __pyx_L15:;
      }
      Py_DECREF(__pyx_4); __pyx_4 = 0;
    }
    Py_DECREF(__pyx_5); __pyx_5 = 0;

    /* "/home/andrew/Programs/cluster/clusterer/closestpair.pyx":59 */
    __pyx_2 = (__pyx_v_min_d != 0);
    if (__pyx_2) {
      __pyx_10 = PyFloat_FromDouble(__pyx_v_min_d); if (!__pyx_10) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; goto __pyx_L1;}
      if (PyObject_Cmp(__pyx_10, __pyx_v_d, &__pyx_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; goto __pyx_L1;}
      __pyx_2 = __pyx_2 < 0;
      Py_DECREF(__pyx_10); __pyx_10 = 0;
    }
    if (__pyx_2) {
      __pyx_3 = PyFloat_FromDouble(__pyx_v_min_d); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; goto __pyx_L1;}
      __pyx_8 = PyTuple_New(3); if (!__pyx_8) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; goto __pyx_L1;}
      PyTuple_SET_ITEM(__pyx_8, 0, __pyx_3);
      Py_INCREF(__pyx_v_min_p1);
      PyTuple_SET_ITEM(__pyx_8, 1, __pyx_v_min_p1);
      Py_INCREF(__pyx_v_min_p2);
      PyTuple_SET_ITEM(__pyx_8, 2, __pyx_v_min_p2);
      __pyx_3 = 0;
      __pyx_r = __pyx_8;
      __pyx_8 = 0;
      goto __pyx_L0;
      goto __pyx_L16;
    }
    if (PyObject_Cmp(__pyx_v_d1, __pyx_v_d2, &__pyx_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; goto __pyx_L1;}
    __pyx_2 = __pyx_2 < 0;
    if (__pyx_2) {
      __pyx_9 = PyTuple_New(3); if (!__pyx_9) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; goto __pyx_L1;}
      Py_INCREF(__pyx_v_d1);
      PyTuple_SET_ITEM(__pyx_9, 0, __pyx_v_d1);
      Py_INCREF(__pyx_v_p11);
      PyTuple_SET_ITEM(__pyx_9, 1, __pyx_v_p11);
      Py_INCREF(__pyx_v_p12);
      PyTuple_SET_ITEM(__pyx_9, 2, __pyx_v_p12);
      __pyx_r = __pyx_9;
      __pyx_9 = 0;
      goto __pyx_L0;
      goto __pyx_L16;
    }
    /*else*/ {
      __pyx_6 = PyTuple_New(3); if (!__pyx_6) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; goto __pyx_L1;}
      Py_INCREF(__pyx_v_d2);
      PyTuple_SET_ITEM(__pyx_6, 0, __pyx_v_d2);
      Py_INCREF(__pyx_v_p21);
      PyTuple_SET_ITEM(__pyx_6, 1, __pyx_v_p21);
      Py_INCREF(__pyx_v_p22);
      PyTuple_SET_ITEM(__pyx_6, 2, __pyx_v_p22);
      __pyx_r = __pyx_6;
      __pyx_6 = 0;
      goto __pyx_L0;
    }
    __pyx_L16:;
  }
  __pyx_L2:;

  __pyx_r = Py_None; Py_INCREF(Py_None);
  goto __pyx_L0;
  __pyx_L1:;
  Py_XDECREF(__pyx_3);
  Py_XDECREF(__pyx_4);
  Py_XDECREF(__pyx_5);
  Py_XDECREF(__pyx_6);
  Py_XDECREF(__pyx_7);
  Py_XDECREF(__pyx_8);
  Py_XDECREF(__pyx_9);
  Py_XDECREF(__pyx_10);
  __Pyx_AddTraceback("closestpair.closest_pair");
  __pyx_r = 0;
  __pyx_L0:;
  Py_DECREF(__pyx_v_min_p1);
  Py_DECREF(__pyx_v_min_p2);
  Py_DECREF(__pyx_v_i);
  Py_DECREF(__pyx_v_point);
  Py_DECREF(__pyx_v_point2);
  Py_DECREF(__pyx_v_d);
  Py_DECREF(__pyx_v_split);
  Py_DECREF(__pyx_v_d1);
  Py_DECREF(__pyx_v_p11);
  Py_DECREF(__pyx_v_p12);
  Py_DECREF(__pyx_v_d2);
  Py_DECREF(__pyx_v_p21);
  Py_DECREF(__pyx_v_p22);
  Py_DECREF(__pyx_v_points_in_strip);
  Py_DECREF(__pyx_v_split_at);
  Py_DECREF(__pyx_v_max_i);
  Py_DECREF(__pyx_v_sd);
  Py_DECREF(__pyx_v_points);
  return __pyx_r;
}
Exemplo n.º 16
0
/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
   with    LOAD_CONST binop(c1,c2)
   The consts table must still be in list form so that the
   new constant can be appended.
   Called with codestr pointing to the first LOAD_CONST.
   Abandons the transformation if the folding fails (i.e.  1+'a').
   If the new constant is a sequence, only folds when the size
   is below a threshold value.  That keeps pyc files from
   becoming large in the presence of code like:  (None,)*1000.
*/
static int
fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
{
    PyObject *newconst, *v, *w;
    Py_ssize_t len_consts, size;
    int opcode;

    /* Pre-conditions */
    assert(PyList_CheckExact(consts));
    assert(codestr[0] == LOAD_CONST);
    assert(codestr[3] == LOAD_CONST);

    /* Create new constant */
    v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
    w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
    opcode = codestr[6];
    switch (opcode) {
        case BINARY_POWER:
            newconst = PyNumber_Power(v, w, Py_None);
            break;
        case BINARY_MULTIPLY:
            newconst = PyNumber_Multiply(v, w);
            break;
        case BINARY_TRUE_DIVIDE:
            newconst = PyNumber_TrueDivide(v, w);
            break;
        case BINARY_FLOOR_DIVIDE:
            newconst = PyNumber_FloorDivide(v, w);
            break;
        case BINARY_MODULO:
            newconst = PyNumber_Remainder(v, w);
            break;
        case BINARY_ADD:
            newconst = PyNumber_Add(v, w);
            break;
        case BINARY_SUBTRACT:
            newconst = PyNumber_Subtract(v, w);
            break;
        case BINARY_SUBSCR:
            /* #5057: if v is unicode, there might be differences between
               wide and narrow builds in cases like '\U00012345'[0] or
               '\U00012345abcdef'[3], so it's better to skip the optimization
               in order to produce compatible pycs.
            */
            if (PyUnicode_Check(v))
                return 0;
            newconst = PyObject_GetItem(v, w);
            break;
        case BINARY_LSHIFT:
            newconst = PyNumber_Lshift(v, w);
            break;
        case BINARY_RSHIFT:
            newconst = PyNumber_Rshift(v, w);
            break;
        case BINARY_AND:
            newconst = PyNumber_And(v, w);
            break;
        case BINARY_XOR:
            newconst = PyNumber_Xor(v, w);
            break;
        case BINARY_OR:
            newconst = PyNumber_Or(v, w);
            break;
        default:
            /* Called with an unknown opcode */
            PyErr_Format(PyExc_SystemError,
                 "unexpected binary operation %d on a constant",
                     opcode);
            return 0;
    }
    if (newconst == NULL) {
        if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
            PyErr_Clear();
        return 0;
    }
    size = PyObject_Size(newconst);
    if (size == -1) {
        if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
            return 0;
        PyErr_Clear();
    } else if (size > 20) {
        Py_DECREF(newconst);
        return 0;
    }

    /* Append folded constant into consts table */
    len_consts = PyList_GET_SIZE(consts);
    if (PyList_Append(consts, newconst)) {
        Py_DECREF(newconst);
        return 0;
    }
    Py_DECREF(newconst);

    /* Write NOP NOP NOP NOP LOAD_CONST newconst */
    memset(codestr, NOP, 4);
    codestr[4] = LOAD_CONST;
    SETARG(codestr, 4, len_consts);
    return 1;
}
Exemplo n.º 17
0
/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
   with    LOAD_CONST binop(c1,c2)
   The consts table must still be in list form so that the
   new constant can be appended.
   Called with codestr pointing to the BINOP.
   Abandons the transformation if the folding fails (i.e.  1+'a').
   If the new constant is a sequence, only folds when the size
   is below a threshold value.  That keeps pyc files from
   becoming large in the presence of code like:  (None,)*1000.
*/
static int
fold_binops_on_constants(unsigned char *codestr, PyObject *consts, PyObject **objs)
{
    PyObject *newconst, *v, *w;
    Py_ssize_t len_consts, size;
    int opcode;

    /* Pre-conditions */
    assert(PyList_CheckExact(consts));

    /* Create new constant */
    v = objs[0];
    w = objs[1];
    opcode = codestr[0];
    switch (opcode) {
        case BINARY_POWER:
            newconst = PyNumber_Power(v, w, Py_None);
            break;
        case BINARY_MULTIPLY:
            newconst = PyNumber_Multiply(v, w);
            break;
        case BINARY_TRUE_DIVIDE:
            newconst = PyNumber_TrueDivide(v, w);
            break;
        case BINARY_FLOOR_DIVIDE:
            newconst = PyNumber_FloorDivide(v, w);
            break;
        case BINARY_MODULO:
            newconst = PyNumber_Remainder(v, w);
            break;
        case BINARY_ADD:
            newconst = PyNumber_Add(v, w);
            break;
        case BINARY_SUBTRACT:
            newconst = PyNumber_Subtract(v, w);
            break;
        case BINARY_SUBSCR:
            newconst = PyObject_GetItem(v, w);
            break;
        case BINARY_LSHIFT:
            newconst = PyNumber_Lshift(v, w);
            break;
        case BINARY_RSHIFT:
            newconst = PyNumber_Rshift(v, w);
            break;
        case BINARY_AND:
            newconst = PyNumber_And(v, w);
            break;
        case BINARY_XOR:
            newconst = PyNumber_Xor(v, w);
            break;
        case BINARY_OR:
            newconst = PyNumber_Or(v, w);
            break;
        default:
            /* Called with an unknown opcode */
            PyErr_Format(PyExc_SystemError,
                 "unexpected binary operation %d on a constant",
                     opcode);
            return 0;
    }
    if (newconst == NULL) {
        if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
            PyErr_Clear();
        return 0;
    }
    size = PyObject_Size(newconst);
    if (size == -1) {
        if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt))
            return 0;
        PyErr_Clear();
    } else if (size > 20) {
        Py_DECREF(newconst);
        return 0;
    }

    /* Append folded constant into consts table */
    len_consts = PyList_GET_SIZE(consts);
    if (PyList_Append(consts, newconst)) {
        Py_DECREF(newconst);
        return 0;
    }
    Py_DECREF(newconst);

    /* Write NOP NOP NOP NOP LOAD_CONST newconst */
    codestr[-2] = LOAD_CONST;
    SETARG(codestr, -2, len_consts);
    return 1;
}
Exemplo n.º 18
0
/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
   with	   LOAD_CONST binop(c1,c2)
   The consts table must still be in list form so that the
   new constant can be appended.
   Called with codestr pointing to the first LOAD_CONST. 
   Abandons the transformation if the folding fails (i.e.  1+'a').  
   If the new constant is a sequence, only folds when the size
   is below a threshold value.	That keeps pyc files from
   becoming large in the presence of code like:	 (None,)*1000.
*/
static int
fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
{
	PyObject *newconst, *v, *w;
	Py_ssize_t len_consts, size;
	int opcode;

	/* Pre-conditions */
	assert(PyList_CheckExact(consts));
	assert(codestr[0] == LOAD_CONST);
	assert(codestr[3] == LOAD_CONST);

	/* Create new constant */
	v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
	w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
	opcode = codestr[6];
	switch (opcode) {
		case BINARY_POWER:
			newconst = PyNumber_Power(v, w, Py_None);
			break;
		case BINARY_MULTIPLY:
			newconst = PyNumber_Multiply(v, w);
			break;
		case BINARY_DIVIDE:
			/* Cannot fold this operation statically since
                           the result can depend on the run-time presence
                           of the -Qnew flag */
			return 0;
		case BINARY_TRUE_DIVIDE:
			newconst = PyNumber_TrueDivide(v, w);
			break;
		case BINARY_FLOOR_DIVIDE:
			newconst = PyNumber_FloorDivide(v, w);
			break;
		case BINARY_MODULO:
			newconst = PyNumber_Remainder(v, w);
			break;
		case BINARY_ADD:
			newconst = PyNumber_Add(v, w);
			break;
		case BINARY_SUBTRACT:
			newconst = PyNumber_Subtract(v, w);
			break;
		case BINARY_SUBSCR:
			newconst = PyObject_GetItem(v, w);
			break;
		case BINARY_LSHIFT:
			newconst = PyNumber_Lshift(v, w);
			break;
		case BINARY_RSHIFT:
			newconst = PyNumber_Rshift(v, w);
			break;
		case BINARY_AND:
			newconst = PyNumber_And(v, w);
			break;
		case BINARY_XOR:
			newconst = PyNumber_Xor(v, w);
			break;
		case BINARY_OR:
			newconst = PyNumber_Or(v, w);
			break;
		default:
			/* Called with an unknown opcode */
			PyErr_Format(PyExc_SystemError,
			     "unexpected binary operation %d on a constant",
				     opcode);
			return 0;
	}
	if (newconst == NULL) {
		PyErr_Clear();
		return 0;
	}
	size = PyObject_Size(newconst);
	if (size == -1)
		PyErr_Clear();
	else if (size > 20) {
		Py_DECREF(newconst);
		return 0;
	}

	/* Append folded constant into consts table */
	len_consts = PyList_GET_SIZE(consts);
	if (PyList_Append(consts, newconst)) {
		Py_DECREF(newconst);
		return 0;
	}
	Py_DECREF(newconst);

	/* Write NOP NOP NOP NOP LOAD_CONST newconst */
	memset(codestr, NOP, 4);
	codestr[4] = LOAD_CONST;
	SETARG(codestr, 4, len_consts);
	return 1;
}
Exemplo n.º 19
0
static PyObject *
range_reverse(PyObject *seq)
{
    rangeobject *range = (rangeobject*) seq;
    longrangeiterobject *it;
    PyObject *one, *sum, *diff, *len = NULL, *product;
    long lstart, lstop, lstep;

    /* XXX(nnorwitz): do the calc for the new start/stop first,
        then if they fit, call the proper iter()?
    */
    assert(PyRange_Check(seq));

    /* If all three fields convert to long, use the int version */
    lstart = PyLong_AsLong(range->start);
    if (lstart != -1 || !PyErr_Occurred()) {
	lstop = PyLong_AsLong(range->stop);
	if (lstop != -1 || !PyErr_Occurred()) {
	    lstep = PyLong_AsLong(range->step);
	    if (lstep != -1 || !PyErr_Occurred()) {
		/* XXX(nnorwitz): need to check for overflow and simplify. */
		long len = get_len_of_range(lstart, lstop, lstep);
		long new_start = lstart + (len - 1) * lstep;
		long new_stop = lstart;
		if (lstep > 0)
		    new_stop -= 1;
		else
		    new_stop += 1;
		return int_range_iter(new_start, new_stop, -lstep);
	    }
	}
    }
    PyErr_Clear();

    it = PyObject_New(longrangeiterobject, &PyLongRangeIter_Type);
    if (it == NULL)
        return NULL;

    /* start + (len - 1) * step */
    len = range_length_obj(range);
    if (!len)
        goto create_failure;

    one = PyLong_FromLong(1);
    if (!one)
        goto create_failure;

    diff = PyNumber_Subtract(len, one);
    Py_DECREF(one);
    if (!diff)
        goto create_failure;

    product = PyNumber_Multiply(len, range->step);
    if (!product)
        goto create_failure;

    sum = PyNumber_Add(range->start, product);
    Py_DECREF(product);
    it->start = sum;
    if (!it->start)
        goto create_failure;
    it->step = PyNumber_Negative(range->step);
    if (!it->step) {
        Py_DECREF(it->start);
        PyObject_Del(it);
        return NULL;
    }

    /* Steal reference to len. */
    it->len = len;

    it->index = PyLong_FromLong(0);
    if (!it->index) {
        Py_DECREF(it);
        return NULL;
    }

    return (PyObject *)it;

create_failure:
    Py_XDECREF(len);
    PyObject_Del(it);
    return NULL;
}
Exemplo n.º 20
0
NPY_NO_EXPORT PyObject *
__New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out,
                  int variance, int num)
{
    PyObject *obj1 = NULL, *obj2 = NULL, *obj3 = NULL;
    PyArrayObject *arr1 = NULL, *arr2 = NULL, *arrnew = NULL;
    PyObject *ret = NULL, *newshape = NULL;
    int i, n;
    npy_intp val;

    arrnew = (PyArrayObject *)PyArray_CheckAxis(self, &axis, 0);
    if (arrnew == NULL) {
        return NULL;
    }
    /* Compute and reshape mean */
    arr1 = (PyArrayObject *)PyArray_EnsureAnyArray(
                    PyArray_Mean(arrnew, axis, rtype, NULL));
    if (arr1 == NULL) {
        Py_DECREF(arrnew);
        return NULL;
    }
    n = PyArray_NDIM(arrnew);
    newshape = PyTuple_New(n);
    if (newshape == NULL) {
        Py_DECREF(arr1);
        Py_DECREF(arrnew);
        return NULL;
    }
    for (i = 0; i < n; i++) {
        if (i == axis) {
            val = 1;
        }
        else {
            val = PyArray_DIM(arrnew,i);
        }
        PyTuple_SET_ITEM(newshape, i, PyInt_FromLong((long)val));
    }
    arr2 = (PyArrayObject *)PyArray_Reshape(arr1, newshape);
    Py_DECREF(arr1);
    Py_DECREF(newshape);
    if (arr2 == NULL) {
        Py_DECREF(arrnew);
        return NULL;
    }

    /* Compute x = x - mx */
    arr1 = (PyArrayObject *)PyArray_EnsureAnyArray(
                PyNumber_Subtract((PyObject *)arrnew, (PyObject *)arr2));
    Py_DECREF(arr2);
    if (arr1 == NULL) {
        Py_DECREF(arrnew);
        return NULL;
    }
    /* Compute x * x */
    if (PyArray_ISCOMPLEX(arr1)) {
        obj3 = PyArray_Conjugate(arr1, NULL);
    }
    else {
        obj3 = (PyObject *)arr1;
        Py_INCREF(arr1);
    }
    if (obj3 == NULL) {
        Py_DECREF(arrnew);
        return NULL;
    }
    arr2 = (PyArrayObject *)PyArray_EnsureAnyArray(
                PyArray_GenericBinaryFunction(arr1, obj3, n_ops.multiply));
    Py_DECREF(arr1);
    Py_DECREF(obj3);
    if (arr2 == NULL) {
        Py_DECREF(arrnew);
        return NULL;
    }
    if (PyArray_ISCOMPLEX(arr2)) {
        obj3 = PyObject_GetAttrString((PyObject *)arr2, "real");
        switch(rtype) {
        case NPY_CDOUBLE:
            rtype = NPY_DOUBLE;
            break;
        case NPY_CFLOAT:
            rtype = NPY_FLOAT;
            break;
        case NPY_CLONGDOUBLE:
            rtype = NPY_LONGDOUBLE;
            break;
        }
    }
    else {
        obj3 = (PyObject *)arr2;
        Py_INCREF(arr2);
    }
    if (obj3 == NULL) {
        Py_DECREF(arrnew);
        return NULL;
    }
    /* Compute add.reduce(x*x,axis) */
    obj1 = PyArray_GenericReduceFunction((PyArrayObject *)obj3, n_ops.add,
                                         axis, rtype, NULL);
    Py_DECREF(obj3);
    Py_DECREF(arr2);
    if (obj1 == NULL) {
        Py_DECREF(arrnew);
        return NULL;
    }
    n = PyArray_DIM(arrnew,axis);
    Py_DECREF(arrnew);
    n = (n-num);
    if (n == 0) {
        n = 1;
    }
    obj2 = PyFloat_FromDouble(1.0/((double )n));
    if (obj2 == NULL) {
        Py_DECREF(obj1);
        return NULL;
    }
    ret = PyNumber_Multiply(obj1, obj2);
    Py_DECREF(obj1);
    Py_DECREF(obj2);

    if (!variance) {
        arr1 = (PyArrayObject *)PyArray_EnsureAnyArray(ret);
        /* sqrt() */
        ret = PyArray_GenericUnaryFunction(arr1, n_ops.sqrt);
        Py_DECREF(arr1);
    }
    if (ret == NULL) {
        return NULL;
    }
    if (PyArray_CheckExact(self)) {
        goto finish;
    }
    if (PyArray_Check(self) && Py_TYPE(self) == Py_TYPE(ret)) {
        goto finish;
    }
    arr1 = (PyArrayObject *)PyArray_EnsureArray(ret);
    if (arr1 == NULL) {
        return NULL;
    }
    ret = PyArray_View(arr1, NULL, Py_TYPE(self));
    Py_DECREF(arr1);

finish:
    if (out) {
        if (PyArray_AssignArray(out, (PyArrayObject *)ret,
                    NULL, NPY_DEFAULT_ASSIGN_CASTING) < 0) {
            Py_DECREF(ret);
            return NULL;
        }
        Py_DECREF(ret);
        Py_INCREF(out);
        return (PyObject *)out;
    }
    return ret;
}
Exemplo n.º 21
0
static PyObject *
longrangeiter_len(longrangeiterobject *r, PyObject *no_args)
{
    return PyNumber_Subtract(r->len, r->index);
}
Exemplo n.º 22
0
/* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
   with    LOAD_CONST binop(c1,c2)
   The consts table must still be in list form so that the
   new constant can be appended.
   Called with codestr pointing to the first LOAD_CONST.
   Abandons the transformation if the folding fails (i.e.  1+'a').
   If the new constant is a sequence, only folds when the size
   is below a threshold value.  That keeps pyc files from
   becoming large in the presence of code like:  (None,)*1000.
*/
static int
fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
{
    PyObject *newconst, *v, *w;
    Py_ssize_t len_consts, size;
    int opcode;

    /* Pre-conditions */
    assert(PyList_CheckExact(consts));
    assert(codestr[0] == LOAD_CONST);
    assert(codestr[3] == LOAD_CONST);

    /* Create new constant */
    v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
    w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
    opcode = codestr[6];
    switch (opcode) {
        case BINARY_POWER:
            newconst = PyNumber_Power(v, w, Py_None);
            break;
        case BINARY_MULTIPLY:
            newconst = PyNumber_Multiply(v, w);
            break;
        case BINARY_DIVIDE:
            /* Cannot fold this operation statically since
               the result can depend on the run-time presence
               of the -Qnew flag */
            return 0;
        case BINARY_TRUE_DIVIDE:
            newconst = PyNumber_TrueDivide(v, w);
            break;
        case BINARY_FLOOR_DIVIDE:
            newconst = PyNumber_FloorDivide(v, w);
            break;
        case BINARY_MODULO:
            newconst = PyNumber_Remainder(v, w);
            break;
        case BINARY_ADD:
            newconst = PyNumber_Add(v, w);
            break;
        case BINARY_SUBTRACT:
            newconst = PyNumber_Subtract(v, w);
            break;
        case BINARY_SUBSCR:
            newconst = PyObject_GetItem(v, w);
            /* #5057: if v is unicode, there might be differences between
               wide and narrow builds in cases like u'\U00012345'[0].
               Wide builds will return a non-BMP char, whereas narrow builds
               will return a surrogate.  In both the cases skip the
               optimization in order to produce compatible pycs.
             */
            if (newconst != NULL &&
                PyUnicode_Check(v) && PyUnicode_Check(newconst)) {
                Py_UNICODE ch = PyUnicode_AS_UNICODE(newconst)[0];
#ifdef Py_UNICODE_WIDE
                if (ch > 0xFFFF) {
#else
                if (ch >= 0xD800 && ch <= 0xDFFF) {
#endif
                    Py_DECREF(newconst);
                    return 0;
                }
            }
            break;
        case BINARY_LSHIFT:
            newconst = PyNumber_Lshift(v, w);
            break;
        case BINARY_RSHIFT:
            newconst = PyNumber_Rshift(v, w);
            break;
        case BINARY_AND:
            newconst = PyNumber_And(v, w);
            break;
        case BINARY_XOR:
            newconst = PyNumber_Xor(v, w);
            break;
        case BINARY_OR:
            newconst = PyNumber_Or(v, w);
            break;
        default:
            /* Called with an unknown opcode */
            PyErr_Format(PyExc_SystemError,
                 "unexpected binary operation %d on a constant",
                     opcode);
            return 0;
    }
    if (newconst == NULL) {
        PyErr_Clear();
        return 0;
    }
    size = PyObject_Size(newconst);
    if (size == -1)
        PyErr_Clear();
    else if (size > 20) {
        Py_DECREF(newconst);
        return 0;
    }

    /* Append folded constant into consts table */
    len_consts = PyList_GET_SIZE(consts);
    if (PyList_Append(consts, newconst)) {
        Py_DECREF(newconst);
        return 0;
    }
    Py_DECREF(newconst);

    /* Write NOP NOP NOP NOP LOAD_CONST newconst */
    memset(codestr, NOP, 4);
    codestr[4] = LOAD_CONST;
    SETARG(codestr, 4, len_consts);
    return 1;
}

static int
fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
{
    PyObject *newconst=NULL, *v;
    Py_ssize_t len_consts;
    int opcode;

    /* Pre-conditions */
    assert(PyList_CheckExact(consts));
    assert(codestr[0] == LOAD_CONST);

    /* Create new constant */
    v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
    opcode = codestr[3];
    switch (opcode) {
        case UNARY_NEGATIVE:
            /* Preserve the sign of -0.0 */
            if (PyObject_IsTrue(v) == 1)
                newconst = PyNumber_Negative(v);
            break;
        case UNARY_CONVERT:
            newconst = PyObject_Repr(v);
            break;
        case UNARY_INVERT:
            newconst = PyNumber_Invert(v);
            break;
        default:
            /* Called with an unknown opcode */
            PyErr_Format(PyExc_SystemError,
                 "unexpected unary operation %d on a constant",
                     opcode);
            return 0;
    }
    if (newconst == NULL) {
        PyErr_Clear();
        return 0;
    }

    /* Append folded constant into consts table */
    len_consts = PyList_GET_SIZE(consts);
    if (PyList_Append(consts, newconst)) {
        Py_DECREF(newconst);
        return 0;
    }
    Py_DECREF(newconst);

    /* Write NOP LOAD_CONST newconst */
    codestr[0] = NOP;
    codestr[1] = LOAD_CONST;
    SETARG(codestr, 1, len_consts);
    return 1;
}