Beispiel #1
0
static PyObject *
python_orblag( PyObject *self, PyObject *args ) {
	char	*usage = "Usage: _orblag(orb, match, reject)\n";
	int	orbfd;
	char	*match = 0;
	char	*reject = 0;
	char	err[STRSZ];
	double	rc;
	int	ilaggard;
	int	nlaggards;
	ClientLag *cl = 0;
	Laggards *laggards = 0;
	PyObject *laggards_obj;
	PyObject *clientlag_obj;
	PyObject *obj;

	if( ! PyArg_ParseTuple( args, "izz", &orbfd, &match, &reject) ) {

		if( ! PyErr_Occurred() ) {

			PyErr_SetString( PyExc_RuntimeError, usage );
		}

		return NULL;
	}

	rc = orblag( orbfd, match, reject, &laggards );

	if( rc < 0 ) {

		sprintf( err, "Error: orblag returned %f\n", rc );

		PyErr_SetString( PyExc_RuntimeError, err );

		return NULL;
	}

	nlaggards = maxtbl( laggards->list );

	laggards_obj = PyTuple_New( nlaggards );

	for( ilaggard = 0; ilaggard < nlaggards; ilaggard++ ) {

		cl = gettbl( laggards->list, ilaggard );

		clientlag_obj = PyTuple_New( 5 );

		PyTuple_SetItem( clientlag_obj, 0, PyFloat_FromDouble( cl->lag ) );
		PyTuple_SetItem( clientlag_obj, 1, PyInt_FromLong( (long) cl->thread ) );
		PyTuple_SetItem( clientlag_obj, 2, PyInt_FromLong( (long) cl->pktid ) );
		PyTuple_SetItem( clientlag_obj, 3, PyString_FromString( cl->who ) );
		PyTuple_SetItem( clientlag_obj, 4, PyString_FromString( cl->what ) );

		PyTuple_SetItem( laggards_obj, ilaggard, clientlag_obj );
	}

	obj = Py_BuildValue( "iiiiO", laggards->oldest,
				      laggards->newest,
				      laggards->maxpktid, 
				      laggards->range, 
				      laggards_obj );

	freeLaggards( laggards );

	return obj;
}
Beispiel #2
0
void interpret_notification(PyObject* dict, const void *pnotif, int size)
{
	const union sctp_notification *notif = pnotif;
	PyDict_SetItemString(dict, "type", PyInt_FromLong(notif->sn_header.sn_type));
	PyDict_SetItemString(dict, "flags", PyInt_FromLong(notif->sn_header.sn_flags));
	PyDict_SetItemString(dict, "length", PyInt_FromLong(notif->sn_header.sn_length));

	switch (notif->sn_header.sn_type) {
	case SCTP_ASSOC_CHANGE:
		{
		const struct sctp_assoc_change* n = &(notif->sn_assoc_change);
		PyDict_SetItemString(dict, "state", PyInt_FromLong(n->sac_state));
		PyDict_SetItemString(dict, "error", PyInt_FromLong(n->sac_error));
		PyDict_SetItemString(dict, "outbound_streams", PyInt_FromLong(n->sac_outbound_streams));
		PyDict_SetItemString(dict, "inbound_streams", PyInt_FromLong(n->sac_inbound_streams));
		PyDict_SetItemString(dict, "assoc_id", PyInt_FromLong(n->sac_assoc_id));
		}
		break;
	case SCTP_PEER_ADDR_CHANGE: 
		{
		const struct sctp_paddr_change* n = &(notif->sn_paddr_change);
		char caddr[256];
		int family;
		int len;
		int port;
		PyObject* oaddr;

		if (from_sockaddr((struct sockaddr*) &(n->spc_aaddr), &family, &len, &port, 
									caddr, sizeof(caddr))) {
			oaddr = PyTuple_New(2);
			PyTuple_SetItem(oaddr, 0, PyString_FromString(caddr));
			PyTuple_SetItem(oaddr, 1, PyInt_FromLong(port));
		} else {
			// something went wrong
			oaddr = Py_None;
			Py_INCREF(Py_None);
		}

		PyDict_SetItemString(dict, "addr", oaddr);
		PyDict_SetItemString(dict, "state", PyInt_FromLong(n->spc_state));
		PyDict_SetItemString(dict, "error", PyInt_FromLong(n->spc_error));
		PyDict_SetItemString(dict, "assoc_id", PyInt_FromLong(n->spc_assoc_id));
		}
		break;
	case SCTP_SEND_FAILED:
		{
		const struct sctp_send_failed* n = &(notif->sn_send_failed);
		const char* cdata = ((char*) notif) + sizeof(struct sctp_send_failed);
		int ldata = size - sizeof(struct sctp_send_failed);

		if (ldata >= 0) {
			PyObject* info = PyDict_New();
			interpret_sndrcvinfo(info, &(n->ssf_info));
			PyDict_SetItemString(dict, "_info", info);
			PyDict_SetItemString(dict, "error", PyInt_FromLong(n->ssf_error));
			PyDict_SetItemString(dict, "assoc_id", PyInt_FromLong(n->ssf_assoc_id));
			PyDict_SetItemString(dict, "data", PyString_FromStringAndSize(cdata, ldata));
		}
		}
		break;
	case SCTP_REMOTE_ERROR:
		{
		const struct sctp_remote_error* n = &(notif->sn_remote_error);
		const char* cdata = ((char*) notif) + sizeof(struct sctp_remote_error);
		int ldata = size - sizeof(struct sctp_remote_error);
		
		if (ldata >= 0) {
			PyDict_SetItemString(dict, "error", PyInt_FromLong(n->sre_error));
			PyDict_SetItemString(dict, "assoc_id", PyInt_FromLong(n->sre_assoc_id));
			PyDict_SetItemString(dict, "data", PyString_FromStringAndSize(cdata, ldata));
		}
		}
		break;
	case SCTP_SHUTDOWN_EVENT:
		{
		const struct sctp_shutdown_event* n = &(notif->sn_shutdown_event);
		PyDict_SetItemString(dict, "assoc_id", PyInt_FromLong(n->sse_assoc_id));
		}
		break;
	case SCTP_PARTIAL_DELIVERY_EVENT:
		{
		const struct sctp_pdapi_event* n = &(notif->sn_pdapi_event);
		PyDict_SetItemString(dict, "indication", PyInt_FromLong(n->pdapi_indication));
		PyDict_SetItemString(dict, "assoc_id", PyInt_FromLong(n->pdapi_assoc_id));
		}
		break;
	case SCTP_ADAPTATION_INDICATION:
		{
		const struct sctp_adaptation_event* n = &(notif->sn_adaptation_event);
		PyDict_SetItemString(dict, "adaptation_ind", PyInt_FromLong(n->sai_adaptation_ind));
		PyDict_SetItemString(dict, "assoc_id", PyInt_FromLong(n->sai_assoc_id));
		}
		break;
	}
}
Beispiel #3
0
static PyObject* get_status(PyObject* dummy, PyObject* args)
{
	PyObject* ret = 0;
	PyObject* dict;
	PyObject* dict2;
	PyObject* oassoc_id;

	PyObject* oaddr;
	char caddr[256];
	int family, len, port;

	int fd;
	struct sctp_status v;
	socklen_t lv = sizeof(v);
	int ok;
	
	ok = PyArg_ParseTuple(args, "iOO", &fd, &dict, &dict2) && \
	     					PyDict_Check(dict) && PyDict_Check(dict2);
	ok = ok && (oassoc_id = PyDict_GetItemString(dict, "assoc_id"));
	ok = ok && PyInt_Check(oassoc_id);

	if (! ok) {
		return ret;
	}

	bzero(&v, sizeof(v));
	v.sstat_assoc_id = PyInt_AsLong(oassoc_id);

	if (getsockopt(fd, SOL_SCTP, SCTP_STATUS, &v, &lv)) {
		PyErr_SetFromErrno(PyExc_IOError);
	} else {
		PyDict_SetItemString(dict, "state", PyInt_FromLong(v.sstat_state));
		PyDict_SetItemString(dict, "rwnd", PyInt_FromLong(v.sstat_rwnd));
		PyDict_SetItemString(dict, "unackdata", PyInt_FromLong(v.sstat_unackdata));
		PyDict_SetItemString(dict, "penddata", PyInt_FromLong(v.sstat_penddata));
		PyDict_SetItemString(dict, "instrms", PyInt_FromLong(v.sstat_instrms));
		PyDict_SetItemString(dict, "outstrms", PyInt_FromLong(v.sstat_outstrms));
		PyDict_SetItemString(dict, "fragmentation_point", PyInt_FromLong(v.sstat_fragmentation_point));

		if (from_sockaddr((struct sockaddr*) &(v.sstat_primary.spinfo_address), &family, 
					&len, &port, caddr, sizeof(caddr))) {
			oaddr = PyTuple_New(2);
			PyTuple_SetItem(oaddr, 0, PyString_FromString(caddr));
			PyTuple_SetItem(oaddr, 1, PyInt_FromLong(port));
		} else {
			// something went wrong
			oaddr = Py_None;
			Py_INCREF(Py_None);
		}
		
		PyDict_SetItemString(dict2, "sockaddr", oaddr);
		PyDict_SetItemString(dict2, "assoc_id", PyInt_FromLong(v.sstat_primary.spinfo_assoc_id));
		PyDict_SetItemString(dict2, "state", PyInt_FromLong(v.sstat_primary.spinfo_state));
		PyDict_SetItemString(dict2, "cwnd", PyInt_FromLong(v.sstat_primary.spinfo_cwnd));
		PyDict_SetItemString(dict2, "srtt", PyInt_FromLong(v.sstat_primary.spinfo_srtt));
		PyDict_SetItemString(dict2, "rto", PyInt_FromLong(v.sstat_primary.spinfo_rto));
		PyDict_SetItemString(dict2, "mtu", PyInt_FromLong(v.sstat_primary.spinfo_mtu));
		ret = Py_None; Py_INCREF(ret);
	}

	return ret;
}
Beispiel #4
0
void *uwsgi_python_tracebacker_thread(void *foobar) {

	struct iovec iov[11];

	PyObject *new_thread = uwsgi_python_setup_thread("uWSGITraceBacker");
	if (!new_thread) return NULL;

	struct sockaddr_un so_sun;
	socklen_t so_sun_len = 0;

	char *str_wid = uwsgi_num2str(uwsgi.mywid);
	char *sock_path = uwsgi_concat2(up.tracebacker, str_wid);

	int current_defer_accept = uwsgi.no_defer_accept;
        uwsgi.no_defer_accept = 1;
	int fd = bind_to_unix(sock_path, uwsgi.listen_queue, uwsgi.chmod_socket, uwsgi.abstract_socket);
	if (fd < 0) {
		uwsgi.no_defer_accept = current_defer_accept;
		free(str_wid);
		free(sock_path);
		return NULL;
	}
        uwsgi.no_defer_accept = current_defer_accept;

	PyObject *traceback_module = PyImport_ImportModule("traceback");
	if (!traceback_module) {
		free(str_wid);
		free(sock_path);
		close(fd);
		return NULL;
	}
	PyObject *traceback_dict = PyModule_GetDict(traceback_module);
	PyObject *extract_stack = PyDict_GetItemString(traceback_dict, "extract_stack");

	PyObject *sys_module = PyImport_ImportModule("sys");
	PyObject *sys_dict = PyModule_GetDict(sys_module);

	PyObject *_current_frames = PyDict_GetItemString(sys_dict, "_current_frames");

	uwsgi_log("python tracebacker for worker %d available on %s\n", uwsgi.mywid, sock_path);

	for(;;) {
		UWSGI_RELEASE_GIL;
		int client_fd = accept(fd, (struct sockaddr *) &so_sun, &so_sun_len);
		if (client_fd < 0) {
			uwsgi_error("accept()");
			UWSGI_GET_GIL;
			continue;
		}
		UWSGI_GET_GIL;
// here is the core of the tracebacker
		PyObject *current_frames = PyEval_CallObject(_current_frames, (PyObject *)NULL);
		if (!current_frames) goto end2;

		PyObject *current_frames_items = PyObject_GetAttrString(current_frames, "items");
		if (!current_frames_items) goto end;

		PyObject *frames_ret = PyEval_CallObject(current_frames_items, (PyObject *)NULL);
		if (!frames_ret) goto end3;

		PyObject *frames_iter = PyObject_GetIter(frames_ret);
		if (!frames_iter) goto end4;


		// we have the first frame, lets parse it
		if (write(client_fd, "*** uWSGI Python tracebacker output ***\n\n", 41) < 0) {
			uwsgi_error("write()");
		}
		PyObject *frame = PyIter_Next(frames_iter);
		while(frame) {
			PyObject *thread_id = PyTuple_GetItem(frame, 0);
			if (!thread_id) goto next2;

			PyObject *stack = PyTuple_GetItem(frame, 1);
			if (!stack) goto next2;

			PyObject *arg_tuple = PyTuple_New(1);
			PyTuple_SetItem(arg_tuple, 0, stack);
			Py_INCREF(stack);
			PyObject *stacktrace = PyEval_CallObject( extract_stack, arg_tuple);
			Py_DECREF(arg_tuple);
			if (!stacktrace) goto next2;
			
			PyObject *stacktrace_iter = PyObject_GetIter(stacktrace);
			if (!stacktrace_iter) { Py_DECREF(stacktrace); goto next2;}

			PyObject *st_items = PyIter_Next(stacktrace_iter);
			// we have the first traceback item
			while(st_items) {
#ifdef PYTHREE
				int thread_name_need_free = 0;
#endif
				PyObject *st_filename = PyTuple_GetItem(st_items, 0);
				if (!st_filename) { Py_DECREF(st_items); goto next; }
				PyObject *st_lineno = PyTuple_GetItem(st_items, 1);
				if (!st_lineno) {Py_DECREF(st_items); goto next;}
				PyObject *st_name = PyTuple_GetItem(st_items, 2);
				if (!st_name) {Py_DECREF(st_items); goto next;}

				PyObject *st_line = PyTuple_GetItem(st_items, 3);

				iov[0].iov_base = "thread_id = ";
				iov[0].iov_len = 12;

				iov[1].iov_base = uwsgi_python_get_thread_name(thread_id);
				if (!iov[1].iov_base) {
					iov[1].iov_base = "<UnnamedPythonThread>";
				}
#ifdef PYTHREE
				else {
					thread_name_need_free = 1;
				}
#endif
				iov[1].iov_len = strlen(iov[1].iov_base);

				iov[2].iov_base = " filename = ";
				iov[2].iov_len = 12;

#ifdef PYTHREE
				PyObject *st_filename_utf8 = PyUnicode_AsUTF8String(st_filename);
				if (!st_filename_utf8) {
					if (thread_name_need_free) free(iov[1].iov_base);
					goto next;
				}
				iov[3].iov_base = PyString_AsString(st_filename_utf8);
#else
				iov[3].iov_base = PyString_AsString(st_filename);
#endif
				iov[3].iov_len = strlen(iov[3].iov_base);

				iov[4].iov_base = " lineno = ";
				iov[4].iov_len = 10 ;

				iov[5].iov_base = uwsgi_num2str(PyInt_AsLong(st_lineno));
				iov[5].iov_len = strlen(iov[5].iov_base);

				iov[6].iov_base = " function = ";
				iov[6].iov_len = 12 ;

#ifdef PYTHREE
                                PyObject *st_name_utf8 = PyUnicode_AsUTF8String(st_name);
                                if (!st_name_utf8) {
					if (thread_name_need_free) free(iov[1].iov_base);
					Py_DECREF(st_filename_utf8);
					goto next;
				}
				iov[7].iov_base = PyString_AsString(st_name_utf8);
#else
				iov[7].iov_base = PyString_AsString(st_name);
#endif
                                iov[7].iov_len = strlen(iov[7].iov_base);

				iov[8].iov_base = "";
				iov[8].iov_len = 0 ;

				iov[9].iov_base = "";
				iov[9].iov_len = 0;

				iov[10].iov_base = "\n";
				iov[10].iov_len = 1;

#ifdef PYTHREE
				PyObject *st_line_utf8 = NULL;
#endif
				if (st_line) {
					iov[8].iov_base = " line = ";
					iov[8].iov_len = 8;
#ifdef PYTHREE
                                	PyObject *st_line_utf8 = PyUnicode_AsUTF8String(st_line);
                                	if (!st_line_utf8) {
						if (thread_name_need_free) free(iov[1].iov_base);
                                        	Py_DECREF(st_filename_utf8);
                                        	Py_DECREF(st_name_utf8);
                                        	goto next;
                                	}
					iov[9].iov_base = PyString_AsString(st_line_utf8);
#else
					iov[9].iov_base = PyString_AsString(st_line);
#endif
					iov[9].iov_len = strlen(iov[9].iov_base);
				}

				if (writev(client_fd, iov, 11) < 0) {
					uwsgi_error("writev()");
				}

				// free the line_no
				free(iov[5].iov_base);
				Py_DECREF(st_items);
#ifdef PYTHREE
				Py_DECREF(st_filename_utf8);
				Py_DECREF(st_name_utf8);
				if (st_line_utf8) {
					Py_DECREF(st_line_utf8);
				}
				if (thread_name_need_free)
					free(iov[1].iov_base);
#endif
				st_items = PyIter_Next(stacktrace_iter);
			}
			if (write(client_fd, "\n", 1) < 0) {
				uwsgi_error("write()");
			}
next:
			Py_DECREF(stacktrace_iter);
			Py_DECREF(stacktrace);
next2:
			Py_DECREF(frame);
			frame = PyIter_Next(frames_iter);
		}

		Py_DECREF(frames_iter);
end4:	
		Py_DECREF(frames_ret);
end3:	
		Py_DECREF(current_frames_items);
end:
		Py_DECREF(current_frames);
end2:
		close(client_fd);
	}
	return NULL;
}
Beispiel #5
0
static LIST*
call_python_function(RULE* r, FRAME* frame)
{
    LIST* result = 0;
    PyObject* arguments = PyTuple_New(frame->args->count);
    int i ;
    PyObject* py_result;

    for(i = 0; i < frame->args->count; ++i)
    {
        PyObject* arg = PyList_New(0);
        LIST* l = lol_get( frame->args, i);

        for(; l; l = l->next)
        {
            PyObject* v = PyString_FromString(l->string);
            /* Steals reference to 'v' */
            PyList_Append(arg, v);            
        }
        /* Steals reference to 'arg' */
        PyTuple_SetItem(arguments, i, arg);
    }

    py_result = PyObject_CallObject(r->python_function, arguments);
    Py_DECREF(arguments);
    if (py_result != NULL) {
        
        if (PyList_Check(py_result)) {
            int size = PyList_Size(py_result);
            int i;
            for(i = 0; i < size; ++i)
            {
                PyObject* item = PyList_GetItem(py_result, i);
                if (PyString_Check(item))
                {
                    result = list_new(result, 
                                      newstr(PyString_AsString(item)));
                }
                else
                {
                    fprintf(stderr, "Non-string object returned by Python call\n");
                }
            }
        }
        else if (PyInstance_Check(py_result))
        {
            static char instance_name[1000];
            static char imported_method_name[1000];
            module_t* m;
            PyObject* method;
            PyObject* method_name = PyString_FromString("foo");
            RULE* r;

            fprintf(stderr, "Got instance!\n");

            snprintf(instance_name, 1000,
                     "pyinstance%d", python_instance_number);
            snprintf(imported_method_name, 1000,
                     "pyinstance%d.foo", python_instance_number);
            ++python_instance_number;
            
            m = bindmodule(instance_name);

            /* This is expected to get bound method. */
            method = PyObject_GetAttr(py_result, method_name);
            
            r = bindrule( imported_method_name, root_module() );

            r->python_function = method;

            result = list_new(0, newstr(instance_name));    

            Py_DECREF(method_name);
        }
        else if (py_result == Py_None)
        {
            result = L0;
        }
        else
        {
            fprintf(stderr, "Non-list object returned by Python call\n");
        }

        Py_DECREF(py_result);
    }
    else {
        PyErr_Print();
        fprintf(stderr,"Call failed\n");
    }
    
    return result;
}
Beispiel #6
0
PyObject *uwsgi_paste_loader(void *arg1) {

    char *paste = (char *) arg1;
    PyObject *paste_module, *paste_dict, *paste_loadapp;
    PyObject *paste_arg, *paste_app;

    uwsgi_log( "Loading paste environment: %s\n", paste);

    if (up.paste_logger) {
        PyObject *paste_logger_dict = get_uwsgi_pydict("paste.script.util.logging_config");
        if (paste_logger_dict) {
            PyObject *paste_logger_fileConfig = PyDict_GetItemString(paste_logger_dict, "fileConfig");
            if (paste_logger_fileConfig) {
                PyObject *paste_logger_arg = PyTuple_New(1);
                if (!paste_logger_arg) {
                    PyErr_Print();
                    exit(UWSGI_FAILED_APP_CODE);
                }
                PyTuple_SetItem(paste_logger_arg, 0, UWSGI_PYFROMSTRING(paste+7));
                if (python_call(paste_logger_fileConfig, paste_logger_arg, 0, NULL)) {
                    PyErr_Print();
                }
            }
        }
    }

    paste_module = PyImport_ImportModule("paste.deploy");
    if (!paste_module) {
        PyErr_Print();
        exit(UWSGI_FAILED_APP_CODE);
    }

    paste_dict = PyModule_GetDict(paste_module);
    if (!paste_dict) {
        PyErr_Print();
        exit(UWSGI_FAILED_APP_CODE);
    }

    paste_loadapp = PyDict_GetItemString(paste_dict, "loadapp");
    if (!paste_loadapp) {
        PyErr_Print();
        exit(UWSGI_FAILED_APP_CODE);
    }

    paste_arg = PyTuple_New(1);
    if (!paste_arg) {
        PyErr_Print();
        exit(UWSGI_FAILED_APP_CODE);
    }

    if (PyTuple_SetItem(paste_arg, 0, UWSGI_PYFROMSTRING(paste))) {
        PyErr_Print();
        exit(UWSGI_FAILED_APP_CODE);
    }

    paste_app = PyEval_CallObject(paste_loadapp, paste_arg);
    if (!paste_app) {
        PyErr_Print();
        exit(UWSGI_FAILED_APP_CODE);
    }


    return paste_app;
}
/**
 ******************************************************************************************************
 * Iterates over the hosts in the cluster and creates the list to be returned to the python client.
 *
 * @param err                   as_error object
 * @param command               Request string sent from the python client
 * @param nodes_tuple           List containing details of each host
 * @param return_value          List t o be returned back to the python client
 * @param host_index            Index of the list nodes_tuple
 * @param index                 Index of the list to be returned.
 *
 * Returns information about a host.
 ********************************************************************************************************/
