Beispiel #1
0
static int ws_onclose(libwebsock_client_state *state)
{
  //generate python client state object and call python callable
  PyObject *stateObject = NULL;
  PyObject *arglist;

  PyObject **clients_array;

  Py_ssize_t clients_len;
  int clients_idx;

  clients_len = PySequence_Size(connected_clients_list);
  clients_array = PySequence_Fast_ITEMS(connected_clients_list);

  for(clients_idx = 0; clients_idx < clients_len; clients_array++, clients_idx++) {
    if(*clients_array && ((libwebsock_ClientStateObject *)*clients_array)->state == state) {
      stateObject = *clients_array;
      break;
    }
  }

  if(stateObject) {
    PySequence_DelItem(connected_clients_list, PySequence_Index(connected_clients_list, stateObject));
    if(onclose_callback) {
      arglist = Py_BuildValue("(O)", stateObject);
      PyObject_CallObject(onclose_callback, arglist);
      Py_DECREF(arglist);
    }
    Py_DECREF(stateObject);
  }
  return 0;
}
Beispiel #2
0
/* {{{ _syspath_append
 */
static int
_syspath_append(const char *dir)
{
	PyObject *sys, *path, *dirstr;

	if (dir == NULL) {
		return -1;
	}

	sys = PyImport_ImportModule("sys");
	path = PyObject_GetAttrString(sys, "path");
	dirstr = PyString_FromString(dir);

	/* Append dir to sys.path if it's not already there. */
	if (PySequence_Index(path, dirstr) == -1) {
		PyErr_Clear();
		PyList_Append(path, dirstr);
	}

	Py_DECREF(dirstr);
	Py_DECREF(path);
	Py_DECREF(sys);

	return 0;
}
// dtor
static void
AtTime_dealloc( PyObject* self ) {
	AtTime * at = (AtTime*)self;
	
	PyObject * tsd = PyThreadState_GetDict();
	PyObject * atd = PyDict_GetItemString(tsd, "_AtTime" );
	if( atd ) {
		PyObject * cur_stack = PyDict_GetItemString( atd, "current_stack" );
		PyObject * time_stack = PyDict_GetItemString( atd, "time_stack" );
		
		Py_ssize_t time_top = PyList_Size(time_stack) - 1;
		PyObject * hashKey = PyLong_FromLong( PyObject_Hash((PyObject*)self) );
		Py_ssize_t pos = PySequence_Index(cur_stack, hashKey);
		Py_DECREF(hashKey);
		
		// Restore the correct time value if we are current
		if( PyList_Size(cur_stack) == pos + 1 ) {
			thread_local(current_time) = PyLong_AsLong( PyList_GetItem( time_stack, time_top ) );
			//PySys_WriteStdout( "Current AtTime object destroyed, restoring thread_local(current_time) to %i\n", thread_local(current_time) );
			PySequence_DelItem(time_stack,time_top);
		} else
			// If we aren't current, then we delete the time at the position one above where we are in the current_stack
			PySequence_DelItem(time_stack,pos+1);
		
		PySequence_DelItem(cur_stack,pos);
		
		// If we are the last AtTime object local to this thread, then remove the _AtTime thread-local dict
		if( PyList_Size(cur_stack) == 0 ) {
			thread_local(use_time_context) = (PyDict_GetItemString( atd, "restore_use_time_context" ) == Py_True) ? TRUE : FALSE;
			//PySys_WriteStdout( "Last AtTime object destroyed, setting thread_local(use_time_context) to FALSE\n" );
			PyDict_DelItemString(tsd, "_AtTime");
		}
	}
	self->ob_type->tp_free(self);
}
Beispiel #4
0
/**
 *  关闭连接
 *  args[0] 连接对象
 *  args[1] 连接池对象
 **/
static PyObject*
connect_close(PyObject *self, PyObject *args)
{
    PyConnectPoolObject *pool = (PyConnectPoolObject*)PySequence_GetItem(args, 1);
    PyConnectObject *con = (PyConnectObject*)PySequence_GetItem(args, 0);
    int index;
    if(!pool || !con || !PyConnectPool_CheckExact(pool) || !PyConnect_CheckExact(con))
    {
        PyErr_SetString(PyExc_RuntimeError, "参数错误");
        return NULL;
    }
    //从busy连接队列中删除掉
    index = PySequence_Index(pool->cons_busy, (PyObject*)con);
    if(index != -1)
        PySequence_DelItem(pool->cons_busy, index);
    //检查空闲的连接池数量
    //如果空闲的连接过多,则关闭多余的连接
    if(PySequence_Length(pool->cons_free) > pool->free_max)
    {
        connect_dealloc(con);
    }
    else
    {
        PyList_Insert((PyObject*)(pool->cons_free), 0, (PyObject*)con);
    }
    return Py_True;
}
Beispiel #5
0
 /** tuple index.
  * @return index if found
  * @throw index_err if not found
  * @throw type_err
  */
 long index(const obj& o)const
 {
     long r = PySequence_Index(_p, o.p());
     if(r == -1)
         throw index_err("index failed");
     return r;
 }
