Пример #1
0
/*
 * python bulkload class method
 */
static PyObject *
python_sorted_btree_bulkload(PyObject *klass, PyObject *args) {
    int i, result;
    PyObject *item_list, *order, *iter, *item, *prev = NULL;

    if (!PyArg_ParseTuple(args, "OO!", &item_list, &PyInt_Type, &order))
        return NULL;

    if ((iter = PyObject_GetIter(item_list)) == NULL)
        return NULL;
    for (i = 0, item = PyIter_Next(iter); item;
            ++i, item = PyIter_Next(iter)) {
        if (i) {
            result = PyObject_RichCompareBool(prev, item, Py_LT);
            if (result <= 0) {
                if (!result)
                    PyErr_SetString(PyExc_ValueError,
                            "the bulkloaded list must already be sorted");
                Py_DECREF(prev);
                Py_DECREF(item);
                Py_DECREF(iter);
                return NULL;
            }
            Py_DECREF(prev);
        }
        prev = item;
    }
    if (prev) Py_DECREF(prev);
    Py_DECREF(iter);

    if ((iter = PyObject_GetIter(item_list)) == NULL)
        return NULL;

    btsort_pyobject *tree = PyObject_GC_New(
            btsort_pyobject, &btsort_pytypeobj);

    if (bulkload(tree, iter, (int)PyInt_AsLong(order)))
        return NULL;

    return (PyObject *)tree;
}
Пример #2
0
 types::set<T> from_python<types::set<T>>::convert(PyObject *obj)
 {
   types::set<T> v = types::empty_set();
   // may be useful to reserve more space ?
   PyObject *iterator = PyObject_GetIter(obj);
   while (PyObject *item = PyIter_Next(iterator)) {
     v.add(::from_python<T>(item));
     Py_DECREF(item);
   }
   Py_DECREF(iterator);
   return v;
 }