static PyObject * AerospikeClient_GetNodes_Returnlist(as_error* err,
	PyObject * command, PyObject * nodes_tuple[], PyObject * return_value,
	uint32_t host_index, Py_ssize_t index) {

	char* tok = NULL;
	char* saved = NULL;
	PyObject * value_tok = NULL;
	bool break_flag = false;

	tok = strtok_r(PyStr_AsString(command), INFO_REQUEST_RESPONSE_DELIMITER, &saved);
	if (tok == NULL) {
		as_error_update(err, AEROSPIKE_ERR_CLIENT, "Unable to get addr in service");
		goto CLEANUP;
	}
	while (tok != NULL && (host_index < MAX_HOST_COUNT)) {
		tok = strtok_r(NULL, IP_PORT_DELIMITER, &saved);
#if defined(__APPLE__)
		if (tok == NULL || saved == NULL) {
#else
		if (tok == NULL || *saved == '\0') {
#endif
			goto CLEANUP;
		}

		nodes_tuple[host_index] = PyTuple_New(2);

		value_tok = PyStr_FromString(tok);
		PyTuple_SetItem(nodes_tuple[host_index], 0 , value_tok);
		//Py_DECREF(value_tok);

		if(strcmp(PyStr_AsString(command),"response_services_p")) {
			tok = strtok_r(NULL, HOST_DELIMITER, &saved);
			if (tok == NULL) {
				as_error_update(err, AEROSPIKE_ERR_CLIENT, "Unable to get port");
				goto CLEANUP;
			}

			if (strstr(tok, INFO_RESPONSE_END)) {
				tok = strtok_r(tok, INFO_RESPONSE_END, &saved);
				break_flag = true;
			}
		} else {
			tok = strtok_r(NULL, INFO_RESPONSE_END, &saved);
			if (tok == NULL) {
				as_error_update(err, AEROSPIKE_ERR_CLIENT, "Unable to get port in service");
				goto CLEANUP;
			}
		}

		value_tok = PyInt_FromString(tok, NULL, 10);
		PyTuple_SetItem(nodes_tuple[host_index], 1 , value_tok);
		PyList_Insert(return_value, index , nodes_tuple[host_index]);
		Py_DECREF(nodes_tuple[host_index]);
		index++;
		host_index++;

		if (break_flag == true) {
			goto CLEANUP;
		}

	}
CLEANUP:

	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);
		return NULL;
	}
	return return_value;
}
/**
 ******************************************************************************************************
 * Returns data about the nodes to AerospikeClient_GetNodes.
 *
 * @param self                  AerospikeClient object
 *
 * Returns a list containing the details of the nodes.
 ********************************************************************************************************/
