Ejemplo n.º 1
0
static int AerospikeClient_Type_Init(AerospikeClient * self, PyObject * args, PyObject * kwds)
{
	PyObject * py_config = NULL;

	static char * kwlist[] = {"config", NULL};

	if ( PyArg_ParseTupleAndKeywords(args, kwds, "O:client", kwlist, &py_config) == false ) {
		return -1;
	}

	if ( ! PyDict_Check(py_config) ) {
		return -1;
	}

	as_config config;
	as_config_init(&config);

	bool lua_system_path = FALSE;
	bool lua_user_path = FALSE;

	PyObject * py_lua = PyDict_GetItemString(py_config, "lua");
	if ( py_lua && PyDict_Check(py_lua) ) {

		PyObject * py_lua_system_path = PyDict_GetItemString(py_lua, "system_path");
		if ( py_lua_system_path && PyString_Check(py_lua_system_path) ) {
			lua_system_path = TRUE;
			memcpy(config.lua.system_path, PyString_AsString(py_lua_system_path), AS_CONFIG_PATH_MAX_LEN);
		}

		PyObject * py_lua_user_path = PyDict_GetItemString(py_lua, "user_path");
		if ( py_lua_user_path && PyString_Check(py_lua_user_path) ) {
			lua_user_path = TRUE;
			memcpy(config.lua.user_path, PyString_AsString(py_lua_user_path), AS_CONFIG_PATH_MAX_LEN);
		}

	}

	if ( ! lua_system_path ) {

		PyObject * py_prefix = PySys_GetObject("prefix");
		if ( py_prefix && PyString_Check(py_prefix) ) {
			char * prefix = PyString_AsString(py_prefix);
			size_t prefix_len = strlen(prefix);

			char system_path[AS_CONFIG_PATH_MAX_LEN] = {0};
			memcpy(system_path, prefix, strlen(prefix));
			memcpy(system_path + prefix_len, "/aerospike/lua", AS_CONFIG_PATH_MAX_LEN - prefix_len);
			system_path[prefix_len + strlen("/aerospike/lua")] = '\0';

			struct stat info;

			if( stat( system_path, &info ) == 0 && (info.st_mode & S_IFDIR) ) {
				memcpy(config.lua.system_path, system_path, AS_CONFIG_PATH_MAX_LEN);
			}
			else {
				memcpy(system_path + prefix_len, "/local/aerospike/lua", AS_CONFIG_PATH_MAX_LEN - prefix_len);
				system_path[prefix_len + strlen("/local/aerospike/lua")] = '\0';

				if( stat( system_path, &info ) == 0 && (info.st_mode & S_IFDIR) ) {
					memcpy(config.lua.system_path, system_path, AS_CONFIG_PATH_MAX_LEN);
				}
				else {
					config.lua.system_path[0] = '\0';
				}
			}
		}
	}

	if ( ! lua_user_path ) {
		memcpy(config.lua.user_path, ".", AS_CONFIG_PATH_MAX_LEN);
	}

	PyObject * py_hosts = PyDict_GetItemString(py_config, "hosts");
	if ( py_hosts && PyList_Check(py_hosts) ) {
		int size = (int) PyList_Size(py_hosts);
		for ( int i = 0; i < size && i < AS_CONFIG_HOSTS_SIZE; i++ ) {
			char *addr = NULL;
			uint16_t port = 3000;
			PyObject * py_host = PyList_GetItem(py_hosts, i);
			PyObject * py_addr, * py_port;

			if( PyTuple_Check(py_host) && PyTuple_Size(py_host) == 2) {

				py_addr = PyTuple_GetItem(py_host, 0);
				if(PyString_Check(py_addr)) {
					addr = strdup(PyString_AsString(py_addr));
				}
				py_port = PyTuple_GetItem(py_host,1);
				if( PyInt_Check(py_port) || PyLong_Check(py_port) ) {
					port = (uint16_t) PyLong_AsLong(py_port);
				}
				else {
					port = 0;
				}
			}
			else if ( PyString_Check(py_host) ) {
				addr = strdup( strtok( PyString_AsString(py_host), ":" ) );
				addr = strtok(addr, ":");
				char *temp = strtok(NULL, ":");
				if(NULL != temp) {
					port = (uint16_t)atoi(temp);
				}
			}
			as_config_add_host(&config, addr, port);
		}
	}

    PyObject * py_shm = PyDict_GetItemString(py_config, "shm");
    if (py_shm && PyDict_Check(py_shm) ) {

        config.use_shm = true;

        PyObject * py_shm_max_nodes = PyDict_GetItemString( py_shm, "shm_max_nodes" );
        if(py_shm_max_nodes && PyInt_Check(py_shm_max_nodes) ) {
            config.shm_max_nodes = PyInt_AsLong(py_shm_max_nodes);
        }

        PyObject * py_shm_max_namespaces = PyDict_GetItemString(py_shm, "shm_max_namespaces");
        if(py_shm_max_namespaces && PyInt_Check(py_shm_max_namespaces) ) {
            config.shm_max_namespaces = PyInt_AsLong(py_shm_max_namespaces);
        }

        PyObject* py_shm_takeover_threshold_sec = PyDict_GetItemString(py_shm, "shm_takeover_threshold_sec");
        if(py_shm_takeover_threshold_sec && PyInt_Check(py_shm_takeover_threshold_sec) ) {
            config.shm_takeover_threshold_sec = PyInt_AsLong( py_shm_takeover_threshold_sec);
        }
    }

    self->is_client_put_serializer = false;
    self->user_serializer_call_info.callback = NULL;
    self->user_deserializer_call_info.callback = NULL;
    PyObject *py_serializer_option = PyDict_GetItemString(py_config, "serialization");
    if (py_serializer_option && PyTuple_Check(py_serializer_option)) {
        PyObject *py_serializer = PyTuple_GetItem(py_serializer_option, 0);
        if (py_serializer && py_serializer != Py_None) {
            if (!PyCallable_Check(py_serializer)) {
                return -1;
            }
            memset(&self->user_serializer_call_info, 0, sizeof(self->user_serializer_call_info));
            self->user_serializer_call_info.callback = py_serializer;
        }
        PyObject *py_deserializer = PyTuple_GetItem(py_serializer_option, 1);
        if (py_deserializer && py_deserializer != Py_None) {
            if (!PyCallable_Check(py_deserializer)) {
                return -1;
            }
            memset(&self->user_deserializer_call_info, 0, sizeof(self->user_deserializer_call_info));
            self->user_deserializer_call_info.callback = py_deserializer;
        }
    }

	as_policies_init(&config.policies);

	PyObject * py_policies = PyDict_GetItemString(py_config, "policies");
	if ( py_policies && PyDict_Check(py_policies)) {
		//global defaults setting
		PyObject * py_key_policy = PyDict_GetItemString(py_policies, "key");
		if ( py_key_policy && PyInt_Check(py_key_policy) ) {
			config.policies.key = PyInt_AsLong(py_key_policy);
		}

		PyObject * py_timeout = PyDict_GetItemString(py_policies, "timeout");
		if ( py_timeout && PyInt_Check(py_timeout) ) {
			config.policies.timeout = PyInt_AsLong(py_timeout);
		}

		PyObject * py_retry = PyDict_GetItemString(py_policies, "retry");
		if ( py_retry && PyInt_Check(py_retry) ) {
			config.policies.retry = PyInt_AsLong(py_retry);
		}

		PyObject * py_exists = PyDict_GetItemString(py_policies, "exists");
		if ( py_exists && PyInt_Check(py_exists) ) {
			config.policies.exists = PyInt_AsLong(py_exists);
		}

		PyObject * py_replica = PyDict_GetItemString(py_policies, "replica");
		if ( py_replica && PyInt_Check(py_replica) ) {
			config.policies.replica = PyInt_AsLong(py_replica);
		}

		PyObject * py_consistency_level = PyDict_GetItemString(py_policies, "consistency_level");
		if ( py_consistency_level && PyInt_Check(py_consistency_level) ) {
			config.policies.consistency_level = PyInt_AsLong(py_consistency_level);
		}

		PyObject * py_commit_level = PyDict_GetItemString(py_policies, "commit_level");
		if ( py_commit_level && PyInt_Check(py_commit_level) ) {
			config.policies.commit_level = PyInt_AsLong(py_commit_level);
		}

		/*
		 * Generation policy is removed from constructor.
		 */
	}

	//conn_timeout_ms
	PyObject * py_connect_timeout = PyDict_GetItemString(py_config, "connect_timeout");
	if ( py_connect_timeout && PyInt_Check(py_connect_timeout) ) {
		config.conn_timeout_ms = PyInt_AsLong(py_connect_timeout);
	}

	self->as = aerospike_new(&config);

	return 0;
}
Ejemplo n.º 2
0
int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePyObject additional)
{
	int return_reason = 0;
		/* get current time */

	if (additional && !PyDict_Check(additional))
		eFatal("additional, but it's not dict");

	if (additional && !res)
		eFatal("additional, but no res");

	long poll_timeout = -1; /* infinite in case of empty timer list */

	{
		ePtrList<eTimer>::iterator it = m_timer_list.begin();
		if (it != m_timer_list.end())
		{
			eTimer *tmr = *it;
			timespec now; 
			clock_gettime(CLOCK_MONOTONIC, &now);
			/* process all timers which are ready. first remove them out of the list. */
			while (tmr->needsActivation(now))
			{
				m_timer_list.erase(it);
				tmr->AddRef();
				tmr->activate();
				tmr->Release();
				it = m_timer_list.begin();
				if (it == m_timer_list.end()) break;
				tmr = *it;
			}
			it = m_timer_list.begin();
			if (it != m_timer_list.end()) poll_timeout = timeout_usec((*it)->getNextActivation());
			if (poll_timeout < 0)
				poll_timeout = 0;
			else /* convert us to ms */
				poll_timeout /= 1000;
		}
	}

	if ((twisted_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > twisted_timeout))
	{
		poll_timeout = twisted_timeout;
		return_reason = 1;
	}

	int nativecount=notifiers.size(),
		fdcount=nativecount,
		ret=0;

	if (additional)
		fdcount += PyDict_Size(additional);

		// build the poll aray
	pollfd pfd[fdcount];  // make new pollfd array
	std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();

	int i=0;
	for (; i < nativecount; ++i, ++it)
	{
		it->second->state = 1; // running and in poll
		pfd[i].fd = it->first;
		pfd[i].events = it->second->getRequested();
	}

	if (additional)
	{
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
		typedef int Py_ssize_t;
# define PY_SSIZE_T_MAX INT_MAX
# define PY_SSIZE_T_MIN INT_MIN
#endif
		PyObject *key, *val;
		Py_ssize_t pos=0;
		while (PyDict_Next(additional, &pos, &key, &val)) {
			pfd[i].fd = PyObject_AsFileDescriptor(key);
			pfd[i++].events = PyInt_AsLong(val);
		}
	}

	m_is_idle = 1;
	++m_idle_count;

	if (this == eApp)
	{
		Py_BEGIN_ALLOW_THREADS
		ret = ::poll(pfd, fdcount, poll_timeout);
		Py_END_ALLOW_THREADS
	} else
PyObject *
convertEpetraMultiVectorToDistAarray(const Epetra_MultiVector & emv)
{
  // Initialization
  PyObject   * dap      = NULL;
  PyObject   * dim_data = NULL;
  PyObject   * dim_dict = NULL;
  PyObject   * size     = NULL;
  PyObject   * buffer   = NULL;
  Py_ssize_t   ndim     = 1;
  npy_intp     dims[3];

  // Get the underlying Epetra_BlockMap
  const Epetra_BlockMap & ebm = emv.Map();

  // Allocate the DistArray object and set the version key value
  dap = PyDict_New();
  if (!dap) goto fail;
  if (PyDict_SetItemString(dap,
                           "__version__",
                           PyString_FromString("0.9.0")) == -1) goto fail;

  // Get the Dimension Data and the number of dimensions.  If the
  // underlying Epetra_BlockMap has variable element sizes, an error
  // will be detected here.
  dim_data = convertEpetraBlockMapToDimData(ebm,
                                            emv.NumVectors());
  if (!dim_data) goto fail;
  ndim = PyTuple_Size(dim_data);

  // Assign the Dimension Data key value.
  if (PyDict_SetItemString(dap,
                           "dim_data",
                           dim_data) == -1) goto fail;

  // Extract the buffer dimensions from the Dimension Data, construct
  // the buffer and assign the buffer key value
  for (Py_ssize_t i = 0; i < ndim; ++i)
  {
    dim_dict = PyTuple_GetItem(dim_data, i);
    if (!dim_dict) goto fail;
    size = PyDict_GetItemString(dim_dict, "size");
    if (!size) goto fail;
    dims[i] = PyInt_AsLong(size);
    if (PyErr_Occurred()) goto fail;
  }
  buffer = PyArray_SimpleNewFromData(ndim,
                                     dims,
                                     NPY_DOUBLE,
                                     (void*)emv[0]);
  if (!buffer) goto fail;
  if (PyDict_SetItemString(dap,
                           "buffer",
                           buffer) == -1) goto fail;

  // Return the DistArray Protocol object
  return dap;

  // Error handling
  fail:
  Py_XDECREF(dap);
  Py_XDECREF(dim_data);
  Py_XDECREF(dim_dict);
  Py_XDECREF(buffer);
  return NULL;
}
Ejemplo n.º 4
0
static int child_init(int rank)
{
	PyObject *pFunc, *pArgs, *pValue, *pResult;
	int rval;
	char *classname;

	PyEval_AcquireLock();
	PyThreadState_Swap(myThreadState);

	// get instance class name
	classname = get_instance_class_name(_sr_apy_handler_obj);
	if (classname == NULL)
	{
		if (!PyErr_Occurred())
			PyErr_Format(PyExc_AttributeError, "'module' instance has no class name");
		python_handle_exception("child_init");
		Py_DECREF(format_exc_obj);
		PyThreadState_Swap(NULL);
		PyEval_ReleaseLock();
		return -1;
	}

	pFunc = PyObject_GetAttrString(_sr_apy_handler_obj, child_init_mname.s);

	if (pFunc == NULL) {
		python_handle_exception("child_init");
		Py_XDECREF(pFunc);
		Py_DECREF(format_exc_obj);
		PyThreadState_Swap(NULL);
		PyEval_ReleaseLock();
		return -1;
	}

	if (!PyCallable_Check(pFunc)) {
		if (!PyErr_Occurred())
			PyErr_Format(PyExc_AttributeError, "class object '%s' has is not callable attribute '%s'", !classname ? "None" : classname, mod_init_fname.s);
		python_handle_exception("child_init");
		Py_DECREF(format_exc_obj);
		Py_XDECREF(pFunc);
		PyThreadState_Swap(NULL);
		PyEval_ReleaseLock();
		return -1;
	}

	pArgs = PyTuple_New(1);
	if (pArgs == NULL) {
		python_handle_exception("child_init");
		Py_DECREF(format_exc_obj);
		Py_DECREF(pFunc);
		PyThreadState_Swap(NULL);
		PyEval_ReleaseLock();
		return -1;
	}

	pValue = PyInt_FromLong((long)rank);
	if (pValue == NULL) {
		python_handle_exception("child_init");
		Py_DECREF(format_exc_obj);
		Py_DECREF(pArgs);
		Py_DECREF(pFunc);
		PyThreadState_Swap(NULL);
		PyEval_ReleaseLock();
		return -1;
	}
	PyTuple_SetItem(pArgs, 0, pValue);
	/* pValue has been stolen */

	pResult = PyObject_CallObject(pFunc, pArgs);
	Py_DECREF(pFunc);
	Py_DECREF(pArgs);




	if (PyErr_Occurred()) {
		python_handle_exception("child_init");
		Py_DECREF(format_exc_obj);
		Py_XDECREF(pResult);
		PyThreadState_Swap(NULL);
		PyEval_ReleaseLock();
		return -1;
	}

	if (pResult == NULL) {
		LM_ERR("PyObject_CallObject() returned NULL but no exception!\n");
		PyThreadState_Swap(NULL);
		PyEval_ReleaseLock();
		return -1;
	}

	if (!PyInt_Check(pResult))
	{
		if (!PyErr_Occurred())
			PyErr_Format(PyExc_TypeError, "method '%s' of class '%s' should return 'int' type", child_init_mname.s, !classname ? "None" : classname);
		python_handle_exception("child_init");
		Py_DECREF(format_exc_obj);
		Py_XDECREF(pResult);
		PyThreadState_Swap(NULL);
		PyEval_ReleaseLock();
		return -1;
	}


	rval = PyInt_AsLong(pResult);
	Py_DECREF(pResult);
	PyThreadState_Swap(NULL);
	PyEval_ReleaseLock();

	return rval;
}
Ejemplo n.º 5
0
int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;
    int i;
    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyString_FromString(argv[1]);
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyInt_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return 1;
                }
                /* pValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
            }
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            if (pValue != NULL) {
                printf("Result of call: %ld\n", PyInt_AsLong(pValue));
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
                fprintf(stderr,"Call failed\n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    Py_Finalize();
    return 0;
}
Ejemplo n.º 6
0
static void gevent_loop() {

    if (!uwsgi.has_threads && uwsgi.mywid == 1) {
        uwsgi_log("!!! Running gevent without threads IS NOT recommended, enable them with --enable-threads !!!\n");
    }

    if (uwsgi.shared->options[UWSGI_OPTION_SOCKET_TIMEOUT] < 30) {
        uwsgi_log("!!! Running gevent with a socket-timeout lower than 30 seconds is not recommended, tune it with --socket-timeout !!!\n");
    }

    // get the GIL
    UWSGI_GET_GIL

    up.gil_get = gil_gevent_get;
    up.gil_release = gil_gevent_release;

    uwsgi.wait_write_hook = uwsgi_gevent_wait_write_hook;
    uwsgi.wait_read_hook = uwsgi_gevent_wait_read_hook;

    struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;

    if (uwsgi.async < 2) {
        uwsgi_log("the gevent loop engine requires async mode (--async <n>)\n");
        exit(1);
    }

    uwsgi.current_wsgi_req = uwsgi_gevent_current_wsgi_req;

    PyObject *gevent_dict = get_uwsgi_pydict("gevent");
    if (!gevent_dict) uwsgi_pyexit;

    PyObject *gevent_version = PyDict_GetItemString(gevent_dict, "version_info");
    if (!gevent_version) uwsgi_pyexit;

    if (PyInt_AsLong(PyTuple_GetItem(gevent_version, 0)) < 1) {
        uwsgi_log("uWSGI requires at least gevent 1.x version\n");
        exit(1);
    }

    ugevent.spawn = PyDict_GetItemString(gevent_dict, "spawn");
    if (!ugevent.spawn) uwsgi_pyexit;

    ugevent.signal = PyDict_GetItemString(gevent_dict, "signal");
    if (!ugevent.signal) uwsgi_pyexit;

    ugevent.greenlet_switch = PyDict_GetItemString(gevent_dict, "sleep");
    if (!ugevent.greenlet_switch) uwsgi_pyexit;

    ugevent.greenlet_switch_args = PyTuple_New(0);
    Py_INCREF(ugevent.greenlet_switch_args);


    PyObject *gevent_get_hub = PyDict_GetItemString(gevent_dict, "get_hub");

    ugevent.hub = python_call(gevent_get_hub, PyTuple_New(0), 0, NULL);
    if (!ugevent.hub) uwsgi_pyexit;

    ugevent.get_current = PyDict_GetItemString(gevent_dict, "getcurrent");
    if (!ugevent.get_current) uwsgi_pyexit;

    ugevent.get_current_args = PyTuple_New(0);
    Py_INCREF(ugevent.get_current_args);


    ugevent.hub_loop = PyObject_GetAttrString(ugevent.hub, "loop");
    if (!ugevent.hub_loop) uwsgi_pyexit;

    // main greenlet waiting for connection (one greenlet per-socket)
    PyObject *uwsgi_gevent_main = PyCFunction_New(uwsgi_gevent_main_def, NULL);
    Py_INCREF(uwsgi_gevent_main);

    // greenlet to run at each request
    PyObject *uwsgi_request_greenlet = PyCFunction_New(uwsgi_gevent_request_def, NULL);
    Py_INCREF(uwsgi_request_greenlet);

    // pre-fill the greenlet args
    ugevent.greenlet_args = PyTuple_New(2);
    PyTuple_SetItem(ugevent.greenlet_args, 0, uwsgi_request_greenlet);

    if (uwsgi.signal_socket > -1) {
        // and these are the watcher for signal sockets

        ugevent.signal_watcher = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", uwsgi.signal_socket, 1);
        if (!ugevent.signal_watcher) uwsgi_pyexit;

        ugevent.my_signal_watcher = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", uwsgi.my_signal_socket, 1);
        if (!ugevent.my_signal_watcher) uwsgi_pyexit;

        PyObject *uwsgi_greenlet_signal = PyCFunction_New(uwsgi_gevent_signal_def, NULL);
        Py_INCREF(uwsgi_greenlet_signal);

        PyObject *uwsgi_greenlet_my_signal = PyCFunction_New(uwsgi_gevent_my_signal_def, NULL);
        Py_INCREF(uwsgi_greenlet_my_signal);

        PyObject *uwsgi_greenlet_signal_handler = PyCFunction_New(uwsgi_gevent_signal_handler_def, NULL);
        Py_INCREF(uwsgi_greenlet_signal_handler);

        ugevent.signal_args = PyTuple_New(2);
        PyTuple_SetItem(ugevent.signal_args, 0, uwsgi_greenlet_signal_handler);

        // start the two signal watchers
        if (!PyObject_CallMethod(ugevent.signal_watcher, "start", "O", uwsgi_greenlet_signal)) uwsgi_pyexit;
        if (!PyObject_CallMethod(ugevent.my_signal_watcher, "start", "O", uwsgi_greenlet_my_signal)) uwsgi_pyexit;

    }

    // start a greenlet for each socket
    ugevent.watchers = uwsgi_malloc(sizeof(PyObject *) * uwsgi_count_sockets(uwsgi.sockets));
    int i = 0;
    while(uwsgi_sock) {
        // this is the watcher for server socket
        ugevent.watchers[i] = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", uwsgi_sock->fd, 1);
        if (!ugevent.watchers[i]) uwsgi_pyexit;

        // start the main greenlet
        PyObject_CallMethod(ugevent.watchers[i], "start", "Ol", uwsgi_gevent_main,(long)uwsgi_sock);
        uwsgi_sock = uwsgi_sock->next;
        i++;
    }

    // patch goodbye_cruel_world
    uwsgi.gbcw_hook = uwsgi_gevent_gbcw;

    // map SIGHUP with gevent.signal
    PyObject *ge_signal_tuple = PyTuple_New(2);
    PyTuple_SetItem(ge_signal_tuple, 0, PyInt_FromLong(SIGHUP));
    PyObject *uwsgi_gevent_unix_signal_handler = PyCFunction_New(uwsgi_gevent_unix_signal_handler_def, NULL);
    Py_INCREF(uwsgi_gevent_unix_signal_handler);
    PyTuple_SetItem(ge_signal_tuple, 1, uwsgi_gevent_unix_signal_handler);

    python_call(ugevent.signal, ge_signal_tuple, 0, NULL);

    for(;;) {
        if (!PyObject_CallMethod(ugevent.hub, "join", NULL)) {
            PyErr_Print();
        }
        else {
            break;
        }
    }

    if (uwsgi.workers[uwsgi.mywid].manage_next_request == 0) {
        uwsgi_log("goodbye to the gevent Hub on worker %d (pid: %d)\n", uwsgi.mywid, uwsgi.mypid);
        if (ugevent.destroy) {
            exit(0);
        }
        exit(UWSGI_RELOAD_CODE);
    }

    uwsgi_log("the gevent Hub is no more :(\n");

}
Ejemplo n.º 7
0
int uwsgi_python_spooler(char *filename, char *buf, uint16_t len, char *body, size_t body_len) {

	static int random_seed_reset = 0;

	UWSGI_GET_GIL;

	PyObject *spool_dict = PyDict_New();
	PyObject *spool_func, *pyargs, *ret;

	if (!random_seed_reset) {
		uwsgi_python_reset_random_seed();
		random_seed_reset = 1;	
	}

	if (!up.embedded_dict) {
		// ignore
		UWSGI_RELEASE_GIL;
		return 0;
	}

	spool_func = PyDict_GetItemString(up.embedded_dict, "spooler");
	if (!spool_func) {
		// ignore
		UWSGI_RELEASE_GIL;
		return 0;
	}

	if (uwsgi_hooked_parse(buf, len, uwsgi_python_add_item, spool_dict)) {
		// malformed packet, destroy it
		UWSGI_RELEASE_GIL;
		return -2;
	}

	pyargs = PyTuple_New(1);

	PyDict_SetItemString(spool_dict, "spooler_task_name", PyString_FromString(filename));

	if (body && body_len > 0) {
		PyDict_SetItemString(spool_dict, "body", PyString_FromStringAndSize(body, body_len));
	}
	PyTuple_SetItem(pyargs, 0, spool_dict);

	ret = python_call(spool_func, pyargs, 0, NULL);

	if (ret) {
		if (!PyInt_Check(ret)) {
			// error, retry
			UWSGI_RELEASE_GIL;
			return -1;
		}	

		int retval = (int) PyInt_AsLong(ret);
		UWSGI_RELEASE_GIL;
		return retval;
		
	}
	
	if (PyErr_Occurred())
		PyErr_Print();

	// error, retry
	UWSGI_RELEASE_GIL;
	return -1;
}
Ejemplo n.º 8
0
it is in 0.3 of the list\n\
\n\
";

static PyObject *clistfns_contents(PyObject *self, PyObject *args)
{
    int i;
    PyObject *items, *counts, *percentages;
    PyObject *countitems, *countitem;
    PyObject *key, *count, *perc;
    long c;
    double total;

    if(!PyArg_ParseTuple(args, "O", &items))
	return NULL;
    if(!PySequence_Check(items)) {
	PyErr_SetString(PyExc_TypeError, "expected mapping type");
	return NULL;
    }
    if((total = PySequence_Length(items)) == -1) {
	PyErr_SetString(PyExc_ValueError, "I couldn't get length of item.");
	return NULL;
    }
    
    counts = clistfns_count(self, args);
    if(!counts || PyErr_Occurred())
	return NULL;

    if(!(percentages = PyDict_New())) {
	Py_DECREF(counts);
	return NULL;
    }

    /* Loop through every element in counts, calculating the probabilities. */
    if(!(countitems = PyMapping_Items(counts))) {
	Py_DECREF(counts);
        Py_DECREF(percentages);
	return NULL;
    }

    /* Go through the loop, counting how often each item appears. */
    i = 0;
    while(1) {
	if(!(countitem = PyList_GetItem(countitems, i))) {
	    PyErr_Clear(); /* clear the exception set by PyList_GetItem */
	    break;         /* no more numbers */
        }
	key = PyTuple_GetItem(countitem, 0);
	count = PyTuple_GetItem(countitem, 1);
	c = PyInt_AsLong(count);
	perc = PyFloat_FromDouble((double)c / total);
	PyDict_SetItem(percentages, key, perc);
	Py_DECREF(perc);
	if(PyErr_Occurred())   /* PyDict_SetItem failed */
	    break;
	i++;
    }
    if(PyErr_Occurred()) {
	Py_DECREF(percentages);
	percentages = NULL;
    }
    Py_DECREF(countitems);
    Py_DECREF(counts);
    
    return percentages;
}
Ejemplo n.º 9
0
static int
parse_syntax_error(PyObject *err, PyObject **message, const char **filename,
		   int *lineno, int *offset, const char **text)
{
	long hold;
	PyObject *v;

	/* old style errors */
	if (PyTuple_Check(err))
		return PyArg_ParseTuple(err, "O(ziiz)", message, filename,
					lineno, offset, text);

	/* new style errors.  `err' is an instance */

	if (! (v = PyObject_GetAttrString(err, "msg")))
		goto finally;
	*message = v;

	if (!(v = PyObject_GetAttrString(err, "filename")))
		goto finally;
	if (v == Py_None)
		*filename = NULL;
	else if (! (*filename = PyString_AsString(v)))
		goto finally;

	Py_DECREF(v);
	if (!(v = PyObject_GetAttrString(err, "lineno")))
		goto finally;
	hold = PyInt_AsLong(v);
	Py_DECREF(v);
	v = NULL;
	if (hold < 0 && PyErr_Occurred())
		goto finally;
	*lineno = (int)hold;

	if (!(v = PyObject_GetAttrString(err, "offset")))
		goto finally;
	if (v == Py_None) {
		*offset = -1;
		Py_DECREF(v);
		v = NULL;
	} else {
		hold = PyInt_AsLong(v);
		Py_DECREF(v);
		v = NULL;
		if (hold < 0 && PyErr_Occurred())
			goto finally;
		*offset = (int)hold;
	}

	if (!(v = PyObject_GetAttrString(err, "text")))
		goto finally;
	if (v == Py_None)
		*text = NULL;
	else if (! (*text = PyString_AsString(v)))
		goto finally;
	Py_DECREF(v);
	return 1;

finally:
	Py_XDECREF(v);
	return 0;
}
Ejemplo n.º 10
0
gboolean BoolFromPython(PyObject * o, const char *key)
{
	char *s;
	int i;
	PyObject *o2;

	if (o == Py_None) {
		return FALSE;
	}

	if (!PyBool_Check(o)) {
		if (PyLong_Check(o)) {
			i = PyLong_AsLong(o);
			if (i == 0)
				return FALSE;
			else
				return TRUE;
		}
#if PY_MAJOR_VERSION < 3
		if (PyInt_Check(o)) {
			i = PyInt_AsLong(o);
			if (i == 0)
				return FALSE;
			else
				return TRUE;
		}
		if (PyString_Check(o)) {
			s = PyString_AsString(o);
			if (isdigit((int)s[0])) {
				i = atoi(s);
				if (i == 0)
					return FALSE;
				else
					return TRUE;
			} else if (strcasecmp(s, "yes") == 0) {
				return TRUE;
			} else if (strcasecmp(s, "true") == 0) {
				return TRUE;
			} else if (strcasecmp(s, "no") == 0) {
				return FALSE;
			} else if (strcasecmp(s, "false") == 0) {
				return FALSE;
			} else {
				PyErr_Format(PyExc_ValueError,
					     "String value of '%s' doesn't seem to be boolean",
					     key);
				return BOOL_INVALID;
			}
		}
#endif
		if (PyUnicode_Check(o)) {
			o2 = PyUnicode_AsASCIIString(o);
			if (o2 == NULL) {
				return BOOL_INVALID;
			}
			s = PyBytes_AsString(o2);
			if (isdigit((int)s[0])) {
				i = atoi(s);
				Py_DECREF(o2);
				if (i == 0)
					return FALSE;
				else
					return TRUE;
			} else if (strcasecmp(s, "yes") == 0) {
				Py_DECREF(o2);
				return TRUE;
			} else if (strcasecmp(s, "true") == 0) {
				Py_DECREF(o2);
				return TRUE;
			} else if (strcasecmp(s, "no") == 0) {
				Py_DECREF(o2);
				return FALSE;
			} else if (strcasecmp(s, "false") == 0) {
				Py_DECREF(o2);
				return FALSE;
			} else {
				Py_DECREF(o2);
				PyErr_Format(PyExc_ValueError,
					     "String value of '%s' doesn't seem to be boolean",
					     key);
				return BOOL_INVALID;
			}
		}

		PyErr_Format(PyExc_ValueError,
			     "Value of '%s' doesn't seem to be boolean", key);
		return BOOL_INVALID;
	}

	if (Py_False == o)
		return FALSE;
	else if (Py_True == o)
		return TRUE;

	PyErr_Format(PyExc_ValueError,
		     "Bool value of '%s' doesn't seem to be boolean", key);
	return BOOL_INVALID;
}
Ejemplo n.º 11
0
PyObject *py_uwsgi_gevent_request(PyObject * self, PyObject * args) {

	PyObject *py_wsgi_req = PyTuple_GetItem(args, 0);
	struct wsgi_request *wsgi_req = (struct wsgi_request *) PyLong_AsLong(py_wsgi_req);

	PyObject *greenlet_switch = NULL;

	PyObject *current_greenlet = GET_CURRENT_GREENLET;
	// another hack to retrieve the current wsgi_req;
	PyObject_SetAttrString(current_greenlet, "uwsgi_wsgi_req", py_wsgi_req);

	// if in edge-triggered mode read from socket now !!!
	if (wsgi_req->socket->edge_trigger) {
		int status = wsgi_req->socket->proto(wsgi_req);
		if (status < 0) {
			goto end;
		}
		goto request;
	}

	greenlet_switch = PyObject_GetAttrString(current_greenlet, "switch");

	for(;;) {
		int ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.socket_timeout);
                wsgi_req->switches++;

                if (ret <= 0) {
                        goto end;
                }

                int status = wsgi_req->socket->proto(wsgi_req);
                if (status < 0) {
                        goto end;
                }
                else if (status == 0) {
                        break;
                }
	}