Пример #3
0
static PyObject *
udn_resolve_from_search_list(PyObject *self, PyObject *args, PyObject *keywds)
{
  PyObject *name_space = NULL;
  PyObject *return_value = NULL;
  PyObject *iterator = NULL;

  PyObject *search_list = NULL;
  PyObject *name;
  PyObject *default_value = NULL;

  static char *kwlist[] = {"search_list", "name", "default", NULL};

  if (!PyArg_ParseTupleAndKeywords(args, keywds, "OO|O", kwlist,
                                   &search_list, &name, &default_value)) {
    return NULL;
  }

  if (!(PyUnicode_Check(name) || PyString_Check(name))) {
    PyErr_SetString(PyExc_ValueError, "name must be string");
    return NULL;
  }

  iterator = PyObject_GetIter(search_list);
  if (iterator == NULL) {
    return_value = NULL;
    /* PyErr_SetString(PyExc_TypeError, "search_list is not iterable"); */
    goto done;
  }

  while ((name_space = PyIter_Next(iterator))) {
    return_value = _resolve_udn(name_space, name);
    Py_DECREF(name_space);
    if (return_value != NULL) {
      goto done;
    }
  }
done:
  if (return_value == NULL) {
    if (default_value != NULL) {
      return_value = default_value;
      Py_INCREF(return_value);
    } else {
      return_value = UnresolvedPlaceholder;
      Py_INCREF(return_value);
    }
  }
  Py_XDECREF(iterator);
  /* change the return value to be a bit more compatible with the way things
     work in the python code.
   */
  return return_value;
}
Пример #4
0
static int __Pyx_EndUnpack(PyObject *iter) {
	PyObject *item;
	if ((item = PyIter_Next(iter))) {
		Py_DECREF(item);
		__Pyx_UnpackError();
		return -1;
	}
	else if (!PyErr_Occurred())
		return 0;
	else
		return -1;
}
Пример #5
0
static PyObject *
iobase_readlines(PyObject *self, PyObject *args)
{
    Py_ssize_t hint = -1, length = 0;
    PyObject *result;

    if (!PyArg_ParseTuple(args, "|O&:readlines", &_PyIO_ConvertSsize_t, &hint)) {
        return NULL;
    }

    result = PyList_New(0);
    if (result == NULL)
        return NULL;

    if (hint <= 0) {
        /* XXX special-casing this made sense in the Python version in order
           to remove the bytecode interpretation overhead, but it could
           probably be removed here. */
        _Py_IDENTIFIER(extend);
        PyObject *ret = _PyObject_CallMethodId(result, &PyId_extend, "O", self);

        if (ret == NULL) {
            Py_DECREF(result);
            return NULL;
        }
        Py_DECREF(ret);
        return result;
    }

    while (1) {
        PyObject *line = PyIter_Next(self);
        if (line == NULL) {
            if (PyErr_Occurred()) {
                Py_DECREF(result);
                return NULL;
            }
            else
                break; /* StopIteration raised */
        }

        if (PyList_Append(result, line) < 0) {
            Py_DECREF(line);
            Py_DECREF(result);
            return NULL;
        }
        length += PyObject_Size(line);
        Py_DECREF(line);

        if (length > hint)
            break;
    }
    return result;
}
Пример #6
0
static PyObject *IOManager_submit(IOManager *self, PyObject *args) {
   PyObject *req_s, *iter;
   IORequest *item;
   Py_ssize_t l;
   int rc;
   struct iocb **cb, **cb_l;
   if (!PyArg_ParseTuple(args, "O", &req_s)) return NULL;

   if ((l = PySequence_Size(req_s)) < 0) return NULL;
   if (l > (self->nr_events - self->pending_events)) {
      PyErr_SetString(PyExc_ValueError, "Queue length exceeded.");
      return NULL;
   }
   
   cb = self->cbs;
   cb_l = cb + (self->nr_events - self->pending_events);
   
   if (!(iter = PyObject_GetIter(req_s))) return NULL;
   for (; (item = (IORequest*) PyIter_Next(iter)); cb++) {
      if (!PyObject_IsInstance((PyObject*) item, (PyObject*) &IORequestType)) {
         Py_DECREF(item);
         PyErr_SetString(PyExc_TypeError, "Elements of argument 0 must be of type IORequest.");
         return IOM_iocb_cleanup(self, cb+1);
      }
      if (cb == cb_l) {
         Py_DECREF(item);
         PyErr_SetString(PyExc_ValueError, "Queue length exceeded (secondary check)");
         return IOM_iocb_cleanup(self, cb+1);
      }
      if (item->submitted) {
         Py_DECREF(item);
         PyErr_SetString(PyExc_ValueError, "Element of argument 0 had already been submitted earlier.");
         return IOM_iocb_cleanup(self, cb+1);
      }
      item->submitted = 1;
      item->iocb.u.c.resfd = self->fd;
      *cb = &item->iocb;
   }
   if (PyErr_Occurred()) return IOM_iocb_cleanup(self, cb);
   
   l = cb - self->cbs;
   rc = io_submit(self->ctx, l, self->cbs);
   if (rc < 0) {
      errno = -rc;
      PyErr_SetFromErrno(PyExc_OSError);
      return IOM_iocb_cleanup(self, cb);
   }
   /* Keep one reference to each element read from the iterable, to make sure
      they aren't deallocated while we wait for their IO requests to complete
   */
   self->pending_events += l;
   Py_RETURN_NONE;
}
Пример #7
0
static PyObject* cstuff_DirectionMap_set_walls(cstuff_DirectionMap* self, PyObject *args) {
	PyObject *list1, *list2, *iterator1, *iterator2, *value;
	int whichwall;
	int row, col, stride;

	if (!PyArg_ParseTuple(args, "Oi", &list1, &whichwall)) {
		return NULL;
	}

	row = 0;
	if (!(iterator1 = PyObject_GetIter(list1))) {
		return NULL;
	}
	while ((list2 = PyIter_Next(iterator1)) && row < g_rows) {
		stride = row * g_cols;
		if (!(iterator2 = PyObject_GetIter(list2))) {
			Py_DECREF(list2);
			break;
		}
		col = 0;
		while ((value = PyIter_Next(iterator2)) && col < g_cols) {
			if (PyLong_CheckExact(value) && PyLong_AsLong(value) == whichwall) {
				self->walls->values[stride+col] = 1;
			}
			Py_DECREF(value);
			col += 1;
		}
		Py_DECREF(iterator2);
		Py_DECREF(list2);
		row += 1;
	}

	Py_DECREF(iterator1);

	if (PyErr_Occurred()) {
		return NULL;
	}

	Py_RETURN_NONE;
}
Пример #8
0
DyND_PyWrapperIter<T> begin(PyObject *obj)
{
  PyObject *iter = PyObject_GetIter(obj);
  if (iter == NULL) {
    std::cout << "not an iterator" << std::endl;
  }

  DyND_PyWrapperIter<T> it;
  it.iter = iter;
  it.item = PyIter_Next(it.iter);

  return it;
}
Пример #9
0
/* Run an iterator to exhaustion.  Shortcut for
   the extend/extendleft methods when maxlen == 0. */