static PyObject * AerospikeClient_GetNodes_Invoke(
	AerospikeClient * self) {

	PyObject * response_services_p = NULL;
	PyObject * response_service_p = NULL;
	PyObject * nodes_tuple[MAX_HOST_COUNT] = {0};
	PyObject * return_value = PyList_New(0);

	as_error err;
	as_error_init(&err);

	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;
	}

	PyObject * py_req_str = NULL;
	py_req_str = PyStr_FromString("services");
	response_services_p = AerospikeClient_InfoNode_Invoke(self, py_req_str, NULL, NULL);
	Py_DECREF(py_req_str);
	if(!response_services_p) {
		as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Services call returned an error");
		goto CLEANUP;
	}

	py_req_str = PyStr_FromString("service");
	response_service_p = AerospikeClient_InfoNode_Invoke(self, py_req_str, NULL, NULL);
	Py_DECREF(py_req_str);
	if(!response_service_p) {
		as_error_update(&err, AEROSPIKE_ERR_CLIENT, "Service call returned an error");
		goto CLEANUP;
	}

	return_value = AerospikeClient_GetNodes_Returnlist(&err, response_service_p, nodes_tuple, return_value, 0, 0);
	if( return_value )
		return_value = AerospikeClient_GetNodes_Returnlist(&err, response_services_p, nodes_tuple, return_value, 1, 1);

