Example #1
0
static int
Jdsym_Proj(PyObject *proj, int n, double *x) {
  PyObject *x_arr = NULL;
  int dimensions[1];
  PyObject *res;

  dimensions[0] = n;

  /* create array objects from x and y */
  x_arr = PyArray_FromDimsAndData(1, dimensions, PyArray_DOUBLE, (char *)x);
  if (x_arr == NULL)
    goto fail;

  /* Call "project" method of "proj" object */
  res = PyObject_CallMethod(proj, "project", "O", x_arr);
  if (res != Py_None) {
    if (!PyErr_Occurred())
      PyErr_SetString(PyExc_RuntimeError, "error when calling projector");
    goto fail;
  }
  Py_DECREF(res);
  
  /* free array objects */
  Py_DECREF(x_arr);
  return 0;

 fail:
  Py_XDECREF(x_arr);
  return -1;
}
Example #2
0
static PyObject *
pixbuf_get_pixels_array(PyObject *self, PyObject *args)
{
    /* 1) read in Python pixbuf, get the underlying gdk_pixbuf */
    PyGObject *py_pixbuf;
    GdkPixbuf *gdk_pixbuf;
    PyArrayObject *array;
    int dims[3] = { 0, 0, 3 };

    if (!PyArg_ParseTuple(args, "O!:pixbuf_get_pixels_array",
			  &PyGdkPixbuf_Type, &py_pixbuf))
	return NULL;

    gdk_pixbuf = GDK_PIXBUF(py_pixbuf->obj);

    /* 2) same as pygtk/gtk/gdk.c _wrap_gdk_pixbuf_get_pixels_array()
     * with 'self' changed to py_pixbuf
     */

    dims[0] = gdk_pixbuf_get_height(gdk_pixbuf);
    dims[1] = gdk_pixbuf_get_width(gdk_pixbuf);
    if (gdk_pixbuf_get_has_alpha(gdk_pixbuf))
        dims[2] = 4;

    array = (PyArrayObject *)PyArray_FromDimsAndData(3, dims, PyArray_UBYTE,
			     (char *)gdk_pixbuf_get_pixels(gdk_pixbuf));
    if (array == NULL)
        return NULL;

    array->strides[0] = gdk_pixbuf_get_rowstride(gdk_pixbuf);
    /* the array holds a ref to the pixbuf pixels through this wrapper*/
    Py_INCREF(py_pixbuf);
    array->base = (PyObject *)py_pixbuf;
    return PyArray_Return(array);
}
static PyObject*
sndarray_samples (PyObject* self, PyObject* arg)
{
    int dim[2], numdims, type, formatbytes;
    PyObject *array, *chunkobj;
    Mix_Chunk* chunk;
    Uint16 format;
    int numchannels;

    if (!PyArg_ParseTuple (arg, "O!", &PySound_Type, &chunkobj))
        return NULL;
    chunk = PySound_AsChunk (chunkobj);

    if (!Mix_QuerySpec (NULL, &format, &numchannels))
        return RAISE (PyExc_SDLError, "Mixer not initialized");

    formatbytes = (abs (format) & 0xff) / 8;
    switch (format)
    {
    case AUDIO_S8:
        type = PyArray_CHAR;
        break;
    case AUDIO_U8:
        type = PyArray_UBYTE;
        break;
    case AUDIO_S16SYS:
        type = PyArray_SHORT;
        break;
    case AUDIO_U16SYS:
        type = PyArray_USHORT;
        break;
    default:
        return RAISE (PyExc_TypeError, "Unpresentable audio format");
    }

    numdims = (numchannels > 1) ? 2 : 1;
    dim[0] = chunk->alen / (numchannels*formatbytes);
    dim[1] = numchannels;
    
    array = PyArray_FromDimsAndData (numdims, dim, type, (char*)chunk->abuf);
    if(array)
    {
        Py_INCREF (chunkobj);
        ((PyArrayObject*) array)->base = chunkobj;
        ((PyArrayObject*) array)->flags |= SAVESPACE;
    }
    return array;
}
Example #4
0
PyObject *PP_array_unpack(void *vr, long nitems, PP_types tc)
   {PyObject *rv;

    rv = NULL;

#ifdef HAVE_PY_NUMERIC
    PP_numpy_map *entry;

    if (tc > 0 && tc < PP_NUM_TYPES)
       entry = tc_to_entry[tc];
    else
       entry = NULL;

    if (entry != NULL)
       {int ndims, dimensions[MAXDIM];

        ndims = 1;
	dimensions[0] = nitems;

	rv = PyArray_FromDimsAndData(ndims, dimensions, entry->type_num, vr);};
#endif

    return rv;}