static PyObject*
consume_iterator(PyObject *it)
{
    PyObject *item;

    while ((item = PyIter_Next(it)) != NULL) {
        Py_DECREF(item);
    }
    Py_DECREF(it);
    if (PyErr_Occurred())
        return NULL;
    Py_RETURN_NONE;
}
Пример #10
0
int _py_itermapping_cb(pgctx_t *ctx, int i, dbtype_t *key, dbtype_t *value, void *user)
{
    PyObject *iter = (PyObject*)user;
    PyObject *item = PyIter_Next(iter);
    PyObject *k = PySequence_GetItem(item, 0);
    PyObject *v = PySequence_GetItem(item, 1);
    *key = from_python(ctx, k);
    *value = from_python(ctx, v);
    Py_DECREF(k); Py_DECREF(v);
    Py_DECREF(item);
    if (PyErr_Occurred()) return -1;
    return 0;
}
Пример #11
0
static PyObject *
LDAPEntry_delete(LDAPEntry *self, PyObject *args, PyObject *kwds) {
	char *dnstr;
	PyObject *keys = PyMapping_Keys((PyObject *)self);
	PyObject *iter, *key;
	PyObject *tmp;
	LDAPValueList *value;

	/* Client must be set. */
	if (self->client == NULL) {
		PyErr_SetString(PyExc_AttributeError, "LDAPClient is not set.");
		return NULL;
	}

	/* Get DN string. */
	tmp = PyObject_Str(self->dn);
	dnstr = PyObject2char(tmp);
	Py_DECREF(tmp);
	if (dnstr == NULL) return NULL;
	if (LDAPClient_DelEntryStringDN(self->client, dnstr) != 0) return NULL;

	if (keys == NULL) return NULL;

	iter = PyObject_GetIter(keys);
	Py_DECREF(keys);
	if (iter == NULL) return NULL;

	for (key = PyIter_Next(iter); key != NULL; key = PyIter_Next(iter)) {
		/* Return value: New reference. */
		value = (LDAPValueList *)LDAPEntry_GetItem(self, key);
		if (value == NULL) {
			Py_DECREF(iter);
			Py_DECREF(key);
			return NULL;
		}
		value->status = 2;
	}
	return Py_None;
}
Пример #12
0
static int Dict_iterNext(JSOBJ obj, JSONTypeContext *tc)
{
#if PY_MAJOR_VERSION >= 3
  PyObject* itemNameTmp;
#endif

  if (GET_TC(tc)->itemName)
  {
    Py_DECREF(GET_TC(tc)->itemName);
    GET_TC(tc)->itemName = NULL;
  }

  if (!(GET_TC(tc)->itemName = PyIter_Next(GET_TC(tc)->iterator)))
  {
    PRINTMARK();
    return 0;
  }

  if (!(GET_TC(tc)->itemValue = PyObject_GetItem(GET_TC(tc)->dictObj, GET_TC(tc)->itemName)))
  {
    PRINTMARK();
    return 0;
  }

  if (PyUnicode_Check(GET_TC(tc)->itemName))
  {
    GET_TC(tc)->itemName = PyUnicode_AsUTF8String (GET_TC(tc)->itemName);
  }
  else
  if (!PyString_Check(GET_TC(tc)->itemName))
  {
    if (UNLIKELY(GET_TC(tc)->itemName == Py_None))
    {
      GET_TC(tc)->itemName = PyString_FromString("null");
      return 1;
    }

    GET_TC(tc)->itemName = PyObject_Str(GET_TC(tc)->itemName);
#if PY_MAJOR_VERSION >= 3
    itemNameTmp = GET_TC(tc)->itemName;
    GET_TC(tc)->itemName = PyUnicode_AsUTF8String (GET_TC(tc)->itemName);
    Py_DECREF(itemNameTmp);
#endif
  }
  else
  {
    Py_INCREF(GET_TC(tc)->itemName);
  }
  PRINTMARK();
  return 1;
}
Пример #13
0
static PyObject *
_io__IOBase_readlines_impl(PyObject *self, Py_ssize_t hint)
/*[clinic end generated code: output=2f50421677fa3dea input=9400c786ea9dc416]*/
{
    Py_ssize_t length = 0;
    PyObject *result;

    result = PyList_New(0);
    if (result == NULL)
        return NULL;

    if (hint <= 0) {
        /* XXX special-casing this made sense in the Python version in order
           to remove the bytecode interpretation overhead, but it could
           probably be removed here. */
        _Py_IDENTIFIER(extend);
        PyObject *ret = _PyObject_CallMethodIdObjArgs(result, &PyId_extend,
                                                      self, NULL);

        if (ret == NULL) {
            Py_DECREF(result);
            return NULL;
        }
        Py_DECREF(ret);
        return result;
    }

    while (1) {
        PyObject *line = PyIter_Next(self);
        if (line == NULL) {
            if (PyErr_Occurred()) {
                Py_DECREF(result);
                return NULL;
            }
            else
                break; /* StopIteration raised */
        }

        if (PyList_Append(result, line) < 0) {
            Py_DECREF(line);
            Py_DECREF(result);
            return NULL;
        }
        length += PyObject_Size(line);
        Py_DECREF(line);

        if (length > hint)
            break;
    }
    return result;
}
Пример #14
0
static PyObject *
Reader_iternext(ReaderObj *self)
{
        PyObject *lineobj;
        PyObject *fields = NULL;
        char *line, c;
	int linelen;

	if (parse_reset(self) < 0)
		return NULL;
        do {
                lineobj = PyIter_Next(self->input_iter);
                if (lineobj == NULL) {
                        /* End of input OR exception */
                        if (!PyErr_Occurred() && self->field_len != 0)
                                PyErr_Format(error_obj,
					     "newline inside string");
                        return NULL;
                }
		++self->line_num;

                line = PyString_AsString(lineobj);
		linelen = PyString_Size(lineobj);

                if (line == NULL || linelen < 0) {
                        Py_DECREF(lineobj);
                        return NULL;
                }
                while (linelen--) {
			c = *line++;
			if (c == '\0') {
				Py_DECREF(lineobj);
				PyErr_Format(error_obj,
					     "line contains NULL byte");
				goto err;
			}
			if (parse_process_char(self, c) < 0) {
				Py_DECREF(lineobj);
				goto err;
			}
		}
                Py_DECREF(lineobj);
		if (parse_process_char(self, 0) < 0)
			goto err;
        } while (self->state != START_RECORD);

        fields = self->fields;
        self->fields = NULL;
err:
        return fields;
}
Пример #15
0
static PyObject * pyAppendToRecord(PyObject *self, PyObject *args) {

    PyObject* py_dict;
    PyObject* py_iter;
    PyObject* py_key;
    PyObject* py_val;

    int recid, rc;
    char *allData;
    unsigned size;


    // retrieve record id and dictionary object
    if (!PyArg_ParseTuple(args, "iO!", &recid, &PyDict_Type, &py_dict)) {
        return Py_BuildValue("i", 1);   // ERROR - PyArg_ParseTuple
    }

    // abort if record id does not exist
    rc = MEgetAllFieldsById(dbhandle, recid, &allData, &size);
    if (size == 0) {
        return Py_BuildValue("i", 2);   // ERROR - record ID not found   
    }

    // retrieve iterator
    py_iter = PyObject_GetIter(py_dict);
    if (!py_iter) { 
        return Py_BuildValue("i", 3);   // ERROR - Not an iterator
    }

    // loop through key-value pairs and append to record
    int kvcount = 0;
    while (py_key = PyIter_Next(py_iter)) {
        py_val = PyDict_GetItem(py_dict, py_key);

    char * key = PyString_AsString(py_key);
    char * val = PyString_AsString(py_val);
    if (debugMode) {
        printf("pyAppendToRecord: %s - %s\n", key, val);
    }

    // add kv pair into meta engine
    kvcount++;
    MEaddKeyValue(dbhandle, recid, key, val);
    Py_DECREF(py_key);
}
Py_DECREF(py_iter);
if (debugMode) {
    printf("pyAppendToRecord: total kv count: %i\n", kvcount);
}
return Py_BuildValue("i", 0);   // SUCCESS
} 
Пример #16
0
llvm_compat_bool iterwrapperHasnextUnboxed(Box* s) {
    RELEASE_ASSERT(s->cls == iterwrapper_cls, "");
    BoxedIterWrapper* self = static_cast<BoxedIterWrapper*>(s);

    Box* next = PyIter_Next(self->iter);
    RELEASE_ASSERT(!self->next, "");
    self->next = next;
    if (!next) {
        if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_StopIteration))
            throwCAPIException();
        PyErr_Clear();
    }
    return next != NULL;
}
Пример #17
0
static PyObj
statement_load_chunks(PyObj self, PyObj args, PyObj kw)
{
	char *words[] = {"chunks_iter", NULL};
	PyObj chunk_iter, ob;
	uint32 total = 0;

	if (PyPgStatement_GetParameters(self) != Py_None)
	{
		PyErr_SetString(PyExc_TypeError,
			"cannot use load_rows with constant parameters");
		return(NULL);
	}

	if (!PyArg_ParseTupleAndKeywords(args, kw, "O:load_chunks", words, &chunk_iter))
		return(NULL);

	if (DB_IS_NOT_READY())
		return(NULL);

	chunk_iter = PyObject_GetIter(chunk_iter);
	if (chunk_iter == NULL)
		return(NULL);

	while ((ob = PyIter_Next(chunk_iter)))
	{
		PyObj row_iter;
		int r;

		row_iter = PyObject_GetIter(ob);
		Py_DECREF(ob);
		if (row_iter == NULL)
		{
			Py_DECREF(chunk_iter);
			return(NULL);
		}

		r = load_rows(self, row_iter, &total);
		Py_DECREF(row_iter);

		if (r)
		{
			Py_DECREF(chunk_iter);
			return(NULL);
		}
	}
	Py_DECREF(chunk_iter);

	return(PyLong_FromUnsignedLong(total));
}
Пример #18
0
static PyObject *
attr_dir_merge(PyObject *_self, PyObject *map)
{
	PyObject *keys, *iter;
	PyObject *key, *value;
	int status;

	keys = PyMapping_Keys(map);
	if (!keys)
	    return NULL;
	iter = PyObject_GetIter(keys);
	Py_DECREF(keys);
	if (!iter)
	    return NULL;

	for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
		value = PyObject_GetItem(map, key);
		if (!value)
			goto err;

		status = attr_dir_ass_subscript(_self, key, value);
		Py_DECREF(value);
		if (status < 0)
			goto err;
		Py_DECREF(key);
	}
	Py_DECREF(iter);
	if (PyErr_Occurred())
		return NULL;

	return Py_None;

 err:
	Py_DECREF(iter);
	Py_DECREF(key);
	return NULL;
}
Пример #19
0
char *uwsgi_python_get_thread_name(PyObject *thread_id) {
	PyObject *threading_module = PyImport_ImportModule("threading");
	if (!threading_module) return NULL;
	
	PyObject *threading_dict = PyModule_GetDict(threading_module);
	if (!threading_dict) return NULL;

	PyObject *threading_enumerate = PyDict_GetItemString(threading_dict, "enumerate");
	if (!threading_enumerate) return NULL;

	PyObject *threads_list = PyEval_CallObject(threading_enumerate, (PyObject *)NULL);
	if (!threads_list) return NULL;

	PyObject *threads_list_iter = PyObject_GetIter(threads_list);
	if (!threads_list_iter) goto clear;

	PyObject *threads_list_next = PyIter_Next(threads_list_iter);
	while(threads_list_next) {
		PyObject *thread_ident = PyObject_GetAttrString(threads_list_next, "ident");
		if (!thread_ident) goto clear2;
		if (PyInt_AsLong(thread_ident) == PyInt_AsLong(thread_id)) {
			PyObject *thread_name = PyObject_GetAttrString(threads_list_next, "name");
			if (!thread_name) goto clear2;
			char *name = PyString_AsString(thread_name);
			Py_DECREF(threads_list_iter);
			Py_DECREF(threads_list);
			return name;
		}
		threads_list_next = PyIter_Next(threads_list_iter);
	}

clear2:
	Py_DECREF(threads_list_iter);
clear:
	Py_DECREF(threads_list);
	return NULL;
}
Пример #20
0
static PyObject *
DiscoDBIter_format(DiscoDBIter *self, PyObject *format, int N)
{
    PyObject
        *iterator = PyObject_GetIter((PyObject *)self),
        *string = PyString_FromString(""),
        *item = NULL;
    int i;

    if (iterator == NULL)
        goto Done;

    if (string == NULL)
        goto Done;

    for (i = 0; i < N + 1; i++) {
        item = PyIter_Next(iterator);
        if (item == NULL) {
            if (PyErr_Occurred())
                goto Done;
            else
                break;
        }

        if (i > 0)
            PyString_ConcatAndDel(&string, PyString_FromString(", "));

        if (i < N)
            PyString_ConcatAndDel(&string, PyString_Format(format, item));
        else
            PyString_ConcatAndDel(&string, PyString_FromString("..."));

        if (string == NULL)
            goto Done;

        Py_CLEAR(item);
    }

 Done:
    Py_CLEAR(iterator);
    Py_CLEAR(item);

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

    return string;
}
Пример #21
0
static range* _python_function(range_request* rr,
                     const char* funcname, const range** r)
{
  range* result = range_new(rr);
  PyObject * pLibcrangeCallFunc;
  PyObject * pNodesReturned;
  PyObject * pMain = PyImport_AddModule("__main__");
  // printf("rr: %p, funcname=%s, range**r = %p\n", rr, funcname, r);
  pLibcrangeCallFunc = PyObject_GetAttrString(pMain, "libcrange_call_func");
  
  if (pLibcrangeCallFunc && PyCallable_Check(pLibcrangeCallFunc)) {
    PyObject * pRangeFuncName;
    PyObject * item;
    pRangeFuncName = PyString_FromString(funcname);
    PyObject * pFuncArgs;
    PyObject * pTempArgList = PyList_New(0);
    PyList_Append(pTempArgList, pRangeFuncName);
    Py_DECREF(pRangeFuncName);
    
    // build our range** into python function args
    const range** p_r = r;
    while (*p_r) {
      item = range_to_py_array(range_request_pool(rr), *p_r);
      PyList_Append(pTempArgList, item);
      Py_DECREF(item);
      p_r++;
    }
    pFuncArgs = PyList_AsTuple(pTempArgList);
    Py_DECREF(pTempArgList);
    
    // call the function
    pNodesReturned = PyObject_CallObject(pLibcrangeCallFunc, pFuncArgs);
    Py_DECREF(pFuncArgs);
    PyObject *iterator = PyObject_GetIter(pNodesReturned);
    if (iterator == NULL) {
      printf("ERROR: python function %s ran, but didn't return an iteratable object", funcname);
      return result;
    }
    // an iteratable object was returned, transform it into result
    while (item = PyIter_Next(iterator)) {
      // PyObject_Print(item, stdout, 0 );
      range_add(result, PyString_AsString(item));
      Py_DECREF(item);
    }
    Py_DECREF(pNodesReturned);
  }

  return result;
}
Пример #22
0
static PyObject *
record_items_next(ApgRecordItemsObject *it)
{
    ApgRecordObject *seq;
    PyObject *key;
    PyObject *val;
    PyObject *tup;

    assert(it != NULL);
    seq = it->it_seq;
    if (seq == NULL) {
        return NULL;
    }
    assert(ApgRecord_CheckExact(seq));
    assert(it->it_map_iter != NULL);

    key = PyIter_Next(it->it_map_iter);
    if (key == NULL) {
        /* likely it_map_iter had less items than seq has values */
        goto exhausted;
    }

    if (it->it_index < Py_SIZE(seq)) {
        val = ApgRecord_GET_ITEM(seq, it->it_index);
        ++it->it_index;
        Py_INCREF(val);
    }
    else {
        /* it_map_iter had more items than seq has values */
        Py_DECREF(key);
        goto exhausted;
    }

    tup = PyTuple_New(2);
    if (tup == NULL) {
        Py_DECREF(val);
        Py_DECREF(key);
        goto exhausted;
    }

    PyTuple_SET_ITEM(tup, 0, key);
    PyTuple_SET_ITEM(tup, 1, val);
    return tup;

exhausted:
    Py_CLEAR(it->it_map_iter);
    Py_CLEAR(it->it_seq);
    return NULL;
}
Пример #23
0
static int
iterable_iterate(PyObject *v, int (*visit)(PyObject *, void *),
		void *arg)
{
    if (NyNodeSet_Check(v)) {
	return NyNodeSet_iterate((NyNodeSetObject *)v, visit, arg);
    } else if (NyHeapView_Check(v)) {
	return NyHeapView_iterate((NyHeapViewObject *)v, visit, arg);
    } else if (PyList_Check(v)) { /* A bit faster than general iterator?? */
	int i, r;
	PyObject *item;
	for (i = 0; i < PyList_GET_SIZE(v); i++) {
	    item = PyList_GET_ITEM(v, i);
	    Py_INCREF(item);
	    r = visit(item, arg);
	    Py_DECREF(item);
	    if (r == -1)
	      return -1;
	    if (r == 1)
	      break;
	}
	return 0;
    } else { /* Do the general case. */
	PyObject *it = PyObject_GetIter(v);
	int r;
	if (it == NULL)
	  goto Err;
	/* Run iterator to exhaustion. */
	for (;;) {
	    PyObject *item = PyIter_Next(it);
	    if (item == NULL) {
		if (PyErr_Occurred())
		  goto Err;
		break;
	    }
	    r = visit(item, arg);
	    Py_DECREF(item);
	    if (r == -1)
	      goto Err;
	    if (r == 1)
	      break;
	}
	Py_DECREF(it);
	return 0;
      Err:
	Py_XDECREF(it);
	return -1;
    }
}
Пример #24
0
/*	Set the slice of UniqueList between `ilow` and `ihigh` to the contents of `itemlist`.
	The `itemlist` must be containing unique elements. The `itemlist` may be NULL, indicating
	the assignment of an empty list (slice deletion).
*/
int
UniqueList_SetSlice(UniqueList *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *itemlist) {
	PyObject *iter = NULL;
	PyObject *newitem;

	if (itemlist != NULL) iter = PyObject_GetIter(itemlist);

	if (iter != NULL) {
		for (newitem = PyIter_Next(iter); newitem != NULL; newitem = PyIter_Next(iter)) {
			if (PyDict_Check(newitem) || PyList_Check(newitem) || PyTuple_Check(newitem)) {
				PyErr_SetString(PyExc_ValueError, "This type of list can not contain instances of Python tuple, list or dict.");
				return -1;
			}
			if (UniqueList_Contains(self, newitem) == 1) {
				PyErr_Format(PyExc_ValueError, "%R is already in the list.", newitem);
				return -1;
			}
			Py_DECREF(newitem);
		}
		Py_DECREF(iter);
	}

    return PyList_SetSlice((PyObject *)self, ilow, ihigh, itemlist);
}
Пример #25
0
/*	Searches amongs lower-cased keystrings to find a match with the key.
  	Sets the `found` parameter's value to 1 if key found in the list, 0 otherwise. */
