/* one-dimensional spline filter: */
int NI_SplineFilter1D(PyArrayObject *input, int order, int axis,
                      NI_ExtendMode mode, PyArrayObject *output)
{
    int npoles = 0, more;
    npy_intp kk, lines, len;
    double *buffer = NULL, poles[MAX_SPLINE_FILTER_POLES];
    NI_LineBuffer iline_buffer, oline_buffer;
    NPY_BEGIN_THREADS_DEF;

    len = PyArray_NDIM(input) > 0 ? PyArray_DIM(input, axis) : 1;
    if (len < 1)
        goto exit;

    /* these are used in the spline filter calculation below: */
    if (get_filter_poles(order, &npoles, poles)) {
        goto exit;
    }

    /* allocate an initialize the line buffer, only a single one is used,
         because the calculation is in-place: */
    lines = -1;
    if (!NI_AllocateLineBuffer(input, axis, 0, 0, &lines, BUFFER_SIZE,
                               &buffer)) {
        goto exit;
    }
    if (!NI_InitLineBuffer(input, axis, 0, 0, lines, buffer,
                           NI_EXTEND_DEFAULT, 0.0, &iline_buffer)) {
        goto exit;
    }
    if (!NI_InitLineBuffer(output, axis, 0, 0, lines, buffer,
                           NI_EXTEND_DEFAULT, 0.0, &oline_buffer)) {
        goto exit;
    }
    NPY_BEGIN_THREADS;

    /* iterate over all the array lines: */
    do {
        /* copy lines from array to buffer: */
        if (!NI_ArrayToLineBuffer(&iline_buffer, &lines, &more)) {
            goto exit;
        }
        /* iterate over the lines in the buffer: */
        for(kk = 0; kk < lines; kk++) {
            /* get line: */
            double *ln = NI_GET_LINE(iline_buffer, kk);
            /* spline filter: */
            if (len > 1) {
                apply_filter(ln, len, poles, npoles, mode);
            }
        }

        /* copy lines from buffer to array: */
        if (!NI_LineBufferToArray(&oline_buffer)) {
            goto exit;
        }
    } while(more);

 exit:
    NPY_END_THREADS;
    free(buffer);
    return PyErr_Occurred() ? 0 : 1;
}
Exemple #2
0
/* function subhandler */
Datum
PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedure *proc)
{
	Datum		rv;
	PyObject   *volatile plargs = NULL;
	PyObject   *volatile plrv = NULL;
	ErrorContextCallback plerrcontext;

	PG_TRY();
	{
		if (!proc->is_setof || proc->setof == NULL)
		{
			/*
			 * Simple type returning function or first time for SETOF
			 * function: actually execute the function.
			 */
			plargs = PLy_function_build_args(fcinfo, proc);
			plrv = PLy_procedure_call(proc, "args", plargs);
			if (!proc->is_setof)
			{
				/*
				 * SETOF function parameters will be deleted when last row is
				 * returned
				 */
				PLy_function_delete_args(proc);
			}
			Assert(plrv != NULL);
		}

		/*
		 * If it returns a set, call the iterator to get the next return item.
		 * We stay in the SPI context while doing this, because PyIter_Next()
		 * calls back into Python code which might contain SPI calls.
		 */
		if (proc->is_setof)
		{
			bool		has_error = false;
			ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;

			if (proc->setof == NULL)
			{
				/* first time -- do checks and setup */
				if (!rsi || !IsA(rsi, ReturnSetInfo) ||
					(rsi->allowedModes & SFRM_ValuePerCall) == 0)
				{
					ereport(ERROR,
							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
							 errmsg("unsupported set function return mode"),
							 errdetail("PL/Python set-returning functions only support returning only value per call.")));
				}
				rsi->returnMode = SFRM_ValuePerCall;

				/* Make iterator out of returned object */
				proc->setof = PyObject_GetIter(plrv);
				Py_DECREF(plrv);
				plrv = NULL;

				if (proc->setof == NULL)
					ereport(ERROR,
							(errcode(ERRCODE_DATATYPE_MISMATCH),
							 errmsg("returned object cannot be iterated"),
							 errdetail("PL/Python set-returning functions must return an iterable object.")));
			}

			/* Fetch next from iterator */
			plrv = PyIter_Next(proc->setof);
			if (plrv)
				rsi->isDone = ExprMultipleResult;
			else
			{
				rsi->isDone = ExprEndResult;
				has_error = PyErr_Occurred() != NULL;
			}

			if (rsi->isDone == ExprEndResult)
			{
				/* Iterator is exhausted or error happened */
				Py_DECREF(proc->setof);
				proc->setof = NULL;

				Py_XDECREF(plargs);
				Py_XDECREF(plrv);

				PLy_function_delete_args(proc);

				if (has_error)
					PLy_elog(ERROR, "error fetching next item from iterator");

				/* Disconnect from the SPI manager before returning */
				if (SPI_finish() != SPI_OK_FINISH)
					elog(ERROR, "SPI_finish failed");

				fcinfo->isnull = true;
				return (Datum) NULL;
			}
		}

		/*
		 * Disconnect from SPI manager and then create the return values datum
		 * (if the input function does a palloc for it this must not be
		 * allocated in the SPI memory context because SPI_finish would free
		 * it).
		 */
		if (SPI_finish() != SPI_OK_FINISH)
			elog(ERROR, "SPI_finish failed");

		plerrcontext.callback = plpython_return_error_callback;
		plerrcontext.previous = error_context_stack;
		error_context_stack = &plerrcontext;

		/*
		 * If the function is declared to return void, the Python return value
		 * must be None. For void-returning functions, we also treat a None
		 * return value as a special "void datum" rather than NULL (as is the
		 * case for non-void-returning functions).
		 */
		if (proc->result.out.d.typoid == VOIDOID)
		{
			if (plrv != Py_None)
				ereport(ERROR,
						(errcode(ERRCODE_DATATYPE_MISMATCH),
						 errmsg("PL/Python function with return type \"void\" did not return None")));

			fcinfo->isnull = false;
			rv = (Datum) 0;
		}
		else if (plrv == Py_None)
		{
			fcinfo->isnull = true;
			if (proc->result.is_rowtype < 1)
				rv = InputFunctionCall(&proc->result.out.d.typfunc,
									   NULL,
									   proc->result.out.d.typioparam,
									   -1);
			else
				/* Tuple as None */
				rv = (Datum) NULL;
		}
		else if (proc->result.is_rowtype >= 1)
		{
			TupleDesc	desc;

			/* make sure it's not an unnamed record */
			Assert((proc->result.out.d.typoid == RECORDOID &&
					proc->result.out.d.typmod != -1) ||
				   (proc->result.out.d.typoid != RECORDOID &&
					proc->result.out.d.typmod == -1));

			desc = lookup_rowtype_tupdesc(proc->result.out.d.typoid,
										  proc->result.out.d.typmod);

			rv = PLyObject_ToCompositeDatum(&proc->result, desc, plrv);
			fcinfo->isnull = (rv == (Datum) NULL);

			ReleaseTupleDesc(desc);
		}
		else
		{
			fcinfo->isnull = false;
			rv = (proc->result.out.d.func) (&proc->result.out.d, -1, plrv);
		}
	}
	PG_CATCH();
	{
		Py_XDECREF(plargs);
		Py_XDECREF(plrv);

		/*
		 * If there was an error the iterator might have not been exhausted
		 * yet. Set it to NULL so the next invocation of the function will
		 * start the iteration again.
		 */
		Py_XDECREF(proc->setof);
		proc->setof = NULL;

		PG_RE_THROW();
	}
	PG_END_TRY();

	error_context_stack = plerrcontext.previous;

	Py_XDECREF(plargs);
	Py_DECREF(plrv);

	return rv;
}
PyObject *Client_gets(PyClient *self, PyObject *args)
{
  //[ ] def gets(self, key, default = None):

  char *pKey;
  size_t cbKey;
  char *pData;
  size_t cbData;
  UINT64 cas;
  int flags;

  if (!PyArg_ParseTuple (args, "s#", &pKey, &cbKey))
  {
    return NULL;
  }

  self->client->getsBegin();

  self->client->getKeyWrite(pKey, cbKey);
  self->client->getFlush();

  bool bError = false;

  if (!self->client->getReadNext(&pKey, &cbKey, &pData, &cbData, &flags, &cas, &bError))
  {
    if (bError)
    {
      if (!PyErr_Occurred())
      {
        return PyErr_Format(umemcache_MemcachedError, "umemcache: %s", self->client->getError());
      }
      return NULL;
    }

    Py_RETURN_NONE;
  }



  PyObject *otuple = PyTuple_New(3);
  PyObject *ovalue = PyString_FromStringAndSize(pData, cbData);
  PyObject *oflags = PyInt_FromLong(flags);
  PyObject *ocas = PyLong_FromUnsignedLongLong(cas);

  PyTuple_SET_ITEM(otuple, 0, ovalue);
  PyTuple_SET_ITEM(otuple, 1, oflags);
  PyTuple_SET_ITEM(otuple, 2, ocas);

  while (self->client->getReadNext(&pKey, &cbKey, &pData, &cbData, &flags, &cas, &bError));

  if (bError)
  {
    Py_DECREF(otuple);

    if (!PyErr_Occurred())
    {
      return PyErr_Format(umemcache_MemcachedError, "umemcache: %s", self->client->getError());
    }

    return NULL;
  }

  return otuple;
}
PyObject *Client_gets_multi(PyClient *self, PyObject *okeys)
{
  //[ ] def gets_multi(self, keys):

  char *pKey;
  size_t cbKey;
  char *pData;
  size_t cbData;
  UINT64 cas;
  int flags;

  self->client->getsBegin();

  PyObject *iterator = PyObject_GetIter(okeys);

  if (iterator == NULL)
  {
    return NULL;
  }

  PyObject *arg;

  while ( (arg = PyIter_Next(iterator)))
  {
    PyObject *ostr;

    if (PyString_Check(arg))
    {
      ostr = arg;
    }
    else
    {
      ostr = PyObject_Str(arg);
    }

    self->client->getKeyWrite(PyString_AS_STRING(ostr), PyString_GET_SIZE(ostr));
    if (ostr != arg)
    {
      Py_DECREF(ostr);
    }

    Py_DECREF(arg);
  }

  Py_DECREF(iterator);
  self->client->getFlush();

  PyObject *odict = PyDict_New();

  bool bError = false;

  while (self->client->getReadNext(&pKey, &cbKey, &pData, &cbData, &flags, &cas, &bError))
  {
    PyObject *okey  = PyString_FromStringAndSize(pKey, cbKey);
    PyObject *otuple = PyTuple_New(3);
    PyObject *ovalue = PyString_FromStringAndSize(pData, cbData);
    PyObject *oflags = PyInt_FromLong(flags);
    PyObject *ocas = PyLong_FromUnsignedLongLong(cas);

    PyTuple_SET_ITEM(otuple, 0, ovalue);
    PyTuple_SET_ITEM(otuple, 1, oflags);
    PyTuple_SET_ITEM(otuple, 2, ocas);
    PyDict_SetItem (odict, okey, otuple);

    Py_DECREF(otuple);
    Py_DECREF(okey);
  }

  if (bError)
  {
    Py_DECREF(odict);

    if (!PyErr_Occurred())
    {
      return PyErr_Format(umemcache_MemcachedError, "umemcache: %s", self->client->getError());
    }

    return NULL;
  }

  return odict;
}
static PyObject *make_ffmpeg_info(void)
{
	PyObject *ffmpeg_info;
	int pos = 0;

#ifdef WITH_FFMPEG
	int curversion;
#endif

	ffmpeg_info = PyStructSequence_New(&BlenderAppFFmpegType);
	if (ffmpeg_info == NULL) {
		return NULL;
	}

#if 0 // UNUSED
#define SetIntItem(flag) \
	PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyLong_FromLong(flag))