CLEANUP:

	if(response_services_p) {
		Py_DECREF(response_services_p);
	}

	if(response_service_p) {
		Py_DECREF(response_service_p);
	}

	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);
		return NULL;
	}

	return return_value;
}
static PyObject *
coordinator_convert(PyObject * self, PyObject * args)
{
    char * shape_name;
    double extent[4];
    int pixel_width = 0;
    int pixel_height = 0;
    int fixed_ratio = 1;
    int offset_x = 0;
    int offset_y = 0;
    int pan_offset_x = 0;
    int pan_offset_y = 0;
    double scaleX = 0.9;
    double scaleY = 0.9;
    double ratioX = 0.0;
    double ratioY = 0.0;
    int startX = 0;
    int startY = 0;
    int margin_x = 0;
    int margin_y = 0;
    int offset_from_upperleft = 1;

    SHPHandle hSHP;
    PyObject * cobject;
    PyObject * region;
    PyObject * zoom_region;

    if (!PyArg_ParseTuple(args, "O!siiiiiiOO", &PyCObject_Type,
        &cobject, 
        &shape_name,
        &pixel_width, 
        &pixel_height, 
        &offset_x, 
        &offset_y, 
        &pan_offset_x, 
        &pan_offset_y, 
        &region,
        &zoom_region))
        return NULL;
    
    hSHP = PyCObject_AsVoidPtr(cobject);
    
    int nEntities;
    int nShapeType;
    double adfBoundsMin[4], adfBoundsMax[4]; 
    int i,j,k,s,nParts,count,numPoints;
        
    SHPGetInfo(hSHP, &nEntities, &nShapeType, adfBoundsMin, adfBoundsMax);
    extent[0] = adfBoundsMin[0];
    extent[1] = adfBoundsMin[1];
    extent[2] = adfBoundsMax[0];
    extent[3] = adfBoundsMax[1];

    if (PyList_Size(region) > 0)
    {
        for (i =0; i<4;++i)        
            extent[i] = PyFloat_AsDouble(PyList_GetItem(region,i));
    }
    
    double view_width = fabs(extent[0] - extent[2]);
    double view_height = fabs(extent[1] - extent[3]);

    if (fixed_ratio == 1)
    {
        if ((double)(pixel_width)/(double)(pixel_height) < view_width/view_height)
        {
            ratioX = pixel_width/view_width;
            ratioY = ratioX;
            startY = (int)((pixel_height-view_height*ratioY)/2.0);
        }
        else
        {
            ratioY = pixel_height/view_height;
            ratioX = ratioY;
            startX = (int)((pixel_width-view_width*ratioX)/2.0);
        }
    }
    else
    {
        ratioX = pixel_width / view_width;
        ratioY = pixel_height / view_height;
    }

    margin_x = (int)(pixel_width * (1-scaleX) / 2.0); 
    margin_y = (int)(pixel_height* (1-scaleY) / 2.0); 
   
    double x,y,tmp_px,tmp_py,prev_x,prev_y,one_pixel_view_x,one_pixel_view_y;
    
    // get zoom parameters
    if (PyList_Size(zoom_region) > 0)
    {
        for (i =0; i<4;++i)        
            extent[i] = PyFloat_AsDouble(PyList_GetItem(zoom_region,i));
            
        view_width = fabs(extent[0] - extent[2]);
        view_height = fabs(extent[1] - extent[3]);
    
        if ((double)(pixel_width)/(double)(pixel_height) < view_width/view_height)
        {
            ratioX = pixel_width/view_width;
            ratioY = ratioX;
            startY = (int)((pixel_height-view_height*ratioY)/2.0);
        }
        else
        {
            ratioY = pixel_height/view_height;
            ratioX = ratioY;
            startX = (int)((pixel_width-view_width*ratioX)/2.0);
        }
    
        margin_x = (int)(pixel_width * (1-scaleX) / 2.0); 
        margin_y = (int)(pixel_height* (1-scaleY) / 2.0); 
    }
    
    // converter coordinators
    PyObject * objects = PyDict_GetItemString(map_dict, shape_name); 
    
    if (objects == NULL)
        return PyList_New(0);
   
    PyObject * list = NULL, * parts = NULL, * point = NULL;
    PyObject *vertices = NULL, *px = NULL, *py =NULL;
    PyObject *_point=NULL,*_px=NULL,*_py=NULL;
    PyObject *polygon=NULL,*part = NULL;

    list = PyList_New(nEntities);
    
    for (i=0; i< nEntities; i++)
    {   
        if (nShapeType == SHPT_POINT || nShapeType == SHPT_POINTZ)
        {
            _point = PyList_GetItem(objects, i);            
            _px = PyTuple_GetItem(_point, 0); 
            _py = PyTuple_GetItem(_point, 1); 
           
            x = PyFloat_AsDouble(_px); 
            y = PyFloat_AsDouble(_py); 
       
            tmp_px = pan_offset_x + startX + ratioX * (x - extent[0]);
            tmp_px = round(offset_x + tmp_px * scaleX + margin_x);
            
            tmp_py = pixel_height - pan_offset_y - startY - ratioY * (y-extent[1]);
            tmp_py = round(offset_y + tmp_py * scaleY + margin_y);

            px = PyInt_FromLong((int)tmp_px);
            py = PyInt_FromLong((int)tmp_py);
            point = PyTuple_New(2);
            
            PyTuple_SetItem(point, 0, px);
            PyTuple_SetItem(point, 1, py);
                
            PyList_SetItem(list, i, point);
        }
        else if (nShapeType == SHPT_POLYGON || nShapeType == SHPT_POLYGONZ)
        {  
            polygon = PyList_GetItem(objects, i);
            nParts = (int)PyList_Size(polygon);
            
            parts = PyList_New(nParts);
            
            for (j=0; j < nParts; j++)
            { 
                part = PyList_GetItem(polygon, j);
                numPoints = (int)PyList_Size(part);
                
                vertices = PyTuple_New(numPoints);
                for (k=0; k < numPoints; k++)
                {
                    _point = PyList_GetItem(part, k);
                    _px = PyTuple_GetItem(_point, 0); 
                    _py = PyTuple_GetItem(_point, 1); 
                    
                    x = PyFloat_AsDouble(_px); 
                    y = PyFloat_AsDouble(_py); 
           
                    tmp_px = pan_offset_x + startX + ratioX * (x - extent[0]);
                    tmp_px = offset_x + tmp_px * scaleX + margin_x;
            
                    tmp_py = pixel_height - pan_offset_y - startY - ratioY * (y-extent[1]);
                    tmp_py = offset_y + tmp_py * scaleY + margin_y;

                    px = PyInt_FromLong((int)tmp_px);
                    py = PyInt_FromLong((int)tmp_py);
                    point = PyTuple_New(2);
                    PyTuple_SetItem(point, 0, px);
                    PyTuple_SetItem(point, 1, py);
                    PyTuple_SetItem(vertices, k, point);
                }
                PyList_SetItem(parts,j,vertices);
            }   
            PyList_SetItem(list, i, parts);
        }
    }    
    return list;
}
static PyObject *
read_centroids(PyObject * self, PyObject * args)
{
    SHPHandle hSHP;
    PyObject * cobject;

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

    hSHP = PyCObject_AsVoidPtr(cobject);
    
    int nEntities;
    int nShapeType;

    SHPGetInfo(hSHP, &nEntities, &nShapeType, NULL, NULL);
    PyObject * list = NULL, * parts = NULL, * point = NULL, *px = NULL, *py = NULL;

    SHPObject * psCShape;
    int i,j,count;
    double x,y;

    list = PyList_New(nEntities);
   
    for (i=0; i< nEntities; i++)
    {   
        psCShape = SHPReadObject( hSHP, i);   
    
        if (psCShape->nSHPType == SHPT_POINT || psCShape->nSHPType == SHPT_POINTZ)
        {
            /*
            x = psCShape->centroidXX[0];
            y = psCShape->centroidYY[0];
            point = Py_BuildValue("(dd)", x,y);
            PyList_SetItem(list, i, point);
            */
            return PyList_New(0);
        }
        else if (psCShape->nSHPType == SHPT_POLYGON || psCShape->nSHPType == SHPT_POLYGONZ)
        {   
            // read polygons from shp file
            count = psCShape->nParts > 1 ? psCShape->nParts : 1;
            parts = PyList_New(count);

            for (j=0; j <psCShape->nParts; j++)
            { 
                x = psCShape->centroidXX[j];
                y = psCShape->centroidYY[j];
                
                //point = Py_BuildValue("(dd)", x,y);
                px = PyFloat_FromDouble(x);
                py = PyFloat_FromDouble(y);
                point = PyTuple_New(2);
                PyTuple_SetItem(point, 0, px);
                PyTuple_SetItem(point, 1, py);
                
                PyList_SetItem(parts,j,point);
            }   
            PyList_SetItem(list, i, parts);
        }
        SHPDestroyObject(psCShape);
    }    
    return list;
}
Beispiel #10
0
void embed_sim_init(gpi_sim_info_t *info)
{
    FENTER

    int i;

    /* Check that we are not already initialised */
    if (pEventFn)
        return;

    // Find the simulation root
    gpi_sim_hdl dut = gpi_get_root_handle(getenv("TOPLEVEL"));

    if (dut == NULL) {
        fprintf(stderr, "Unable to find root instance!\n");
        gpi_sim_end();
        return;
    }

    PyObject *cocotb_module, *cocotb_init, *cocotb_args, *cocotb_retval;
    PyObject *simlog_obj, *simlog_func;
    PyObject *argv_list, *argc, *arg_dict, *arg_value;

    cocotb_module = NULL;
    arg_dict = NULL;

    //Ensure that the current thread is ready to callthe Python C API
    PyGILState_STATE gstate = PyGILState_Ensure();

    if (get_module_ref(COCOTB_MODULE, &cocotb_module))
        goto cleanup;

    // Create a logger object
    simlog_obj = PyObject_GetAttrString(cocotb_module, "log");

    if (simlog_obj == NULL) {
        PyErr_Print();
        fprintf(stderr, "Failed to to get simlog object\n");
    }

    simlog_func = PyObject_GetAttrString(simlog_obj, "_printRecord");
    if (simlog_func == NULL) {
        PyErr_Print();
        fprintf(stderr, "Failed to get the _printRecord method");
        goto cleanup;
    }

    if (!PyCallable_Check(simlog_func)) {
        PyErr_Print();
        fprintf(stderr, "_printRecord is not callable");
        goto cleanup;
    }

    set_log_handler(simlog_func);

    Py_DECREF(simlog_func);

    simlog_func = PyObject_GetAttrString(simlog_obj, "_willLog");
    if (simlog_func == NULL) {
        PyErr_Print();
        fprintf(stderr, "Failed to get the _willLog method");
        goto cleanup;
    }

    if (!PyCallable_Check(simlog_func)) {
        PyErr_Print();
        fprintf(stderr, "_willLog is not callable");
        goto cleanup;
    }

    set_log_filter(simlog_func);

    argv_list = PyList_New(0);
    for (i = 0; i < info->argc; i++) {
        arg_value = PyString_FromString(info->argv[i]);
        PyList_Append(argv_list, arg_value);
    }

    arg_dict = PyModule_GetDict(cocotb_module);
    PyDict_SetItemString(arg_dict, "argv", argv_list);

    argc = PyInt_FromLong(info->argc);
    PyDict_SetItemString(arg_dict, "argc", argc);

    if (!PyCallable_Check(simlog_func)) {
        PyErr_Print();
        fprintf(stderr, "_printRecord is not callable");
        goto cleanup;
    }

    gpi_print_registered_impl();
    LOG_INFO("Running on %s version %s", info->product, info->version);
    LOG_INFO("Python interpreter initialised and cocotb loaded!");

    // Now that logging has been set up ok we initialise the testbench
    if (-1 == PyObject_SetAttrString(cocotb_module, "SIM_NAME", PyString_FromString(info->product))) {
        PyErr_Print();
        fprintf(stderr, "Unable to set SIM_NAME");
        goto cleanup;
    }

    // Set languare in use
    const char *lang = getenv("TOPLEVEL_LANG");
    if (!lang)
       fprintf(stderr, "You should really set TOPLEVEL_LANG to \"verilog/vhdl\"");
    else {
        if (-1 == PyObject_SetAttrString(cocotb_module, "LANGUAGE", PyString_FromString(lang))) {
            fprintf(stderr, "Unable to set LANGUAGE");
            goto cleanup;
        }
    }

    // Hold onto a reference to our _fail_test function
    pEventFn = PyObject_GetAttrString(cocotb_module, "_sim_event");

    if (!PyCallable_Check(pEventFn)) {
        PyErr_Print();
        fprintf(stderr, "cocotb._sim_event is not callable");
        goto cleanup;
    }
    Py_INCREF(pEventFn);

    cocotb_init = PyObject_GetAttrString(cocotb_module, "_initialise_testbench");         // New reference

    if (cocotb_init == NULL || !PyCallable_Check(cocotb_init)) {
        if (PyErr_Occurred())
            PyErr_Print();
        fprintf(stderr, "Cannot find function \"%s\"\n", "_initialise_testbench");
        Py_DECREF(cocotb_init);
        goto cleanup;
    }

    cocotb_args = PyTuple_New(1);
    PyTuple_SetItem(cocotb_args, 0, PyLong_FromLong((long)dut));        // Note: This function “steals” a reference to o.
    cocotb_retval = PyObject_CallObject(cocotb_init, cocotb_args);

    if (cocotb_retval != NULL) {
        LOG_DEBUG("_initialise_testbench successful");
        Py_DECREF(cocotb_retval);
    } else {
        PyErr_Print();
        fprintf(stderr,"Call failed\n");
        gpi_sim_end();
        goto cleanup;
    }

    FEXIT

cleanup:
    if (cocotb_module) {
        Py_DECREF(cocotb_module);
    }
    if (arg_dict) {
        Py_DECREF(arg_dict);
    }
    PyGILState_Release(gstate);
}
static PyObject* _adjust_players(PyObject* self, PyObject* args) {
	PyObject* list_obj;
	PyObject* seq;
	int size, i = 0;

	// get the list from the arguments
	// note: "O" flag means it's a python object
	if (!PyArg_ParseTuple(args, "O", &list_obj)) {
		return NULL;
	}

	// verify that it's actually a list
	seq = PySequence_Fast(list_obj, "expected a list");
	if (!seq) {
		return NULL;
	}

	// get the list size
	size = PySequence_Size(seq);
	if (size < 0) {
		return NULL;
	}

	// create a vector of players to hold our soon-to-be-created player objects
	std::vector<Player*> players;
	PyObject* py_tuple;
	for (i = 0; i < size; ++i) {
		// create a new player object
		Player* p = new Player();

		// get the current item from the list, which happens to be a tuple (mu, sigma, rank)
		py_tuple = PySequence_Fast_GET_ITEM(seq, i);


		// convert the tuple items into their c types
		p->mu = PyFloat_AsDouble(PyTuple_GetItem(py_tuple, 0));
		p->sigma = PyFloat_AsDouble(PyTuple_GetItem(py_tuple, 1));
		p->rank = (int)PyInt_AsLong(PyTuple_GetItem(py_tuple, 2));

		// add the player to the players vector
		players.push_back(p);
	}

	// run trueskill on the players
	TrueSkill ts;
	ts.adjust_players(players);

	// create the result list
	PyObject* result = PyList_New(size);
	for (i = 0; i < size; ++i) {
		// create a tuple and set it's values for the player
		PyObject* py_tuple;

		py_tuple = PyTuple_New(3);
		PyTuple_SetItem(py_tuple, 0, PyFloat_FromDouble(players[i]->mu));
		PyTuple_SetItem(py_tuple, 1, PyFloat_FromDouble(players[i]->sigma));
		PyTuple_SetItem(py_tuple, 2, PyInt_FromLong((long)players[i]->rank));

		// push the tuple onto the list
		PyList_SetItem(result, i, py_tuple);
	}

	// return the list
	return result;
}
static PyObject *
RtAudio_getDeviceInfo(PyRtAudio *self, PyObject *args)
{
  int device;
  RtAudioDeviceInfo info;

  PyObject *name = NULL;
  PyObject *probed = NULL;
  PyObject *outputChannels = NULL;
  PyObject *inputChannels = NULL;
  PyObject *duplexChannels = NULL;
  PyObject *isDefault = NULL;
  PyObject *sampleRates = NULL;
  PyObject *nativeFormats = NULL;
  PyObject *deviceInfo = NULL;


  if(!PyArg_ParseTuple(args, "i", &device))
    return NULL;

  try
    {
      info = self->rtaudio->getDeviceInfo(device);
    }
  catch(RtError &error)
    {
      PyErr_Format(RtAudioError, error.getMessageString());
      return NULL;
    }

  name = PyString_FromString(info.name.c_str());
  if(name == NULL) return NULL;
  
  if(info.probed)
    {
      probed = Py_True;
      Py_INCREF(Py_True);
    }
  else
    {
      probed = Py_False;
      Py_INCREF(Py_False);
    }
  
  outputChannels = PyInt_FromLong(info.outputChannels);
  if(outputChannels == NULL) goto fail;
  
  inputChannels = PyInt_FromLong(info.inputChannels);
  if(inputChannels == NULL) goto fail;
  
  duplexChannels = PyInt_FromLong(info.duplexChannels);
  if(duplexChannels == NULL) goto fail;
  
  if(info.isDefault)
    {
      isDefault = Py_True;
      Py_INCREF(Py_True);
    }
  else
    {
      isDefault = Py_False;
      Py_INCREF(Py_False);
    }
  
  sampleRates = PyTuple_New(info.sampleRates.size());
  if(sampleRates == NULL)
    goto fail;

  for(uint i=0; i < info.sampleRates.size(); i++)
    {
      PyObject *rate = PyInt_FromLong(info.sampleRates[i]);
      if(rate == NULL)
        goto fail;
      if(PyTuple_SetItem(sampleRates, i, rate))
        {
          Py_DECREF(rate);
          goto fail;
        }
    }

  nativeFormats = PyLong_FromUnsignedLong(info.nativeFormats);
  if(nativeFormats == NULL)
    return NULL;
  
  deviceInfo = PyDict_New();
  if(deviceInfo == NULL)
    goto fail;

  if(PyDict_SetItemString(deviceInfo, "name", name))
    goto fail;
  if(PyDict_SetItemString(deviceInfo, "probed", probed))
    goto fail;
  if(PyDict_SetItemString(deviceInfo, "outputChannels", outputChannels))
    goto fail;
  if(PyDict_SetItemString(deviceInfo, "inputChannels", inputChannels)) 
    goto fail;
  if(PyDict_SetItemString(deviceInfo, "deviceChannels", duplexChannels))
    goto fail;
  if(PyDict_SetItemString(deviceInfo, "isDefault", isDefault))
    goto fail;
  if(PyDict_SetItemString(deviceInfo, "sampleRates", sampleRates))
    goto fail;
  if(PyDict_SetItemString(deviceInfo, "nativeFormats", nativeFormats))
    goto fail;

  return deviceInfo;

 fail:
  Py_XDECREF(name);
  Py_XDECREF(probed);
  Py_XDECREF(outputChannels);
  Py_XDECREF(inputChannels);
  Py_XDECREF(duplexChannels);
  Py_XDECREF(isDefault);
  Py_XDECREF(sampleRates);
  Py_XDECREF(nativeFormats);
  Py_XDECREF(deviceInfo);
  return NULL;
}
Beispiel #13
0
PyObject *
ot_pvw_receive(PyObject *self, PyObject *args)
{
    PyObject *py_state, *py_choices, *py_return = NULL;
    struct dm_ddh_crs crs;
    struct dm_ddh_pk pk;
    struct ddh_sk sk;
    struct ddh_ctxt ctxt;
    struct state *st;
    int num_ots;
    unsigned int N, msglength, err = 0;
    double start, end;

    if (!PyArg_ParseTuple(args, "OOII", &py_state, &py_choices, &N, &msglength))
        return NULL;

    st = (struct state *) PyCapsule_GetPointer(py_state, NULL);
    if (st == NULL)
        return NULL;

    if (N != 2) {
        PyErr_SetString(PyExc_RuntimeError, "N must be 2");
        return NULL;
    }

    if ((num_ots = PySequence_Length(py_choices)) == -1)
        return NULL;

    start = current_time();
    // FIXME: choice of mode should not be hardcoded
    dm_ddh_crs_setup(&crs, EXT, &st->p);
    end = current_time();
    fprintf(stderr, "CRS setup: %f\n", end - start);

    dm_ddh_pk_setup(&pk);
    ddh_sk_setup(&sk);
    ddh_ctxt_setup(&ctxt);

    py_return = PyTuple_New(num_ots);

    start = current_time();
    for (int j = 0; j < num_ots; ++j) {
        unsigned int choice;
        // double start, end;

        choice = PyLong_AsLong(PySequence_GetItem(py_choices, j));

        // start = current_time();
        dm_ddh_keygen(&pk, &sk, choice, &crs, &st->p);
        // end = current_time();
        // fprintf(stderr, "ddh keygen: %f\n", end - start);

        // start = current_time();
        send_dm_ddh_pk(&pk, st);
        // end = current_time();
        // fprintf(stderr, "send dm ddh pk: %f\n", end - start);

        for (unsigned int b = 0; b <= 1; ++b) {
            char *msg;
            PyObject *str;

            // start = current_time();
            receive_ddh_ctxt(&ctxt, st);
            // end = current_time();
            // fprintf(stderr, "receive dm ddh ctxt: %f\n", end - start);

            // start = current_time();
            msg = dm_ddh_dec(&sk, &ctxt, &st->p);
            // end = current_time();
            // fprintf(stderr, "decrypt ddh ctxt: %f\n", end - start);

            str = PyString_FromStringAndSize(msg, msglength);
            free(msg);
            if (str == NULL) {
                err = 1;
                goto cleanup;
            }
            if (choice == b) {
                PyTuple_SetItem(py_return, j, str);
            }
        }
    }
    end = current_time();
    fprintf(stderr, "OT: %f\n", end - start);

 cleanup:
    ddh_ctxt_cleanup(&ctxt);
    ddh_sk_cleanup(&sk);
    dm_ddh_pk_cleanup(&pk);
    dm_ddh_crs_cleanup(&crs);

    if (err)
        return NULL;
    else
        return py_return;
}
Beispiel #14
0
static PyObject *
python_orbclients( PyObject *self, PyObject *args ) {
	char	*usage = "Usage: _orbclients(orb)\n";
	int	orbfd;
	Orbclient *oc = 0;
	double 	atime;
	int	nclients;
	int	iclient;
	int	rc;
	char	*ip;
	struct	in_addr addr;
	PyObject *obj;
	PyObject *client_obj;

	if( ! PyArg_ParseTuple( args, "i", &orbfd ) ) {

		if( ! PyErr_Occurred() ) {

			PyErr_SetString( PyExc_RuntimeError, usage );
		}

		return NULL;
	}

	rc = orbclients( orbfd, &atime, &oc, &nclients );

	if( rc < 0 ) {

		PyErr_SetString( PyExc_RuntimeError, "error querying orb clients" );

		return NULL;
	}

	obj = PyTuple_New( nclients );

	for( iclient = 0; iclient < nclients; iclient++ ) {

		memcpy( &addr.s_addr, oc[iclient].address, 4 );
		ip = inet_ntoa( addr );

		client_obj = PyDict_New();

		PyDict_SetItemString( client_obj, "lastpkt", Py_BuildValue( "f", oc[iclient].lastpkt ) );
		PyDict_SetItemString( client_obj, "started", Py_BuildValue( "f", oc[iclient].started ) );
		PyDict_SetItemString( client_obj, "read", PyInt_FromLong( (long) oc[iclient].read ) );
		PyDict_SetItemString( client_obj, "pid", Py_BuildValue( "i", oc[iclient].pid ) );
		PyDict_SetItemString( client_obj, "bytes", PyInt_FromLong( (long) oc[iclient].bytes ) );
		PyDict_SetItemString( client_obj, "packets", PyInt_FromLong( (long) oc[iclient].packets ) );
		PyDict_SetItemString( client_obj, "pktid", Py_BuildValue( "i", oc[iclient].pktid ) );
		PyDict_SetItemString( client_obj, "port", Py_BuildValue( "i", oc[iclient].port ) );
		PyDict_SetItemString( client_obj, "address", Py_BuildValue( "s", ip ) );
		PyDict_SetItemString( client_obj, "thread", Py_BuildValue( "i", oc[iclient].thread ) );
		PyDict_SetItemString( client_obj, "fd", Py_BuildValue( "i", oc[iclient].fd ) );
		PyDict_SetItemString( client_obj, "nreject", Py_BuildValue( "i", oc[iclient].nreject ) );
		PyDict_SetItemString( client_obj, "nselect", Py_BuildValue( "i", oc[iclient].nselect ) );
		PyDict_SetItemString( client_obj, "errors", Py_BuildValue( "i", oc[iclient].errors ) );
		PyDict_SetItemString( client_obj, "priority", Py_BuildValue( "i", oc[iclient].priority ) );
		PyDict_SetItemString( client_obj, "lastrequest", Py_BuildValue( "i", oc[iclient].lastrequest ) );
		PyDict_SetItemString( client_obj, "mymessages", Py_BuildValue( "i", oc[iclient].mymessages ) );
		PyDict_SetItemString( client_obj, "nrequests", PyInt_FromLong( (long) oc[iclient].nrequests ) );
		PyDict_SetItemString( client_obj, "nwrites", PyInt_FromLong( (long) oc[iclient].nwrites ) );
		PyDict_SetItemString( client_obj, "nreads", PyInt_FromLong( (long) oc[iclient].nreads ) );
		PyDict_SetItemString( client_obj, "written", PyInt_FromLong( (long) oc[iclient].written ) );
		PyDict_SetItemString( client_obj, "perm", PyString_FromFormat( "%c", oc[iclient].perm ) );
		PyDict_SetItemString( client_obj, "what", Py_BuildValue( "s", oc[iclient].what ) );
		PyDict_SetItemString( client_obj, "host", Py_BuildValue( "s", oc[iclient].host ) );
		PyDict_SetItemString( client_obj, "who", Py_BuildValue( "s", oc[iclient].who ) );
		PyDict_SetItemString( client_obj, "select", Py_BuildValue( "s", oc[iclient].select ) );
		PyDict_SetItemString( client_obj, "reject", Py_BuildValue( "s", oc[iclient].reject ) );

		PyTuple_SetItem( obj, iclient, client_obj );
	}

	return Py_BuildValue( "fO", atime, obj );
}
Beispiel #15
0
int main(int argc, char** argv)
{
    // 初始化Python
    //在使用Python系统前,必须使用Py_Initialize对其
    //进行初始化。它会载入Python的内建模块并添加系统路
    //径到模块搜索路径中。这个函数没有返回值,检查系统
    //是否初始化成功需要使用Py_IsInitialized。

    Py_Initialize();

    // 检查初始化是否成功
    if ( !Py_IsInitialized() )
    {
        return -1;
    }

    // 添加当前路径
    //把输入的字符串作为Python代码直接运行,返回0
    //表示成功,-1表示有错。大多时候错误都是因为字符串
    //中有语法错误。
    PyRun_SimpleString("import sys");
    PyRun_SimpleString("sys.path.append('./')");
    PyObject *pName,*pModule,*pDict,*pFunc,*pArgs;

    // 载入名为pytest的脚本
    pName = PyString_FromString("pytest");
    pModule = PyImport_Import(pName);
    if ( !pModule )
    {
        printf("can't find pytest.py");
        getchar();
        return -1;
    }
    pDict = PyModule_GetDict(pModule);
    if ( !pDict )
    {
        return -1;
    }

    // 找出函数名为add的函数
    pFunc = PyDict_GetItemString(pDict, "add");
    if ( !pFunc || !PyCallable_Check(pFunc) )
    {
        printf("can't find function [add]");
        getchar();
        return -1;
    }

    // 参数进栈
    *pArgs;
    pArgs = PyTuple_New(2);

    //  PyObject* Py_BuildValue(char *format, ...)
    //  把C++的变量转换成一个Python对象。当需要从
    //  C++传递变量到Python时,就会使用这个函数。此函数
    //  有点类似C的printf,但格式不同。常用的格式有
    //  s 表示字符串,
    //  i 表示整型变量,
    //  f 表示浮点数,
    //  O 表示一个Python对象。

    PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",3));
    PyTuple_SetItem(pArgs, 1, Py_BuildValue("l",4));

    // 调用Python函数
    PyObject_CallObject(pFunc, pArgs);

    //下面这段是查找函数foo 并执行foo
    pFunc = PyDict_GetItemString(pDict, "foo");
    if ( !pFunc || !PyCallable_Check(pFunc) )
    {
        printf("can't find function [foo]");
        getchar();
        return -1;
    }

    pArgs = PyTuple_New(1);
    PyTuple_SetItem(pArgs, 0, Py_BuildValue("l",2)); //

    PyObject_CallObject(pFunc, pArgs);


    Py_DECREF(pName);
    Py_DECREF(pArgs);
    Py_DECREF(pModule);

    // 关闭Python
    Py_Finalize();
    return 0;
} 
static PyObject *
read_objects(PyObject * self, PyObject * args)
{
    SHPHandle hSHP;
    PyObject * cobject;
    char * shape_name;

    if (!PyArg_ParseTuple(args, "O!s", &PyCObject_Type, &cobject, &shape_name))
        return NULL;

    hSHP = PyCObject_AsVoidPtr(cobject);
    
    // check if already in memory
    if (map_dict != NULL)
    {
        PyObject * result = PyDict_GetItemString(map_dict, shape_name);
        if (result != NULL)
            return result;
    }
    
    int nEntities;
    int nShapeType;

    SHPGetInfo(hSHP, &nEntities, &nShapeType, NULL, NULL);

    PyObject * list = NULL, * parts = NULL, * point = NULL, *vertices = NULL, *px = NULL, *py = NULL;

    SHPObject * psCShape;
    int i,j,k,s,count,numPoints;
    double x,y;

    list = PyList_New(nEntities);
   
    for (i=0; i< nEntities; i++)
    {   
        psCShape = SHPReadObject( hSHP, i);   
    
        if (psCShape->nSHPType == SHPT_POINT || psCShape->nSHPType == SHPT_POINTZ)
        {
            // read points from shp file
            numPoints = psCShape->nVertices; 
            
            for (j=0; j < numPoints; j++)
            {
                x = psCShape->padfX[j];
                y = psCShape->padfY[j];
       
                //point = Py_BuildValue("(dd)", x,y);
                px = PyFloat_FromDouble(x);
                py = PyFloat_FromDouble(y);
                point = PyTuple_New(2);
                PyTuple_SetItem(point, 0, px);
                PyTuple_SetItem(point, 1, py);
                
                PyList_SetItem(list, i, point);
            } 
        }
        else if (psCShape->nSHPType == SHPT_POLYGON || psCShape->nSHPType == SHPT_POLYGONZ)
        {   
            // read polygons from shp file
            count = psCShape->nParts > 1 ? psCShape->nParts : 1;
            parts = PyList_New(count);

            for (j=0; j <psCShape->nParts; j++)
            { 
                if ( j < psCShape->nParts - 1)
                    numPoints = psCShape->panPartStart[j+1] - psCShape->panPartStart[j];
                else
                    numPoints = psCShape->nVertices - psCShape->panPartStart[j];
               
                s = psCShape->panPartStart[j]; 
                vertices = PyList_New(numPoints);
    
                for (k=0; k < numPoints; k++)
                {
                    x = psCShape->padfX[s+k];
                    y = psCShape->padfY[s+k];
                                
                    px = PyFloat_FromDouble(x);
                    py = PyFloat_FromDouble(y);
                    point = PyTuple_New(2);
                    PyTuple_SetItem(point, 0, px);
                    PyTuple_SetItem(point, 1, py);
                    PyList_SetItem(vertices, k, point);
                }
                PyList_SetItem(parts,j,vertices);
            }   
            PyList_SetItem(list, i, parts);
        }
        SHPDestroyObject(psCShape);
    }    
    
    // store the list
    if (map_dict == NULL)
    {
        map_dict = PyDict_New();
    }
    PyDict_SetItemString(map_dict, shape_name, list);
    //Py_DECREF(list);
    
    return list;
}
Beispiel #17
0
static PyObject*
take_action(PyObject *arg_types, PyObject *children_list, int speculative, 
	    D_ParseNode *dd, PyObject *string_list, int n_children, 
	    struct D_Parser *parser, void **children, int pn_offset,
	    PyObject *action) {
  int i;
  int arg_count = PyList_Size(arg_types);
  D_ParserPyInterface *ppi = d_interface(parser);
  PyObject *arglist = PyTuple_New(arg_count);
  PyObject *globals_holder = NULL;
  PyObject *result = NULL;

  Py_INCREF(children_list);
  PyTuple_SetItem(arglist, 0, children_list);
  for (i=1; i<arg_count; i++) {
    PyObject *item = PyList_GetItem(arg_types, i);
    int type = PyInt_AsLong(item);
    if (type == 1) {
      PyTuple_SetItem(arglist, i, Py_BuildValue("i", speculative));
    }
    else if (type == 2) {
      if (!dd->user.inced_global_state) {
	dd->user.inced_global_state = 1;
      }
      globals_holder = PyList_New(1);
      Py_INCREF(dd->globals);
      PyList_SetItem(globals_holder, 0, dd->globals);
      PyTuple_SetItem(arglist, i, globals_holder);
    }
    else if (type == 3) {
      Py_INCREF(string_list);
      PyTuple_SetItem(arglist, i, string_list);
    }
    else if (type == 4) {
      PyObject* nodes = PyList_New(n_children);
      int j;
      for (j=0; j<n_children; j++) {
	PyList_SetItem(nodes, j, make_py_node(parser, D_PN(children[j], pn_offset)));
      }
      PyTuple_SetItem(arglist, i, nodes);
    }
    else if (type == 5) {
      PyTuple_SetItem(arglist, i, make_py_node(parser, dd));
    }
    else if (type == 6) {
      Py_INCREF(Py_None);
      PyTuple_SetItem(arglist, i, Py_None);
    }
    else if (type == 7) {
      Py_INCREF(ppi->self);
      PyTuple_SetItem(arglist, i, ppi->self);
    }
  }
  result = PyEval_CallObject(action, arglist);
  if (globals_holder) {
    Py_DECREF(dd->globals);
    dd->globals = PyList_GetItem(globals_holder, 0);
    Py_INCREF(dd->globals);
  }
  Py_DECREF(arglist);
  return result;
}
/**
 * @brief Sets the address where the request for the Apertium-APY will be sent
 *
 * The address will not be set if there is no response from an APY server <br>
 * pythonInit() must have been called before or an error will occur (the module is not loaded)
 * @param address Pointer to a string with the new address
 * @param port Pointer to a string with the new port. NULL if no port is needed
 * @param order Position this address will take in the list. Negative value to append at the end
 * @param force Any number that is not 0 indicates the function to forcefully change the address,
 * despite not receiving an answer from the server
 * @return 1 if the call was successful and the address was set, or 0 otherwise
 */