Beispiel #6
0
PyObject *Timer_end(Timer *self, PyObject *args) {
    if (!(self->flags & TIMER_ACTIVE))
        Py_RETURN_NONE;
        
    if (PySequence_DelItem(ACTIVE_TIMERS, PySequence_Index(ACTIVE_TIMERS, (PyObject *)self)) == -1)
        return NULL;
    self->flags &= ~TIMER_ACTIVE;
    
    POST_EVENT(self, TIMER_END);
    
    Py_RETURN_NONE;
}
Beispiel #7
0
static PyObject *t_sequence_index(t_sequence *self, PyObject *value)
{
    int index;

    if (self->itemvalue.flags & V_PURE)
        index = PySequence_Index(self->sequence, value);
    else
    {
        value = _useValue(self, value);
        if (!value)
            return NULL;

        index = PySequence_Index(self->sequence, value);
        Py_DECREF(value);
    }

    if (index < 0)
        return NULL;

    return PyInt_FromLong(index);
}
static PyObject *
evregpy_disconnect (PyObject *self, PyObject *function)
{
  PyObject *func;
  int index;
  PyObject *callback_list = (((eventregistry_object *) self)->callbacks);

  if (!PyArg_ParseTuple (function, "O", &func))
    return NULL;

  index = PySequence_Index (callback_list, func);
  if (index < 0)
    Py_RETURN_NONE;

  if (PySequence_DelItem (callback_list, index) < 0)
    return NULL;

  Py_RETURN_NONE;
}
 int List::index(const Object &obj) const
 {
     return PySequence_Index(mPtr, obj.borrowReference());
 }
Beispiel #10
0
/*	Set item to LDAPEntry with a case-insensitive key. */
int
LDAPEntry_SetItem(LDAPEntry *self, PyObject *key, PyObject *value) {
	int found = 0;
	int rc = 0;
	int status = 1;
	char *newkey = lowercase(PyObject2char(key));
	LDAPValueList *list;

	/* Search for a match. */
	key = searchLowerCaseKeyMatch(self, key, &found);
	if (found == 1) {
		status = 2;
	}
	if (value != NULL) {
		/* If theres an item with a `dn` key, and with a string value set to the dn attribute. */
		if (strcmp(newkey, "dn") == 0) {
			if (PyUnicode_Check(value)) {
				char *dnstr = PyObject2char(value);
				LDAPEntry_SetStringDN(self, dnstr);
				free(dnstr);
			} else {
				PyErr_SetString(PyExc_TypeError, "Distinguished name must be string type.");
				Py_DECREF(key);
				return -1;
			}
		} else {
			/* Set the new value to the item. */
			if (LDAPValueList_Check(value) == 0) {
				/* Convert value to LDAPValueList object. */
				list = LDAPValueList_New();
				if (PyList_Check(value) || PyTuple_Check(value)) {
					LDAPValueList_Extend(list, value);
				} else {
					LDAPValueList_Append(list, value);
				}
				rc = PyDict_SetItem((PyObject *)self, key, (PyObject *)list);
				list->status = status;
				Py_DECREF(list);
			} else {
				rc = PyDict_SetItem((PyObject *)self, key, value);
				((LDAPValueList *)value)->status = status;
			}
			/* Avoid inconsistency. (same key in the added and the deleted list) */
			if (PySequence_Contains((PyObject *)self->deleted, key)) {
				if (UniqueList_Remove(self->deleted, key) != 0) return -1;
			}
			if (rc != 0) return rc;
			/* New key should be added to the attribute list. */
			if (found == 0) {
				if (UniqueList_Append(self->attributes, key) != 0) {
					Py_DECREF(key);
					return -1;
				}
			}
		}
	} else {
		/* This means, it has to remove the item. */
		if (PyDict_DelItem((PyObject *)self, key) != 0) return -1;
		if (UniqueList_Append(self->deleted, key) != 0) return -1;
		/* Remove from the attributes list. */
		if (PySequence_DelItem((PyObject *)self->attributes, PySequence_Index((PyObject *)self->attributes, key)) != 0) {
			Py_DECREF(key);
			return -1;
		}
	}
	Py_DECREF(key);
	return 0;
}
Beispiel #11
0
 /** tuple find a item.
  * @return index if found, -1 otherwise
  * @throw type_err
  */
 long find(const obj& o)const
 {
     long r = PySequence_Index(_p, o.p());
     return r;
 }