Exemplo n.º 1
0
static PyObject *mpy_Matrix_inplace_mul(PyObject *M, PyObject *N)
{
    PyObject *tmp_arg_as_py_double;

    if (PyObject_TypeCheck(M, &mpy_MatrixType)) {
        if (PyNumber_Check(N)) {
            tmp_arg_as_py_double = PyNumber_Float(N);
            if (tmp_arg_as_py_double != NULL) {
                double N_as_double = PyFloat_AsDouble(tmp_arg_as_py_double);
                ((mpy_Matrix *)M)->M *= N_as_double;
                Py_INCREF(M);
                return M;
            }
        }
    }
    else if (PyNumber_Check(M)) {
        if (PyObject_TypeCheck(N, &mpy_MatrixType)) {
            tmp_arg_as_py_double = PyNumber_Float(M);
            if (tmp_arg_as_py_double != NULL) {
                double M_as_double = PyFloat_AsDouble(tmp_arg_as_py_double);
                ((mpy_Matrix *)N)->M *= M_as_double;
                Py_INCREF(N);
                return N;
            }
        }
    }

    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}
Exemplo n.º 2
0
static PyObject *mpy_Matrix_mul(PyObject *M, PyObject *N)
{
    PyObject *tmp_arg_as_py_double;
    mpy_Matrix *result;

    if (PyObject_TypeCheck(M, &mpy_MatrixType)) {
        if (PyNumber_Check(N)) {
            tmp_arg_as_py_double = PyNumber_Float(N);
            if (tmp_arg_as_py_double != NULL) {
                double N_as_double = PyFloat_AsDouble(tmp_arg_as_py_double);
                result = mpy_Matrix_NEW();
                result->M = (((mpy_Matrix *)M)->M)*N_as_double;
                return (PyObject *)result;
            }
        }
    }
    else if (PyNumber_Check(M)) {
        if (PyObject_TypeCheck(N, &mpy_MatrixType)) {
            tmp_arg_as_py_double = PyNumber_Float(M);
            if (tmp_arg_as_py_double != NULL) {
                double M_as_double = PyFloat_AsDouble(tmp_arg_as_py_double);
                result = mpy_Matrix_NEW();
                result->M = (((mpy_Matrix *)N)->M)*M_as_double;
                return (PyObject *)result;
            }
        }
    }

    Py_INCREF(Py_NotImplemented);
    return Py_NotImplemented;
}
Exemplo n.º 3
0
static PyObject *Polygon_addContour(Polygon *self, PyObject *args) {
#ifdef WITH_NUMERIC
    PyObject *a=NULL;
    gpc_vertex_list *vl;
    int hole = 0;
    if (! PyArg_ParseTuple(args, "O|i", &a, &hole))
        return Polygon_Raise(ERR_ARG);
    if ((a = PyArray_ContiguousFromObject(a, PyArray_DOUBLE, 2, 2)) == NULL)
        return Polygon_Raise(ERR_ARG);
    if (((PyArrayObject *)a)->nd != 2)            return Polygon_Raise(ERR_ARG);
    if (((PyArrayObject *)a)->dimensions[1] != 2) return Polygon_Raise(ERR_ARG);
    vl = PyMem_New(gpc_vertex_list, 1);
    vl->num_vertices = ((PyArrayObject *)a)->dimensions[0];
    vl->vertex = PyMem_New(gpc_vertex, vl->num_vertices);
    memcpy((vl->vertex), (((PyArrayObject *)a)->data), 2*vl->num_vertices*sizeof(double));
    Py_DECREF(a);
#else
    PyObject *list=NULL, *flist, *point=NULL, *X, *Y;
    gpc_vertex_list *vl;
    gpc_vertex *v;
    int i, imax, hole = 0;
    if (! PyArg_ParseTuple(args, "O|i", &list, &hole))
        return Polygon_Raise(ERR_ARG);
    if (! PySequence_Check(list))
        return Polygon_Raise(ERR_ARG);
    flist = PySequence_Fast(list, "this is not a sequence");
    if ((! flist) || ((imax = PySequence_Length(flist)) <= 2))
        return Polygon_Raise(ERR_INV);
    vl = PyMem_New(gpc_vertex_list, 1);
    vl->num_vertices = imax;
    vl->vertex = v = PyMem_New(gpc_vertex, imax);
    for (i=0; i<imax; i++) {
        point = PySequence_Fast(PySequence_Fast_GET_ITEM(flist, i), "this is not a point");
        if ((!point) || (PySequence_Length(point) != 2))
            return Polygon_Raise(ERR_INV);
        v->x = PyFloat_AsDouble(X = PyNumber_Float(PySequence_Fast_GET_ITEM(point, 0)));
        v->y = PyFloat_AsDouble(Y = PyNumber_Float(PySequence_Fast_GET_ITEM(point, 1)));
        v++;
        Py_DECREF(X);
        Py_DECREF(Y);
        Py_DECREF(point);
    }
    Py_DECREF(flist);
#endif /* WITH_NUMERIC */
    gpc_add_contour(self->p, vl, hole);
    self->bbValid = 0;
    PyMem_Free(vl->vertex);
    PyMem_Free(vl);
    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 4
0
static PyObject *
LFO_setFreq(LFO *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;
	
	if (arg == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}
    
	int isNumber = PyNumber_Check(arg);
	
	tmp = arg;
	Py_INCREF(tmp);
	Py_DECREF(self->freq);
	if (isNumber == 1) {
		self->freq = PyNumber_Float(tmp);
        self->modebuffer[2] = 0;
	}
	else {
		self->freq = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->freq, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->freq_stream);
        self->freq_stream = (Stream *)streamtmp;
		self->modebuffer[2] = 1;
	}
    
    (*self->mode_func_ptr)(self);
    
	Py_INCREF(Py_None);
	return Py_None;
}	
Exemplo n.º 5
0
PyObject * tupleToDoubleArray(PyObject *tuple_obj, double *arr, unsigned n, int demandExactLen) {
	PyObject *item, *f_item;
	unsigned pytuple_len = (unsigned) PyTuple_Size(tuple_obj);
	unsigned i;
	if (pytuple_len != n && ((demandExactLen != 0) || (pytuple_len < n))) {
		PyErr_SetString(PyExc_IndexError, "tuple index out of range");
		return 0L;
	}
	for (i = 0; i < n; ++i) {
		item = PyTuple_GetItem(tuple_obj, i);
		if (item == 0L) {
			return 0L;
		}
		Py_INCREF(item);
		f_item = PyNumber_Float(item);
		if (f_item == 0L) {
			Py_DECREF(item);
			return 0L;
		}
		arr[i] = PyFloat_AsDouble(item);
		Py_DECREF(item);
		Py_DECREF(f_item);
	}
	return none();
}
Exemplo n.º 6
0
PyObject * listToDoubleArray(PyObject *list_obj, double *arr, unsigned n) {
	PyObject *item, *f_item;
	unsigned pylist_len = (unsigned) PyList_Size(list_obj);
	unsigned i;
	if (pylist_len != n) {
		PyErr_SetString(PyExc_IndexError, "list index out of range");
		return 0L;
	}
	for (i = 0; i < n; ++i) {
		item = PyList_GetItem(list_obj, i);
		if (item == 0L) {
			return 0L;
		}
		Py_INCREF(item);
		f_item = PyNumber_Float(item);
		if (f_item == 0L) {
			Py_DECREF(item);
			return 0L;
		}
		arr[i] = PyFloat_AsDouble(item);
		Py_DECREF(item);
		Py_DECREF(f_item);
	}
	return none();
}
Exemplo n.º 7
0
static PyObject *
SigTo_setValue(SigTo *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;
	
	if (arg == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}
    
	int isNumber = PyNumber_Check(arg);
	
	tmp = arg;
	Py_INCREF(tmp);
    Py_DECREF(self->value);
	if (isNumber == 1) {
		self->value = PyNumber_Float(tmp);
        self->modebuffer[2] = 0;
    }
    else {
		self->value = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->value, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->value_stream);
        self->value_stream = (Stream *)streamtmp;
        self->modebuffer[2] = 1;
    }
    
	Py_INCREF(Py_None);
	return Py_None;
}	
Exemplo n.º 8
0
static PyObject *py_ue_sborder_set_padding(ue_PySBorder *self, PyObject * args)
{
	ue_py_slate_cast(SBorder);

	PyObject *py_padding;
	if (!PyArg_ParseTuple(args, "O:set_padding", &py_padding))
	{
		return nullptr;
	}

	FMargin *margin = ue_py_check_struct<FMargin>(py_padding);
	if (!margin)
	{
		if (!PyNumber_Check(py_padding))
		{
			return PyErr_Format(PyExc_Exception, "argument is not a FMargin or a number");
		}
		PyObject *py_float = PyNumber_Float(py_padding);
		FMargin new_margin(PyFloat_AsDouble(py_float));
		margin = &new_margin;
		Py_DECREF(py_float);
	}

	py_SBorder->SetPadding(*margin);

	Py_RETURN_SLATE_SELF;
}
Exemplo n.º 9
0
static PyObject *
Chorus_setMix(Chorus *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;
	
	if (arg == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}
    
	int isNumber = PyNumber_Check(arg);
    
	tmp = arg;
	Py_INCREF(tmp);
	Py_DECREF(self->mix);
	if (isNumber == 1) {
		self->mix = PyNumber_Float(tmp);
        self->modebuffer[4] = 0;
	}
	else {
		self->mix = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->mix, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->mix_stream);
        self->mix_stream = (Stream *)streamtmp;
		self->modebuffer[4] = 1;
	}
    
    (*self->mode_func_ptr)(self);
    
	Py_INCREF(Py_None);
	return Py_None;
}	
Exemplo n.º 10
0
NUITKA_MAY_BE_UNUSED static PyObject *TO_FLOAT( PyObject *value )
{
    PyObject *result;

#if PYTHON_VERSION < 300
    if ( PyString_CheckExact( value ) )
    {
        result = PyFloat_FromString( value, NULL );
    }
#else
    if ( PyUnicode_CheckExact( value ) )
    {
        result = PyFloat_FromString( value );
    }
#endif
    else
    {
        result = PyNumber_Float( value );
    }

    if (unlikely( result == NULL ))
    {
        return NULL;
    }

    return result;
}
Exemplo n.º 11
0
static PyObject *
FourBandMain_setFreq3(FourBandMain *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;

	if (arg == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}

	int isNumber = PyNumber_Check(arg);

	tmp = arg;
	Py_INCREF(tmp);
	Py_DECREF(self->freq3);
	if (isNumber == 1) {
		self->freq3 = PyNumber_Float(tmp);
        self->modebuffer[2] = 0;
	}
	else {
		self->freq3 = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->freq3, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->freq3_stream);
        self->freq3_stream = (Stream *)streamtmp;
		self->modebuffer[2] = 1;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
Exemplo n.º 12
0
static PyObject *
BandSplitter_setQ(BandSplitter *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;

	if (arg == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}

	int isNumber = PyNumber_Check(arg);

	tmp = arg;
	Py_INCREF(tmp);
	Py_DECREF(self->q);
	if (isNumber == 1) {
		self->q = PyNumber_Float(tmp);
        self->modebuffer[0] = 0;
        BandSplitter_compute_variables((BandSplitter *)self, PyFloat_AS_DOUBLE(self->q));
	}
	else {
		self->q = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->q, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->q_stream);
        self->q_stream = (Stream *)streamtmp;
		self->modebuffer[0] = 1;
	}

    (*self->mode_func_ptr)(self);

	Py_INCREF(Py_None);
	return Py_None;
}
Exemplo n.º 13
0
static PyObject *
OscBank_setArnda(OscBank *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;
	
	if (arg == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}
    
	int isNumber = PyNumber_Check(arg);
	
	tmp = arg;
	Py_INCREF(tmp);
	Py_DECREF(self->arnda);
	if (isNumber == 1) {
		self->arnda = PyNumber_Float(tmp);
        self->modebuffer[8] = 0;
	}
	else {
		self->arnda = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->arnda, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->arnda_stream);
        self->arnda_stream = (Stream *)streamtmp;
		self->modebuffer[8] = 1;
	}
    
	Py_INCREF(Py_None);
	return Py_None;
}	
Exemplo n.º 14
0
static PyObject *
NewMatrix_setMatrix(NewMatrix *self, PyObject *value)
{
    int i, j;
    PyObject *innerlist;

    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "Cannot delete the list attribute.");
        return PyInt_FromLong(-1);
    }
    
    if (! PyList_Check(value)) {
        PyErr_SetString(PyExc_TypeError, "The matrix value value must be a list.");
        return PyInt_FromLong(-1);
    }

    int height = PyList_Size(value);
    int width = PyList_Size(PyList_GetItem(value, 0));
    if (height != self->height || width != self->width) {
        PyErr_SetString(PyExc_TypeError, "New matrix must be of the same size as actual matrix.");
        return PyInt_FromLong(-1);
    }
    
    for(i=0; i<self->height; i++) {
        innerlist = PyList_GetItem(value, i);
        for (j=0; j<self->width; j++) {
            self->data[i][j] = PyFloat_AS_DOUBLE(PyNumber_Float(PyList_GET_ITEM(innerlist, j)));
        }    
    }

    Py_INCREF(Py_None);
    return Py_None;    
}
Exemplo n.º 15
0
static PyObject *
Harmonizer_setFeedback(Harmonizer *self, PyObject *arg)
{
    PyObject *tmp, *streamtmp;

    if (arg == NULL) {
        Py_INCREF(Py_None);
        return Py_None;
    }

    int isNumber = PyNumber_Check(arg);

    tmp = arg;
    Py_INCREF(tmp);
    Py_DECREF(self->feedback);
    if (isNumber == 1) {
        self->feedback = PyNumber_Float(tmp);
        self->modebuffer[3] = 0;
    }
    else {
        self->feedback = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->feedback, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->feedback_stream);
        self->feedback_stream = (Stream *)streamtmp;
        self->modebuffer[3] = 1;
    }

    (*self->mode_func_ptr)(self);

    Py_INCREF(Py_None);
    return Py_None;
}
Exemplo n.º 16
0
static PyObject *
OscBank_setArndf(OscBank *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;

    ASSERT_ARG_NOT_NULL

	int isNumber = PyNumber_Check(arg);

	tmp = arg;
	Py_INCREF(tmp);
	Py_DECREF(self->arndf);
	if (isNumber == 1) {
		self->arndf = PyNumber_Float(tmp);
        self->modebuffer[7] = 0;
	}
	else {
		self->arndf = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->arndf, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->arndf_stream);
        self->arndf_stream = (Stream *)streamtmp;
		self->modebuffer[7] = 1;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
Exemplo n.º 17
0
static PyObject *NumberValue(PyObject *self, PyObject *args)
{
  PyObject *object;
  PyObject *result = NULL;

  if (!PyArg_ParseTuple(args, "O:NumberValue", &object))
    return NULL;
  
  result = PyNumber_Float(object);
  if (result)
    return result;

  PyErr_Clear();

  if (PyString_Check(object) || PyUnicode_Check(object))
    return string_to_number(object);

  /* convert it to a string */
  object = object_to_string(object);
  if (object) {
    result = string_to_number(object);
    Py_DECREF(object);
  }
  else result = NULL;

  return result;
}
Exemplo n.º 18
0
/*
 * READER
 */
static int
parse_save_field(ReaderObj *self)
{
    PyObject *field;

    field = PyString_FromStringAndSize(self->field, self->field_len);
    if (field == NULL)
        return -1;
    self->field_len = 0;
    if (self->numeric_field) {
        PyObject *tmp;

        self->numeric_field = 0;
        tmp = PyNumber_Float(field);
        if (tmp == NULL) {
            Py_DECREF(field);
            return -1;
        }
        Py_DECREF(field);
        field = tmp;
    }
    PyList_Append(self->fields, field);
    Py_DECREF(field);
    return 0;
}
Exemplo n.º 19
0
Arquivo: _csv.c Projeto: 1st1/cpython
/*
 * READER
 */
static int
parse_save_field(ReaderObj *self)
{
    PyObject *field;

    field = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND,
                                      (void *) self->field, self->field_len);
    if (field == NULL)
        return -1;
    self->field_len = 0;
    if (self->numeric_field) {
        PyObject *tmp;

        self->numeric_field = 0;
        tmp = PyNumber_Float(field);
        Py_DECREF(field);
        if (tmp == NULL)
            return -1;
        field = tmp;
    }
    if (PyList_Append(self->fields, field) < 0) {
        Py_DECREF(field);
        return -1;
    }
    Py_DECREF(field);
    return 0;
}
Exemplo n.º 20
0
static PyObject *
LFO_setSharp(LFO *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;

    ASSERT_ARG_NOT_NULL

	int isNumber = PyNumber_Check(arg);

	tmp = arg;
	Py_INCREF(tmp);
	Py_DECREF(self->sharp);
	if (isNumber == 1) {
		self->sharp = PyNumber_Float(tmp);
        self->modebuffer[3] = 0;
	}
	else {
		self->sharp = tmp;
        streamtmp = PyObject_CallMethod((PyObject *)self->sharp, "_getStream", NULL);
        Py_INCREF(streamtmp);
        Py_XDECREF(self->sharp_stream);
        self->sharp_stream = (Stream *)streamtmp;
		self->modebuffer[3] = 1;
	}

    (*self->mode_func_ptr)(self);

	Py_INCREF(Py_None);
	return Py_None;
}
Exemplo n.º 21
0
 bool GetDoubleFromPyObject(PyObject* object, double* val)
 {
     if(!val || !object) return false;
     
     if( PyFloat_Check( object ) )
     {
         *val = PyFloat_AS_DOUBLE( object );
         return true;
     }
     
     if( PyInt_Check( object ) )
     {
         *val = static_cast<double>( PyInt_AS_LONG( object ) );
         return true;
     }
     
     PyObject* floatObject = PyNumber_Float(object);
     if(floatObject)
     {
         *val = PyFloat_AS_DOUBLE( floatObject );
         Py_DECREF(floatObject);
         return true;
     }
     
     PyErr_Clear();
     return false;
 }
