Beispiel #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;
}
Beispiel #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;
}
Beispiel #3
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;
}
Beispiel #4
0
PYTHON_INIT_DEFINITION(ptPlayer, args, keywords)
{
    // we have two sets of arguments we can use, hence the generic PyObject* pointers
    // argument set 1: pyKey, string, uint32_t, float
    // argument set 2: string, uint32_t
    PyObject* firstObj = NULL; // can be a pyKey or a string
    PyObject* secondObj = NULL; // can be a string or a uint32_t
    PyObject* thirdObj = NULL; // uint32_t
    PyObject* fourthObj = NULL; // float
    if (!PyArg_ParseTuple(args, "OO|OO", &firstObj, &secondObj, &thirdObj, &fourthObj))
    {
        PyErr_SetString(PyExc_TypeError, "__init__ expects one of two argument lists: (ptKey, string, unsigned long, float) or (string, unsigned long)");
        PYTHON_RETURN_INIT_ERROR;
    }

    plKey key;
    plString name;
    uint32_t pid = -1;
    float distSeq = -1;

    if (pyKey::Check(firstObj))
    {
        if (!(PyString_CheckEx(secondObj) && PyNumber_Check(thirdObj) && PyFloat_Check(fourthObj)))
        {
            PyErr_SetString(PyExc_TypeError, "__init__ expects one of two argument lists: (ptKey, string, unsigned long, float) or (string, unsigned long)");
            PYTHON_RETURN_INIT_ERROR;
        }

        key = pyKey::ConvertFrom(firstObj)->getKey();
        name = PyString_AsStringEx(secondObj);
        pid = PyNumber_AsSsize_t(thirdObj, NULL);
        distSeq = (float)PyFloat_AsDouble(fourthObj);
    } else if (PyString_CheckEx(firstObj)) {
        name = PyString_AsStringEx(firstObj);
        if (!PyNumber_Check(secondObj) || thirdObj  || fourthObj)
        {
            PyErr_SetString(PyExc_TypeError, "__init__ expects one of two argument lists: (ptKey, string, unsigned long, float) or (string, unsigned long)");
            PYTHON_RETURN_INIT_ERROR;
        }

        pid = PyNumber_AsSsize_t(secondObj, NULL);
    } else {
        PyErr_SetString(PyExc_TypeError, "__init__ expects one of two argument lists: (ptKey, string, unsigned long, float) or (string, unsigned long)");
        PYTHON_RETURN_INIT_ERROR;
    }

    self->fThis->Init(key, name.c_str(), pid, distSeq);
    PYTHON_RETURN_INIT_OK;
}
Beispiel #5
0
static PyObject *
PyComm_setData (PyObject *self, PyObject *args)
{
  PyObject *current;
  int team, player;
  std::vector<float> values;

  int size = PyTuple_Size(args);
  for (int i = 0; i < size; i++) {
    // retrive i'th object
    current = PyTuple_GET_ITEM(args, i);

    // check type
    if (!PyNumber_Check(current)) {
      PyErr_SetString(PyExc_TypeError,
          "setData() expects all float or integer arguments");
      return NULL;
    }

    // add it to the list
    values.push_back(static_cast<float>(PyFloat_AsDouble(current)));
  }

  Py_BEGIN_ALLOW_THREADS

  // set comm data
  ((PyComm*)self)->comm->setData(values);

  Py_END_ALLOW_THREADS
  
  Py_INCREF(Py_None);
  return Py_None;
}
Beispiel #6
0
static PyObject *PyView_find(PyView *o, PyObject *_args, PyObject* _kwargs) {
  PWONumber start(0);
  PWOMapping crit;
  try {
    PWOSequence args(_args);
    if (_kwargs) {
      PWOMapping kwargs(_kwargs);
      if (kwargs.hasKey("start")) {
        start = kwargs["start"];
        kwargs.delItem("start");
      }
      crit = kwargs;
    }
    int numargs = args.len();
    for (int i=0; i<numargs; ++i) {
      if (PyNumber_Check((PyObject*)args[i]))
        start = args[i];
      else
        crit = args[i];
    }
    c4_Row temp;
    o->makeRow(temp, crit, false);
    return PWONumber(o->Find(temp, start)).disOwn();
  }
  catch (...) { return 0; }
}
Beispiel #7
0
static int pyimg_setitem(PyObject *self, Py_ssize_t i, PyObject *v) {
    long tmp;
    struct bug_img *img = &((pyimgObject *) self)->img;
    if(!img->start) {
        PyErr_SetString(PyExc_Exception, "No memory allocated yet.");
        return -1;
    }
    if(i >= img->length || i < 0) {
        PyErr_SetString(PyExc_Exception, "Index out of bounds.");
        return -1;
    }
    if(!PyNumber_Check(v)) {
        PyErr_SetString(PyExc_Exception, "Value must be a number");
        return -1;
    }
    PyObject *n = PyNumber_Int(v);
    if(!n) {
        PyErr_SetString(PyExc_Exception, "Value must be convertable to an integer");
        return -1;
    }
    tmp = PyInt_AS_LONG(n);
    Py_DECREF(n);
    if(tmp > 255) tmp = 255;
    if(tmp < 0) tmp = 0;
    ((unsigned char *) img->start)[i] = tmp;
    return 0;
}
Beispiel #8
0
static PyObject *
MatrixPointer_setY(MatrixPointer *self, PyObject *arg)
{
	PyObject *tmp, *streamtmp;
	
	if (arg == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}
    
	int isNumber = PyNumber_Check(arg);
	if (isNumber == 1) {
		PySys_WriteStderr("MatrixPointer y attributes must be a PyoObject.\n");
        if (PyInt_AsLong(PyObject_CallMethod(self->server, "getIsBooted", NULL))) {
            PyObject_CallMethod(self->server, "shutdown", NULL);
        }
        Py_Exit(1);
	}
	
	tmp = arg;
	Py_INCREF(tmp);
	Py_XDECREF(self->y);
    
    self->y = tmp;
    streamtmp = PyObject_CallMethod((PyObject *)self->y, "_getStream", NULL);
    Py_INCREF(streamtmp);
    Py_XDECREF(self->y_stream);
    self->y_stream = (Stream *)streamtmp;
    
	Py_INCREF(Py_None);
	return Py_None;
}	
Beispiel #9
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;
}
Beispiel #10
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;
}
Beispiel #11
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;
}	
Beispiel #12
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;
}	
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;
}
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;
}
Beispiel #15
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;
}
Beispiel #16
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;
}
Beispiel #17
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;
}	
Beispiel #18
0
unsigned char *convert_uchar_array(unsigned char *result,PyObject *input, int size) {
  int i;
  if (!PySequence_Check(input)) {
    printf("Expected a sequence\n");
    exit(EXIT_FAILURE);
  }
  int length=PySequence_Length(input);
  if (length > size) {
    printf("Size mismatch.\n");
    exit(EXIT_FAILURE);
  }
//  result = (unsigned char *) malloc(size*sizeof(unsigned char));
  if(result==NULL)
  {
    fprintf(stderr,"Unable to allocate %d bytes.\n",(size*sizeof(unsigned char)));
    return result;
  }
  for (i = 0; i < length; i++) {
    PyObject *o = PySequence_GetItem(input,i);
    if (PyNumber_Check(o)) {
      if(PyFloat_AsDouble(o)>255)
        result[i] = (unsigned char) 255;
      else
        result[i] = (unsigned char) PyFloat_AsDouble(o);
    } else {
      PyErr_SetString(PyExc_ValueError,"uchar: Sequence elements must be numbers");
      free(result);       
      return NULL;
    }
    free(o);
  }
  return result;
}
Beispiel #19
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;
}	
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");
}
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;
}
Beispiel #22
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())));
  }
}
Beispiel #23
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;
}
Beispiel #24
0
	static int
		buffer_initobj(PyObject *self, PyObject *args, PyObject *kwds)
	{
		PyIOCPBufferObject *s = (PyIOCPBufferObject *)self;
		PyObject* arg1, *arg2;
		int r = PyArg_ParseTuple( args, "O|O", &arg1, &arg2 );
		if( r == 0) return -1;
		char *init_buffer = 0;
		int init_buffer_len = 0;
		if( PyNumber_Check( arg1 ) )
		{
			int len = PyInt_AsLong( arg1 );
			s->ptr_start = (char *) PyMem_Malloc( len );
			s->len = len;
		
		}

		else if( PyString_Check( arg1 ) )
		{
			PyString_AsStringAndSize( arg1, &init_buffer, &init_buffer_len );
			if( PyNumber_Check( arg2 ) )
			{
				s->ptr_start = (char *) PyMem_Malloc( PyInt_AsLong( arg2 ) );
				s->len = PyInt_AsLong( arg2 );
				
			}
			else
			{
				s->ptr_start = (char *) PyMem_Malloc( init_buffer_len );
				s->len = init_buffer_len;
			}
			if( init_buffer )
				if( 0!= memcpy_s( s->ptr_start, s->len, init_buffer, init_buffer_len ) )
					return -1;

		}
		else
		{
			return -1;
		}

		return 0;

	}