request:

#ifdef UWSGI_ROUTING
	if (uwsgi_apply_routes(wsgi_req) == UWSGI_ROUTE_BREAK) {
		goto end;
	}
#endif

	for(;;) {
		if (uwsgi.p[wsgi_req->uh->modifier1]->request(wsgi_req) <= UWSGI_OK) {
			goto end;
		}
		wsgi_req->switches++;
		// switch after each yield
		GEVENT_SWITCH;
	}

end:
	if (greenlet_switch) {
		Py_DECREF(greenlet_switch);
	}

	Py_DECREF(current_greenlet);

	uwsgi_close_request(wsgi_req);
	free_req_queue;


	if (uwsgi.workers[uwsgi.mywid].manage_next_request == 0) {
		int running_cores = 0;
		int i;
                for(i=0;i<uwsgi.async;i++) {
                        if (uwsgi.workers[uwsgi.mywid].cores[i].in_request) {
                                running_cores++;
                        }
                }

                if (running_cores == 0) {
                        // no need to worry about freeing memory
                        PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi");
                        if (uwsgi_dict) {
                                PyObject *ae = PyDict_GetItemString(uwsgi_dict, "atexit");
                                if (ae) {
                                        python_call(ae, PyTuple_New(0), 0, NULL);
                                }
                        }
                }
        } else {
                // If we stopped any watcher due to being out of async workers, restart it.
                int i = 0;
                struct uwsgi_socket *uwsgi_sock = uwsgi.sockets;
                for (; uwsgi_sock; uwsgi_sock = uwsgi_sock->next, ++i) {
                        PyObject *py_watcher_active = PyObject_GetAttrString(ugevent.watchers[i], "active");
                        if (py_watcher_active && PyBool_Check(py_watcher_active) &&
                            !PyInt_AsLong(py_watcher_active)) {
                            start_watcher(i, uwsgi_sock);
                        }
                        Py_XDECREF(py_watcher_active);
                }
        }

	Py_INCREF(Py_None);
	return Py_None;

}
Ejemplo n.º 12
0
int GetIntFromDict(PyObject * dict, const char *key)
{
	PyObject *o;
	PyObject *o2;
	char *s;
	int i;

	o = PyDict_GetItemString(dict, key);
	if (o == NULL) {
		PyErr_Format(PyExc_ValueError, "Missing key in dictionary: %s",
			     key);
		return INT_INVALID;
	}

	if (PyLong_Check(o)) {
		/* Well we loose here something, but it is intentional :-) */
		return PyLong_AsLongLong(o);
	}

#if PY_MAJOR_VERSION < 3
	if (PyInt_Check(o)) {
		return PyInt_AsLong(o);
	}

	if (PyString_Check(o)) {
		s = PyString_AsString(o);
		if (isdigit((int)s[0])) {
			i = atoi(s);
			return i;
		} else {
			PyErr_Format(PyExc_ValueError,
				     "Value of '%s' doesn't seem to be integer",
				     key);
			return INT_INVALID;
		}
	}
#endif

	if (PyUnicode_Check(o)) {
		o2 = PyUnicode_AsASCIIString(o);
		if (o2 == NULL) {
			return INT_INVALID;
		}
		s = PyBytes_AsString(o2);
		if (isdigit((int)s[0])) {
			i = atoi(s);
			Py_DECREF(o2);
			return i;
		} else {
			Py_DECREF(o2);
			PyErr_Format(PyExc_ValueError,
				     "Value of '%s' doesn't seem to be integer",
				     key);
			return INT_INVALID;
		}
	}

	PyErr_Format(PyExc_ValueError,
		     "Value of '%s' doesn't seem to be integer", key);
	return INT_INVALID;
}
Ejemplo n.º 13
0
static PyObject* Level2User_Join(PyObject * self, PyObject * args){
  CThostFtdcLevel2UserApi * user = (CThostFtdcLevel2UserApi *) PyInt_AsLong(PyTuple_GET_ITEM(args, 0));
  PyObject * ret = Py_BuildValue("i", user->Join());
  return ret;
}
Ejemplo n.º 14
0
int RM_cmd(int args, char** argv) {
	printf("Ross-MacDonald\n");

	size_t const NB_ARGS = 12;

	PyObject *pName, *pModule, *pFunc;
	PyObject *pArgs, *pValue;

	if (args < NB_ARGS) {
		fprintf(stderr, "Not enough arguments\n");
		return -1;
	} else if (strcmp("rm", argv[0]) != 0) {
		fprintf(stderr, "Incorrect call\n");
		return -1;
	}

	Py_Initialize();

	PyRun_SimpleString("import sys, os");
	PyRun_SimpleString("sys.path.append('models')");

	pName = PyString_FromString("RM");

	pModule = PyImport_Import(pName);
	Py_DECREF(pName);

	if (pModule != NULL) {
		pFunc = PyObject_GetAttrString(pModule, "main");

		if (pFunc && PyCallable_Check(pFunc)) {
			pArgs = PyTuple_New(NB_ARGS - 1);

			for (int i = 0; i < NB_ARGS; ++i) {
				pValue = PyFloat_FromDouble(atof(argv[i]));
				if (!pValue) {
					Py_DECREF(pArgs);
					Py_DECREF(pModule);
					fprintf(stderr, "Error arguments\n");
					return -1;
				}
				PyTuple_SetItem(pArgs, i-1, pValue);
			}

			pValue = PyObject_CallObject(pFunc, pArgs);
			Py_DECREF(pArgs);
			if (pValue != NULL) {
				if (PyInt_AsLong(pValue) != 0) {
					printf("Call error\n");
				} else {
					printf("Call success\n");
				}
				Py_DECREF(pValue);
			}
			else {
				Py_DECREF(pFunc);
				Py_DECREF(pModule);
				PyErr_Print();
				fprintf(stderr,"Call failed\n");
				return -1;
			}
		}
		else {
			if (PyErr_Occurred())
				PyErr_Print();
			fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
		}
		Py_XDECREF(pFunc);
		Py_DECREF(pModule);
	}

	else {
		PyErr_Print();
		fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
		return 1;
	}

	Py_Finalize();

	return 0;
}
Ejemplo n.º 15
0
static PyObject* bignumpy(PyObject* self, PyObject* args) {
  const char* filename = NULL;
  PyObject* dtype = NULL;
  PyObject* shape = Py_None;
  int fd;
  PyArrayObject* z;
  PyArray_Descr* descr;
  int nd = 0;
  npy_intp dims[BIGNUMPY_MAXDIMS];
  npy_intp strides[BIGNUMPY_MAXDIMS];
  size_t nelm;
  int i;
  npy_intp stride;
  PyObject* obj;
  struct BignumpyObject* bno;

  if (!PyArg_ParseTuple(args,"sO|O",&filename,&dtype,&shape)) return NULL;

  // We actually start with the type.  If we can't figure out the size
  // it is useless to open the file.  dtype can be almost anything, so
  // here, we create an array of zeros to get information about the
  // type using the actual numpy interface
  z = /*owned*/ (PyArrayObject*)PyObject_CallFunctionObjArgs(ZEROS,EMPTY,dtype,NULL);
  if (!z) return NULL;
  Py_INCREF(descr = PyArray_DESCR(z));
  Py_DECREF(z); z = NULL;

  // OK, we can open the file.  If it does not exist, we may have to create it
  fd = open(filename,O_RDWR|O_CREAT,0666);
  if (fd < 0) {
    Py_DECREF(descr);
    return PyErr_SetFromErrnoWithFilename(PyExc_OSError,filename);
  }


  // Figure out the current size of the file.
  struct stat status;
  if (fstat(fd,&status) < 0) {
    Py_DECREF(descr);
    close(fd);
    return PyErr_SetFromErrnoWithFilename(PyExc_OSError,filename);
  }

  //If the size is zero and we have a shape,
  // then we'll use ftruncate to change the size.  If we have no shape,
  // assume shape is (size(file)/elsize,)
  if (shape == Py_None) {
    strides[nd] = descr->elsize;
    dims[nd++] = status.st_size/descr->elsize;
  } else {
    PyObject* iterator = PyObject_GetIter(shape);
    if (!iterator) {
      long v = PyInt_AsLong(shape);
      if (v == -1 && PyErr_Occurred()) {
	PyErr_SetString(PyExc_RuntimeError,"invalid shape");
	Py_DECREF(descr);
	close(fd);
	return NULL;
      }
      dims[nd++] = v;
    } else {
      PyObject* item;
      while((item = PyIter_Next(iterator))) {
	if (nd >= BIGNUMPY_MAXDIMS) {
	  Py_DECREF(iterator);
	  Py_DECREF(item);
	  Py_DECREF(descr);
	  close(fd);
	  PyErr_SetString(PyExc_RuntimeError,"shape has too many dimensions");
	  return NULL;
	}
	long v = PyInt_AsLong(item);
	if (v == -1 && PyErr_Occurred()) {
	  Py_DECREF(iterator);
	  Py_DECREF(item);
	  Py_DECREF(descr);
	  close(fd);
	  PyErr_SetString(PyExc_RuntimeError,"invalid shape");
	  return NULL;
	}
	strides[nd] = 1;
	dims[nd++] = v;
	Py_DECREF(item);
      }
      Py_DECREF(iterator);
    }
  }

  // ----------------------------------------------------------------------
  // Compute the number of required elements
  // ----------------------------------------------------------------------
  nelm = 0;
  if (nd > 0) {
    nelm = 1;
    for(i=0;i<nd;++i) nelm *= dims[i];
  }

  // ---------------------------------------------------------------------- 
  // The strides include the element size.  We compute from back to front
  // ----------------------------------------------------------------------
  stride = descr->elsize;
  for(i=0;i<nd;++i) {
    strides[nd-1-i] = stride;
    stride *= dims[nd-1-i];
  }

  // ----------------------------------------------------------------------
  // Grow (but do not shrink) to be the expected size
  // ----------------------------------------------------------------------
  off_t expected = nelm * descr->elsize;
  if (status.st_size < expected) {
    if (ftruncate(fd,expected) < 0) {
      Py_DECREF(descr);
      close(fd);
      return PyErr_SetFromErrnoWithFilename(PyExc_OSError,filename);
    }
  }

  // ----------------------------------------------------------------------
  // At this point, we can map the values into memory
  // ----------------------------------------------------------------------
  if (!expected) expected = 1; // mmap doesn't like 0 size
  void* m = mmap(NULL,expected,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
  if (m == MAP_FAILED) {
    Py_DECREF(descr);
    close(fd);
    return PyErr_SetFromErrnoWithFilename(PyExc_OSError,filename);
  }

  // ----------------------------------------------------------------------
  // Make a C object to hold the map state (pointer and size);
  // ----------------------------------------------------------------------

  obj = PyArray_NewFromDescr(
			      &Bignumpy_Type,
			      descr, /* steals reference */
			      nd,
			      dims,
			      strides,
			      m,
			      NPY_ARRAY_DEFAULT,
			      NULL);
  bno = (struct BignumpyObject*)obj;

  bno->fd = fd;
  bno->map = m;
  bno->size = expected;
  return obj;
}
Ejemplo n.º 16
0
/*
**	CALL - perform library call
*/
static object *
lib_call(amigalibobject *self, object *args)				// call
{
	object *dic;
	int LVO_value;
	ULONG regspec, orig_regspec;
	int pos=0;
	object *key, *value;

	/* the registers: */
	ULONG reg[16];

	if (!newgetargs(args, "(ii)O", &LVO_value, &regspec, &dic)) return NULL;

	if(!PyDict_Check(dic))
	{
		err_setstr(TypeError, "2nd arg must be dictionary");
		return NULL;
	}

	if(LVO_value>-30 || LVO_value%2)
	{
		err_setstr(ValueError, "illegal LVO value");
		return NULL;
	}

	if(regspec<0 || regspec >0xFFFF)
	{
		err_setstr(ValueError, "illegal regspec");
		return NULL;
	}
	
	orig_regspec=regspec;

	while(PyDict_Next(dic,&pos,&key,&value))
	{
		int regnr =  PyInt_AsLong(key);
		if(!PyInt_Check(key) || regnr<0 || regnr>15)
		{
			err_setstr(ValueError, "illegal key/regnr.");
			return NULL;
		}
		else
		{
			ULONG regval;
			if(!obj2reg(value,&regval))
			{
				err_setstr(ValueError, "illegal register value");
				return NULL;
			}

			/* OK: the register `regnr' must get the value `regval' */
			/* Check if register regnr indeed must get a value */
			if(regspec & (1<<regnr))
			{
				/* Yes, put value in register & clear register bit */
				reg[regnr]=regval;
				regspec &= ~(1<<regnr);
			}
			else
			{
				err_setstr(ValueError, "registers not consistent with LVO spec");
				return NULL;
			}
		}
	}

	/* Are there still registers to assign a value to? */
	if(regspec)
	{
		err_setstr(ValueError, "too few arguments provided");
		return NULL;
	}

	/* XXXX TODO: SHOULD PASS ARGUMENTS ALSO */
	return newintobject(
			amigalibs_calllib(self->libbase,
				LVO_value,orig_regspec,reg));
}
Ejemplo n.º 17
0
/**
 * \ingroup python_interface_edgeseq
 * \brief Selects a subset of the edge sequence based on some criteria
 */
PyObject* igraphmodule_EdgeSeq_select(igraphmodule_EdgeSeqObject *self, PyObject *args) {
  igraphmodule_EdgeSeqObject *result;
  igraphmodule_GraphObject *gr;
  long i, j, n, m;

  gr=self->gref;
  result=igraphmodule_EdgeSeq_copy(self);
  if (result == 0)
    return NULL;

  /* First, filter by positional arguments */
  n = PyTuple_Size(args);
  for (i=0; i<n; i++) {
    PyObject *item = PyTuple_GET_ITEM(args, i);
    if (item == Py_None) {
      /* None means: select nothing */
      igraph_es_destroy(&result->es);
      igraph_es_none(&result->es);
      /* We can simply bail out here */
      return (PyObject*)result;
    } else if (PyCallable_Check(item)) {
      /* Call the callable for every edge in the current sequence to
       * determine what's up */
      igraph_bool_t was_excluded = 0;
      igraph_vector_t v;

      if (igraph_vector_init(&v, 0)) {
        igraphmodule_handle_igraph_error();
        return 0;
      }

      m = PySequence_Size((PyObject*)result);
      for (j=0; j<m; j++) {
        PyObject *edge = PySequence_GetItem((PyObject*)result, j);
        PyObject *call_result;
        if (edge == 0) {
          Py_DECREF(result);
          igraph_vector_destroy(&v);
          return NULL;
        }
        call_result = PyObject_CallFunctionObjArgs(item, edge, NULL);
        if (call_result == 0) {
          Py_DECREF(edge); Py_DECREF(result);
          igraph_vector_destroy(&v);
          return NULL;
        }
        if (PyObject_IsTrue(call_result))
          igraph_vector_push_back(&v,
            igraphmodule_Edge_get_index_long((igraphmodule_EdgeObject*)edge));
        else was_excluded=1;
        Py_DECREF(call_result);
        Py_DECREF(edge);
      }

      if (was_excluded) {
        igraph_es_destroy(&result->es);
        if (igraph_es_vector_copy(&result->es, &v)) {
          Py_DECREF(result);
          igraph_vector_destroy(&v);
          igraphmodule_handle_igraph_error();
          return NULL;
        }
      }

      igraph_vector_destroy(&v);
    } else if (PyInt_Check(item)) {
      /* Integers are treated specially: from now on, all remaining items
       * in the argument list must be integers and they will be used together
       * to restrict the edge set. Integers are interpreted as indices on the
       * edge set and NOT on the original, untouched edge sequence of the
       * graph */
      igraph_vector_t v, v2;
      if (igraph_vector_init(&v, 0)) {
        igraphmodule_handle_igraph_error();
        return 0;
      }
      if (igraph_vector_init(&v2, 0)) {
        igraph_vector_destroy(&v);
        igraphmodule_handle_igraph_error();
        return 0;
      }
      if (igraph_es_as_vector(&gr->g, self->es, &v2)) {
        igraph_vector_destroy(&v);
        igraph_vector_destroy(&v2);
        igraphmodule_handle_igraph_error();
        return 0;
      }
      m = igraph_vector_size(&v2);
      for (; i<n; i++) {
        PyObject *item2 = PyTuple_GET_ITEM(args, i);
        long idx;
        if (!PyInt_Check(item2)) {
          Py_DECREF(result);
          PyErr_SetString(PyExc_TypeError, "edge indices expected");
          igraph_vector_destroy(&v);
          igraph_vector_destroy(&v2);
          return NULL;
        }
        idx = PyInt_AsLong(item2);
        if (idx >= m || idx < 0) {
          PyErr_SetString(PyExc_ValueError, "edge index out of range");
          igraph_vector_destroy(&v);
          igraph_vector_destroy(&v2);
          return NULL;
        }
        if (igraph_vector_push_back(&v, VECTOR(v2)[idx])) {
          Py_DECREF(result);
          igraphmodule_handle_igraph_error();
          igraph_vector_destroy(&v);
          igraph_vector_destroy(&v2);
          return NULL;
        }
      }
      igraph_vector_destroy(&v2);
      igraph_es_destroy(&result->es);
      if (igraph_es_vector_copy(&result->es, &v)) {
        Py_DECREF(result);
        igraphmodule_handle_igraph_error();
        igraph_vector_destroy(&v);
        return NULL;
      }
      igraph_vector_destroy(&v);
    } else {
      /* Iterators and everything that was not handled directly */
      PyObject *iter, *item2;
      igraph_vector_t v, v2;
      
      /* Allocate stuff */
      if (igraph_vector_init(&v, 0)) {
        igraphmodule_handle_igraph_error();
        return 0;
      }
      if (igraph_vector_init(&v2, 0)) {
        igraph_vector_destroy(&v);
        igraphmodule_handle_igraph_error();
        return 0;
      }
      if (igraph_es_as_vector(&gr->g, self->es, &v2)) {
        igraph_vector_destroy(&v);
        igraph_vector_destroy(&v2);
        igraphmodule_handle_igraph_error();
        return 0;
      }
      m = igraph_vector_size(&v2);

      /* Create an appropriate iterator */
      if (PySlice_Check(item)) {
        /* Create an iterator from the slice (which is not iterable by default )*/
        Py_ssize_t start, stop, step, sl;
        PyObject* range;
        igraph_bool_t ok;

        /* Casting to void* because Python 2.x expects PySliceObject*
         * but Python 3.x expects PyObject* */
        ok = (PySlice_GetIndicesEx((void*)item, igraph_vector_size(&v2),
              &start, &stop, &step, &sl) == 0);
        if (ok) {
          range = igraphmodule_PyRange_create(start, stop, step);
          ok = (range != 0);
        }
        if (ok) {
          iter = PyObject_GetIter(range);
          Py_DECREF(range);
          ok = (iter != 0);
        }
        if (!ok) {
          igraph_vector_destroy(&v);
          igraph_vector_destroy(&v2);
          PyErr_SetString(PyExc_TypeError, "error while converting slice to iterator");
          Py_DECREF(result);
          return 0;
        }
      } else {
        /* Simply create the iterator corresponding to the object */
        iter = PyObject_GetIter(item);
      }

      /* Did we manage to get an iterator? */
      if (iter == 0) {
        igraph_vector_destroy(&v);
        igraph_vector_destroy(&v2);
        PyErr_SetString(PyExc_TypeError, "invalid edge filter among positional arguments");
        Py_DECREF(result);
        return 0;
      }
      /* Do the iteration */
      while ((item2=PyIter_Next(iter)) != 0) {
        if (PyInt_Check(item2)) {
          long idx = PyInt_AsLong(item2);
          Py_DECREF(item2);
          if (idx >= m || idx < 0) {
            PyErr_SetString(PyExc_ValueError, "edge index out of range");
            Py_DECREF(result);
            Py_DECREF(iter);
            igraph_vector_destroy(&v);
            igraph_vector_destroy(&v2);
            return NULL;
          }
          if (igraph_vector_push_back(&v, VECTOR(v2)[idx])) {
            Py_DECREF(result);
            Py_DECREF(iter);
            igraphmodule_handle_igraph_error();
            igraph_vector_destroy(&v);
            igraph_vector_destroy(&v2);
            return NULL;
          }
        } else {
          /* We simply ignore elements that we don't know */
          Py_DECREF(item2);
        }
      }
      /* Deallocate stuff */
      igraph_vector_destroy(&v2);
      Py_DECREF(iter);
      if (PyErr_Occurred()) {
        igraph_vector_destroy(&v);
        Py_DECREF(result);
        return 0;
      }
      igraph_es_destroy(&result->es);
      if (igraph_es_vector_copy(&result->es, &v)) {
        Py_DECREF(result);
        igraphmodule_handle_igraph_error();
        igraph_vector_destroy(&v);
        return NULL;
      }
      igraph_vector_destroy(&v);
    }
  }

  return (PyObject*)result;
}
Ejemplo n.º 18
0
long int ofxPythonObject::asInt() const
{
	if (isInt())
		return PyInt_AsLong(get()->obj);
	return 0;
}
Ejemplo n.º 19
0
static gboolean
marshal_value(CORBA_TypeCode tc, gconstpointer *val, PyObject *value)
{
    gboolean ret = FALSE;
    gint i;

    while (tc->kind == CORBA_tk_alias)
	tc = tc->subtypes[0];

    switch (tc->kind) {
    case CORBA_tk_null:
    case CORBA_tk_void:
	ret = (value == Py_None);
	break;
    case CORBA_tk_short:
	alignval(val, ORBIT_ALIGNOF_CORBA_SHORT);
	getval(val, CORBA_short) = PyInt_AsLong(value);
	advanceptr(val, sizeof(CORBA_short));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_long:
	alignval(val, ORBIT_ALIGNOF_CORBA_LONG);
	getval(val, CORBA_long) = PyInt_AsLong(value);
	advanceptr(val, sizeof(CORBA_long));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_ushort:
	alignval(val, ORBIT_ALIGNOF_CORBA_SHORT);
	getval(val, CORBA_unsigned_short) = PyInt_AsLong(value);
	advanceptr(val, sizeof(CORBA_unsigned_short));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_ulong:
    case CORBA_tk_enum:
	alignval(val, ORBIT_ALIGNOF_CORBA_LONG);
	if (PyLong_Check(value))
	    getval(val, CORBA_unsigned_long) = PyLong_AsUnsignedLong(value);
	else
	    getval(val, CORBA_unsigned_long) = PyInt_AsLong(value);
	advanceptr(val, sizeof(CORBA_unsigned_long));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_float:
	alignval(val, ORBIT_ALIGNOF_CORBA_FLOAT);
	getval(val, CORBA_float) = PyFloat_AsDouble(value);
	advanceptr(val, sizeof(CORBA_float));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_double:
	alignval(val, ORBIT_ALIGNOF_CORBA_DOUBLE);
	getval(val, CORBA_double) = PyFloat_AsDouble(value);
	advanceptr(val, sizeof(CORBA_double));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_boolean:
	getval(val, CORBA_boolean) = PyObject_IsTrue(value);
	advanceptr(val, sizeof(CORBA_boolean));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_char:
	if (PyString_Check(value) && PyString_Size(value) == 1) {
	    getval(val, CORBA_char) = PyString_AsString(value)[0];
	    advanceptr(val, sizeof(CORBA_char));
	    ret = TRUE;
	}
	break;
    case CORBA_tk_octet:
	getval(val, CORBA_long) = PyInt_AsLong(value);
	advanceptr(val, sizeof(CORBA_octet));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_any:
	if (PyObject_TypeCheck(value, &PyCORBA_Any_Type)) {
	    alignval(val, ORBIT_ALIGNOF_CORBA_ANY);
	    CORBA_any__copy(&getval(val, CORBA_any),
			    &((PyCORBA_Any *)value)->any);
	    advanceptr(val, sizeof(CORBA_any));
	    ret = TRUE;
	}
	break;
    case CORBA_tk_TypeCode:
	alignval(val, ORBIT_ALIGNOF_CORBA_POINTER);
	if (PyObject_TypeCheck(value, &PyCORBA_TypeCode_Type)) {
	    getval(val, CORBA_TypeCode) =
		(CORBA_TypeCode)CORBA_Object_duplicate(
			(CORBA_Object)((PyCORBA_TypeCode *)value)->tc, NULL);
	    ret = TRUE;
	}
	advanceptr(val, sizeof(CORBA_TypeCode));
	break;
    case CORBA_tk_Principal:
	g_warning("can't marshal Principal");
	break;
    case CORBA_tk_objref:
	alignval(val, ORBIT_ALIGNOF_CORBA_POINTER);
	if (value == Py_None) {
	    getval(val, CORBA_Object) = CORBA_OBJECT_NIL;
	    ret = TRUE;
	} else if (PyObject_TypeCheck(value, &PyCORBA_Object_Type)) {
	    CORBA_Object objref = ((PyCORBA_Object *)value)->objref;
	    getval(val, CORBA_Object) = CORBA_Object_duplicate(objref, NULL);
	    advanceptr(val, sizeof(CORBA_Object));
	    ret = TRUE;
	}
	break;
    case CORBA_tk_except:
    case CORBA_tk_struct:
	alignval(val, tc->c_align);
	for (i = 0; i < tc->sub_parts; i++) {
	    gchar *pyname;
	    PyObject *item;
	    gboolean itemret;

	    pyname = _pyorbit_escape_name(tc->subnames[i]);
	    item = PyObject_GetAttrString(value, pyname);
	    g_free(pyname);
	    if (!item) break;
	    itemret = marshal_value(tc->subtypes[i], val, item);
	    Py_DECREF(item);
	    if (!itemret) break;
	}
	if (i == tc->sub_parts) ret = TRUE; /* no error */
	break;
    case CORBA_tk_union: {
	PyObject *discrim, *subval;
	CORBA_TypeCode subtc;
	gconstpointer body;
	int sz = 0;

	discrim = PyObject_GetAttrString(value, "_d");
	if (!discrim) break;
	subval = PyObject_GetAttrString(value, "_v");
	if (!subval) break;

	alignval(val, MAX(tc->c_align, tc->discriminator->c_align));
	ret = marshal_value(tc->discriminator, val, discrim);
	if (!ret) {
	    Py_DECREF(discrim);
	    Py_DECREF(subval);
	    break;
	}

	/* calculate allocation info */
	for (i = 0; i < tc->sub_parts; i++) {
	    sz = MAX(sz, ORBit_gather_alloc_info(tc->subtypes[i]));
	}
	subtc = get_union_tc(tc, discrim);
	Py_DECREF(discrim);
	if (!subtc) {
	    Py_DECREF(subval);
	    break;
	}

	alignval(val, tc->c_align);
	body = *val;

	ret = marshal_value(subtc, &body, subval);
	Py_DECREF(subval);
	advanceptr(val, sz);
	break;
    }
    case CORBA_tk_string:
	if (PyString_Check(value)) {
	    /* error out if python string is longer than the bound
	     * specified in the string typecode. */
	    if (tc->length > 0 && PyString_Size(value) > tc->length)
		break;

	    alignval(val, ORBIT_ALIGNOF_CORBA_POINTER);
	    getval(val, CORBA_string) = CORBA_string_dup(PyString_AsString(value));
	    advanceptr(val, sizeof(CORBA_char *));
	    ret = TRUE;
	}
	break;
    case CORBA_tk_sequence: 
	if (PySequence_Check(value)) {
	    CORBA_sequence_CORBA_octet *sval;
	    gconstpointer seqval;
	    gint i;

	    /* error out if python sequence is longer than the bound
	     * specified in the sequence typecode. */
	    if (tc->length > 0 && PySequence_Length(value) > tc->length)
		break;

	    alignval(val, ORBIT_ALIGNOF_CORBA_SEQ);
	    sval = (CORBA_sequence_CORBA_octet *)*val;
	    if (CORBA_TypeCode_equal(tc->subtypes[0], TC_CORBA_octet, NULL)
		&& PyString_Check(value))
	    {
		Py_ssize_t length;
		char *buffer;
		if (PyString_AsStringAndSize(value, &buffer, &length) == -1)
		    ret = FALSE;
		else {
		    sval->_release = CORBA_TRUE;
		    sval->_buffer  = ORBit_alloc_tcval(TC_CORBA_octet, length);
		    memcpy(sval->_buffer, buffer, length);
		    sval->_length  = length;
		    ret = TRUE;
		}
	    } else {
		sval->_release = TRUE;
		sval->_length = PySequence_Length(value);
		sval->_buffer = ORBit_alloc_tcval(tc->subtypes[0], sval->_length);
		seqval = sval->_buffer;
		for (i = 0; i < sval->_length; i++) {
		    PyObject *item = PySequence_GetItem(value, i);
		    gboolean itemret;
		    
		    if (!item) break;
		    itemret = marshal_value(tc->subtypes[0], &seqval, item);
		    Py_DECREF(item);
		    if (!itemret) break;
		}
		if (i == sval->_length) ret = TRUE; /* no error */
	    }
	    advanceptr(val, sizeof(CORBA_sequence_CORBA_octet));
	}
	break;
    case CORBA_tk_array:
	if (PySequence_Check(value) &&
	    PySequence_Length(value) == tc->length) {
	    gint i;

	    for (i = 0; i < tc->length; i++) {
		PyObject *item = PySequence_GetItem(value, i);
		gboolean itemret;

		if (!item) break;
		itemret = marshal_value(tc->subtypes[0], val, item);
		Py_DECREF(item);
		if (!itemret) break;
	    }
	    if (i == tc->length) ret = TRUE; /* no error */
	}
	break;
    case CORBA_tk_longlong:
	alignval(val, ORBIT_ALIGNOF_CORBA_LONG_LONG);
	if (PyLong_Check(value))
	    getval(val, CORBA_long_long) = PyLong_AsLongLong(value);
	else
	    getval(val, CORBA_long_long) = PyInt_AsLong(value);
	advanceptr(val, sizeof(CORBA_long_long));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_ulonglong:
	alignval(val, ORBIT_ALIGNOF_CORBA_LONG_LONG);
	if (PyLong_Check(value))
	    getval(val, CORBA_unsigned_long_long) =PyLong_AsUnsignedLongLong(value);
	else
	    getval(val, CORBA_unsigned_long_long) = PyInt_AsLong(value);
	advanceptr(val, sizeof(CORBA_unsigned_long_long));
	if (!PyErr_Occurred()) ret = TRUE;
	break;
    case CORBA_tk_longdouble:
	g_warning("can't marshal long double");
	break;
    case CORBA_tk_wchar:
	if (PyUnicode_Check(value) && PyUnicode_GetSize(value) == 1) {
	    alignval(val, ORBIT_ALIGNOF_CORBA_SHORT);
	    getval(val, CORBA_wchar) = PyUnicode_AsUnicode(value)[0];
	    advanceptr(val, sizeof(CORBA_wchar));
	    ret = TRUE;
	}
	break;
    case CORBA_tk_wstring:
	if (PyUnicode_Check(value)) {
	    int length = PyUnicode_GetSize(value);
	    Py_UNICODE *unicode = PyUnicode_AsUnicode(value);
	    CORBA_wchar *wstr;

	    /* error out if python unicode string is longer than the
	     * bound specified in the wstring typecode. */
	    if (tc->length > 0 && length > tc->length)
		break;

	    alignval(val, ORBIT_ALIGNOF_CORBA_POINTER);
	    wstr = CORBA_wstring_alloc(length);
	    getval(val, CORBA_wstring) = wstr;
	    for (i = 0; i < length; i++) {
		wstr[i] = unicode[i];
	    }
	    advanceptr(val, sizeof(CORBA_wchar *));
	    ret = TRUE;
	}
	break;
    default:
	g_warning("unhandled typecode: %s (kind=%d)", tc->repo_id, tc->kind);
	break;
    }
    if (!ret)
	PyErr_Clear();
    return ret;
}
Ejemplo n.º 20
0
QList<PythonVariable> PythonEngine::variableList()
{
    QStringList filter_name;
    filter_name << "__builtins__"
                << "StdoutCatcher"
                << "python_engine_stdout"
                << "result_pythonlab"
                << "python_engine_get_completion_interpreter"
                << "python_engine_get_completion_script"
                << "PythonLabRopeProject"
                << "pythonlab_rope_project"
                << "python_engine_pyflakes_check"
                << "CatchOutErr"
                << "agros2d_material_eval"
                << "chart"
                << "memory_chart"
                << "sgn"
                << "test"
                << "vtk_contours_actor"
                << "vtk_figure"
                << "vtk_geometry_actor"
                << "vtk_scalar_actor"
                << "help"
                << "__a2d_help__";

    QStringList filter_type;
    filter_type << "builtin_function_or_method";

    QList<PythonVariable> list;

    PyObject *keys = PyDict_Keys(m_dict);
    for (int i = 0; i < PyList_Size(keys); ++i)
    {
        PyObject *key = PyList_GetItem(keys, i);
        PyObject *value = PyDict_GetItem(m_dict, key);

        // variable
        PythonVariable var;

        // variable name
        var.name = PyString_AsString(key);

        // variable type
        var.type = value->ob_type->tp_name;

        // variable value
        if (var.type == "bool")
        {
            var.value = PyInt_AsLong(value) ? "True" : "False";
        }
        else if (var.type == "int")
        {
            var.value = (int) PyInt_AsLong(value);
        }
        else if (var.type == "float")
        {
            var.value = PyFloat_AsDouble(value);
        }
        else if (var.type == "str")
        {
            var.value = PyString_AsString(value);
        }
        else if (var.type == "list")
        {
            var.value = QString("%1 items").arg(PyList_Size(value));
        }
        else if (var.type == "tuple")
        {
            var.value = QString("%1 items").arg(PyTuple_Size(value));
        }
        else if (var.type == "dict")
        {
            var.value = QString("%1 items").arg(PyDict_Size(value));
        }
        else if (var.type == "numpy.ndarray")
        {
            var.value = ""; //TODO count
        }
        else if (var.type == "module")
        {
            var.value = PyString_AsString(PyObject_GetAttrString(value, "__name__"));
        }
        else if (var.type == "function"
                 || var.type == "instance"
                 || var.type == "classobj")
        {
            // qDebug() << value->ob_type->tp_name;
        }

        // append
        if (!filter_name.contains(var.name) && !filter_type.contains(var.type))
        {
            list.append(var);
        }
    }
    Py_DECREF(keys);

    return list;
}
Ejemplo n.º 21
0
struct pcmreader_s*
open_pcmreader(PyObject* pcmreader_obj)
{
    struct pcmreader_s* pcmreader = malloc(sizeof(struct pcmreader_s));
    PyObject* attr;
    PyObject* audiotools_pcm;

    /*setup some placeholder values*/
    pcmreader->pcmreader_obj = NULL;
    pcmreader->framelist_type = NULL;
    pcmreader->sample_rate = 0;
    pcmreader->channels = 0;
    pcmreader->channel_mask = 0;
    pcmreader->bits_per_sample = 0;
    pcmreader->bytes_per_sample = 0;
    pcmreader->callbacks = NULL;

    pcmreader->read = pcmreader_read;
    pcmreader->close = pcmreader_close;
    pcmreader->add_callback = pcmreader_add_callback;
    pcmreader->del = pcmreader_del;

    /*attempt to extract proper values from the pcmreader_obj*/
    if ((attr = PyObject_GetAttrString(pcmreader_obj,
                                       "sample_rate")) == NULL)
        goto error;
    pcmreader->sample_rate = (unsigned int)PyInt_AsLong(attr);
    Py_DECREF(attr);
    if (PyErr_Occurred())
        goto error;

    if ((attr = PyObject_GetAttrString(pcmreader_obj,
                                       "bits_per_sample")) == NULL)
        goto error;
    pcmreader->bits_per_sample = (unsigned int)PyInt_AsLong(attr);
    Py_DECREF(attr);
    if (PyErr_Occurred())
        goto error;

    if ((attr = PyObject_GetAttrString(pcmreader_obj,
                                       "channels")) == NULL)
        goto error;
    pcmreader->channels = (unsigned int)PyInt_AsLong(attr);
    Py_DECREF(attr);
    if (PyErr_Occurred())
        goto error;

    if ((attr = PyObject_GetAttrString(pcmreader_obj,
                                       "channel_mask")) == NULL)
        goto error;
    pcmreader->channel_mask = (unsigned int)PyInt_AsLong(attr);
    Py_DECREF(attr);
    if (PyErr_Occurred())
        goto error;

    pcmreader->bytes_per_sample = pcmreader->bits_per_sample / 8;

    /*attach and incref the wrapped PCMReader object*/
    pcmreader->pcmreader_obj = pcmreader_obj;
    Py_INCREF(pcmreader_obj);

    /*attach a pcm.FrameList type object for verification during reads*/
    if ((audiotools_pcm = PyImport_ImportModule("audiotools.pcm")) == NULL) {
        goto error;
    }

    pcmreader->framelist_type = PyObject_GetAttrString(audiotools_pcm,
                                                       "FrameList");

    Py_DECREF(audiotools_pcm);

    return pcmreader;

 error:
    Py_XDECREF(pcmreader->pcmreader_obj);
    Py_XDECREF(pcmreader->framelist_type);
    free(pcmreader);
    return NULL;
}
Ejemplo n.º 22
0
/**
 * \ingroup python_interface_vertexseq
 * \brief Selects a single vertex from the vertex sequence based on some criteria
 */
PyObject* igraphmodule_VertexSeq_find(igraphmodule_VertexSeqObject *self, PyObject *args) {
  PyObject *item;
  igraph_integer_t i;
  Py_ssize_t n;
  igraph_vit_t vit;

  if (!PyArg_ParseTuple(args, "O", &item))
    return NULL;

  if (PyCallable_Check(item)) {
    /* Call the callable for every vertex in the current sequence and return
     * the first one for which it evaluates to True */
    n = PySequence_Size((PyObject*)self);
    for (i=0; i<n; i++) {
      PyObject *vertex = PySequence_GetItem((PyObject*)self, i);
      PyObject *call_result;
      if (vertex == 0)
        return NULL;
      call_result = PyObject_CallFunctionObjArgs(item, vertex, NULL);
      if (call_result == 0) {
        Py_DECREF(vertex);
        return NULL;
      }
      if (PyObject_IsTrue(call_result)) {
        Py_DECREF(call_result);
        return vertex;  /* reference passed to caller */
      }
      Py_DECREF(call_result);
      Py_DECREF(vertex);
    }
  } else if (PyInt_Check(item)) {
    /* Integers are interpreted as indices on the vertex set and NOT on the
     * original, untouched vertex sequence of the graph */
    return PySequence_GetItem((PyObject*)self, PyInt_AsLong(item));
  } else if (PyBaseString_Check(item)) {
    /* Strings are interpreted as vertex names */
    if (igraphmodule_get_vertex_id_by_name(&self->gref->g, item, &i))
      return NULL;

    /* We now have the ID of the vertex in the graph. If the vertex sequence
     * itself represents the full vertex sequence of the graph, we can return
     * here. If not, we have to check whether the vertex sequence contains this
     * ID or not. */
    if (igraph_vs_is_all(&self->vs))
      return PySequence_GetItem((PyObject*)self, i);

    if (igraph_vit_create(&self->gref->g, self->vs, &vit)) {
      igraphmodule_handle_igraph_error();
      return NULL;
    }

    for (n = 0; !IGRAPH_VIT_END(vit); IGRAPH_VIT_NEXT(vit), n++) {
      if (IGRAPH_VIT_GET(vit) == i) {
        igraph_vit_destroy(&vit);
        return PySequence_GetItem((PyObject*)self, n);
      }
    }

    igraph_vit_destroy(&vit);
    PyErr_SetString(PyExc_ValueError, "vertex with the given name exists but not in the current sequence");
    return NULL;
  }

  PyErr_SetString(PyExc_IndexError, "no such vertex");
  return NULL;
}
Ejemplo n.º 23
0
static PyObject *
cbson_dumps (PyObject *self,
             PyObject *args)
{
   const char *keystr;
   PyObject *doc;
   PyObject *key;
   PyObject *ret;
   PyObject *value;
   Py_ssize_t pos = 0;
   bson_t *b;
   size_t keylen;

   if (!PyArg_ParseTuple(args, "O", &doc)) {
      return NULL;
   }

   if (!PyDict_Check(doc)) {
      PyErr_SetString(PyExc_TypeError, "doc must be a dict.");
      return NULL;
   }

   b = bson_new();

   while (PyDict_Next(doc, &pos, &key, &value)) {
      /*
       * TODO: Key validation. Make sure no NULL is present. Ensure valid UTF-8.
       */

      if (PyString_Check(key)) {
         keystr = PyString_AS_STRING(key);
         keylen = PyString_GET_SIZE(key);
      } else if (PyUnicode_Check(key)) {
         /*
          * TODO: Convert to UTF-8.
          */
         keystr = (const char *)PyUnicode_AS_UNICODE(key);
         keylen = PyUnicode_GET_SIZE(key);
      } else {
         PyErr_SetString(PyExc_TypeError, "key must be a string.");
         bson_destroy(b);
         return NULL;
      }

      if (value == Py_None) {
         if (!bson_append_null(b, keystr, keylen)) {
            goto failure;
         }
      } else if (PyString_Check(value)) {
         /*
          * TODO: Validate UTF-8.
          */
         if (!bson_append_utf8(b, keystr, keylen,
                               PyString_AS_STRING(value),
                               PyString_GET_SIZE(value))) {
            goto failure;
         }
      } else if (PyUnicode_Check(value)) {
         /*
          * TODO: Convert and validate UTF-8.
          */
         if (!bson_append_utf8(b, keystr, keylen,
                               (const char *)PyUnicode_AS_UNICODE(value),
                               PyUnicode_GET_SIZE(value))) {
            goto failure;
         }
      } else if (PyDateTime_Check(value)) {
         /*
          * TODO: Convert to msec since epoch.
          */
      } else if (PyBool_Check(value)) {
         if (!bson_append_bool(b, keystr, keylen, (value == Py_True))) {
            goto failure;
         }
      } else if (PyLong_Check(value)) {
         if (!bson_append_int64(b, keystr, keylen, PyLong_AsLong(value))) {
            goto failure;
         }
      } else if (PyInt_Check(value)) {
         if (!bson_append_int32(b, keystr, keylen, PyInt_AsLong(value))) {
            goto failure;
         }
      } else if (PyFloat_Check(value)) {
         if (!bson_append_double(b, keystr, keylen, PyFloat_AsDouble(value))) {
            goto failure;
         }
      } else if (cbson_oid_check(value)) {
         if (!bson_append_oid(b, keystr, keylen, &((cbson_oid_t *)value)->oid)) {
            goto failure;
         }
      /* } else if (CHECK FOR REGEX) { */
      /* } else if (CHECK FOR BINARY) { */
      } else {
         goto failure;
      }
   }

   ret = PyString_FromStringAndSize((const char *)bson_get_data(b), b->len);
   bson_destroy(b);

   return ret;

failure:
   PyErr_SetString(PyExc_TypeError, "Cannot encode type.");
   bson_destroy(b);
   return NULL;
}
Ejemplo n.º 24
0
//============================================================================
// METHOD: SPELLpyValue::set
//============================================================================
void SPELLpyValue::set( PyObject* pyValue )
{
	m_type = NONE;
	m_intValue = 0;
	m_boolValue = false;
	m_floatValue = 0.0;
	m_timeValue.set(0,0);
	m_stringValue = "";

	if (pyValue == NULL) return;

	DEBUG("[PYVAL] Set value from " + PYREPR(pyValue));
	if (pyValue != Py_None)
	{
		if (PyBool_Check(pyValue))
		{
			m_type = BOOLEAN;
			m_boolValue = pyValue == Py_True;
		}
		else if (PyLong_Check(pyValue))
		{
			DEBUG("[PYVAL] Long check");
			m_type = LONG;
			m_intValue = PyLong_AsLongLong(pyValue);
		}
		else if (PyInt_Check(pyValue))
		{
			DEBUG("[PYVAL] Int check");
			m_type = LONG;
			m_intValue = PyInt_AsLong(pyValue);
		}
		else if (PyFloat_Check(pyValue))
		{
			m_type = DOUBLE;
			m_floatValue = PyFloat_AsDouble(pyValue);
		}
		else if (SPELLpythonHelper::instance().isTime(pyValue))
		{
			m_timeValue = SPELLpythonHelper::instance().evalTime(PYSSTR(pyValue));
			if (m_timeValue.isDelta())
			{
				m_type = RELTIME;
			}
			else
			{
				m_type = ABSTIME;
			}
		}
		else if (PyString_Check(pyValue))
		{
			m_type = STRING;
			m_stringValue = PYSTR(pyValue);
		}
		else if (PyList_Check(pyValue))
		{
			m_type = LIST;
			m_listValue.clear();
			unsigned int numItems = PyList_Size(pyValue);
			for(unsigned int idx = 0; idx < numItems; idx++)
			{
				m_listValue.push_back( SPELLpyValue( PyList_GetItem( pyValue, idx) ));
			}
		}
		else if (PyDict_Check(pyValue))
		{
			m_type = DICT;
			m_dictValue.clear();
			PyObject* keys = PyDict_Keys(pyValue);
			unsigned int numItems = PyList_Size(keys);
			for(unsigned int idx = 0; idx < numItems; idx++)
			{
				PyObject* key = PyList_GetItem(keys,idx);
				PyObject* value = PyDict_GetItem( pyValue, key );
				m_dictValue.insert( std::make_pair( PYSSTR(key), SPELLpyValue(value) ) );
			}
		}
		else
		{
			THROW_EXCEPTION("Cannot create variable value",
					        "Cannot infer type from value (" + PYREPR(pyValue) + ")",
					        SPELL_ERROR_LANGUAGE);
		}
		SPELLpythonHelper::instance().checkError();
	}
}
Ejemplo n.º 25
0
/* Copies a Python int or long object to a signed 64-bit value
 * Returns 1 if successful or -1 on error
 */
int pyexe_integer_signed_copy_to_64bit(
     PyObject *integer_object,
     int64_t *value_64bit,
     libcerror_error_t **error )
{
	static char *function   = "pyexe_integer_signed_copy_to_64bit";
	int result              = 0;

#if defined( HAVE_LONG_LONG )
	PY_LONG_LONG long_value = 0;
#else
	long long_value         = 0;
#endif

	if( integer_object == NULL )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
		 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
		 "%s: invalid integer object.",
		 function );

		return( -1 );
	}
	PyErr_Clear();

	result = PyObject_IsInstance(
	          integer_object,
	          (PyObject *) &PyLong_Type );

	if( result == -1 )
	{
		pyexe_error_fetch(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
		 "%s: unable to determine if integer object is of type long.",
		 function );

		return( -1 );
	}
	else if( result != 0 )
	{
		PyErr_Clear();

#if defined( HAVE_LONG_LONG )
		long_value = PyLong_AsLongLong(
		              integer_object );
#else
		long_value = PyLong_AsLong(
		              integer_object );
#endif
	}
