/* parse an array of vectors */ int mathutils_array_parse_alloc_v(float **array, int array_dim, PyObject *value, const char *error_prefix) { PyObject *value_fast; const int array_dim_flag = array_dim; int i, size; /* non list/tuple cases */ if (!(value_fast = PySequence_Fast(value, error_prefix))) { /* PySequence_Fast sets the error */ return -1; } size = PySequence_Fast_GET_SIZE(value_fast); if (size != 0) { PyObject **value_fast_items = PySequence_Fast_ITEMS(value_fast); float *fp; array_dim &= ~MU_ARRAY_FLAGS; fp = *array = PyMem_Malloc(size * array_dim * sizeof(float)); for (i = 0; i < size; i++, fp += array_dim) { PyObject *item = value_fast_items[i]; if (mathutils_array_parse(fp, array_dim, array_dim_flag, item, error_prefix) == -1) { PyMem_Free(*array); *array = NULL; size = -1; break; } } } Py_DECREF(value_fast); return size; }
static void recursive_store(char* data, IntList sizes, IntList strides, int64_t dim, ScalarType scalarType, int elementSize, PyObject* obj) { int64_t ndim = sizes.size(); if (dim == ndim) { torch::utils::store_scalar(data, scalarType, obj); return; } auto n = sizes[dim]; auto seq = THPObjectPtr(PySequence_Fast(obj, "not a sequence")); if (!seq) throw python_error(); auto seq_size = PySequence_Fast_GET_SIZE(seq.get()); if (seq_size != n) { throw ValueError("expected sequence of length %lld at dim %lld (got %lld)", (long long)n, (long long)dim, (long long)seq_size); } PyObject** items = PySequence_Fast_ITEMS(seq.get()); for (int64_t i = 0; i < n; i++) { recursive_store(data, sizes, strides, dim + 1, scalarType, elementSize, items[i]); data += strides[dim] * elementSize; } }
static PyObject * PositionWeightMatrix_score( PositionWeightMatrix* self, PyObject* args ) { PyObject* seq = parseTupleToFastSequence( args ); if( !seq ) return NULL; double** matrix = self -> matrix; int n = self -> n; int m = self -> m; int len = PySequence_Fast_GET_SIZE( seq ); //check that length is correct if( n != len ) { //TODO raise error return NULL; } PyObject** elems = PySequence_Fast_ITEMS( seq ); double s = score( matrix, n, m, elems ); return PyFloat_FromDouble(s); }
static PyObject *bpy_user_map(PyObject *UNUSED(self), PyObject *args, PyObject *kwds) { #if 0 /* If someone knows how to get a proper 'self' in that case... */ BPy_StructRNA *pyrna = (BPy_StructRNA *)self; Main *bmain = pyrna->ptr.data; #else Main *bmain = G.main; /* XXX Ugly, but should work! */ #endif static const char *kwlist[] = {"subset", "key_types", "value_types", NULL}; PyObject *subset = NULL; PyObject *key_types = NULL; PyObject *val_types = NULL; BLI_bitmap *key_types_bitmap = NULL; BLI_bitmap *val_types_bitmap = NULL; PyObject *ret = NULL; if (!PyArg_ParseTupleAndKeywords( args, kwds, "|O$O!O!:user_map", (char **)kwlist, &subset, &PySet_Type, &key_types, &PySet_Type, &val_types)) { return NULL; } if (key_types) { key_types_bitmap = pyrna_set_to_enum_bitmap( rna_enum_id_type_items, key_types, sizeof(short), true, USHRT_MAX, "key types"); if (key_types_bitmap == NULL) { goto error; } } if (val_types) { val_types_bitmap = pyrna_set_to_enum_bitmap( rna_enum_id_type_items, val_types, sizeof(short), true, USHRT_MAX, "value types"); if (val_types_bitmap == NULL) { goto error; } } IDUserMapData data_cb = {NULL}; if (subset) { PyObject *subset_fast = PySequence_Fast(subset, "user_map"); if (subset_fast == NULL) { goto error; } PyObject **subset_array = PySequence_Fast_ITEMS(subset_fast); Py_ssize_t subset_len = PySequence_Fast_GET_SIZE(subset_fast); data_cb.user_map = _PyDict_NewPresized(subset_len); data_cb.is_subset = true; for (; subset_len; subset_array++, subset_len--) { PyObject *set = PySet_New(NULL); PyDict_SetItem(data_cb.user_map, *subset_array, set); Py_DECREF(set); } Py_DECREF(subset_fast); } else { data_cb.user_map = PyDict_New(); } data_cb.types_bitmap = key_types_bitmap; ListBase *lb_array[MAX_LIBARRAY]; int lb_index; lb_index = set_listbasepointers(bmain, lb_array); while (lb_index--) { if (val_types_bitmap && lb_array[lb_index]->first) { if (!id_check_type(lb_array[lb_index]->first, val_types_bitmap)) { continue; } } for (ID *id = lb_array[lb_index]->first; id; id = id->next) { /* One-time init, ID is just used as placeholder here, we abuse this in iterator callback * to avoid having to rebuild a complete bpyrna object each time for the key searching * (where only ID pointer value is used). */ if (data_cb.py_id_key_lookup_only == NULL) { data_cb.py_id_key_lookup_only = pyrna_id_CreatePyObject(id); } if (!data_cb.is_subset) { PyObject *key = data_cb.py_id_key_lookup_only; PyObject *set; RNA_id_pointer_create(id, &((BPy_StructRNA *)key)->ptr); /* We have to insert the key now, otherwise ID unused would be missing from final dict... */ if ((set = PyDict_GetItem(data_cb.user_map, key)) == NULL) { /* Cannot use our placeholder key here! */ key = pyrna_id_CreatePyObject(id); set = PySet_New(NULL); PyDict_SetItem(data_cb.user_map, key, set); Py_DECREF(set); Py_DECREF(key); } } data_cb.id_curr = id; BKE_library_foreach_ID_link(id, foreach_libblock_id_user_map_callback, &data_cb, IDWALK_NOP); if (data_cb.py_id_curr) { Py_DECREF(data_cb.py_id_curr); data_cb.py_id_curr = NULL; } } } ret = data_cb.user_map; error: Py_XDECREF(data_cb.py_id_key_lookup_only); if (key_types_bitmap) { MEM_freeN(key_types_bitmap); } if (val_types_bitmap) { MEM_freeN(val_types_bitmap); } return ret; }
static PyObject * osk_struts_set (PyObject *self, PyObject *args) { Display *dpy; unsigned long xid; unsigned long struts[12] = { 0, }; PyObject *obj, *seq, **items; int i; if (!PyArg_ParseTuple (args, "kO", &xid, &obj)) return NULL; seq = PySequence_Fast (obj, "expected sequence type"); if (!seq) return NULL; if (PySequence_Fast_GET_SIZE (seq) != 12) { PyErr_SetString (PyExc_ValueError, "expected 12 values"); return NULL; } items = PySequence_Fast_ITEMS (seq); for (i = 0; i < 12; i++, items++) { struts[i] = PyLong_AsUnsignedLongMask (*items); if (PyErr_Occurred ()) { Py_DECREF (seq); return NULL; } if (struts[i] < 0) { PyErr_SetString (PyExc_ValueError, "expected value >= 0"); Py_DECREF (seq); return NULL; } } Py_DECREF (seq); dpy = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()); gdk_error_trap_push (); XChangeProperty (dpy, xid, XInternAtom (dpy, "_NET_WM_STRUT", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4); XChangeProperty (dpy, xid, XInternAtom (dpy, "_NET_WM_STRUT_PARTIAL", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12); gdk_error_trap_pop_ignored (); Py_RETURN_NONE; }
PyObject* mk_disjoint (PyObject* add, PyObject* rm) { // both arguments are [pygame.Rect] int n_rects[2], n_edges[2], i, j, k, l, row0, row1, col0, col1, in_rect, r_i, r_left; PyRectObject** rects[2]; GAME_Rect r; int* edges[2], * grid; PyObject* r_o, * rs; // turn into arrays add = PySequence_Fast(add, "expected list"); // NOTE: ref[+1] rm = PySequence_Fast(rm, "expected list"); // NOTE: ref[+2] n_rects[0] = PySequence_Fast_GET_SIZE(add); n_rects[1] = PySequence_Fast_GET_SIZE(rm); rects[0] = (PyRectObject**) PySequence_Fast_ITEMS(add); rects[1] = (PyRectObject**) PySequence_Fast_ITEMS(rm); // get edges n_edges[0] = 0; n_edges[1] = 0; i = 2 * (n_rects[0] + n_rects[1]); // max number of edges edges[0] = PyMem_New(int, i); edges[1] = PyMem_New(int, i); // NOTE: alloc[+1] for (i = 0; i < 2; i++) { // rects for (j = 0; j < n_rects[i]; j++) { // add|rm r = rects[i][j]->r; n_edges[0] += set_add(edges[0], n_edges[0], r.x); n_edges[0] += set_add(edges[0], n_edges[0], r.x + r.w); n_edges[1] += set_add(edges[1], n_edges[1], r.y); n_edges[1] += set_add(edges[1], n_edges[1], r.y + r.h); } } // sort edges quicksort(edges[0], n_edges[0]); quicksort(edges[1], n_edges[1]); // generate grid of (rows of) subrects and mark contents // each has 2 if add, has no 1 if rm i = (n_edges[0] - 1) * (n_edges[1] - 1); grid = PyMem_New(int, i); // NOTE: alloc[+2] for (j = 0; j < i; j++) grid[j] = 1; for (i = 0; i < 2; i++) { // rects for (j = 0; j < n_rects[i]; j++) { // add|rm r = rects[i][j]->r; if (r.w > 0 && r.h > 0) { row0 = find(edges[1], n_edges[1], r.y, 0); row1 = find(edges[1], n_edges[1], r.y + r.h, row0); col0 = find(edges[0], n_edges[0], r.x, 0); col1 = find(edges[0], n_edges[0], r.x + r.w, col0); for (k = row0; k < row1; k++) { // rows for (l = col0; l < col1; l++) { // cols if (i == 0) // add grid[(n_edges[0] - 1) * k + l] |= 2; else // rm (i == 1) grid[(n_edges[0] - 1) * k + l] ^= 1; } } } } } // generate subrects rs = PyList_New(0); in_rect = 0; for (i = 0; i < n_edges[1] - 1; i++) { // rows for (j = 0; j < n_edges[0] - 1; j++) { // cols if (in_rect && i != r_i) { // on a different row: rect ended in_rect = 0; k = edges[1][r_i]; // NOTE: ref[+3] r_o = PyRect_New4(r_left, k, edges[0][n_edges[0] - 1] - r_left, edges[1][r_i + 1] - k); PyList_Append(rs, r_o); Py_DECREF(r_o); // NOTE: ref[-3] } if (grid[(n_edges[0] - 1) * i + j] == 3) { // add and not rm if (!in_rect) { in_rect = 1; r_i = i; r_left = edges[0][j]; } } else if (in_rect) { // rect ended in_rect = 0; k = edges[1][r_i]; // NOTE: ref[+3] r_o = PyRect_New4(r_left, k, edges[0][j] - r_left, edges[1][r_i + 1] - k); PyList_Append(rs, r_o); Py_DECREF(r_o); // NOTE: ref[-3] } } } if (in_rect) { // last rect ended k = edges[1][r_i]; // NOTE: ref[+3] r_o = PyRect_New4(r_left, k, edges[0][n_edges[0] - 1] - r_left, edges[1][r_i + 1] - k); PyList_Append(rs, r_o); Py_DECREF(r_o); // NOTE: ref[-3] } // cleanup PyMem_Free(grid); // NOTE: alloc[-2] PyMem_Free(edges[0]); PyMem_Free(edges[1]); // NOTE: alloc[-1] Py_DECREF(rm); // NOTE: ref[-2] Py_DECREF(add); // NOTE: ref[-1] return rs; }
PyObject* fastdraw (PyObject* self, PyObject* args) { // don't do much error checking because the point of this is performance // and we own the class calling this; guaranteed to get // [obj], pygame.Surface, {obj: set(Graphic)}, [pygame.Rect] // and layers is sorted PyObject* layers_in, * sfc, * graphics_in, * dirty; PyObject** layers, *** graphics, ** gs, * g, * g_dirty, * g_rect, * r_o, ** graphics_obj, * tmp, * pre_draw, * clip, * dbl_tmp[2], * rtn, * opaque_in, * dirty_opaque, * l_dirty_opaque, ** dirty_by_layer, * rs, * draw_in, * draw; char* dbl[4] = {"was_visible", "visible", "_last_postrot_rect", "_postrot_rect"}; int n_layers, * n_graphics, i, j, k, l, n, n_dirty, r_new, r_good; PyRectObject* r, * tmp_r; if (!PyArg_UnpackTuple(args, "fastdraw", 4, 4, &layers_in, &sfc, &graphics_in, &dirty)) return NULL; pre_draw = PyString_FromString("_pre_draw"); // NOTE: ref[+1a] clip = PyString_FromString("clip"); // NOTE: ref[+1b] // get arrays of layers, graphics and sizes // NOTE: ref[+2] layers_in = PySequence_Fast(layers_in, "layers: expected sequence"); n_layers = PySequence_Fast_GET_SIZE(layers_in); layers = PySequence_Fast_ITEMS(layers_in); graphics_obj = PyMem_New(PyObject*, n_layers); // NOTE: alloc[+1] n_graphics = PyMem_New(int, n_layers); // NOTE: alloc[+2] graphics = PyMem_New(PyObject**, n_layers); // NOTE: alloc[+3] for (i = 0; i < n_layers; i++) { // graphics_in // NOTE: ref[+3] tmp = PySequence_Fast(PyDict_GetItem(graphics_in, layers[i]), "graphics values: expected sequence"); // need to keep it around since graphics references its array graphics_obj[i] = tmp; n_graphics[i] = PySequence_Fast_GET_SIZE(tmp); graphics[i] = PySequence_Fast_ITEMS(tmp); } // get dirty rects from graphics for (i = 0; i < n_layers; i++) { // graphics gs = graphics[i]; for (j = 0; j < n_graphics[i]; j++) { // gs g = gs[j]; PyObject_CallMethodObjArgs(g, pre_draw, NULL); // NOTE: ref[+4] (list) g_dirty = PyObject_GetAttrString(g, "_dirty"); for (k = 0; k < 2; k++) // last/current // NOTE: ref[+5] dbl_tmp[k] = PyObject_GetAttrString(g, dbl[k]); if (dbl_tmp[0] != dbl_tmp[1]) { // visiblity changed since last draw: set dirty everywhere Py_DECREF(g_dirty); // NOTE: ref[-4] g_dirty = PyList_New(1); // NOTE: ref[+4] // NOTE: ref[+6] g_rect = PyObject_GetAttrString(g, "_postrot_rect"); PyList_SET_ITEM(g_dirty, 0, g_rect); // NOTE: ref[-6] PyObject_SetAttrString(g, "_dirty", g_dirty); } n = PyList_GET_SIZE(g_dirty); for (k = 0; k < 2; k++) { // last/current if (dbl_tmp[k] == Py_True) { // NOTE: ref[+6] (pygame.Rect) g_rect = PyObject_GetAttrString(g, dbl[k + 2]); for (l = 0; l < n; l++) { // g_dirty r_o = PyList_GET_ITEM(g_dirty, l); // pygame.Rect // NOTE: ref[+7] r_o = PyObject_CallMethodObjArgs(r_o, clip, g_rect, NULL); PyList_Append(dirty, r_o); Py_DECREF(r_o); // NOTE: ref[-7] } Py_DECREF(g_rect); // NOTE: ref[-6] } } Py_DECREF(dbl_tmp[0]); Py_DECREF(dbl_tmp[1]); // NOTE: ref[-5] Py_DECREF(g_dirty); // NOTE: ref[-4] tmp = PyObject_GetAttrString(g, "visible"); // NOTE: ref[+4] PyObject_SetAttrString(g, "was_visible", tmp); Py_DECREF(tmp); // NOTE: ref[-4] } } // only have something to do if dirty is non-empty rtn = Py_False; Py_INCREF(rtn); // since we're (possibly) returning it n_dirty = PyList_GET_SIZE(dirty); if (PyList_GET_SIZE(dirty) == 0) { goto end; } opaque_in = PyString_FromString("opaque_in"); // NOTE: ref[+4] dirty_opaque = PyList_New(0); // NOTE: ref[+5] dirty_by_layer = PyMem_New(PyObject*, n_layers); // NOTE: alloc[+4] for (i = 0; i < n_layers; i++) { // graphics gs = graphics[i]; n = n_graphics[i]; // get opaque regions of dirty rects l_dirty_opaque = PyList_New(0); // NOTE: ref[+6] for (j = 0; j < n_dirty; j++) { // dirty r = (PyRectObject*) PyList_GET_ITEM(dirty, j); // pygame.Rect r_new = 0; r_good = 1; for (k = 0; k < n; k++) { // gs g = gs[k]; // NOTE: ref[+7] g_rect = PyObject_GetAttrString(g, "_postrot_rect"); if (r_new) tmp_r = r; // NOTE: ref[+8] r = (PyRectObject*) PyObject_CallMethodObjArgs((PyObject*) r, clip, g_rect, NULL); if (r_new) Py_DECREF(tmp_r); // NOTE: ref[-8](k>0) r_new = 1; Py_DECREF(g_rect); // NOTE: ref[-7] // NOTE: ref[+7] tmp = PyObject_CallMethodObjArgs(g, opaque_in, (PyObject*) r, NULL); r_good = r->r.w > 0 && r->r.h > 0 && tmp == Py_True; Py_DECREF(tmp); // NOTE: ref[-7] if (!r_good) break; } if (r_good) PyList_Append(l_dirty_opaque, (PyObject*) r); if (r_new) Py_DECREF((PyObject*) r); // NOTE: ref[-8](k=0) } // undirty below opaque graphics and make dirty rects disjoint // NOTE: ref[+7] dirty_by_layer[i] = mk_disjoint(dirty, dirty_opaque); tmp = dirty_opaque; // NOTE: ref[+8] (not sure why this returns a new reference) dirty_opaque = PySequence_InPlaceConcat(dirty_opaque, l_dirty_opaque); Py_DECREF(tmp); // NOTE: ref[-5] ref[-8+5] Py_DECREF(l_dirty_opaque); // NOTE: ref[-6] ref[-7+6] } draw = PyString_FromString("_draw"); // NOTE: ref[+7] // redraw in dirty rects for (i = n_layers - 1; i >= 0; i--) { // layers rs = dirty_by_layer[i]; n = PyList_GET_SIZE(rs); gs = graphics[i]; for (j = 0; j < n_graphics[i]; j++) { // gs g = gs[j]; tmp = PyObject_GetAttrString(g, "visible"); // NOTE: ref[+8] if (tmp == Py_True) { // NOTE: ref[+9] g_rect = PyObject_GetAttrString(g, "_postrot_rect"); draw_in = PyList_New(0); // NOTE: ref[+10] for (k = 0; k < n; k++) { // rs r = (PyRectObject*) PyList_GET_ITEM(rs, k); // NOTE: ref[+11] r = (PyRectObject*) PyObject_CallMethodObjArgs(g_rect, clip, r, NULL); if (r->r.w > 0 && r->r.h > 0) PyList_Append(draw_in, (PyObject*) r); Py_DECREF(r); // NOTE: ref[-11] } if (PyList_GET_SIZE(draw_in) > 0) { PyObject_CallMethodObjArgs(g, draw, sfc, draw_in, NULL); } Py_DECREF(draw_in); // NOTE: ref[-10] Py_DECREF(g_rect); // NOTE: ref[-9] } Py_DECREF(tmp); // ref[-8] tmp = PyList_New(0); // NOTE: ref[+8] PyObject_SetAttrString(g, "_dirty", tmp); Py_DECREF(tmp); // NOTE: ref[-8] } } // add up dirty rects to return Py_DECREF(rtn); rtn = PyList_New(0); for (i = 0; i < n_layers; i++) { // dirty_by_layer tmp = rtn; // NOTE: ref[+8] (not sure why this returns a new reference) rtn = PySequence_InPlaceConcat(rtn, dirty_by_layer[i]); Py_DECREF(tmp); // NOTE: ref[-8] } // cleanup (in reverse order) Py_DECREF(draw); // NOTE: ref[-7] // NOTE: ref[-6] for (i = 0; i < n_layers; i++) Py_DECREF(dirty_by_layer[i]); PyMem_Free(dirty_by_layer); // NOTE: alloc[-4] Py_DECREF(dirty_opaque); // NOTE: ref[-5] Py_DECREF(opaque_in); // NOTE: ref[-4] end: for (i = 0; i < n_layers; i++) Py_DECREF(graphics_obj[i]); // NOTE: ref[-3] PyMem_Free(graphics); // NOTE: alloc[-3] PyMem_Free(n_graphics); // NOTE: alloc[-2] PyMem_Free(graphics_obj); // NOTE: alloc[-1] Py_DECREF(layers_in); // NOTE: ref[-2] Py_DECREF(clip); // NOTE: ref[-1b] Py_DECREF(pre_draw); // NOTE: ref[-1a] return rtn; }
/* This function is based on list_ass_subscript from Python source listobject.c. But it uses LDAPValueList's setslice and setitem functions instead of memory opertation. It is probably a much more slower, but cleaner solution. */ static int LVL_ass_subscript(LDAPValueList *self, PyObject *item, PyObject *value) { size_t cur; Py_ssize_t i; Py_ssize_t start, stop, step, slicelength; PyObject *seq; PyObject **seqitems; if (PyIndex_Check(item)) { i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i == -1 && PyErr_Occurred()) return -1; if (i < 0) i += PyList_GET_SIZE(self); return LVL_ass_item(self, i, value); } else if (PySlice_Check(item)) { if (PySlice_GetIndicesEx(item, Py_SIZE(self), &start, &stop, &step, &slicelength) < 0) { return -1; } if (step == 1) return LDAPValueList_SetSlice(self, start, stop, value); /* Make sure s[5:2] = [..] inserts at the right place: before 5, not before 2. */ if ((step < 0 && start < stop) || (step > 0 && start > stop)) { stop = start; } if (value == NULL) { /* delete slice */ if (slicelength <= 0) return 0; if (step < 0) { stop = start + 1; start = stop + step*(slicelength - 1) - 1; step = -step; } for (cur = start, i = 0; cur < (size_t)stop; cur += step, i++) { if (LDAPValueList_SetSlice(self, cur-i, cur+1-i, (PyObject *)NULL) != 0) { return -1; } } return 0; } else { /* assign slice */ /* protect against a[::-1] = a */ if (self == (LDAPValueList*)value) { seq = PyList_GetSlice(value, 0, PyList_GET_SIZE(value)); } else { seq = PySequence_Fast(value, "must assign iterable to extended slice"); } if (!seq) return -1; if (PySequence_Fast_GET_SIZE(seq) != slicelength) { PyErr_Format(PyExc_ValueError, "attempt to assign sequence of size %zd to extended slice of " "size %zd", PySequence_Fast_GET_SIZE(seq), slicelength); Py_DECREF(seq); return -1; } if (!slicelength) { Py_DECREF(seq); return 0; } seqitems = PySequence_Fast_ITEMS(seq); for (cur = start, i = 0; i < slicelength; cur += (size_t)step, i++) { if (LDAPValueList_SetItem(self, cur, seqitems[i]) != 0) { return -1; } } Py_DECREF(seq); return 0; } } else { PyErr_Format(PyExc_TypeError, "list indices must be integers, not %.200s", item->ob_type->tp_name); return -1; } }
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; Py_buffer buffer_view; /* types for sequence handling */ PyObject ** objects; PyObject * seq; PyTypeObject * common_type; /* Check if it's an ndarray */ if (PyArray_Check(obj)) { dtype = PyArray_DESCR((PyArrayObject *)obj); Py_INCREF(dtype); goto promote_types; } /* See if it's a python None */ if (obj == Py_None) { dtype = PyArray_DescrFromType(NPY_OBJECT); if (dtype == NULL) { goto fail; } Py_INCREF(dtype); goto promote_types; } /* Check if it's a NumPy scalar */ else 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; } /* PEP 3118 buffer interface */ if (PyObject_CheckBuffer(obj) == 1) { 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(); } } /* The array interface */ ip = PyArray_GetAttrString_SuppressException(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); } /* The array struct interface */ ip = PyArray_GetAttrString_SuppressException(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); } /* 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 */ ip = PyArray_GetAttrString_SuppressException(obj, "__array__"); if (ip != NULL) { Py_DECREF(ip); 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; } /* * fails if convertable to list but no len is defined which some libraries * require to get object arrays */ size = PySequence_Size(obj); if (size < 0) { goto fail; } /* Recursive case, first check the sequence contains only one type */ seq = PySequence_Fast(obj, "Could not convert object to sequence"); if (seq == NULL) { goto fail; } objects = PySequence_Fast_ITEMS(seq); common_type = size > 0 ? Py_TYPE(objects[0]) : NULL; for (i = 1; i < size; ++i) { if (Py_TYPE(objects[i]) != common_type) { common_type = NULL; break; } } /* all types are the same and scalar, one recursive call is enough */ if (common_type != NULL && !string_type && (common_type == &PyFloat_Type || /* TODO: we could add longs if we add a range check */ #if !defined(NPY_PY3K) common_type == &PyInt_Type || #endif common_type == &PyBool_Type || common_type == &PyComplex_Type)) { size = 1; } /* Recursive call for each sequence item */ for (i = 0; i < size; ++i) { int res = PyArray_DTypeFromObjectHelper(objects[i], maxdims - 1, out_dtype, string_type); if (res < 0) { Py_DECREF(seq); goto fail; } else if (res > 0) { Py_DECREF(seq); return res; } } Py_DECREF(seq); 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 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, NULL) < 0) return NULL; if (i == 0) { return PyArray_DATA(mp); } return PyArray_BYTES(mp)+i*PyArray_STRIDES(mp)[0]; }
static IDProperty *idp_from_PySequence_Fast(const char *name, PyObject *ob) { IDProperty *prop; IDPropertyTemplate val = {0}; PyObject **ob_seq_fast_items; PyObject *item; int i; ob_seq_fast_items = PySequence_Fast_ITEMS(ob); if ((val.array.type = idp_sequence_type(ob)) == (char)-1) { PyErr_SetString(PyExc_TypeError, "only floats, ints and dicts are allowed in ID property arrays"); return NULL; } /* validate sequence and derive type. * we assume IDP_INT unless we hit a float * number; then we assume it's */ val.array.len = PySequence_Fast_GET_SIZE(ob); switch (val.array.type) { case IDP_DOUBLE: { double *prop_data; prop = IDP_New(IDP_ARRAY, &val, name); prop_data = IDP_Array(prop); for (i = 0; i < val.array.len; i++) { item = ob_seq_fast_items[i]; if (((prop_data[i] = PyFloat_AsDouble(item)) == -1.0) && PyErr_Occurred()) { return NULL; } } break; } case IDP_INT: { int *prop_data; prop = IDP_New(IDP_ARRAY, &val, name); prop_data = IDP_Array(prop); for (i = 0; i < val.array.len; i++) { item = ob_seq_fast_items[i]; if (((prop_data[i] = PyC_Long_AsI32(item)) == -1) && PyErr_Occurred()) { return NULL; } } break; } case IDP_IDPARRAY: { prop = IDP_NewIDPArray(name); for (i = 0; i < val.array.len; i++) { item = ob_seq_fast_items[i]; if (BPy_IDProperty_Map_ValidateAndCreate(NULL, prop, item) == false) { return NULL; } } break; } default: /* should never happen */ PyErr_SetString(PyExc_RuntimeError, "internal error with idp array.type"); return NULL; } return prop; }
int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *result) { PyGILState_STATE gilstate; bool use_gil = !PyC_IsInterpreterActive(); PyObject *pyctx; PyObject *item; PointerRNA *ptr = NULL; bool done = false; if (use_gil) gilstate = PyGILState_Ensure(); pyctx = (PyObject *)CTX_py_dict_get(C); item = PyDict_GetItemString(pyctx, member); if (item == NULL) { /* pass */ } else if (item == Py_None) { done = true; } else if (BPy_StructRNA_Check(item)) { ptr = &(((BPy_StructRNA *)item)->ptr); //result->ptr = ((BPy_StructRNA *)item)->ptr; CTX_data_pointer_set(result, ptr->id.data, ptr->type, ptr->data); CTX_data_type_set(result, CTX_DATA_TYPE_POINTER); done = true; } else if (PySequence_Check(item)) { PyObject *seq_fast = PySequence_Fast(item, "bpy_context_get sequence conversion"); if (seq_fast == NULL) { PyErr_Print(); PyErr_Clear(); } else { int len = PySequence_Fast_GET_SIZE(seq_fast); PyObject **seq_fast_items = PySequence_Fast_ITEMS(seq_fast); int i; for (i = 0; i < len; i++) { PyObject *list_item = seq_fast_items[i]; if (BPy_StructRNA_Check(list_item)) { #if 0 CollectionPointerLink *link = MEM_callocN(sizeof(CollectionPointerLink), "bpy_context_get"); link->ptr = ((BPy_StructRNA *)item)->ptr; BLI_addtail(&result->list, link); #endif ptr = &(((BPy_StructRNA *)list_item)->ptr); CTX_data_list_add(result, ptr->id.data, ptr->type, ptr->data); } else { printf("PyContext: '%s' list item not a valid type in sequece type '%s'\n", member, Py_TYPE(item)->tp_name); } } Py_DECREF(seq_fast); CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); done = true; } } if (done == false) { if (item) { printf("PyContext '%s' not a valid type\n", member); } else { printf("PyContext '%s' not found\n", member); } } else { if (G.debug & G_DEBUG_PYTHON) { printf("PyContext '%s' found\n", member); } } if (use_gil) PyGILState_Release(gilstate); return done; }
/* * For each positional argument and each argument in a possible "out" * keyword, look for overrides of the standard ufunc behaviour, i.e., * non-default __array_ufunc__ methods. * * Returns the number of overrides, setting corresponding objects * in PyObject array ``with_override`` and the corresponding * __array_ufunc__ methods in ``methods`` (both using new references). * * Only the first override for a given class is returned. * * returns -1 on failure. */ NPY_NO_EXPORT int PyUFunc_WithOverride(PyObject *args, PyObject *kwds, PyObject **with_override, PyObject **methods) { int i; int num_override_args = 0; int narg, nout = 0; PyObject *out_kwd_obj; PyObject **arg_objs, **out_objs; narg = PyTuple_Size(args); if (narg < 0) { return -1; } arg_objs = PySequence_Fast_ITEMS(args); nout = get_out_objects(kwds, &out_kwd_obj, &out_objs); if (nout < 0) { return -1; } for (i = 0; i < narg + nout; ++i) { PyObject *obj; int j; int new_class = 1; if (i < narg) { obj = arg_objs[i]; } else { obj = out_objs[i - narg]; } /* * Have we seen this class before? If so, ignore. */ for (j = 0; j < num_override_args; j++) { new_class = (Py_TYPE(obj) != Py_TYPE(with_override[j])); if (!new_class) { break; } } if (new_class) { /* * Now see if the object provides an __array_ufunc__. However, we should * ignore the base ndarray.__ufunc__, so we skip any ndarray as well as * any ndarray subclass instances that did not override __array_ufunc__. */ PyObject *method = get_non_default_array_ufunc(obj); if (method == NULL) { continue; } if (method == Py_None) { PyErr_Format(PyExc_TypeError, "operand '%.200s' does not support ufuncs " "(__array_ufunc__=None)", obj->ob_type->tp_name); Py_DECREF(method); goto fail; } Py_INCREF(obj); with_override[num_override_args] = obj; methods[num_override_args] = method; ++num_override_args; } } return num_override_args; fail: for (i = 0; i < num_override_args; i++) { Py_DECREF(with_override[i]); Py_DECREF(methods[i]); } return -1; }
int ObjectRow_PyObject__init(ObjectRow_PyObject *self, PyObject *args, PyObject *kwargs) { PyObject *pytmp, *pydesc, *cursor, *row, *o_type, *pickle_dict = 0; struct module_state *mstate; if (!PyArg_ParseTuple(args, "OO|O!", &cursor, &row, &PyDict_Type, &pickle_dict)) return -1; mstate = GETSTATE_FROMTYPE(self); if (pickle_dict) { /* If row or cursor weren't specified, then we require the third arg * (pickle_dict) be a dictionary, and we basically behave as this dict. * We do this for example from Database.add() */ self->pickle = pickle_dict; Py_INCREF(self->pickle); self->row = Py_None; Py_INCREF(self->row); self->desc = Py_None; Py_INCREF(self->desc); return 0; } /* First argument is the db cursor from which we fetch the row description * and object types. Or, it is a 2-tuple of same. */ if (PyTuple_Check(cursor)) { self->desc = PySequence_GetItem(cursor, 0); // new ref self->object_types = PySequence_GetItem(cursor, 1); // new ref } else if (!PyObject_HasAttrString(cursor, "_db")) { PyErr_Format(PyExc_ValueError, "First argument is not a Cursor or tuple object"); return -1; } else { PyObject *weak_db = PyObject_GetAttrString(cursor, "_db"); // new ref PyObject *db = PyWeakref_GetObject(weak_db); // borrowed ref self->object_types = PyObject_GetAttrString(db, "_object_types"); // new ref self->desc = PyObject_GetAttrString(cursor, "description"); // new ref Py_XDECREF(weak_db); } self->row = row; self->type_name = PySequence_GetItem(row, 0); // new ref if (!PyString_Check(self->type_name) && !PyUnicode_Check(self->type_name)) { Py_XDECREF(self->desc); Py_XDECREF(self->object_types); PyErr_Format(PyExc_ValueError, "First element of row must be object type"); return -1; } o_type = PyDict_GetItem(self->object_types, self->type_name); // borrowed ref self->attrs = PySequence_GetItem(o_type, 1); // new ref if (!self->attrs) { char *type_name; #if PY_MAJOR_VERSION >= 3 PyObject *bytes = PyUnicode_AsUTF8String(self->type_name); type_name = strdup(PyBytes_AS_STRING(bytes)); Py_DECREF(bytes); #else type_name = strdup(PyString_AsString(self->type_name)); #endif PyErr_Format(PyExc_ValueError, "Object type '%s' not defined.", type_name); free(type_name); Py_XDECREF(self->desc); Py_XDECREF(self->object_types); return -1; } /* For the queries dict key we use the address of the desc object rather * than the desc itself. desc is a tuple, and if we use it as a key it * will result in a hash() on the desc for each row which is much more * expensive. pysqlite passes us the same description tuple object for * each row in a query so it is safe to use the address. */ pydesc = PyLong_FromVoidPtr(self->desc); pytmp = PyDict_GetItem(mstate->queries, pydesc); self->query_info = pytmp ? (QueryInfo *)PyCObject_AsVoidPtr(pytmp) : NULL; if (!self->query_info) { /* This is a row for a query we haven't seen before, so we need to do * some initial setup. Most of what we do here is convert row and * attribute metadata into convenient data structures for later access. */ PyObject **desc_tuple = PySequence_Fast_ITEMS(self->desc); PyObject *key, *value; int i = 0; Py_ssize_t pos = 0; self->query_info = (QueryInfo *)malloc(sizeof(QueryInfo)); self->query_info->refcount = 0; self->query_info->pickle_idx = -1; self->query_info->idxmap = PyDict_New(); /* Iterate over the columns from the SQL query and keep track of * attribute names and their indexes within the row tuple. Start at * index 2 because index 0 and 1 are internal (the object type name * literal and type id). */ for (i = 2; i < PySequence_Length(self->desc); i++) { PyObject **desc_col = PySequence_Fast_ITEMS(desc_tuple[i]); ObjectAttribute *attr = (ObjectAttribute *)malloc(sizeof(ObjectAttribute)); attr->pickled = 0; attr->index = i; if (PyStr_Compare(desc_col[0], "pickle") == 0) self->query_info->pickle_idx = i; pytmp = PyCObject_FromVoidPtr(attr, free); PyDict_SetItem(self->query_info->idxmap, desc_col[0], pytmp); Py_DECREF(pytmp); } /* Now iterate over the kaa.db object attribute dict, storing the * type of each attribute, its flags, and figure out whether or not * we need to look in the pickle for that attribute. */ while (PyDict_Next(self->attrs, &pos, &key, &value)) { pytmp = PyDict_GetItem(self->query_info->idxmap, key); ObjectAttribute *attr = pytmp ? (ObjectAttribute *)PyCObject_AsVoidPtr(pytmp) : NULL; if (!attr) { attr = (ObjectAttribute *)malloc(sizeof(ObjectAttribute)); attr->index = -1; pytmp = PyCObject_FromVoidPtr(attr, free); PyDict_SetItem(self->query_info->idxmap, key, pytmp); Py_DECREF(pytmp); } attr->type = PySequence_Fast_GET_ITEM(value, 0); attr->flags = PyInt_AsLong(PySequence_Fast_GET_ITEM(value, 1)); attr->named_ivtidx = PyObject_RichCompareBool(PySequence_Fast_GET_ITEM(value, 2), key, Py_EQ) == 1; if (IS_ATTR_INDEXED_IGNORE_CASE(attr->flags) || attr->flags & ATTR_SIMPLE) // attribute is set to ignore case, or it's ATTR_SIMPLE, so we // need to look in the pickle for this attribute. attr->pickled = 1; else attr->pickled = 0; } /* Create a hash table that maps object type ids to type names. */ pos = 0; self->query_info->type_names = PyDict_New(); while (PyDict_Next(self->object_types, &pos, &key, &value)) { PyObject *type_id = PySequence_Fast_GET_ITEM(value, 0); PyDict_SetItem(self->query_info->type_names, type_id, key); } pytmp = PyCObject_FromVoidPtr(self->query_info, NULL); PyDict_SetItem(mstate->queries, pydesc, pytmp); Py_DECREF(pytmp); } Py_DECREF(pydesc); self->query_info->refcount++; if (self->query_info->pickle_idx >= 0) { // Pickle column included in row. Set _pickle member to True which // indicates the pickle data was fetched, but just hasn't yet been // unpickled. if (PySequence_Fast_GET_ITEM(self->row, self->query_info->pickle_idx) != Py_None) self->has_pickle = 1; self->pickle = Py_True; } else self->pickle = Py_False; Py_INCREF(self->pickle); Py_INCREF(self->row); if (pickle_dict && pickle_dict != Py_None) { Py_DECREF(self->pickle); self->pickle = pickle_dict; Py_INCREF(self->pickle); self->has_pickle = self->unpickled = 1; } return 0; }
static PyObject* PositionWeightMatrix_nullScoreDistribution( PositionWeightMatrix* self, PyObject* args ) { PyObject* seq; int reps; if( !PyArg_ParseTuple( args, "Oi", &seq, &reps ) ) return NULL; seq = PySequence_Fast( seq, "argument must be iterable" ); if( !seq ) return NULL; PyObject** elems = PySequence_Fast_ITEMS( seq ); size_t len = PySequence_Fast_GET_SIZE( seq ); //allocate array to put elems in long* array = malloc( len * sizeof(long) ); size_t i; for( i = 0 ; i < len ; ++i ) { array[i] = PyInt_AsLong( elems[i] ); } //check for error if( PyErr_Occurred() ) { free( array ); return NULL; } double** matrix = self->matrix; int n = self->n; int m = self->m; //allocate scores array size_t nmers = ( len - n + 1 ); size_t lenScores = nmers*reps; double* scores = malloc( lenScores * sizeof( double ) ); //for each replicate, shuffle and score the sequence int rep = 0; while( rep < reps ) { shuffle( array, len, sizeof( long ) ); scoreAllLong( matrix, n, m, array, len, scores, nmers * rep ); ++rep; } //sort the scores quicksort( scores, lenScores, sizeof( double ), &compareDoubleDescending ); //free the long array free( array ); //build a python list to return PyObject* list = PyList_New( lenScores ); for( i = 0 ; i < lenScores ; ++i ) { PyList_SetItem( list, i, PyFloat_FromDouble( scores[i] ) ); } //free the scores array free( scores ); return list; }