Exemplo n.º 1
0
static PyObject *
tasklet_reduce(PyTaskletObject * t)
{
    PyObject *tup = NULL, *lis = NULL;
    PyFrameObject *f;
    PyThreadState *ts = PyThreadState_GET();

    if (t == ts->st.current)
        RUNTIME_ERROR("You cannot __reduce__ the tasklet which is"
                      " current.", NULL);
    lis = PyList_New(0);
    if (lis == NULL) goto err_exit;
    f = t->f.frame;
    while (f != NULL) {
        if (PyList_Append(lis, (PyObject *) f)) goto err_exit;
        f = f->f_back;
    }
    if (PyList_Reverse(lis)) goto err_exit;
    assert(t->cstate != NULL);
    tup = Py_BuildValue("(O()(" TASKLET_TUPLEFMT "))",
                        t->ob_type,
                        t->flags,
                        t->tempval,
                        t->cstate->nesting_level,
                        lis
                        );
err_exit:
    Py_XDECREF(lis);
    return tup;
}
Exemplo n.º 2
0
void list_base::reverse()
{
    if (PyList_CheckExact(this->ptr()))
    {
        if (PyList_Reverse(this->ptr()) == -1)
            throw_error_already_set();
    }
    else
    {
        this->attr("reverse")();
    }
}
Exemplo n.º 3
0
static PyObject *
slpmodule_getthreads(PySlpModuleObject *mod, void *context)
{
	PyObject *lis = PyList_New(0);
	PyThreadState *ts = PyThreadState_GET();
	PyInterpreterState *interp = ts->interp;

	if (lis == NULL)
		return NULL;

	for (ts = interp->tstate_head; ts != NULL; ts = ts->next) {
		PyObject *id = PyInt_FromLong(ts->thread_id);

		if (id == NULL || PyList_Append(lis, id))
			return NULL;
	}
	PyList_Reverse(lis);
	return lis;
}
Exemplo n.º 4
0
static PyObject*
test_list_api(PyObject *self)
{
	PyObject* list;
	int i;

	/* SF bug 132008:  PyList_Reverse segfaults */
#define NLIST 30
	list = PyList_New(NLIST);
	if (list == (PyObject*)NULL)
		return (PyObject*)NULL;
	/* list = range(NLIST) */
	for (i = 0; i < NLIST; ++i) {
		PyObject* anint = PyInt_FromLong(i);
		if (anint == (PyObject*)NULL) {
			Py_DECREF(list);
			return (PyObject*)NULL;
		}
		PyList_SET_ITEM(list, i, anint);
	}
	/* list.reverse(), via PyList_Reverse() */
	i = PyList_Reverse(list);   /* should not blow up! */
	if (i != 0) {
		Py_DECREF(list);
		return (PyObject*)NULL;
	}
	/* Check that list == range(29, -1, -1) now */
	for (i = 0; i < NLIST; ++i) {
		PyObject* anint = PyList_GET_ITEM(list, i);
		if (PyInt_AS_LONG(anint) != NLIST-1-i) {
			PyErr_SetString(TestError,
			                "test_list_api: reverse screwed up");
			Py_DECREF(list);
			return (PyObject*)NULL;
		}
	}
	Py_DECREF(list);
#undef NLIST

	Py_INCREF(Py_None);
	return Py_None;
}
Exemplo n.º 5
0
static PyObject *
frame_stack(PyObject *self, PyObject *args)
{
    // frame_stack(frame, top_frames, top_codes, upper_frames, upper_codes)
    // returns a list of frames.
    PyFrameObject* frame;
    const PySetObject* top_frames;
    const PySetObject* top_codes;
    const PySetObject* upper_frames;
    const PySetObject* upper_codes;
    if (!PyArg_ParseTuple(args, "OOOOO", &frame, &top_frames, &top_codes,
                          &upper_frames, &upper_codes))
    {
        return NULL;
    }
    PyObject* frame_stack = PyList_New(0);
    if (frame_stack == NULL)
    {
        return NULL;
    }
    while (frame != NULL)
    {
        if (PySet_Contains(upper_frames, frame) == 1 ||
            PySet_Contains(upper_codes, frame->f_code) == 1)
        {
            break;
        }
        if (PyList_Append(frame_stack, (PyObject*)frame))
        {
            return NULL;
        }
        if (PySet_Contains(top_frames, frame) == 1 ||
            PySet_Contains(top_codes, frame->f_code) == 1)
        {
            break;
        }
        frame = frame->f_back;
    }
    PyList_Reverse(frame_stack);
    return frame_stack;
}
Exemplo n.º 6
0
 void List::reverse()
 {
     PyList_Reverse(mPtr);
 }