#if PY_MAJOR_VERSION < 3
	if( result == 0 )
	{
		PyErr_Clear();

		result = PyObject_IsInstance(
		          integer_object,
		          (PyObject *) &PyInt_Type );

		if( result == -1 )
		{
			pyexe_error_fetch(
			 error,
			 LIBCERROR_ERROR_DOMAIN_RUNTIME,
			 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
			 "%s: unable to determine if integer object is of type int.",
			 function );

			return( -1 );
		}
		else if( result != 0 )
		{
			PyErr_Clear();

			long_value = PyInt_AsLong(
			              integer_object );
		}
	}
#endif /* PY_MAJOR_VERSION < 3 */
	if( result == 0 )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
		 "%s: unsupported integer object type.",
		 function );

		return( -1 );
	}
	if( PyErr_Occurred() )
	{
		pyexe_error_fetch(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
		 "%s: unable to convert integer object to long.",
		 function );

		return( -1 );
	}
#if defined( HAVE_LONG_LONG ) && ( SIZEOF_LONG_LONG > 8 )
	if( ( long_value < (PY_LONG_LONG) INT64_MIN )
	 || ( long_value > (PY_LONG_LONG) INT64_MAX ) )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: invalid long value out of bounds.",
		 function );

		return( -1 );
	}