Exemplo n.º 22
0
inline FloatPoint coerce_FloatPoint(PyObject* obj) {
  // Fast method if the Point is a real FloatPoint or Point type.
  PyTypeObject* t = get_FloatPointType();
  if (t == 0) {
    PyErr_SetString(PyExc_RuntimeError, "Couldn't get FloatPoint type.");
    throw std::runtime_error("Couldn't get FloatPoint type.");
  }
  if (PyObject_TypeCheck(obj, t)) {
    return FloatPoint(*(((FloatPointObject*)obj)->m_x));
  }

  PyTypeObject* t2 = get_PointType();
  if (t2 == 0) {
    PyErr_SetString(PyExc_RuntimeError, "Couldn't get Point type.");
    throw std::runtime_error("Couldn't get Point type.");
  }
  if (PyObject_TypeCheck(obj, t2))
    return FloatPoint(*(((PointObject*)obj)->m_x));

  PyObject* py_x0 = NULL;
  PyObject* py_y0 = NULL;
  PyObject* py_x1 = NULL;
  PyObject* py_y1 = NULL;

  // Treat 2-element sequences as Points.
  if (PySequence_Check(obj)) {
    if (PySequence_Length(obj) == 2) {
      py_x0 = PySequence_GetItem(obj, 0);
      py_x1 = PyNumber_Float(py_x0);
      if (py_x1 != NULL) {
        double x = PyFloat_AsDouble(py_x1);
        Py_DECREF(py_x1);
        py_y0 = PySequence_GetItem(obj, 1);
        py_y1 = PyNumber_Float(py_y0);
        if (py_y1 != NULL) {
          double y = PyFloat_AsDouble(py_y1);
          Py_DECREF(py_y1);
          return FloatPoint(x, y);
        }
      }
    }
  }

  PyErr_Clear();
  PyErr_SetString(PyExc_TypeError, "Argument is not a FloatPoint (or convertible to one.)");
  throw std::invalid_argument("Argument is not a FloatPoint (or convertible to one.)");
}
Exemplo n.º 23
0
static PyObject *py_ue_fslate_style_set_set(ue_PyFSlateStyleSet *self, PyObject * args)
{
	char *name;
	PyObject *py_value;
	if (!PyArg_ParseTuple(args, "sO:set", &name, &py_value))
		return NULL;

	FSlateSound *slate_sound = ue_py_check_struct<FSlateSound>(py_value);
	if (slate_sound)
	{
		self->style_set->Set(FName(name), *slate_sound);
		Py_RETURN_NONE;
	}

	FSlateBrush *slate_brush = ue_py_check_struct<FSlateBrush>(py_value);
	if (slate_brush)
	{
		self->style_set->Set(FName(name), slate_brush);
		Py_RETURN_NONE;
	}

	FSlateColor *slate_color = ue_py_check_struct<FSlateColor>(py_value);
	if (slate_brush)
	{
		self->style_set->Set(FName(name), *slate_color);
		Py_RETURN_NONE;
	}

	FSlateFontInfo *slate_font = ue_py_check_struct<FSlateFontInfo>(py_value);
	if (slate_font)
	{
		self->style_set->Set(FName(name), *slate_font);
		Py_RETURN_NONE;
	}

	ue_PyFLinearColor *py_linear_color = py_ue_is_flinearcolor(py_value);
	if (py_linear_color)
	{
		self->style_set->Set(FName(name), py_linear_color->color);
		Py_RETURN_NONE;
	}

	ue_PyFColor *py_color = py_ue_is_fcolor(py_value);
	if (py_color)
	{
		self->style_set->Set(FName(name), py_color->color);
		Py_RETURN_NONE;
	}

	if (PyNumber_Check(py_value))
	{
		PyObject *py_float = PyNumber_Float(py_value);
		self->style_set->Set(FName(name), (float)PyFloat_AsDouble(py_float));
		Py_DECREF(py_float);
		Py_RETURN_NONE;
	}

	return PyErr_Format(PyExc_ValueError, "unsupported value type");
}
Exemplo n.º 24
0
static PyObject *
to_float(PyObject *self, PyObject *arg)
{
    if (arg == Py_None)
        Py_RETURN_NONE;

    return PyNumber_Float(arg);
}
static bool coord_from_py_noerr(PyObject* obj, coord& c){
  if (!PyNumber_Check(obj)){
    return false;
  }
  PyObject* pythonFloat = PyNumber_Float(obj);
  c = PyFloat_AsDouble(pythonFloat);
  return true;
}
Exemplo n.º 26
0
static int Bar_setbounds(BarObject *self, PyObject *value, void *closure) {
  int i;
  double lb=0.0, ub=0.0;
  PyObject *lo, *uo;

  void (*bounder)(glp_prob*,int,int,double,double) = NULL;
  if (!Bar_Valid(self, 1)) return -1;

  i = Bar_Index(self)+1;
  bounder = Bar_Row(self) ? glp_set_row_bnds : glp_set_col_bnds;

  if (value==NULL || value==Py_None) {
    // We want it unbounded and free.
    bounder(LP, i, GLP_FR, 0.0, 0.0);
    return 0;
  }

  if (PyNumber_Check(value)) {
    // We want an equality fixed bound.
    value = PyNumber_Float(value);
    if (!value) return -1;
    lb = PyFloat_AsDouble(value);
    Py_DECREF(value);
    if (PyErr_Occurred()) return -1;
    bounder(LP, i, GLP_FX, lb, lb);
    return 0;
  }

  char t_error[] = "bounds must be set to None, number, or pair of numbers";
  if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value)!=2) {
    PyErr_SetString(PyExc_TypeError, t_error);
    return -1;
  }

  // Get the lower and upper object.  These references are borrowed.
  lo = PyTuple_GetItem(value, 0);
  uo = PyTuple_GetItem(value, 1);

  if ((lo!=Py_None && !PyNumber_Check(lo)) ||
      (uo!=Py_None && !PyNumber_Check(uo))) {
    PyErr_SetString(PyExc_TypeError, t_error);
    return -1;
  }
  if (lo==Py_None) lo=NULL; else lb=PyFloat_AsDouble(lo);
  if (PyErr_Occurred()) return -1;
  if (uo==Py_None) uo=NULL; else ub=PyFloat_AsDouble(uo);
  if (PyErr_Occurred()) return -1;

  if (!lo && !uo)	bounder(LP, i, GLP_FR, 0.0, 0.0);
  else if (!uo)		bounder(LP, i, GLP_LO, lb, 0.0);
  else if (!lo)		bounder(LP, i, GLP_UP, 0.0, ub);
  else if (lb<=ub)	bounder(LP, i, lb==ub ? GLP_FX : GLP_DB, lb, ub);
  else {
    PyErr_SetString(PyExc_ValueError, "lower bound cannot exceed upper bound");
    return -1;
  }
  return 0;
}
Exemplo n.º 27
0
void pb_num_feature::add_feature(
    const std::string& key,
    double value,
    std::vector<std::pair<std::string, double> >& ret_fv) const {
  scoped_gil lk;

  pb_object pkey(pb_unicode_from_string(key));
  PB_CHECK(pkey,
           "cannot convert input key to Python object: " << key);

  pb_object pval(PyFloat_FromDouble(value));
  PB_CHECK(pval,
           "cannot convert input value to Python object for key: " << key);

  pb_object ret(PyObject_CallMethodObjArgs(
      ins_.get(),
      method_.get(),
      pkey.get(),
      pval.get(),
      NULL));
  PB_CHECK(ret,
           name_ << " method cannot be called");
  PB_CHECK(PyList_CheckExact(ret.get()),
           name_ << " method returned non-list type: " << pb_str(ret.get()));

  size_t size = PyList_Size(ret.get());
  for (size_t i = 0; i < size; ++i) {
    PyObject* tpl = PyList_GetItem(ret.get(), i);

    PB_CHECK(tpl,
             "item " << i << " cannot be accessed: "
             << pb_str(ret.get()));
    PB_CHECK(PyTuple_CheckExact(tpl),
             "list must not contain non-tuple: " << pb_str(tpl));
    PB_CHECK(PyTuple_Size(tpl) == 2,
             "tuple length must be 2: " << pb_str(tpl));

    PyObject* f_key = PyTuple_GetItem(tpl, 0);
    PyObject* f_val = PyTuple_GetItem(tpl, 1);

    PB_CHECK(PyUnicode_CheckExact(f_key),
             "feature key must be a unicode string: " << pb_str(tpl));
    PB_CHECK(PyNumber_Check(f_val),
             "feature value must be a number: " << pb_str(tpl));

    pb_object f_key_enc(PyUnicode_AsUTF8String(f_key));
    PB_CHECK(f_key_enc,
             "feature key cannot be encoded as UTF-8: "
             << pb_str(tpl));
    pb_object f_val_float(PyNumber_Float(f_val));
    PB_CHECK(f_val_float,
             "value cannot be converted as float: " << pb_str(tpl));

    ret_fv.push_back(std::make_pair(
        std::string(PyBytes_AsString(f_key_enc.get())),
        PyFloat_AsDouble(f_val_float.get())));
  }
}
Exemplo n.º 28
0
static PyObject*
tp_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
    SELF self;
    PySoy_scenes_Scene_Object*   scene;
    PySoy_bodies_Body_Object*    controlled;
    PySoy_atoms_Position_Object* dest;
    float radius, speed, granularity = -1.0f, fuzziness = -1.0f;
    int updates = FALSE, paused = FALSE;
    PyObject* bounds = Py_None;

    static char *kw[] = {"scene", "controlled", "dest", "speed", "granularity", "fuzziness", "bounds", "updates", "paused", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!O!ff|fOii",   kw,
                                     &PySoy_scenes_Scene_Type,      &scene,
                                     &PySoy_bodies_Body_Type,       &controlled,
                                     &PySoy_atoms_Position_Type,    &dest,
                                                                    &speed,
                                                                    &granularity,
                                                                    &fuzziness,
                                                                    &bounds,
                                                                    &updates,
                                                                    &paused))
        return NULL;

    if (fuzziness == -1.0f) {
        fuzziness = speed/10000.0f;
    } else if (fuzziness < 0.0f) {
        PyErr_SetString(PyExc_ValueError, "'fuzziness' must be a number greater than 0");
        return NULL;
    }

    self = (SELF) PyType_GenericNew(type, args, kwds);
    if (!self)
        return NULL;

    if (bounds == Py_None) {
        self->g = soy_controllers_space_navigator_new(scene->g, controlled->g, speed, fuzziness, granularity, dest->g, updates, paused);
    } else if (PySoy_atoms_Size_Check(bounds)) {
        soycontrollersgraphSpace* graph = soy_controllers_graph_space_new_with_size(scene->g, granularity, ((PySoy_atoms_Size_Object*)bounds)->g,NULL);
        self->g = soy_controllers_space_navigator_new_with_graph(scene->g, controlled->g, speed, fuzziness, (soycontrollersgraphIGraph*) graph, dest->g, updates, paused);
    } else if (PyNumber_Check(bounds)) {
        PyObject* flt = PyNumber_Float(bounds);
        if (flt == NULL) 
            return NULL;
        radius = (float)PyFloat_AsDouble(flt);
        Py_DECREF(flt);
        if (PyErr_Occurred()) { return NULL; }

        soycontrollersgraphSpace* graph = soy_controllers_graph_space_new_with_radius(scene->g, granularity, radius,NULL);
        self->g = soy_controllers_space_navigator_new_with_graph(scene->g, controlled->g, speed, fuzziness, (soycontrollersgraphIGraph*) graph, dest->g, updates, paused);
    } else {
        PyErr_SetString(PyExc_ValueError, "'bounds' must be either a number greater then 0 or a soy.atoms.Size");
        return NULL;
    }

    return (PyObject*) self;
}
Exemplo n.º 29
0
/*
 * new_points
 *
 * Stores the items of a Numerical Python array or a Python sequence in a
 * C vector of double values.  Returns the number of elements or -1.
 * The C vector must be freed with g_free().
 */
