PyObject *wrap_calc_fit_R(PyObject *self,PyObject *args) { PyObject *cs1, *cs2, *mass; if(!PyArg_ParseTuple(args,"OOO",&cs1, &cs2, &mass)) return NULL; int natoms1 = PySequence_Length(cs1); int natoms2 = PySequence_Length(cs2); if( natoms1 != natoms2 ) { Error("Cannot fit coordinate sets with different lengths"); } rvec x1[natoms1]; rvec x2[natoms1]; real m[natoms1]; PyObject2rvec( cs1, x1, natoms1); PyObject2rvec( cs2, x2, natoms2); PyObject2real_array(mass, m, natoms1); center(x1, natoms1); center(x2, natoms1); matrix R; clear_mat(R); calc_fit_R(natoms1,m,x1,x2,R); PyObject *ret = matrix2PyObject(R); return ret; }
static PyObject* pyGenericPhysical_calcBoxBounds(pyGenericPhysical* self, PyObject* args) { PyObject* points; if (!(PyArg_ParseTuple(args, "O", &points) && PySequence_Check(points))) { PyErr_SetString(PyExc_TypeError, "calcBoxBounds expects a sequence of hsVector3"); return NULL; } hsVector3* myPoints = new hsVector3[PySequence_Length(points)]; for (Py_ssize_t i = 0; i < PySequence_Length(points); ++i) { PyObject* item = PySequence_GetItem(points, i); if (!pyVector3_Check(item)) { PyErr_SetString(PyExc_TypeError, "calcBoxBounds expects a sequence of hsVector3"); Py_XDECREF(item); delete[] myPoints; return NULL; } myPoints[i] = *((pyVector3*)item)->fThis; Py_DECREF(item); } self->fThis->calcBoxBounds(PySequence_Length(points), myPoints); delete[] myPoints; Py_INCREF(Py_None); return Py_None; }
void build_b14_from_bonds(PyObject *atomlist ) { int i,k, l; int natoms = PySequence_Length(atomlist); for(i=0;i<natoms;i++){ PyObject *atom = PySequence_GetItem(atomlist, i); PyObject *b14 = PyObject_GetAttrString(atom,"b14"); int atom_id = PyInt_AsLong (PyObject_GetAttrString(atom,"id") ); PyObject *b13 = PyObject_GetAttrString(atom,"b13"); int len_b13 = PySequence_Length(b13); for(k=0;k<len_b13;k++){ PyObject *bb = PySequence_GetItem(b13, k); PyObject *bb_bonds = PyObject_GetAttrString(bb,"bonds"); int len_bonds = PySequence_Length(bb_bonds); for(l=0;l<len_bonds;l++){ PyObject *bond = PySequence_GetItem(bb_bonds, l); int atom_id2 = PyInt_AsLong (PyObject_GetAttrString(bond,"id") ); if( atom_id < atom_id2 && ! is_bound(atom, atom_id2 -1 )) { PyObject *bb14 = PyObject_GetAttrString(bond,"b14"); PyList_Append(b14, bond ); PyList_Append(bb14, atom ); } } } } }
PyObject* create_Group(PyObject* args){ if (PySequence_Length(args) == 0){ throw TypeError("A group must contain at least one object"); } // Use either the function arguments as the sequence of objects, or // a single sequence-argument as the sequence. i.e. allow both // Group(a, b, c, d) and Group([a,b,c,d]) PyObject* sequence = (PySequence_Length(args) == 1 && PySequence_Check(PySequence_GetItem(args, 0))) ? PySequence_GetItem(args, 0) : args; const auto n = PySequence_Length(sequence); // Prevent empty seguence arguments groups, i.e. Group([]) if (n == 0){ throw TypeError("A group must contain at least one object."); } objects_t faintObjects; for (int i = 0; i != n; i++){ PyObject* object = PySequence_GetItem(sequence, i); if (!PyObject_IsInstance(object, (PyObject*)&ShapeType)){ throw TypeError("Unsupported item in list"); } faintObjects.push_back(proxy_shape(object)); } Object* group = create_composite_object_raw(faintObjects, Ownership::OWNER); return create_Shape(group); }
static float victorDist(PyObject *d1, PyObject *d2, float cost) { float dist=0.0; float last=1.0; float *lasti; int i,j, dl1, dl2; dl1 =(int)PySequence_Length(d1); dl2 =(int)PySequence_Length(d2); if (dl1== 0) { return (float)dl2; } else if (dl2 ==0) { return (float)dl1; } lasti=(float *)malloc((dl2+1)*sizeof(float)); for (i=0;i<dl2+1;i++) { lasti[i]=i; } for (i=1;i<dl1+1; i++) { if (i>1) lasti[dl2]=last; last=i; for (j=1;j<dl2+1;j++) { dist=min3(lasti[j]+1, last+1, lasti[j-1]+cost*abs(intat(d1, i-1) - intat(d2, j-1))); lasti[j-1]=last; last=dist; } } free(lasti); return dist; }
PyObject *wrap_fit(PyObject *self,PyObject *args) { PyObject *cs1, *cs2, *mass; if(!PyArg_ParseTuple(args,"OOO",&cs1, &cs2, &mass)) return NULL; int natoms1 = PySequence_Length(cs1); int natoms2 = PySequence_Length(cs2); if( natoms1 != natoms2 ) { Error("Cannot fit coordinate sets with different lengths"); } rvec x1[natoms1]; rvec x2[natoms1]; real m[natoms1]; PyObject2rvec( cs1, x1, natoms1); PyObject2rvec( cs2, x2, natoms2); PyObject2real_array(mass, m, natoms1); rvec cent; center_and_get_vec(x1, natoms1, cent); // center x1 and get vector for back translation center(x2, natoms1); // center x2 do_fit(natoms1, m, x1, x2); int i; for(i=0;i<natoms1;i++) // translate back rvec_add( x2[i], cent, x2[i]); PyObject *ret = rvec2PyObject(x2, natoms1); return ret; }
static PyObject* connectPool_repr(PyConnectPoolObject *v) { //printf("connectPool_repr\n"); char buf[64 + NAME_MAX]; PyOS_snprintf(buf, sizeof(buf), "connect to %s in port %d with %d busy and %d free", v->db, v->port, PySequence_Length(v->cons_busy), PySequence_Length(v->cons_free)); return PyString_FromString(buf); }
int convert_2d_pylist( PyObject *list, double ***array, int *rows, int *cols ) { int i, j; PyObject *item; PyObject *num; // Is the object a list? if (!PyList_Check(list)) return -1; *rows = PySequence_Length(list); // Does the list contain elements? if (*rows < 0) { return -1; } item = PySequence_GetItem(list,0); if(!PySequence_Check(item)) { return -1; } *cols = PySequence_Length(item); if(*cols < 0) { return -1; } Py_DECREF(item); *array = (double **)malloc(*rows * sizeof(double*) ); for( i = 0; i < *rows; i++ ) { (*array)[i] = (double*)malloc(*cols*sizeof(double)); } for( i = 0; i < *rows; i++ ) { item = PySequence_GetItem(list,i); if(!PySequence_Check(item)) { return -1; } if( PySequence_Length(item) != *cols) { return -1; } for( j = 0; j < *cols; j++ ) { num = PySequence_GetItem( item, j ); if (PyInt_Check(num)) { (*array)[i][j] = (double)PyInt_AsLong( num ); } else if (PyLong_Check(num)) { (*array)[i][j] = PyLong_AsDouble( num ); } else if (PyFloat_Check(num)) { (*array)[i][j] = PyFloat_AsDouble( num ); } else { return -1; } } Py_DECREF(item); } return 0; }
static PyObject* mqttv3_subscribeMany(PyObject* self, PyObject *args) { MQTTClient c; PyObject* topicList; PyObject* qosList; int count; char** topics; int* qoss; int i, rc = 0; if (!PyArg_ParseTuple(args, "kOO", &c, &topicList, &qosList)) return NULL; if (!PySequence_Check(topicList) || !PySequence_Check(qosList)) { PyErr_SetString(PyExc_TypeError, "3rd and 4th parameters must be sequences"); return NULL; } if ((count = PySequence_Length(topicList)) != PySequence_Length(qosList)) { PyErr_SetString(PyExc_TypeError, "3rd and 4th parameters must be sequences of the same length"); return NULL; } topics = malloc(count * sizeof(char*)); for (i = 0; i < count; ++i) topics[i] = PyString_AsString(PySequence_GetItem(topicList, i)); qoss = malloc(count * sizeof(int)); for (i = 0; i < count; ++i) qoss[i] = (int) PyInt_AsLong(PySequence_GetItem(qosList, i)); Py_BEGIN_ALLOW_THREADS rc = MQTTClient_subscribeMany(c, count, topics, qoss); Py_END_ALLOW_THREADS for (i = 0; i < count; ++i) PySequence_SetItem(qosList, i, PyInt_FromLong((long) qoss[i])); free(topics); free(qoss); if (rc == MQTTCLIENT_SUCCESS) return Py_BuildValue("iO", rc, qosList); else return Py_BuildValue("i", rc); }
static PyObject *Polygon_addContour(Polygon *self, PyObject *args) { #ifdef WITH_NUMERIC PyObject *a=NULL; gpc_vertex_list *vl; int hole = 0; if (! PyArg_ParseTuple(args, "O|i", &a, &hole)) return Polygon_Raise(ERR_ARG); if ((a = PyArray_ContiguousFromObject(a, PyArray_DOUBLE, 2, 2)) == NULL) return Polygon_Raise(ERR_ARG); if (((PyArrayObject *)a)->nd != 2) return Polygon_Raise(ERR_ARG); if (((PyArrayObject *)a)->dimensions[1] != 2) return Polygon_Raise(ERR_ARG); vl = PyMem_New(gpc_vertex_list, 1); vl->num_vertices = ((PyArrayObject *)a)->dimensions[0]; vl->vertex = PyMem_New(gpc_vertex, vl->num_vertices); memcpy((vl->vertex), (((PyArrayObject *)a)->data), 2*vl->num_vertices*sizeof(double)); Py_DECREF(a); #else PyObject *list=NULL, *flist, *point=NULL, *X, *Y; gpc_vertex_list *vl; gpc_vertex *v; int i, imax, hole = 0; if (! PyArg_ParseTuple(args, "O|i", &list, &hole)) return Polygon_Raise(ERR_ARG); if (! PySequence_Check(list)) return Polygon_Raise(ERR_ARG); flist = PySequence_Fast(list, "this is not a sequence"); if ((! flist) || ((imax = PySequence_Length(flist)) <= 2)) return Polygon_Raise(ERR_INV); vl = PyMem_New(gpc_vertex_list, 1); vl->num_vertices = imax; vl->vertex = v = PyMem_New(gpc_vertex, imax); for (i=0; i<imax; i++) { point = PySequence_Fast(PySequence_Fast_GET_ITEM(flist, i), "this is not a point"); if ((!point) || (PySequence_Length(point) != 2)) return Polygon_Raise(ERR_INV); v->x = PyFloat_AsDouble(X = PyNumber_Float(PySequence_Fast_GET_ITEM(point, 0))); v->y = PyFloat_AsDouble(Y = PyNumber_Float(PySequence_Fast_GET_ITEM(point, 1))); v++; Py_DECREF(X); Py_DECREF(Y); Py_DECREF(point); } Py_DECREF(flist); #endif /* WITH_NUMERIC */ gpc_add_contour(self->p, vl, hole); self->bbValid = 0; PyMem_Free(vl->vertex); PyMem_Free(vl); Py_INCREF(Py_None); return Py_None; }
int Config_init(Config *self, PyObject *args, PyObject *kwds) { char *path; int err; if (kwds) { PyErr_SetString(PyExc_TypeError, "Repository takes no keyword arguments"); return -1; } if (PySequence_Length(args) > 0) { if (!PyArg_ParseTuple(args, "s", &path)) { return -1; } err = git_config_open_ondisk(&self->config, path); } else { err = git_config_new(&self->config); } if (err < 0) { if (err == GIT_ENOTFOUND) { Error_set_exc(PyExc_IOError); } else { Error_set(err); } return -1; } return 0; }
static bool Connect(PyObject* pConnectString, HDBC hdbc, bool fAnsi, long timeout, Object& encoding) { // This should have been checked by the global connect function. I(PyString_Check(pConnectString) || PyUnicode_Check(pConnectString)); const int cchMax = 600; if (PySequence_Length(pConnectString) >= cchMax) { PyErr_SetString(PyExc_TypeError, "connection string too long"); return false; } // The driver manager determines if the app is a Unicode app based on whether we call SQLDriverConnectA or // SQLDriverConnectW. Some drivers, notably Microsoft Access/Jet, change their behavior based on this, so we try // the Unicode version first. (The Access driver only supports Unicode text, but SQLDescribeCol returns SQL_CHAR // instead of SQL_WCHAR if we connect with the ANSI version. Obviously this causes lots of errors since we believe // what it tells us (SQL_CHAR).) // Python supports only UCS-2 and UCS-4, so we shouldn't need to worry about receiving surrogate pairs. However, // Windows does use UCS-16, so it is possible something would be misinterpreted as one. We may need to examine // this more. SQLRETURN ret; if (timeout > 0) { Py_BEGIN_ALLOW_THREADS ret = SQLSetConnectAttr(hdbc, SQL_ATTR_LOGIN_TIMEOUT, (SQLPOINTER)(uintptr_t)timeout, SQL_IS_UINTEGER); Py_END_ALLOW_THREADS if (!SQL_SUCCEEDED(ret)) RaiseErrorFromHandle("SQLSetConnectAttr(SQL_ATTR_LOGIN_TIMEOUT)", hdbc, SQL_NULL_HANDLE); }
static Detection_t * read_det_list(PyObject * det_list_obj, int * out_num_det) /* Read a Python list of Detection tuples and return a C * list of Detection_t objects that has to be freed with free_det_list */ { int num_det = (int) PySequence_Length(det_list_obj); Detection_t * det_list = (Detection_t *)malloc(num_det * sizeof(Detection_t)); int detidx; for (detidx = 0; detidx < num_det; detidx ++) { /* new reference */ PyObject * det_obj = PySequence_GetItem(det_list_obj, (Py_ssize_t) detidx); PyArg_ParseTuple(det_obj, "idddd", &det_list[detidx].staidx, &det_list[detidx].time, &det_list[detidx].azimuth, &det_list[detidx].slowness, &det_list[detidx].amp); #ifdef VERYVERBOSE printf("staidx %d time %lf azimuth %lf slowness %lf amp %lf\n", det_list[detidx].staidx, det_list[detidx].time, det_list[detidx].azimuth, det_list[detidx].slowness, det_list[detidx].amp); #endif Py_DECREF(det_obj); } *out_num_det = num_det; return det_list; }
// @object PySPropValueArray|A sequence of <o PySPropValue>, as passed to many MAPI functions. BOOL PyMAPIObject_AsSPropValueArray(PyObject *obs, SPropValue **ppv, ULONG *pcValues) { int seqLen = PySequence_Length(obs); SPropValue *pPV; HRESULT hr; if (S_OK != (hr=MAPIAllocateBuffer(sizeof(SPropValue) * seqLen, (void **)&pPV))) { OleSetOleError(hr); return FALSE; } for (ULONG i=0; i<(ULONG)seqLen; i++) { PyObject *myob = PySequence_GetItem(obs, i); if (myob==NULL) { MAPIFreeBuffer(pPV); return FALSE; } BOOL rc = PyMAPIObject_AsSPropValue(myob, pPV+i, pPV); Py_DECREF(myob); if (!rc) { MAPIFreeBuffer(pPV); return FALSE; } } *pcValues = seqLen; *ppv = pPV; return TRUE; }
static PyObject * Affine_itransform(polypaths_planar_overrideAffineObject *self, PyObject *seq) { Py_ssize_t i; Py_ssize_t len; PyObject *point; polypaths_planar_overrideSeq2Object *varray; double x, y, a, b, c, d, e, f; a = self->a; b = self->b; c = self->c; d = self->d; e = self->e; f = self->f; assert(polypaths_planar_overrideAffine_Check(self)); if (polypaths_planar_overrideSeq2_Check(seq)) { /* Optimized code path for Seq2s */ varray = (polypaths_planar_overrideSeq2Object *)seq; for (i = 0; i < Py_SIZE(seq); i++) { x = varray->vec[i].x; y = varray->vec[i].y; varray->vec[i].x = x*a + y*d + c; varray->vec[i].y = x*b + y*e + f; } } else { /* General vector sequence */ len = PySequence_Length(seq); if (len == -1) { PyErr_SetString(PyExc_TypeError, "Affine.itransform(): Cannot transform non-sequence"); return NULL; } for (i = 0; i < len; i++) { point = PySequence_GetItem(seq, i); if (point == NULL) { return NULL; } if (!polypaths_planar_overrideVec2_Parse(point, &x, &y)) { Py_DECREF(point); PyErr_Format(PyExc_TypeError, "Affine.itransform(): " "Element at position %lu is not a valid vector", i); return NULL; } Py_DECREF(point); point = (PyObject *)polypaths_planar_overrideVec2_FromDoubles(x*a + y*d + c, x*b + y*e + f); if (point == NULL) { return NULL; } if (PySequence_SetItem(seq, i, point) < 0) { Py_DECREF(point); return NULL; } Py_DECREF(point); } } Py_INCREF(Py_None); return Py_None; }
static int set_installonly(_SackObject *self, PyObject *obj, void *unused) { if (!PySequence_Check(obj)) { PyErr_SetString(PyExc_AttributeError, "Expected a sequence."); return -1; } const int len = PySequence_Length(obj); PycompString pStrings[len]; const char *strings[len + 1]; for (int i = 0; i < len; ++i) { UniquePtrPyObject item(PySequence_GetItem(obj, i)); if (PyUnicode_Check(item.get()) || PyString_Check(item.get())) { pStrings[i] = PycompString(item.get()); strings[i] = pStrings[i].getCString(); } else strings[i] = NULL; if (strings[i] == NULL) return -1; } strings[len] = NULL; DnfSack *sack = self->sack; dnf_sack_set_installonly(sack, strings); return 0; }
unsigned char *convert_uchar_array(unsigned char *result,PyObject *input, int size) { int i; if (!PySequence_Check(input)) { printf("Expected a sequence\n"); exit(EXIT_FAILURE); } int length=PySequence_Length(input); if (length > size) { printf("Size mismatch.\n"); exit(EXIT_FAILURE); } // result = (unsigned char *) malloc(size*sizeof(unsigned char)); if(result==NULL) { fprintf(stderr,"Unable to allocate %d bytes.\n",(size*sizeof(unsigned char))); return result; } for (i = 0; i < length; i++) { PyObject *o = PySequence_GetItem(input,i); if (PyNumber_Check(o)) { if(PyFloat_AsDouble(o)>255) result[i] = (unsigned char) 255; else result[i] = (unsigned char) PyFloat_AsDouble(o); } else { PyErr_SetString(PyExc_ValueError,"uchar: Sequence elements must be numbers"); free(result); return NULL; } free(o); } return result; }
PyObject * SKAux_IdIndex(PyObject * self, PyObject * args) { PyObject * list, *obj, *item; int length, i, equal; if (!PyArg_ParseTuple(args, "OO", &list, &obj)) return NULL; if (!PySequence_Check(list)) { PyErr_SetString(PyExc_TypeError, "argument must be a sequence"); return NULL; } length = PySequence_Length(list); for (i = 0; i < length; i++) { item = PySequence_GetItem(list, i); equal = (item == obj); Py_DECREF(item); if (equal) break; } if (i < length) return PyInt_FromLong(i); Py_INCREF(Py_None); return Py_None; }
static PyObject * pixmap_CreateGC(PaxPixmapObject *self, PyObject *args, PyObject * kwargs) { Display *display; Drawable d; unsigned long mask = 0; XGCValues values; GC gc; PyObject * dict; if (PySequence_Length(args) > 0) { if (!PyArg_ParseTuple(args, "O", &dict)) return NULL; } else dict = kwargs; display = self->display; d = self->pixmap; if (dict) { if (!PaxGC_MakeValues(dict, &mask, &values)) return NULL; } gc = XCreateGC(display, d, mask, &values); return PaxGC_FromGC(display, d, gc, PAXGC_OWNED, (PyObject*)self); }
static int parse_filter_chain_spec(lzma_filter filters[], PyObject *filterspecs) { Py_ssize_t i, num_filters; num_filters = PySequence_Length(filterspecs); if (num_filters == -1) return -1; if (num_filters > LZMA_FILTERS_MAX) { PyErr_Format(PyExc_ValueError, "Too many filters - liblzma supports a maximum of %d", LZMA_FILTERS_MAX); return -1; } for (i = 0; i < num_filters; i++) { int ok = 1; PyObject *spec = PySequence_GetItem(filterspecs, i); if (spec == NULL || !lzma_filter_converter(spec, &filters[i])) ok = 0; Py_XDECREF(spec); if (!ok) { filters[i].id = LZMA_VLI_UNKNOWN; free_filter_chain(filters); return -1; } } filters[num_filters].id = LZMA_VLI_UNKNOWN; return 0; }
static PyObject *PSetIDR(PyObject *self, PyObject *args) { int idr, ndr, i; double *pdr; PyObject *p, *q; if (spol_file) { SPOLStatement("SetIDR", args, NULL); Py_INCREF(Py_None); return Py_None; } p = NULL; if (!PyArg_ParseTuple(args, "i|O", &idr, &p)) return NULL; ndr = 0; pdr = NULL; if (p) { if (!PyList_Check(p) && !PyTuple_Check(p)) return NULL; ndr = PySequence_Length(p); pdr = (double *) malloc(sizeof(double)*ndr); for (i = 0; i < ndr; i++) { q = PySequence_GetItem(p, i); pdr[i] = PyFloat_AsDouble(q); Py_DECREF(q); } } if (SetIDR(idr, ndr, pdr) < 0) return NULL; Py_INCREF(Py_None); return Py_None; }
static PyObject* mqttv3_unsubscribeMany(PyObject* self, PyObject *args) { MQTTClient c; PyObject* topicList; int count; char** topics; int i, rc = 0; if (!PyArg_ParseTuple(args, "kOO", &c, &topicList)) return NULL; if (!PySequence_Check(topicList)) { PyErr_SetString(PyExc_TypeError, "3rd parameter must be sequences"); return NULL; } count = PySequence_Length(topicList); topics = malloc(count * sizeof(char*)); for (i = 0; i < count; ++i) topics[i] = PyString_AsString(PySequence_GetItem(topicList, i)); Py_BEGIN_ALLOW_THREADS rc = MQTTClient_unsubscribeMany(c, count, topics); Py_END_ALLOW_THREADS free( topics); return Py_BuildValue("i", rc); }
PyObject * fill_conical_gradient(PyObject * self, PyObject * args) { ImagingObject * image; int x, y, maxx, maxy; int cx, cy; double angle, t; int length; unsigned char *dest; PyObject * list; Gradient gradient; if (!PyArg_ParseTuple(args, "OOiid", &image, &list, &cx, &cy, &angle)) return NULL; if (!PySequence_Check(list)) { PyErr_SetString(PyExc_TypeError, "gradient argument must be a sequence"); return NULL; } length = PySequence_Length(list); gradient = gradient_from_list(list); if (!gradient) return NULL; angle = fmod(angle, 2 * PI); if (angle < -PI) angle += 2 * PI; else if (angle > PI) angle -= 2 * PI; maxx = image->image->xsize - cx; maxy = image->image->ysize - cy; for (y = -cy; y < maxy; y++) { dest = (unsigned char*)(image->image->image32[y + cy]); for (x = -cx; x < maxx; x++, dest += 4) { if (x || y) { t = atan2(y, x) - angle; if (t < -PI) t += 2 * PI; else if (t > PI) t -= 2 * PI; t = fabs(t / PI); } else t = 0; store_gradient_color(gradient, length, t, dest); } } free_gradient(gradient); Py_INCREF(Py_None); return Py_None; }
static int set_installonly(_SackObject *self, PyObject *obj, void *unused) { if (!PySequence_Check(obj)) { PyErr_SetString(PyExc_AttributeError, "Expected a sequence."); return -1; } const int len = PySequence_Length(obj); const char *strings[len + 1]; PyObject *tmp_py_str[len]; for (int i = 0; i < len; ++i) { PyObject *item = PySequence_GetItem(obj, i); tmp_py_str[i] = NULL; strings[i] = NULL; if (PyUnicode_Check(item) || PyString_Check(item)) { strings[i] = pycomp_get_string(item, &tmp_py_str[i]); } Py_DECREF(item); if (strings[i] == NULL) { pycomp_free_tmp_array(tmp_py_str, i); return -1; } } strings[len] = NULL; HifSack *sack = self->sack; hif_sack_set_installonly(sack, strings); pycomp_free_tmp_array(tmp_py_str, len - 1); return 0; }
int run_script(void) { int rc = 0; /* load the code objects to execute */ PyObject *m=NULL, *d=NULL, *seq=NULL; /* We execute then in the context of '__main__' */ m = PyImport_AddModule("__main__"); if (m) d = PyModule_GetDict(m); if (d) seq = PyMarshal_ReadObjectFromString(pScript, numScriptBytes); if (seq) { Py_ssize_t i, max = PySequence_Length(seq); for (i=0;i<max;i++) { PyObject *sub = PySequence_GetItem(seq, i); if (sub /*&& PyCode_Check(sub) */) { PyObject *discard = PyEval_EvalCode((PyCodeObject *)sub, d, d); if (!discard) { PyErr_Print(); rc = 255; } Py_XDECREF(discard); /* keep going even if we fail */ } Py_XDECREF(sub); } } return rc; }
static PyObject * tkwin_GetGC(TkWinObject * self, PyObject* args, PyObject * kwargs) { Display * display; GC gc; unsigned long mask = 0; XGCValues values; PyObject * dict; if (PySequence_Length(args) > 0) { if (!PyArg_ParseTuple(args, "O", &dict)) return NULL; } else dict = kwargs; display = Tk_Display(self->tkwin); if (dict) { if (!PaxGC_MakeValues(dict, &mask, &values)) return NULL; } gc = Tk_GetGC(self->tkwin, mask, &values); return PaxGC_FromGC(display, Tk_WindowId(self->tkwin), gc, PAXGC_SHARED, NULL); }
/* Close all file descriptors in the range start_fd inclusive to * end_fd exclusive except for those in py_fds_to_keep. If the * range defined by [start_fd, end_fd) is large this will take a * long time as it calls close() on EVERY possible fd. */ static void _close_fds_by_brute_force(int start_fd, int end_fd, PyObject *py_fds_to_keep) { Py_ssize_t num_fds_to_keep = PySequence_Length(py_fds_to_keep); Py_ssize_t keep_seq_idx; int fd_num; /* As py_fds_to_keep is sorted we can loop through the list closing * fds inbetween any in the keep list falling within our range. */ for (keep_seq_idx = 0; keep_seq_idx < num_fds_to_keep; ++keep_seq_idx) { PyObject* py_keep_fd = PySequence_Fast_GET_ITEM(py_fds_to_keep, keep_seq_idx); int keep_fd = PyLong_AsLong(py_keep_fd); if (keep_fd < start_fd) continue; for (fd_num = start_fd; fd_num < keep_fd; ++fd_num) { while (close(fd_num) < 0 && errno == EINTR); } start_fd = keep_fd + 1; } if (start_fd <= end_fd) { for (fd_num = start_fd; fd_num < end_fd; ++fd_num) { while (close(fd_num) < 0 && errno == EINTR); } } }
static Station_t * read_sta_list(PyObject * sta_list_obj, int * out_num_sta) /* Read a Python list of Station tuples (name, lon, lat) and return a C * list of Station_t objects that has to be freed with free_sta_list */ { int num_sta = (int) PySequence_Length(sta_list_obj); Station_t * sta_list = (Station_t *)malloc(num_sta * sizeof(Station_t)); int staidx; for (staidx = 0; staidx < num_sta; staidx ++) { /* new reference */ PyObject * sta_obj = PySequence_GetItem(sta_list_obj, (Py_ssize_t) staidx); PyArg_ParseTuple(sta_obj, "sdd", &sta_list[staidx].name, &sta_list[staidx].lon, &sta_list[staidx].lat); #ifdef VERYVERBOSE printf("name %s lon %lf lat %lf\n", sta_list[staidx].name, sta_list[staidx].lon, sta_list[staidx].lat); #endif Py_DECREF(sta_obj); } *out_num_sta = num_sta; return sta_list; }
// narg is a lua table containing a list of defs to add. static PyObject *PyUpb_SymbolTable_add_defs(PyObject *o, PyObject *defs) { upb_symtab *s = Check_SymbolTable(o, NULL); if (!PySequence_Check(defs)) return PyUpb_Error("Must be a sequence"); Py_ssize_t n = PySequence_Length(defs); // Prevent stack overflow. if (n > 2048) return PyUpb_Error("Too many defs"); upb_def *cdefs[n]; int i = 0; for (i = 0; i < n; i++) { PyObject *pydef = PySequence_GetItem(defs, i); upb_def *def = Check_MessageDef(pydef, NULL); cdefs[i++] = def; upb_msgdef *md = upb_dyncast_msgdef(def); if (!md) continue; upb_msg_field_iter j; for(upb_msg_field_begin(&j, md); !upb_msg_field_done(&j); upb_msg_field_next(&j)) { upb_fielddef *f = upb_msg_iter_field(j); upb_fielddef_setaccessor(f, PyUpb_AccessorForField(f)); } upb_msgdef_layout(md); } upb_status status = UPB_STATUS_INIT; upb_symtab_add(s, cdefs, n, &status); PyUpb_CheckStatus(&status); return Py_None; }
// @object PySRestriction| BOOL PyMAPIObject_AsSingleSRestriction(PyObject *ob, SRestriction *pRest, void *pAllocMoreLinkBlock) { if (!PySequence_Check(ob) || PySequence_Length(ob)!=2) { PyErr_SetString(PyExc_TypeError, "The SRestriction object must be a sequence of length 2"); return FALSE; } PyObject *obResType = PySequence_GetItem(ob, 0); if (obResType==NULL) return FALSE; if (!PyInt_Check(obResType)) { PyErr_SetString(PyExc_TypeError, "SRestriction must be a sequence of (integer, object)"); Py_DECREF(obResType); return FALSE; } // @pyparm int|restrictionType||An integer describing the contents of the second parameter. // @pyparm object|restriction||An object in one of the formats describe below. pRest->rt = PyInt_AsLong(obResType); Py_DECREF(obResType); PyObject *subOb = PySequence_GetItem(ob, 1); if (subOb==NULL) return FALSE; // @comm The parameters can be one of the following pairs of values. // @flagh restrictionType|restrictionValue BOOL ok; switch (pRest->rt) { case RES_AND: // @flag RES_AND|<o PySAndRestriction> ok = PyMAPIObject_AsSAndRestriction(subOb, &pRest->res.resAnd, pAllocMoreLinkBlock); break; case RES_OR: // @flag RES_OR|<o PySOrRestriction> ok = PyMAPIObject_AsSOrRestriction(subOb, &pRest->res.resOr, pAllocMoreLinkBlock); break; case RES_PROPERTY: // @flag RES_PROPERTY|<o PySPropertyRestriction> ok = PyMAPIObject_AsSPropertyRestriction(subOb, &pRest->res.resProperty, pAllocMoreLinkBlock); break; case RES_EXIST: // @flag RES_EXIST|<o PySExistRestriction> ok = PyMAPIObject_AsSExistRestriction(subOb, &pRest->res.resExist, pAllocMoreLinkBlock); break; case RES_NOT: // @flag RES_NOT|<o PySNotRestriction> ok = PyMAPIObject_AsSNotRestriction(subOb, &pRest->res.resNot, pAllocMoreLinkBlock); break; case RES_CONTENT: // @flag RES_CONTENT|<o PySContentRestriction> ok = PyMAPIObject_AsSContentRestriction(subOb, &pRest->res.resContent, pAllocMoreLinkBlock); break; case RES_BITMASK: // @flag RES_BITMASK|<o PySBitMaskRestriction> ok = PyMAPIObject_AsSBitMaskRestriction(subOb, &pRest->res.resBitMask, pAllocMoreLinkBlock); break; default: PyErr_SetString(PyExc_ValueError, "Unsupported restriction type! Please fix in mapiutil.cpp!!!"); ok = FALSE; } Py_DECREF(subOb); return ok; }