#elif ( SIZEOF_LONG > 8 )
	if( ( long_value > (long) INT64_MIN )
	 || ( long_value > (long) INT64_MAX ) )
	{
		libcerror_error_set(
		 error,
		 LIBCERROR_ERROR_DOMAIN_RUNTIME,
		 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
		 "%s: invalid long value out of bounds.",
		 function );

		return( -1 );
	}
#endif
	*value_64bit = (int64_t) long_value;

	return( 1 );
}
Ejemplo n.º 26
0
static int
_buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str,
                      PyArrayObject* arr, Py_ssize_t *offset,
                      char *active_byteorder)
{
    int k;
    char _active_byteorder = '@';
    Py_ssize_t _offset = 0;

    if (active_byteorder == NULL) {
        active_byteorder = &_active_byteorder;
    }
    if (offset == NULL) {
        offset = &_offset;
    }

    if (descr->subarray) {
        PyObject *item, *subarray_tuple;
        Py_ssize_t total_count = 1;
        Py_ssize_t dim_size;
        char buf[128];
        int old_offset;
        int ret;

        if (PyTuple_Check(descr->subarray->shape)) {
            subarray_tuple = descr->subarray->shape;
            Py_INCREF(subarray_tuple);
        }
        else {
            subarray_tuple = Py_BuildValue("(O)", descr->subarray->shape);
        }

        _append_char(str, '(');
        for (k = 0; k < PyTuple_GET_SIZE(subarray_tuple); ++k) {
            if (k > 0) {
                _append_char(str, ',');
            }
            item = PyTuple_GET_ITEM(subarray_tuple, k);
            dim_size = PyNumber_AsSsize_t(item, NULL);

            PyOS_snprintf(buf, sizeof(buf), "%ld", (long)dim_size);
            _append_str(str, buf);
            total_count *= dim_size;
        }
        _append_char(str, ')');

        Py_DECREF(subarray_tuple);

        old_offset = *offset;
        ret = _buffer_format_string(descr->subarray->base, str, arr, offset,
                                    active_byteorder);
        *offset = old_offset + (*offset - old_offset) * total_count;
        return ret;
    }
    else if (PyDataType_HASFIELDS(descr)) {
        int base_offset = *offset;

        _append_str(str, "T{");
        for (k = 0; k < PyTuple_GET_SIZE(descr->names); ++k) {
            PyObject *name, *item, *offset_obj, *tmp;
            PyArray_Descr *child;
            char *p;
            Py_ssize_t len;
            int new_offset;

            name = PyTuple_GET_ITEM(descr->names, k);
            item = PyDict_GetItem(descr->fields, name);

            child = (PyArray_Descr*)PyTuple_GetItem(item, 0);
            offset_obj = PyTuple_GetItem(item, 1);
            new_offset = base_offset + PyInt_AsLong(offset_obj);

            /* Insert padding manually */
            if (*offset > new_offset) {
                PyErr_SetString(PyExc_RuntimeError,
                                "This should never happen: Invalid offset in "
                                "buffer format string generation. Please "
                                "report a bug to the Numpy developers.");
                return -1;
            }
            while (*offset < new_offset) {
                _append_char(str, 'x');
                ++*offset;
            }

            /* Insert child item */
            _buffer_format_string(child, str, arr, offset,
                                  active_byteorder);

            /* Insert field name */
#if defined(NPY_PY3K)
            /* FIXME: XXX -- should it use UTF-8 here? */
            tmp = PyUnicode_AsUTF8String(name);
#else
            tmp = name;
#endif
            if (tmp == NULL || PyBytes_AsStringAndSize(tmp, &p, &len) < 0) {
                PyErr_SetString(PyExc_ValueError, "invalid field name");
                return -1;
            }
            _append_char(str, ':');
            while (len > 0) {
                if (*p == ':') {
                    Py_DECREF(tmp);
                    PyErr_SetString(PyExc_ValueError,
                                    "':' is not an allowed character in buffer "
                                    "field names");
                    return -1;
                }
                _append_char(str, *p);
                ++p;
                --len;
            }
            _append_char(str, ':');
#if defined(NPY_PY3K)
            Py_DECREF(tmp);
#endif
        }
        _append_char(str, '}');
    }
    else {
        int is_standard_size = 1;
        int is_native_only_type = (descr->type_num == NPY_LONGDOUBLE ||
                                   descr->type_num == NPY_CLONGDOUBLE);
#if NPY_SIZEOF_LONG_LONG != 8
        is_native_only_type = is_native_only_type || (
            descr->type_num == NPY_LONGLONG ||
            descr->type_num == NPY_ULONGLONG);
#endif

        *offset += descr->elsize;

        if (descr->byteorder == '=' &&
                _is_natively_aligned_at(descr, arr, *offset)) {
            /* Prefer native types, to cater for Cython */
            is_standard_size = 0;
            if (*active_byteorder != '@') {
                _append_char(str, '@');
                *active_byteorder = '@';
            }
        }
        else if (descr->byteorder == '=' && is_native_only_type) {
            /* Data types that have no standard size */
            is_standard_size = 0;
            if (*active_byteorder != '^') {
                _append_char(str, '^');
                *active_byteorder = '^';
            }
        }
        else if (descr->byteorder == '<' || descr->byteorder == '>' ||
                 descr->byteorder == '=') {
            is_standard_size = 1;
            if (*active_byteorder != descr->byteorder) {
                _append_char(str, descr->byteorder);
                *active_byteorder = descr->byteorder;
            }

            if (is_native_only_type) {
                /* It's not possible to express native-only data types
                   in non-native byte orders */
                PyErr_Format(PyExc_ValueError,
                             "cannot expose native-only dtype '%c' in "
                             "non-native byte order '%c' via buffer interface",
                             descr->type, descr->byteorder);
            }
        }

        switch (descr->type_num) {
        case NPY_BOOL:         if (_append_char(str, '?')) return -1; break;
        case NPY_BYTE:         if (_append_char(str, 'b')) return -1; break;
        case NPY_UBYTE:        if (_append_char(str, 'B')) return -1; break;
        case NPY_SHORT:        if (_append_char(str, 'h')) return -1; break;
        case NPY_USHORT:       if (_append_char(str, 'H')) return -1; break;
        case NPY_INT:          if (_append_char(str, 'i')) return -1; break;
        case NPY_UINT:         if (_append_char(str, 'I')) return -1; break;
        case NPY_LONG:
            if (is_standard_size && (NPY_SIZEOF_LONG == 8)) {
                if (_append_char(str, 'q')) return -1;
            }
            else {
                if (_append_char(str, 'l')) return -1;
            }
            break;
        case NPY_ULONG:
            if (is_standard_size && (NPY_SIZEOF_LONG == 8)) {
                if (_append_char(str, 'Q')) return -1;
            }
            else {
                if (_append_char(str, 'L')) return -1;
            }
            break;
        case NPY_LONGLONG:     if (_append_char(str, 'q')) return -1; break;
        case NPY_ULONGLONG:    if (_append_char(str, 'Q')) return -1; break;
        case NPY_HALF:         if (_append_char(str, 'e')) return -1; break;
        case NPY_FLOAT:        if (_append_char(str, 'f')) return -1; break;
        case NPY_DOUBLE:       if (_append_char(str, 'd')) return -1; break;
        case NPY_LONGDOUBLE:   if (_append_char(str, 'g')) return -1; break;
        case NPY_CFLOAT:       if (_append_str(str, "Zf")) return -1; break;
        case NPY_CDOUBLE:      if (_append_str(str, "Zd")) return -1; break;
        case NPY_CLONGDOUBLE:  if (_append_str(str, "Zg")) return -1; break;
        /* XXX: datetime */
        /* XXX: timedelta */
        case NPY_OBJECT:       if (_append_char(str, 'O')) return -1; break;
        case NPY_STRING: {
            char buf[128];
            PyOS_snprintf(buf, sizeof(buf), "%ds", descr->elsize);
            if (_append_str(str, buf)) return -1;
            break;
        }
        case NPY_UNICODE: {
            /* Numpy Unicode is always 4-byte */
            char buf[128];
            assert(descr->elsize % 4 == 0);
            PyOS_snprintf(buf, sizeof(buf), "%dw", descr->elsize / 4);
            if (_append_str(str, buf)) return -1;
            break;
        }
        case NPY_VOID: {
            /* Insert padding bytes */
            char buf[128];
            PyOS_snprintf(buf, sizeof(buf), "%dx", descr->elsize);
            if (_append_str(str, buf)) return -1;
            break;
        }
        default:
            PyErr_Format(PyExc_ValueError,
                         "cannot include dtype '%c' in a buffer",
                         descr->type);
            return -1;
        }
    }

    return 0;
}
Ejemplo n.º 27
0
AerospikeQuery * AerospikeQuery_Where(AerospikeQuery * self, PyObject * args)
{
	as_error err;
	int rc = 0;

	PyObject * py_arg1 = NULL;
	PyObject * py_arg2 = NULL;
	PyObject * py_arg3 = NULL;
	PyObject * py_arg4 = NULL;
	PyObject * py_arg5 = NULL;
	PyObject * py_arg6 = NULL;

	if ( PyArg_ParseTuple(args, "O|OOOOO:where", &py_arg1, &py_arg2, &py_arg3, &py_arg4, &py_arg5, &py_arg6) == false ) {
		return NULL;
	}

	as_error_init(&err);

	if (!self || !self->client->as) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
		goto CLEANUP;
	}

	if (!self->client->is_conn_16) {
		as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster");
		goto CLEANUP;
	}

	if ( PyTuple_Check(py_arg1) ) {

		Py_ssize_t size = PyTuple_Size(py_arg1);
		if ( size < 1 ) {
			// If it ain't atleast 1, then raise error
			return NULL;
		}

		PyObject * py_op = PyTuple_GetItem(py_arg1, 0);
		PyObject * py_op_data = PyTuple_GetItem(py_arg1, 1);

		if ( PyInt_Check(py_op) && PyInt_Check(py_op_data)) {
			as_predicate_type op = (as_predicate_type) PyInt_AsLong(py_op);
			as_index_datatype op_data = (as_index_datatype) PyInt_AsLong(py_op_data);
			rc = AerospikeQuery_Where_Add(
				self,
				op,
				op_data,
				size > 2 ? PyTuple_GetItem(py_arg1, 2) : Py_None,
				size > 3 ? PyTuple_GetItem(py_arg1, 3) : Py_None,
				size > 4 ? PyTuple_GetItem(py_arg1, 4) : Py_None,
				size > 5 ? PyInt_AsLong(PyTuple_GetItem(py_arg1, 5)) : 0
			);
		}
	}
	else if ( (py_arg1) && PyStr_Check(py_arg1) && (py_arg2) && PyStr_Check(py_arg2) ) {

		char * op = PyStr_AsString(py_arg2);

		if ( strcmp(op, "equals") == 0 ) {
			if ( PyInt_Check(py_arg3) || PyLong_Check(py_arg3) ) {
				rc = AerospikeQuery_Where_Add(
					self,
					AS_PREDICATE_EQUAL,
					AS_INDEX_NUMERIC,
					py_arg1,
					py_arg3,
					Py_None,
					0
				);
			}
			else if ( PyStr_Check(py_arg3) || PyUnicode_Check(py_arg3) ) {
				rc = AerospikeQuery_Where_Add(
					self,
					AS_PREDICATE_EQUAL,
					AS_INDEX_STRING,
					py_arg1,
					py_arg3,
					Py_None,
					0
				);
			}
			else {
				as_error_update(&err, AEROSPIKE_ERR_PARAM, "predicate 'equals' expects a bin and string value.");
				PyObject * py_err = NULL;
				error_to_pyobject(&err, &py_err);
				PyErr_SetObject(PyExc_Exception, py_err);
				rc = 1;
			}
		}
		else if ( strcmp(op, "between") == 0 ) {
			rc = AerospikeQuery_Where_Add(
				self,
				AS_PREDICATE_RANGE,
				AS_INDEX_NUMERIC,
				py_arg1,
				py_arg3,
				py_arg4,
				0
			);
		}
		else if ( strcmp(op, "contains") == 0 ) {
			int index_type = 0;
			int type = 0;
			if(PyInt_Check(py_arg3)) {
				index_type = PyInt_AsLong(py_arg3);
			} else if (PyLong_Check(py_arg3)) {
				index_type = PyLong_AsLongLong(py_arg3);
                if(-1 == index_type) {
                    as_error_update(&err, AEROSPIKE_ERR_PARAM, "integer value exceeds sys.maxsize");
                    goto CLEANUP;
                }
			}

			if(PyInt_Check(py_arg4)) {
				type = PyInt_AsLong(py_arg4);
			} else if ( PyLong_Check(py_arg4) ) {
				type = PyLong_AsLongLong(py_arg4);
                if(-1 == type) {
                    as_error_update(&err, AEROSPIKE_ERR_PARAM, "integer value exceeds sys.maxsize");
                    goto CLEANUP;
                }
			}
			if ( (PyInt_Check(py_arg5) || PyLong_Check(py_arg5)) && type == 1) {
				rc = AerospikeQuery_Where_Add(
					self,
					AS_PREDICATE_EQUAL,
					AS_INDEX_NUMERIC,
					py_arg1,
					py_arg5,
					Py_None,
					index_type
				);
			}
			else if ( (PyStr_Check(py_arg5) || PyUnicode_Check(py_arg5)) && type == 0) {
				rc = AerospikeQuery_Where_Add(
					self,
					AS_PREDICATE_EQUAL,
					AS_INDEX_STRING,
					py_arg1,
					py_arg5,
					Py_None,
					index_type
				);
			}
			else {
				as_error_update(&err, AEROSPIKE_ERR_PARAM, "predicate 'contains' expects a bin and a string or int value.");
				PyObject * py_err = NULL;
				error_to_pyobject(&err, &py_err);
				PyErr_SetObject(PyExc_Exception, py_err);
				rc = 1;
			}
		}
		else if ( strcmp(op, "range") == 0 ) {
			int index_type = 0;
			if(PyInt_Check(py_arg3)) {
				index_type = PyInt_AsLong(py_arg3);
			} else if (PyLong_Check(py_arg3)) {
				index_type = PyLong_AsLongLong(py_arg3);
                if(-1 == index_type) {
                    as_error_update(&err, AEROSPIKE_ERR_PARAM, "integer value exceeds sys.maxsize");
                    goto CLEANUP;
                }
			}

			if ( PyInt_Check(py_arg4) || PyLong_Check(py_arg4)) {
				rc = AerospikeQuery_Where_Add(
					self,
					AS_PREDICATE_RANGE,
					AS_INDEX_NUMERIC,
					py_arg1,
					py_arg5,
					py_arg6,
					index_type
				);
			}
			else {
				as_error_update(&err, AEROSPIKE_ERR_PARAM, "predicate 'range' expects a bin and two numeric values.");
				PyObject * py_err = NULL;
				error_to_pyobject(&err, &py_err);
				PyErr_SetObject(PyExc_Exception, py_err);
				rc = 1;
			}
		}
		else {
			as_error_update(&err, AEROSPIKE_ERR_PARAM, "predicate '%s' is invalid.", op);
			PyObject * py_err = NULL;
			error_to_pyobject(&err, &py_err);
			PyErr_SetObject(PyExc_Exception, py_err);
			rc = 1;
		}

		// if ( PyInt_Check(py_op) ) {
		// 	rc = AerospikeQuery_Where_Add(
		// 		&self->query,
		// 		PyInt_AsLong(py_op),
		// 		size > 1 ? PyTuple_GetItem(py_predicate, 1) : Py_None,
		// 		size > 2 ? PyTuple_GetItem(py_predicate, 2) : Py_None,
		// 		size > 3 ? PyTuple_GetItem(py_predicate, 3) : Py_None
		// 	);
		// }
	}
	else {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "predicate is invalid.");
		PyObject * py_err = NULL;
		error_to_pyobject(&err, &py_err);
		PyObject *exception_type = raise_exception(&err);
		PyErr_SetObject(exception_type, py_err);
		Py_DECREF(py_err);
		as_query_destroy(&self->query);
		rc = 1;
	}