Example #5
0
static double
PyGSL_monte_function_wrap(double *x,  size_t dim, void *params)
{
     double tmp, *dresult;
     int dimension;
     PyGSL_solver *s;
     PyArrayObject *a_array = NULL;
     PyObject *result = NULL, *arglist=NULL, *callback, *retval;
     PyGSL_error_info  info;
     /* the line number to appear in the traceback */ 
     int trb_lineno = -1, i;
     struct pygsl_array_cache * cache_ptr;
     gsl_vector_view view;

     FUNC_MESS_BEGIN();
     s = (PyGSL_solver  *) params;

     /*
     flag = PyGSL_function_wrap_On_O(&view.vector, s->cbs[0], s->args,
                                     &tmp, NULL, view.vector.size, 
                                     "monte_wrap");
     */

     FUNC_MESS_BEGIN();    
     callback = s->cbs[0];
     assert(s->args != NULL);
     assert(callback != NULL);

     /* Do I need to copy the array ??? */
     for(i = 2; i < PyGSL_SOLVER_N_ARRAYS; ++i){
	  cache_ptr = &(s->cache[i]);
	  if(cache_ptr->ref == NULL)
	       /* no array found */
	       break;
	  if(x == cache_ptr->data){
	       a_array = cache_ptr->ref;
	       break;
	  }
     }
     if(i >= PyGSL_SOLVER_N_ARRAYS){
	  trb_lineno = __LINE__ -1;
	  pygsl_error("Not enough space to cache arrays...", filename, trb_lineno, GSL_ESANITY);
	  goto fail;
     }
     if(a_array == NULL){
	  dimension = dim;
	  a_array = (PyArrayObject *) PyArray_FromDimsAndData(1, &dimension, PyArray_DOUBLE, (char *)x);
	  cache_ptr->ref = a_array;
	  cache_ptr->data = x;
	  /*
	   * Required for deallocation.      
	   * Tuples steal the references. But this array will be dereferenced by the
	   * normal procedure!
	   */
	  Py_INCREF(a_array);
     }
     if (a_array == NULL){
	  trb_lineno = __LINE__ - 2;
	  goto fail;
     }
     /* arglist was already set up */
     result = (PyObject *) s->cache[1].ref;
     dresult = (double *) s->cache[1].data;
     arglist = (PyObject *) s->cache[0].ref;

     Py_INCREF(s->args);
     PyTuple_SET_ITEM(arglist, 0, (PyObject *) a_array);
     PyTuple_SET_ITEM(arglist, 1, s->args);

     /*
     arglist = Py_BuildValue("(OOO)", a_array, s->args, result);
     if(DEBUG > 2){
	  fprintf(stderr, "callback = %p, arglist = %p\n", callback, arglist);
     }
     */
     FUNC_MESS("    Call Python Object BEGIN");
     retval = PyEval_CallObject(callback, arglist);
     FUNC_MESS("    Call Python Object END");

     /*
     info.callback = callback;
     info.message  = __FUNCTION__;
     if(PyGSL_CHECK_PYTHON_RETURN(retval, 0, &info) != GSL_SUCCESS){
	  trb_lineno = __LINE__ - 1;
	  goto fail;
     }
     info.argnum = 1;
     DEBUG_MESS(3, "result was %e", *dresult);
     */
     FUNC_MESS_END();
     return *dresult;


 fail:
     PyGSL_add_traceback(NULL, __FILE__, __FUNCTION__, trb_lineno);
     FUNC_MESS("Failure");

     if(s->isset == 1){
	  longjmp(s->buffer, GSL_EFAILED);
	  FUNC_MESS("\t\t Using jump buffer");
     } else {
	  FUNC_MESS("\t\t Jump buffer was not defined!");
     }
     tmp = gsl_nan();
     return tmp;
}
static PyObject *
RtAudio_openStream(PyRtAudio *self, PyObject *args)
{
  int outputDevice;
  int outputChannels;
  int inputDevice;
  int inputChannels;
  RtAudioFormat format;
  int sampleRate;
  int bufferSize;
  int numberOfBuffers;

  char *buffer;
  int dims[1];
  int type;

  if(!PyArg_ParseTuple(args, "iiiikiii", 
                       &outputDevice,
                       &outputChannels,
                       &inputDevice,
                       &inputChannels,
                       &format,
                       &sampleRate,
                       &bufferSize,
                       &numberOfBuffers))
    return NULL;

  try
    {
      
      self->rtaudio->openStream(outputDevice, outputChannels,
                                inputDevice, inputChannels,
                                format, sampleRate, 
                                &bufferSize, &numberOfBuffers);
    }
  catch(RtError &error)
    {
      PyErr_Format(RtAudioError, error.getMessageString());
      return NULL;
    }

  bufferSize *= outputChannels; // fix for input, duplex

  /* the stream buffer is only allocated after a stream has been opened. */
  try
    {
      buffer = self->rtaudio->getStreamBuffer();
    }
  catch(RtError &error)
    {
      PyErr_Format(RtAudioError, error.getMessageString());
      return NULL;
    }

  dims[0] = bufferSize;
  type = RtAudioFormat_2_NumarrayType(format);

  Py_XDECREF(self->_streamBuffer);
  if(self->_streamBuffer == NULL)
    {
      self->_streamBuffer = (PyArrayObject *)
        PyArray_FromDimsAndData(1, dims, type, buffer);
      if(self->_streamBuffer == NULL)
        return NULL;
    }
  
  Py_RETURN_NONE;
  
}
Example #7
0
File: _nk20.c Project: kevinarpe/kx
static PyObject*
_ktoarray(PyObject* self, PyObject* k)
{
	PyArrayObject *ret;
	if (!PyK_KCheck(k)) {
		return PyErr_Format(PyExc_TypeError, "not k object");
	}
	
	K kobj = ((PyK_K*)k)->kobj;
	if (!kobj) {
		return PyErr_Format(PyExc_AssertionError, "null kobj");
	}
	int t = kobj->t;
	/* XXX k objects of type 0 should be converted 
	 * to non-contiguous arrays rather than trigger
	 * an error.
	 */
	if (abs(t) >= LEN(types) && t != 5 ) {
		return PyErr_Format(PyExc_TypeError, 
				    "cannot create an array from a "
				    "k object of type %d", t);
	}
	int type = types[abs(t)]; /* PyArray type */
	int nd = t <= 0 || t == 5;          /* Number of dimensions (0 or 1) */
	int* d = &kobj->n;        /* Shape */
	char* data;
	switch (t) {
	case 1:
		data = (char*)&Ki(kobj);
		break;
	case 3:
		data = &Kc(kobj);
		break;
	case 4:
		data = Ks(kobj);
		break;
	default:
		data = KC(kobj);
	}
	/* Special handling for symbols arrays: convert data to Python strings */
	PyObject** buf = 0;
	if (t == -4) {
		int n = *d, i = 0;
		buf = (PyObject**)malloc(n * sizeof(PyObject*));
		for (i = 0; i < n; ++i) {
			char* s = KS(kobj)[i];
			if (!s) goto fail;
			buf[i] = PyString_FromString(s);
			if (!buf[i]) goto fail;
		}
		data = (char*)buf;
	} else if (t == 0 || t == 5) {
		int n = *d, i = 0;
		buf = (PyObject**)malloc(n * sizeof(PyObject*));
		for (i = 0; i < n; ++i) {
			K ki = KK(kobj)[i];
			if (!ki) goto fail;
			ci(ki);
			buf[i] = PyK_mk_K(ki);
			if (!buf[i]) {
				cd(ki);
				goto fail;
			}
		}
		data = (char*)buf;
	}
	if (!(ret = (PyArrayObject *)PyArray_FromDimsAndData(nd, d, type, data))) {
		goto fail;
	}
	if (buf) {
		ret->flags |= OWN_DATA;
	} else {
		Py_INCREF(k);
		ret->base = k;
	}
	return (PyObject*)ret;
 fail:
	if (buf) free(buf);
	return NULL;
}