PyObject *
searchLowerCaseKeyMatch(LDAPEntry *self, PyObject *key, int* found) {
	PyObject *keys = PyDict_Keys((PyObject *)self);
	PyObject *iter = PyObject_GetIter(keys);
	PyObject *item;

	if (iter == NULL) {
		Py_DECREF(keys);
		return NULL;
	}
	/* Searching for same lowercase key amongs the other keys. */
	for (item = PyIter_Next(iter); item != NULL; item = PyIter_Next(iter)) {
		if (lowerCaseMatch(item, key) == 1) {
			key = item;
			*found = 1;
			break;
		}
		*found = 0;
		Py_DECREF(item);
	}
	Py_DECREF(iter);
	Py_DECREF(keys);
	return key;
}
Пример #26
0
static PyObject *
chain_next(chainobject *lz)
{
	PyObject *it;
	PyObject *item;

	while (lz->iternum < lz->tuplesize) {
		it = PyTuple_GET_ITEM(lz->ittuple, lz->iternum);
		item = PyIter_Next(it);
		if (item != NULL)
			return item;
		lz->iternum++;
	}
	return NULL;
}
Пример #27
0
  void getWords (vector<pair<int, string>> & wordList)
  {
    PyObject * pyIter = PyObject_CallMethod (m_pyDict, (char *) "iteritems", NULL);
    PyObject * pyItem;
    while (pyItem = PyIter_Next (pyIter))
      {
	const char * key = PyString_AsString (PyTuple_GetItem (pyItem, 0));
	long value = PyInt_AsLong (PyTuple_GetItem (pyItem, 1));

	wordList . push_back (pair<int, string> (value, key));

	Py_DECREF (pyItem);
      }
    Py_DECREF (pyIter);
  }