Beispiel #25
0
static int boxPack_FromPyObject(PyObject * value, boxPack **boxarray )
{
	int len, i;
	PyObject *list_item, *item_1, *item_2;
	boxPack *box;
	
	
	/* Error checking must alredy be done */
	if( !PyList_Check( value ) ) {
		PyErr_SetString( PyExc_TypeError, "can only back a list of [x,y,x,w]" );
		return -1;
	}
	
	len = PyList_Size( value );
	
	(*boxarray) = MEM_mallocN( len*sizeof(boxPack), "boxPack box");
	
	
	for( i = 0; i < len; i++ ) {
		list_item = PyList_GET_ITEM( value, i );
		if( !PyList_Check( list_item ) || PyList_Size( list_item ) < 4 ) {
			MEM_freeN(*boxarray);
			PyErr_SetString( PyExc_TypeError, "can only back a list of [x,y,x,w]" );
			return -1;
		}
		
		box = (*boxarray)+i;
		
		item_1 = PyList_GET_ITEM(list_item, 2);
		item_2 = PyList_GET_ITEM(list_item, 3);
		
		if (!PyNumber_Check(item_1) || !PyNumber_Check(item_2)) {
			MEM_freeN(*boxarray);
			PyErr_SetString( PyExc_TypeError, "can only back a list of 2d boxes [x,y,x,w]" );
			return -1;
		}
		
		box->w =  (float)PyFloat_AsDouble( item_1 );
		box->h =  (float)PyFloat_AsDouble( item_2 );
		box->index = i;
		/* verts will be added later */
	}
	return 0;
}
Beispiel #26
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;
}
Beispiel #27
0
GeoPoint Python::ReadLonLat(PyObject *py_location) {
  if (!PyDict_Check(py_location)) {
    PyErr_SetString(PyExc_TypeError, "Location is not a dictionary.");
    return GeoPoint::Invalid();
  }

  PyObject *py_latitude = PyDict_GetItemString(py_location, "latitude"),
           *py_longitude = PyDict_GetItemString(py_location, "longitude");

  if (!PyNumber_Check(py_latitude) || !PyNumber_Check(py_longitude)) {
    PyErr_SetString(PyExc_TypeError, "Failed to parse location.");
    return GeoPoint::Invalid();
  }

  GeoPoint location(Angle::Degrees(PyFloat_AsDouble(py_longitude)),
                    Angle::Degrees(PyFloat_AsDouble(py_latitude)));

  return location;
}
static int py_ue_frotator_set_roll(ue_PyFRotator *self, PyObject *value, void *closure) {
	if (value && PyNumber_Check(value)) {
		PyObject *f_value = PyNumber_Float(value);
		self->rot.Roll = PyFloat_AsDouble(f_value);
		Py_DECREF(f_value);
		return 0;
	}
	PyErr_SetString(PyExc_TypeError, "value is not numeric");
	return -1;
}
Beispiel #29
0
//--------------------
//--------------------
//--------------------
	PNumber::PNumber(PyObject* obj, bool stealReferance) {
		if (!obj) { this->myObject = NULL; return; }
		if (PyNumber_Check(obj) == 0) {
			throw new CPPPythonException("PNumber objects must have valid numbers to wrap");
		}
		if (!stealReferance) {
			Py_INCREF(obj);
		}
		this->myObject = obj;
	}
Beispiel #30
0
/*
** Maps Python specific types to common types between Lua and Python
*/
static VALUE_TYPES pyGetType(PyObject *value) {
   ASSERT(value, "pyGetType: null value!");
   if (value == Py_None)        return Nothing;
   if (PyNumber_Check(value))   return Number;
   if (PyString_Check(value))   return String;
   if (PyCallable_Check(value)) return Function;
   if (PyCObject_Check(value))  return Pointer;
   if (PyTuple_Check(value))    return Container;
   return Undefined;
}