CLEANUP:
	if ( rc == 1 ) {
		return NULL;
	}

	if ( err.code != AEROSPIKE_OK ) {
		PyObject * py_err = NULL;
		error_to_pyobject(&err, &py_err);
		PyObject *exception_type = raise_exception(&err);
		PyErr_SetObject(exception_type, py_err);
		Py_DECREF(py_err);
		TRACE();
		return NULL;
	}

	Py_INCREF(self);
	return self;
}
Ejemplo n.º 28
0
/**
 *******************************************************************************************************
 * Registers a UDF module with the Aerospike DB.
 * @param self                  AerospikeClient object
 * @param args                  The args is a tuple object containing an argument
 *                              list passed from Python to a C function
 * @param kwds                  Dictionary of keywords
 *
 * Returns an integer status. 0(Zero) is success value.
 * In case of error,appropriate exceptions will be raised.
 *******************************************************************************************************
 */
PyObject * AerospikeClient_UDF_Put(AerospikeClient * self, PyObject *args, PyObject * kwds)
{
	// Initialize error
	as_error err;
	as_error_init(&err);

	// Python Function Arguments
	PyObject * py_filename = NULL;
	long language = 0;
	PyObject * py_udf_type = NULL;
	PyObject * py_policy = NULL;
	PyObject * py_ustr = NULL;

	uint8_t * bytes = NULL;
	as_policy_info info_policy;
	as_policy_info *info_policy_p = NULL;

	// Python Function Keyword Arguments
	static char * kwlist[] = {"filename", "udf_type", "policy", NULL};

	// Python Function Argument Parsing
	if ( PyArg_ParseTupleAndKeywords(args, kwds, "O|lO:udf_put", kwlist,
				&py_filename, &language, &py_policy) == false ) {
		return NULL;
	}

	if(language != AS_UDF_TYPE_LUA)
	{
		as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Invalid UDF language");
		goto CLEANUP;
	}
	py_udf_type = PyLong_FromLong(language);

	if (!self || !self->as) {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid aerospike object");
		goto CLEANUP;
	}

	if (!self->is_conn_16) {
		as_error_update(&err, AEROSPIKE_ERR_CLUSTER, "No connection to aerospike cluster");
		goto CLEANUP;
	}

	// Convert PyObject into a filename string
	char *filename = NULL;
	if (PyUnicode_Check(py_filename)) {
		py_ustr = PyUnicode_AsUTF8String(py_filename);
		filename = PyString_AsString(py_ustr);
	} else if (PyString_Check(py_filename)) {
		filename = PyString_AsString(py_filename);
	} else {
		as_error_update(&err, AEROSPIKE_ERR_PARAM, "Filename should be a string");
		goto CLEANUP;
	}

	// Convert python object to policy_info
	pyobject_to_policy_info( &err, py_policy, &info_policy, &info_policy_p,
			&self->as->config.policies.info);
	if ( err.code != AEROSPIKE_OK ) {
		goto CLEANUP;
	}
	as_udf_type udf_type = (as_udf_type)PyInt_AsLong(py_udf_type);

	// Convert lua file to content
	as_bytes content;
	FILE * file_p = fopen(filename,"r");
	FILE * copy_file_p = NULL;

	char copy_filepath[AS_CONFIG_PATH_MAX_LEN] = {0};
	uint32_t user_path_len = strlen(self->as->config.lua.user_path);
    memcpy( copy_filepath,
        self->as->config.lua.user_path,
        user_path_len);
    if ( self->as->config.lua.user_path[user_path_len-1] != '/' ) {
        memcpy( copy_filepath + user_path_len, "/", 1);
        user_path_len = user_path_len + 1;
    }
    char* extracted_filename = strrchr(filename, '/');
    if (extracted_filename) {
        memcpy( copy_filepath + user_path_len, extracted_filename + 1, strlen(extracted_filename) - 1);
        copy_filepath[user_path_len + strlen(extracted_filename) - 1] = '\0';
    } else {
        memcpy( copy_filepath + user_path_len, filename, strlen(filename));
        copy_filepath[user_path_len + strlen(filename)] = '\0';
    }


	if ( !file_p ) {
		as_error_update(&err, AEROSPIKE_ERR_LUA_FILE_NOT_FOUND, "cannot open script file");
		goto CLEANUP;
	}

	bytes = (uint8_t *) malloc(SCRIPT_LEN_MAX);
	if ( bytes == NULL ) {
		as_error_update(&err, errno, "malloc failed");
		goto CLEANUP;
	}

    int size = 0;

    uint8_t * buff = bytes;

	if (access(self->as->config.lua.user_path, W_OK) == 0) {
		copy_file_p = fopen(copy_filepath, "r");
		if (!copy_file_p) {
			copy_file_p = fopen(copy_filepath, "w+");
			int read  = (int)fread(buff, 1, LUA_FILE_BUFFER_FRAME, file_p);
			if (read && fwrite(buff, 1, read, copy_file_p)) {
				while (read) {
					size += read;
					buff += read;
					read = (int)fread(buff, 1, LUA_FILE_BUFFER_FRAME, file_p);
					if (!fwrite(buff, 1, read, copy_file_p)) {
						break;
					}
				}
			} else {
				as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Write of lua file to user path failed");
				goto CLEANUP;
			}
		} else {
			int read  = (int)fread(buff, 1, LUA_FILE_BUFFER_FRAME, file_p);
			while (read) {
				size += read;
				buff += read;
				read = (int)fread(buff, 1, LUA_FILE_BUFFER_FRAME, file_p);
			}
		}
	} else {
		as_error_update(&err, AEROSPIKE_ERR_CLIENT, "No permissions to write lua file to user path");
		goto CLEANUP;
	}

	if (file_p) {
		fclose(file_p);
	}
	if (copy_file_p) {
		fclose(copy_file_p);
	}

	as_bytes_init_wrap(&content, bytes, size, true);

	// Invoke operation
    Py_BEGIN_ALLOW_THREADS
	aerospike_udf_put(self->as, &err, info_policy_p, filename, udf_type, &content);
    Py_END_ALLOW_THREADS
	if( err.code != AEROSPIKE_OK ) {
		as_error_update(&err, err.code, NULL);
		goto CLEANUP;
	} else {
		aerospike_udf_put_wait(self->as, &err, info_policy_p, as_basename(NULL, filename), 2000);
	}

CLEANUP:
	if(bytes) {
		free(bytes);
	}

	if (py_ustr) {
		Py_DECREF(py_ustr);
	}

	if ( err.code != AEROSPIKE_OK ) {
		PyObject * py_err = NULL;
		error_to_pyobject(&err, &py_err);
		PyObject *exception_type = raise_exception(&err);
		if(PyObject_HasAttrString(exception_type, "module")) {
			PyObject_SetAttrString(exception_type, "module", Py_None);
		} 
		if(PyObject_HasAttrString(exception_type, "func")) {
			PyObject_SetAttrString(exception_type, "func", Py_None);
		}
		PyErr_SetObject(exception_type, py_err);
		Py_DECREF(py_err);
		return NULL;
	}

	return PyLong_FromLong(0);
}
Ejemplo n.º 29
0
/*
 *
 * Tests for and converts a Python datetime.datetime or datetime.date
 * object into a NumPy pandas_datetimestruct.  Uses tzinfo (if present)
 * to convert to UTC time.
 *
 * While the C API has PyDate_* and PyDateTime_* functions, the following
 * implementation just asks for attributes, and thus supports
 * datetime duck typing. The tzinfo time zone conversion would require
 * this style of access anyway.
 *
 * Returns -1 on error, 0 on success, and 1 (with no error set)
 * if obj doesn't have the needed date or datetime attributes.
 */