#endif
#define SetStrItem(str) \
	PyStructSequence_SET_ITEM(ffmpeg_info, pos++, PyUnicode_FromString(str))
#define SetObjItem(obj) \
	PyStructSequence_SET_ITEM(ffmpeg_info, pos++, obj)

#ifdef WITH_FFMPEG
#  define FFMPEG_LIB_VERSION(lib)  { \
		curversion = lib ## _version(); \
		SetObjItem(Py_BuildValue("(iii)", \
		                         curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
		SetObjItem(PyUnicode_FromFormat("%2d, %2d, %2d", \
		                                curversion >> 16, (curversion >> 8) % 256, curversion % 256)); \
} (void)0
#else
#  define FFMPEG_LIB_VERSION(lib)  { \
		SetStrItem("Unknown"); \
		SetStrItem("Unknown"); \
} (void)0
#endif

#ifdef WITH_FFMPEG
	SetObjItem(PyBool_FromLong(1));
#else
	SetObjItem(PyBool_FromLong(0));
#endif

	FFMPEG_LIB_VERSION(avcodec);
	FFMPEG_LIB_VERSION(avdevice);
	FFMPEG_LIB_VERSION(avformat);
	FFMPEG_LIB_VERSION(avutil);
	FFMPEG_LIB_VERSION(swscale);

#undef FFMPEG_LIB_VERSION

	if (PyErr_Occurred()) {
		Py_CLEAR(ffmpeg_info);
		return NULL;
	}

// #undef SetIntItem
#undef SetStrItem
#undef SetObjItem

	return ffmpeg_info;
}