static PyObject * ExclusionFilter_names_to_ccn(PyObject *py_obj_Names) { PyObject *py_iterator = NULL, *py_item = NULL; PyObject *py_exclude, *py_o; struct ccn_charbuf *exclude, *name; int r; // Build exclusion list - This uses explicit exclusion rather than // Bloom filters as Bloom will be deprecated // IMPORTANT: Exclusion component list must be sorted following // "Canonical CCNx ordering" // http://www.ccnx.org/releases/latest/doc/technical/CanonicalOrder.html // in which shortest components go first. // This sorting is expected to be handled on the Python side, not here. assert(py_obj_Names); py_exclude = CCNObject_New_charbuf(EXCLUSION_FILTER, &exclude); JUMP_IF_NULL(py_exclude, error); if (py_obj_Names == Py_None) return py_exclude; r = ccn_charbuf_append_tt(exclude, CCN_DTAG_Exclude, CCN_DTAG); JUMP_IF_NEG_MEM(r, error); // This code is similar to what's used in Name; // could probably be generalized. py_iterator = PyObject_GetIter(py_obj_Names); JUMP_IF_NULL(py_iterator, error); while ((py_item = PyIter_Next(py_iterator))) { int type; if (!PyObject_IsInstance(py_item, g_type_Name)) { PyErr_SetString(PyExc_ValueError, "Expected Name element"); goto error; } py_o = PyObject_GetAttrString(py_item, "type"); JUMP_IF_NULL(py_o, error); type = PyLong_AsLong(py_o); Py_DECREF(py_o); JUMP_IF_ERR(error); if (type == 0) { py_o = PyObject_GetAttrString(py_item, "ccn_data"); JUMP_IF_NULL(py_o, error); if (!CCNObject_IsValid(NAME, py_o)) { Py_DECREF(py_o); PyErr_SetString(PyExc_TypeError, "Expected CCN Name"); goto error; } name = CCNObject_Get(NAME, py_o); /* append without CCN name tag */ assert(name->length >= 4); r = ccn_charbuf_append(exclude, name->buf + 1, name->length - 2); Py_DECREF(py_o); JUMP_IF_NEG_MEM(r, error); } else if (type == 1) { r = ccn_charbuf_append_tt(exclude, CCN_DTAG_Any, CCN_DTAG); JUMP_IF_NEG_MEM(r, error); r = ccn_charbuf_append_closer(exclude); JUMP_IF_NEG_MEM(r, error); } else { PyErr_SetString(PyExc_ValueError, "Unhandled Name type"); goto error; } Py_CLEAR(py_item); } Py_CLEAR(py_iterator); r = ccn_charbuf_append_closer(exclude); /* </Exclude> */ JUMP_IF_NEG_MEM(r, error); return py_exclude; error: Py_XDECREF(py_item); Py_XDECREF(py_iterator); Py_XDECREF(py_exclude); return NULL; }
static int GREENLET_NOINLINE(g_initialstub)(void* mark) { int err; PyObject *o, *run; PyObject *exc, *val, *tb; PyObject *run_info; PyGreenlet* self = ts_target; PyObject* args = ts_passaround_args; PyObject* kwargs = ts_passaround_kwargs; /* save exception in case getattr clears it */ PyErr_Fetch(&exc, &val, &tb); /* self.run is the object to call in the new greenlet */ run = PyObject_GetAttrString((PyObject*) self, "run"); if (run == NULL) { Py_XDECREF(exc); Py_XDECREF(val); Py_XDECREF(tb); return -1; } /* restore saved exception */ PyErr_Restore(exc, val, tb); /* recheck the state in case getattr caused thread switches */ if (!STATE_OK) { Py_DECREF(run); return -1; } /* recheck run_info in case greenlet reparented anywhere above */ run_info = green_statedict(self); if (run_info == NULL || run_info != ts_current->run_info) { Py_DECREF(run); PyErr_SetString(PyExc_GreenletError, run_info ? "cannot switch to a different thread" : "cannot switch to a garbage collected greenlet"); return -1; } /* by the time we got here another start could happen elsewhere, * that means it should now be a regular switch */ if (PyGreenlet_STARTED(self)) { Py_DECREF(run); ts_passaround_args = args; ts_passaround_kwargs = kwargs; return 1; } /* start the greenlet */ self->stack_start = NULL; self->stack_stop = (char*) mark; if (ts_current->stack_start == NULL) { /* ts_current is dying */ self->stack_prev = ts_current->stack_prev; } else { self->stack_prev = ts_current; } self->top_frame = NULL; self->exc_type = NULL; self->exc_value = NULL; self->exc_traceback = NULL; self->recursion_depth = PyThreadState_GET()->recursion_depth; /* restore arguments in case they are clobbered */ ts_target = self; ts_passaround_args = args; ts_passaround_kwargs = kwargs; /* perform the initial switch */ err = g_switchstack(); /* returns twice! The 1st time with err=1: we are in the new greenlet The 2nd time with err=0: back in the caller's greenlet */ if (err == 1) { /* in the new greenlet */ PyGreenlet* origin; #if GREENLET_USE_TRACING PyObject* tracefunc; #endif PyObject* result; PyGreenlet* parent; self->stack_start = (char*) 1; /* running */ /* grab origin while we still can */ origin = ts_origin; ts_origin = NULL; /* now use run_info to store the statedict */ o = self->run_info; self->run_info = green_statedict(self->parent); Py_INCREF(self->run_info); Py_XDECREF(o); #if GREENLET_USE_TRACING if ((tracefunc = PyDict_GetItem(self->run_info, ts_tracekey)) != NULL) { Py_INCREF(tracefunc); if (g_calltrace(tracefunc, args ? ts_event_switch : ts_event_throw, origin, self) < 0) { /* Turn trace errors into switch throws */ Py_CLEAR(kwargs); Py_CLEAR(args); } Py_DECREF(tracefunc); } #endif Py_DECREF(origin); if (args == NULL) { /* pending exception */ result = NULL; } else { /* call g.run(*args, **kwargs) */ result = PyEval_CallObjectWithKeywords( run, args, kwargs); Py_DECREF(args); Py_XDECREF(kwargs); } Py_DECREF(run); result = g_handle_exit(result); /* jump back to parent */ self->stack_start = NULL; /* dead */ for (parent = self->parent; parent != NULL; parent = parent->parent) { result = g_switch(parent, result, NULL); /* Return here means switch to parent failed, * in which case we throw *current* exception * to the next parent in chain. */ assert(result == NULL); } /* We ran out of parents, cannot continue */ PyErr_WriteUnraisable((PyObject *) self); Py_FatalError("greenlets cannot continue"); } /* back in the parent */ if (err < 0) { /* start failed badly, restore greenlet state */ self->stack_start = NULL; self->stack_stop = NULL; self->stack_prev = NULL; } return err; }
int API_wouldBlock_gevent(void *sock, int fd, int ops, int timeout) { /* Setup gevent and yield to gevent hub */ static int once = 1; static PyObject *sockmodule = NULL; static PyObject *waitread = NULL; static PyObject *waitwrite = NULL; PyObject *resObject = NULL; PyObject *argList; PyObject *kwargList; if (once) { /*FIXME: References for module, class or methods are never released */ sockmodule = PyImport_ImportModule ("gevent.socket"); if (sockmodule == NULL) { PRINTMARK(); return -1; } waitread = PyObject_GetAttrString(sockmodule, "wait_read"); waitwrite = PyObject_GetAttrString(sockmodule, "wait_write"); if (waitread == NULL || waitwrite == NULL) { PRINTMARK(); return -1; } if (!PyFunction_Check(waitread) || !PyFunction_Check(waitwrite)) { PRINTMARK(); return -1; } PRINTMARK(); once = 0; } PRINTMARK(); //FIXME: do this once argList = PyTuple_New(1); PyTuple_SET_ITEM(argList, 0, PyInt_FromLong(fd)); kwargList = PyDict_New(); PyDict_SetItemString(kwargList, "timeout", PyInt_FromLong(timeout)); PRINTMARK(); switch (ops) { case 1: PRINTMARK(); resObject = PyObject_Call (waitread, argList, kwargList); PRINTMARK(); break; case 2: PRINTMARK(); resObject = PyObject_Call (waitwrite, argList, kwargList); PRINTMARK(); break; } Py_DECREF(argList); Py_DECREF(kwargList); if (resObject == NULL) { if (!PyErr_Occurred()) { PyErr_Format(PyExc_RuntimeError, "umemcached: Python exception not set for operation %d", ops); } PRINTMARK(); return 0; } PRINTMARK(); Py_DECREF(resObject); PRINTMARK(); return 1; }
PyObject *py_uwsgi_gevent_request(PyObject * self, PyObject * args) { PyObject *py_wsgi_req = PyTuple_GetItem(args, 0); struct wsgi_request *wsgi_req = (struct wsgi_request *) PyLong_AsLong(py_wsgi_req); PyObject *greenlet_switch = NULL; PyObject *current_greenlet = GET_CURRENT_GREENLET; // another hack to retrieve the current wsgi_req; PyObject_SetAttrString(current_greenlet, "uwsgi_wsgi_req", py_wsgi_req); // if in edge-triggered mode read from socket now !!! if (wsgi_req->socket->edge_trigger) { int status = wsgi_req->socket->proto(wsgi_req); if (status < 0) { goto end; } goto request; } greenlet_switch = PyObject_GetAttrString(current_greenlet, "switch"); for(;;) { int ret = uwsgi.wait_read_hook(wsgi_req->fd, uwsgi.socket_timeout); wsgi_req->switches++; if (ret <= 0) { goto end; } int status = wsgi_req->socket->proto(wsgi_req); if (status < 0) { goto end; } else if (status == 0) { break; } } request: #ifdef UWSGI_ROUTING if (uwsgi_apply_routes(wsgi_req) == UWSGI_ROUTE_BREAK) { goto end; } #endif for(;;) { if (uwsgi.p[wsgi_req->uh->modifier1]->request(wsgi_req) <= UWSGI_OK) { goto end; } wsgi_req->switches++; // switch after each yield GEVENT_SWITCH; } end: if (greenlet_switch) { Py_DECREF(greenlet_switch); } Py_DECREF(current_greenlet); uwsgi_close_request(wsgi_req); free_req_queue; if (uwsgi.workers[uwsgi.mywid].manage_next_request == 0) { int running_cores = 0; int i; for(i=0;i<uwsgi.async;i++) { if (uwsgi.workers[uwsgi.mywid].cores[i].in_request) { running_cores++; } } if (running_cores == 0) { // no need to worry about freeing memory PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi"); if (uwsgi_dict) { PyObject *ae = PyDict_GetItemString(uwsgi_dict, "atexit"); if (ae) { python_call(ae, PyTuple_New(0), 0, NULL); } } } } else { // If we stopped any watcher due to being out of async workers, restart it. int i = 0; struct uwsgi_socket *uwsgi_sock = uwsgi.sockets; for (; uwsgi_sock; uwsgi_sock = uwsgi_sock->next, ++i) { PyObject *py_watcher_active = PyObject_GetAttrString(ugevent.watchers[i], "active"); if (py_watcher_active && PyBool_Check(py_watcher_active) && !PyInt_AsLong(py_watcher_active)) { start_watcher(i, uwsgi_sock); } Py_XDECREF(py_watcher_active); } } Py_INCREF(Py_None); return Py_None; }
void initc(void) { PyObject *m = Py_InitModule3("c", c_funcs, "C repository types module"); Empty_TUPLE = PyTuple_New(0); None_PAIR = PyTuple_Pack(2, Py_None, Py_None); _init_view(m); _init_repository(m); _init_container(m); _init_sequence(m); _init_db(m); _init_cursor(m); _init_env(m); _init_txn(m); _init_lock(m); _init_record(m); _init_store(m); PyExc_DBError = PyErr_NewException("chandlerdb.persistence.c.DBError", NULL, NULL); PyObject_SetAttrString(m, "DBError", PyExc_DBError); MAKE_EXC(m, DBLockDeadlockError, DBError); MAKE_EXC(m, DBLockNotGrantedError, DBError); MAKE_EXC(m, DBAccessError, DBError); MAKE_EXC(m, DBBusyError, DBError); MAKE_EXC(m, DBInvalidArgError, DBError); MAKE_EXC(m, DBNoSpaceError, DBError); MAKE_EXC(m, DBNotFoundError, DBError); MAKE_EXC(m, DBNoSuchFileError, DBError); MAKE_EXC(m, DBPermissionsError, DBError); MAKE_EXC(m, DBVersionMismatchError, DBError); MAKE_EXC(m, DBRunRecoveryError, DBError); PyModule_AddIntConstant(m, "DB_VERSION_MAJOR", DB_VERSION_MAJOR); PyModule_AddIntConstant(m, "DB_VERSION_MINOR", DB_VERSION_MINOR); PyModule_AddIntConstant(m, "DB_VERSION_PATCH", DB_VERSION_PATCH); if (!(m = PyImport_ImportModule("chandlerdb.util.c"))) return; LOAD_FN(m, PyUUID_Check); LOAD_FN(m, PyUUID_Make16); LOAD_FN(m, _hash_bytes); LOAD_OBJ(m, Nil); LOAD_TYPE(m, CtxMgr); LOAD_TYPE(m, SkipList); SkipList_Node = (PyTypeObject *) PyObject_GetAttrString((PyObject *) SkipList, "Node"); Py_DECREF(m); if (!(m = PyImport_ImportModule("chandlerdb.item.c"))) return; LOAD_TYPE(m, CItem); LOAD_TYPE(m, ItemRef); LOAD_FN(m, CItem_getLocalAttributeValue); Py_DECREF(m); if (!(m = PyImport_ImportModule("chandlerdb.schema.c"))) return; LOAD_FN(m, CAttribute_invokeAfterChange); Py_DECREF(m); }
static PyObject * DiscoDB_query(register DiscoDB *self, PyObject *query) { PyObject *clause = NULL, *clauses = NULL, *literal = NULL, *literals = NULL, *iterclauses = NULL, *iterliterals = NULL, *negated = NULL, *pack = NULL, *term = NULL; struct ddb_query_clause *ddb_clauses = NULL; struct ddb_cursor *cursor = NULL; uint32_t i = 0, j = 0; clauses = PyObject_GetAttrString(query, "clauses"); if (clauses == NULL) goto Done; iterclauses = PyObject_GetIter(clauses); if (iterclauses == NULL) goto Done; if ((i = PyObject_Length(clauses)) < 0) goto Done; ddb_clauses = ddb_query_clause_alloc(i); for (i = 0; (clause = PyIter_Next(iterclauses)); i++) { literals = PyObject_GetAttrString(clause, "literals"); if (literals == NULL) goto Done; iterliterals = PyObject_GetIter(literals); if (iterliterals == NULL) goto Done; if ((j = PyObject_Length(literals)) < 0) goto Done; ddb_clauses[i].num_terms = j; ddb_clauses[i].terms = ddb_query_term_alloc(j); for (j = 0; (literal = PyIter_Next(iterliterals)); j++) { negated = PyObject_GetAttrString(literal, "negated"); if (negated == NULL) goto Done; term = PyObject_GetAttrString(literal, "term"); if (term == NULL) goto Done; ddb_clauses[i].terms[j].not = PyObject_IsTrue(negated); pack = Py_BuildValue("(O)", term); if (pack == NULL) goto Done; if (!PyArg_ParseTuple(pack, "s#", &ddb_clauses[i].terms[j].key.data, &ddb_clauses[i].terms[j].key.length)) goto Done; Py_CLEAR(literal); Py_CLEAR(negated); Py_CLEAR(pack); Py_CLEAR(term); } Py_CLEAR(clause); Py_CLEAR(literals); Py_CLEAR(iterliterals); } cursor = ddb_query(self->discodb, ddb_clauses, i); if (cursor == NULL) if (ddb_has_error(self->discodb)) goto Done; Done: Py_CLEAR(clause); Py_CLEAR(clauses); Py_CLEAR(literal); Py_CLEAR(literals); Py_CLEAR(iterclauses); Py_CLEAR(iterliterals); Py_CLEAR(negated); Py_CLEAR(pack); Py_CLEAR(term); ddb_query_clause_dealloc(ddb_clauses, i); if (PyErr_Occurred()) { ddb_cursor_dealloc(cursor); return NULL; } return DiscoDBIter_new(&DiscoDBIterEntryType, self, cursor); }
int RM_cmd(int args, char** argv) { printf("Ross-MacDonald\n"); size_t const NB_ARGS = 12; PyObject *pName, *pModule, *pFunc; PyObject *pArgs, *pValue; if (args < NB_ARGS) { fprintf(stderr, "Not enough arguments\n"); return -1; } else if (strcmp("rm", argv[0]) != 0) { fprintf(stderr, "Incorrect call\n"); return -1; } Py_Initialize(); PyRun_SimpleString("import sys, os"); PyRun_SimpleString("sys.path.append('models')"); pName = PyString_FromString("RM"); pModule = PyImport_Import(pName); Py_DECREF(pName); if (pModule != NULL) { pFunc = PyObject_GetAttrString(pModule, "main"); if (pFunc && PyCallable_Check(pFunc)) { pArgs = PyTuple_New(NB_ARGS - 1); for (int i = 0; i < NB_ARGS; ++i) { pValue = PyFloat_FromDouble(atof(argv[i])); if (!pValue) { Py_DECREF(pArgs); Py_DECREF(pModule); fprintf(stderr, "Error arguments\n"); return -1; } PyTuple_SetItem(pArgs, i-1, pValue); } pValue = PyObject_CallObject(pFunc, pArgs); Py_DECREF(pArgs); if (pValue != NULL) { if (PyInt_AsLong(pValue) != 0) { printf("Call error\n"); } else { printf("Call success\n"); } Py_DECREF(pValue); } else { Py_DECREF(pFunc); Py_DECREF(pModule); PyErr_Print(); fprintf(stderr,"Call failed\n"); return -1; } } else { if (PyErr_Occurred()) PyErr_Print(); fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]); } Py_XDECREF(pFunc); Py_DECREF(pModule); } else { PyErr_Print(); fprintf(stderr, "Failed to load \"%s\"\n", argv[1]); return 1; } Py_Finalize(); return 0; }
//------------------------------------------------------------------------------------- bool MethodDescription::checkArgs(PyObject* args) { if (args == NULL || !PyTuple_Check(args)) { PyErr_Format(PyExc_SystemError, "Method::checkArgs: method[%s] args is not a tuple.\n", getName()); PyErr_PrintEx(0); return false; } int offset = (isExposed() == true && g_componentType == CELLAPP_TYPE) ? 1 : 0; uint8 argsSize = argTypes_.size(); uint8 giveArgsSize = PyTuple_Size(args); if (giveArgsSize != argsSize + offset) { PyErr_Format(PyExc_TypeError, "Method::checkArgs: method[%s] requires exactly %d argument%s; %d given", getName(), argsSize, (argsSize == 1) ? "" : "s", PyTuple_Size(args)); PyErr_PrintEx(0); return false; } // 检查是否是一个exposed方法 if(offset > 0) { PyObject* pyExposed = PyTuple_GetItem(args, 0); if (!PyLong_Check(pyExposed)) { PyObject* pyeid = PyObject_GetAttrString(pyExposed, "id"); if (pyeid == NULL || !PyLong_Check(pyeid)) { Py_XDECREF(pyeid); PyErr_Format( PyExc_TypeError, "Method::checkArgs: method[%s] requires None, an id, or an object with an " "id as its first agument", getName()); PyErr_PrintEx(0); return false; } Py_DECREF(pyeid); } } for(uint8 i=0; i <argsSize; i++) { PyObject* pyArg = PyTuple_GetItem(args, i + offset); if (!argTypes_[i]->isSameType(pyArg)) { PyObject* pExample = argTypes_[i]->parseDefaultStr(""); PyErr_Format(PyExc_TypeError, "Method::checkArgs: method[%s] argument %d: Expected %s, %s found", getName(), i+1, pExample->ob_type->tp_name, pyArg != NULL ? pyArg->ob_type->tp_name : "NULL"); PyErr_PrintEx(0); Py_DECREF(pExample); return false; } } return true; }
PyObject * microprotocols_adapt(PyObject *obj, PyObject *proto, PyObject *alt) { PyObject *adapter, *adapted, *key, *meth; char buffer[256]; /* we don't check for exact type conformance as specified in PEP 246 because the ISQLQuote type is abstract and there is no way to get a quotable object to be its instance */ Dprintf("microprotocols_adapt: trying to adapt %s", Py_TYPE(obj)->tp_name); /* look for an adapter in the registry */ if (!(key = PyTuple_Pack(2, Py_TYPE(obj), proto))) { return NULL; } adapter = PyDict_GetItem(psyco_adapters, key); Py_DECREF(key); if (adapter) { adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL); return adapted; } /* try to have the protocol adapt this object*/ if ((meth = PyObject_GetAttrString(proto, "__adapt__"))) { adapted = PyObject_CallFunctionObjArgs(meth, obj, NULL); Py_DECREF(meth); if (adapted && adapted != Py_None) return adapted; Py_XDECREF(adapted); if (PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_TypeError)) { PyErr_Clear(); } else { return NULL; } } } else { /* proto.__adapt__ not found. */ PyErr_Clear(); } /* then try to have the object adapt itself */ if ((meth = PyObject_GetAttrString(obj, "__conform__"))) { adapted = PyObject_CallFunctionObjArgs(meth, proto, NULL); Py_DECREF(meth); if (adapted && adapted != Py_None) return adapted; Py_XDECREF(adapted); if (PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_TypeError)) { PyErr_Clear(); } else { return NULL; } } } else { /* obj.__conform__ not found. */ PyErr_Clear(); } /* Finally check if a superclass can be adapted and use the same adapter. */ if (!(adapter = _get_superclass_adapter(obj, proto))) { return NULL; } if (Py_None != adapter) { adapted = PyObject_CallFunctionObjArgs(adapter, obj, NULL); return adapted; } /* else set the right exception and return NULL */ PyOS_snprintf(buffer, 255, "can't adapt type '%s'", Py_TYPE(obj)->tp_name); psyco_set_error(ProgrammingError, NULL, buffer); return NULL; }
/* This is the write call back registered within the event loop */ void write_cb(struct ev_loop *loop, struct ev_io *w, int revents) { char response[1024]; int stop=0; //0: not stop, 1: stop, 2: stop and call tp close int ret; //python_handler return struct client *cli= ((struct client*) (((char*)w) - offsetof(struct client,ev_write))); if (cli->response_iter_sent==-2) { //we must send an header or an error ret=python_handler(cli); //look for python callback and execute it if (ret==0) //look for python callback and execute it { //uri not found snprintf(response, sizeof(response), "HTTP/1.0 500 Not found\r\nContent-Type: text/html\r\nServer: %s\r\n\r\n<html><head><title>Page not found</title></head><body><p>Page not found!!!</p></body></html>", VERSION ); if (response) write_cli(cli,response, strlen(response), revents); else printf("problem with error 500: Page not found\n"); stop=1; } else if (ret==-411) { snprintf(response, sizeof(response), "HTTP/1.0 411 Length Required\r\nContent-Type: text/html\r\nServer: %s\r\n\r\n<html><head><title>Length Required</head><body><p>Length Required!!!</p></body></html>", VERSION); if (response) write_cli(cli,response, strlen(response), revents); else printf("problem with error 411\n"); stop=1; } else if (ret==-500) { snprintf(response, sizeof(response), "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/html\r\nServer: %s\r\n\r\n<html><head><title>Internal Server Error</title></head><body><p>Internal Server Error!!!</p></body></html>", VERSION); if (response) write_cli(cli,response, strlen(response), revents); else printf("problem with error 500: Internal server error\n"); stop=1; } else if (ret==-501) { //problem to parse the request snprintf(response,sizeof(response), "HTTP/1.0 501 Not Implemented\r\nContent-Type: text/html\r\nServer: %s\r\n\r\n<html><head><title>Not Implemented</head><body><p>Not Implemented!!!</p></body></html>", VERSION); if (response) write_cli(cli,response, strlen(response), revents); else printf("problem with error 501\n"); stop=1; } else { //uri found, we thus send the html header write_cli(cli, cli->response_header, cli->response_header_length, revents); cli->response_iter_sent++; //-1: header sent } } else if (strcmp(cli->cmd,"HEAD")==0) { //we don't send additonal data for a HEAD command stop=2; } else { //we let the python developer to manage other HTTP command if (((PyList_Check(cli->response_content))||(PyTuple_Check(cli->response_content))) && (cli->response_content_obj==NULL)) //we treat list object { int tuple = PyTuple_Check(cli->response_content); cli->response_iter_sent++; if (cli->response_iter_sent<(tuple ? PyTuple_Size(cli->response_content) : PyList_Size(cli->response_content))) { PyObject *pydummy = tuple ? PyTuple_GetItem(cli->response_content, cli->response_iter_sent) : PyList_GetItem(cli->response_content, cli->response_iter_sent); char *buff; #if (PY_VERSION_HEX < 0x02050000) int buflen; if (PyObject_AsReadBuffer(pydummy, (const void **) &buff, &buflen)==0) #else Py_ssize_t buflen; if (PyObject_AsReadBuffer(pydummy, (const void **) &buff, &buflen)==0) #endif { // if this is a readable buffer, we send it. Other else, we ignore it. if (write_cli(cli, buff, buflen, revents)==0) { cli->response_iter_sent = tuple ? PyTuple_Size(cli->response_content) : PyList_Size(cli->response_content); //break the for loop } } else { printf("The item %i of your list is not a string!!!! It will be skipped\n",cli->response_iter_sent); } } else // all iterations has been sent { stop=2; } } else if (PyFile_Check(cli->response_content) && (cli->response_content_obj==NULL)) // we treat file object { if (cli->response_iter_sent==-1) // we need to initialise the file descriptor { cli->response_fp=PyFile_AsFile(cli->response_content); } cli->response_iter_sent++; char buff[MAX_BUFF]=""; size_t len=fread(buff, sizeof(char), MAX_BUFF, cli->response_fp); if ((int)len==0) { stop=2; } else { if (write_cli(cli,buff, len, revents)==0) { stop=2; } if ((int)len<MAX_BUFF) { //we have send the whole file stop=2; } } //free(buff); } else if ((cli->response_content_obj!=NULL) && (PyIter_Check(cli->response_content_obj))) { //we treat Iterator object cli->response_iter_sent++; PyObject *pyelem = cli->response_content; if (pyelem == NULL) { stop = 2; } else { char *buff; #if (PY_VERSION_HEX < 0x02050000) int buflen; if (PyObject_AsReadBuffer(pyelem, (const void **) &buff, &buflen)==0) #else Py_ssize_t buflen; if (PyObject_AsReadBuffer(pyelem, (const void **) &buff, &buflen)==0) #endif { // if this is a readable buffer, we send it. Other else, we ignore it. if (write_cli(cli, buff, buflen, revents)==0) { stop=2; //break the iterator loop } } else { printf("The item %i of your iterator is not a string!!!! It will be skipped\n",cli->response_iter_sent); } Py_DECREF(pyelem); cli->response_content = PyIter_Next(cli->response_content_obj); if (cli->response_content==NULL) { if (debug) { printf("host=%s,port=%i iterator ended uri=%s\n", cli->remote_addr, cli->remote_port, cli->uri ); } stop=2; } } } else { printf("wsgi output of is neither a list, neither a fileobject, neither an iterable object!!!!!\n"); //PyErr_SetString(PyExc_TypeError, "Result must be a list, a fileobject or an iterable object"); stop=1; } }// end of GET OR POST request if (stop==2) { if (cli->response_content!=NULL) { if (PyObject_HasAttrString(cli->response_content, "close")) { PyObject *pydummy=PyObject_GetAttrString(cli->response_content, "close"); PyObject_CallFunction(pydummy, NULL); Py_DECREF(pydummy); } } ev_io_stop(EV_A_ w); close_connection(cli); } if (stop==1) { ev_io_stop(EV_A_ w); close_connection(cli); } }
/* The procedure call the Environ method called "method" and give pydict has parameter */ void update_environ(PyObject *pyenviron, PyObject *pydict, char *method) { PyObject *pyupdate=PyObject_GetAttrString(pyenviron, method); PyObject_CallFunction(pyupdate, "(O)", pydict); Py_DECREF(pyupdate); }
/* This is the main python handler that will transform and treat the client html request. return 1 if we have found a python object to treat the requested uri return 0 if not (page not found) return -1 in case of problem return -2 in case the request command is not implemented */ int python_handler(struct client *cli) { PyObject *pydict, *pydummy; int ret; if (debug) printf("host=%s,port=%i:python_handler:HEADER:\n%s**\n", cli->remote_addr, cli->remote_port, cli->input_header); // 1)initialise environ PyObject *pyenviron_class=PyObject_GetAttrString(py_base_module, "Environ"); if (!pyenviron_class) { printf("load Environ failed from base module"); exit(1); } PyObject *pyenviron=PyObject_CallObject(pyenviron_class, NULL); if (!pyenviron) { printf("Failed to create an instance of Environ"); exit(1); } Py_DECREF(pyenviron_class); // 2)transform headers into a dictionary and send it to environ.update_headers pydict=header_to_dict(cli); if (pydict==Py_None) { Py_DECREF(pyenviron); return -500; } update_environ(pyenviron, pydict, "update_headers"); Py_DECREF(pydict); // 2bis) we check if the request method is supported PyObject *pysupportedhttpcmd = PyObject_GetAttrString(py_base_module, "supported_HTTP_command"); if (cli->cmd==NULL) pydummy=Py_None; else pydummy = PyString_FromString(cli->cmd); if (PySequence_Contains(pysupportedhttpcmd,pydummy)!=1) { //return not implemented Py_DECREF(pysupportedhttpcmd); Py_DECREF(pydummy); Py_DECREF(pyenviron); return -501; } Py_DECREF(pydummy); // 2ter) we treat directly the OPTIONS command if (strcmp(cli->cmd,"OPTIONS")==0) { pydummy=PyString_FromFormat("HTTP/1.0 200 OK\r\nServer: %s\r\nAllow: ", VERSION) ; PyObject *pyitem; int index, max; max = PyList_Size(pysupportedhttpcmd); for (index=0; index<max; index++) { pyitem=PyList_GetItem(pysupportedhttpcmd, index); // no need to decref pyitem PyString_Concat(&pydummy, PyObject_Str(pyitem)); if (index<max-1) PyString_Concat(&pydummy, PyString_FromString(", ")); } PyString_Concat(&pydummy, PyString_FromString("\r\nContent-Length: 0\r\n\r\n")); cli->response_header = PyString_AsString(pydummy); cli->response_header_length=(int)PyString_Size(pydummy); cli->response_content=PyList_New(0); Py_DECREF(pyenviron); return 1; } Py_DECREF(pysupportedhttpcmd); // 3)find if the uri is registered if (handle_uri(cli)!=1) { if (py_generic_cb==NULL) { //printf("uri not found\n"); Py_DECREF(pyenviron); return 0; } else { cli->wsgi_cb=py_generic_cb; Py_INCREF(cli->wsgi_cb); cli->uri_path=(char *)calloc(1, sizeof(char)); strcpy(cli->uri_path,""); } } // 4) build path_info, ... pydict=py_build_method_variables(cli); update_environ(pyenviron, pydict, "update_uri"); Py_DECREF(pydict); // 5) in case of POST, put it into the wsgi.input if (strcmp(cli->cmd,"POST")==0) { ret=manage_header_body(cli, pyenviron); if (ret < 0) { return ret; } } // 6) add some request info pydict=py_get_request_info(cli); update_environ(pyenviron, pydict, "update_from_request"); Py_DECREF(pydict); // 7) build response object PyObject *pystart_response_class=PyObject_GetAttrString(py_base_module, "Start_response"); PyObject *pystart_response=PyInstance_New(pystart_response_class, NULL, NULL); Py_DECREF(pystart_response_class); if (PyErr_Occurred()) { PyErr_Print(); return -500; } // 7b) add the current date to the response object PyObject *py_response_header=PyObject_GetAttrString(pystart_response,"response_headers"); char *sftime; sftime=cur_time_rfc1123(); pydummy = PyString_FromString(sftime); PyDict_SetItemString(py_response_header, "Date", pydummy); Py_DECREF(pydummy); Py_DECREF(py_response_header); free(sftime); pydummy = PyString_FromString(VERSION); PyDict_SetItemString(py_response_header, "Server", pydummy); Py_DECREF(pydummy); // 8) execute python callbacks with his parameters PyObject *pyarglist = Py_BuildValue("(OO)", pyenviron, pystart_response ); cli->response_content = PyEval_CallObject(cli->wsgi_cb,pyarglist); if (cli->response_content!=NULL) { if ((PyFile_Check(cli->response_content)==0) && (PyIter_Check(cli->response_content)==1)) { //This is an Iterator object. We have to execute it first cli->response_content_obj = cli->response_content; cli->response_content = PyIter_Next(cli->response_content_obj); } } Py_DECREF(pyarglist); Py_XDECREF(cli->wsgi_cb); if (cli->response_content!=NULL) { PyObject *pydummy = PyObject_Str(pystart_response); cli->response_header = PyString_AsString(pydummy); cli->response_header_length = (int)PyString_Size(pydummy); Py_DECREF(pydummy); } else //python call return is NULL { printf("Python error!!!\n"); char buff[200]; sprintf(buff, "HTTP/1.0 500 Not found\r\nContent-Type: text/html\r\nServer: %s* \r\n\r\n", VERSION); cli->response_header = buff; if (cli->response_header == NULL) { printf("ERROR!!!! Memory allocation error in the Python error handling procedure\n"); cli->response_header_length=0; goto leave_python_handler; } cli->response_header_length=strlen(cli->response_header); if (PyErr_Occurred()) { //get_traceback();py_b PyObject *pyerrormsg_method=PyObject_GetAttrString(py_base_module,"redirectStdErr"); PyObject *pyerrormsg=PyObject_CallFunction(pyerrormsg_method, NULL); Py_DECREF(pyerrormsg_method); Py_DECREF(pyerrormsg); PyErr_Print(); PyObject *pysys=PyObject_GetAttrString(py_base_module,"sys"); PyObject *pystderr=PyObject_GetAttrString(pysys,"stderr"); Py_DECREF(pysys); PyObject *pygetvalue=PyObject_GetAttrString(pystderr, "getvalue"); Py_DECREF(pystderr); PyObject *pyres=PyObject_CallFunction(pygetvalue, NULL); Py_DECREF(pygetvalue); printf("%s\n", PyString_AsString(pyres)); //test if we must send it to the page PyObject *pysendtraceback = PyObject_GetAttrString(py_config_module,"send_traceback_to_browser"); cli->response_content=PyList_New(0); if (pysendtraceback==Py_True) { pydummy = PyString_FromString("<h1>Error</h1><pre>"); PyList_Append(cli->response_content, pydummy ); Py_DECREF(pydummy); PyList_Append(cli->response_content, pyres); pydummy = PyString_FromString("</pre>"); PyList_Append(cli->response_content, pydummy); Py_DECREF(pydummy); } else { PyObject *pyshortmsg = PyObject_GetAttrString(py_config_module,"send_traceback_short"); PyList_Append(cli->response_content, pyshortmsg); Py_DECREF(pyshortmsg); } Py_DECREF(pyres); Py_DECREF(pysendtraceback); } else { cli->response_content=PyList_New(0); pydummy = PyString_FromString("Page not found."); PyList_Append(cli->response_content, pydummy ); Py_DECREF(pydummy); } } leave_python_handler: Py_XDECREF(pystart_response); Py_XDECREF(pyenviron); return 1; }
/* TODO triple check this to make sure reference counting is correct */ PyObject* chunk_render(PyObject *self, PyObject *args) { RenderState state; int xoff, yoff; PyObject *imgsize, *imgsize0_py, *imgsize1_py; int imgsize0, imgsize1; PyObject *blocks_py; PyObject *left_blocks_py; PyObject *right_blocks_py; PyObject *up_left_blocks_py; PyObject *up_right_blocks_py; RenderModeInterface *rendermode; void *rm_data; PyObject *t = NULL; if (!PyArg_ParseTuple(args, "OOiiO", &state.self, &state.img, &xoff, &yoff, &state.blockdata_expanded)) return NULL; /* fill in important modules */ state.textures = textures; state.chunk = chunk_mod; /* set up the render mode */ rendermode = get_render_mode(&state); rm_data = calloc(1, rendermode->data_size); if (rendermode->start(rm_data, &state)) { free(rm_data); return Py_BuildValue("i", "-1"); } /* get the image size */ imgsize = PyObject_GetAttrString(state.img, "size"); imgsize0_py = PySequence_GetItem(imgsize, 0); imgsize1_py = PySequence_GetItem(imgsize, 1); Py_DECREF(imgsize); imgsize0 = PyInt_AsLong(imgsize0_py); imgsize1 = PyInt_AsLong(imgsize1_py); Py_DECREF(imgsize0_py); Py_DECREF(imgsize1_py); /* get the block data directly from numpy: */ blocks_py = PyObject_GetAttrString(state.self, "blocks"); state.blocks = blocks_py; left_blocks_py = PyObject_GetAttrString(state.self, "left_blocks"); state.left_blocks = left_blocks_py; right_blocks_py = PyObject_GetAttrString(state.self, "right_blocks"); state.right_blocks = right_blocks_py; up_left_blocks_py = PyObject_GetAttrString(state.self, "up_left_blocks"); state.up_left_blocks = up_left_blocks_py; up_right_blocks_py = PyObject_GetAttrString(state.self, "up_right_blocks"); state.up_right_blocks = up_right_blocks_py; for (state.x = 15; state.x > -1; state.x--) { for (state.y = 0; state.y < 16; state.y++) { PyObject *blockid = NULL; /* set up the render coordinates */ state.imgx = xoff + state.x*12 + state.y*12; /* 128*12 -- offset for z direction, 15*6 -- offset for x */ state.imgy = yoff - state.x*6 + state.y*6 + 128*12 + 15*6; for (state.z = 0; state.z < 128; state.z++) { state.imgy -= 12; /* get blockid */ state.block = getArrayByte3D(blocks_py, state.x, state.y, state.z); if (state.block == 0) { continue; } /* make sure we're rendering inside the image boundaries */ if ((state.imgx >= imgsize0 + 24) || (state.imgx <= -24)) { continue; } if ((state.imgy >= imgsize1 + 24) || (state.imgy <= -24)) { continue; } /* decref'd on replacement *and* at the end of the z for block */ if (blockid) { Py_DECREF(blockid); } blockid = PyInt_FromLong(state.block); // check for occlusion if (rendermode->occluded(rm_data, &state)) { continue; } // everything stored here will be a borrowed ref /* get the texture and mask from block type / ancil. data */ if (!PySequence_Contains(special_blocks, blockid)) { /* t = textures.blockmap[blockid] */ t = PyList_GetItem(blockmap, state.block); } else { PyObject *tmp; unsigned char ancilData = getArrayByte3D(state.blockdata_expanded, state.x, state.y, state.z); if ((state.block == 85) || (state.block == 9) || (state.block == 55) || (state.block == 54) || (state.block == 2) || (state.block == 90)) { ancilData = generate_pseudo_data(&state, ancilData); } tmp = PyTuple_New(2); Py_INCREF(blockid); /* because SetItem steals */ PyTuple_SetItem(tmp, 0, blockid); PyTuple_SetItem(tmp, 1, PyInt_FromLong(ancilData)); /* this is a borrowed reference. no need to decref */ t = PyDict_GetItem(specialblockmap, tmp); Py_DECREF(tmp); } /* if we found a proper texture, render it! */ if (t != NULL && t != Py_None) { PyObject *src, *mask, *mask_light; src = PyTuple_GetItem(t, 0); mask = PyTuple_GetItem(t, 1); mask_light = PyTuple_GetItem(t, 2); if (mask == Py_None) mask = src; rendermode->draw(rm_data, &state, src, mask, mask_light); } } if (blockid) { Py_DECREF(blockid); blockid = NULL; } } } /* free up the rendermode info */ rendermode->finish(rm_data, &state); free(rm_data); Py_DECREF(blocks_py); Py_XDECREF(left_blocks_py); Py_XDECREF(right_blocks_py); Py_XDECREF(up_left_blocks_py); Py_XDECREF(up_right_blocks_py); return Py_BuildValue("i",2); }
PyObject * PyFile_GetLine(PyObject *f, int n) { PyObject *result; if (f == NULL) { PyErr_BadInternalCall(); return NULL; } { PyObject *reader; PyObject *args; reader = PyObject_GetAttrString(f, "readline"); if (reader == NULL) return NULL; if (n <= 0) args = PyTuple_New(0); else args = Py_BuildValue("(i)", n); if (args == NULL) { Py_DECREF(reader); return NULL; } result = PyEval_CallObject(reader, args); Py_DECREF(reader); Py_DECREF(args); if (result != NULL && !PyBytes_Check(result) && !PyUnicode_Check(result)) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_TypeError, "object.readline() returned non-string"); } } if (n < 0 && result != NULL && PyBytes_Check(result)) { char *s = PyBytes_AS_STRING(result); Py_ssize_t len = PyBytes_GET_SIZE(result); if (len == 0) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } else if (s[len-1] == '\n') { if (result->ob_refcnt == 1) _PyBytes_Resize(&result, len-1); else { PyObject *v; v = PyBytes_FromStringAndSize(s, len-1); Py_DECREF(result); result = v; } } } if (n < 0 && result != NULL && PyUnicode_Check(result)) { Py_UNICODE *s = PyUnicode_AS_UNICODE(result); Py_ssize_t len = PyUnicode_GET_SIZE(result); if (len == 0) { Py_DECREF(result); result = NULL; PyErr_SetString(PyExc_EOFError, "EOF when reading a line"); } else if (s[len-1] == '\n') { if (result->ob_refcnt == 1) PyUnicode_Resize(&result, len-1); else { PyObject *v; v = PyUnicode_FromUnicode(s, len-1); Py_DECREF(result); result = v; } } } return result; }
/* * Initialize an IterParser object * * The Python arguments are: * * *fd*: A Python file object or a callable object * *buffersize*: The size of the read buffer */ static int IterParser_init(IterParser *self, PyObject *args, PyObject *kwds) { PyObject* fd = NULL; PyObject* read = NULL; ssize_t buffersize = 1 << 14; static char *kwlist[] = {"fd", "buffersize", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|n:IterParser.__init__", kwlist, &fd, &buffersize)) { return -1; } /* Keep the buffersize within a reasonable range */ self->buffersize = CLAMP(buffersize, (ssize_t)(1 << 10), (ssize_t)(1 << 24)); #ifdef __clang__ /* Clang can't handle the file descriptors Python gives us, so in that case, we just call the object's read method. */ read = PyObject_GetAttrString(fd, "read"); if (read != NULL) { fd = read; } #else self->file = PyObject_AsFileDescriptor(fd); if (self->file != -1) { /* This is a real C file handle or descriptor. We therefore need to allocate our own read buffer, and get the real C object. */ self->buffer = malloc((size_t)self->buffersize); if (self->buffer == NULL) { PyErr_SetString(PyExc_MemoryError, "Out of memory"); goto fail; } self->fd = fd; Py_INCREF(self->fd); lseek(self->file, 0, SEEK_SET); } else #endif if (PyCallable_Check(fd)) { /* fd is a Python callable */ self->fd = fd; Py_INCREF(self->fd); self->read = fd; Py_INCREF(self->read); } else { PyErr_SetString( PyExc_TypeError, "Arg 1 to iterparser must be a file object or callable object"); goto fail; } PyErr_Clear(); self->queue_read_idx = 0; self->queue_write_idx = 0; self->done = 0; self->text = malloc((size_t)buffersize * sizeof(XML_Char)); self->text_alloc = buffersize; if (self->text == NULL) { PyErr_SetString(PyExc_MemoryError, "Out of memory"); goto fail; } text_clear(self); self->read_args = Py_BuildValue("(n)", buffersize); if (self->read_args == NULL) { goto fail; } self->dict_singleton = PyDict_New(); if (self->dict_singleton == NULL) { goto fail; } self->td_singleton = PyUnicode_FromString("TD"); if (self->td_singleton == NULL) { goto fail; } if (queue_realloc(self, buffersize)) { goto fail; } /* Set up an expat parser with our callbacks */ self->parser = XML_ParserCreate(NULL); if (self->parser == NULL) { PyErr_SetString(PyExc_MemoryError, "Out of memory"); goto fail; } XML_SetUserData(self->parser, self); XML_SetElementHandler( self->parser, (XML_StartElementHandler)startElement, (XML_EndElementHandler)endElement); XML_SetCharacterDataHandler( self->parser, (XML_CharacterDataHandler)characterData); XML_SetXmlDeclHandler( self->parser, (XML_XmlDeclHandler)xmlDecl); Py_XDECREF(read); return 0; fail: Py_XDECREF(read); Py_XDECREF(self->fd); Py_XDECREF(self->read); free(self->text); Py_XDECREF(self->dict_singleton); Py_XDECREF(self->td_singleton); Py_XDECREF(self->read_args); free(self->queue); return -1; }
/************************************************************************* * method: initpympi * This is called right after python has been initialized. MPI has already * been initialized here * ************************************************************************/ void initpympi() { PyObject* mpiName = 0; char versionString[32]; PyObject* lastWish = 0; int version; int subversion; PyObject* pickleModule = 0; PyObject* pickleDict = 0; PyObject* docString = 0; PyObject* pyWorld = 0; PyObject* member = 0; PyMethodDef* methodPtr = 0; char* docExtra = 0; int myRank = 0; int result = MPI_Comm_rank(MPI_COMM_WORLD, &myRank ); /* ----------------------------------------------- */ /* The IBM poe environment is brain dead */ /* ----------------------------------------------- */ #ifdef _AIX Py_InteractiveFlag++; #endif /* ----------------------------------------------- */ /* Cover our butts on assumptions */ /* ----------------------------------------------- */ Assert( sizeof(MPI_Comm) <= sizeof(long) ); /* ----------------------------------------------- */ /* We subvert the input stream to handle broadcast */ /* ----------------------------------------------- */ Original_ReadlineFunctionPointer = PyOS_ReadlineFunctionPointer; if ( !Original_ReadlineFunctionPointer ) { Original_ReadlineFunctionPointer = PyOS_StdioReadline; } PyOS_ReadlineFunctionPointer = MPI_ReadlineFunctionPointer; /* ----------------------------------------------- */ /* Setup the initial mpi module */ /* ----------------------------------------------- */ module = Py_InitModule("mpi",MPI_methods); Assert(module); PyMPI_dictionary = PyModule_GetDict(module); Assert(PyMPI_dictionary); /* ----------------------------------------------- */ /* Set up a docstring for the mpi module itself */ /* ----------------------------------------------- */ docExtra = DocStringFromMethods(MPI_methods,"mpi\n\nBasic mpi calls\n\n"); Assert(docExtra); docString = PyString_FromString(docExtra); free(docExtra); /* ----------------------------------------------- */ /* We start off with errors handled with flag */ /* ----------------------------------------------- */ if ( MPI_Errhandler_set(MPI_COMM_WORLD,MPI_ERRORS_RETURN) != MPI_SUCCESS ) { PYCHECK( PyErr_SetString(PyExc_SystemError,"MPI Failure -- MPI_Errhandler_set()") ); } /* ----------------------------------------------- */ /* See if we conform! */ /* ----------------------------------------------- */ MPICHECKCOMMLESS( MPI_Get_version(&version,&subversion) ); Assert(version == MPI_VERSION && subversion == MPI_SUBVERSION); /* ----------------------------------------------- */ /* We have some cleanup work to do on exit */ /* ----------------------------------------------- */ PYCHECK( lastWish = PyCFunction_New(&lastwishMethods,module) ); Assert(lastWish); sysExitfunc = PySys_GetObject("exitfunc"); PyErr_Clear(); PYCHECK( PySys_SetObject("exitfunc",lastWish) ); /* ----------------------------------------------- */ /* Set common attributes */ /* ----------------------------------------------- */ PYCHECK( PyDict_SetItemString(PyMPI_dictionary,"stdout",Py_None) ); Py_INCREF(Py_None); PYCHECK( PyString_ConcatFromString(&docString,"name: Name of MPI model (MPICH, LAM, mpi)") ); #ifdef MPICH_NAME PYCHECK( mpiName = PyString_FromString("MPICH") ); #else # ifdef LAM_MPI PYCHECK( mpiName = PyString_FromString("LAM") ); # else PYCHECK( mpiName = PyString_FromString("mpi") ); # endif #endif PYCHECK( PyDict_SetItemString(PyMPI_dictionary,"name",mpiName) ); PYCHECK( PyString_ConcatFromString(&docString,"rank: Rank of MPI_COMM_WORLD communicator\n") ); MPICHECK( MPI_COMM_WORLD, MPI_Comm_rank(MPI_COMM_WORLD,&worldRank)); PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"rank", PyInt_FromLong((long)worldRank))); PYCHECK(PyString_ConcatFromString(&docString,"procs: Size of MPI_COMM_WORLD communicator\n")); MPICHECK( MPI_COMM_WORLD, MPI_Comm_size(MPI_COMM_WORLD,&worldProcs)); PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"procs", PyInt_FromLong((long)worldProcs))); PYCHECK(PyString_ConcatFromString(&docString,"tick: Tick size of high-resolution timer\n")); PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"tick", PyFloat_FromDouble(MPI_Wtick()))); PYCHECK(PyString_ConcatFromString(&docString,"version: String showing mpi version\n")); sprintf(versionString,"%d.%d",MPI_VERSION,MPI_SUBVERSION); #if defined(MPICH_NAME) && MPI_VERSION == 1 && MPI_SUBVERSION == 1 && MPI_STATUS_SIZE == 5 strcat(versionString,".2"); /* MPICH 1.1.2 is evil */ #endif PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"version", PyString_FromString(versionString))); PYCHECK(PyString_ConcatFromString(&docString,"COMM_WORLD: MPI_COMM_WORLD communicator\n")); PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"COMM_WORLD", PyMPI_Comm(MPI_COMM_WORLD))); PYCHECK(PyString_ConcatFromString(&docString,"COMM_NULL: MPI_COMM_NULL communicator (non-functional)\n")); PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"COMM_NULL", PyMPI_Comm(MPI_COMM_NULL))); PYCHECK(PyString_ConcatFromString(&docString,"MAX: MPI_MAX\n")); /*PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"MAX", PyInt_FromLong((long)MPI_MAX)));*/ reduceOpLookup[eMaxOp] = MPI_MAX; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"MAX", PyInt_FromLong((long)eMaxOp))); PYCHECK(PyString_ConcatFromString(&docString,"MIN: MPI_MIN\n")); /*PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"MIN", PyInt_FromLong((long)MPI_MIN)));*/ reduceOpLookup[eMinOp] = MPI_MIN; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"MIN", PyInt_FromLong((long)eMinOp))); PYCHECK( PyString_ConcatFromString(&docString,"SUM: MPI_SUM\n")); /*PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"SUM", PyInt_FromLong((long)MPI_SUM)));*/ reduceOpLookup[eSumOp] = MPI_SUM; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"SUM", PyInt_FromLong((long)eSumOp))); PYCHECK( PyString_ConcatFromString(&docString,"PROD: MPI_PROD\n")); /*PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"PROD", PyInt_FromLong((long)MPI_PROD)));*/ reduceOpLookup[eProdOp] = MPI_PROD; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"PROD", PyInt_FromLong((long)eProdOp))); PYCHECK( PyString_ConcatFromString(&docString,"LAND: MPI_LAND\n")); /*PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"LAND", PyInt_FromLong((long)MPI_LAND)));*/ reduceOpLookup[eLandOp] = MPI_LAND; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"LAND", PyInt_FromLong((long)eLandOp))); PYCHECK( PyString_ConcatFromString(&docString,"BAND: MPI_BAND\n")); /*PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"BAND", PyInt_FromLong((long)MPI_BAND)));*/ reduceOpLookup[eBandOp] = MPI_BAND; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"BAND", PyInt_FromLong((long)eBandOp))); PYCHECK( PyString_ConcatFromString(&docString,"LOR: MPI_LOR\n")); /*PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"LOR", PyInt_FromLong((long)MPI_LOR)));*/ reduceOpLookup[eLorOp] = MPI_LOR; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"LOR", PyInt_FromLong((long)eLorOp))); PYCHECK( PyString_ConcatFromString(&docString,"BOR: MPI_BOR\n")); /* PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"BOR", PyInt_FromLong((long)MPI_BOR)));*/ reduceOpLookup[eBorOp] = MPI_BOR; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"BOR", PyInt_FromLong((long)eBorOp))); PYCHECK( PyString_ConcatFromString(&docString,"LXOR: MPI_LXOR\n")); /* PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"LXOR", PyInt_FromLong((long)MPI_LXOR)));*/ reduceOpLookup[eLxorOp] = MPI_LXOR; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"LXOR", PyInt_FromLong((long)eLxorOp))); PYCHECK( PyString_ConcatFromString(&docString,"BXOR: MPI_BXOR\n")); /* PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"BXOR", PyInt_FromLong((long)MPI_BXOR)));*/ reduceOpLookup[eBxorOp] = MPI_BXOR; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"BXOR", PyInt_FromLong((long)eBxorOp))); PYCHECK( PyString_ConcatFromString(&docString,"MINLOC: MPI_MINLOC\n")); /* PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"MINLOC", PyInt_FromLong((long)MPI_MINLOC)));*/ reduceOpLookup[eMinlocOp] = MPI_MINLOC; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"MINLOC", PyInt_FromLong((long)eMinlocOp))); PYCHECK( PyString_ConcatFromString(&docString,"MAXLOC: MPI_MAXLOC\n")); /* PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"MAXLOC", PyInt_FromLong((long)MPI_MAXLOC)));*/ reduceOpLookup[eMaxlocOp] = MPI_MAXLOC; PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"MAXLOC", PyInt_FromLong((long)eMaxlocOp))); PYCHECK( PyString_ConcatFromString(&docString,"ANY_SOURCE: MPI_ANY_SOURCE (used for untargeted recv)\n")); PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"ANY_SOURCE", PyInt_FromLong((long)MPI_ANY_SOURCE))); PYCHECK( PyString_ConcatFromString(&docString,"ANY_TAG: MPI_ANY_TAG (used for untargeted recv)\n")); PYCHECK(PyDict_SetItemString(PyMPI_dictionary,"ANY_TAG", PyInt_FromLong((long)MPI_ANY_TAG))); /* ----------------------------------------------- */ /* We set up an internal communicator for PYTHON */ /* messaging and another for Python I/O */ /* ----------------------------------------------- */ MPICHECK( MPI_COMM_WORLD, MPI_Comm_dup(MPI_COMM_WORLD,&PyMPI_COMM_WORLD)); MPICHECK( MPI_COMM_WORLD, MPI_Comm_dup(MPI_COMM_WORLD,&PyMPI_COMM_INPUT)); PYCHECK( PyString_ConcatFromString(&docString,"WORLD: Internal Python communicator\n") ); PYCHECK( pyWorld = PyMPI_Comm(PyMPI_COMM_WORLD) ); PYCHECK( PyDict_SetItemString(PyMPI_dictionary,"WORLD",pyWorld) ); /* ----------------------------------------------- */ /* Fetch member functions to appear as mpi.xxxx */ /* Make pyWorld immortal to avoid dealloc issues */ /* Magic Rating: 10/10 */ /* ----------------------------------------------- */ Py_INCREF(pyWorld); for(methodPtr = PyMPIMethods_Comm; methodPtr->ml_name; ++methodPtr) { PYCHECK( member = PyObject_GetAttrString(pyWorld,methodPtr->ml_name) ); Py_INCREF(member); PYCHECK( PyDict_SetItemString(PyMPI_dictionary,methodPtr->ml_name,member) ); } /* ----------------------------------------------- */ /* Set up the overloaded input */ /* ----------------------------------------------- */ PYCHECK( overloadedInput = PyMPI_File(worldRank != 0,PySys_GetObject("stdin"),PyMPI_COMM_INPUT) ); Py_INCREF(overloadedInput); PyDict_SetItemString(PyMPI_dictionary,"stdin",overloadedInput); PySys_SetObject("stdin",overloadedInput); /* ----------------------------------------------- */ /* Initial model is no output throttle */ /* ----------------------------------------------- */ PYCHECK( PyMPI_UnrestrictedOutput(3) ); /* ----------------------------------------------- */ /* Have to set up some stuff for communicating */ /* arbitrary python objects */ /* ----------------------------------------------- */ /* -- set up PyMPI_pythonPickleType -- */ MPICHECKCOMMLESS( MPI_Type_contiguous(1,MPI_CHAR,&PyMPI_pythonPickleType) ); MPICHECKCOMMLESS( MPI_Type_commit(&PyMPI_pythonPickleType) ); Assert( PyMPI_pythonPickleType != MPI_CHAR ); /* -- set up PyMPI_pythonFuncPickleType -- */ MPICHECKCOMMLESS( MPI_Type_contiguous(1,MPI_CHAR,&PyMPI_pythonFuncPickleType) ); MPICHECKCOMMLESS( MPI_Type_commit(&PyMPI_pythonFuncPickleType) ); Assert( PyMPI_pythonFuncPickleType != MPI_CHAR ); /* -- set up PyMPI_pythonFuncPickleType -- */ pickleModule = PyImport_ImportModule("cPickle"); if ( !pickleModule || PyErr_Occurred() ) { PyErr_Clear(); } else { PYCHECK( pickleDict = PyModule_GetDict(pickleModule) ); PYCHECK( PyMPI_pickleDumperFunction = PyDict_GetItemString(pickleDict,"dumps") ); PYCHECK( PyMPI_pickleLoaderFunction = PyDict_GetItemString(pickleDict,"loads") ); } /* ----------------------------------------------- */ /* Set up the __doc__ string of the communicator */ /* type with more info (based on list) */ /* ----------------------------------------------- */ PyMPIObject_Communicator_Type.tp_doc = DocStringFromMethods(PyMPIMethods_Comm, PyMPIObject_Communicator_Type.tp_doc); /* ----------------------------------------------- */ /* Set up same info in module doc */ /* ----------------------------------------------- */ docExtra = DocStringFromMethods(PyMPIMethods_Comm, "\nAnd these communicator methods map to the Python world communicator (not MPI_COMM_WORLD)\n\n"); Assert(docExtra); PYCHECK( PyString_ConcatFromString(&docString,docExtra) ); free(docExtra); /* ----------------------------------------------- */ /* Stick in the doc string and we're ready to go */ /* ----------------------------------------------- */ PYCHECK( PyDict_SetItemString(PyMPI_dictionary, "__doc__", docString) ); return; pythonError: Assert( PyErr_Occurred() ); return; /* We have set a Python exception, let Python handle it */ }
/* initialise stuff extension classes */ void py_sugarext_register_classes(PyObject *d) { PyObject *module; if ((module = PyImport_ImportModule("gobject")) != NULL) { _PyGObject_Type = (PyTypeObject *)PyObject_GetAttrString(module, "GObject"); if (_PyGObject_Type == NULL) { PyErr_SetString(PyExc_ImportError, "cannot import name GObject from gobject"); return ; } } else { PyErr_SetString(PyExc_ImportError, "could not import gobject"); return ; } if ((module = PyImport_ImportModule("gtk")) != NULL) { _PyGtkWidget_Type = (PyTypeObject *)PyObject_GetAttrString(module, "Widget"); if (_PyGtkWidget_Type == NULL) { PyErr_SetString(PyExc_ImportError, "cannot import name Widget from gtk"); return ; } _PyGtkEntry_Type = (PyTypeObject *)PyObject_GetAttrString(module, "Entry"); if (_PyGtkEntry_Type == NULL) { PyErr_SetString(PyExc_ImportError, "cannot import name Entry from gtk"); return ; } _PyGtkMenu_Type = (PyTypeObject *)PyObject_GetAttrString(module, "Menu"); if (_PyGtkMenu_Type == NULL) { PyErr_SetString(PyExc_ImportError, "cannot import name Menu from gtk"); return ; } _PyGtkContainer_Type = (PyTypeObject *)PyObject_GetAttrString(module, "Container"); if (_PyGtkContainer_Type == NULL) { PyErr_SetString(PyExc_ImportError, "cannot import name Container from gtk"); return ; } _PyGtkImage_Type = (PyTypeObject *)PyObject_GetAttrString(module, "Image"); if (_PyGtkImage_Type == NULL) { PyErr_SetString(PyExc_ImportError, "cannot import name Image from gtk"); return ; } } else { PyErr_SetString(PyExc_ImportError, "could not import gtk"); return ; } if ((module = PyImport_ImportModule("gtk.gdk")) != NULL) { _PyGdkWindow_Type = (PyTypeObject *)PyObject_GetAttrString(module, "Window"); if (_PyGdkWindow_Type == NULL) { PyErr_SetString(PyExc_ImportError, "cannot import name Window from gtk.gdk"); return ; } } else { PyErr_SetString(PyExc_ImportError, "could not import gtk.gdk"); return ; } #line 1063 "_sugarext.c" pygobject_register_class(d, "SugarKeyGrabber", SUGAR_TYPE_KEY_GRABBER, &PySugarKeyGrabber_Type, Py_BuildValue("(O)", &PyGObject_Type)); pyg_set_object_has_new_constructor(SUGAR_TYPE_KEY_GRABBER); pygobject_register_class(d, "SugarMenu", SUGAR_TYPE_MENU, &PySugarMenu_Type, Py_BuildValue("(O)", &PyGtkMenu_Type)); pygobject_register_class(d, "SugarGrid", SUGAR_TYPE_GRID, &PySugarGrid_Type, Py_BuildValue("(O)", &PyGObject_Type)); pyg_set_object_has_new_constructor(SUGAR_TYPE_GRID); pygobject_register_class(d, "EggSMClient", EGG_TYPE_SM_CLIENT, &PyEggSMClient_Type, Py_BuildValue("(O)", &PyGObject_Type)); pyg_set_object_has_new_constructor(EGG_TYPE_SM_CLIENT); pygobject_register_class(d, "EggSMClientXSMP", EGG_TYPE_SM_CLIENT_XSMP, &PyEggSMClientXSMP_Type, Py_BuildValue("(O)", &PyEggSMClient_Type)); pyg_set_object_has_new_constructor(EGG_TYPE_SM_CLIENT_XSMP); pygobject_register_class(d, "GsmSession", GSM_TYPE_SESSION, &PyGsmSession_Type, Py_BuildValue("(O)", &PyGObject_Type)); pyg_set_object_has_new_constructor(GSM_TYPE_SESSION); pygobject_register_class(d, "AcmeVolume", ACME_TYPE_VOLUME, &PyAcmeVolume_Type, Py_BuildValue("(O)", &PyGObject_Type)); pyg_set_object_has_new_constructor(ACME_TYPE_VOLUME); pygobject_register_class(d, "AcmeVolumeAlsa", ACME_TYPE_VOLUME_ALSA, &PyAcmeVolumeAlsa_Type, Py_BuildValue("(O)", &PyAcmeVolume_Type)); pyg_set_object_has_new_constructor(ACME_TYPE_VOLUME_ALSA); }
int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs) { static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL}; PyObject* database; int detect_types = 0; PyObject* isolation_level = NULL; PyObject* factory = NULL; int check_same_thread = 1; int cached_statements = 100; double timeout = 5.0; int rc; PyObject* class_attr = NULL; PyObject* class_attr_str = NULL; int is_apsw_connection = 0; PyObject* database_utf8; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist, &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements)) { return -1; } self->initialized = 1; self->begin_statement = NULL; self->statement_cache = NULL; self->statements = NULL; self->cursors = NULL; Py_INCREF(Py_None); self->row_factory = Py_None; Py_INCREF(&PyUnicode_Type); self->text_factory = (PyObject*)&PyUnicode_Type; if (PyString_Check(database) || PyUnicode_Check(database)) { if (PyString_Check(database)) { database_utf8 = database; Py_INCREF(database_utf8); } else { database_utf8 = PyUnicode_AsUTF8String(database); if (!database_utf8) { return -1; } } Py_BEGIN_ALLOW_THREADS rc = sqlite3_open(PyString_AsString(database_utf8), &self->db); Py_END_ALLOW_THREADS Py_DECREF(database_utf8); if (rc != SQLITE_OK) { _pysqlite_seterror(self->db, NULL); return -1; } } else { /* Create a pysqlite connection from a APSW connection */ class_attr = PyObject_GetAttrString(database, "__class__"); if (class_attr) { class_attr_str = PyObject_Str(class_attr); if (class_attr_str) { if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) { /* In the APSW Connection object, the first entry after * PyObject_HEAD is the sqlite3* we want to get hold of. * Luckily, this is the same layout as we have in our * pysqlite_Connection */ self->db = ((pysqlite_Connection*)database)->db; Py_INCREF(database); self->apsw_connection = database; is_apsw_connection = 1; } } } Py_XDECREF(class_attr_str); Py_XDECREF(class_attr); if (!is_apsw_connection) { PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object"); return -1; } } if (!isolation_level) { isolation_level = PyString_FromString(""); if (!isolation_level) { return -1; } } else { Py_INCREF(isolation_level); } self->isolation_level = NULL; if (pysqlite_connection_set_isolation_level(self, isolation_level) < 0) { Py_DECREF(isolation_level); return -1; } Py_DECREF(isolation_level); self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements); if (PyErr_Occurred()) { return -1; } self->created_statements = 0; self->created_cursors = 0; /* Create lists of weak references to statements/cursors */ self->statements = PyList_New(0); self->cursors = PyList_New(0); if (!self->statements || !self->cursors) { return -1; } /* By default, the Cache class INCREFs the factory in its initializer, and * decrefs it in its deallocator method. Since this would create a circular * reference here, we're breaking it by decrementing self, and telling the * cache class to not decref the factory (self) in its deallocator. */ self->statement_cache->decref_factory = 0; Py_DECREF(self); self->inTransaction = 0; self->detect_types = detect_types; self->timeout = timeout; (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000)); #ifdef WITH_THREAD self->thread_ident = PyThread_get_thread_ident(); #endif self->check_same_thread = check_same_thread; self->function_pinboard = PyDict_New(); if (!self->function_pinboard) { return -1; } self->collations = PyDict_New(); if (!self->collations) { return -1; } self->Warning = pysqlite_Warning; self->Error = pysqlite_Error; self->InterfaceError = pysqlite_InterfaceError; self->DatabaseError = pysqlite_DatabaseError; self->DataError = pysqlite_DataError; self->OperationalError = pysqlite_OperationalError; self->IntegrityError = pysqlite_IntegrityError; self->InternalError = pysqlite_InternalError; self->ProgrammingError = pysqlite_ProgrammingError; self->NotSupportedError = pysqlite_NotSupportedError; return 0; }
NPY_NO_EXPORT int PyArray_DTypeFromObjectHelper(PyObject *obj, int maxdims, PyArray_Descr **out_dtype, int string_type) { int i, size; PyArray_Descr *dtype = NULL; PyObject *ip; #if PY_VERSION_HEX >= 0x02060000 Py_buffer buffer_view; #endif /* Check if it's an ndarray */ if (PyArray_Check(obj)) { dtype = PyArray_DESCR((PyArrayObject *)obj); Py_INCREF(dtype); goto promote_types; } /* Check if it's a NumPy scalar */ if (PyArray_IsScalar(obj, Generic)) { if (!string_type) { dtype = PyArray_DescrFromScalar(obj); if (dtype == NULL) { goto fail; } } else { int itemsize; PyObject *temp; if (string_type == NPY_STRING) { if ((temp = PyObject_Str(obj)) == NULL) { return -1; } #if defined(NPY_PY3K) #if PY_VERSION_HEX >= 0x03030000 itemsize = PyUnicode_GetLength(temp); #else itemsize = PyUnicode_GET_SIZE(temp); #endif #else itemsize = PyString_GET_SIZE(temp); #endif } else if (string_type == NPY_UNICODE) { #if defined(NPY_PY3K) if ((temp = PyObject_Str(obj)) == NULL) { #else if ((temp = PyObject_Unicode(obj)) == NULL) { #endif return -1; } itemsize = PyUnicode_GET_DATA_SIZE(temp); #ifndef Py_UNICODE_WIDE itemsize <<= 1; #endif } else { goto fail; } Py_DECREF(temp); if (*out_dtype != NULL && (*out_dtype)->type_num == string_type && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(string_type); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; } goto promote_types; } /* Check if it's a Python scalar */ dtype = _array_find_python_scalar_type(obj); if (dtype != NULL) { if (string_type) { int itemsize; PyObject *temp; if (string_type == NPY_STRING) { if ((temp = PyObject_Str(obj)) == NULL) { return -1; } #if defined(NPY_PY3K) #if PY_VERSION_HEX >= 0x03030000 itemsize = PyUnicode_GetLength(temp); #else itemsize = PyUnicode_GET_SIZE(temp); #endif #else itemsize = PyString_GET_SIZE(temp); #endif } else if (string_type == NPY_UNICODE) { #if defined(NPY_PY3K) if ((temp = PyObject_Str(obj)) == NULL) { #else if ((temp = PyObject_Unicode(obj)) == NULL) { #endif return -1; } itemsize = PyUnicode_GET_DATA_SIZE(temp); #ifndef Py_UNICODE_WIDE itemsize <<= 1; #endif } else { goto fail; } Py_DECREF(temp); if (*out_dtype != NULL && (*out_dtype)->type_num == string_type && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(string_type); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; } goto promote_types; } /* Check if it's an ASCII string */ if (PyBytes_Check(obj)) { int itemsize = PyString_GET_SIZE(obj); /* If it's already a big enough string, don't bother type promoting */ if (*out_dtype != NULL && (*out_dtype)->type_num == NPY_STRING && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(NPY_STRING); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; goto promote_types; } /* Check if it's a Unicode string */ if (PyUnicode_Check(obj)) { int itemsize = PyUnicode_GET_DATA_SIZE(obj); #ifndef Py_UNICODE_WIDE itemsize <<= 1; #endif /* * If it's already a big enough unicode object, * don't bother type promoting */ if (*out_dtype != NULL && (*out_dtype)->type_num == NPY_UNICODE && (*out_dtype)->elsize >= itemsize) { return 0; } dtype = PyArray_DescrNewFromType(NPY_UNICODE); if (dtype == NULL) { goto fail; } dtype->elsize = itemsize; goto promote_types; } #if PY_VERSION_HEX >= 0x02060000 /* PEP 3118 buffer interface */ memset(&buffer_view, 0, sizeof(Py_buffer)); if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_FORMAT|PyBUF_STRIDES) == 0 || PyObject_GetBuffer(obj, &buffer_view, PyBUF_FORMAT) == 0) { PyErr_Clear(); dtype = _descriptor_from_pep3118_format(buffer_view.format); PyBuffer_Release(&buffer_view); if (dtype) { goto promote_types; } } else if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_STRIDES) == 0 || PyObject_GetBuffer(obj, &buffer_view, PyBUF_SIMPLE) == 0) { PyErr_Clear(); dtype = PyArray_DescrNewFromType(NPY_VOID); dtype->elsize = buffer_view.itemsize; PyBuffer_Release(&buffer_view); goto promote_types; } else { PyErr_Clear(); } #endif /* The array interface */ ip = PyObject_GetAttrString(obj, "__array_interface__"); if (ip != NULL) { if (PyDict_Check(ip)) { PyObject *typestr; #if defined(NPY_PY3K) PyObject *tmp = NULL; #endif typestr = PyDict_GetItemString(ip, "typestr"); #if defined(NPY_PY3K) /* Allow unicode type strings */ if (PyUnicode_Check(typestr)) { tmp = PyUnicode_AsASCIIString(typestr); typestr = tmp; } #endif if (typestr && PyBytes_Check(typestr)) { dtype =_array_typedescr_fromstr(PyBytes_AS_STRING(typestr)); #if defined(NPY_PY3K) if (tmp == typestr) { Py_DECREF(tmp); } #endif Py_DECREF(ip); if (dtype == NULL) { goto fail; } goto promote_types; } } Py_DECREF(ip); } else { PyErr_Clear(); } /* The array struct interface */ ip = PyObject_GetAttrString(obj, "__array_struct__"); if (ip != NULL) { PyArrayInterface *inter; char buf[40]; if (NpyCapsule_Check(ip)) { inter = (PyArrayInterface *)NpyCapsule_AsVoidPtr(ip); if (inter->two == 2) { PyOS_snprintf(buf, sizeof(buf), "|%c%d", inter->typekind, inter->itemsize); dtype = _array_typedescr_fromstr(buf); Py_DECREF(ip); if (dtype == NULL) { goto fail; } goto promote_types; } } Py_DECREF(ip); } else { PyErr_Clear(); } /* The old buffer interface */ #if !defined(NPY_PY3K) if (PyBuffer_Check(obj)) { dtype = PyArray_DescrNewFromType(NPY_VOID); if (dtype == NULL) { goto fail; } dtype->elsize = Py_TYPE(obj)->tp_as_sequence->sq_length(obj); PyErr_Clear(); goto promote_types; } #endif /* The __array__ attribute */ if (PyObject_HasAttrString(obj, "__array__")) { ip = PyObject_CallMethod(obj, "__array__", NULL); if(ip && PyArray_Check(ip)) { dtype = PyArray_DESCR((PyArrayObject *)ip); Py_INCREF(dtype); Py_DECREF(ip); goto promote_types; } Py_XDECREF(ip); if (PyErr_Occurred()) { goto fail; } } /* Not exactly sure what this is about... */ #if !defined(NPY_PY3K) if (PyInstance_Check(obj)) { dtype = _use_default_type(obj); if (dtype == NULL) { goto fail; } else { goto promote_types; } } #endif /* * If we reached the maximum recursion depth without hitting one * of the above cases, the output dtype should be OBJECT */ if (maxdims == 0 || !PySequence_Check(obj)) { if (*out_dtype == NULL || (*out_dtype)->type_num != NPY_OBJECT) { Py_XDECREF(*out_dtype); *out_dtype = PyArray_DescrFromType(NPY_OBJECT); if (*out_dtype == NULL) { return -1; } } return 0; } /* Recursive case */ size = PySequence_Size(obj); if (size < 0) { goto fail; } /* Recursive call for each sequence item */ for (i = 0; i < size; ++i) { int res; ip = PySequence_GetItem(obj, i); if (ip == NULL) { goto fail; } res = PyArray_DTypeFromObjectHelper(ip, maxdims - 1, out_dtype, string_type); if (res < 0) { Py_DECREF(ip); goto fail; } else if (res > 0) { Py_DECREF(ip); return res; } Py_DECREF(ip); } return 0; promote_types: /* Set 'out_dtype' if it's NULL */ if (*out_dtype == NULL) { if (!string_type && dtype->type_num == NPY_STRING) { Py_DECREF(dtype); return RETRY_WITH_STRING; } if (!string_type && dtype->type_num == NPY_UNICODE) { Py_DECREF(dtype); return RETRY_WITH_UNICODE; } *out_dtype = dtype; return 0; } /* Do type promotion with 'out_dtype' */ else { PyArray_Descr *res_dtype = PyArray_PromoteTypes(dtype, *out_dtype); Py_DECREF(dtype); if (res_dtype == NULL) { return -1; } if (!string_type && res_dtype->type_num == NPY_UNICODE && (*out_dtype)->type_num != NPY_UNICODE) { Py_DECREF(res_dtype); return RETRY_WITH_UNICODE; } if (!string_type && res_dtype->type_num == NPY_STRING && (*out_dtype)->type_num != NPY_STRING) { Py_DECREF(res_dtype); return RETRY_WITH_STRING; } Py_DECREF(*out_dtype); *out_dtype = res_dtype; return 0; } fail: Py_XDECREF(*out_dtype); *out_dtype = NULL; return -1; } #undef RETRY_WITH_STRING #undef RETRY_WITH_UNICODE /* new reference */ NPY_NO_EXPORT PyArray_Descr * _array_typedescr_fromstr(char *c_str) { PyArray_Descr *descr = NULL; PyObject *stringobj = PyString_FromString(c_str); if (stringobj == NULL) { return NULL; } if (PyArray_DescrConverter(stringobj, &descr) != NPY_SUCCEED) { Py_DECREF(stringobj); return NULL; } Py_DECREF(stringobj); return descr; } NPY_NO_EXPORT int check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis) { /* Check that index is valid, taking into account negative indices */ if ((*index < -max_item) || (*index >= max_item)) { /* Try to be as clear as possible about what went wrong. */ if (axis >= 0) { PyErr_Format(PyExc_IndexError, "index %"NPY_INTP_FMT" is out of bounds " "for axis %d with size %"NPY_INTP_FMT, *index, axis, max_item); } else { PyErr_Format(PyExc_IndexError, "index %"NPY_INTP_FMT" is out of bounds " "for size %"NPY_INTP_FMT, *index, max_item); } return -1; } /* adjust negative indices */ if (*index < 0) { *index += max_item; } return 0; } NPY_NO_EXPORT char * index2ptr(PyArrayObject *mp, npy_intp i) { npy_intp dim0; if (PyArray_NDIM(mp) == 0) { PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed"); return NULL; } dim0 = PyArray_DIMS(mp)[0]; if (check_and_adjust_index(&i, dim0, 0) < 0) return NULL; if (i == 0) { return PyArray_DATA(mp); } return PyArray_BYTES(mp)+i*PyArray_STRIDES(mp)[0]; }
boost::python::object GCHMCIntegrator::Call( int nosteps_arg, int steps_per_trial_arg, TARGET_TYPE temp_arg, TARGET_TYPE ts, int pyseed, int _massMatNumOpt, // EU int _metroFixmanOpt, // EU double _lj14sf //-- ){ #ifdef DEBUG_TIME //boost::timer boost_timer; #endif int ntrials_arg = 0; if(nosteps_arg%steps_per_trial_arg){ fprintf(stderr, "GCHMCIntegrator::Call(): Incorrect nosteps/steps_per_trial combination: %d/%d\n", nosteps_arg, steps_per_trial_arg); exit(1); } ntrials_arg = nosteps_arg/steps_per_trial_arg; int tx, tshm; boost::python::list booConfList; int i, j; double **retConfsPois = new double* [ntrials_arg]; double *retPotEsPoi = new double[ntrials_arg]; double *accs = new double; *accs = 0.0; vector3 *xCall; PyObject *SrcConfObj = PyObject_CallMethod(this->universe, "copyConfiguration", NULL); // START GET CURR CONF PyObject *CallConfArr = PyObject_GetAttrString(SrcConfObj, "array"); PyArrayObject *CallConfiguration = (PyArrayObject *)CallConfArr; xCall = (vector3 *)CallConfiguration->data; // STOP GET CURR CONF PyObject *ConfObjs[ntrials_arg]; PyObject *ConfArrs[ntrials_arg]; PyArrayObject *Confs[ntrials_arg]; boost::python::object booConfObjs[ntrials_arg]; boost::python::list booPotEs; for(i=0; i<ntrials_arg; i++){ ConfObjs[i] = PyObject_CallMethod(this->universe, "copyConfiguration", NULL); ConfArrs[i] = PyObject_GetAttrString(ConfObjs[i], "array"); Confs[i] = (PyArrayObject *)(ConfArrs[i]); retConfsPois[i] = (double *)(Confs[i])->data; } // Put curr conf from universe into shm for(int a=0; a<sys->mr->natms; a++){ tx = (int)(sys->indexMap[ a ][2]); tshm = ((int)(sys->indexMap[ a ][1]) * 3) + 2; shm[ tshm +0] = xCall[tx][0]; shm[ tshm +1] = xCall[tx][1]; shm[ tshm +2] = xCall[tx][2]; } *sys->pyseed = pyseed; sys->massMatNumOpt = _massMatNumOpt; // EU sys->metroFixmanOpt = _metroFixmanOpt; // EU sys->lj14sf = _lj14sf; //-- sys->sysRetConfsPois = retConfsPois; sys->sysRetPotEsPoi = retPotEsPoi; sys->sysAccs = accs; if(nosteps_arg % ntrials_arg){ fprintf(stderr, "nosteps_arg % ntrials_arg not 0\n"); exit(1); } this->nosteps = nosteps_arg; this->ntrials = ntrials_arg; this->temperature = temp_arg; this->delta_t = ts; ++(this->sweep); int arrays_cut = 2 + 4*3*(sys->mr->natms); shm[arrays_cut + 0] = 0.0; // step (will be incremented in MidVV shm[arrays_cut + 1] = (TARGET_TYPE)(this->nosteps); shm[arrays_cut + 2] = this->temperature; shm[arrays_cut + 3] = delta_t; // picosec shm[arrays_cut + 7] = 1.0; // acceptance always 1 !! shm[arrays_cut + 9] = nosteps/ntrials; // steps_per_trial shm[arrays_cut + 10] = 0.0; // initialize trial // * Get <Universe>'s pyo_evaluator // PyObject *pyo_evaluator; PyObject *cpyo_evaluator; pyo_evaluator = PyObject_CallMethod(this->universe, "energyEvaluator", NULL); cpyo_evaluator = PyObject_CallMethod(pyo_evaluator, "CEvaluator", NULL); this->evaluator = (PyFFEvaluatorObject *)cpyo_evaluator; sys->evaluator = this->evaluator; #ifdef DEBUG_TIME //std::cout<<"Time simmain nosteps"<<this->nosteps<<" time "<<boost_timer.elapsed()<<std::endl; #endif sys->Advance(this->nosteps); #ifdef DEBUG_TIME //std::cout<<"Time simmain nosteps"<<this->nosteps<<" time "<<boost_timer.elapsed()<<std::endl; #endif xCall = (vector3 *)configuration->data; for(int a=0; a<sys->mr->natms; a++){ tx = (int)(sys->indexMap[ a ][2]); tshm = ((int)(sys->indexMap[ a ][1]) * 3) + 2; xCall[tx][0] = shm[ tshm +0]; xCall[tx][1] = shm[ tshm +1]; xCall[tx][2] = shm[ tshm +2]; } for(i=0; i<ntrials_arg; i++){ booConfObjs[i] = boost::python::object(boost::python::handle<>( boost::python::borrowed((PyObject *)(ConfObjs[i])) )); booConfList.append(booConfObjs[i]); booPotEs.append(retPotEsPoi[i]); // potential energies } double ntrials = (shm[arrays_cut + 1]) / (shm[arrays_cut + 9]); double booAccPerTrials = *accs / ntrials; //float(acc)/float(ntrials) boost::python::list booRetList; booRetList.append(booConfList); booRetList.append(booPotEs); //booRetList.append(booAccPerTrials); // RESTORE booRetList.append(int(*accs)); booRetList.append(int(ntrials)); booRetList.append(ts); // delta_t delete accs; return booRetList; // (xs, energies, acc, ntrials, delta_t) }
bool InteractionClassifier::initialize(const std::string& model, const std::vector<std::string>& interactions) { // Update list of interactions // TODO: Load from model this->interactions = interactions; // Step 1 topviewkinect::util::log_println("Initializing Python..."); Py_Initialize(); init_numpy(); PyRun_SimpleString("import sys"); std::ostringstream path_ss; path_ss << "sys.path.append(\"" << topviewkinect::EAGLESENSE_DIRECTORY << "\")"; PyRun_SimpleString(path_ss.str().c_str()); // Step 2 topviewkinect::util::log_println("Initializing classifier..."); PyObject* p_module = PyImport_ImportModule("classifier"); if (p_module == NULL) { PyErr_Print(); topviewkinect::util::log_println("Failed to load the classifier module."); return false; } PyObject* p_classifier_class = PyObject_GetAttrString(p_module, "TopviewInteractionClassifier"); if (p_classifier_class == NULL) { PyErr_Print(); topviewkinect::util::log_println("Failed to load the classifier class."); return false; } Py_DECREF(p_module); this->p_classifier = PyObject_CallObject(p_classifier_class, NULL); if (this->p_classifier == NULL) { PyErr_Print(); topviewkinect::util::log_println("Failed to create a classifier object."); return false; } Py_DECREF(p_classifier_class); // Step 3 topviewkinect::util::log_println("Loading model..."); std::string path_to_model = topviewkinect::get_model_filepath(model); PyObject* p_load_args = Py_BuildValue("(s)", path_to_model.c_str()); PyObject* p_load = PyObject_GetAttrString(this->p_classifier, "load"); PyObject* p_result = PyObject_CallObject(p_load, p_load_args); if (p_result == NULL) { PyErr_Print(); topviewkinect::util::log_println("Failed to call the classifier `load` function."); return false; } Py_DECREF(p_load_args); Py_DECREF(p_load); Py_DECREF(p_result); // Step 4 topviewkinect::util::log_println("Loading 'predict' function..."); this->p_predict_func = PyObject_GetAttrString(this->p_classifier, "predict"); if (!PyCallable_Check(this->p_predict_func)) { PyErr_Print(); topviewkinect::util::log_println("Failed to load the classifier `predict` function."); return false; } return true; }
// * GCHMCIntegrator Constructor * // GCHMCIntegrator::GCHMCIntegrator(PyObject *universe, std::string ligdir, std::string gaff_fn) { std::cout<<"GCHMCIntegrator instance created 20160826"<<std::endl; import_array(); temperature = 300.0; delta_t = 0.0015; trouble = 0; this->universe = universe; PyObject *pyo_universe_spec; pyo_universe_spec = PyObject_GetAttrString(universe, "_spec"); universe_spec = (PyUniverseSpecObject *)pyo_universe_spec; PyObject *objectList; objectList = PyObject_CallMethod(universe, "objectList", NULL); PyObject *object0 = PyList_GetItem(objectList, 0); // * Get confarr * // PyObject *confobj; PyObject *confarr; confobj = PyObject_CallMethod(universe, "configuration", NULL); confarr = PyObject_GetAttrString(confobj, "array"); configuration = (PyArrayObject *)confarr; vector3 *x; x = (vector3 *)configuration->data; p_energy_po = new energy_data; #if defined(NUMPY) gradarr = (PyArrayObject *)PyArray_Copy(configuration); #else gradarr = (PyArrayObject *)PyArray_FromDims(configuration->nd, configuration->dimensions, PyArray_DOUBLE); #endif vector3 *f; f = (vector3 *)gradarr->data; for(int i=0; i<configuration->dimensions[0]; i++){ f[i][0] = f[i][1] = f[i][2] = .0; } p_energy_po->gradients = (PyObject *)gradarr; p_energy_po->gradient_fn = NULL; p_energy_po->force_constants = NULL; p_energy_po->fc_fn = NULL; int natoms = configuration->dimensions[0]; int order[natoms+2]; int acceptance; PyObject *prmtop_order_obj = PyObject_GetAttrString(object0, "prmtop_order"); for(int i=0; i<PyList_Size(prmtop_order_obj); i++){ order[i] = (int)PyInt_AS_LONG( PyObject_GetAttrString(PyList_GetItem((prmtop_order_obj), i), "number") ); } PyArrayObject *prmtop_order = (PyArrayObject *)prmtop_order_obj; order[natoms] = 1; order[natoms+1] = 1945; acceptance = order[natoms]; int argc = 6; ligdir += '/'; //gaffdir += '/'; //string gaff_fn = gaffdir + "gaff.dat"; /* Options are: IC: Internal Coordinates: fully flexible TD: Torsional Dynamics: only torsions are flexible RR: Rigid Rings: torsional dynamics with rigid rings */ const char *argv[6] = { "-ligdir", ligdir.c_str(), "-gaff", gaff_fn.c_str(), "-ictd", "TD" }; //+++++++ Simbody PART ++++++++++++ bArgParser parser(argc, argv); parser.Print(); TARGET_TYPE **indexMap = NULL; TARGET_TYPE *PrmToAx_po = NULL; TARGET_TYPE *MMTkToPrm_po = NULL; std::cout<<"MMTK configuration->dimensions[0]"<<configuration->dimensions[0]<<std::endl<<std::flush; indexMap = new TARGET_TYPE*[(configuration->dimensions[0])]; int _indexMap[natoms][3]; PrmToAx_po = new TARGET_TYPE[configuration->dimensions[0]]; MMTkToPrm_po = new TARGET_TYPE[configuration->dimensions[0]]; int natoms3 = 3*(configuration->dimensions[0]); int arrays_cut = 2 + 4*natoms3; SHMSZ = ( 2*sizeof(TARGET_TYPE) + // Counter and flag natoms3*sizeof(TARGET_TYPE) + // Positions natoms3*sizeof(TARGET_TYPE) + // Velocities natoms3*sizeof(TARGET_TYPE) + // indexMap natoms3*sizeof(TARGET_TYPE) + // Gradients 5*sizeof(TARGET_TYPE) + // ac + 0 -> 4 // step, nosteps, temperature, timestep, trouble 1*sizeof(TARGET_TYPE) + // ac + 5 // potential energy 2*sizeof(TARGET_TYPE) + // ac + 6->7 // DAE step done; Move accepted 1*sizeof(TARGET_TYPE) + // ac + 8 // KE 1*sizeof(TARGET_TYPE) + // ac + 9 // steps_per_trial 1*sizeof(TARGET_TYPE) //+ // ac + 10 // trial ); shm = new TARGET_TYPE[SHMSZ]; std::cout<<"parser.mol2F "<<parser.mol2F<<std::endl<<std::flush; std::cout<<"parser.rbF "<<parser.rbF<<std::endl<<std::flush; std::cout<<"parser.gaffF "<<parser.gaffF<<std::endl<<std::flush; std::cout<<"parser.frcmodF "<<parser.frcmodF<<std::endl<<std::flush; std::cout<<"parser.ictdF "<<parser.ictdF<<std::endl<<std::flush; // LS EU //(((PyFFEnergyTermObject **)(evaluator)->terms->data)[5])->param[0] = 0.5; // LS EU sys = new SymSystem( parser.mol2F, parser.rbF, parser.gaffF, parser.frcmodF, parser.ictdF, PrmToAx_po, MMTkToPrm_po, evaluator, p_energy_po, configuration, universe_spec, shm ); for(int i=0; i<natoms; i++){ _indexMap[i][2] = order[i]; } // * Memory alloc for convinient arrays * // coords = new TARGET_TYPE*[sys->mr->natms]; TARGET_TYPE **vels = new TARGET_TYPE*[sys->mr->natms]; TARGET_TYPE **inivels = new TARGET_TYPE*[sys->mr->natms]; TARGET_TYPE **grads = new TARGET_TYPE*[sys->mr->natms]; // * Seed the random number generator * // srand (time(NULL)); //+++++++ SERVER PART +++++++++ float mysweep = 0; shm[0] = CNT_START; int cli; sweep = 0; bool system_initialized = false; int big_loop_i; shm[1] = CLIENT_FLAG; int a; int start, start_1, stop; int i = natoms*2*3 + 2; // * Assign convenient pointers for order * / start = 2 + natoms3 + natoms3; for(a=0; a<natoms; a++){ indexMap[a] = &(shm[a*3 + start]); } // * Assign order in shm[][2] * // start = 2 + natoms3 + natoms3; cli = start; for (a=0; a<natoms; a++){ // Order cli += 2; shm[cli++] = order[a]; } cli = 2; for (a=0; a<natoms; a++){ // Positions shm[cli++] = x[a][0]; shm[cli++] = x[a][1]; shm[cli++] = x[a][2]; } for (a=0; a<natoms; a++){ // Velocities shm[cli++] = .0; //vels[a][0];//shm[cli++] = v[a][0]; shm[cli++] = .0; //vels[a][1];//shm[cli++] = v[a][1]; shm[cli++] = .0; //vels[a][2];//shm[cli++] = v[a][2]; } for (a=0; a<natoms; a++){ // Order cli += 2; shm[cli++] = order[a]; } for (a=0; a<natoms; a++){ // Forces shm[cli++] = .0; shm[cli++] = .0; shm[cli++] = .0; } shm[cli++] = big_loop_i; // Step shm[cli++] = 50.0; //(TARGET_TYPE)nosteps; // Number of steps shm[cli++] = temperature; // Temeperature shm[cli++] = delta_t; shm[cli++] = trouble; shm[cli++] = p_energy_po->energy; cli++; // DAE set only by the server shm[cli] = acceptance + 0.0; //Set the client flag shm[1] = CLIENT_FLAG; shm[arrays_cut + 6] = 13.0; // set DAE to done // * Assign convenient pointers for coordinates * // int shm_coords_sup = (natoms3)+2; int j=-1, k=0; start = 2; for(j=0; j<natoms; j++){ coords[j] = &(shm[j*3 + start]); } mysweep = shm[0] - CNT_START; // * Assign convenient pointers for velocities * // start = 2 + natoms3; start_1 = start - 1; stop = 2 + natoms3 + natoms3; for(j=0; j<natoms; j++){ vels[j] = &(shm[j*3 + start]); inivels[j] = &(shm[j*3 + start]); } // * Assign convenient pointers for gradients * // start = 2 + natoms3 + natoms3 + natoms3; start_1 = start - 1; stop = 2 + natoms3 + natoms3 + natoms3 + natoms3; for(j=0; j<natoms; j++){ grads[j] = &(shm[j*3 + start]); } // * Rewrite indexMap to memory * / start = 2 + natoms3 + natoms3; start_1 = start - 1; stop = 2 + natoms3 + natoms3 + natoms3; // ************************* / // * Initialize Simulation * / // ************************* / TARGET_TYPE timestep, mytimestep; mytimestep = shm[arrays_cut + 3]; // * Build bMainResidue and fill indexMap * / sys->InitSimulation(coords, vels, inivels, indexMap, grads, mytimestep, true); system_initialized = true; }
static void gevent_loop() { // ensure SIGPIPE is ignored signal(SIGPIPE, SIG_IGN); if (!uwsgi.has_threads && uwsgi.mywid == 1) { uwsgi_log("!!! Running gevent without threads IS NOT recommended, enable them with --enable-threads !!!\n"); } if (uwsgi.socket_timeout < 30) { uwsgi_log("!!! Running gevent with a socket-timeout lower than 30 seconds is not recommended, tune it with --socket-timeout !!!\n"); } // get the GIL UWSGI_GET_GIL up.gil_get = gil_gevent_get; up.gil_release = gil_gevent_release; uwsgi.wait_write_hook = uwsgi_gevent_wait_write_hook; uwsgi.wait_read_hook = uwsgi_gevent_wait_read_hook; uwsgi.wait_milliseconds_hook = uwsgi_gevent_wait_milliseconds_hook; struct uwsgi_socket *uwsgi_sock = uwsgi.sockets; if (uwsgi.async < 1) { uwsgi_log("the gevent loop engine requires async mode (--async <n>)\n"); exit(1); } uwsgi.current_wsgi_req = uwsgi_gevent_current_wsgi_req; PyObject *gevent_dict = get_uwsgi_pydict("gevent"); if (!gevent_dict) uwsgi_pyexit; PyObject *gevent_version = PyDict_GetItemString(gevent_dict, "version_info"); if (!gevent_version) uwsgi_pyexit; if (PyInt_AsLong(PyTuple_GetItem(gevent_version, 0)) < 1) { uwsgi_log("uWSGI requires at least gevent 1.x version\n"); exit(1); } // call gevent.monkey.patch_all() if requested if (ugevent.monkey) { monkey_patch(); } ugevent.spawn = PyDict_GetItemString(gevent_dict, "spawn"); if (!ugevent.spawn) uwsgi_pyexit; ugevent.signal = PyDict_GetItemString(gevent_dict, "signal"); if (!ugevent.signal) uwsgi_pyexit; ugevent.greenlet_switch = PyDict_GetItemString(gevent_dict, "sleep"); if (!ugevent.greenlet_switch) uwsgi_pyexit; ugevent.greenlet_switch_args = PyTuple_New(0); Py_INCREF(ugevent.greenlet_switch_args); PyObject *gevent_get_hub = PyDict_GetItemString(gevent_dict, "get_hub"); ugevent.hub = python_call(gevent_get_hub, PyTuple_New(0), 0, NULL); if (!ugevent.hub) uwsgi_pyexit; ugevent.get_current = PyDict_GetItemString(gevent_dict, "getcurrent"); if (!ugevent.get_current) uwsgi_pyexit; ugevent.get_current_args = PyTuple_New(0); Py_INCREF(ugevent.get_current_args); ugevent.hub_loop = PyObject_GetAttrString(ugevent.hub, "loop"); if (!ugevent.hub_loop) uwsgi_pyexit; // main greenlet waiting for connection (one greenlet per-socket) uwsgi_gevent_main = PyCFunction_New(uwsgi_gevent_main_def, NULL); Py_INCREF(uwsgi_gevent_main); // greenlet to run at each request PyObject *uwsgi_request_greenlet = PyCFunction_New(uwsgi_gevent_request_def, NULL); Py_INCREF(uwsgi_request_greenlet); // pre-fill the greenlet args ugevent.greenlet_args = PyTuple_New(2); PyTuple_SetItem(ugevent.greenlet_args, 0, uwsgi_request_greenlet); if (uwsgi.signal_socket > -1) { // and these are the watcher for signal sockets ugevent.signal_watcher = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", uwsgi.signal_socket, 1); if (!ugevent.signal_watcher) uwsgi_pyexit; ugevent.my_signal_watcher = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", uwsgi.my_signal_socket, 1); if (!ugevent.my_signal_watcher) uwsgi_pyexit; PyObject *uwsgi_greenlet_signal = PyCFunction_New(uwsgi_gevent_signal_def, NULL); Py_INCREF(uwsgi_greenlet_signal); PyObject *uwsgi_greenlet_my_signal = PyCFunction_New(uwsgi_gevent_my_signal_def, NULL); Py_INCREF(uwsgi_greenlet_my_signal); PyObject *uwsgi_greenlet_signal_handler = PyCFunction_New(uwsgi_gevent_signal_handler_def, NULL); Py_INCREF(uwsgi_greenlet_signal_handler); ugevent.signal_args = PyTuple_New(2); PyTuple_SetItem(ugevent.signal_args, 0, uwsgi_greenlet_signal_handler); // start the two signal watchers if (!PyObject_CallMethod(ugevent.signal_watcher, "start", "O", uwsgi_greenlet_signal)) uwsgi_pyexit; if (!PyObject_CallMethod(ugevent.my_signal_watcher, "start", "O", uwsgi_greenlet_my_signal)) uwsgi_pyexit; } // start a greenlet for each socket ugevent.watchers = uwsgi_malloc(sizeof(PyObject *) * uwsgi_count_sockets(uwsgi.sockets)); int i = 0; while(uwsgi_sock) { // this is the watcher for server socket ugevent.watchers[i] = PyObject_CallMethod(ugevent.hub_loop, "io", "ii", uwsgi_sock->fd, 1); if (!ugevent.watchers[i]) uwsgi_pyexit; // start the main greenlet start_watcher(i, uwsgi_sock); uwsgi_sock = uwsgi_sock->next; i++; } // patch goodbye_cruel_world uwsgi.gbcw_hook = uwsgi_gevent_gbcw; // map SIGHUP with gevent.signal PyObject *ge_signal_tuple = PyTuple_New(2); PyTuple_SetItem(ge_signal_tuple, 0, PyInt_FromLong(SIGHUP)); PyObject *uwsgi_gevent_unix_signal_handler = PyCFunction_New(uwsgi_gevent_unix_signal_handler_def, NULL); Py_INCREF(uwsgi_gevent_unix_signal_handler); PyTuple_SetItem(ge_signal_tuple, 1, uwsgi_gevent_unix_signal_handler); python_call(ugevent.signal, ge_signal_tuple, 0, NULL); // map SIGINT/SIGTERM with gevent.signal ge_signal_tuple = PyTuple_New(2); PyTuple_SetItem(ge_signal_tuple, 0, PyInt_FromLong(SIGINT)); PyObject *uwsgi_gevent_unix_signal_int_handler = PyCFunction_New(uwsgi_gevent_unix_signal_int_handler_def, NULL); Py_INCREF(uwsgi_gevent_unix_signal_int_handler); PyTuple_SetItem(ge_signal_tuple, 1, uwsgi_gevent_unix_signal_int_handler); python_call(ugevent.signal, ge_signal_tuple, 0, NULL); ge_signal_tuple = PyTuple_New(2); PyTuple_SetItem(ge_signal_tuple, 0, PyInt_FromLong(SIGTERM)); PyTuple_SetItem(ge_signal_tuple, 1, uwsgi_gevent_unix_signal_int_handler); python_call(ugevent.signal, ge_signal_tuple, 0, NULL); PyObject *wait_for_me = ugevent.hub; if (!ugevent.wait_for_hub) { // spawn the control greenlet PyObject *uwsgi_greenlet_ctrl_gl_handler = PyCFunction_New(uwsgi_gevent_ctrl_gl_def, NULL); Py_INCREF(uwsgi_greenlet_ctrl_gl_handler); PyObject *ctrl_gl_args = PyTuple_New(1); PyTuple_SetItem(ctrl_gl_args, 0, uwsgi_greenlet_ctrl_gl_handler); ugevent.ctrl_gl = python_call(ugevent.spawn, ctrl_gl_args, 0, NULL); Py_INCREF(ugevent.ctrl_gl); wait_for_me = ugevent.ctrl_gl; } for(;;) { if (!PyObject_CallMethod(wait_for_me, "join", NULL)) { PyErr_Print(); } else { break; } } // no need to worry about freeing memory PyObject *uwsgi_dict = get_uwsgi_pydict("uwsgi"); if (uwsgi_dict) { PyObject *ae = PyDict_GetItemString(uwsgi_dict, "atexit"); if (ae) { python_call(ae, PyTuple_New(0), 0, NULL); } } if (uwsgi.workers[uwsgi.mywid].manage_next_request == 0) { uwsgi_log("goodbye to the gevent Hub on worker %d (pid: %d)\n", uwsgi.mywid, uwsgi.mypid); if (ugevent.destroy) { exit(0); } exit(UWSGI_RELOAD_CODE); } uwsgi_log("the gevent Hub is no more :(\n"); }
static void GeanyPy_init_manager(const gchar *dir) { PyObject *module, *man, *args; gchar *sys_plugin_dir = NULL; g_return_if_fail(dir != NULL); module = PyImport_ImportModule("geany.manager"); if (module == NULL) { g_warning(_("Failed to import manager module")); return; } man = PyObject_GetAttrString(module, "PluginManager"); Py_DECREF(module); if (man == NULL) { g_warning(_("Failed to retrieve PluginManager from manager module")); return; } #ifdef GEANYPY_WINDOWS { /* Detect the system plugin's dir at runtime on Windows since we * don't really know where Geany is installed. */ gchar *geany_base_dir; geany_base_dir = g_win32_get_package_installation_directory_of_module(NULL); if (geany_base_dir) { sys_plugin_dir = g_build_filename(geany_base_dir, "lib", "geanypy", "plugins", NULL); g_free(geany_base_dir); } if (!g_file_test(sys_plugin_dir, G_FILE_TEST_EXISTS)) { g_warning(_("System plugin directory not found.")); g_free(sys_plugin_dir); sys_plugin_dir = NULL; } } #else sys_plugin_dir = g_strdup(GEANYPY_PLUGIN_DIR); #endif g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "User plugins: %s", dir); if (sys_plugin_dir) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "System plugins: %s", sys_plugin_dir); args = Py_BuildValue("([s, s])", sys_plugin_dir, dir); g_free(sys_plugin_dir); } else args = Py_BuildValue("([s])", dir); manager = PyObject_CallObject(man, args); if (PyErr_Occurred()) PyErr_Print(); Py_DECREF(man); Py_DECREF(args); if (manager == NULL) { g_warning(_("Unable to instantiate new PluginManager")); return; } }
PyMODINIT_FUNC initgreenlet(void) #endif { PyObject* m = NULL; char** p = NULL; PyObject *c_api_object; static void *_PyGreenlet_API[PyGreenlet_API_pointers]; GREENLET_NOINLINE_INIT(); #if PY_MAJOR_VERSION >= 3 m = PyModule_Create(&greenlet_module_def); #else m = Py_InitModule("greenlet", GreenMethods); #endif if (m == NULL) { INITERROR; } if (PyModule_AddStringConstant(m, "__version__", GREENLET_VERSION) < 0) { INITERROR; } #if PY_MAJOR_VERSION >= 3 ts_curkey = PyUnicode_InternFromString("__greenlet_ts_curkey"); ts_delkey = PyUnicode_InternFromString("__greenlet_ts_delkey"); #if GREENLET_USE_TRACING ts_tracekey = PyUnicode_InternFromString("__greenlet_ts_tracekey"); ts_event_switch = PyUnicode_InternFromString("switch"); ts_event_throw = PyUnicode_InternFromString("throw"); #endif #else ts_curkey = PyString_InternFromString("__greenlet_ts_curkey"); ts_delkey = PyString_InternFromString("__greenlet_ts_delkey"); #if GREENLET_USE_TRACING ts_tracekey = PyString_InternFromString("__greenlet_ts_tracekey"); ts_event_switch = PyString_InternFromString("switch"); ts_event_throw = PyString_InternFromString("throw"); #endif #endif if (ts_curkey == NULL || ts_delkey == NULL) { INITERROR; } if (PyType_Ready(&PyGreenlet_Type) < 0) { INITERROR; } PyExc_GreenletError = PyErr_NewException("greenlet.error", NULL, NULL); if (PyExc_GreenletError == NULL) { INITERROR; } #if PY_MAJOR_VERSION >= 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5) PyExc_GreenletExit = PyErr_NewException("greenlet.GreenletExit", PyExc_BaseException, NULL); #else PyExc_GreenletExit = PyErr_NewException("greenlet.GreenletExit", NULL, NULL); #endif if (PyExc_GreenletExit == NULL) { INITERROR; } ts_empty_tuple = PyTuple_New(0); if (ts_empty_tuple == NULL) { INITERROR; } ts_empty_dict = PyDict_New(); if (ts_empty_dict == NULL) { INITERROR; } ts_current = green_create_main(); if (ts_current == NULL) { INITERROR; } Py_INCREF(&PyGreenlet_Type); PyModule_AddObject(m, "greenlet", (PyObject*) &PyGreenlet_Type); Py_INCREF(PyExc_GreenletError); PyModule_AddObject(m, "error", PyExc_GreenletError); Py_INCREF(PyExc_GreenletExit); PyModule_AddObject(m, "GreenletExit", PyExc_GreenletExit); PyModule_AddObject(m, "GREENLET_USE_GC", PyBool_FromLong(GREENLET_USE_GC)); PyModule_AddObject(m, "GREENLET_USE_TRACING", PyBool_FromLong(GREENLET_USE_TRACING)); /* also publish module-level data as attributes of the greentype. */ for (p=copy_on_greentype; *p; p++) { PyObject* o = PyObject_GetAttrString(m, *p); if (!o) continue; PyDict_SetItemString(PyGreenlet_Type.tp_dict, *p, o); Py_DECREF(o); } /* * Expose C API */ /* types */ _PyGreenlet_API[PyGreenlet_Type_NUM] = (void *) &PyGreenlet_Type; /* exceptions */ _PyGreenlet_API[PyExc_GreenletError_NUM] = (void *) PyExc_GreenletError; _PyGreenlet_API[PyExc_GreenletExit_NUM] = (void *) PyExc_GreenletExit; /* methods */ _PyGreenlet_API[PyGreenlet_New_NUM] = (void *) PyGreenlet_New; _PyGreenlet_API[PyGreenlet_GetCurrent_NUM] = (void *) PyGreenlet_GetCurrent; _PyGreenlet_API[PyGreenlet_Throw_NUM] = (void *) PyGreenlet_Throw; _PyGreenlet_API[PyGreenlet_Switch_NUM] = (void *) PyGreenlet_Switch; _PyGreenlet_API[PyGreenlet_SetParent_NUM] = (void *) PyGreenlet_SetParent; #ifdef GREENLET_USE_PYCAPSULE c_api_object = PyCapsule_New((void *) _PyGreenlet_API, "greenlet._C_API", NULL); #else c_api_object = PyCObject_FromVoidPtr((void *) _PyGreenlet_API, NULL); #endif if (c_api_object != NULL) { PyModule_AddObject(m, "_C_API", c_api_object); } #if PY_MAJOR_VERSION >= 3 return m; #endif }
PyMODINIT_FUNC INIT_MODULE(_psycopg)(void) { #if PY_VERSION_HEX < 0x03020000 static void *PSYCOPG_API[PSYCOPG_API_pointers]; PyObject *c_api_object; #endif PyObject *module = NULL, *dict; #ifdef PSYCOPG_DEBUG if (getenv("PSYCOPG_DEBUG")) psycopg_debug_enabled = 1; #endif Dprintf("initpsycopg: initializing psycopg %s", PSYCOPG_VERSION); /* initialize all the new types and then the module */ Py_TYPE(&connectionType) = &PyType_Type; Py_TYPE(&cursorType) = &PyType_Type; Py_TYPE(&typecastType) = &PyType_Type; Py_TYPE(&qstringType) = &PyType_Type; Py_TYPE(&binaryType) = &PyType_Type; Py_TYPE(&isqlquoteType) = &PyType_Type; Py_TYPE(&pbooleanType) = &PyType_Type; Py_TYPE(&pintType) = &PyType_Type; Py_TYPE(&pfloatType) = &PyType_Type; Py_TYPE(&pdecimalType) = &PyType_Type; Py_TYPE(&asisType) = &PyType_Type; Py_TYPE(&listType) = &PyType_Type; Py_TYPE(&chunkType) = &PyType_Type; Py_TYPE(&NotifyType) = &PyType_Type; Py_TYPE(&XidType) = &PyType_Type; if (PyType_Ready(&connectionType) == -1) goto exit; if (PyType_Ready(&cursorType) == -1) goto exit; if (PyType_Ready(&typecastType) == -1) goto exit; if (PyType_Ready(&qstringType) == -1) goto exit; if (PyType_Ready(&binaryType) == -1) goto exit; if (PyType_Ready(&isqlquoteType) == -1) goto exit; if (PyType_Ready(&pbooleanType) == -1) goto exit; if (PyType_Ready(&pintType) == -1) goto exit; if (PyType_Ready(&pfloatType) == -1) goto exit; if (PyType_Ready(&pdecimalType) == -1) goto exit; if (PyType_Ready(&asisType) == -1) goto exit; if (PyType_Ready(&listType) == -1) goto exit; if (PyType_Ready(&chunkType) == -1) goto exit; if (PyType_Ready(&NotifyType) == -1) goto exit; if (PyType_Ready(&XidType) == -1) goto exit; #ifdef PSYCOPG_EXTENSIONS Py_TYPE(&lobjectType) = &PyType_Type; if (PyType_Ready(&lobjectType) == -1) goto exit; #endif /* import mx.DateTime module, if necessary */ #ifdef HAVE_MXDATETIME Py_TYPE(&mxdatetimeType) = &PyType_Type; if (PyType_Ready(&mxdatetimeType) == -1) goto exit; if (0 != mxDateTime_ImportModuleAndAPI()) { PyErr_Clear(); /* only fail if the mx typacaster should have been the default */ #ifdef PSYCOPG_DEFAULT_MXDATETIME PyErr_SetString(PyExc_ImportError, "can't import mx.DateTime module (requested as default adapter)"); goto exit; #endif } #endif /* import python builtin datetime module, if available */ pyDateTimeModuleP = PyImport_ImportModule("datetime"); if (pyDateTimeModuleP == NULL) { Dprintf("initpsycopg: can't import datetime module"); PyErr_SetString(PyExc_ImportError, "can't import datetime module"); goto exit; } /* Initialize the PyDateTimeAPI everywhere is used */ PyDateTime_IMPORT; if (psyco_adapter_datetime_init()) { goto exit; } Py_TYPE(&pydatetimeType) = &PyType_Type; if (PyType_Ready(&pydatetimeType) == -1) goto exit; /* import psycopg2.tz anyway (TODO: replace with C-level module?) */ pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz"); if (pyPsycopgTzModule == NULL) { Dprintf("initpsycopg: can't import psycopg2.tz module"); PyErr_SetString(PyExc_ImportError, "can't import psycopg2.tz module"); goto exit; } pyPsycopgTzLOCAL = PyObject_GetAttrString(pyPsycopgTzModule, "LOCAL"); pyPsycopgTzFixedOffsetTimezone = PyObject_GetAttrString(pyPsycopgTzModule, "FixedOffsetTimezone"); /* initialize the module and grab module's dictionary */ #if PY_MAJOR_VERSION < 3 module = Py_InitModule("_psycopg", psycopgMethods); #else module = PyModule_Create(&psycopgmodule); #endif if (!module) { goto exit; } dict = PyModule_GetDict(module); /* initialize all the module's exported functions */ /* PyBoxer_API[PyBoxer_Fake_NUM] = (void *)PyBoxer_Fake; */ /* Create a CObject containing the API pointer array's address */ /* If anybody asks for a PyCapsule we'll deal with it. */ #if PY_VERSION_HEX < 0x03020000 c_api_object = PyCObject_FromVoidPtr((void *)PSYCOPG_API, NULL); if (c_api_object != NULL) PyModule_AddObject(module, "_C_API", c_api_object); #endif /* other mixed initializations of module-level variables */ psycoEncodings = PyDict_New(); psyco_encodings_fill(psycoEncodings); psyco_null = Bytes_FromString("NULL"); psyco_DescriptionType = psyco_make_description_type(); /* set some module's parameters */ PyModule_AddStringConstant(module, "__version__", PSYCOPG_VERSION); PyModule_AddStringConstant(module, "__doc__", "psycopg PostgreSQL driver"); PyModule_AddObject(module, "apilevel", Text_FromUTF8(APILEVEL)); PyModule_AddObject(module, "threadsafety", PyInt_FromLong(THREADSAFETY)); PyModule_AddObject(module, "paramstyle", Text_FromUTF8(PARAMSTYLE)); /* put new types in module dictionary */ PyModule_AddObject(module, "connection", (PyObject*)&connectionType); PyModule_AddObject(module, "cursor", (PyObject*)&cursorType); PyModule_AddObject(module, "ISQLQuote", (PyObject*)&isqlquoteType); PyModule_AddObject(module, "Notify", (PyObject*)&NotifyType); PyModule_AddObject(module, "Xid", (PyObject*)&XidType); #ifdef PSYCOPG_EXTENSIONS PyModule_AddObject(module, "lobject", (PyObject*)&lobjectType); #endif /* encodings dictionary in module dictionary */ PyModule_AddObject(module, "encodings", psycoEncodings); #ifdef HAVE_MXDATETIME /* If we can't find mx.DateTime objects at runtime, * remove them from the module (and, as consequence, from the adapters). */ if (0 != psyco_adapter_mxdatetime_init()) { PyDict_DelItemString(dict, "DateFromMx"); PyDict_DelItemString(dict, "TimeFromMx"); PyDict_DelItemString(dict, "TimestampFromMx"); PyDict_DelItemString(dict, "IntervalFromMx"); } #endif /* initialize default set of typecasters */ typecast_init(dict); /* initialize microprotocols layer */ microprotocols_init(dict); psyco_adapters_init(dict); /* create a standard set of exceptions and add them to the module's dict */ psyco_errors_init(); psyco_errors_fill(dict); /* Solve win32 build issue about non-constant initializer element */ cursorType.tp_alloc = PyType_GenericAlloc; binaryType.tp_alloc = PyType_GenericAlloc; isqlquoteType.tp_alloc = PyType_GenericAlloc; pbooleanType.tp_alloc = PyType_GenericAlloc; pintType.tp_alloc = PyType_GenericAlloc; pfloatType.tp_alloc = PyType_GenericAlloc; pdecimalType.tp_alloc = PyType_GenericAlloc; connectionType.tp_alloc = PyType_GenericAlloc; asisType.tp_alloc = PyType_GenericAlloc; qstringType.tp_alloc = PyType_GenericAlloc; listType.tp_alloc = PyType_GenericAlloc; chunkType.tp_alloc = PyType_GenericAlloc; pydatetimeType.tp_alloc = PyType_GenericAlloc; NotifyType.tp_alloc = PyType_GenericAlloc; XidType.tp_alloc = PyType_GenericAlloc; #ifdef PSYCOPG_EXTENSIONS lobjectType.tp_alloc = PyType_GenericAlloc; #endif #ifdef HAVE_MXDATETIME mxdatetimeType.tp_alloc = PyType_GenericAlloc; #endif Dprintf("initpsycopg: module initialization complete"); exit: #if PY_MAJOR_VERSION > 2 return module; #else return; #endif }
/* * * Tests for and converts a Python datetime.datetime or datetime.date * object into a NumPy pandas_datetimestruct. * * While the C API has PyDate_* and PyDateTime_* functions, the following * implementation just asks for attributes, and thus supports * datetime duck typing. The tzinfo time zone conversion would require * this style of access anyway. * * 'out_bestunit' gives a suggested unit based on whether the object * was a datetime.date or datetime.datetime object. * * If 'apply_tzinfo' is 1, this function uses the tzinfo to convert * to UTC time, otherwise it returns the struct with the local time. * * Returns -1 on error, 0 on success, and 1 (with no error set) * if obj doesn't have the neeeded date or datetime attributes. */ int convert_pydatetime_to_datetimestruct(PyObject *obj, pandas_datetimestruct *out, PANDAS_DATETIMEUNIT *out_bestunit, int apply_tzinfo) { PyObject *tmp; int isleap; /* Initialize the output to all zeros */ memset(out, 0, sizeof(pandas_datetimestruct)); out->month = 1; out->day = 1; /* Need at least year/month/day attributes */ if (!PyObject_HasAttrString(obj, "year") || !PyObject_HasAttrString(obj, "month") || !PyObject_HasAttrString(obj, "day")) { return 1; } /* Get the year */ tmp = PyObject_GetAttrString(obj, "year"); if (tmp == NULL) { return -1; } out->year = PyInt_AsLong(tmp); if (out->year == -1 && PyErr_Occurred()) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); /* Get the month */ tmp = PyObject_GetAttrString(obj, "month"); if (tmp == NULL) { return -1; } out->month = PyInt_AsLong(tmp); if (out->month == -1 && PyErr_Occurred()) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); /* Get the day */ tmp = PyObject_GetAttrString(obj, "day"); if (tmp == NULL) { return -1; } out->day = PyInt_AsLong(tmp); if (out->day == -1 && PyErr_Occurred()) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); /* Validate that the month and day are valid for the year */ if (out->month < 1 || out->month > 12) { goto invalid_date; } isleap = is_leapyear(out->year); if (out->day < 1 || out->day > days_per_month_table[isleap][out->month-1]) { goto invalid_date; } /* Check for time attributes (if not there, return success as a date) */ if (!PyObject_HasAttrString(obj, "hour") || !PyObject_HasAttrString(obj, "minute") || !PyObject_HasAttrString(obj, "second") || !PyObject_HasAttrString(obj, "microsecond")) { /* The best unit for date is 'D' */ if (out_bestunit != NULL) { *out_bestunit = PANDAS_FR_D; } return 0; } /* Get the hour */ tmp = PyObject_GetAttrString(obj, "hour"); if (tmp == NULL) { return -1; } out->hour = PyInt_AsLong(tmp); if (out->hour == -1 && PyErr_Occurred()) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); /* Get the minute */ tmp = PyObject_GetAttrString(obj, "minute"); if (tmp == NULL) { return -1; } out->min = PyInt_AsLong(tmp); if (out->min == -1 && PyErr_Occurred()) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); /* Get the second */ tmp = PyObject_GetAttrString(obj, "second"); if (tmp == NULL) { return -1; } out->sec = PyInt_AsLong(tmp); if (out->sec == -1 && PyErr_Occurred()) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); /* Get the microsecond */ tmp = PyObject_GetAttrString(obj, "microsecond"); if (tmp == NULL) { return -1; } out->us = PyInt_AsLong(tmp); if (out->us == -1 && PyErr_Occurred()) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); if (out->hour < 0 || out->hour >= 24 || out->min < 0 || out->min >= 60 || out->sec < 0 || out->sec >= 60 || out->us < 0 || out->us >= 1000000) { goto invalid_time; } /* Apply the time zone offset if it exists */ if (apply_tzinfo && PyObject_HasAttrString(obj, "tzinfo")) { tmp = PyObject_GetAttrString(obj, "tzinfo"); if (tmp == NULL) { return -1; } if (tmp == Py_None) { Py_DECREF(tmp); } else { PyObject *offset; int seconds_offset, minutes_offset; /* The utcoffset function should return a timedelta */ offset = PyObject_CallMethod(tmp, "utcoffset", "O", obj); if (offset == NULL) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); /* * The timedelta should have a function "total_seconds" * which contains the value we want. */ tmp = PyObject_CallMethod(offset, "total_seconds", ""); if (tmp == NULL) { return -1; } seconds_offset = PyInt_AsLong(tmp); if (seconds_offset == -1 && PyErr_Occurred()) { Py_DECREF(tmp); return -1; } Py_DECREF(tmp); /* Convert to a minutes offset and apply it */ minutes_offset = seconds_offset / 60; add_minutes_to_datetimestruct(out, -minutes_offset); } } /* The resolution of Python's datetime is 'us' */ if (out_bestunit != NULL) { *out_bestunit = PANDAS_FR_us; } return 0; invalid_date: PyErr_Format(PyExc_ValueError, "Invalid date (%d,%d,%d) when converting to NumPy datetime", (int)out->year, (int)out->month, (int)out->day); return -1; invalid_time: PyErr_Format(PyExc_ValueError, "Invalid time (%d,%d,%d,%d) when converting " "to NumPy datetime", (int)out->hour, (int)out->min, (int)out->sec, (int)out->us); return -1; }
PyMODINIT_FUNC PyInit__multiprocess(void) { PyObject *module, *temp, *value; /* Initialize module */ module = PyModule_Create(&multiprocessing_module); if (!module) return NULL; /* Get copy of objects from pickle */ temp = PyImport_ImportModule("dill"); if (!temp) temp = PyImport_ImportModule(PICKLE_MODULE); if (!temp) return NULL; pickle_dumps = PyObject_GetAttrString(temp, "dumps"); pickle_loads = PyObject_GetAttrString(temp, "loads"); pickle_protocol = PyObject_GetAttrString(temp, "DEFAULT_PROTOCOL"); if (!pickle_protocol) pickle_protocol = PyObject_GetAttrString(temp, "HIGHEST_PROTOCOL"); Py_XDECREF(temp); /* Get copy of BufferTooShort */ temp = PyImport_ImportModule("multiprocess"); if (!temp) return NULL; BufferTooShort = PyObject_GetAttrString(temp, "BufferTooShort"); Py_XDECREF(temp); /* Add connection type to module */ if (PyType_Ready(&ConnectionType) < 0) return NULL; Py_INCREF(&ConnectionType); PyModule_AddObject(module, "Connection", (PyObject*)&ConnectionType); #if defined(MS_WINDOWS) || \ (defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)) /* Add SemLock type to module */ if (PyType_Ready(&SemLockType) < 0) return NULL; Py_INCREF(&SemLockType); { PyObject *py_sem_value_max; /* Some systems define SEM_VALUE_MAX as an unsigned value that * causes it to be negative when used as an int (NetBSD). */ if ((int)(SEM_VALUE_MAX) < 0) py_sem_value_max = PyLong_FromLong(INT_MAX); else py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX); if (py_sem_value_max == NULL) return NULL; PyDict_SetItemString(SemLockType.tp_dict, "SEM_VALUE_MAX", py_sem_value_max); } PyModule_AddObject(module, "SemLock", (PyObject*)&SemLockType); #endif #ifdef MS_WINDOWS /* Add PipeConnection to module */ if (PyType_Ready(&PipeConnectionType) < 0) return NULL; Py_INCREF(&PipeConnectionType); PyModule_AddObject(module, "PipeConnection", (PyObject*)&PipeConnectionType); /* Initialize win32 class and add to multiprocessing */ temp = create_win32_namespace(); if (!temp) return NULL; PyModule_AddObject(module, "win32", temp); /* Initialize the event handle used to signal Ctrl-C */ sigint_event = CreateEvent(NULL, TRUE, FALSE, NULL); if (!sigint_event) { PyErr_SetFromWindowsErr(0); return NULL; } if (!SetConsoleCtrlHandler(ProcessingCtrlHandler, TRUE)) { PyErr_SetFromWindowsErr(0); return NULL; } #endif /* Add configuration macros */ temp = PyDict_New(); if (!temp) return NULL; #define ADD_FLAG(name) \ value = Py_BuildValue("i", name); \ if (value == NULL) { Py_DECREF(temp); return NULL; } \ if (PyDict_SetItemString(temp, #name, value) < 0) { \ Py_DECREF(temp); Py_DECREF(value); return NULL; } \ Py_DECREF(value) #if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED) ADD_FLAG(HAVE_SEM_OPEN); #endif #ifdef HAVE_SEM_TIMEDWAIT ADD_FLAG(HAVE_SEM_TIMEDWAIT); #endif #ifdef HAVE_FD_TRANSFER ADD_FLAG(HAVE_FD_TRANSFER); #endif #ifdef HAVE_BROKEN_SEM_GETVALUE ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE); #endif #ifdef HAVE_BROKEN_SEM_UNLINK ADD_FLAG(HAVE_BROKEN_SEM_UNLINK); #endif if (PyModule_AddObject(module, "flags", temp) < 0) return NULL; return module; }
static int ConditionObject_init(ConditionObject *self, PyObject *args, PyObject *kwargs) { PyObject *lock = NULL, *m = NULL; static char *keywords[] = {"lock", NULL}; DEBUG("self:%p", self); if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O:__init__", keywords, &lock)) { return -1; } Py_CLEAR(self->lock); if (lock == NULL) { DEBUG("create RLock self:%p", self); lock = PyObject_CallFunctionObjArgs((PyObject*)&RLockObjectType, NULL); if (lock == NULL) { return -1; } self->lock = lock; } else { self->lock = lock; Py_INCREF(self->lock); } DEBUG("self:%p lock:%p", self, self->lock); Py_CLEAR(self->waiters); self->waiters = PyList_New(0); GDEBUG("list:%p", self->waiters); if (self->waiters == NULL) { Py_CLEAR(self->lock); return -1; } m = PyObject_GetAttrString(lock, "acquire"); if (m == NULL) { return -1; } if (PyObject_SetAttrString((PyObject*)self, "acquire", m) == -1) { Py_DECREF(m); return -1; } Py_XDECREF(m); m = PyObject_GetAttrString(lock, "release"); if (m == NULL) { return -1; } if (PyObject_SetAttrString((PyObject*)self, "release", m) == -1) { Py_DECREF(m); return -1; } Py_XDECREF(m); m = PyObject_GetAttrString(lock, "_release_save"); if (m != NULL) { if (PyObject_SetAttrString((PyObject*)self, "_release_save", m) == -1) { Py_DECREF(m); return -1; } DEBUG("import _release_save self:%p lock:%p", self, lock); } PyErr_Clear(); Py_XDECREF(m); m = PyObject_GetAttrString(lock, "_acquire_restore"); if (m != NULL) { if (PyObject_SetAttrString((PyObject*)self, "_acquire_restore", m) == -1) { Py_DECREF(m); return -1; } DEBUG("import _acquire_restore self:%p lock:%p", self, lock); } PyErr_Clear(); Py_XDECREF(m); m = PyObject_GetAttrString(lock, "_is_owned"); if (m != NULL) { if (PyObject_SetAttrString((PyObject*)self, "_is_owned", m) == -1) { Py_DECREF(m); return -1; } DEBUG("import _is_owned self:%p lock:%p", self, lock); } PyErr_Clear(); Py_XDECREF(m); return 1; }
static int pyth_metric_init (apr_pool_t *p) { DIR *dp; struct dirent *entry; int i; char* modname; PyObject *pmod, *pinitfunc, *pobj, *pparamdict; py_metric_init_t minfo; Ganglia_25metric *gmi; mapped_info_t *mi; const char* path = python_module.module_params; cfg_t *module_cfg; /* Allocate a pool that will be used by this module */ apr_pool_create(&pool, p); metric_info = apr_array_make(pool, 10, sizeof(Ganglia_25metric)); metric_mapping_info = apr_array_make(pool, 10, sizeof(mapped_info_t)); /* Verify path exists and can be read */ if (!path) { err_msg("[PYTHON] Missing python module path.\n"); return -1; } if (access(path, F_OK)) { /* 'path' does not exist */ err_msg("[PYTHON] Can't open the python module path %s.\n", path); return -1; } if (access(path, R_OK)) { /* Don't have read access to 'path' */ err_msg("[PYTHON] Can't read from the python module path %s.\n", path); return -1; } /* Init Python environment */ /* Set up the python path to be able to load module from our module path */ Py_Initialize(); PyObject *sys_path = PySys_GetObject("path"); PyObject *addpath = PyString_FromString(path); PyList_Append(sys_path, addpath); PyEval_InitThreads(); gtstate = PyEval_SaveThread(); /* Initialize each python module */ if ((dp = opendir(path)) == NULL) { /* Error: Cannot open the directory - Shouldn't happen */ /* Log? */ err_msg("[PYTHON] Can't open the python module path %s.\n", path); return -1; } i = 0; while ((entry = readdir(dp)) != NULL) { modname = is_python_module(entry->d_name); if (modname == NULL) continue; /* Find the specified module configuration in gmond.conf If this return NULL then either the module config doesn't exist or the module is disabled. */ module_cfg = find_module_config(modname); if (!module_cfg) continue; PyEval_RestoreThread(gtstate); pmod = PyImport_ImportModule(modname); if (!pmod) { /* Failed to import module. Log? */ err_msg("[PYTHON] Can't import the metric module [%s].\n", modname); if (PyErr_Occurred()) { PyErr_Print(); } gtstate = PyEval_SaveThread(); continue; } pinitfunc = PyObject_GetAttrString(pmod, "metric_init"); if (!pinitfunc || !PyCallable_Check(pinitfunc)) { /* No metric_init function. */ err_msg("[PYTHON] Can't find the metric_init function in the python module [%s].\n", modname); Py_DECREF(pmod); gtstate = PyEval_SaveThread(); continue; } /* Build a parameter dictionary to pass to the module */ pparamdict = build_params_dict(module_cfg); if (!pparamdict || !PyDict_Check(pparamdict)) { /* No metric_init function. */ err_msg("[PYTHON] Can't build the parameters dictionary for [%s].\n", modname); Py_DECREF(pmod); gtstate = PyEval_SaveThread(); continue; } /* Now call the metric_init method of the python module */ pobj = PyObject_CallFunction(pinitfunc, "(N)", pparamdict); if (!pobj) { /* failed calling metric_init */ err_msg("[PYTHON] Can't call the metric_init function in the python module [%s].\n", modname); if (PyErr_Occurred()) { PyErr_Print(); } Py_DECREF(pinitfunc); Py_DECREF(pmod); gtstate = PyEval_SaveThread(); continue; } if (PyList_Check(pobj)) { int j; int size = PyList_Size(pobj); for (j = 0; j < size; j++) { PyObject* plobj = PyList_GetItem(pobj, j); if (PyMapping_Check(plobj)) { fill_metric_info(plobj, &minfo, modname, pool); gmi = (Ganglia_25metric*)apr_array_push(metric_info); fill_gmi(gmi, &minfo); mi = (mapped_info_t*)apr_array_push(metric_mapping_info); mi->pmod = pmod; mi->mod_name = apr_pstrdup(pool, modname); mi->pcb = minfo.pcb; } } } else if (PyMapping_Check(pobj)) { fill_metric_info(pobj, &minfo, modname, pool); gmi = (Ganglia_25metric*)apr_array_push(metric_info); fill_gmi(gmi, &minfo); mi = (mapped_info_t*)apr_array_push(metric_mapping_info); mi->pmod = pmod; mi->mod_name = apr_pstrdup(pool, modname); mi->pcb = minfo.pcb; } Py_DECREF(pobj); Py_DECREF(pinitfunc); gtstate = PyEval_SaveThread(); } closedir(dp); apr_pool_cleanup_register(pool, NULL, pyth_metric_cleanup, apr_pool_cleanup_null); /* Replace the empty static metric definition array with the dynamic array that we just created */ /*XXX Need to put this into a finalize MACRO. This is just pushing a NULL entry onto the array so that the looping logic can determine the end if the array. We should probably give back a ready APR array rather than a pointer to a Ganglia_25metric array. */ gmi = apr_array_push(metric_info); memset (gmi, 0, sizeof(*gmi)); mi = apr_array_push(metric_mapping_info); memset (mi, 0, sizeof(*mi)); python_module.metrics_info = (Ganglia_25metric *)metric_info->elts; return 0; }