Пример #28
0
static response_status
start_response_write(client_t *client)
{
    PyObject *iterator;
    PyObject *item;
    char *buf;
    Py_ssize_t buflen;
    response_status ret;

    iterator = PyObject_GetIter(client->response);
    if (PyErr_Occurred()){
        /* write_error_log(__FILE__, __LINE__); */
        call_error_logger();
        return STATUS_ERROR;
    }
    client->response_iter = iterator;

    item =  PyIter_Next(iterator);
    DEBUG("client %p", client);
    if(item != NULL && PyBytes_Check(item)){

        //write string only
        buf = PyBytes_AS_STRING(item);
        buflen = PyBytes_GET_SIZE(item);

        /* DEBUG("status_code %d body:%.*s", client->status_code, (int)buflen, buf); */
        ret = write_headers(client, buf, buflen, 0);
        //TODO when ret == STATUS_SUSPEND keep item
        Py_DECREF(item);
        return ret;
    }else{
        if (item == NULL && !PyErr_Occurred()){
            //Stop Iteration
            RDEBUG("WARN iter item == NULL");
            return write_headers(client, NULL, 0, 0);
        }else{
            PyErr_SetString(PyExc_TypeError, "response item must be a string");
            Py_XDECREF(item);
            if (PyErr_Occurred()){
                /* write_error_log(__FILE__, __LINE__); */
                call_error_logger();
                return STATUS_ERROR;
            }
        }

    }
    return STATUS_ERROR;
}
Пример #29
0
static PyObject *
deque_extendleft(dequeobject *deque, PyObject *iterable)
{
    PyObject *it, *item;

    /* Handle case where id(deque) == id(iterable) */
    if ((PyObject *)deque == iterable) {
        PyObject *result;
        PyObject *s = PySequence_List(iterable);
        if (s == NULL)
            return NULL;
        result = deque_extendleft(deque, s);
        Py_DECREF(s);
        return result;
    }

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

    if (deque->maxlen == 0)
        return consume_iterator(it);

    while ((item = PyIter_Next(it)) != NULL) {
        deque->state++;
        if (deque->leftindex == 0) {
            block *b = newblock(NULL, deque->leftblock,
                                deque->len);
            if (b == NULL) {
                Py_DECREF(item);
                Py_DECREF(it);
                return NULL;
            }
            assert(deque->leftblock->leftlink == NULL);
            deque->leftblock->leftlink = b;
            deque->leftblock = b;
            deque->leftindex = BLOCKLEN;
        }
        deque->len++;
        deque->leftindex--;
        deque->leftblock->data[deque->leftindex] = item;
        TRIM(deque, deque_pop);
    }
    Py_DECREF(it);
    if (PyErr_Occurred())
        return NULL;
    Py_RETURN_NONE;
}
Пример #30
0
static void copyFlowsToPython(FlowGraph graph, PyObject *edges)
{
    PyObject *item, *iter;
    int from, to;
    float flow;

    iter = PyObject_GetIter(edges);
    while((item = PyIter_Next(iter))) {
        from = PyInt_AsLong(PyList_GetItem(item, 0));
        to = PyInt_AsLong(PyList_GetItem(item, 1));
        flow = Graph_getFlow(graph, from, to);
        PyList_SetItem(item, 3, PyFloat_FromDouble(flow));
        Py_DECREF(item);
    }
    Py_DECREF(iter);
}