Exemplo n.º 7
0
static int PDFfile_init(PDFfile *self, PyObject * /*args*/, PyObject * /*kwds*/)
{
	int i;
	if (!ScCore->primaryMainWindow()->HaveDoc) {
		PyErr_SetString(PyExc_SystemError, "Must open doc first");
		return -1;
	}
// defaut save into file
	QString tf = ScCore->primaryMainWindow()->doc->PDF_Options.fileName;
	if (tf.isEmpty()) {
		QFileInfo fi = QFileInfo(ScCore->primaryMainWindow()->doc->DocName);
		tf = fi.path()+"/"+fi.baseName()+".pdf";
	}
	PyObject *file = NULL;
	file = PyString_FromString(tf.toAscii());
	if (file){
		Py_DECREF(self->file);
		self->file = file;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'file' attribute");
		return -1;
	}
// embed all used fonts
	PyObject *fonts = NULL;
	fonts = PyList_New(0);
	if (fonts){
		Py_DECREF(self->fonts);
		self->fonts = fonts;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'fonts' attribute");
		return -1;
	}
	// get all used fonts
	QMap<QString,int> ReallyUsed = ScCore->primaryMainWindow()->doc->UsedFonts;
	// create list of all used fonts
	QList<QString> tmpEm;
	tmpEm = ReallyUsed.keys();
	QList<QString>::Iterator itef;
	for (itef = tmpEm.begin(); itef != tmpEm.end(); ++itef) {
// AV: dunno what this is for, but it looks as if it's the only place where HasMetrics is used...
//		if (PrefsManager::instance()->appPrefs.AvailFonts[(*itef).toAscii()]->HasMetrics) {
			PyObject *tmp= NULL;
			tmp = PyString_FromString((*itef).toAscii());
			if (tmp) {
				PyList_Append(self->fonts, tmp);
// do i need Py_DECREF(tmp) here?
// Does PyList_Append increase reference or 'steal' one from provided argument
// If it 'steal' reference comment next line
				Py_DECREF(tmp);
			}
			else {
				PyErr_SetString(PyExc_SystemError, "Can not initialize 'fonts' attribute");
				return -1;
			}
//		}
	}
// set to print all pages
	PyObject *pages = NULL;
	int num = 0;
	// which one should I use ???
	// new = ScCore->primaryMainWindow()->view->Pages.count()
	num = ScCore->primaryMainWindow()->doc->Pages->count();
	pages = PyList_New(num);
	if (!pages){
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'pages' attribute");
		return -1;
	}
	for (i = 0; i<num; ++i) {
		PyObject *tmp;
		tmp = PyInt_FromLong((long)i+1L);
		if (tmp)
			PyList_SetItem(pages, i, tmp);
		else {
			PyErr_SetString(PyExc_SystemError, "Can not initialize 'pages' attribute");
			return -1;
		}
	}
	Py_DECREF(self->pages);
	self->pages = pages;
// do not print thumbnails
	self->thumbnails = ScCore->primaryMainWindow()->doc->PDF_Options.Thumbnails;
// set automatic compression
	self->compress = ScCore->primaryMainWindow()->doc->PDF_Options.Compress;
	self->compressmtd = ScCore->primaryMainWindow()->doc->PDF_Options.CompressMethod;
// use maximum image quality
	self->quality = ScCore->primaryMainWindow()->doc->PDF_Options.Quality;
// default resolution
	PyObject *resolution = NULL;
	resolution = PyInt_FromLong(300);
	if (resolution){
		Py_DECREF(self->resolution);
		self->resolution = resolution;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'resolutin' attribute");
		return -1;
	}
// do not downsample images
	int down = ScCore->primaryMainWindow()->doc->PDF_Options.RecalcPic ? ScCore->primaryMainWindow()->doc->PDF_Options.PicRes : 0;
	PyObject *downsample = NULL;
	downsample = PyInt_FromLong(down);
	if (downsample){
		Py_DECREF(self->downsample);
		self->downsample = downsample;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'downsamle' attribute");
		return -1;
	}
	// no bookmarks
	self->bookmarks = ScCore->primaryMainWindow()->doc->PDF_Options.Bookmarks;
	// left margin binding
	self->binding = ScCore->primaryMainWindow()->doc->PDF_Options.Binding;
	// do not enable presentation effects
	self->presentation = ScCore->primaryMainWindow()->doc->PDF_Options.PresentMode;
	// set effects values for all pages
	PyObject *effval = NULL;
	num = 0;
	// which one should I use ???
	// new = ScCore->primaryMainWindow()->view->Pages.count();
	num = ScCore->primaryMainWindow()->doc->Pages->count();
	effval = PyList_New(num);
	if (!effval){
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'effval' attribute");
		return -1;
	}
	int num2 = ScCore->primaryMainWindow()->doc->PDF_Options.PresentVals.count();
	for (i = 0; i<num2; ++i) {
		PyObject *tmp;
		PDFPresentationData t = ScCore->primaryMainWindow()->doc->PDF_Options.PresentVals[i];
		tmp = Py_BuildValue(const_cast<char*>("[iiiiii]"), t.pageEffectDuration, t.pageViewDuration, t.effectType, t.Dm, t.M, t.Di );
		if (tmp)
			PyList_SetItem(effval, i, tmp);
		else {
			PyErr_SetString(PyExc_SystemError, "Can not initialize 'effval' attribute");
			return -1;
		}
		for (; i<num; ++i) {
			PyObject *tmp;
			tmp = Py_BuildValue(const_cast<char*>("[iiiiii]"), 1, 1, 0, 0, 0, 0);
			if (tmp)
				PyList_SetItem(effval, i, tmp);
			else {
				PyErr_SetString(PyExc_SystemError, "Can not initialize 'effval' attribute");
				return -1;
			}
		}
	}
	Py_DECREF(self->effval);
	self->effval = effval;
// do not save linked text frames as PDF article
	self->article = ScCore->primaryMainWindow()->doc->PDF_Options.Articles;
// do not encrypt file
	self->encrypt = ScCore->primaryMainWindow()->doc->PDF_Options.Encrypt;
// do not Use Custom Rendering Settings
	self->uselpi = ScCore->primaryMainWindow()->doc->PDF_Options.UseLPI;
	self->usespot = ScCore->primaryMainWindow()->doc->PDF_Options.UseSpotColors;
	self->domulti = ScCore->primaryMainWindow()->doc->PDF_Options.doMultiFile;
// get default values for lpival
	int n = ScCore->primaryMainWindow()->doc->PDF_Options.LPISettings.size();
	PyObject *lpival=PyList_New(n);
	if (!lpival){
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'lpival' attribute");
		return -1;
	}
	QMap<QString,LPIData>::Iterator it = ScCore->primaryMainWindow()->doc->PDF_Options.LPISettings.begin();
	while (it != ScCore->primaryMainWindow()->doc->PDF_Options.LPISettings.end()) {
		PyObject *tmp;
		tmp = Py_BuildValue(const_cast<char*>("[siii]"), it.key().toAscii().constData(), it.value().Frequency, it.value().Angle, it.value().SpotFunc);
		if (!tmp) {
			PyErr_SetString(PyExc_SystemError, "Can not initialize 'lpival' attribute");
			return -1;
		}
		PyList_SetItem(lpival, --n, tmp);
		++it;
	}
	PyList_Reverse(lpival);
	Py_DECREF(self->lpival);
	self->lpival = lpival;
// set owner's password
	PyObject *owner = NULL;
	owner = PyString_FromString(ScCore->primaryMainWindow()->doc->PDF_Options.PassOwner.toAscii());
	if (owner){
		Py_DECREF(self->owner);
		self->owner = owner;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'owner' attribute");
		return -1;
	}
// set user'a password
	PyObject *user = NULL;
	user = PyString_FromString(ScCore->primaryMainWindow()->doc->PDF_Options.PassUser.toAscii());
	if (user){
		Py_DECREF(self->user);
		self->user = user;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'user' attribute");
		return -1;
	}
// allow printing document
	self->aprint = ScCore->primaryMainWindow()->doc->PDF_Options.Permissions & 4;
// allow changing document
	self->achange = ScCore->primaryMainWindow()->doc->PDF_Options.Permissions & 8;
// allow copying document
	self->acopy = ScCore->primaryMainWindow()->doc->PDF_Options.Permissions & 16;
// allow adding annotation and fields
	self->aanot = ScCore->primaryMainWindow()->doc->PDF_Options.Permissions & 32;
// use 1.4 pdf version *aka. Acrobat 5)
	self->version = ScCore->primaryMainWindow()->doc->PDF_Options.Version;
// output destination is screen
	self->outdst = ScCore->primaryMainWindow()->doc->PDF_Options.UseRGB ? 0 : 1;

	self->profiles = ScCore->primaryMainWindow()->doc->PDF_Options.UseProfiles; // bool
	self->profilei = ScCore->primaryMainWindow()->doc->PDF_Options.UseProfiles2; // bool
	self->noembicc = ScCore->primaryMainWindow()->doc->PDF_Options.EmbeddedI; // bool
	self->intents = ScCore->primaryMainWindow()->doc->PDF_Options.Intent; // int - 0 - 3
	self->intenti = ScCore->primaryMainWindow()->doc->PDF_Options.Intent2; // int - 0 - 3
	QString tp = ScCore->primaryMainWindow()->doc->PDF_Options.SolidProf;
	if (!ScCore->InputProfiles.contains(tp))
		tp = ScCore->primaryMainWindow()->view->Doc->CMSSettings.DefaultSolidColorRGBProfile;
	PyObject *solidpr = NULL;
	solidpr = PyString_FromString(tp.toAscii());
	if (solidpr){
		Py_DECREF(self->solidpr);
		self->solidpr = solidpr;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'solidpr' attribute");
		return -1;
	}
	QString tp2 = ScCore->primaryMainWindow()->doc->PDF_Options.ImageProf;
	if (!ScCore->InputProfiles.contains(tp2))
		tp2 = ScCore->primaryMainWindow()->view->Doc->CMSSettings.DefaultSolidColorRGBProfile;
	PyObject *imagepr = NULL;
	imagepr = PyString_FromString(tp2.toAscii());
	if (imagepr){
		Py_DECREF(self->imagepr);
		self->imagepr = imagepr;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'imagepr' attribute");
		return -1;
	}
	QString tp3 = ScCore->primaryMainWindow()->doc->PDF_Options.PrintProf;
	if (!ScCore->PDFXProfiles.contains(tp3))
		tp3 = ScCore->primaryMainWindow()->view->Doc->CMSSettings.DefaultPrinterProfile;
	PyObject *printprofc = NULL;
	printprofc = PyString_FromString(tp3.toAscii());
	if (printprofc){
		Py_DECREF(self->printprofc);
		self->printprofc = printprofc;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'printprofc' attribute");
		return -1;
	}
	QString tinfo = ScCore->primaryMainWindow()->doc->PDF_Options.Info;
	PyObject *info = NULL;
	info = PyString_FromString(tinfo.toAscii());
	if (info){
		Py_DECREF(self->info);
		self->info = info;
	} else {
		PyErr_SetString(PyExc_SystemError, "Can not initialize 'info' attribute");
		return -1;
	}
	self->bleedt = ScCore->primaryMainWindow()->doc->PDF_Options.bleeds.Top*ScCore->primaryMainWindow()->doc->unitRatio(); // double -
	self->bleedl = ScCore->primaryMainWindow()->doc->PDF_Options.bleeds.Left*ScCore->primaryMainWindow()->doc->unitRatio(); // double -
	self->bleedr = ScCore->primaryMainWindow()->doc->PDF_Options.bleeds.Right*ScCore->primaryMainWindow()->doc->unitRatio(); // double -
	self->bleedb = ScCore->primaryMainWindow()->doc->PDF_Options.bleeds.Bottom*ScCore->primaryMainWindow()->doc->unitRatio(); // double -

	return 0;
}
Exemplo n.º 8
0
static PyObject *
nlargest(PyObject *self, PyObject *args)
{
    PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
    Py_ssize_t i, n;
    int cmp;

    if (!PyArg_ParseTuple(args, "nO:nlargest", &n, &iterable))
        return NULL;

    it = PyObject_GetIter(iterable);
    if (it == NULL)
        return NULL;

    heap = PyList_New(0);
    if (heap == NULL)
        goto fail;

    for (i=0 ; i<n ; i++ ){
        elem = PyIter_Next(it);
        if (elem == NULL) {
            if (PyErr_Occurred())
                goto fail;
            else
                goto sortit;
        }
        if (PyList_Append(heap, elem) == -1) {
            Py_DECREF(elem);
            goto fail;
        }
        Py_DECREF(elem);
    }
    if (PyList_GET_SIZE(heap) == 0)
        goto sortit;

    for (i=n/2-1 ; i>=0 ; i--)
        if(_siftup((PyListObject *)heap, i) == -1)
            goto fail;

    sol = PyList_GET_ITEM(heap, 0);
    while (1) {
        elem = PyIter_Next(it);
        if (elem == NULL) {
            if (PyErr_Occurred())
                goto fail;
            else
                goto sortit;
        }
        cmp = PyObject_RichCompareBool(sol, elem, Py_LT);
        if (cmp == -1) {
            Py_DECREF(elem);
            goto fail;
        }
        if (cmp == 0) {
            Py_DECREF(elem);
            continue;
        }
        oldelem = PyList_GET_ITEM(heap, 0);
        PyList_SET_ITEM(heap, 0, elem);
        Py_DECREF(oldelem);
        if (_siftup((PyListObject *)heap, 0) == -1)
            goto fail;
        sol = PyList_GET_ITEM(heap, 0);
    }
sortit:
    if (PyList_Sort(heap) == -1)
        goto fail;
    if (PyList_Reverse(heap) == -1)
        goto fail;
    Py_DECREF(it);
    return heap;

fail:
    Py_DECREF(it);
    Py_XDECREF(heap);
    return NULL;
}
Exemplo n.º 9
0
PyObject* netsnmptable_parse_mib(PyObject *self, PyObject *args) {
    PyObject* table = NULL;
    PyObject* varbind = NULL;
    PyObject* py_indexes_list = NULL;
    PyObject* py_columns_list = NULL;
    table_info_t* tbl = NULL;
    char *tag = NULL;
    int ret_exceptional = 0;
    int col = 0;

    if (args) {
        if (!PyArg_ParseTuple(args, "OO", &table, &varbind)) {
            PyErr_SetString(PyExc_LookupError, "Invalid arguments");
            ret_exceptional = 1;
            goto done;
        }

        /* get the table root oid out of varbind, which is contained in args */
        if (table && varbind) {
            py_indexes_list = py_netsnmp_attr_obj(table, "indexes");
            py_columns_list = py_netsnmp_attr_obj(table, "columns");
            if (!py_indexes_list || !py_columns_list) {
                PyErr_SetString(PyExc_TypeError,
                        "Table object has no indexes or columns attributes.");
                ret_exceptional = 1;
                goto done;
            }

            if (py_netsnmp_attr_string(varbind, "tag", &tag, NULL) < 0) {
                PyErr_SetString(PyExc_TypeError,
                        "Varbind object has no tag attribute");
                ret_exceptional = 1;
                goto done;
            } else {
                tbl = table_allocate(tag);
                if (!tbl) {
                    ret_exceptional = 1;
                    goto done;
                }

                if (table_get_field_names(tbl) < 0) {
                	PyErr_SetString(PyExc_RuntimeError, "Error while parsing table structure from MIB.");
                	ret_exceptional = 1;
                    goto done;
                }

                for (col = 0; col < tbl->column_scheme.fields; col++) {
                    PyList_Append(py_columns_list,
                            tbl->column_scheme.column[col].py_label_str);
                    PyList_Reverse(py_columns_list);
                }
            }
        }
    }

    done: if (ret_exceptional) {
        table_deallocate(tbl);
        return NULL;
    }

    return PyLong_FromVoidPtr((void *) tbl);
}
static PyObject* find_changes(PyObject* self, PyObject* args) {
    PyObject* arg1 = NULL;
    PyObject* arg2 = NULL;
    if (!PyArg_ParseTuple(args, "OO", &arg1, &arg2)) return NULL;
    PyObject* np_data = PyArray_FROM_OTF(arg1, NPY_FLOAT32, NPY_IN_ARRAY);
    PyObject* np_penalties = PyArray_FROM_OTF(arg2, NPY_FLOAT32, NPY_IN_ARRAY);
    if (np_data == NULL || np_penalties == NULL) {
        Py_XDECREF(np_data);
        Py_XDECREF(np_penalties);
        return NULL;
    }
    int T = PyArray_DIM(np_data, 0);
    if (T != PyArray_DIM(np_penalties, 0) + 1) {
        PyErr_SetString(PyExc_ValueError, "Dimensions of data and penalty are not compatible");
        Py_DECREF(np_data);
        Py_DECREF(np_penalties);
        return NULL;
    }
    float* data = (float*) PyArray_DATA(np_data);
    float* penalties = (float*) PyArray_DATA(np_penalties);
    double* vals = new double[T];
    int* prev = new int[T];
    std::list<SuffStat> checks;
    checks.push_back(SuffStat(0));
    for (int t = 0; t < MIN_SEP - 1; ++t)
        checks.back().add(data[t]);
    for (int t = MIN_SEP - 1; t < T; ++t) {
        double max_val = -std::numeric_limits<double>::max();
        int max_ind = -1;
        for (std::list<SuffStat>::iterator iter = checks.begin(); iter != checks.end(); ++iter) {
            if (iter->prune_t == t) {
                iter = checks.erase(iter);
                --iter;
                continue;
            }
            iter->add(data[t]);
            double val = iter->ll();
            if (iter->t > 0)
                val += vals[iter->t - 1] - penalties[iter->t - 1];
            iter->cost = val;
            if (val > max_val) {
                max_val = val;
                max_ind = iter->t;
            }
        }
        vals[t] = max_val;
        prev[t] = max_ind;
        for (std::list<SuffStat>::iterator iter = checks.begin(); iter != checks.end(); ++iter) {
            if (t < T-1 && iter->prune_t == -1 && iter->cost < vals[t] - penalties[t])
                iter->prune_t = t + MIN_SEP;
        }
        if (t - MIN_SEP + 2 >= MIN_SEP) {
            checks.push_back(SuffStat(t-MIN_SEP+2));
            for (int s = t - MIN_SEP + 2; s <= t; ++s)
                checks.back().add(data[s]);
        }
    }
    PyObject* changes = PyList_New(0);
    int ind = prev[T-1];
    while (ind > 1) {
        PyObject* num = PyInt_FromLong(ind);
        PyList_Append(changes, num);
        Py_DECREF(num);
        ind = prev[ind-1];
    }
    PyList_Reverse(changes);
    delete[] vals;
    delete[] prev;
    Py_DECREF(np_data);
    Py_DECREF(np_penalties);
    return changes;
}
PyObject *do_pile(PyObject *origList, int num_piles, int num_shuffles)
{
	double length = PyList_Size(origList);
	int i;

	// init empty list to return
	PyObject * shuffledList = PyList_New(0);

	// init piles
	PyObject **pilesArray = malloc(num_piles * sizeof(PyObject *));
	for(i = 0; i < num_piles; i++)
	{
		pilesArray[i] = PyList_New(0);
	}

	// deal items into respective piles
	for(i = 0; i < length; i++)
	{
		int pile = i % num_piles;

		// Retrieve Object
		PyObject *temp = PyList_GetItem(origList, i);
		if (temp == NULL)
		{
			Py_DECREF(shuffledList);
			for(i = 0; i < num_piles; i++)
			{
				Py_DECREF(pilesArray[i]);
			}
			free(pilesArray);
			return NULL;
		}

		// append the item
		int test = PyList_Append(pilesArray[pile], temp);
		if (test != 0)
		{
			Py_DECREF(shuffledList);
			for(i = 0; i < num_piles; i++)
			{
				Py_DECREF(pilesArray[i]);
			}
			free(pilesArray);
			return NULL;
		}
		Py_INCREF(temp);

	}

	// Reverse Piles
	for(i = 0; i < num_piles; i++)
	{
		int test = PyList_Reverse(pilesArray[i]);
		if (test != 0)
		{
			Py_DECREF(shuffledList);
			for(i = 0; i < num_piles; i++)
			{
				Py_DECREF(pilesArray[i]);
			}
			free(pilesArray);
			return NULL;
		}
	}

	// Build final list
	for(i = 0; i < num_piles; i++)
	{
		int pile_size = (int)PyList_Size(pilesArray[i]);
		int j;
		for(j = 0; j < pile_size; j++)
		{
			PyObject *temp = PyList_GetItem(pilesArray[i], j);
			if (temp == NULL)
			{
				Py_DECREF(shuffledList);
				for(i = 0; i < num_piles; i++)
				{
					Py_DECREF(pilesArray[i]);
				}
				free(pilesArray);
				return NULL;
			}

			// append the item
			int test = PyList_Append(shuffledList, temp);
			if (test != 0)
			{
				Py_DECREF(shuffledList);
				for(i = 0; i < num_piles; i++)
				{
					Py_DECREF(pilesArray[i]);
				}
				free(pilesArray);
				return NULL;
			}
			Py_INCREF(temp);
		}
	}

	// free uneeded memory
	for(i = 0; i < num_piles; i++)
	{
		Py_DECREF(pilesArray[i]);
	}
	free(pilesArray);

	// recursively shuffle the desired amount
	num_shuffles--;
	if (num_shuffles > 0)
	{
		shuffledList = do_pile(shuffledList, num_piles, num_shuffles);
	}

	return shuffledList;
}
Exemplo n.º 12
0
 /** reverse in place.
  */
 void reverse()
 {
     int r = PyList_Reverse(_p);
     if(r == -1)
         throw err("reverse failed");
 }