unsigned int KPythonList::Size() { PyLockGIL lock; return PyList_Size(this->list); }
static PyObject *robustLS_to_sdp (PyObject *self, PyObject *args, PyObject *kwrds) { PyObject *Alist,*bt, *Ai; spmatrix *A,*b; int_t m,n,mp,np,pt,i,j,k,N,nnz=0,ri=0; char *kwlist[] = {"Alist","bt",NULL}; if(!PyArg_ParseTupleAndKeywords(args,kwrds,"OO",kwlist,&Alist,&bt)) return NULL; if(!PyList_Check(Alist)) { PyErr_SetString(PyExc_TypeError,"Alist must be a list of matrices"); return NULL; } // get pt = p + 1 pt = PyList_Size(Alist); // get size of bt if(Matrix_Check(bt)){ m = MAT_NROWS(bt); np = MAT_NCOLS(bt); } else if (SpMatrix_Check(bt)){ m = SP_NROWS(bt); np = SP_NCOLS(bt); } else { PyErr_SetString(PyExc_TypeError,"b must be a vector"); return NULL; } if (np!=1) { PyErr_SetString(PyExc_TypeError,"b must be a vector"); return NULL; } // get n and check A0 if (!(Ai = PyList_GetItem(Alist,0))) return NULL; if (Matrix_Check(Ai)) { n = MAT_NCOLS(Ai); nnz += m*n; } else if (SpMatrix_Check(Ai)) { n = SP_NCOLS(Ai); nnz += SP_NNZ(Ai); } else { PyErr_SetString(PyExc_TypeError,"only spmatrix and matrix types allowed"); return NULL; } // check remaining matrices in Alist for (i=1;i<pt;i++) { if (!(Ai = PyList_GetItem(Alist,i))) return NULL; if (Matrix_Check(Ai)) { mp = MAT_NROWS(Ai); np = MAT_NCOLS(Ai); nnz += m*n; } else if (SpMatrix_Check(Ai)) { mp = SP_NROWS(Ai); np = SP_NCOLS(Ai); nnz += SP_NNZ(Ai); } else { PyErr_SetString(PyExc_TypeError,"only spmatrix and matrix types allowed"); return NULL; } if (!(mp==m && np==n)){ PyErr_SetString(PyExc_TypeError,"matrices in Alist must have same size"); return NULL; } } nnz += 2*m + pt; // generate b b = SpMatrix_New(n+2,1,2,DOUBLE); if (!b) return PyErr_NoMemory(); SP_COL(b)[0] = 0; SP_VALD(b)[0] = -1; SP_ROW(b)[0] = 0; SP_VALD(b)[1] = -1; SP_ROW(b)[1] = 1; SP_COL(b)[1] = 2; // generate A N = m+pt; A = SpMatrix_New(N*N,n+3,nnz,DOUBLE); if (!A) return PyErr_NoMemory(); // build A0 SP_COL(A)[0] = ri; for(i=0;i<m;i++){ if(SpMatrix_Check(bt)){ SP_VALD(A)[ri] = -SP_VALD(bt)[i]; SP_ROW(A)[ri++] = pt+i; } else{ SP_VALD(A)[ri] = -MAT_BUFD(bt)[i]; SP_ROW(A)[ri++] = pt+i; } } for(i=0;i<m;i++) { SP_VALD(A)[ri] = 1; SP_ROW(A)[ri++] = (N+1)*pt + i*N+i; } // build A1 SP_COL(A)[1] = ri; for(i=0;i<pt-1;i++){ SP_VALD(A)[ri] = -1; SP_ROW(A)[ri++] = N+1 + i*N+i; } // build A2 SP_COL(A)[2] = ri; SP_VALD(A)[ri] = -1; SP_ROW(A)[ri++] = 0; SP_COL(A)[3] = ri; // build A3,... for(j=0;j<n;j++){ // generate col. i for(i=0;i<pt;i++){ Ai = PyList_GetItem(Alist,i); if(SpMatrix_Check(Ai)) { nnz = SP_COL(Ai)[j+1]-SP_COL(Ai)[j]; for(k=0;k<nnz;k++) { SP_VALD(A)[ri] = -SP_VALD(Ai)[SP_COL(Ai)[j]+k]; SP_ROW(A)[ri++] = pt+i*N + SP_ROW(Ai)[SP_COL(Ai)[j]+k]; } } else { for (k=0;k<m;k++) { SP_VALD(A)[ri] = -MAT_BUFD(Ai)[j*m+k]; SP_ROW(A)[ri++] = pt+i*N + k; } } } SP_COL(A)[j+4] = ri; } return Py_BuildValue("NN",A,b); }
PyObject * Repository_create_commit(Repository *self, PyObject *args) { Signature *py_author, *py_committer; PyObject *py_oid, *py_message, *py_parents, *py_parent; PyObject *py_result = NULL; char *message = NULL; char *update_ref = NULL; char *encoding = NULL; git_oid oid; git_tree *tree = NULL; int parent_count; git_commit **parents = NULL; int err = 0, i = 0; size_t len; if (!PyArg_ParseTuple(args, "zO!O!OOO!|s", &update_ref, &SignatureType, &py_author, &SignatureType, &py_committer, &py_message, &py_oid, &PyList_Type, &py_parents, &encoding)) return NULL; len = py_oid_to_git_oid(py_oid, &oid); if (len == 0) goto out; message = py_str_to_c_str(py_message, encoding); if (message == NULL) goto out; err = git_tree_lookup_prefix(&tree, self->repo, &oid, len); if (err < 0) { Error_set(err); goto out; } parent_count = (int)PyList_Size(py_parents); parents = malloc(parent_count * sizeof(git_commit*)); if (parents == NULL) { PyErr_SetNone(PyExc_MemoryError); goto out; } for (; i < parent_count; i++) { py_parent = PyList_GET_ITEM(py_parents, i); len = py_oid_to_git_oid(py_parent, &oid); if (len == 0) goto out; err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len); if (err < 0) { Error_set(err); goto out; } } err = git_commit_create(&oid, self->repo, update_ref, py_author->signature, py_committer->signature, encoding, message, tree, parent_count, (const git_commit**)parents); if (err < 0) { Error_set(err); goto out; } py_result = git_oid_to_python(&oid); out: free(message); git_tree_free(tree); while (i > 0) { i--; git_commit_free(parents[i]); } free(parents); return py_result; }
/* _curs_doall() - execute an operation (commit or rollback) on all the cursors * * returns NULL on success, a python dictionary if at least one cursor failed. * the dictionary maps key: cursor/cursobject to value: * error/PyString. * Note: in case the dictionary could not be created (out of memory e.g.), * Py_None is returned instead. */ static PyObject* _curs_doall(connobject *self, int (*operation)(cursobject *) ) { int len, i, has_errors = 0; cursobject *cursor; doall_state_t *cursors = NULL; PyObject* errs = NULL; Dprintf("curs_doall: acquiring lock\n"); pthread_mutex_lock(&(self->lock)); Dprintf("curs_doall: lock acquired\n"); /* collect all the cursors, so we can use them while not holding the GIL * We keep a reference to the cursors, in case the self->cursors changes * during the call. */ len = PyList_Size(self->cursors); cursors = (doall_state_t *)malloc(len * sizeof (doall_state_t)); if (!cursors) { pthread_mutex_unlock(&(self->lock)); Dprintf("curs_doall: lock released\n"); return PyErr_NoMemory(); } for (i = 0; i < len; i++) { cursors[i].cursor = (cursobject *)PyList_GetItem(self->cursors, i); assert(cursors[i].cursor); Py_INCREF(cursors[i].cursor); cursors[i].errmsg = NULL; } Py_BEGIN_ALLOW_THREADS; Dprintf("curs_doall: %d cursors\n", len); /* acquire all the required locks */ for (i = 0; i < len; i++) { cursor = cursors[i].cursor; Dprintf("curs_doall: lock/iterating on %p\n", cursor); if (cursor->keeper->status == KEEPER_BEGIN && cursor->isolation_level > 0){ pthread_mutex_lock(&(cursor->keeper->lock)); if (cursor->keeper->status == KEEPER_BEGIN) { cursor->keeper->status = KEEPER_CONN_LOCK; Dprintf("curs_doall: acquired lock on keeper %p cursor %p\n", cursor->keeper, cursor); } else { pthread_mutex_unlock(&(cursor->keeper->lock)); } } } /* does all the operations */ for (i = 0; i < len; i++) { int status = 0; cursor = cursors[i].cursor; Dprintf("curs_doall: iterating on %p\n", cursor); if (cursor->keeper->status == KEEPER_CONN_LOCK) { Dprintf("curs_doall: operating on cursor %p\n", cursor); cursor->keeper->status = KEEPER_BEGIN; status = (*operation)(cursor); if (status == -1) { has_errors = 1; if (cursor->critical) { cursors[i].errmsg = strdup(cursor->critical); } } cursor->keeper->status = KEEPER_CONN_READY; } } /* unlocks all the connections */ for (i = 0; i < len; i++) { cursor = cursors[i].cursor; if (cursor->keeper->status == KEEPER_CONN_READY) { pthread_mutex_unlock(&(cursor->keeper->lock)); cursor->keeper->status = KEEPER_READY; Dprintf("curs_doall: released lock on keeper %p\n", cursor->keeper); } } pthread_mutex_unlock(&(self->lock)); Dprintf("curs_doall: lock released\n"); Py_END_ALLOW_THREADS; /* if an error occurred, set up the error dictionary (or set errs to * None if we can't create the dictionary). */ if (has_errors) { errs = PyDict_New(); if (errs) { for (i = 0; i < len; i++) { if (cursors[i].errmsg != NULL) { PyObject *str = PyString_FromString(cursors[i].errmsg); PyDict_SetItem(errs, (PyObject *)cursors[i].cursor, str); Py_XDECREF(str); } } } else { errs = Py_None; Py_INCREF(errs); } } /* clean up the state array */ for (i = 0; i < len; i++) { Py_DECREF(cursors[i].cursor); if (cursors[i].errmsg) free(cursors[i].errmsg); } free(cursors); /* errs will be NULL if has_errors is False */ return errs; }
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->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; }
static PyObject * AMC_save (AMC *self, PyObject *args) { char *filename; GIOChannel *file; int size, frames, i, j, k; PyObject *keys; // get filename if (!PyArg_ParseTuple (args, "s;expected 'string'", &filename)) return NULL; file = g_io_channel_new_file (filename, "w", NULL); if (file == NULL) { // FIXME - we should be using the GError, not errno return PyErr_SetFromErrnoWithFilename (PyExc_IOError, filename); } // write out comment size = PyList_Size (self->comments); for (i = 0; i < size; i++) { PyObject *line; char *cline; line = PyList_GetItem (self->comments, i); cline = PyString_AsString (line); g_io_channel_write_chars (file, cline, strlen (cline), NULL, NULL); g_io_channel_write_chars (file, "\n", 1, NULL, NULL); } // write out format size = PyList_Size (self->format); if (size == 0) g_io_channel_write_chars (file, ":FULLY-SPECIFIED\n:DEGREES\n", strlen (":FULLY-SPECIFIED\n:DEGREES\n"), NULL, NULL); for (i = 0; i < size; i++) { PyObject *line; char *cline; line = PyList_GetItem (self->format, i); cline = PyString_AsString (line); g_io_channel_write_chars (file, cline, strlen (cline), NULL, NULL); g_io_channel_write_chars (file, "\n", 1, NULL, NULL); } // get keys keys = PyDict_Keys (self->bones); size = PyList_Size (keys); if (size == 0) // FIXME - throw error return Py_False; // find # of frames { PyObject *bone = PyDict_GetItem (self->bones, PyList_GetItem (keys, 0)); PyArrayObject *array = (PyArrayObject *) bone; frames = array->dimensions[0]; } for (j = 0; j < frames; j++) { GIOStatus status; char *frame; // Write out the frame number frame = g_strdup_printf ("%d\n", j + 1); g_io_channel_write_chars (file, frame, strlen (frame), NULL, NULL); g_free (frame); for (i = 0; i < size; i++) { PyObject *key; char *bone_name; PyArrayObject *bone; // Write the bone name key = PyList_GetItem (keys, i); bone = (PyArrayObject *) PyDict_GetItem (self->bones, key); bone_name = PyString_AsString (key); g_io_channel_write_chars (file, bone_name, strlen (bone_name), NULL, NULL); // Write out the bone data for (k = 0; k < bone->dimensions[1]; k++) { char *data = g_strdup_printf (" %f", *((float*) (bone->data + (j * bone->strides[0]) + (k * bone->strides[1])))); g_io_channel_write_chars (file, data, strlen (data), NULL, NULL); g_free (data); } g_io_channel_write_chars (file, "\n", 1, NULL, NULL); } } g_io_channel_shutdown (file, TRUE, NULL); g_io_channel_unref (file); Py_RETURN_TRUE; }
/** ******************************************************************************************************* * This function will get a batch of records from the Aeropike DB. * * @param err as_error object * @param self AerospikeClient object * @param py_keys The list of keys * @param batch_policy_p as_policy_batch object * * Returns the record if key exists otherwise NULL. ******************************************************************************************************* */ static PyObject * batch_get_aerospike_batch_read(as_error *err, AerospikeClient * self, PyObject *py_keys, as_policy_batch * batch_policy_p) { PyObject * py_recs = NULL; as_batch_read_records records; // Initialisation flags bool batch_initialised = false; as_batch_read_record* record = NULL; // Convert python keys list to as_key ** and add it to as_batch.keys // keys can be specified in PyList or PyTuple if ( py_keys != NULL && PyList_Check(py_keys) ) { Py_ssize_t size = PyList_Size(py_keys); py_recs = PyList_New(size); if (size > MAX_STACK_ALLOCATION) { as_batch_read_init(&records, size); } else { as_batch_read_inita(&records, size); } // Batch object initialised batch_initialised = true; for ( int i = 0; i < size; i++ ) { PyObject * py_key = PyList_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ) { as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } record = as_batch_read_reserve(&records); pyobject_to_key(err, py_key, &record->key); record->read_all_bins = true; if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } } } else if ( py_keys != NULL && PyTuple_Check(py_keys) ) { Py_ssize_t size = PyTuple_Size(py_keys); py_recs = PyList_New(size); if (size > MAX_STACK_ALLOCATION) { as_batch_read_init(&records, size); } else { as_batch_read_inita(&records, size); } // Batch object initialised batch_initialised = true; for ( int i = 0; i < size; i++ ) { PyObject * py_key = PyTuple_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ) { as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } record = as_batch_read_reserve(&records); pyobject_to_key(err, py_key, &record->key); record->read_all_bins = true; if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } } } else { as_error_update(err, AEROSPIKE_ERR_PARAM, "Keys should be specified as a list or tuple."); goto CLEANUP; } // Invoke C-client API Py_BEGIN_ALLOW_THREADS aerospike_batch_read(self->as, err, batch_policy_p, &records); Py_END_ALLOW_THREADS if (err->code != AEROSPIKE_OK) { goto CLEANUP; } batch_get_recs(self, err, &records, &py_recs); CLEANUP: if (batch_initialised == true) { // We should destroy batch object as we are using 'as_batch_init' for initialisation // Also, pyobject_to_key is soing strdup() in case of Unicode. So, object destruction // is necessary. as_batch_read_destroy(&records); } return py_recs; }
static int CLazyLinker_init(CLazyLinker *self, PyObject *args, PyObject *kwds) { static char *kwlist[] = { (char*)"nodes", (char*)"thunks", (char*)"pre_call_clear", (char*)"allow_gc", (char*)"call_counts", (char*)"call_times", (char*)"compute_map_list", (char*)"storage_map_list", (char*)"base_input_output_list", (char*)"node_n_inputs", (char*)"node_n_outputs", (char*)"node_input_offset", (char*)"node_output_offset", (char*)"var_owner", (char*)"is_lazy_list", (char*)"output_vars", (char*)"node_prereqs", (char*)"node_output_size", (char*)"update_storage", (char*)"dependencies", NULL}; PyObject *compute_map_list=NULL, *storage_map_list=NULL, *base_input_output_list=NULL, *node_n_inputs=NULL, *node_n_outputs=NULL, *node_input_offset=NULL, *node_output_offset=NULL, *var_owner=NULL, *is_lazy=NULL, *output_vars=NULL, *node_prereqs=NULL, *node_output_size=NULL, *update_storage=NULL, *dependencies=NULL; assert(!self->nodes); if (! PyArg_ParseTupleAndKeywords(args, kwds, "OOOiOOOOOOOOOOOOOOOO", kwlist, &self->nodes, &self->thunks, &self->pre_call_clear, &self->allow_gc, &self->call_counts, &self->call_times, &compute_map_list, &storage_map_list, &base_input_output_list, &node_n_inputs, &node_n_outputs, &node_input_offset, &node_output_offset, &var_owner, &is_lazy, &output_vars, &node_prereqs, &node_output_size, &update_storage, &dependencies )) return -1; Py_INCREF(self->nodes); Py_INCREF(self->thunks); Py_INCREF(self->pre_call_clear); Py_INCREF(self->call_counts); Py_INCREF(self->call_times); Py_ssize_t n_applies = PyList_Size(self->nodes); self->n_applies = n_applies; self->n_vars = PyList_Size(var_owner); if (PyList_Size(self->thunks) != n_applies) return -1; if (PyList_Size(self->call_counts) != n_applies) return -1; if (PyList_Size(self->call_times) != n_applies) return -1; // allocated and initialize thunk_cptr_data and thunk_cptr_fn if (n_applies) { self->thunk_cptr_data = (void**)calloc(n_applies, sizeof(void*)); self->thunk_cptr_fn = (void**)calloc(n_applies, sizeof(void*)); self->is_lazy = (int*)calloc(n_applies, sizeof(int)); self->node_prereqs = (Py_ssize_t**)calloc(n_applies, sizeof(Py_ssize_t*)); self->node_n_prereqs = (Py_ssize_t*)calloc(n_applies, sizeof(Py_ssize_t)); assert(self->node_prereqs); assert(self->node_n_prereqs); assert(self->is_lazy); assert(self->thunk_cptr_fn); assert(self->thunk_cptr_data); for (int i = 0; i < n_applies; ++i) { PyObject * thunk = PyList_GetItem(self->thunks, i); //thunk is borrowed if (PyObject_HasAttrString(thunk, "cthunk")) { PyObject * cthunk = PyObject_GetAttrString(thunk, "cthunk"); //new reference assert (cthunk && PyCObject_Check(cthunk)); self->thunk_cptr_fn[i] = PyCObject_AsVoidPtr(cthunk); self->thunk_cptr_data[i] = PyCObject_GetDesc(cthunk); Py_DECREF(cthunk); // cthunk is kept alive by membership in self->thunks } PyObject * el_i = PyList_GetItem(is_lazy, i); self->is_lazy[i] = PyNumber_AsSsize_t(el_i, NULL); /* now get the prereqs */ el_i = PyList_GetItem(node_prereqs, i); assert (PyList_Check(el_i)); self->node_n_prereqs[i] = PyList_Size(el_i); if (self->node_n_prereqs[i]) { self->node_prereqs[i] = (Py_ssize_t*)malloc( PyList_Size(el_i)*sizeof(Py_ssize_t)); for (int j = 0; j < PyList_Size(el_i); ++j) { PyObject * el_ij = PyList_GetItem(el_i, j); Py_ssize_t N = PyNumber_AsSsize_t(el_ij, PyExc_IndexError); if (PyErr_Occurred()) return -1; // N < n. variables assert(N < PyList_Size(var_owner)); self->node_prereqs[i][j] = N; } } } } if (PyList_Check(base_input_output_list)) { Py_ssize_t n_inputs_outputs_base = PyList_Size(base_input_output_list); self->node_inputs_outputs_base = (Py_ssize_t*)calloc(n_inputs_outputs_base,sizeof(Py_ssize_t)); assert(self->node_inputs_outputs_base); for (int i = 0; i < n_inputs_outputs_base; ++i) { PyObject *el_i = PyList_GetItem(base_input_output_list, i); Py_ssize_t idx = PyNumber_AsSsize_t(el_i, PyExc_IndexError); if (PyErr_Occurred()) return -1; self->node_inputs_outputs_base[i] = idx; } self->node_n_inputs = (Py_ssize_t*)calloc(n_applies,sizeof(Py_ssize_t)); assert(self->node_n_inputs); self->node_n_outputs = (Py_ssize_t*)calloc(n_applies,sizeof(Py_ssize_t)); assert(self->node_n_outputs); self->node_inputs = (Py_ssize_t**)calloc(n_applies,sizeof(Py_ssize_t*)); assert(self->node_inputs); self->node_outputs = (Py_ssize_t**)calloc(n_applies,sizeof(Py_ssize_t*)); assert(self->node_outputs); for (int i = 0; i < n_applies; ++i) { Py_ssize_t N; N = PyNumber_AsSsize_t(PyList_GetItem(node_n_inputs, i),PyExc_IndexError); if (PyErr_Occurred()) return -1; assert (N <= n_inputs_outputs_base); self->node_n_inputs[i] = N; N = PyNumber_AsSsize_t(PyList_GetItem(node_n_outputs, i),PyExc_IndexError); if (PyErr_Occurred()) return -1; assert (N <= n_inputs_outputs_base); self->node_n_outputs[i] = N; N = PyNumber_AsSsize_t(PyList_GetItem(node_input_offset, i),PyExc_IndexError); if (PyErr_Occurred()) return -1; assert (N <= n_inputs_outputs_base); self->node_inputs[i] = &self->node_inputs_outputs_base[N]; N = PyNumber_AsSsize_t(PyList_GetItem(node_output_offset, i),PyExc_IndexError); if (PyErr_Occurred()) return -1; assert (N <= n_inputs_outputs_base); self->node_outputs[i] = &self->node_inputs_outputs_base[N]; } } else { PyErr_SetString(PyExc_TypeError, "base_input_output_list must be list"); return -1; } // allocation for var_owner if (PyList_Check(var_owner)) { self->var_owner = (Py_ssize_t*)calloc(self->n_vars,sizeof(Py_ssize_t)); self->var_has_owner = (int*)calloc(self->n_vars,sizeof(int)); self->var_computed = (int*)calloc(self->n_vars,sizeof(int)); self->var_computed_cells = (PyObject**)calloc(self->n_vars,sizeof(PyObject*)); self->var_value_cells = (PyObject**)calloc(self->n_vars,sizeof(PyObject*)); for (int i = 0; i < self->n_vars; ++i) { PyObject * el_i = PyList_GetItem(var_owner, i); if (el_i == Py_None) { self->var_has_owner[i] = 0; } else { Py_ssize_t N = PyNumber_AsSsize_t(el_i, PyExc_IndexError); if (PyErr_Occurred()) return -1; assert (N <= n_applies); self->var_owner[i] = N; self->var_has_owner[i] = 1; } self->var_computed_cells[i] = PyList_GetItem(compute_map_list, i); Py_INCREF(self->var_computed_cells[i]); self->var_value_cells[i] = PyList_GetItem(storage_map_list, i); Py_INCREF(self->var_value_cells[i]); } } else { PyErr_SetString(PyExc_TypeError, "var_owner must be list"); return -1; } if (dependencies != Py_None) { self->dependencies = (Py_ssize_t**)calloc(self->n_vars, sizeof(Py_ssize_t *)); self->n_dependencies = (Py_ssize_t*)calloc(self->n_vars, sizeof(Py_ssize_t)); assert(self->dependencies); assert(self->n_dependencies); for (int i = 0; i < self->n_vars; ++i) { PyObject *tmp = PyList_GetItem(dependencies, i); // refcounting - tmp is borrowed if (unpack_list_of_ssize_t(tmp, &self->dependencies[i], &self->n_dependencies[i], "dependencies")) return -1; } } if (unpack_list_of_ssize_t(output_vars, &self->output_vars, &self->n_output_vars, "output_vars")) return -1; for (int i = 0; i < self->n_output_vars; ++i) { assert(self->output_vars[i] < self->n_vars); } if (unpack_list_of_ssize_t(update_storage, &self->update_storage, &self->n_updates, "updates_storage")) return -1; return 0; }
static int lazy_rec_eval(CLazyLinker * self, Py_ssize_t var_idx, PyObject*one, PyObject*zero) { PyObject *rval = NULL; int verbose = 0; int err = 0; if (verbose) fprintf(stderr, "lazy_rec computing %i\n", (int)var_idx); if (self->var_computed[var_idx] || !self->var_has_owner[var_idx]) return 0; Py_ssize_t owner_idx = self->var_owner[var_idx]; // STEP 1: compute the pre-requirements of the node // Includes input nodes for non-lazy ops. for (int i = 0; i < self->node_n_prereqs[owner_idx]; ++i) { Py_ssize_t prereq_idx = self->node_prereqs[owner_idx][i]; if (!self->var_computed[prereq_idx]) { err = lazy_rec_eval(self, prereq_idx, one, zero); if (err) return err; } assert (self->var_computed[prereq_idx]); } // STEP 2: compute the node itself if (self->is_lazy[owner_idx]) { // update the compute_map cells corresponding to the inputs of this thunk for (int i = 0; i < self->node_n_inputs[owner_idx]; ++i) { int in_idx = self->node_inputs[owner_idx][i]; if (self->var_computed[in_idx]) { Py_INCREF(one); err = PyList_SetItem(self->var_computed_cells[in_idx], 0, one); } else { Py_INCREF(zero); err = PyList_SetItem(self->var_computed_cells[in_idx], 0, zero); } if (err) goto fail; } rval = pycall(self, owner_idx, verbose); // refcounting - rval is new ref //TODO: to prevent infinite loops // - consider check that a thunk does not ask for an input that is already computed if (rval == NULL) { assert (PyErr_Occurred()); err = 1; goto fail; } //update the computed-ness of any output cells for (int i = 0; i < self->node_n_outputs[owner_idx]; ++i) { int out_idx = self->node_outputs[owner_idx][i]; PyObject * el_i = PyList_GetItem(self->var_computed_cells[out_idx], 0); Py_ssize_t N = PyNumber_AsSsize_t(el_i, PyExc_IndexError); if (PyErr_Occurred()) { err = -1; goto pyfail; } assert (N==0 || N==1); self->var_computed[out_idx] = N; } if (!self->var_computed[var_idx]) { /* * If self is not computed after the call, this means that some * inputs are needed. Compute the ones on the returned list * and try to compute the current node again (with recursive call). * This allows a node to request more nodes more than once before * finally yielding a result. */ if (!PyList_Check(rval)) { //TODO: More helpful error to help find *which node* made this // bad thunk PyErr_SetString(PyExc_TypeError, "lazy thunk should return a list"); err = 1; goto pyfail; } if (!PyList_Size(rval)) { PyErr_SetString(PyExc_ValueError, "lazy thunk returned empty list without computing output"); err = 1; goto pyfail; } for (int i = 0; i < PyList_Size(rval); ++i) { PyObject * el_i = PyList_GetItem(rval, i); Py_ssize_t N = PyNumber_AsSsize_t(el_i, PyExc_IndexError); if (PyErr_Occurred()) { err = 1; goto pyfail; } assert (N <= self->node_n_inputs[owner_idx]); Py_ssize_t input_idx = self->node_inputs[owner_idx][N]; err = lazy_rec_eval(self, input_idx, one, zero); if (err) goto pyfail; } Py_DECREF(rval); /* * We intentionally skip all the end-of-function processing * (mark outputs, GC) as it will be performed by the call * that actually manages to compute the result. */ return lazy_rec_eval(self, var_idx, one, zero); } Py_DECREF(rval); } else //owner is not a lazy op. Ensure all intputs are evaluated. { // loop over inputs to owner // call lazy_rec_eval on each one that is not computed. // if there's an error, pass it up the stack for (int i = 0; i < self->node_n_inputs[owner_idx]; ++i) { Py_ssize_t input_idx = self->node_inputs[owner_idx][i]; if (!self->var_computed[input_idx]) { err = lazy_rec_eval(self, input_idx, one, zero); if (err) return err; } assert (self->var_computed[input_idx]); } // call the thunk for this owner. if (self->thunk_cptr_fn[owner_idx]) { err = c_call(self, owner_idx, verbose); if (err) goto fail; } else { rval = pycall(self, owner_idx, verbose); //rval is new ref if (rval) //pycall returned normally (no exception) { if (rval == Py_None) { Py_DECREF(rval); //ignore a return of None } else if (PyList_Check(rval)) { PyErr_SetString(PyExc_TypeError, "non-lazy thunk should return None, not list"); err = 1; goto pyfail; } else // don't know what it returned, but it wasn't right. { PyErr_SetObject(PyExc_TypeError, rval); err = 1; // We don't release rval since we put it in the error above goto fail; } } else // pycall returned NULL (internal error) { err = 1; goto fail; } } } // loop over all outputs and mark them as computed for (int i = 0; i < self->node_n_outputs[owner_idx]; ++i) { self->var_computed[self->node_outputs[owner_idx][i]] = 1; } // Free vars that are not needed anymore if (self->allow_gc) { for (int i = 0; i < self->node_n_inputs[owner_idx]; ++i) { int cleanup = 1; Py_ssize_t i_idx = self->node_inputs[owner_idx][i]; if (!self->var_has_owner[i_idx]) continue; for (int j = 0; j < self->n_output_vars; ++j) { if (i_idx == self->output_vars[j]) { cleanup = 0; break; } } if (!cleanup) continue; for (int j = 0; j < self->n_dependencies[i_idx]; ++j) { if (!self->var_computed[self->dependencies[i_idx][j]]) { cleanup = 0; break; } } if (!cleanup) continue; Py_INCREF(Py_None); err = PyList_SetItem(self->var_value_cells[i_idx], 0, Py_None); //See the Stack gc implementation for why we change it to 2 and not 0. self->var_computed[i_idx] = 2; if (err) goto fail; } } return 0; pyfail: Py_DECREF(rval); fail: set_position_of_error(self, owner_idx); return err; }
/** \ingroup python_interface_edgeseq * \brief Returns the list of values for a given attribute */ PyObject* igraphmodule_EdgeSeq_get_attribute_values(igraphmodule_EdgeSeqObject* self, PyObject* o) { igraphmodule_GraphObject *gr = self->gref; PyObject *result=0, *values, *item; long int i, n; if (!igraphmodule_attribute_name_check(o)) return 0; PyErr_Clear(); values=PyDict_GetItem(ATTR_STRUCT_DICT(&gr->g)[ATTRHASH_IDX_EDGE], o); if (!values) { PyErr_SetString(PyExc_KeyError, "Attribute does not exist"); return NULL; } else if (PyErr_Occurred()) return NULL; switch (igraph_es_type(&self->es)) { case IGRAPH_ES_NONE: n = 0; result = PyList_New(0); break; case IGRAPH_ES_ALL: n = PyList_Size(values); result = PyList_New(n); if (!result) return 0; for (i=0; i<n; i++) { item = PyList_GET_ITEM(values, i); Py_INCREF(item); PyList_SET_ITEM(result, i, item); } break; case IGRAPH_ES_VECTOR: case IGRAPH_ES_VECTORPTR: n = igraph_vector_size(self->es.data.vecptr); result = PyList_New(n); if (!result) return 0; for (i=0; i<n; i++) { item = PyList_GET_ITEM(values, (long)VECTOR(*self->es.data.vecptr)[i]); Py_INCREF(item); PyList_SET_ITEM(result, i, item); } break; case IGRAPH_ES_SEQ: n = self->es.data.seq.to - self->es.data.seq.from; result = PyList_New(n); if (!result) return 0; for (i=0; i<n; i++) { item = PyList_GET_ITEM(values, (long)self->es.data.seq.from+i); Py_INCREF(item); PyList_SET_ITEM(result, i, item); } break; default: PyErr_SetString(PyExc_RuntimeError, "invalid edge selector"); } return result; }
/* Returns 0 on error (no new refs), 1 on success */ static int setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, PyObject **module, PyObject **registry) { PyObject *globals; /* Setup globals and lineno. */ PyFrameObject *f = PyThreadState_GET()->frame; while (--stack_level > 0 && f != NULL) f = f->f_back; if (f == NULL) { globals = PyThreadState_Get()->interp->sysdict; *lineno = 1; } else { globals = f->f_globals; *lineno = PyFrame_GetLineNumber(f); } *module = NULL; /* Setup registry. */ assert(globals != NULL); assert(PyDict_Check(globals)); *registry = PyDict_GetItemString(globals, "__warningregistry__"); if (*registry == NULL) { int rc; *registry = PyDict_New(); if (*registry == NULL) return 0; rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); if (rc < 0) goto handle_error; } else Py_INCREF(*registry); /* Setup module. */ *module = PyDict_GetItemString(globals, "__name__"); if (*module == NULL) { *module = PyString_FromString("<string>"); if (*module == NULL) goto handle_error; } else Py_INCREF(*module); /* Setup filename. */ *filename = PyDict_GetItemString(globals, "__file__"); if (*filename != NULL) { Py_ssize_t len = PyString_Size(*filename); const char *file_str = PyString_AsString(*filename); if (file_str == NULL || (len < 0 && PyErr_Occurred())) goto handle_error; /* if filename.lower().endswith((".pyc", ".pyo")): */ if (len >= 4 && file_str[len-4] == '.' && tolower(file_str[len-3]) == 'p' && tolower(file_str[len-2]) == 'y' && (tolower(file_str[len-1]) == 'c' || tolower(file_str[len-1]) == 'o')) { *filename = PyString_FromStringAndSize(file_str, len-1); if (*filename == NULL) goto handle_error; } else Py_INCREF(*filename); } else { const char *module_str = PyString_AsString(*module); if (module_str && strcmp(module_str, "__main__") == 0) { PyObject *argv = PySys_GetObject("argv"); if (argv != NULL && PyList_Size(argv) > 0) { int is_true; *filename = PyList_GetItem(argv, 0); Py_INCREF(*filename); /* If sys.argv[0] is false, then use '__main__'. */ is_true = PyObject_IsTrue(*filename); if (is_true < 0) { Py_DECREF(*filename); goto handle_error; } else if (!is_true) { Py_DECREF(*filename); *filename = PyString_FromString("__main__"); if (*filename == NULL) goto handle_error; } } else { /* embedded interpreters don't have sys.argv, see bug #839151 */ *filename = PyString_FromString("__main__"); if (*filename == NULL) goto handle_error; } } if (*filename == NULL) { *filename = *module; Py_INCREF(*filename); } } return 1; handle_error: /* filename not XDECREF'ed here as there is no way to jump here with a dangling reference. */ Py_XDECREF(*registry); Py_XDECREF(*module); return 0; }
static void PyTesseract_invalidate_iterators(PyTesseract *self) { for (Py_ssize_t index = 0; index < PyList_Size(self->iterators); ++index) { PyResultIterator *iterator = (PyResultIterator *)PyList_GetItem(self->iterators, index); iterator->valid = false; } }
static int pyjobject_init(JNIEnv *env, PyJObject *pyjob) { jobjectArray methodArray = NULL; jobjectArray fieldArray = NULL; int i, len = 0; jobject langClass = NULL; jstring className = NULL; const char *cClassName = NULL; PyObject *pyClassName = NULL; PyObject *pyAttrName = NULL; JepThread *jepThread; PyObject *cachedMethodList = NULL; (*env)->PushLocalFrame(env, 20); // ------------------------------ call Class.getMethods() // well, first call getClass() if (objectGetClass == 0) { objectGetClass = (*env)->GetMethodID(env, pyjob->clazz, "getClass", "()Ljava/lang/Class;"); if (process_java_exception(env) || !objectGetClass) { goto EXIT_ERROR; } } langClass = (*env)->CallObjectMethod(env, pyjob->clazz, objectGetClass); if (process_java_exception(env) || !langClass) { goto EXIT_ERROR; } /* * attach attribute java_name to the pyjobject instance to assist with * understanding the type at runtime */ if (classGetName == 0) { classGetName = (*env)->GetMethodID(env, langClass, "getName", "()Ljava/lang/String;"); } className = (*env)->CallObjectMethod(env, pyjob->clazz, classGetName); cClassName = jstring2char(env, className); pyClassName = PyString_FromString(cClassName); release_utf_char(env, className, cClassName); pyAttrName = PyString_FromString("java_name"); if (PyObject_SetAttr((PyObject *) pyjob, pyAttrName, pyClassName) == -1) { PyErr_Format(PyExc_RuntimeError, "Couldn't add java_name as attribute."); } else { pyjobject_addfield(pyjob, pyAttrName); } pyjob->javaClassName = pyClassName; Py_DECREF(pyAttrName); (*env)->DeleteLocalRef(env, className); // then, get methodid for getMethods() if (classGetMethods == 0) { classGetMethods = (*env)->GetMethodID(env, langClass, "getMethods", "()[Ljava/lang/reflect/Method;"); if (process_java_exception(env) || !classGetMethods) { goto EXIT_ERROR; } } /* * Performance improvement. The code below is very similar to previous * versions except methods are now cached in memory. * * Previously every time you instantiate a pyjobject, JEP would get the * complete list of methods, turn them into pyjmethods, and add them as * attributes to the pyjobject. * * Now JEP retains a python dictionary in memory with a key of the fully * qualified Java classname to a list of pyjmethods. Since the * Java methods will never change at runtime for a particular Class, this * is safe and drastically speeds up pyjobject instantiation by reducing * reflection calls. We continue to set and reuse the pyjmethods as * attributes on the pyjobject instance, but if pyjobject_getattr sees a * pyjmethod, it will put it inside a pymethod and return that, enabling * the reuse of the pyjmethod for this particular object instance. * * We have the GIL at this point, so we can safely assume we're * synchronized and multiple threads will not alter the dictionary at the * same time. */ jepThread = pyembed_get_jepthread(); if (jepThread == NULL) { goto EXIT_ERROR; } if (jepThread->fqnToPyJmethods == NULL) { PyObject *methodCache = PyDict_New(); jepThread->fqnToPyJmethods = methodCache; } cachedMethodList = PyDict_GetItem(jepThread->fqnToPyJmethods, pyClassName); if (cachedMethodList == NULL) { PyObject *pyjMethodList = NULL; pyjMethodList = PyList_New(0); // - GetMethodID fails when you pass the clazz object, it expects // a java.lang.Class jobject. // - if you CallObjectMethod with the langClass jclass object, // it'll return an array of methods, but they're methods of the // java.lang.reflect.Method class -- not ->object. // // so what i did here was find the methodid using langClass, // but then i call the method using clazz. methodIds for java // classes are shared.... methodArray = (jobjectArray) (*env)->CallObjectMethod(env, pyjob->clazz, classGetMethods); if (process_java_exception(env) || !methodArray) { goto EXIT_ERROR; } // for each method, create a new pyjmethod object // and add to the internal methods list. len = (*env)->GetArrayLength(env, methodArray); for (i = 0; i < len; i++) { PyJMethodObject *pymethod = NULL; jobject rmethod = NULL; rmethod = (*env)->GetObjectArrayElement(env, methodArray, i); // make new PyJMethodObject, linked to pyjob if (pyjob->object) { pymethod = pyjmethod_new(env, rmethod, pyjob); } else { pymethod = pyjmethod_new_static(env, rmethod, pyjob); } if (!pymethod) { continue; } if (pymethod->pyMethodName && PyString_Check(pymethod->pyMethodName)) { int multi = 0; Py_ssize_t cacheLen = PyList_Size(pyjMethodList); int cacheIndex = 0; for (cacheIndex = 0; cacheIndex < cacheLen; cacheIndex += 1) { PyObject* cached = PyList_GetItem(pyjMethodList, cacheIndex); if (pyjmethod_check(cached)) { PyJMethodObject* cachedMethod = (PyJMethodObject*) cached; if (PyObject_RichCompareBool(pymethod->pyMethodName, cachedMethod->pyMethodName, Py_EQ)) { PyObject* multimethod = PyJmultiMethod_New((PyObject*) pymethod, cached); PyList_SetItem(pyjMethodList, cacheIndex, multimethod); multi = 1; break; } } else if (PyJmultiMethod_Check(cached)) { PyObject* methodName = PyJmultiMethod_GetName(cached); if (PyObject_RichCompareBool(pymethod->pyMethodName, methodName, Py_EQ)) { Py_DECREF(methodName); PyJmultiMethod_Append(cached, (PyObject*) pymethod); multi = 1; break; } else { Py_DECREF(methodName); } } } if (!multi) { if (PyList_Append(pyjMethodList, (PyObject*) pymethod) != 0) { printf("WARNING: couldn't add method"); } } } Py_DECREF(pymethod); (*env)->DeleteLocalRef(env, rmethod); } // end of looping over available methods PyDict_SetItem(jepThread->fqnToPyJmethods, pyClassName, pyjMethodList); cachedMethodList = pyjMethodList; Py_DECREF(pyjMethodList); // fqnToPyJmethods will hold the reference (*env)->DeleteLocalRef(env, methodArray); } // end of setting up cache for this Java Class len = (int) PyList_Size(cachedMethodList); for (i = 0; i < len; i++) { PyObject* name = NULL; PyObject* cached = PyList_GetItem(cachedMethodList, i); if (pyjmethod_check(cached)) { PyJMethodObject* cachedMethod = (PyJMethodObject*) cached; name = cachedMethod->pyMethodName; Py_INCREF(name); } else if (PyJmultiMethod_Check(cached)) { name = PyJmultiMethod_GetName(cached); } if (name) { if (PyObject_SetAttr((PyObject *) pyjob, name, cached) != 0) { PyErr_SetString(PyExc_RuntimeError, "Couldn't add method as attribute."); } else { pyjobject_addmethod(pyjob, name); } Py_DECREF(name); } } // end of cached method optimizations // ------------------------------ process fields if (classGetFields == 0) { classGetFields = (*env)->GetMethodID(env, langClass, "getFields", "()[Ljava/lang/reflect/Field;"); if (process_java_exception(env) || !classGetFields) { goto EXIT_ERROR; } } fieldArray = (jobjectArray) (*env)->CallObjectMethod(env, pyjob->clazz, classGetFields); if (process_java_exception(env) || !fieldArray) { goto EXIT_ERROR; } // for each field, create a pyjfield object and // add to the internal members list. len = (*env)->GetArrayLength(env, fieldArray); for (i = 0; i < len; i++) { jobject rfield = NULL; PyJFieldObject *pyjfield = NULL; rfield = (*env)->GetObjectArrayElement(env, fieldArray, i); pyjfield = pyjfield_new(env, rfield, pyjob); if (!pyjfield) { continue; } if (pyjfield->pyFieldName && PyString_Check(pyjfield->pyFieldName)) { if (PyObject_SetAttr((PyObject *) pyjob, pyjfield->pyFieldName, (PyObject *) pyjfield) != 0) { printf("WARNING: couldn't add field.\n"); } else { pyjobject_addfield(pyjob, pyjfield->pyFieldName); } } Py_DECREF(pyjfield); (*env)->DeleteLocalRef(env, rfield); } (*env)->DeleteLocalRef(env, fieldArray); // we've finished the object. pyjob->finishAttr = 1; (*env)->PopLocalFrame(env, NULL); return 1; EXIT_ERROR: (*env)->PopLocalFrame(env, NULL); if (PyErr_Occurred()) { // java exceptions translated by this time if (pyjob) { pyjobject_dealloc(pyjob); } } return 0; }
static PyObject* features_calculateFeatures(PyObject *self, PyObject *args) { const char *data; ULL prim, qmod, win, fmod, val, ppow, fval, mul, add; int i, j, min; Py_ssize_t amount; ULL *best, *features; PyObject *pis, *item, *result; Py_ssize_t data_len; if (!PyArg_ParseTuple(args, "s#KKKKO", &data, &data_len, &prim, &qmod, &win, &fmod, &pis)) return NULL; amount = PyList_Size(pis); if (!(best = malloc(2 * amount * sizeof(ULL)))) return NULL; features = best + amount; ppow = 1; for (i = 1; i < win; i++) ppow = (ppow * prim) % qmod; val = 0; min = (win < data_len) ? win : data_len; for (i = 0; i < min; i++) val = (val * prim + data[i]) % qmod; if (win <= data_len) { for (j = 0; j < amount; j++) { item = PyList_GET_ITEM(pis, j); if (!PyArg_ParseTuple(item, "KK", &mul, &add)) goto free_best; best[j] = (val * mul + add) % fmod; features[j] = val; } } for (i = win; i < data_len; i++) { val = (val + (qmod - data[i - win]) * ppow) % qmod; val = (val * prim + data[i]) % qmod; for (j = 0; j < amount; j++) { item = PyList_GET_ITEM(pis, j); if (!PyArg_ParseTuple(item, "KK", &mul, &add)) goto free_best; fval = (val * mul + add) % fmod; if (fval > best[j]) { best[j] = fval; features[j] = val; } } } if (!(result = PyList_New(amount))) goto free_best; for (i = 0; i < amount; i++) { if (!(item = Py_BuildValue("K", features[i]))) goto free_result; PyList_SET_ITEM(result, i, item); } free(best); return result; free_result: Py_DECREF(result); free_best: free(best); return NULL; }
void ChPythonEngine::ImportSolidWorksSystem(const char* solidworks_py_file, ChSystem& msystem) throw(ChException) { std::ostringstream sstream; //sstream << "from " << std::string(solidworks_py_file) << " import exported_items\n"; sstream << "import builtins \n"; sstream << "import imp \n"; sstream << "import os \n"; sstream << "mdirname, mmodulename= os.path.split('" << std::string(solidworks_py_file) << "') \n"; sstream << "builtins.exported_system_relpath = mdirname + '/' \n"; sstream << "fp, pathname, description = imp.find_module(mmodulename,[builtins.exported_system_relpath]) \n"; sstream << "try: \n"; sstream << " imported_mod = imp.load_module('imported_mod', fp, pathname, description) \n"; sstream << "finally: \n"; sstream << " if fp: \n"; sstream << " fp.close() \n"; sstream << "exported_items = imported_mod.exported_items \n"; this->Run(sstream.str().c_str()); PyObject * module = PyImport_AddModule("__main__"); // borrowed reference if (!module) throw ChException("ERROR. No Python __main__ module?"); PyObject * dictionary = PyModule_GetDict(module); // borrowed reference if (!dictionary) throw ChException("ERROR. No Python dictionary?"); PyObject * result = PyDict_GetItemString(dictionary, "exported_items"); // borrowed reference if (!result) throw ChException("ERROR. Missing Python object 'exported_items' in SolidWorks file"); if (PyList_Check(result)) { int nitems = PyList_Size(result); //GetLog() << "N.of list items: " << nitems << "\n"; for (int i = 0; i< nitems; i++) { PyObject* mobj = PyList_GetItem(result,i); if (mobj) { // GetLog() << " Python type: " << mobj->ob_type->tp_name << "\n"; SwigPyObject * mswigobj = SWIG_Python_GetSwigThis(mobj); if (mswigobj) { void* objptr = mswigobj->ptr; ChSharedPtr<ChPhysicsItem>* pt_to_shp = (ChSharedPtr<ChPhysicsItem>*)objptr; /// Add the ChPhysicsItem to the ChSystem msystem.Add( (*pt_to_shp) ); } else { throw ChException("ERROR. Only shared pointers to ChPhysicsItem subclasses can be inside exported_items."); } } } msystem.Setup(); msystem.Update(); } else { throw ChException("ERROR. exported_items python object is not a list."); } }
PyObject * CLazyLinker_call(PyObject *_self, PyObject *args, PyObject *kwds) { CLazyLinker * self = (CLazyLinker*)_self; static char *kwlist[] = { (char*)"time_thunks", (char *)"n_calls", NULL}; int n_calls=1; if (! PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwlist, &self->do_timing, &n_calls)) return NULL; int err = 0; self->position_of_error = -1; // create constants used to fill the var_compute_cells PyObject * one = PyInt_FromLong(1); PyObject * zero = PyInt_FromLong(0); // pre-allocate our return value Py_INCREF(Py_None); PyObject * rval = Py_None; //clear storage of pre_call_clear elements for (int call_i = 0; call_i < n_calls && (!err); ++call_i) { Py_ssize_t n_pre_call_clear = PyList_Size(self->pre_call_clear); assert(PyList_Check(self->pre_call_clear)); for (int i = 0; i < n_pre_call_clear; ++i) { PyObject * el_i = PyList_GetItem(self->pre_call_clear, i); Py_INCREF(Py_None); PyList_SetItem(el_i, 0, Py_None); } //clear the computed flag out of all non-input vars for (int i = 0; i < self->n_vars; ++i) { self->var_computed[i] = !self->var_has_owner[i]; if (self->var_computed[i]) { Py_INCREF(one); PyList_SetItem(self->var_computed_cells[i], 0, one); } else { Py_INCREF(zero); PyList_SetItem(self->var_computed_cells[i], 0, zero); } } for (int i = 0; i < self->n_output_vars && (!err); ++i) { err = lazy_rec_eval(self, self->output_vars[i], one, zero); } if (!err) { // save references to outputs prior to updating storage containers assert (self->n_output_vars >= self->n_updates); Py_DECREF(rval); rval = PyList_New(self->n_output_vars); for (int i = 0; i < (self->n_output_vars); ++i) { Py_ssize_t src = self->output_vars[i]; PyObject * item = PyList_GetItem(self->var_value_cells[src], 0); if (self->var_computed[src] != 1) { err = 1; PyErr_Format(PyExc_AssertionError, "The compute map of output %d should contain " "1 at the end of execution, not %d.", i, self->var_computed[src]); break; } Py_INCREF(item); PyList_SetItem(rval, i, item); } } if (!err) { // Update the inputs that have an update rule for (int i = 0; i < self->n_updates; ++i) { PyObject* tmp = PyList_GetItem(rval, self->n_output_vars - self->n_updates + i); Py_INCREF(tmp); Py_ssize_t dst = self->update_storage[i]; PyList_SetItem(self->var_value_cells[dst], 0, tmp); } } } /* Clear everything that is left and not an output. This is needed for lazy evaluation since the current GC algo is too conservative with lazy graphs. */ if (self->allow_gc && !err) { for (Py_ssize_t i = 0; i < self->n_vars; ++i) { int do_cleanup = 1; if (!self->var_has_owner[i] || !self->var_computed[i]) continue; for (int j = 0; j < self->n_output_vars; ++j) { if (i == self->output_vars[j]) { do_cleanup = 0; break; } } if (!do_cleanup) continue; Py_INCREF(Py_None); PyList_SetItem(self->var_value_cells[i], 0, Py_None); } } Py_DECREF(one); Py_DECREF(zero); if (err) { Py_DECREF(rval); return NULL; } return rval; }
static PyObject* py_set_colors(PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *newdict, *newtuple, *keys, *vals; const char *kwnames[] = {"colors", NULL}; PyObject *retval = NULL; char *keyname; float rgb[3]; VMDApp *app; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!:color.set_colors", (char**) kwnames, &PyDict_Type, &newdict)) return NULL; if (!(app = get_vmdapp())) return NULL; keys = PyDict_Keys(newdict); vals = PyDict_Values(newdict); for (int i=0; i<PyList_Size(keys); i++) { // Get color name from input dictionary keyname = as_charptr(PyList_GetItem(keys, i)); if (PyErr_Occurred()) goto cleanup; // Check this color name actually exists if (app->color_index(keyname) < 0) { PyErr_Format(PyExc_ValueError, "Unknown color '%s'", keyname); goto cleanup; } // Unpack value tuples into 3 floats newtuple = PyList_GetItem(vals, i); if (!PyTuple_Check(newtuple) || PyTuple_Size(newtuple) != 3) { PyErr_SetString(PyExc_ValueError, "color definition must be 3-tuple of floats"); goto cleanup; } for (int j=0; j<3; j++) { rgb[j] = (float)PyFloat_AsDouble(PyTuple_GET_ITEM(newtuple, j)); if (PyErr_Occurred()) { PyErr_SetString(PyExc_ValueError, "color definition must be floats"); goto cleanup; } } // Finally actually change the color app->color_change_rgb(keyname, rgb[0], rgb[1], rgb[2]); } retval = Py_None; // Getting the keys and values from the dictionary makes a new reference. // We tell Python we're done with it so as to not leak memory. // This needs to happen even if there was a problem setting color, which is // why we don't return NULL from the error checking statements above. cleanup: Py_DECREF(keys); Py_DECREF(vals); Py_XINCREF(retval); return retval; }
void XBPyThread::Process() { CLog::Log(LOGDEBUG,"Python thread: start processing"); int m_Py_file_input = Py_file_input; // get the global lock PyEval_AcquireLock(); PyThreadState* state = Py_NewInterpreter(); if (!state) { PyEval_ReleaseLock(); CLog::Log(LOGERROR,"Python thread: FAILED to get thread state!"); return; } // swap in my thread state PyThreadState_Swap(state); XBMCAddon::AddonClass::Ref<XBMCAddon::Python::LanguageHook> languageHook(new XBMCAddon::Python::LanguageHook(state->interp)); languageHook->RegisterMe(); m_pExecuter->InitializeInterpreter(addon); CLog::Log(LOGDEBUG, "%s - The source file to load is %s", __FUNCTION__, m_source); // get path from script file name and add python path's // this is used for python so it will search modules from script path first CStdString scriptDir; URIUtils::GetDirectory(CSpecialProtocol::TranslatePath(m_source), scriptDir); URIUtils::RemoveSlashAtEnd(scriptDir); CStdString path = scriptDir; // add on any addon modules the user has installed ADDON::VECADDONS addons; ADDON::CAddonMgr::Get().GetAddons(ADDON::ADDON_SCRIPT_MODULE, addons); for (unsigned int i = 0; i < addons.size(); ++i) #ifdef TARGET_WINDOWS { CStdString strTmp(CSpecialProtocol::TranslatePath(addons[i]->LibPath())); g_charsetConverter.utf8ToSystem(strTmp); path += PY_PATH_SEP + strTmp; } #else path += PY_PATH_SEP + CSpecialProtocol::TranslatePath(addons[i]->LibPath()); #endif // and add on whatever our default path is path += PY_PATH_SEP; // we want to use sys.path so it includes site-packages // if this fails, default to using Py_GetPath PyObject *sysMod(PyImport_ImportModule((char*)"sys")); // must call Py_DECREF when finished PyObject *sysModDict(PyModule_GetDict(sysMod)); // borrowed ref, no need to delete PyObject *pathObj(PyDict_GetItemString(sysModDict, "path")); // borrowed ref, no need to delete if( pathObj && PyList_Check(pathObj) ) { for( int i = 0; i < PyList_Size(pathObj); i++ ) { PyObject *e = PyList_GetItem(pathObj, i); // borrowed ref, no need to delete if( e && PyString_Check(e) ) { path += PyString_AsString(e); // returns internal data, don't delete or modify path += PY_PATH_SEP; } } } else { path += Py_GetPath(); } Py_DECREF(sysMod); // release ref to sysMod // set current directory and python's path. if (m_argv != NULL) PySys_SetArgv(m_argc, m_argv); CLog::Log(LOGDEBUG, "%s - Setting the Python path to %s", __FUNCTION__, path.c_str()); PySys_SetPath((char *)path.c_str()); CLog::Log(LOGDEBUG, "%s - Entering source directory %s", __FUNCTION__, scriptDir.c_str()); PyObject* module = PyImport_AddModule((char*)"__main__"); PyObject* moduleDict = PyModule_GetDict(module); // when we are done initing we store thread state so we can be aborted PyThreadState_Swap(NULL); PyEval_ReleaseLock(); // we need to check if we was asked to abort before we had inited bool stopping = false; { CSingleLock lock(m_pExecuter->m_critSection); m_threadState = state; stopping = m_stopping; } PyEval_AcquireLock(); PyThreadState_Swap(state); if (!stopping) { try { if (m_type == 'F') { // run script from file // We need to have python open the file because on Windows the DLL that python // is linked against may not be the DLL that xbmc is linked against so // passing a FILE* to python from an fopen has the potential to crash. PyObject* file = PyFile_FromString((char *) CSpecialProtocol::TranslatePath(m_source).c_str(), (char*)"r"); FILE *fp = PyFile_AsFile(file); if (fp) { PyObject *f = PyString_FromString(CSpecialProtocol::TranslatePath(m_source).c_str()); PyDict_SetItemString(moduleDict, "__file__", f); if (addon.get() != NULL) { PyObject *pyaddonid = PyString_FromString(addon->ID().c_str()); PyDict_SetItemString(moduleDict, "__xbmcaddonid__", pyaddonid); CStdString version = ADDON::GetXbmcApiVersionDependency(addon); PyObject *pyxbmcapiversion = PyString_FromString(version.c_str()); PyDict_SetItemString(moduleDict, "__xbmcapiversion__", pyxbmcapiversion); CLog::Log(LOGDEBUG,"Instantiating addon using automatically obtained id of \"%s\" dependent on version %s of the xbmc.python api",addon->ID().c_str(),version.c_str()); } Py_DECREF(f); XBMCAddon::Python::PyContext pycontext; // this is a guard class that marks this callstack as being in a python context PyRun_FileExFlags(fp, CSpecialProtocol::TranslatePath(m_source).c_str(), m_Py_file_input, moduleDict, moduleDict,1,NULL); } else CLog::Log(LOGERROR, "%s not found!", m_source); } else { //run script PyRun_String(m_source, m_Py_file_input, moduleDict, moduleDict); } } catch (const XbmcCommons::Exception& e) { e.LogThrowMessage(); } catch (...) { CLog::Log(LOGERROR, "failure in %s", m_source); } } if (!PyErr_Occurred()) CLog::Log(LOGINFO, "Scriptresult: Success"); else if (PyErr_ExceptionMatches(PyExc_SystemExit)) CLog::Log(LOGINFO, "Scriptresult: Aborted"); else { PythonBindings::PythonToCppException e; e.LogThrowMessage(); { CPyThreadState releaseGil; CSingleLock gc(g_graphicsContext); CGUIDialogKaiToast *pDlgToast = (CGUIDialogKaiToast*)g_windowManager.GetWindow(WINDOW_DIALOG_KAI_TOAST); if (pDlgToast) { CStdString desc; CStdString path; CStdString script; URIUtils::Split(m_source, path, script); if (script.Equals("default.py")) { CStdString path2; URIUtils::RemoveSlashAtEnd(path); URIUtils::Split(path, path2, script); } desc.Format(g_localizeStrings.Get(2100), script); pDlgToast->QueueNotification(CGUIDialogKaiToast::Error, g_localizeStrings.Get(257), desc); } } } PyObject *m = PyImport_AddModule((char*)"xbmc"); if(!m || PyObject_SetAttrString(m, (char*)"abortRequested", PyBool_FromLong(1))) CLog::Log(LOGERROR, "Scriptresult: failed to set abortRequested"); // make sure all sub threads have finished for(PyThreadState* s = state->interp->tstate_head, *old = NULL; s;) { if(s == state) { s = s->next; continue; } if(old != s) { CLog::Log(LOGINFO, "Scriptresult: Waiting on thread %"PRIu64, (uint64_t)s->thread_id); old = s; } CPyThreadState pyState; Sleep(100); pyState.Restore(); s = state->interp->tstate_head; } // pending calls must be cleared out XBMCAddon::RetardedAsynchCallbackHandler::clearPendingCalls(state); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); //set stopped event - this allows ::stop to run and kill remaining threads //this event has to be fired without holding m_pExecuter->m_critSection //before //Also the GIL (PyEval_AcquireLock) must not be held //if not obeyed there is still no deadlock because ::stop waits with timeout (smart one!) stoppedEvent.Set(); { CSingleLock lock(m_pExecuter->m_critSection); m_threadState = NULL; } PyEval_AcquireLock(); PyThreadState_Swap(state); m_pExecuter->DeInitializeInterpreter(); // run the gc before finishing if (!m_stopping && languageHook->HasRegisteredAddonClasses() && PyRun_SimpleString(GC_SCRIPT) == -1) CLog::Log(LOGERROR,"Failed to run the gc to clean up after running prior to shutting down the Interpreter %s",m_source); Py_EndInterpreter(state); // This is a total hack. Python doesn't necessarily release // all of the objects associated with the interpreter when // you end the interpreter. As a result there are objects // managed by the windowing system that still receive events // until python decides to clean them up. Python will eventually // clean them up on the creation or ending of a subsequent // interpreter. So we are going to keep creating and ending // interpreters until we have no more python objects hanging // around. if (languageHook->HasRegisteredAddonClasses()) { CLog::Log(LOGDEBUG, "The python script \"%s\" has left several " "classes in memory that we will be attempting to clean up. The classes include: %s", m_source, getListOfAddonClassesAsString(languageHook).c_str()); int countLimit; for (countLimit = 0; languageHook->HasRegisteredAddonClasses() && countLimit < 100; countLimit++) { PyThreadState* tmpstate = Py_NewInterpreter(); PyThreadState* oldstate = PyThreadState_Swap(tmpstate); if (PyRun_SimpleString(GC_SCRIPT) == -1) CLog::Log(LOGERROR,"Failed to run the gc to clean up after running %s",m_source); PyThreadState_Swap(oldstate); Py_EndInterpreter(tmpstate); } // If necessary and successfull, debug log the results. if (countLimit > 0 && !languageHook->HasRegisteredAddonClasses()) CLog::Log(LOGDEBUG,"It took %d Py_NewInterpreter/Py_EndInterpreter calls" " to clean up the classes leftover from running \"%s.\"", countLimit,m_source); // If not successful, produce an error message detailing what's been left behind if (languageHook->HasRegisteredAddonClasses()) CLog::Log(LOGERROR, "The python script \"%s\" has left several " "classes in memory that we couldn't clean up. The classes include: %s", m_source, getListOfAddonClassesAsString(languageHook).c_str()); } // unregister the language hook languageHook->UnregisterMe(); PyThreadState_Swap(NULL); PyEval_ReleaseLock(); }
extern "C" void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing) { /* context values */ struct wmWindow *win= CTX_wm_window(C); struct Scene *scene= CTX_data_scene(C); struct Main* maggie1= CTX_data_main(C); RAS_Rect area_rect; area_rect.SetLeft(cam_frame->xmin); area_rect.SetBottom(cam_frame->ymin); area_rect.SetRight(cam_frame->xmax); area_rect.SetTop(cam_frame->ymax); int exitrequested = KX_EXIT_REQUEST_NO_REQUEST; Main* blenderdata = maggie1; char* startscenename = scene->id.name+2; char pathname[FILE_MAXDIR+FILE_MAXFILE], oldsce[FILE_MAXDIR+FILE_MAXFILE]; STR_String exitstring = ""; BlendFileData *bfd= NULL; BLI_strncpy(pathname, blenderdata->name, sizeof(pathname)); BLI_strncpy(oldsce, G.sce, sizeof(oldsce)); #ifndef DISABLE_PYTHON resetGamePythonPath(); // need this so running a second time wont use an old blendfiles path setGamePythonPath(G.sce); // Acquire Python's GIL (global interpreter lock) // so we can safely run Python code and API calls PyGILState_STATE gilstate = PyGILState_Ensure(); PyObject *pyGlobalDict = PyDict_New(); /* python utility storage, spans blend file loading */ #endif bgl::InitExtensions(true); do { View3D *v3d= CTX_wm_view3d(C); RegionView3D *rv3d= CTX_wm_region_view3d(C); // get some preferences SYS_SystemHandle syshandle = SYS_GetSystem(); bool properties = (SYS_GetCommandLineInt(syshandle, "show_properties", 0) != 0); bool usefixed = (SYS_GetCommandLineInt(syshandle, "fixedtime", 0) != 0); bool profile = (SYS_GetCommandLineInt(syshandle, "show_profile", 0) != 0); bool frameRate = (SYS_GetCommandLineInt(syshandle, "show_framerate", 0) != 0); bool game2ipo = (SYS_GetCommandLineInt(syshandle, "game2ipo", 0) != 0); bool displaylists = (SYS_GetCommandLineInt(syshandle, "displaylists", 0) != 0); bool nodepwarnings = (SYS_GetCommandLineInt(syshandle, "ignore_deprecation_warnings", 0) != 0); bool novertexarrays = (SYS_GetCommandLineInt(syshandle, "novertexarrays", 0) != 0); // create the canvas, rasterizer and rendertools RAS_ICanvas* canvas = new KX_BlenderCanvas(win, area_rect); canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE); RAS_IRenderTools* rendertools = new KX_BlenderRenderTools(); RAS_IRasterizer* rasterizer = NULL; if(displaylists) { if (GLEW_VERSION_1_1 && !novertexarrays) rasterizer = new RAS_ListRasterizer(canvas, true, true); else rasterizer = new RAS_ListRasterizer(canvas); } else if (GLEW_VERSION_1_1 && !novertexarrays) rasterizer = new RAS_VAOpenGLRasterizer(canvas, false); else rasterizer = new RAS_OpenGLRasterizer(canvas); // create the inputdevices KX_BlenderKeyboardDevice* keyboarddevice = new KX_BlenderKeyboardDevice(); KX_BlenderMouseDevice* mousedevice = new KX_BlenderMouseDevice(); // create a networkdevice NG_NetworkDeviceInterface* networkdevice = new NG_LoopBackNetworkDeviceInterface(); // // create a ketsji/blendersystem (only needed for timing and stuff) KX_BlenderSystem* kxsystem = new KX_BlenderSystem(); // create the ketsjiengine KX_KetsjiEngine* ketsjiengine = new KX_KetsjiEngine(kxsystem); // set the devices ketsjiengine->SetKeyboardDevice(keyboarddevice); ketsjiengine->SetMouseDevice(mousedevice); ketsjiengine->SetNetworkDevice(networkdevice); ketsjiengine->SetCanvas(canvas); ketsjiengine->SetRenderTools(rendertools); ketsjiengine->SetRasterizer(rasterizer); ketsjiengine->SetNetworkDevice(networkdevice); ketsjiengine->SetUseFixedTime(usefixed); ketsjiengine->SetTimingDisplay(frameRate, profile, properties); #ifndef DISABLE_PYTHON CValue::SetDeprecationWarnings(nodepwarnings); #endif //lock frame and camera enabled - storing global values int tmp_lay= scene->lay; Object *tmp_camera = scene->camera; if (v3d->scenelock==0){ scene->lay= v3d->lay; scene->camera= v3d->camera; } // some blender stuff MT_CmMatrix4x4 projmat; MT_CmMatrix4x4 viewmat; float camzoom; int i; for (i = 0; i < 16; i++) { float *viewmat_linear= (float*) rv3d->viewmat; viewmat.setElem(i, viewmat_linear[i]); } for (i = 0; i < 16; i++) { float *projmat_linear= (float*) rv3d->winmat; projmat.setElem(i, projmat_linear[i]); } if(rv3d->persp==RV3D_CAMOB) { if(scene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */ camzoom = 1.0f; } else { camzoom = (1.41421 + (rv3d->camzoom / 50.0)); camzoom *= camzoom; camzoom = 4.0 / camzoom; } } else { camzoom = 2.0; } ketsjiengine->SetDrawType(v3d->drawtype); ketsjiengine->SetCameraZoom(camzoom); // if we got an exitcode 3 (KX_EXIT_REQUEST_START_OTHER_GAME) load a different file if (exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME || exitrequested == KX_EXIT_REQUEST_RESTART_GAME) { exitrequested = KX_EXIT_REQUEST_NO_REQUEST; if (bfd) BLO_blendfiledata_free(bfd); char basedpath[240]; // base the actuator filename with respect // to the original file working directory if (exitstring != "") strcpy(basedpath, exitstring.Ptr()); // load relative to the last loaded file, this used to be relative // to the first file but that makes no sense, relative paths in // blend files should be relative to that file, not some other file // that happened to be loaded first BLI_convertstringcode(basedpath, pathname); bfd = load_game_data(basedpath); // if it wasn't loaded, try it forced relative if (!bfd) { // just add "//" in front of it char temppath[242]; strcpy(temppath, "//"); strcat(temppath, basedpath); BLI_convertstringcode(temppath, pathname); bfd = load_game_data(temppath); } // if we got a loaded blendfile, proceed if (bfd) { blenderdata = bfd->main; startscenename = bfd->curscene->id.name + 2; if(blenderdata) { BLI_strncpy(G.sce, blenderdata->name, sizeof(G.sce)); BLI_strncpy(pathname, blenderdata->name, sizeof(pathname)); #ifndef DISABLE_PYTHON setGamePythonPath(G.sce); #endif } } // else forget it, we can't find it else { exitrequested = KX_EXIT_REQUEST_QUIT_GAME; } } Scene *blscene = NULL; if (!bfd) { blscene = (Scene*) blenderdata->scene.first; for (Scene *sce= (Scene*) blenderdata->scene.first; sce; sce= (Scene*) sce->id.next) { if (startscenename == (sce->id.name+2)) { blscene = sce; break; } } } else { blscene = bfd->curscene; } if (blscene) { int startFrame = blscene->r.cfra; ketsjiengine->SetGame2IpoMode(game2ipo,startFrame); // Quad buffered needs a special window. if(blscene->gm.stereoflag == STEREO_ENABLED){ if (blscene->gm.stereomode != RAS_IRasterizer::RAS_STEREO_QUADBUFFERED) rasterizer->SetStereoMode((RAS_IRasterizer::StereoMode) blscene->gm.stereomode); } rasterizer->SetBackColor(blscene->gm.framing.col[0], blscene->gm.framing.col[1], blscene->gm.framing.col[2], 0.0f); } if (exitrequested != KX_EXIT_REQUEST_QUIT_GAME) { if (rv3d->persp != RV3D_CAMOB) { ketsjiengine->EnableCameraOverride(startscenename); ketsjiengine->SetCameraOverrideUseOrtho((rv3d->persp == RV3D_ORTHO)); ketsjiengine->SetCameraOverrideProjectionMatrix(projmat); ketsjiengine->SetCameraOverrideViewMatrix(viewmat); ketsjiengine->SetCameraOverrideClipping(v3d->near, v3d->far); ketsjiengine->SetCameraOverrideLens(v3d->lens); } // create a scene converter, create and convert the startingscene KX_ISceneConverter* sceneconverter = new KX_BlenderSceneConverter(blenderdata, ketsjiengine); ketsjiengine->SetSceneConverter(sceneconverter); sceneconverter->addInitFromFrame=false; if (always_use_expand_framing) sceneconverter->SetAlwaysUseExpandFraming(true); bool usemat = false, useglslmat = false; if(GLEW_ARB_multitexture && GLEW_VERSION_1_1) usemat = true; if(GPU_glsl_support()) useglslmat = true; else if(blscene->gm.matmode == GAME_MAT_GLSL) usemat = false; if(usemat && (blscene->gm.matmode != GAME_MAT_TEXFACE)) sceneconverter->SetMaterials(true); if(useglslmat && (blscene->gm.matmode == GAME_MAT_GLSL)) sceneconverter->SetGLSLMaterials(true); KX_Scene* startscene = new KX_Scene(keyboarddevice, mousedevice, networkdevice, startscenename, blscene); #ifndef DISABLE_PYTHON // some python things PyObject* dictionaryobject = initGamePythonScripting("Ketsji", psl_Lowest, blenderdata); ketsjiengine->SetPyNamespace(dictionaryobject); initRasterizer(rasterizer, canvas); PyObject *gameLogic = initGameLogic(ketsjiengine, startscene); PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module. PyObject *gameLogic_keys = PyDict_Keys(PyModule_GetDict(gameLogic)); PyDict_SetItemString(dictionaryobject, "GameLogic", gameLogic); // Same as importing the module. initGameKeys(); initPythonConstraintBinding(); initMathutils(); initGeometry(); initBGL(); #ifdef WITH_FFMPEG initVideoTexture(); #endif #endif // DISABLE_PYTHON //initialize Dome Settings if(blscene->gm.stereoflag == STEREO_DOME) ketsjiengine->InitDome(blscene->gm.dome.res, blscene->gm.dome.mode, blscene->gm.dome.angle, blscene->gm.dome.resbuf, blscene->gm.dome.tilt, blscene->gm.dome.warptext); // initialize 3D Audio Settings AUD_set3DSetting(AUD_3DS_SPEED_OF_SOUND, blscene->audio.speed_of_sound); AUD_set3DSetting(AUD_3DS_DOPPLER_FACTOR, blscene->audio.doppler_factor); AUD_set3DSetting(AUD_3DS_DISTANCE_MODEL, blscene->audio.distance_model); if (sceneconverter) { // convert and add scene sceneconverter->ConvertScene( startscene, rendertools, canvas); ketsjiengine->AddScene(startscene); // init the rasterizer rasterizer->Init(); // start the engine ketsjiengine->StartEngine(true); // Set the animation playback rate for ipo's and actions // the framerate below should patch with FPS macro defined in blendef.h // Could be in StartEngine set the framerate, we need the scene to do this ketsjiengine->SetAnimFrameRate( (((double) blscene->r.frs_sec) / blscene->r.frs_sec_base) ); // the mainloop printf("\nBlender Game Engine Started\n\n"); while (!exitrequested) { // first check if we want to exit exitrequested = ketsjiengine->GetExitCode(); // kick the engine bool render = ketsjiengine->NextFrame(); // XXX 2.5 Bug, This is never true! FIXME- Campbell if (render) { // render the frame ketsjiengine->Render(); } wm_window_process_events_nosleep(C); // test for the ESC key //XXX while (qtest()) while(wmEvent *event= (wmEvent *)win->queue.first) { short val = 0; //unsigned short event = 0; //XXX extern_qread(&val); if (keyboarddevice->ConvertBlenderEvent(event->type,event->val)) exitrequested = KX_EXIT_REQUEST_BLENDER_ESC; /* Coordinate conversion... where * should this really be? */ if (event->type==MOUSEMOVE) { /* Note nice! XXX 2.5 event hack */ val = event->x - ar->winrct.xmin; mousedevice->ConvertBlenderEvent(MOUSEX, val); val = ar->winy - (event->y - ar->winrct.ymin) - 1; mousedevice->ConvertBlenderEvent(MOUSEY, val); } else { mousedevice->ConvertBlenderEvent(event->type,event->val); } BLI_remlink(&win->queue, event); wm_event_free(event); } } printf("\nBlender Game Engine Finished\n\n"); exitstring = ketsjiengine->GetExitString(); // when exiting the mainloop #ifndef DISABLE_PYTHON // Clears the dictionary by hand: // This prevents, extra references to global variables // inside the GameLogic dictionary when the python interpreter is finalized. // which allows the scene to safely delete them :) // see: (space.c)->start_game //PyDict_Clear(PyModule_GetDict(gameLogic)); // Keep original items, means python plugins will autocomplete members int listIndex; PyObject *gameLogic_keys_new = PyDict_Keys(PyModule_GetDict(gameLogic)); for (listIndex=0; listIndex < PyList_Size(gameLogic_keys_new); listIndex++) { PyObject* item = PyList_GET_ITEM(gameLogic_keys_new, listIndex); if (!PySequence_Contains(gameLogic_keys, item)) { PyDict_DelItem( PyModule_GetDict(gameLogic), item); } } Py_DECREF(gameLogic_keys_new); gameLogic_keys_new = NULL; #endif ketsjiengine->StopEngine(); #ifndef DISABLE_PYTHON exitGamePythonScripting(); #endif networkdevice->Disconnect(); } if (sceneconverter) { delete sceneconverter; sceneconverter = NULL; } #ifndef DISABLE_PYTHON Py_DECREF(gameLogic_keys); gameLogic_keys = NULL; #endif } //lock frame and camera enabled - restoring global values if (v3d->scenelock==0){ scene->lay= tmp_lay; scene->camera= tmp_camera; } // set the cursor back to normal canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL); // clean up some stuff if (ketsjiengine) { delete ketsjiengine; ketsjiengine = NULL; } if (kxsystem) { delete kxsystem; kxsystem = NULL; } if (networkdevice) { delete networkdevice; networkdevice = NULL; } if (keyboarddevice) { delete keyboarddevice; keyboarddevice = NULL; } if (mousedevice) { delete mousedevice; mousedevice = NULL; } if (rasterizer) { delete rasterizer; rasterizer = NULL; } if (rendertools) { delete rendertools; rendertools = NULL; } if (canvas) { delete canvas; canvas = NULL; } } while (exitrequested == KX_EXIT_REQUEST_RESTART_GAME || exitrequested == KX_EXIT_REQUEST_START_OTHER_GAME); if (bfd) BLO_blendfiledata_free(bfd); BLI_strncpy(G.sce, oldsce, sizeof(G.sce)); #ifndef DISABLE_PYTHON Py_DECREF(pyGlobalDict); // Release Python's GIL PyGILState_Release(gilstate); #endif }
static nxweb_result python_on_request(nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) { nxb_buffer* nxb=req->nxb; nxweb_handler* handler=conn->handler; const char* request_uri=req->uri; char* query_string=strchr(request_uri, '?'); int ulen=query_string? (query_string-request_uri) : strlen(request_uri); if (query_string) query_string++; int pfxlen=req->path_info? (req->path_info - req->uri) : 0; int plen=ulen-pfxlen; const char* path_info=request_uri+pfxlen; if (handler->uri && *handler->uri) { pfxlen=strlen(handler->uri); ulen=pfxlen+plen; char* u=nxb_alloc_obj(nxb, ulen+1); memcpy(u, handler->uri, pfxlen); memcpy(u+pfxlen, path_info, plen); u[ulen]='\0'; request_uri=u; path_info=request_uri+pfxlen; } const char* host_port=req->host? strchr(req->host, ':') : 0; int content_fd=0; if (req->content_length) { nxd_fwbuffer* fwb=nxweb_get_request_data(req, PYTHON_HANDLER_KEY).ptr; if (fwb) { if (fwb->error || fwb->size > fwb->max_size) { nxweb_send_http_error(resp, 413, "Request Entity Too Large"); // most likely cause return NXWEB_ERROR; } else if (req->content_received!=fwb->size) { nxweb_log_error("content_received does not match upload stored size for %s", req->uri); nxweb_send_http_error(resp, 500, "Internal Server Error"); return NXWEB_ERROR; } else { content_fd=fwb->fd; if (lseek(content_fd, 0, SEEK_SET)==-1) { nxweb_log_error("can't lseek() temp upload file for %s", req->uri); nxweb_send_http_error(resp, 500, "Internal Server Error"); return NXWEB_ERROR; } } } } nxweb_log_debug("invoke python"); PyGILState_STATE gstate=PyGILState_Ensure(); PyObject* py_func_args=PyTuple_New(1); PyObject* py_environ=PyDict_New(); assert(PyDict_Check(py_environ)); dict_set(py_environ, "SERVER_NAME", PyString_FromStringAndSize(req->host, host_port? (host_port-req->host) : strlen(req->host))); dict_set(py_environ, "SERVER_PORT", PyString_FromString(host_port? host_port+1 : "")); dict_set(py_environ, "SERVER_PROTOCOL", PyString_FromString(req->http11? "HTTP/1.1" : "HTTP/1.0")); dict_set(py_environ, "SERVER_SOFTWARE", PyString_FromString(PACKAGE_STRING)); dict_set(py_environ, "GATEWAY_INTERFACE", PyString_FromString("CGI/1.1")); dict_set(py_environ, "REQUEST_METHOD", PyString_FromString(req->method)); dict_set(py_environ, "REQUEST_URI", PyString_FromStringAndSize(request_uri, ulen)); dict_set(py_environ, "SCRIPT_NAME", PyString_FromStringAndSize(request_uri, pfxlen)); dict_set(py_environ, "PATH_INFO", PyString_FromStringAndSize(path_info, plen)); dict_set(py_environ, "QUERY_STRING", PyString_FromString(query_string? query_string : "")); dict_set(py_environ, "REMOTE_ADDR", PyString_FromString(conn->remote_addr)); dict_set(py_environ, "CONTENT_TYPE", PyString_FromString(req->content_type? req->content_type : "")); dict_set(py_environ, "CONTENT_LENGTH", PyInt_FromLong(req->content_received)); if (req->cookie) dict_set(py_environ, "HTTP_COOKIE", PyString_FromString(req->cookie)); if (req->host) dict_set(py_environ, "HTTP_HOST", PyString_FromString(req->host)); if (req->user_agent) dict_set(py_environ, "HTTP_USER_AGENT", PyString_FromString(req->user_agent)); if (req->if_modified_since) { struct tm tm; gmtime_r(&req->if_modified_since, &tm); char ims[32]; nxweb_format_http_time(ims, &tm); dict_set(py_environ, "HTTP_IF_MODIFIED_SINCE", PyString_FromString(ims)); } if (req->headers) { // write added headers // encode http headers into CGI variables; see 4.1.18 in https://tools.ietf.org/html/rfc3875 char hname[256]; memcpy(hname, "HTTP_", 5); char* h=hname+5; nx_simple_map_entry* itr; for (itr=nx_simple_map_itr_begin(req->headers); itr; itr=nx_simple_map_itr_next(itr)) { nx_strtoupper(h, itr->name); char* p; for (p=h; *p; p++) { if (*p=='-') *p='_'; } dict_set(py_environ, hname, PyString_FromString(itr->value)); } } dict_set(py_environ, "wsgi.url_scheme", PyString_FromString(conn->secure? "https" : "http")); if (req->content_length) { if (content_fd) { dict_set(py_environ, "nxweb.req.content_fd", PyInt_FromLong(content_fd)); } else { dict_set(py_environ, "nxweb.req.content", PyByteArray_FromStringAndSize(req->content? req->content : "", req->content_received)); } } if (req->if_modified_since) dict_set(py_environ, "nxweb.req.if_modified_since", PyLong_FromLong(req->if_modified_since)); dict_set(py_environ, "nxweb.req.uid", PyLong_FromLongLong(req->uid)); if (req->parent_req) { nxweb_http_request* preq=req->parent_req; while (preq->parent_req) preq=preq->parent_req; // find root request if (preq->uid) { dict_set(py_environ, "nxweb.req.root_uid", PyLong_FromLongLong(preq->uid)); } } // call python PyTuple_SetItem(py_func_args, 0, py_environ); PyObject* py_result=PyObject_CallObject(py_nxweb_on_request_func, py_func_args); Py_DECREF(py_func_args); if (py_result && PyTuple_Check(py_result) && PyTuple_Size(py_result)==3) { PyObject* py_status=PyTuple_GET_ITEM(py_result, 0); PyObject* py_headers=PyTuple_GET_ITEM(py_result, 1); PyObject* py_body=PyTuple_GET_ITEM(py_result, 2); if (py_status && PyString_Check(py_status)) { const char* status_string=PyString_AS_STRING(py_status); int status_code=0; const char* p=status_string; while (*p && *p>='0' && *p<='9') { status_code=status_code*10+(*p-'0'); p++; } while (*p && *p==' ') p++; if (status_code>=200 && status_code<600 && *p) { resp->status_code=status_code; resp->status=nxb_copy_str(nxb, p); } } if (py_headers && PyList_Check(py_headers)) { const int size=PyList_Size(py_headers); int i; for (i=0; i<size; i++) { PyObject* py_header_tuple=PyList_GET_ITEM(py_headers, i); if (py_header_tuple && PyTuple_Check(py_header_tuple) && PyTuple_Size(py_header_tuple)==2) { PyObject* py_name=PyTuple_GET_ITEM(py_header_tuple, 0); PyObject* py_value=PyTuple_GET_ITEM(py_header_tuple, 1); if (py_name && PyString_Check(py_name) && py_value && PyString_Check(py_value)) { nxweb_add_response_header_safe(resp, PyString_AS_STRING(py_name), PyString_AS_STRING(py_value)); } } } } if ((!resp->status_code || resp->status_code==200) && !resp->content_type) resp->content_type="text/html"; char* rcontent=0; nxe_ssize_t rsize=0; if (PyByteArray_Check(py_body)) { rcontent=PyByteArray_AS_STRING(py_body); rsize=PyByteArray_Size(py_body); } else if (PyString_Check(py_body)) { rcontent=PyString_AS_STRING(py_body); rsize=PyString_Size(py_body); } if (rcontent && rsize>0) nxweb_response_append_data(resp, rcontent, rsize); } else if (py_result && PyString_Check(py_result)) { resp->status_code=500; resp->status="Internal Server Error"; resp->content_type="text/html"; nxweb_log_error("python call failed: %s", PyString_AS_STRING(py_result)); nxweb_response_printf(resp, "python call failed: %H", PyString_AS_STRING(py_result)); } else { PyErr_Print(); nxweb_log_error("python call failed"); nxweb_response_printf(resp, "python call failed"); } Py_XDECREF(py_result); // Release the thread. No Python API allowed beyond this point. PyGILState_Release(gstate); nxweb_log_debug("invoke python complete"); return NXWEB_OK; }
/** ******************************************************************************************************* * This function will get a batch of records from the Aeropike DB. * * @param err as_error object * @param self AerospikeClient object * @param py_keys The list of keys * @param batch_policy_p as_policy_batch object * * Returns the record if key exists otherwise NULL. ******************************************************************************************************* */ static PyObject * batch_get_aerospike_batch_get(as_error *err, AerospikeClient * self, PyObject *py_keys, as_policy_batch * batch_policy_p) { PyObject * py_recs = NULL; LocalData data; data.client = self; as_batch batch; bool batch_initialised = false; // Convert python keys list to as_key ** and add it to as_batch.keys // keys can be specified in PyList or PyTuple if ( py_keys != NULL && PyList_Check(py_keys) ) { Py_ssize_t size = PyList_Size(py_keys); py_recs = PyList_New(size); data.py_recs = py_recs; as_batch_init(&batch, size); // Batch object initialised batch_initialised = true; for ( int i = 0; i < size; i++ ) { PyObject * py_key = PyList_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ) { as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } pyobject_to_key(err, py_key, as_batch_keyat(&batch, i)); if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } } } else if ( py_keys != NULL && PyTuple_Check(py_keys) ) { Py_ssize_t size = PyTuple_Size(py_keys); py_recs = PyList_New(size); data.py_recs = py_recs; as_batch_init(&batch, size); // Batch object initialised batch_initialised = true; for ( int i = 0; i < size; i++ ) { PyObject * py_key = PyTuple_GetItem(py_keys, i); if ( !PyTuple_Check(py_key) ) { as_error_update(err, AEROSPIKE_ERR_PARAM, "Key should be a tuple."); goto CLEANUP; } pyobject_to_key(err, py_key, as_batch_keyat(&batch, i)); if ( err->code != AEROSPIKE_OK ) { goto CLEANUP; } } } else { as_error_update(err, AEROSPIKE_ERR_PARAM, "Keys should be specified as a list or tuple."); goto CLEANUP; } // Invoke C-client API Py_BEGIN_ALLOW_THREADS aerospike_batch_get(self->as, err, batch_policy_p, &batch, (aerospike_batch_read_callback) batch_get_cb, &data); Py_END_ALLOW_THREADS CLEANUP: if (batch_initialised == true) { // We should destroy batch object as we are using 'as_batch_init' for initialisation // Also, pyobject_to_key is soing strdup() in case of Unicode. So, object destruction // is necessary. as_batch_destroy(&batch); } return py_recs; }
static PyObject *csolve(PyObject* self, PyObject *args, PyObject *kwargs) { /* Expects a function call * sol = csolve((m,n,p),c,Gx,Gi,Gp,h,dims,Ax,Ai,Ap,b,verbose) * where * * the triple (m,n,p) corresponds to: * `m`: the rows of G * `n`: the cols of G and A, must agree with the length of c * `p`: the rows of A * `c` is a Numpy array of doubles * "G" is a sparse matrix in column compressed storage. "Gx" are the values, * "Gi" are the rows, and "Gp" are the column pointers. * `Gx` is a Numpy array of doubles * `Gi` is a Numpy array of ints * `Gp` is a Numpy array of ints * `h` is a Numpy array * `dims` is a dictionary with * `dims['l']` an integer specifying the dimension of positive orthant cone * `dims['q']` an *list* specifying dimensions of second-order cones * * "A" is an optional sparse matrix in column compressed storage. "Ax" are * the values, "Ai" are the rows, and "Ap" are the column pointers. * `Ax` is a Numpy array of doubles * `Ai` is a Numpy array of ints * `Ap` is a Numpy array of ints * `b` is an optional argument, which is a Numpy array of doubles * `verbose` is an optional bool signaling whether to print info * * This call will solve the problem * * minimize c'*x * subject to A*x = b * h - G*x \in K * * The code returns a Python dictionary with five keys, 'x', 'y', 'info', 's', * and 'z'. These correspond to the following: * * `x`: primal variables * `y`: dual variables for equality constraints * `s`: slacks for Gx + s <= h, s \in K * `z`: dual variables for inequality constraints s \in K * `info`: another dictionary with the following fields: * exitflag: 0=OPTIMAL, 1=PRIMAL INFEASIBLE, 2=DUAL INFEASIBLE, -1=MAXIT REACHED * infostring: gives information about the status of solution * pcost: value of primal objective * dcost: value of dual objective * pres: primal residual on inequalities and equalities * dres: dual residual * pinf: primal infeasibility measure * dinf: dual infeasibility measure * pinfres: NaN * dinfres: 3.9666e+15 * gap: duality gap * relgap: relative duality gap * r0: ??? * numerr: numerical error? * iter: number of iterations * timing: dictionary with timing information */ /* data structures for arguments */ //matrix *c, *h, *b = NULL; //spmatrix *G, *A = NULL; PyArrayObject *Gx, *Gi, *Gp, *c, *h; PyArrayObject *Ax = NULL; PyArrayObject *Ai = NULL; PyArrayObject *Ap = NULL; PyArrayObject *b = NULL; PyObject *dims, *verbose = NULL; idxint n; // number or variables idxint m; // number of conic variables idxint p = 0; // number of equality constraints idxint ncones = 0; // number of cones idxint numConicVariables = 0; /* ECOS data structures */ idxint l = 0; idxint *q = NULL; pfloat *Gpr = NULL; idxint *Gjc = NULL; idxint *Gir = NULL; pfloat *Apr = NULL; idxint *Ajc = NULL; idxint *Air = NULL; pfloat *cpr = NULL; pfloat *hpr = NULL; pfloat *bpr = NULL; pwork* mywork; idxint i; static char *kwlist[] = {"shape", "c", "Gx", "Gi", "Gp", "h", "dims", "Ax", "Ai", "Ap", "b", "verbose", NULL}; // parse the arguments and ensure they are the correct type #ifdef DLONG static char *argparse_string = "(lll)O!O!O!O!O!O!|O!O!O!O!O!"; #else static char *argparse_string = "(iii)O!O!O!O!O!O!|O!O!O!O!O!"; #endif if( !PyArg_ParseTupleAndKeywords(args, kwargs, argparse_string, kwlist, &m, &n, &p, &PyArray_Type, &c, &PyArray_Type, &Gx, &PyArray_Type, &Gi, &PyArray_Type, &Gp, &PyArray_Type, &h, &PyDict_Type, &dims, &PyArray_Type, &Ax, &PyArray_Type, &Ai, &PyArray_Type, &Ap, &PyArray_Type, &b, &PyBool_Type, &verbose) ) { return NULL; } if (m < 0) { PyErr_SetString(PyExc_ValueError, "m must be a positive integer"); return NULL; } if (n < 0) { PyErr_SetString(PyExc_ValueError, "n must be a positive integer"); return NULL; } if (p < 0) { PyErr_SetString(PyExc_ValueError, "p must be a positive integer"); return NULL; } /* get the typenum for the primitive int and double types */ int intType = getIntType(); int doubleType = getDoubleType(); /* set G */ if( !PyArray_ISFLOAT(Gx) || PyArray_NDIM(Gx) != 1) { PyErr_SetString(PyExc_TypeError, "Gx must be a numpy array of floats"); return NULL; } if( !PyArray_ISINTEGER(Gi) || PyArray_NDIM(Gi) != 1) { PyErr_SetString(PyExc_TypeError, "Gi must be a numpy array of ints"); return NULL; } if( !PyArray_ISINTEGER(Gp) || PyArray_NDIM(Gp) != 1) { PyErr_SetString(PyExc_TypeError, "Gp must be a numpy array of ints"); return NULL; } PyArrayObject *Gx_arr = getContiguous(Gx, doubleType); PyArrayObject *Gi_arr = getContiguous(Gi, intType); PyArrayObject *Gp_arr = getContiguous(Gp, intType); Gpr = (pfloat *) PyArray_DATA(Gx_arr); Gir = (idxint *) PyArray_DATA(Gi_arr); Gjc = (idxint *) PyArray_DATA(Gp_arr); /* set c */ if (!PyArray_ISFLOAT(c) || PyArray_NDIM(c) != 1) { PyErr_SetString(PyExc_TypeError, "c must be a dense numpy array with one dimension"); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); return NULL; } if (PyArray_DIM(c,0) != n){ PyErr_SetString(PyExc_ValueError, "c has incompatible dimension with G"); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); return NULL; } PyArrayObject *c_arr = getContiguous(c, doubleType); cpr = (pfloat *) PyArray_DATA(c_arr); /* set h */ if (!PyArray_ISFLOAT(h) || PyArray_NDIM(h) != 1) { PyErr_SetString(PyExc_TypeError, "h must be a dense numpy array with one dimension"); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); return NULL; } if (PyArray_DIM(h,0) != m){ PyErr_SetString(PyExc_ValueError, "h has incompatible dimension with G"); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); return NULL; } PyArrayObject *h_arr = getContiguous(h, doubleType); hpr = (pfloat *) PyArray_DATA(h_arr); /* get dims['l'] */ PyObject *linearObj = PyDict_GetItemString(dims, "l"); if(linearObj) { if(PyInt_Check(linearObj) && ((l = (idxint) PyInt_AsLong(linearObj)) >= 0)) { numConicVariables += l; } else { PyErr_SetString(PyExc_TypeError, "dims['l'] ought to be a nonnegative integer"); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); return NULL; } } /* get dims['q'] */ PyObject *socObj = PyDict_GetItemString(dims, "q"); if(socObj) { if (PyList_Check(socObj)) { ncones = PyList_Size(socObj); q = calloc(ncones, sizeof(idxint)); for (i = 0; i < ncones; ++i) { PyObject *qi = PyList_GetItem(socObj, i); if(PyInt_Check(qi) && ((q[i] = (idxint) PyInt_AsLong(qi)) > 0)) { numConicVariables += q[i]; } else { PyErr_SetString(PyExc_TypeError, "dims['q'] ought to be a list of positive integers"); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); return NULL; } } } else { PyErr_SetString(PyExc_TypeError, "dims['q'] ought to be a list"); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); return NULL; } } PyArrayObject *Ax_arr = NULL; PyArrayObject *Ai_arr = NULL; PyArrayObject *Ap_arr = NULL; PyArrayObject *b_arr = NULL; if(Ax && Ai && Ap && b) { /* set A */ if( !PyArray_ISFLOAT(Ax) || PyArray_NDIM(Ax) != 1 ) { PyErr_SetString(PyExc_TypeError, "Ax must be a numpy array of floats"); if(q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); return NULL; } if( !PyArray_ISINTEGER(Ai) || PyArray_NDIM(Ai) != 1) { PyErr_SetString(PyExc_TypeError, "Ai must be a numpy array of ints"); if(q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); return NULL; } if( !PyArray_ISINTEGER(Ap) || PyArray_NDIM(Ap) != 1) { PyErr_SetString(PyExc_TypeError, "Ap must be a numpy array of ints"); if(q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); return NULL; } // if ((SpMatrix_Check(A) && SP_ID(A) != DOUBLE)){ // PyErr_SetString(PyExc_TypeError, "A must be a sparse 'd' matrix"); // if(q) free(q); // Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); // Py_DECREF(c_arr); Py_DECREF(h_arr); // return NULL; // } // if ((p = SP_NROWS(A)) < 0) { // PyErr_SetString(PyExc_ValueError, "p must be a nonnegative integer"); // if(q) free(q); // Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); // Py_DECREF(c_arr); Py_DECREF(h_arr); // return NULL; // } // if (SP_NCOLS(A) != n) { // PyErr_SetString(PyExc_ValueError, "A has incompatible dimension with c"); // if(q) free(q); // Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); // Py_DECREF(c_arr); Py_DECREF(h_arr); // return NULL; // } // if (p != 0) { // Apr = SP_VALD(A); // Air = SP_ROW(A); // Ajc = SP_COL(A); // } Ax_arr = getContiguous(Ax, doubleType); Ai_arr = getContiguous(Ai, intType); Ap_arr = getContiguous(Ap, intType); Apr = (pfloat *) PyArray_DATA(Ax_arr); Air = (idxint *) PyArray_DATA(Ai_arr); Ajc = (idxint *) PyArray_DATA(Ap_arr); /* set b */ // if (!Matrix_Check(b) || MAT_NCOLS(b) != 1 || MAT_ID(b) != DOUBLE) { // PyErr_SetString(PyExc_TypeError, "b must be a dense 'd' matrix with one column"); // if(q) free(q); // return NULL; // } // if (MAT_NROWS(b) != p){ // PyErr_SetString(PyExc_ValueError, "b has incompatible dimension with A"); // if(q) free(q); // return NULL; // } // if (p != 0) { // bpr = MAT_BUFD(b); // } if (!PyArray_ISFLOAT(b) || PyArray_NDIM(b) != 1) { PyErr_SetString(PyExc_TypeError, "b must be a dense numpy array with one dimension"); if(q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); Py_DECREF(Ax_arr); Py_DECREF(Ai_arr); Py_DECREF(Ap_arr); return NULL; } if (PyArray_DIM(b,0) != p){ PyErr_SetString(PyExc_ValueError, "b has incompatible dimension with A"); if(q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); Py_DECREF(Ax_arr); Py_DECREF(Ai_arr); Py_DECREF(Ap_arr); return NULL; } b_arr = getContiguous(b, doubleType); bpr = (pfloat *) PyArray_DATA(b_arr); } else if (Ax || Ai || Ap || b) { // check that A and b are both supplied PyErr_SetString(PyExc_ValueError, "A and b arguments must be supplied together"); if(q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); return NULL; } /* check that sum(q) + l = m */ if( numConicVariables != m ){ PyErr_SetString(PyExc_ValueError, "Number of rows of G does not match dims.l+sum(dims.q)"); if (q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); if (b_arr) Py_DECREF(b_arr); if (Ax_arr) Py_DECREF(Ax_arr); if (Ai_arr) Py_DECREF(Ai_arr); if (Ap_arr) Py_DECREF(Ap_arr); return NULL; } /* This calls ECOS setup function. */ mywork = ECOS_setup(n, m, p, l, ncones, q, Gpr, Gjc, Gir, Apr, Ajc, Air, cpr, hpr, bpr); if( mywork == NULL ){ PyErr_SetString(PyExc_RuntimeError, "Internal problem occurred in ECOS while setting up the problem.\nPlease send a bug report with data to Alexander Domahidi.\nEmail: [email protected]"); if(q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); if (b_arr) Py_DECREF(b_arr); if (Ax_arr) Py_DECREF(Ax_arr); if (Ai_arr) Py_DECREF(Ai_arr); if (Ap_arr) Py_DECREF(Ap_arr); return NULL; } /* Set settings for ECOS. */ if(verbose) { mywork->stgs->verbose = (idxint) PyObject_IsTrue(verbose); } /* Solve! */ idxint exitcode = ECOS_solve(mywork); /* create output (all data is *deep copied*) */ // TODO: request CVXOPT API for constructing from existing pointer /* x */ // matrix *x; // if(!(x = Matrix_New(n,1,DOUBLE))) // return PyErr_NoMemory(); // memcpy(MAT_BUFD(x), mywork->x, n*sizeof(double)); npy_intp veclen[1]; veclen[0] = n; PyObject *x = PyArray_SimpleNewFromData(1, veclen, NPY_DOUBLE, mywork->x); /* y */ // matrix *y; // if(!(y = Matrix_New(p,1,DOUBLE))) // return PyErr_NoMemory(); // memcpy(MAT_BUFD(y), mywork->y, p*sizeof(double)); veclen[0] = p; PyObject *y = PyArray_SimpleNewFromData(1, veclen, NPY_DOUBLE, mywork->y); /* info dict */ // infostring const char* infostring; switch( exitcode ){ case ECOS_OPTIMAL: infostring = "Optimal solution found"; break; case ECOS_MAXIT: infostring = "Maximum number of iterations reached"; break; case ECOS_PINF: infostring = "Primal infeasible"; break; case ECOS_DINF: infostring = "Dual infeasible"; break; case ECOS_NUMERICS: infostring = "Run into numerical problems"; break; case ECOS_OUTCONE: infostring = "PROBLEM: Multipliers leaving the cone"; break; default: infostring = "UNKNOWN PROBLEM IN SOLVER"; } // numerical errors idxint numerr = 0; if( (exitcode == ECOS_NUMERICS) || (exitcode == ECOS_OUTCONE) || (exitcode == ECOS_FATAL) ){ numerr = 1; } // timings #if PROFILING > 0 PyObject *tinfos = Py_BuildValue( #if PROFILING > 1 "{s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d}", #else "{s:d,s:d,s:d}", #endif #if PROFILING > 1 "tkktcreate",(double)mywork->info->tkktcreate, "tkktsolve",(double)mywork->info->tkktsolve, "tkktfactor",(double)mywork->info->tfactor, "torder",(double)mywork->info->torder, "ttranspose",(double)mywork->info->ttranspose, #endif "runtime",(double)mywork->info->tsolve + (double)mywork->info->tsetup, "tsetup",(double)mywork->info->tsetup, "tsolve",(double)mywork->info->tsolve); #endif PyObject *infoDict = Py_BuildValue( #if PROFILING > 0 "{s:l,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:l,s:s,s:O,s:l}", #else "{s:l,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:d,s:l,s:s,s:l}", #endif "exitFlag", exitcode, "pcost", (double)mywork->info->pcost, "dcost", (double)mywork->info->dcost, "pres", (double)mywork->info->pres, "dres", (double)mywork->info->dres, "pinf", (double)mywork->info->pinf, "dinf", (double)mywork->info->dinf, "pinfres",(double)mywork->info->pinfres, "dinfres",(double)mywork->info->dinfres, "gap",(double)mywork->info->gap, "relgap",(double)mywork->info->relgap, "r0",(double)mywork->stgs->feastol, "iter",mywork->info->iter, "infostring",infostring, #if PROFILING > 0 "timing", tinfos, #endif "numerr",numerr); #if PROFILING > 0 // give reference to infoDict Py_DECREF(tinfos); #endif /* s */ // matrix *s; // if(!(s = Matrix_New(m,1,DOUBLE))) // return PyErr_NoMemory(); // memcpy(MAT_BUFD(s), mywork->s, m*sizeof(double)); veclen[0] = m; PyObject *s = PyArray_SimpleNewFromData(1, veclen, NPY_DOUBLE, mywork->s); /* z */ // matrix *z; // if(!(z = Matrix_New(m,1,DOUBLE))) // return PyErr_NoMemory(); // memcpy(MAT_BUFD(z), mywork->z, m*sizeof(double)); veclen[0] = m; PyObject *z = PyArray_SimpleNewFromData(1, veclen, NPY_DOUBLE, mywork->z); /* cleanup */ ECOS_cleanup(mywork, 4); PyObject *returnDict = Py_BuildValue( "{s:O,s:O,s:O,s:O,s:O}", "x",x, "y",y, "z",z, "s",s, "info",infoDict); // give up ownership to the return dictionary Py_DECREF(x); Py_DECREF(y); Py_DECREF(z); Py_DECREF(s); Py_DECREF(infoDict); // no longer need pointers to arrays that held primitives if(q) free(q); Py_DECREF(Gx_arr); Py_DECREF(Gi_arr); Py_DECREF(Gp_arr); Py_DECREF(c_arr); Py_DECREF(h_arr); if (b_arr) Py_DECREF(b_arr); if (Ax_arr) Py_DECREF(Ax_arr); if (Ai_arr) Py_DECREF(Ai_arr); if (Ap_arr) Py_DECREF(Ap_arr); return returnDict; }
std::string getPythonTraceback() { PyObject* tracebackModule; PyObject* tracebackDictionary; PyObject* tracebackFunction; tracebackModule = PyImport_ImportModule("traceback"); if (!tracebackModule) { throw python_error("unable to load traceback module while importing numpy inside PDAL"); } tracebackDictionary = PyModule_GetDict(tracebackModule); tracebackFunction = PyDict_GetItemString(tracebackDictionary, "format_exception"); if (!tracebackFunction) { throw python_error("unable to find traceback function while importing numpy inside PDAL"); } if (!PyCallable_Check(tracebackFunction)) { throw python_error("invalid traceback function while importing numpy inside PDAL"); } // get exception info PyObject *type, *value, *traceback; PyErr_Fetch(&type, &value, &traceback); PyErr_NormalizeException(&type, &value, &traceback); std::ostringstream mssg; if (traceback) { // create an argument for "format exception" PyObject* args = PyTuple_New(3); PyTuple_SetItem(args, 0, type); PyTuple_SetItem(args, 1, value); PyTuple_SetItem(args, 2, traceback); // get a list of string describing what went wrong PyObject* output = PyObject_CallObject(tracebackFunction, args); // print error message int i, n = PyList_Size(output); for (i=0; i<n; i++) mssg << PyString_AsString(PyList_GetItem(output, i)); // clean up Py_XDECREF(args); Py_XDECREF(output); } else if (value != NULL) { PyObject *s = PyObject_Str(value); const char* text = PyString_AS_STRING(s); Py_DECREF(s); mssg << text; } else { mssg << "unknown error"; } Py_XDECREF(value); Py_XDECREF(type); Py_XDECREF(traceback); return mssg.str(); }
// Here we actually print static PyObject *Printer_print(Printer *self) { if (!ScCore->primaryMainWindow()->HaveDoc) { PyErr_SetString(PyExc_SystemError, "Need to open documetnt first"); return NULL; } // copied from void ScribusMainWindow::slotFilePrint() in file scribus.cpp QString fna, prn, cmd, scmd, cc, data, SepName; QString printcomm; bool fil, PSfile; PSfile = false; // ReOrderText(ScCore->primaryMainWindow()->doc, ScCore->primaryMainWindow()->view); prn = QString(PyString_AsString(self->printer)); fna = QString(PyString_AsString(self->file)); fil = (QString(PyString_AsString(self->printer)) == QString("File")) ? true : false; std::vector<int> pageNs; PrintOptions options; for (int i = 0; i < PyList_Size(self->pages); ++i) { options.pageNumbers.push_back((int)PyInt_AsLong(PyList_GetItem(self->pages, i))); } int Nr = (self->copies < 1) ? 1 : self->copies; SepName = QString(PyString_AsString(self->separation)); options.printer = prn; options.prnEngine = (PrintEngine) self->pslevel; options.toFile = fil; options.separationName = SepName; options.outputSeparations = (SepName == QString("No")) ? false : true; options.useColor = self->color; options.mirrorH = self->mph; options.mirrorV = self->mpv; options.useICC = self->useICC; options.doGCR = self->ucr; options.cropMarks = false; options.bleedMarks = false; options.registrationMarks = false; options.colorMarks = false; options.markOffset = 0.0; options.bleeds.set(0, 0, 0, 0); if (!PrinterUtil::checkPrintEngineSupport(options.printer, options.prnEngine, options.toFile)) options.prnEngine = PrinterUtil::getDefaultPrintEngine(options.printer, options.toFile); printcomm = QString(PyString_AsString(self->cmd)); QMap<QString, QMap<uint, FPointArray> > ReallyUsed; ReallyUsed.clear(); ScCore->primaryMainWindow()->doc->getUsedFonts(ReallyUsed); PrefsManager *prefsManager=PrefsManager::instance(); #if defined(_WIN32) if (!options.toFile) { QByteArray devMode; bool printDone = false; if ( PrinterUtil::getDefaultSettings(prn, options.devMode) ) { ScPrintEngine_GDI winPrint; printDone = winPrint.print( *ScCore->primaryMainWindow()->doc, options ); } if (!printDone) PyErr_SetString(PyExc_SystemError, "Printing failed"); Py_RETURN_NONE; } #endif PSLib *dd = new PSLib(options, true, prefsManager->appPrefs.fontPrefs.AvailFonts, ReallyUsed, ScCore->primaryMainWindow()->doc->PageColors, false, true); if (dd != NULL) { if (!fil) fna = QDir::toNativeSeparators(ScPaths::getTempFileDir()+"/tmp.ps"); PSfile = dd->PS_set_file(fna); fna = QDir::toNativeSeparators(fna); if (PSfile) { options.setDevParam = false; options.doClip = false; dd->CreatePS(ScCore->primaryMainWindow()->doc, options); if (options.prnEngine == PostScript1 || options.prnEngine == PostScript2) { if (ScCore->haveGS()) { QString tmp; QStringList opts; opts.append( QString("-dDEVICEWIDTHPOINTS=%1").arg(tmp.setNum(ScCore->primaryMainWindow()->doc->pageWidth())) ); opts.append( QString("-dDEVICEHEIGHTPOINTS=%1").arg(tmp.setNum(ScCore->primaryMainWindow()->doc->pageHeight())) ); convertPS2PS(fna, fna+".tmp", opts, options.prnEngine); moveFile( fna + ".tmp", fna ); } else { PyErr_SetString(PyExc_SystemError, "Printing failed : GhostScript is needed to print to PostScript Level 1 or Level 2"); Py_RETURN_NONE; } } if (!fil) { if (!printcomm.isEmpty()) cmd = printcomm + " "+fna; else { cmd = "lpr -P" + prn; if (Nr > 1) cmd += " -#" + cc.setNum(Nr); #ifdef HAVE_CUPS // This need yet to be implemented by object Printer // cmd += printer->PrinterOpts; #endif cmd += " "+fna; } system(cmd.toLocal8Bit().constData()); unlink(fna.toLocal8Bit().constData()); } } else { delete dd; PyErr_SetString(PyExc_SystemError, "Printing failed"); return NULL; } delete dd; } // Py_INCREF(Py_None); // return Py_None; Py_RETURN_NONE; }
PyObject *wsgi_convert_headers_to_bytes(PyObject *headers) { PyObject *result = NULL; int i; long size; if (!PyList_Check(headers)) { PyErr_Format(PyExc_TypeError, "expected list object for headers, " "value of type %.200s found", headers->ob_type->tp_name); return 0; } size = PyList_Size(headers); result = PyList_New(size); for (i = 0; i < size; i++) { PyObject *header = NULL; PyObject *header_name = NULL; PyObject *header_value = NULL; PyObject *header_name_as_bytes = NULL; PyObject *header_value_as_bytes = NULL; PyObject *result_tuple = NULL; header = PyList_GetItem(headers, i); if (!PyTuple_Check(header)) { PyErr_Format(PyExc_TypeError, "list of tuple values " "expected for headers, value of type %.200s found", header->ob_type->tp_name); Py_DECREF(result); return 0; } if (PyTuple_Size(header) != 2) { PyErr_Format(PyExc_ValueError, "tuple of length 2 " "expected for header, length is %d", (int)PyTuple_Size(header)); Py_DECREF(result); return 0; } result_tuple = PyTuple_New(2); PyList_SET_ITEM(result, i, result_tuple); header_name = PyTuple_GetItem(header, 0); header_value = PyTuple_GetItem(header, 1); header_name_as_bytes = wsgi_convert_string_to_bytes(header_name); if (!header_name_as_bytes) goto failure; PyTuple_SET_ITEM(result_tuple, 0, header_name_as_bytes); if (!wsgi_validate_header_name(header_name_as_bytes)) goto failure; header_value_as_bytes = wsgi_convert_string_to_bytes(header_value); if (!header_value_as_bytes) goto failure; PyTuple_SET_ITEM(result_tuple, 1, header_value_as_bytes); if (!wsgi_validate_header_value(header_value_as_bytes)) goto failure; } return result; failure: Py_DECREF(result); return NULL; }
void MainExport initFreeCAD() { // Init phase =========================================================== App::Application::Config()["ExeName"] = "FreeCAD"; App::Application::Config()["ExeVendor"] = "FreeCAD"; App::Application::Config()["AppDataSkipVendor"] = "true"; int argc=1; char** argv; argv = (char**)malloc(sizeof(char*)* (argc+1)); #if defined(FC_OS_WIN32) argv[0] = (char*)malloc(MAX_PATH); strncpy(argv[0],App::Application::Config()["AppHomePath"].c_str(),MAX_PATH); argv[0][MAX_PATH-1] = '\0'; // ensure null termination #elif defined(FC_OS_CYGWIN) HMODULE hModule = GetModuleHandle("FreeCAD.dll"); char szFileName [MAX_PATH]; GetModuleFileName(hModule, szFileName, MAX_PATH-1); argv[0] = (char*)malloc(MAX_PATH); strncpy(argv[0],szFileName,MAX_PATH); argv[0][MAX_PATH-1] = '\0'; // ensure null termination #elif defined(FC_OS_LINUX) || defined(FC_OS_BSD) putenv("LANG=C"); putenv("LC_ALL=C"); // get whole path of the library Dl_info info; int ret = dladdr((void*)initFreeCAD, &info); if ((ret == 0) || (!info.dli_fname)) { free(argv); PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!"); return; } argv[0] = (char*)malloc(PATH_MAX); strncpy(argv[0], info.dli_fname,PATH_MAX); argv[0][PATH_MAX-1] = '\0'; // ensure null termination // this is a workaround to avoid a crash in libuuid.so #elif defined(FC_OS_MACOSX) // The MacOS approach uses the Python sys.path list to find the path // to FreeCAD.so - this should be OS-agnostic, except these two // strings, and the call to access(). const static char libName[] = "/FreeCAD.so"; const static char upDir[] = "/../"; char *buf = NULL; PyObject *pySysPath = PySys_GetObject("path"); if ( PyList_Check(pySysPath) ) { int i; // pySysPath should be a *PyList of strings - iterate through it // backwards since the FreeCAD path was likely appended just before // we were imported. for (i = PyList_Size(pySysPath) - 1; i >= 0 ; --i) { char *basePath; PyObject *pyPath = PyList_GetItem(pySysPath, i); long sz = 0; #if PY_MAJOR_VERSION >= 3 if ( PyUnicode_Check(pyPath) ) { // Python 3 string basePath = PyUnicode_AsUTF8AndSize(pyPath, &sz); } #else if ( PyString_Check(pyPath) ) { // Python 2 string type PyString_AsStringAndSize(pyPath, &basePath, &sz); } else if ( PyUnicode_Check(pyPath) ) { // Python 2 unicode type - explicitly use UTF-8 codec PyObject *fromUnicode = PyUnicode_AsUTF8String(pyPath); PyString_AsStringAndSize(fromUnicode, &basePath, &sz); Py_XDECREF(fromUnicode); } #endif // #if/else PY_MAJOR_VERSION >= 3 else { continue; } if (sz + sizeof(libName) > PATH_MAX) { continue; } // buf gets assigned to argv[0], which is free'd at the end buf = (char *)malloc(sz + sizeof(libName)); if (buf == NULL) { break; } strcpy(buf, basePath); // append libName to buf strcat(buf, libName); if (access(buf, R_OK | X_OK) == 0) { // The FreeCAD "home" path is one level up from // libName, so replace libName with upDir. strcpy(buf + sz, upDir); buf[sz + sizeof(upDir)] = '\0'; break; } } // end for (i = PyList_Size(pySysPath) - 1; i >= 0 ; --i) { } // end if ( PyList_Check(pySysPath) ) { if (buf == NULL) { PyErr_SetString(PyExc_ImportError, "Cannot get path of the FreeCAD module!"); return; } argv[0] = buf; #else # error "Implement: Retrieve the path of the module for your platform." #endif argv[argc] = 0; try { // Inits the Application App::Application::init(argc,argv); } catch (const Base::Exception& e) { std::string appName = App::Application::Config()["ExeName"]; std::stringstream msg; msg << "While initializing " << appName << " the following exception occurred: '" << e.what() << "'\n\n"; msg << "\nPlease contact the application's support team for more information.\n\n"; printf("Initialization of %s failed:\n%s", appName.c_str(), msg.str().c_str()); } free(argv[0]); free(argv); return; } //InitFreeCAD....
void wsgi_publish_event(const char *name, PyObject *event) { int i; PyObject *module = NULL; PyObject *list = NULL; module = PyImport_ImportModule("mod_wsgi"); if (module) { PyObject *dict = NULL; dict = PyModule_GetDict(module); list = PyDict_GetItemString(dict, "event_callbacks"); Py_INCREF(list); } else { Py_BEGIN_ALLOW_THREADS ap_log_error(APLOG_MARK, APLOG_ERR, 0, wsgi_server, "mod_wsgi (pid=%d): Unable to import mod_wsgi when " "publishing events.", getpid()); Py_END_ALLOW_THREADS PyErr_Clear(); return; } Py_XDECREF(module); if (!list) { Py_BEGIN_ALLOW_THREADS ap_log_error(APLOG_MARK, APLOG_ERR, 0, wsgi_server, "mod_wsgi (pid=%d): Unable to find event subscribers.", getpid()); Py_END_ALLOW_THREADS PyErr_Clear(); return; } for (i=0; i<PyList_Size(list); i++) { PyObject *callback = NULL; PyObject *res = NULL; PyObject *args = NULL; callback = PyList_GetItem(list, i); Py_INCREF(callback); args = Py_BuildValue("(s)", name); res = PyObject_Call(callback, args, event); if (!res) { PyObject *m = NULL; PyObject *result = NULL; PyObject *type = NULL; PyObject *value = NULL; PyObject *traceback = NULL; Py_BEGIN_ALLOW_THREADS ap_log_error(APLOG_MARK, APLOG_ERR, 0, wsgi_server, "mod_wsgi (pid=%d): Exception occurred within " "event callback.", getpid()); Py_END_ALLOW_THREADS PyErr_Fetch(&type, &value, &traceback); PyErr_NormalizeException(&type, &value, &traceback); if (!value) { value = Py_None; Py_INCREF(value); } if (!traceback) { traceback = Py_None; Py_INCREF(traceback); } m = PyImport_ImportModule("traceback"); if (m) { PyObject *d = NULL; PyObject *o = NULL; d = PyModule_GetDict(m); o = PyDict_GetItemString(d, "print_exception"); if (o) { PyObject *log = NULL; PyObject *args = NULL; Py_INCREF(o); log = newLogObject(NULL, APLOG_ERR, NULL, 0); args = Py_BuildValue("(OOOOO)", type, value, traceback, Py_None, log); result = PyEval_CallObject(o, args); Py_DECREF(args); Py_DECREF(log); Py_DECREF(o); } } if (!result) { /* * If can't output exception and traceback then * use PyErr_Print to dump out details of the * exception. For SystemExit though if we do * that the process will actually be terminated * so can only clear the exception information * and keep going. */ PyErr_Restore(type, value, traceback); if (!PyErr_ExceptionMatches(PyExc_SystemExit)) { PyErr_Print(); PyErr_Clear(); } else { PyErr_Clear(); } } else { Py_XDECREF(type); Py_XDECREF(value); Py_XDECREF(traceback); } Py_XDECREF(result); Py_XDECREF(m); } else if (PyDict_Check(res)) { PyDict_Update(event, res); } Py_XDECREF(res); Py_DECREF(callback); Py_DECREF(args); } Py_DECREF(list); }
static PyObject *pytracer_execute(PyObject *self, PyObject *args) { PyObject *ret; int exit_status; /* Reads arguments */ const char *binary, *databasepath; char **argv; size_t argv_len; int verbosity; PyObject *py_binary, *py_argv, *py_databasepath; if(!(PyArg_ParseTuple(args, "OO!Oi", &py_binary, &PyList_Type, &py_argv, &py_databasepath, &verbosity))) return NULL; if(verbosity < 0) { PyErr_SetString(Err_Base, "verbosity should be >= 0"); return NULL; } trace_verbosity = verbosity; binary = get_string(py_binary); if(binary == NULL) return NULL; databasepath = get_string(py_databasepath); if(databasepath == NULL) return NULL; /* Converts argv from Python list to char[][] */ { size_t i; int bad = 0; argv_len = PyList_Size(py_argv); argv = malloc((argv_len + 1) * sizeof(char*)); for(i = 0; i < argv_len; ++i) { PyObject *arg = PyList_GetItem(py_argv, i); char *str = get_string(arg); if(str == NULL) break; argv[i] = str; } if(bad) { size_t j; for(j = 0; j < i; ++j) free(argv[j]); free(argv); return NULL; } argv[argv_len] = NULL; } if(fork_and_trace(binary, argv_len, argv, databasepath, &exit_status) == 0) { ret = PyLong_FromLong(exit_status); } else { PyErr_SetString(Err_Base, "Error occurred"); ret = NULL; } /* Deallocs argv */ { size_t i; for(i = 0; i < argv_len; ++i) free(argv[i]); free(argv); } return ret; }
static PyObject * _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io) { Py_ssize_t i; PyObject *binary; PyObject *v; Py_ssize_t npath; size_t taillen; PyObject *syspath; PyObject *path; const char* tail; PyObject *filebytes; const char* filepath; Py_ssize_t len; PyObject* result; _Py_IDENTIFIER(open); filebytes = PyUnicode_EncodeFSDefault(filename); if (filebytes == NULL) { PyErr_Clear(); return NULL; } filepath = PyBytes_AS_STRING(filebytes); /* Search tail of filename in sys.path before giving up */ tail = strrchr(filepath, SEP); if (tail == NULL) tail = filepath; else tail++; taillen = strlen(tail); syspath = PySys_GetObject("path"); if (syspath == NULL || !PyList_Check(syspath)) goto error; npath = PyList_Size(syspath); for (i = 0; i < npath; i++) { v = PyList_GetItem(syspath, i); if (v == NULL) { PyErr_Clear(); break; } if (!PyUnicode_Check(v)) continue; path = PyUnicode_EncodeFSDefault(v); if (path == NULL) { PyErr_Clear(); continue; } len = PyBytes_GET_SIZE(path); if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) { Py_DECREF(path); continue; /* Too long */ } strcpy(namebuf, PyBytes_AS_STRING(path)); Py_DECREF(path); if (strlen(namebuf) != len) continue; /* v contains '\0' */ if (len > 0 && namebuf[len-1] != SEP) namebuf[len++] = SEP; strcpy(namebuf+len, tail); binary = _PyObject_CallMethodId(io, &PyId_open, "ss", namebuf, "rb"); if (binary != NULL) { result = binary; goto finally; } PyErr_Clear(); } goto error; error: result = NULL; finally: Py_DECREF(filebytes); return result; }
static PyObject *parseString(PyObject *self, PyObject *args) { char *cp; int i, len, list_size; TidyDoc tdoc; TidyOption option = TidyUnknownOption; PyObject *res = NULL, *arglist = NULL; PyObject *key_list = NULL, *item = NULL, *value = NULL; TidyBuffer output = {0}; TidyBuffer errbuf = {0}; if (!PyArg_ParseTuple(args, "s#|O", &cp, &len, &arglist)) return NULL; if (arglist && !PyDict_Check(arglist)) { PyErr_SetString(PyExc_TypeError, "Second argument must be a dictionary!"); return NULL; } tdoc = tidyCreate(); tidySetErrorBuffer(tdoc, &errbuf); if (!arglist) goto im_so_lazy; /* no args provided */ key_list = PyDict_Keys(arglist); list_size = PyList_Size(key_list); for (i = 0; i < list_size; i++) { item = PyList_GetItem(key_list, i); value = PyDict_GetItem(arglist, item); Py_INCREF(item); Py_INCREF(value); option = tidyGetOptionByName(tdoc, PyString_AsString(item)); if (option == TidyUnknownOption) { PyErr_Format(PyExc_KeyError, "Unknown tidy option '%s'", PyString_AsString(item)); TDOC_RETURN(); } switch (tidyOptGetType(option)) { case TidyString: PY_TO_TIDY(String_Check, Value, String_AsString, "a String"); break; case TidyInteger: PY_TO_TIDY(Int_Check, Int, Int_AsLong, "an Integer"); break; case TidyBoolean: PY_TO_TIDY(Int_Check, Bool, Int_AsLong, "a Boolean or an Integer"); break; default: { PyErr_Format(PyExc_RuntimeError, "Something strange happened, there is no option type %d", tidyOptGetType(option)); TDOC_RETURN(); } } Py_DECREF(item); Py_DECREF(value); } im_so_lazy: tidyParseString(tdoc, cp); tidyCleanAndRepair(tdoc); tidySaveBuffer(tdoc, &output); res = Py_BuildValue("s#", output.bp, output.size); tidyBufFree(&output); tidyBufFree(&errbuf); tidyRelease(tdoc); return res; }