static gint
new_points(PyObject *sequence, gdouble **out_points, int *out_array_type)
{
    int n = 0;
    gdouble *points = NULL;

    *out_array_type = PyArray_NOTYPE;
#ifndef WITHOUT_NUMPY
    if (PyArray_Check(sequence)) {
        PyArrayObject *array;

        array = (PyArrayObject *)
                PyArray_ContiguousFromObject(sequence, PyArray_DOUBLE, 1, 1);
        if (!array)
            return -1;
        n = array->dimensions[0];
        points = g_new(gdouble, n);
        memcpy(points, array->data, n * sizeof(double));
        Py_DECREF(array);
        *out_array_type = ((PyArrayObject *) sequence)->descr->type_num;
    } else
#endif
        if (PySequence_Check(sequence)) {
            n = PySequence_Length(sequence);
            if (n > 0) {
                int i;

                points = g_new(gdouble, n);
                for (i = 0; i < n; ++i) {
                    PyObject *item, *value;

                    item = PySequence_GetItem(sequence, i);
                    if (PyFloat_Check(item)) {
                        points[i] = PyFloat_AS_DOUBLE(item);
                    } else if (PyNumber_Check(item)
                               && (value = PyNumber_Float(item))) {
                        points[i] = PyFloat_AS_DOUBLE(value);
                        Py_DECREF(value);
                    } else {
                        PyErr_SetString(PyExc_TypeError,
                                        "sequence items must be numbers");
                        Py_DECREF(item);
                        g_free(points);
                        points = NULL;
                        return -1;
                    }
                    Py_DECREF(item);
                }
            }
        } else if (sequence != Py_None) {
            PyErr_SetString(PyExc_TypeError, "argument must be sequence or None");
            return -1;
        }
    *out_points = points;
    return n;
}
Exemplo n.º 30
0
static PyObject *string_to_number(PyObject *string)
{
  PyObject *result = PyNumber_Float(string);
  if (result == NULL) {
    PyErr_Clear();
    Py_INCREF(PyNumber_NaN);
    result = PyNumber_NaN;
  }
  return result;
}