int convert_pydatetime_to_datetimestruct(PyObject *obj,
                                         pandas_datetimestruct *out) {
    PyObject *tmp;
    int isleap;

    /* Initialize the output to all zeros */
    memset(out, 0, sizeof(pandas_datetimestruct));
    out->month = 1;
    out->day = 1;

    /* Need at least year/month/day attributes */
    if (!PyObject_HasAttrString(obj, "year") ||
        !PyObject_HasAttrString(obj, "month") ||
        !PyObject_HasAttrString(obj, "day")) {
        return 1;
    }

    /* Get the year */
    tmp = PyObject_GetAttrString(obj, "year");
    if (tmp == NULL) {
        return -1;
    }
    out->year = PyInt_AsLong(tmp);
    if (out->year == -1 && PyErr_Occurred()) {
        Py_DECREF(tmp);
        return -1;
    }
    Py_DECREF(tmp);

    /* Get the month */
    tmp = PyObject_GetAttrString(obj, "month");
    if (tmp == NULL) {
        return -1;
    }
    out->month = PyInt_AsLong(tmp);
    if (out->month == -1 && PyErr_Occurred()) {
        Py_DECREF(tmp);
        return -1;
    }
    Py_DECREF(tmp);

    /* Get the day */
    tmp = PyObject_GetAttrString(obj, "day");
    if (tmp == NULL) {
        return -1;
    }
    out->day = PyInt_AsLong(tmp);
    if (out->day == -1 && PyErr_Occurred()) {
        Py_DECREF(tmp);
        return -1;
    }
    Py_DECREF(tmp);

    /* Validate that the month and day are valid for the year */
    if (out->month < 1 || out->month > 12) {
        goto invalid_date;
    }
    isleap = is_leapyear(out->year);
    if (out->day < 1 ||
        out->day > days_per_month_table[isleap][out->month - 1]) {
        goto invalid_date;
    }

    /* Check for time attributes (if not there, return success as a date) */
    if (!PyObject_HasAttrString(obj, "hour") ||
        !PyObject_HasAttrString(obj, "minute") ||
        !PyObject_HasAttrString(obj, "second") ||
        !PyObject_HasAttrString(obj, "microsecond")) {
        return 0;
    }

    /* Get the hour */
    tmp = PyObject_GetAttrString(obj, "hour");
    if (tmp == NULL) {
        return -1;
    }
    out->hour = PyInt_AsLong(tmp);
    if (out->hour == -1 && PyErr_Occurred()) {
        Py_DECREF(tmp);
        return -1;
    }
    Py_DECREF(tmp);

    /* Get the minute */
    tmp = PyObject_GetAttrString(obj, "minute");
    if (tmp == NULL) {
        return -1;
    }
    out->min = PyInt_AsLong(tmp);
    if (out->min == -1 && PyErr_Occurred()) {
        Py_DECREF(tmp);
        return -1;
    }
    Py_DECREF(tmp);

    /* Get the second */
    tmp = PyObject_GetAttrString(obj, "second");
    if (tmp == NULL) {
        return -1;
    }
    out->sec = PyInt_AsLong(tmp);
    if (out->sec == -1 && PyErr_Occurred()) {
        Py_DECREF(tmp);
        return -1;
    }
    Py_DECREF(tmp);

    /* Get the microsecond */
    tmp = PyObject_GetAttrString(obj, "microsecond");
    if (tmp == NULL) {
        return -1;
    }
    out->us = PyInt_AsLong(tmp);
    if (out->us == -1 && PyErr_Occurred()) {
        Py_DECREF(tmp);
        return -1;
    }
    Py_DECREF(tmp);

    if (out->hour < 0 || out->hour >= 24 || out->min < 0 || out->min >= 60 ||
        out->sec < 0 || out->sec >= 60 || out->us < 0 || out->us >= 1000000) {
        goto invalid_time;
    }

    /* Apply the time zone offset if it exists */
    if (PyObject_HasAttrString(obj, "tzinfo")) {
        tmp = PyObject_GetAttrString(obj, "tzinfo");
        if (tmp == NULL) {
            return -1;
        }
        if (tmp == Py_None) {
            Py_DECREF(tmp);
        } else {
            PyObject *offset;
            int seconds_offset, minutes_offset;

            /* The utcoffset function should return a timedelta */
            offset = PyObject_CallMethod(tmp, "utcoffset", "O", obj);
            if (offset == NULL) {
                Py_DECREF(tmp);
                return -1;
            }
            Py_DECREF(tmp);

            /*
             * The timedelta should have a function "total_seconds"
             * which contains the value we want.
             */
            tmp = PyObject_CallMethod(offset, "total_seconds", "");
            if (tmp == NULL) {
                return -1;
            }
            seconds_offset = PyInt_AsLong(tmp);
            if (seconds_offset == -1 && PyErr_Occurred()) {
                Py_DECREF(tmp);
                return -1;
            }
            Py_DECREF(tmp);

            /* Convert to a minutes offset and apply it */
            minutes_offset = seconds_offset / 60;

            add_minutes_to_datetimestruct(out, -minutes_offset);
        }
    }

    return 0;

invalid_date:
    PyErr_Format(PyExc_ValueError,
                 "Invalid date (%d,%d,%d) when converting to NumPy datetime",
                 (int)out->year, (int)out->month, (int)out->day);
    return -1;

invalid_time:
    PyErr_Format(PyExc_ValueError,
                 "Invalid time (%d,%d,%d,%d) when converting "
                 "to NumPy datetime",
                 (int)out->hour, (int)out->min, (int)out->sec, (int)out->us);
    return -1;
}
static int get_int_result(PyObject *pResult, int pos)
{
	
	return (int)PyInt_AsLong(PyTuple_GetItem(pResult, pos));
}