int setAPYAddress(char* address, char* port, int order, int force){
    char* msg;
    PyObject *pFunc, *pArg, *pArgs, *new_address;

    if (iface_module != NULL) {
        pFunc = PyObject_GetAttrString(iface_module, "setAPYAddress");

        if (pFunc) {
            pArgs = PyTuple_New(4);

            pArg = PyBytes_FromString(address);
            PyTuple_SetItem(pArgs, 0, pArg);

            pArg = port == NULL ? Py_None : PyBytes_FromString(port);
            PyTuple_SetItem(pArgs, 1, pArg);

            PyTuple_SetItem(pArgs, 2, order > -1 ? PyLong_FromLong(order) : Py_None);

            PyTuple_SetItem(pArgs, 3, force == 0 ? Py_False : Py_True);

            new_address = PyObject_CallObject(pFunc, pArgs);
            Py_XDECREF(pArgs);
            Py_XDECREF(pFunc);

            if (new_address != NULL){
                if(new_address != Py_None){
                    if(files_module != NULL){
                        pFunc = PyObject_GetAttrString(files_module, "setKey");

                        if (pFunc) {
                            pArgs = PyTuple_New(2);

                            PyTuple_SetItem(pArgs, 0, PyUnicode_FromString("apyAddress"));

                            PyTuple_SetItem(pArgs, 1, new_address);

                            PyObject_CallObject(pFunc, pArgs);

                            Py_XDECREF(new_address);
                        }
                        else{
                            Py_XDECREF(new_address);
                            return 0;
                        }
                    }
                    else{
                        notify_error("Module: \'apertiumFiles\' is not loaded");
                        return 0;
                    }
                    return 1;
                }
                else{
                    msg = malloc(sizeof(char)*(strlen(address)+(port == NULL ? 0 : strlen(port))+100));
                    if(port != NULL){
                        sprintf(msg,"No response from server at %s:%s",address,port);
                    }
                    else{
                        sprintf(msg,"No response from server at %s",address);
                    }
                    notify_error(msg);
                    free(msg);
                    return 0;
                }
            }
            else {
                return 0;
            }
        }
        else {
            return 0;
        }
        Py_XDECREF(pFunc);
    }
    else {
        notify_error("Module: \'apertiumInterfaceAPY\' is not loaded");
        return 0;
    }
}
Beispiel #19
0
int init_uwsgi_app(int loader, void *arg1, struct wsgi_request *wsgi_req, PyThreadState *interpreter, int app_type) {

    PyObject *app_list = NULL, *applications = NULL;


    if (uwsgi_apps_cnt >= uwsgi.max_apps) {
        uwsgi_log("ERROR: you cannot load more than %d apps in a worker\n", uwsgi.max_apps);
        return -1;
    }


    int id = uwsgi_apps_cnt;

    int multiapp = 0;

    int i;

    struct uwsgi_app *wi;

    time_t now = uwsgi_now();

    if (uwsgi_get_app_id(NULL, wsgi_req->appid, wsgi_req->appid_len, -1) != -1) {
        uwsgi_log( "mountpoint %.*s already configured. skip.\n", wsgi_req->appid_len, wsgi_req->appid);
        return -1;
    }

    wi = &uwsgi_apps[id];

    memset(wi, 0, sizeof(struct uwsgi_app));
    wi->modifier1 = python_plugin.modifier1;
    wi->mountpoint_len = wsgi_req->appid_len < 0xff ? wsgi_req->appid_len : (0xff-1);
    strncpy(wi->mountpoint, wsgi_req->appid, wi->mountpoint_len);

    // dynamic chdir ?
    if (wsgi_req->chdir_len > 0) {
        strncpy(wi->chdir, wsgi_req->chdir, wsgi_req->chdir_len < 0xff ? wsgi_req->chdir_len : (0xff-1));
#ifdef UWSGI_DEBUG
        uwsgi_debug("chdir to %s\n", wi->chdir);
#endif
        if (chdir(wi->chdir)) {
            uwsgi_error("chdir()");
        }
    }

    // Initialize a new environment for the new interpreter

    // reload "os" environ to allow dynamic setenv()
    if (up.reload_os_env) {

        char **e, *p;
        PyObject *k, *env_value;

        PyObject *os_module = PyImport_ImportModule("os");
        if (os_module) {
            PyObject *os_module_dict = PyModule_GetDict(os_module);
            PyObject *py_environ = PyDict_GetItemString(os_module_dict, "environ");
            if (py_environ) {
                for (e = environ; *e != NULL; e++) {
                    p = strchr(*e, '=');
                    if (p == NULL) continue;

                    k = PyString_FromStringAndSize(*e, (int)(p-*e));
                    if (k == NULL) {
                        PyErr_Print();
                        continue;
                    }

                    env_value = PyString_FromString(p+1);
                    if (env_value == NULL) {
                        PyErr_Print();
                        Py_DECREF(k);
                        continue;
                    }

#ifdef UWSGI_DEBUG
                    uwsgi_log("%s = %s\n", PyString_AsString(k), PyString_AsString(env_value));
#endif

                    if (PyObject_SetItem(py_environ, k, env_value)) {
                        PyErr_Print();
                    }

                    Py_DECREF(k);
                    Py_DECREF(env_value);

                }

            }
        }
    }

    if (interpreter == NULL && id) {

        wi->interpreter = Py_NewInterpreter();
        if (!wi->interpreter) {
            uwsgi_log( "unable to initialize the new python interpreter\n");
            exit(1);
        }
        PyThreadState_Swap(wi->interpreter);
        init_pyargv();

        // we need to inizialize an embedded module for every interpreter
        init_uwsgi_embedded_module();
        init_uwsgi_vars();

    }
    else if (interpreter) {
        wi->interpreter = interpreter;
    }
    else {
        wi->interpreter = up.main_thread;
    }

    if (wsgi_req->home_len) {
        set_dyn_pyhome(wsgi_req->home, wsgi_req->home_len);
    }

    if (wsgi_req->touch_reload_len > 0 && wsgi_req->touch_reload_len < 0xff) {
        struct stat trst;
        strncpy(wi->touch_reload, wsgi_req->touch_reload, wsgi_req->touch_reload_len);
        if (!stat(wi->touch_reload, &trst)) {
            wi->touch_reload_mtime = trst.st_mtime;
        }
    }

    wi->callable = up.loaders[loader](arg1);

    if (!wi->callable) {
        uwsgi_log("unable to load app %d (mountpoint='%s') (callable not found or import error)\n", id, wi->mountpoint);
        goto doh;
    }

    // the module contains multiple apps
    if (PyDict_Check((PyObject *)wi->callable)) {
        applications = wi->callable;
        uwsgi_log("found a multiapp module...\n");
        app_list = PyDict_Keys(applications);
        multiapp = PyList_Size(app_list);
        if (multiapp < 1) {
            uwsgi_log("you have to define at least one app in the apllications dictionary\n");
            goto doh;
        }

        PyObject *app_mnt = PyList_GetItem(app_list, 0);
        if (!PyString_Check(app_mnt)) {
            uwsgi_log("the app mountpoint must be a string\n");
            goto doh;
        }
        char *tmp_mountpoint = PyString_AsString(app_mnt);
        wi->mountpoint_len = strlen(wi->mountpoint) < 0xff ? strlen(wi->mountpoint) : (0xff-1);
        strncpy(wi->mountpoint, tmp_mountpoint, wi->mountpoint_len);
        wsgi_req->appid = wi->mountpoint;
        wsgi_req->appid_len = wi->mountpoint_len;
#ifdef UWSGI_DEBUG
        uwsgi_log("main mountpoint = %s\n", wi->mountpoint);
#endif
        wi->callable = PyDict_GetItem(applications, app_mnt);
        if (PyString_Check((PyObject *) wi->callable)) {
            PyObject *callables_dict = get_uwsgi_pydict((char *)arg1);
            if (callables_dict) {
                wi->callable = PyDict_GetItem(callables_dict, (PyObject *)wi->callable);
            }
        }
    }

    Py_INCREF((PyObject *)wi->callable);

    wi->environ = malloc(sizeof(PyObject*)*uwsgi.cores);
    if (!wi->environ) {
        uwsgi_error("malloc()");
        exit(1);
    }

    for(i=0; i<uwsgi.cores; i++) {
        wi->environ[i] = PyDict_New();
        if (!wi->environ[i]) {
            uwsgi_log("unable to allocate new env dictionary for app\n");
            exit(1);
        }
    }

    wi->argc = 1;

    if (app_type == PYTHON_APP_TYPE_WSGI) {
#ifdef UWSGI_DEBUG
        uwsgi_log("-- WSGI callable selected --\n");
#endif
        wi->request_subhandler = uwsgi_request_subhandler_wsgi;
        wi->response_subhandler = uwsgi_response_subhandler_wsgi;
        wi->argc = 2;
    }
    else if (app_type == PYTHON_APP_TYPE_WEB3) {
#ifdef UWSGI_DEBUG
        uwsgi_log("-- Web3 callable selected --\n");
#endif
        wi->request_subhandler = uwsgi_request_subhandler_web3;
        wi->response_subhandler = uwsgi_response_subhandler_web3;
    }
    else if (app_type == PYTHON_APP_TYPE_PUMP) {
#ifdef UWSGI_DEBUG
        uwsgi_log("-- Pump callable selected --\n");
#endif
        wi->request_subhandler = uwsgi_request_subhandler_pump;
        wi->response_subhandler = uwsgi_response_subhandler_pump;
    }

    wi->args = malloc(sizeof(PyObject*)*uwsgi.cores);
    if (!wi->args) {
        uwsgi_error("malloc()");
        exit(1);
    }

    for(i=0; i<uwsgi.cores; i++) {
        wi->args[i] = PyTuple_New(wi->argc);
        if (!wi->args[i]) {
            uwsgi_log("unable to allocate new tuple for app args\n");
            exit(1);
        }

        // add start_response on WSGI app
        Py_INCREF((PyObject *)up.wsgi_spitout);
        if (app_type == PYTHON_APP_TYPE_WSGI) {
            if (PyTuple_SetItem(wi->args[i], 1, up.wsgi_spitout)) {
                uwsgi_log("unable to set start_response in args tuple\n");
                exit(1);
            }
        }
    }

    if (app_type == PYTHON_APP_TYPE_WSGI) {
        // prepare sendfile() for WSGI app
        wi->sendfile = PyCFunction_New(uwsgi_sendfile_method, NULL);

        wi->eventfd_read = PyCFunction_New(uwsgi_eventfd_read_method, NULL);
        wi->eventfd_write = PyCFunction_New(uwsgi_eventfd_write_method, NULL);
    }

    // cache most used values
    wi->error = PyFile_FromFile(stderr, "wsgi_errors", "w", NULL);
    Py_INCREF((PyObject *)wi->error);

    wi->gateway_version = PyTuple_New(2);
    PyTuple_SetItem(wi->gateway_version, 0, PyInt_FromLong(1));
    PyTuple_SetItem(wi->gateway_version, 1, PyInt_FromLong(0));
    Py_INCREF((PyObject *)wi->gateway_version);

    wi->uwsgi_version = PyString_FromString(UWSGI_VERSION);
    Py_INCREF((PyObject *)wi->uwsgi_version);

    wi->uwsgi_node = PyString_FromString(uwsgi.hostname);
    Py_INCREF((PyObject *)wi->uwsgi_node);

    if (uwsgi.threads > 1 && id) {
        // if we have multiple threads we need to initialize a PyThreadState for each one
        for(i=0; i<uwsgi.threads; i++) {
            //uwsgi_log("%p\n", uwsgi.core[i]->ts[id]);
            uwsgi.workers[uwsgi.mywid].cores[i].ts[id] = PyThreadState_New( ((PyThreadState *)wi->interpreter)->interp);
            if (!uwsgi.workers[uwsgi.mywid].cores[i].ts[id]) {
                uwsgi_log("unable to allocate new PyThreadState structure for app %s", wi->mountpoint);
                goto doh;
            }
        }
        PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_save_key) );
    }
    else if (interpreter == NULL && id) {
        PyThreadState_Swap(up.main_thread);
    }

    const char *default_app = "";

    if ((wsgi_req->appid_len == 0 || (wsgi_req->appid_len = 1 && wsgi_req->appid[0] == '/')) && uwsgi.default_app == -1) {
        default_app = " (default app)" ;
        uwsgi.default_app = id;
    }

    wi->started_at = now;
    wi->startup_time = uwsgi_now() - now;

    if (app_type == PYTHON_APP_TYPE_WSGI) {
        uwsgi_log( "WSGI app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app);
    }
    else if (app_type == PYTHON_APP_TYPE_WEB3) {
        uwsgi_log( "Web3 app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app);
    }
    else if (app_type == PYTHON_APP_TYPE_PUMP) {
        uwsgi_log( "Pump app %d (mountpoint='%.*s') ready in %d seconds on interpreter %p pid: %d%s\n", id, wi->mountpoint_len, wi->mountpoint, (int) wi->startup_time, wi->interpreter, (int) getpid(), default_app);
    }


    uwsgi_apps_cnt++;

    if (multiapp > 1) {
        for(i=1; i<multiapp; i++) {
            PyObject *app_mnt = PyList_GetItem(app_list, i);
            if (!PyString_Check(app_mnt)) {
                uwsgi_log("applications dictionary key must be a string, skipping.\n");
                continue;
            }

            wsgi_req->appid = PyString_AsString(app_mnt);
            wsgi_req->appid_len = strlen(wsgi_req->appid);
            PyObject *a_callable = PyDict_GetItem(applications, app_mnt);
            if (PyString_Check(a_callable)) {

                PyObject *callables_dict = get_uwsgi_pydict((char *)arg1);
                if (callables_dict) {
                    a_callable = PyDict_GetItem(callables_dict, a_callable);
                }
            }
            if (!a_callable) {
                uwsgi_log("skipping broken app %s\n", wsgi_req->appid);
                continue;
            }
            init_uwsgi_app(LOADER_CALLABLE, a_callable, wsgi_req, wi->interpreter, app_type);
        }
    }

    // emulate COW
    uwsgi_emulate_cow_for_apps(id);

    return id;

doh:
    if (PyErr_Occurred())
        PyErr_Print();
    if (interpreter == NULL && id) {
        Py_EndInterpreter(wi->interpreter);
        if (uwsgi.threads > 1) {
            PyThreadState_Swap((PyThreadState *) pthread_getspecific(up.upt_save_key));
        }
        else {
            PyThreadState_Swap(up.main_thread);
        }
    }
    return -1;
}
/**
 * @brief Initializes the Python environment
 *
 * Loads both the apertiumFiles and apertiumInterfaceAPY modules, which are used by the plugin.<br>
 * All the functions in this file require this to be first called in order to work properly
 * @param filename Name of the file where the preferences for the plugin will be stored
 */
void pythonInit(const char* filename){
    PyObject *pFunc, *pArgs, *addresses;

    Py_SetProgramName(NULL);
    Py_Initialize();

    files_module = PyImport_ImportModule("apertiumpluginutils.apertiumFiles");

    if (files_module != NULL) {
        pFunc = PyObject_GetAttrString(files_module, "setFile");

        if (pFunc) {
            pArgs = PyTuple_New(1);

            PyTuple_SetItem(pArgs, 0, PyUnicode_FromString(filename));

            PyObject_CallObject(pFunc, pArgs);
            Py_XDECREF(pArgs);
        }
        else {
            return;
        }
        Py_XDECREF(pFunc);

        pFunc = PyObject_GetAttrString(files_module, "read");

        if (pFunc) {
            pArgs = PyTuple_New(0);
            PyObject_CallObject(pFunc, pArgs);
            Py_XDECREF(pArgs);
        }
        else {
            return;
        }
        Py_XDECREF(pFunc);

        pFunc = PyObject_GetAttrString(files_module, "getKey");

        if (pFunc) {
            pArgs = PyTuple_New(1);

            PyTuple_SetItem(pArgs, 0, PyUnicode_FromString("apyAddress"));

            addresses = PyObject_CallObject(pFunc, pArgs);
            if(addresses == NULL || addresses == Py_None){
                Py_XDECREF(pArgs);
                Py_XDECREF(pFunc);
                return;
            }
            Py_XDECREF(pArgs);
        }
        else {
            return;
        }
        Py_XDECREF(pFunc);
    }
    else {
        notify_error_popup("Failed to load module: \'apertiumFiles\'");
        return;
    }

    iface_module = PyImport_ImportModule("apertiumpluginutils.apertiumInterfaceAPY");

    if (iface_module != NULL) {
        pFunc = PyObject_GetAttrString(iface_module, "setAPYList");

        if (pFunc) {
            pArgs = PyTuple_New(1);

            PyTuple_SetItem(pArgs, 0, addresses);

            if(PyObject_CallObject(pFunc, pArgs) == NULL){
                Py_XDECREF(pFunc);
                Py_XDECREF(addresses);
                notify_error_popup("Error on setAPYList(result is null)");
                return;
            }
        }
        else {
            notify_error_popup("Error on setAPYList(pfunc)");
            return;
        }
        Py_XDECREF(pFunc);
        Py_XDECREF(addresses);
    }
    else{
        notify_error_popup("Failed to load module: \'apertiumInterfaceAPY\'");
        return;
    }
}
int importPythonModule (char *binImg1_data,
                        int   binImg1_data_len,
                        char *binImg2_data,
                        char  binImg2_data_len)
{
    PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;

    int i;

    Py_Initialize();
    pName = PyUnicode_DecodeFSDefault("face");
    /* Error checking of pName left out */

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

    if (pModule != NULL)
    {
        /* pFunc is a new reference */
        pFunc = PyObject_GetAttrString(pModule, "get_images_from_cpp");

        if (pFunc && PyCallable_Check(pFunc))
        {
            pArgs = PyTuple_New(2);

            pValue = PyBytes_FromStringAndSize ( binImg2_data, binImg1_data_len);;

            if (!pValue)
            {
               Py_DECREF(pArgs);
               Py_DECREF(pModule);
               
               fprintf(stderr, "Cannot convert argument\n");
               return 1;
            }

            /* pValue reference stolen here: */
            PyTuple_SetItem(pArgs, 0, pValue);

            pValue = PyBytes_FromStringAndSize ( binImg2_data, binImg2_data_len);;

            if (!pValue)
            {
               Py_DECREF(pArgs);
               Py_DECREF(pModule);
      
               fprintf(stderr, "Cannot convert argument\n");
               return 1;
            }

            /* pValue reference stolen here: */
            PyTuple_SetItem(pArgs, 1, pValue);

            pValue = PyObject_CallObject(pFunc, pArgs);

            Py_DECREF(pArgs);

            if (pValue != NULL)
            {
                printf("Result of call: %f\n", PyLong_AsDouble(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", "get_images_from_cpp");
        }
      

        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
 
   }
      
   else
   {
      PyErr_Print();
      fprintf(stderr, "Failed to load \"%s\"\n", "face");
         
      return 1;
   }

   Py_Finalize();

   return 0;
}
static void python_process_event(int cpu, void *data,
				 int size __unused,
				 unsigned long long nsecs, char *comm)
{
	PyObject *handler, *retval, *context, *t, *obj;
	static char handler_name[256];
	struct format_field *field;
	unsigned long long val;
	unsigned long s, ns;
	struct event *event;
	unsigned n = 0;
	int type;
	int pid;

	t = PyTuple_New(MAX_FIELDS);
	if (!t)
		Py_FatalError("couldn't create Python tuple");

	type = trace_parse_common_type(data);

	event = find_cache_event(type);
	if (!event)
		die("ug! no event found for type %d", type);

	pid = trace_parse_common_pid(data);

	sprintf(handler_name, "%s__%s", event->system, event->name);

	s = nsecs / NSECS_PER_SEC;
	ns = nsecs - s * NSECS_PER_SEC;

	scripting_context->event_data = data;

	context = PyCObject_FromVoidPtr(scripting_context, NULL);

	PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
	PyTuple_SetItem(t, n++,
			PyCObject_FromVoidPtr(scripting_context, NULL));
	PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
	PyTuple_SetItem(t, n++, PyInt_FromLong(s));
	PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
	PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
	PyTuple_SetItem(t, n++, PyString_FromString(comm));

	for (field = event->format.fields; field; field = field->next) {
		if (field->flags & FIELD_IS_STRING) {
			int offset;
			if (field->flags & FIELD_IS_DYNAMIC) {
				offset = *(int *)(data + field->offset);
				offset &= 0xffff;
			} else
				offset = field->offset;
			obj = PyString_FromString((char *)data + offset);
		} else { /* FIELD_IS_NUMERIC */
			val = read_size(data + field->offset, field->size);
			if (field->flags & FIELD_IS_SIGNED) {
				if ((long long)val >= LONG_MIN &&
				    (long long)val <= LONG_MAX)
					obj = PyInt_FromLong(val);
				else
					obj = PyLong_FromLongLong(val);
			} else {
				if (val <= LONG_MAX)
					obj = PyInt_FromLong(val);
				else
					obj = PyLong_FromUnsignedLongLong(val);
			}
		}
		PyTuple_SetItem(t, n++, obj);
	}

	if (_PyTuple_Resize(&t, n) == -1)
		Py_FatalError("error resizing Python tuple");

	handler = PyDict_GetItemString(main_dict, handler_name);
	if (handler && PyCallable_Check(handler)) {
		retval = PyObject_CallObject(handler, t);
		if (retval == NULL)
			handler_call_die(handler_name);
	} else {
		handler = PyDict_GetItemString(main_dict, "trace_unhandled");
		if (handler && PyCallable_Check(handler)) {
			if (_PyTuple_Resize(&t, N_COMMON_FIELDS) == -1)
				Py_FatalError("error resizing Python tuple");

			retval = PyObject_CallObject(handler, t);
			if (retval == NULL)
				handler_call_die("trace_unhandled");
		}
	}

	Py_DECREF(t);
}
Beispiel #23
0
static PyObject *Py_FindObjects(PyObject *obj, PyObject *args)
{
    PyArrayObject *input = NULL;
    PyObject *result = NULL, *tuple = NULL, *start = NULL, *end = NULL;
    PyObject *slc = NULL;
    int jj;
    npy_intp max_label;
    npy_intp ii, *regions = NULL;

    if (!PyArg_ParseTuple(args, "O&n",
                          NI_ObjectToInputArray, &input, &max_label))
        goto exit;

    if (max_label < 0)
        max_label = 0;
    if (max_label > 0) {
        if (PyArray_NDIM(input) > 0) {
            regions = (npy_intp*)malloc(2 * max_label * PyArray_NDIM(input) *
                                        sizeof(npy_intp));
        } else {
            regions = (npy_intp*)malloc(max_label * sizeof(npy_intp));
        }
        if (!regions) {
            PyErr_NoMemory();
            goto exit;
        }
    }

    if (!NI_FindObjects(input, max_label, regions))
        goto exit;

    result = PyList_New(max_label);
    if (!result) {
        PyErr_NoMemory();
        goto exit;
    }

    for(ii = 0; ii < max_label; ii++) {
        npy_intp idx =
                PyArray_NDIM(input) > 0 ? 2 * PyArray_NDIM(input) * ii : ii;
        if (regions[idx] >= 0) {
            PyObject *tuple = PyTuple_New(PyArray_NDIM(input));
            if (!tuple) {
                PyErr_NoMemory();
                goto exit;
            }
            for(jj = 0; jj < PyArray_NDIM(input); jj++) {
                start = PyLong_FromSsize_t(regions[idx + jj]);
                end = PyLong_FromSsize_t(regions[idx + jj +
                                             PyArray_NDIM(input)]);
                if (!start || !end) {
                    PyErr_NoMemory();
                    goto exit;
                }
                slc = PySlice_New(start, end, NULL);
                if (!slc) {
                    PyErr_NoMemory();
                    goto exit;
                }
                Py_XDECREF(start);
                Py_XDECREF(end);
                start = end = NULL;
                PyTuple_SetItem(tuple, jj, slc);
                slc = NULL;
            }
            PyList_SetItem(result, ii, tuple);
            tuple = NULL;
        } else {
            Py_INCREF(Py_None);
            PyList_SetItem(result, ii, Py_None);
        }
    }

    Py_INCREF(result);

 exit:
    Py_XDECREF(input);
    Py_XDECREF(result);
    Py_XDECREF(tuple);
    Py_XDECREF(start);
    Py_XDECREF(end);
    Py_XDECREF(slc);
    free(regions);
    if (PyErr_Occurred()) {
        Py_XDECREF(result);
        return NULL;
    } else {
        return result;
    }
}
/**
 *******************************************************************************************************
 * This callback will be called with the results with aerospike_batch_exists().
 *
 * @param err                   Error object
 * @param records               An array of as_batch_read_record entries
 * @param py_recs               The pyobject to be filled in with the return
 *                              value
 *
 * Returns boolean value(true or false).
 *******************************************************************************************************
 */
static
void batch_exists_recs(as_error *err, as_batch_read_records* records, PyObject **py_recs)
{
	// Loop over records array
    as_vector* list = &records->list;
    for (uint32_t i = 0; i < list->size; i++) {
        as_batch_read_record* batch = as_vector_get(list, i);

		PyObject * rec = PyDict_New();
		PyObject * p_key = NULL;
		PyObject * py_rec = NULL;
        py_rec = PyTuple_New(2);
        p_key = PyTuple_New(4);

	    if ( batch->key.ns && strlen(batch->key.ns) > 0 ) {
		    PyTuple_SetItem(p_key, 0, PyStr_FromString(batch->key.ns));
	    }

	    if ( batch->key.set && strlen(batch->key.set) > 0 ) {
		    PyTuple_SetItem(p_key, 1, PyStr_FromString(batch->key.set));
	    }

		if ( batch->key.valuep ) {
			switch(((as_val*)(batch->key.valuep))->type){
				case AS_INTEGER:
					PyTuple_SetItem(p_key, 2, PyInt_FromLong((long)batch->key.value.integer.value));
					break;

				case AS_STRING:
					PyTuple_SetItem(p_key, 2, PyStr_FromString((const char *)batch->key.value.string.value));
					break;
				default:
					break;
			}
		} else {
			Py_INCREF(Py_None);
			PyTuple_SetItem(p_key, 2, Py_None);
		}

		if (batch->key.digest.init) {
            PyTuple_SetItem(p_key, 3, PyByteArray_FromStringAndSize((char *) batch->key.digest.value, AS_DIGEST_VALUE_SIZE));
        }

        PyTuple_SetItem(py_rec, 0, p_key);
		if ( batch->result == AEROSPIKE_OK ){

            PyObject *py_gen = PyInt_FromLong((long)batch->record.gen);
			PyDict_SetItemString( rec, "gen", py_gen );
            Py_DECREF(py_gen);
            PyObject *py_ttl = PyInt_FromLong((long)batch->record.ttl);
			PyDict_SetItemString( rec, "ttl", py_ttl );
            Py_DECREF(py_ttl);

            PyTuple_SetItem(py_rec, 1, rec);
			PyList_SetItem( *py_recs, i, py_rec );
		} else if (batch->result == AEROSPIKE_ERR_RECORD_NOT_FOUND){
		    Py_DECREF(rec);
			Py_INCREF(Py_None);
            PyTuple_SetItem(py_rec, 1, Py_None);
			PyList_SetItem( *py_recs, i, py_rec);
		}
	}
}
Beispiel #25
0
/**
 * @brief Python entry point for MSER
 *
 * @param pyArray
 * @param delta
 * @param max_area
 * @param min_area
 * @param max_variation
 * @param min_diversity
 * @return
 */
PyObject * vl_mser_python(
    PyArrayObject & pyArray,
    double delta,
    double max_area,
    double min_area,
    double max_variation,
    double min_diversity)
{
  // check data type
  assert(pyArray.descr->type_num == PyArray_UBYTE);
  assert(pyArray.flags & NPY_FORTRAN);

  unsigned char * data = (unsigned char *) pyArray.data;
  unsigned int width = pyArray.dimensions[0];
  unsigned int height = pyArray.dimensions[1];

  VlMserFilt *filt = 0;
  int i, j, dof, nframes, q;
  vl_uint const *regions;
  float const *frames;

  npy_intp nregions;

  // image dims
  const int ndims = 2;
  int dims[ndims];
  dims[0] = width;
  dims[1] = height;

  // mser filter
  filt = vl_mser_new(ndims, dims);

  // set parameters
  if (delta >= 0)
    vl_mser_set_delta(filt, (vl_mser_pix) delta);
  if (max_area >= 0)
    vl_mser_set_max_area(filt, max_area);
  if (min_area >= 0)
    vl_mser_set_min_area(filt, min_area);
  if (max_variation >= 0)
    vl_mser_set_max_variation(filt, max_variation);
  if (min_diversity >= 0)
    vl_mser_set_min_diversity(filt, min_diversity);

  // do mser computation
  vl_mser_process(filt, (vl_mser_pix*) data);

  // fit ellipses
  vl_mser_ell_fit(filt);

  // get results
  nregions = (npy_intp) vl_mser_get_regions_num(filt);
  regions = vl_mser_get_regions(filt);

  nframes = vl_mser_get_ell_num(filt);
  dof = vl_mser_get_ell_dof(filt);
  frames = vl_mser_get_ell(filt);

  // convert results to PyArrayObjects
  npy_intp odims[2];
  odims[0] = dof;
  odims[1] = nframes;

  // allocate pyarray objects
  PyArrayObject * _regions = (PyArrayObject*) PyArray_SimpleNew(
    1, (npy_intp*) &nregions, PyArray_DOUBLE);

  PyArrayObject * _frames = (PyArrayObject*) PyArray_NewFromDescr(
    &PyArray_Type, PyArray_DescrFromType(PyArray_DOUBLE),
    2, odims, NULL, NULL, NPY_F_CONTIGUOUS, NULL);

  // check if valid pointers
  assert(_regions);
  assert(_frames);

  // fill pyarray objects
  double * _regions_buf = (double *) _regions->data;
  for (i = 0; i < nregions; ++i) {
    _regions_buf[i] = regions[i];
  }

  double * _frames_buf = (double *) _frames->data;
  for (j = 0; j < dof; ++j) {
    for (i = 0; i < nframes; ++i) {
      _frames_buf[i * dof + j] = frames[i * dof + j]; //+ ((j < ndims) ? 1.0 : 0.0);
    }
  }

    // cleanup
    vl_mser_delete (filt) ;

  // construct tuple to return both results: (regions, frames)
  PyObject * tuple = PyTuple_New(2);
  PyTuple_SetItem(tuple, 0, PyArray_Return(_regions));
  PyTuple_SetItem(tuple, 1, PyArray_Return(_frames));

  return tuple;
}
/**
 *******************************************************************************************************
 * This callback will be called with the results with aerospike_batch_exists().
 *
 * @param results               An array of n as_batch_read entries
 * @param n                     The number of results from the batch request
 * @param udata                 The return value to be filled with result of
 *                              exists_many()
 *
 * Returns boolean value(true or false).
 *******************************************************************************************************
 */
static
bool batch_exists_cb(const as_batch_read* results, uint32_t n, void* udata)
{
    // Typecast udata back to PyObject
    PyObject * py_recs = (PyObject *) udata;

    // Loop over results array
    for ( uint32_t i =0; i < n; i++ ){

        PyObject * rec = PyDict_New();
        PyObject * py_rec = NULL;
        PyObject * p_key = NULL;
        py_rec = PyTuple_New(2);
        p_key = PyTuple_New(4);

	    if ( results[i].key->ns && strlen(results[i].key->ns) > 0 ) {
		    PyTuple_SetItem(p_key, 0, PyStr_FromString(results[i].key->ns));
	    }

	    if ( results[i].key->set && strlen(results[i].key->set) > 0 ) {
		    PyTuple_SetItem(p_key, 1, PyStr_FromString(results[i].key->set));
	    }

        if ( results[i].key->valuep ) {
            switch(((as_val*)(results[i].key->valuep))->type){
                case AS_INTEGER:
                    PyTuple_SetItem(p_key, 2, PyInt_FromLong((long)results[i].key->value.integer.value));
                    break;

                case AS_STRING:
                    PyTuple_SetItem(p_key, 2, PyStr_FromString((const char *)results[i].key->value.string.value));
                    break;
                default:
                    break;
            }
        } else {
            Py_INCREF(Py_None);
            PyTuple_SetItem(p_key, 2, Py_None);
        }
		if (results[i].key->digest.init) {
            PyTuple_SetItem(p_key, 3, PyByteArray_FromStringAndSize((char *) results[i].key->digest.value, AS_DIGEST_VALUE_SIZE));
        }

        PyTuple_SetItem(py_rec, 0, p_key);
        if ( results[i].result == AEROSPIKE_OK ){
            PyObject *py_gen = PyInt_FromLong((long)results[i].record.gen);
            PyDict_SetItemString( rec, "gen", py_gen );
            Py_DECREF(py_gen);
            PyObject *py_ttl = PyInt_FromLong((long)results[i].record.ttl);
            PyDict_SetItemString( rec, "ttl", py_ttl );
            Py_DECREF(py_ttl);

            PyTuple_SetItem(py_rec, 1, rec);
            if ( PyList_SetItem( py_recs, i, py_rec ) ){
                return false;
            }
        } else if (results[i].result == AEROSPIKE_ERR_RECORD_NOT_FOUND){
            Py_DECREF(rec);
            Py_INCREF(Py_None);
            PyTuple_SetItem(py_rec, 1, Py_None);

            if( PyList_SetItem( py_recs, i, py_rec)){
                return false;
            }
        }
    }
    return true;
}
Beispiel #27
0
static PyObject* sctp_recv_msg(PyObject* dummy, PyObject* args)
{
	int fd, max_len;

	struct sockaddr_storage sfrom;
	socklen_t sfrom_len = sizeof(sfrom);
	int family;
	int len;
	int port;
	char cfrom[256];
	char *msg;
	int size;
	int flags;
	struct sctp_sndrcvinfo sinfo;

	PyObject* notification = PyDict_New();
	PyObject* ret = 0;
	PyObject* oaddr = 0;
	
	if (! PyArg_ParseTuple(args, "ii", &fd, &max_len)) {
		return ret;
	}

	msg = malloc(max_len);
	if (! msg) {
		PyErr_SetString(PyExc_MemoryError, "Out of memory, malloc() failed");
		return ret;
	}

	bzero(&sfrom, sizeof(sfrom));
	bzero(&sinfo, sizeof(sinfo));

	size = sctp_recvmsg(fd, msg, max_len, (struct sockaddr*) &sfrom, &sfrom_len, &sinfo, &flags);

	if (size < 0) {
		free(msg);
		PyErr_SetFromErrno(PyExc_IOError);
		return ret;
	}

	if (flags & MSG_NOTIFICATION) {
		interpret_notification(notification, msg, size);
		size = -1;
	} else {
		interpret_sndrcvinfo(notification, &sinfo);
	}

	if (from_sockaddr((struct sockaddr*) &sfrom, &family, &len, &port, cfrom, sizeof(cfrom))) {
		oaddr = PyTuple_New(2);
		PyTuple_SetItem(oaddr, 0, PyString_FromString(cfrom));
		PyTuple_SetItem(oaddr, 1, PyInt_FromLong(port));
	} else {
		// something went wrong
		oaddr = Py_None;
		Py_INCREF(Py_None);
	}
			
	ret = PyTuple_New(4);
	PyTuple_SetItem(ret, 0, oaddr);
	PyTuple_SetItem(ret, 1, PyInt_FromLong(flags));
	if (size >= 0) {
		PyTuple_SetItem(ret, 2, PyString_FromStringAndSize(msg, size));
	} else {
		PyTuple_SetItem(ret, 2, Py_None);
		Py_INCREF(Py_None);
	}
	PyTuple_SetItem(ret, 3, notification);

	free(msg);
	return ret;
}
Beispiel #28
0
int
python_exec2(struct sip_msg *_msg, char *method_name, char *mystr)
{
    PyObject *pFunc, *pArgs, *pValue, *pResult;
    PyObject *msg;
    int rval;

    PyEval_AcquireLock();
    PyThreadState_Swap(myThreadState);

    pFunc = PyObject_GetAttrString(handler_obj, method_name);
    if (pFunc == NULL || !PyCallable_Check(pFunc)) {
        LM_ERR("%s not found or is not callable\n", method_name);
        Py_XDECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    msg = newmsgobject(_msg);
    if (msg == NULL) {
        LM_ERR("can't create MSGtype instance\n");
        Py_DECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

    pArgs = PyTuple_New(mystr == NULL ? 1 : 2);
    if (pArgs == NULL) {
        LM_ERR("PyTuple_New() has failed\n");
        msg_invalidate(msg);
        Py_DECREF(msg);
        Py_DECREF(pFunc);
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }
    PyTuple_SetItem(pArgs, 0, msg);
    /* Tuple steals msg */

    if (mystr != NULL) {
        pValue = PyString_FromString(mystr);
        if (pValue == NULL) {
            LM_ERR("PyString_FromString(%s) has failed\n", mystr);
            msg_invalidate(msg);
            Py_DECREF(pArgs);
            Py_DECREF(pFunc);
            PyThreadState_Swap(NULL);
            PyEval_ReleaseLock();
            return -1;
        }
        PyTuple_SetItem(pArgs, 1, pValue);
        /* Tuple steals pValue */
    }

    pResult = PyObject_CallObject(pFunc, pArgs);
    msg_invalidate(msg);
    Py_DECREF(pArgs);
    Py_DECREF(pFunc);
    if (PyErr_Occurred()) {
        Py_XDECREF(pResult);
        python_handle_exception("python_exec2");
        PyThreadState_Swap(NULL);
        PyEval_ReleaseLock();
        return -1;
    }

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

    rval = PyInt_AsLong(pResult);
    Py_DECREF(pResult);
    PyThreadState_Swap(NULL);
    PyEval_ReleaseLock();
    return rval;
}
Beispiel #29
0
void Pywrap::AddArgument (unsigned int argument)
{
    PyTuple_SetItem(args, index, PyInt_FromSize_t(argument));
    index++;
}
static gboolean wrap_frame_done_cb (GdkPixbufLoader *loader, 
				    GdkPixbufFrame *frame, 
				    gpointer data)
{
	PyObject *result, *func, *func_data, *frame_python, *loader_python;
	PyObject *func_data_orig;
	PyObject *crap;
	int check;
	long ret;
	int func_data_length;

	//PyGTK_BLOCK_THREADS
	dbg();
	func = (PyObject *) gtk_object_get_data (GTK_OBJECT (loader),
						"py_frame_done_cb_func");
        func_data_orig = (PyObject *) gtk_object_get_data (GTK_OBJECT (loader),
						"py_frame_done_cb_data");

	Py_XINCREF (func_data_orig);
	/*FIXME check if frame_python == NULL ? */
	frame_python = PyGdkPixbuf_FrameNew (frame);

	if (!PyTuple_Check (func_data_orig))
	{	dprint ("f**k, func_data_orig isn't a tuple.\n");
		PyErr_SetString (PyExc_TypeError, 
				"func_data_orig isn't a tuple");
		goto error;
	}

	func_data_length = PyTuple_Size (func_data_orig);
	dprint ("func_data_length == %d\n", func_data_length);

	func_data = PyTuple_New (func_data_length + 2);
	if (func_data == NULL)
		goto error;

	//if (!PyList_Check (func_data))
	//	dprint ("f**k, func_data isn't a list.\n");

	//Py_XINCREF (func_data);

	loader_python = PyGtk_New ((GtkObject *) loader);
	//Py_XINCREF (loader_python);

//	crap = PyTuple_GetItem (func_data, 0); //, loader_python);
//	PyObject_Print (crap, stderr, Py_PRINT_RAW);
//	Py_XDECREF (crap);
	check = PyTuple_SetItem (func_data, 0, loader_python);
	//check = PyList_SetItem (func_data, 0, loader_python);
	dprint ("tried to set item\n");
	if (check != 0)
	{	//dprint ("error appending Loader to arg tuple %d\n", check);
		//Py_XDECREF (func_data);
		//Py_XDECREF (frame_python);
		//return TRUE;
		//PyErr_SetString (PyExc_RuntimeError, 
		//		"error appending frame to arg tuple\n");
		goto error;
	}

	check = PyTuple_SetItem (func_data, 1, frame_python);
	//check = PyList_SetItem (func_data, 1, frame_python);
	if (check != 0)
	{	//dprint ("error appending frame to arg tuple %d\n", check);
		goto error;	
	}

	result = PyObject_CallObject (func, func_data);
	if (result == NULL)
	{	
		//PyErr_Print ();
		//PyErr_Clear ();
		//PyObject_Print (func, stderr, Py_PRINT_RAW);
		//dprint ("error calling function\n");

		goto error;
	}
	
	if (PyInt_Check (result))
		ret = PyInt_AsLong (result);
	else
	  {	
		dprint ("return value of cb isn't an int??\n");
		Py_XDECREF (result);
		PyErr_SetString (PyExc_RuntimeError, 
				"return value of cb isn't an int??\n"); 
		goto error;
	  }

	Py_XDECREF (result);
	return (gboolean) ret;

error:
	Py_XDECREF (func_data);
	Py_XDECREF (frame_python);
	//dprint ("error calling frame_done_cb\n");
	//if (0) //(PyGtk_FatalExceptions)
	if (PyGtk_FatalExceptions)
	  {	//PyErr_Print ();
		//PyErr_Clear ();
		gtk_main_quit ();
	  }
	else
	  {	PyErr_Print ();
		PyErr_Clear ();
	  }
	//PyGTK_UNBLOCK_THREADS
	return TRUE;
}