static PyObject * describe_plugin(PyObject * mod, PyObject * args) { char * libname = NULL; int plug_id; int port_num; void * dlfile; LADSPA_Descriptor * (* descrip_func)(unsigned long); LADSPA_Descriptor * descrip; PyObject * dict = NULL; PyObject * ports = NULL; PyObject * portinfo = NULL; PyArg_ParseTuple(args, "si", &libname, &plug_id); if (libname == NULL) { Py_INCREF(Py_None); return Py_None; } dlfile = dlopen(libname, RTLD_NOW); if (dlfile == NULL) { Py_INCREF(Py_None); return Py_None; } descrip_func = dlsym(dlfile, "ladspa_descriptor"); descrip = descrip_func(plug_id); if (descrip == NULL) { Py_INCREF(Py_None); return Py_None; } dict = PyDict_New(); PyDict_SetItemString(dict, "lib_name", PyString_FromString(libname)); PyDict_SetItemString(dict, "lib_type", PyString_FromString("ladspa")); PyDict_SetItemString(dict, "lib_index", PyInt_FromLong(plug_id)); PyDict_SetItemString(dict, "unique_id", PyInt_FromLong(descrip->UniqueID)); PyDict_SetItemString(dict, "properties", PyInt_FromLong(descrip->Properties)); PyDict_SetItemString(dict, "label", PyString_FromString(descrip->Label)); PyDict_SetItemString(dict, "name", PyString_FromString(descrip->Name)); PyDict_SetItemString(dict, "maker", PyString_FromString(descrip->Maker)); PyDict_SetItemString(dict, "copyright", PyString_FromString(descrip->Copyright)); ports = PyList_New(descrip->PortCount); for(port_num=0; port_num < descrip->PortCount; port_num++) { portinfo = PyDict_New(); PyDict_SetItemString(portinfo, "descriptor", PyInt_FromLong(descrip->PortDescriptors[port_num])); PyDict_SetItemString(portinfo, "name", PyString_FromString(descrip->PortNames[port_num])); PyDict_SetItemString(portinfo, "hint_type", PyInt_FromLong(descrip->PortRangeHints[port_num].HintDescriptor)); PyDict_SetItemString(portinfo, "hint_lower", PyFloat_FromDouble(descrip->PortRangeHints[port_num].LowerBound)); PyDict_SetItemString(portinfo, "hint_upper", PyFloat_FromDouble(descrip->PortRangeHints[port_num].UpperBound)); PyList_SetItem(ports, port_num, portinfo); } PyDict_SetItemString(dict, "ports", ports); dlclose(dlfile); Py_INCREF(dict); return dict; }
JSOBJ Object_newDouble(double value) { return PyFloat_FromDouble(value); }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::seq_slice(PyObject* self, Py_ssize_t startIndex, Py_ssize_t endIndex) { if(startIndex < 0) startIndex = 0; if(endIndex > VECTOR_SIZE) endIndex = VECTOR_SIZE; if(endIndex < startIndex) endIndex = startIndex; ScriptVector3* sv = static_cast<ScriptVector3*>(self); Vector3& my_v = sv->getVector(); PyObject* pyResult = NULL; int length = (int)(endIndex - startIndex); if (length == VECTOR_SIZE) { pyResult = sv; Py_INCREF(pyResult); } else switch(length) { case 0: pyResult = PyTuple_New(0); break; case 1: pyResult = PyTuple_New(1); PyTuple_SET_ITEM(pyResult, 0, PyFloat_FromDouble(sv->getVector()[static_cast<int>(startIndex)])); break; case 2: { Vector2 v; for (int i = (int)startIndex; i < (int)endIndex; ++i){ v[i - static_cast<int>(startIndex)] = my_v[i]; } pyResult = new ScriptVector2(v); break; } case 3: { Vector3 v; for (int i = (int)startIndex; i < (int)endIndex; ++i){ v[i - static_cast<int>(startIndex)] = my_v[i]; } pyResult = new ScriptVector3(v); break; } default: PyErr_Format(PyExc_IndexError, "Bad slice indexes [%d, %d] for Vector%d", startIndex, endIndex, VECTOR_SIZE); PyErr_PrintEx(0); break; } return pyResult; }
static PyObject* CameraInfo_GetFocalLength(CameraInfoObj* self, void* closure) { return PyFloat_FromDouble(self->instance.flen); }
/** * Set one or more options in a decoder instance. * * Handled options are removed from the hash. * * @param di Decoder instance. * @param options A GHashTable of options to set. * * @return SRD_OK upon success, a (negative) error code otherwise. * * @since 0.1.0 */ SRD_API int srd_inst_option_set(struct srd_decoder_inst *di, GHashTable *options) { struct srd_decoder_option *sdo; PyObject *py_di_options, *py_optval; GVariant *value; GSList *l; double val_double; gint64 val_int; int ret; const char *val_str; if (!di) { srd_err("Invalid decoder instance."); return SRD_ERR_ARG; } if (!options) { srd_err("Invalid options GHashTable."); return SRD_ERR_ARG; } if (!PyObject_HasAttrString(di->decoder->py_dec, "options")) { /* Decoder has no options. */ if (g_hash_table_size(options) == 0) { /* No options provided. */ return SRD_OK; } else { srd_err("Protocol decoder has no options."); return SRD_ERR_ARG; } return SRD_OK; } ret = SRD_ERR_PYTHON; py_optval = NULL; /* * The 'options' tuple is a class variable, but we need to * change it. Changing it directly will affect the entire class, * so we need to create a new object for it, and populate that * instead. */ if (!(py_di_options = PyObject_GetAttrString(di->py_inst, "options"))) goto err_out; Py_DECREF(py_di_options); py_di_options = PyDict_New(); PyObject_SetAttrString(di->py_inst, "options", py_di_options); for (l = di->decoder->options; l; l = l->next) { sdo = l->data; if ((value = g_hash_table_lookup(options, sdo->id))) { /* A value was supplied for this option. */ if (!g_variant_type_equal(g_variant_get_type(value), g_variant_get_type(sdo->def))) { srd_err("Option '%s' should have the same type " "as the default value.", sdo->id); goto err_out; } } else { /* Use default for this option. */ value = sdo->def; } if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) { val_str = g_variant_get_string(value, NULL); if (!(py_optval = PyUnicode_FromString(val_str))) { /* Some UTF-8 encoding error. */ PyErr_Clear(); srd_err("Option '%s' requires a UTF-8 string value.", sdo->id); goto err_out; } } else if (g_variant_is_of_type(value, G_VARIANT_TYPE_INT64)) { val_int = g_variant_get_int64(value); if (!(py_optval = PyLong_FromLong(val_int))) { /* ValueError Exception */ PyErr_Clear(); srd_err("Option '%s' has invalid integer value.", sdo->id); goto err_out; } } else if (g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE)) { val_double = g_variant_get_double(value); if (!(py_optval = PyFloat_FromDouble(val_double))) { /* ValueError Exception */ PyErr_Clear(); srd_err("Option '%s' has invalid float value.", sdo->id); goto err_out; } } if (PyDict_SetItemString(py_di_options, sdo->id, py_optval) == -1) goto err_out; /* Not harmful even if we used the default. */ g_hash_table_remove(options, sdo->id); } if (g_hash_table_size(options) != 0) srd_warn("Unknown options specified for '%s'", di->inst_id); ret = SRD_OK; err_out: Py_XDECREF(py_optval); if (PyErr_Occurred()) { srd_exception_catch("Stray exception in srd_inst_option_set()"); ret = SRD_ERR_PYTHON; } return ret; }
static PyObject* pyAffineParts_getF(pyAffineParts* self, void*) { return PyFloat_FromDouble(self->fThis->fF); }
PyObject *PyMAPIObject_FromSPropValue(SPropValue *pv) { PyObject *val; ULONG i; switch (PROP_TYPE(pv->ulPropTag)) { case PT_I2: // case PT_SHORT: val = PyInt_FromLong(pv->Value.i); break; case PT_I4: // case PT_LONG: val = PyInt_FromLong(pv->Value.l); break; case PT_R4: // case PT_FLOAT: val = PyFloat_FromDouble(pv->Value.flt); break; case PT_R8: // case PT_DOUBLE: val = PyFloat_FromDouble(pv->Value.dbl); break; case PT_BOOLEAN: val = pv->Value.b ? Py_True : Py_False; Py_INCREF(val); break; /* case PT_CURRENCY: pv->Value.cur?? break; */ case PT_APPTIME : val = PyWinObject_FromDATE(pv->Value.at); break; case PT_SYSTIME: val = PyWinObject_FromFILETIME(pv->Value.ft); break; case PT_STRING8: val = PyString_FromString(pv->Value.lpszA); break; case PT_UNICODE: val = PyWinObject_FromWCHAR(pv->Value.lpszW); break; case PT_BINARY: val = PyString_FromStringAndSize((char *)pv->Value.bin.lpb, pv->Value.bin.cb); break; case PT_CLSID: val = PyWinObject_FromIID(*pv->Value.lpguid); break; case PT_I8: // case PT_LONGLONG: val = PyWinObject_FromLARGE_INTEGER(pv->Value.li); break; case PT_ERROR: val = PyInt_FromLong(pv->Value.err); break; case PT_NULL: val = Py_None; Py_INCREF(Py_None); break; case PT_MV_I2: val = PyTuple_New(pv->Value.MVi.cValues); if (val) { for (i=0;i<pv->Value.MVi.cValues;i++) PyTuple_SET_ITEM(val, i, PyInt_FromLong(pv->Value.MVi.lpi[i])); } break; case PT_MV_LONG: val = PyTuple_New(pv->Value.MVi.cValues); if (val) { for (i=0;i<pv->Value.MVl.cValues;i++) PyTuple_SET_ITEM(val, i, PyInt_FromLong(pv->Value.MVl.lpl[i])); } break; case PT_MV_R4: val = PyTuple_New(pv->Value.MVflt.cValues); if (val) { for (i=0;i<pv->Value.MVflt.cValues;i++) PyTuple_SET_ITEM(val, i, PyFloat_FromDouble(pv->Value.MVflt.lpflt[i])); } break; case PT_MV_DOUBLE : val = PyTuple_New(pv->Value.MVdbl.cValues); if (val) { for (i=0;i<pv->Value.MVdbl.cValues;i++) PyTuple_SET_ITEM(val, i, PyFloat_FromDouble(pv->Value.MVdbl.lpdbl[i])); } break; /* case PT_MV_CURRENCY: MVcur SCurrencyArray */ case PT_MV_APPTIME : val = PyTuple_New(pv->Value.MVat.cValues); if (val) { for (i=0;i<pv->Value.MVat.cValues;i++) PyTuple_SET_ITEM(val, i, PyWinObject_FromDATE(pv->Value.MVat.lpat[i])); } break; case PT_MV_SYSTIME: val = PyTuple_New(pv->Value.MVft.cValues); if (val) { for (i=0;i<pv->Value.MVft.cValues;i++) PyTuple_SET_ITEM(val, i, PyWinObject_FromFILETIME(pv->Value.MVft.lpft[i])); } break; case PT_MV_BINARY: val = PyTuple_New(pv->Value.MVbin.cValues); if (val) { for (i=0;i<pv->Value.MVbin.cValues;i++) PyTuple_SET_ITEM(val, i, PyString_FromStringAndSize((char *)pv->Value.MVbin.lpbin[i].lpb, pv->Value.MVbin.lpbin[i].cb)); } break; case PT_MV_STRING8: val = PyTuple_New(pv->Value.MVszA.cValues); if (val) { for (i=0;i<pv->Value.MVszA.cValues;i++) PyTuple_SET_ITEM(val, i, PyString_FromString(pv->Value.MVszA.lppszA[i])); } break; case PT_MV_UNICODE: val = PyTuple_New(pv->Value.MVszW.cValues); if (val) { for (i=0;i<pv->Value.MVszW.cValues;i++) PyTuple_SET_ITEM(val, i, PyWinObject_FromWCHAR(pv->Value.MVszW.lppszW[i])); } break; /* case PT_MV_CLSID: MVguid SGuidArray case PT_MV_I8: MVli SLargeIntegerArray */ case PT_OBJECT: val = PyInt_FromLong(pv->Value.x); break; default: printf("File %s: Unsupported MAPI property type 0x%X", __FILE__, PROP_TYPE(pv->ulPropTag)); /* Dont set exception, as this prevents otherwise valid props from being returned */ val = Py_None; Py_INCREF(Py_None); break; } PyObject *rc = PyTuple_New(2); if (rc==NULL) { Py_DECREF(val); PyErr_SetString(PyExc_MemoryError, "Tuple(2) for PROP result"); return NULL; } PyTuple_SET_ITEM(rc, 0, PyLong_FromUnsignedLong(pv->ulPropTag)); PyTuple_SET_ITEM(rc, 1, val); return rc; }
PyObject* PythonQtConv::ConvertQtValueToPythonInternal(int type, const void* data) { switch (type) { case QMetaType::Void: Py_INCREF(Py_None); return Py_None; case QMetaType::Char: return PyInt_FromLong(*((char*)data)); case QMetaType::UChar: return PyInt_FromLong(*((unsigned char*)data)); case QMetaType::Short: return PyInt_FromLong(*((short*)data)); case QMetaType::UShort: return PyInt_FromLong(*((unsigned short*)data)); case QMetaType::Long: return PyInt_FromLong(*((long*)data)); case QMetaType::ULong: // does not fit into simple int of python return PyLong_FromUnsignedLong(*((unsigned long*)data)); case QMetaType::Bool: return PythonQtConv::GetPyBool(*((bool*)data)); case QMetaType::Int: return PyInt_FromLong(*((int*)data)); case QMetaType::UInt: // does not fit into simple int of python return PyLong_FromUnsignedLong(*((unsigned int*)data)); case QMetaType::QChar: return PyInt_FromLong(*((short*)data)); case QMetaType::Float: return PyFloat_FromDouble(*((float*)data)); case QMetaType::Double: return PyFloat_FromDouble(*((double*)data)); case QMetaType::LongLong: return PyLong_FromLongLong(*((qint64*)data)); case QMetaType::ULongLong: return PyLong_FromUnsignedLongLong(*((quint64*)data)); // implicit conversion from QByteArray to str has been removed: //case QMetaType::QByteArray: { // QByteArray* v = (QByteArray*) data; // return PyString_FromStringAndSize(*v, v->size()); // } case QMetaType::QVariantMap: return PythonQtConv::QVariantMapToPyObject(*((QVariantMap*)data)); case QMetaType::QVariantList: return PythonQtConv::QVariantListToPyObject(*((QVariantList*)data)); case QMetaType::QString: return PythonQtConv::QStringToPyObject(*((QString*)data)); case QMetaType::QStringList: return PythonQtConv::QStringListToPyObject(*((QStringList*)data)); case PythonQtMethodInfo::Variant: return PythonQtConv::QVariantToPyObject(*((QVariant*)data)); case QMetaType::QObjectStar: case QMetaType::QWidgetStar: return PythonQt::priv()->wrapQObject(*((QObject**)data)); default: if (PythonQt::priv()->isPythonQtObjectPtrMetaId(type)) { // special case, it is a PythonQtObjectPtr which contains a PyObject, take it directly: PyObject* o = ((PythonQtObjectPtr*)data)->object(); Py_INCREF(o); return o; } else { if (type > 0) { // if the type is known, we can construct it via QMetaType::construct void* newCPPObject = QMetaType::construct(type, data); // XXX this could be optimized by using metatypeid directly PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType::typeName(type)); wrap->_ownedByPythonQt = true; wrap->_useQMetaTypeDestroy = true; return (PyObject*)wrap; } std::cerr << "Unknown type that can not be converted to Python: " << type << ", in " << __FILE__ << ":" << __LINE__ << std::endl; } } Py_INCREF(Py_None); return Py_None; }
static PyObject *PyAsap_ParamProvMaxCutoff(PyAsap_EMTParamProvObject *self, PyObject *noargs) { double cutoff = self->cobj->GetMaxListCutoffDistance(); return PyFloat_FromDouble(cutoff); }
inline PyObject* to_python(double d) { return PyFloat_FromDouble(d); }
inline PyObject* to_python(float f) { return PyFloat_FromDouble(f); }
static PyObject * math_pow(PyObject *self, PyObject *args) { PyObject *ox, *oy; double r, x, y; int odd_y; if (! PyArg_UnpackTuple(args, "pow", 2, 2, &ox, &oy)) return NULL; x = PyFloat_AsDouble(ox); y = PyFloat_AsDouble(oy); if ((x == -1.0 || y == -1.0) && PyErr_Occurred()) return NULL; /* deal directly with IEEE specials, to cope with problems on various platforms whose semantics don't exactly match C99 */ r = 0.; /* silence compiler warning */ if (!Py_IS_FINITE(x) || !Py_IS_FINITE(y)) { errno = 0; if (Py_IS_NAN(x)) r = y == 0. ? 1. : x; /* NaN**0 = 1 */ else if (Py_IS_NAN(y)) r = x == 1. ? 1. : y; /* 1**NaN = 1 */ else if (Py_IS_INFINITY(x)) { odd_y = Py_IS_FINITE(y) && fmod(fabs(y), 2.0) == 1.0; if (y > 0.) r = odd_y ? x : fabs(x); else if (y == 0.) r = 1.; else /* y < 0. */ r = odd_y ? copysign(0., x) : 0.; } else if (Py_IS_INFINITY(y)) { if (fabs(x) == 1.0) r = 1.; else if (y > 0. && fabs(x) > 1.0) r = y; else if (y < 0. && fabs(x) < 1.0) { r = -y; /* result is +inf */ if (x == 0.) /* 0**-inf: divide-by-zero */ errno = EDOM; } else r = 0.; } } else { /* let libm handle finite**finite */ errno = 0; PyFPE_START_PROTECT("in math_pow", return 0); r = pow(x, y); PyFPE_END_PROTECT(r); /* a NaN result should arise only from (-ve)**(finite non-integer); in this case we want to raise ValueError. */ if (!Py_IS_FINITE(r)) { if (Py_IS_NAN(r)) { errno = EDOM; } /* an infinite result here arises either from: (A) (+/-0.)**negative (-> divide-by-zero) (B) overflow of x**y with x and y finite */ else if (Py_IS_INFINITY(r)) { if (x == 0.) errno = EDOM; else errno = ERANGE; } } } if (errno && is_error(r)) return NULL; else return PyFloat_FromDouble(r); }
static PyObject * math_ldexp(PyObject *self, PyObject *args) { double x, r; PyObject *oexp; long exp; if (! PyArg_ParseTuple(args, "dO:ldexp", &x, &oexp)) return NULL; if (PyLong_Check(oexp)) { /* on overflow, replace exponent with either LONG_MAX or LONG_MIN, depending on the sign. */ exp = PyLong_AsLong(oexp); if (exp == -1 && PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) { if (Py_SIZE(oexp) < 0) { exp = LONG_MIN; } else { exp = LONG_MAX; } PyErr_Clear(); } else { /* propagate any unexpected exception */ return NULL; } } } else { PyErr_SetString(PyExc_TypeError, "Expected an int or long as second argument " "to ldexp."); return NULL; } if (x == 0. || !Py_IS_FINITE(x)) { /* NaNs, zeros and infinities are returned unchanged */ r = x; errno = 0; } else if (exp > INT_MAX) { /* overflow */ r = copysign(Py_HUGE_VAL, x); errno = ERANGE; } else if (exp < INT_MIN) { /* underflow to +-0 */ r = copysign(0., x); errno = 0; } else { errno = 0; PyFPE_START_PROTECT("in math_ldexp", return 0); r = ldexp(x, (int)exp); PyFPE_END_PROTECT(r); if (Py_IS_INFINITY(r)) errno = ERANGE; } if (errno && is_error(r)) return NULL; return PyFloat_FromDouble(r); }
static PyObject* math_fsum(PyObject *self, PyObject *seq) { PyObject *item, *iter, *sum = NULL; Py_ssize_t i, j, n = 0, m = NUM_PARTIALS; double x, y, t, ps[NUM_PARTIALS], *p = ps; double xsave, special_sum = 0.0, inf_sum = 0.0; volatile double hi, yr, lo; iter = PyObject_GetIter(seq); if (iter == NULL) return NULL; PyFPE_START_PROTECT("fsum", Py_DECREF(iter); return NULL) for(;;) { /* for x in iterable */ assert(0 <= n && n <= m); assert((m == NUM_PARTIALS && p == ps) || (m > NUM_PARTIALS && p != NULL)); item = PyIter_Next(iter); if (item == NULL) { if (PyErr_Occurred()) goto _fsum_error; break; } x = PyFloat_AsDouble(item); Py_DECREF(item); if (PyErr_Occurred()) goto _fsum_error; xsave = x; for (i = j = 0; j < n; j++) { /* for y in partials */ y = p[j]; if (fabs(x) < fabs(y)) { t = x; x = y; y = t; } hi = x + y; yr = hi - x; lo = y - yr; if (lo != 0.0) p[i++] = lo; x = hi; } n = i; /* ps[i:] = [x] */ if (x != 0.0) { if (! Py_IS_FINITE(x)) { /* a nonfinite x could arise either as a result of intermediate overflow, or as a result of a nan or inf in the summands */ if (Py_IS_FINITE(xsave)) { PyErr_SetString(PyExc_OverflowError, "intermediate overflow in fsum"); goto _fsum_error; } if (Py_IS_INFINITY(xsave)) inf_sum += xsave; special_sum += xsave; /* reset partials */ n = 0; } else if (n >= m && _fsum_realloc(&p, n, ps, &m)) goto _fsum_error; else p[n++] = x; } } if (special_sum != 0.0) { if (Py_IS_NAN(inf_sum)) PyErr_SetString(PyExc_ValueError, "-inf + inf in fsum"); else sum = PyFloat_FromDouble(special_sum); goto _fsum_error; } hi = 0.0; if (n > 0) { hi = p[--n]; /* sum_exact(ps, hi) from the top, stop when the sum becomes inexact. */ while (n > 0) { x = hi; y = p[--n]; assert(fabs(y) < fabs(x)); hi = x + y; yr = hi - x; lo = y - yr; if (lo != 0.0) break; } /* Make half-even rounding work across multiple partials. Needed so that sum([1e-16, 1, 1e16]) will round-up the last digit to two instead of down to zero (the 1e-16 makes the 1 slightly closer to two). With a potential 1 ULP rounding error fixed-up, math.fsum() can guarantee commutativity. */ if (n > 0 && ((lo < 0.0 && p[n-1] < 0.0) || (lo > 0.0 && p[n-1] > 0.0))) { y = lo * 2.0; x = hi + y; yr = x - hi; if (y == yr) hi = x; } } sum = PyFloat_FromDouble(hi); _fsum_error: PyFPE_END_PROTECT(hi) Py_DECREF(iter); if (p != ps) PyMem_Free(p); return sum; }
static PyObject* PyNeighbor_getradius(PyNeighbor* self, void* closure) { float value = self->neighbor.radius; return PyFloat_FromDouble((double)value); }
static PyObject *next(PyObject *self) { ligolw_Tokenizer *tokenizer = (ligolw_Tokenizer *) self; PyObject *type; PyObject *token; Py_UNICODE *start, *end; /* * Identify the start and end of the next token. */ do { type = next_token(tokenizer, &start, &end); if(!type) return NULL; } while(type == Py_None); /* * Extract token as desired type. */ if(start == NULL) { /* * unquoted zero-length string == None */ Py_INCREF(Py_None); token = Py_None; } else if(type == (PyObject *) &PyFloat_Type) { char ascii_buffer[end - start + 1]; char *ascii_end; if(PyUnicode_EncodeDecimal(start, end - start, ascii_buffer, NULL)) return NULL; token = PyFloat_FromDouble(strtod(ascii_buffer, &ascii_end)); if(ascii_end == ascii_buffer || *ascii_end != 0) { /* * strtod() couldn't convert the token, emulate * float()'s error message */ Py_XDECREF(token); PyErr_Format(PyExc_ValueError, "invalid literal for float(): '%s'", ascii_buffer); token = NULL; } } else if(type == (PyObject *) &PyUnicode_Type) { token = PyUnicode_FromUnicode(start, end - start); } else if(type == (PyObject *) &PyString_Type) { token = PyUnicode_Encode(start, end - start, NULL, NULL); } else if(type == (PyObject *) &PyInt_Type) { token = PyInt_FromUnicode(start, end - start, 0); } else if(type == (PyObject *) &PyLong_Type) { token = PyLong_FromUnicode(start, end - start, 0); } else { token = PyObject_CallFunction(type, "u#", start, end - start); } /* * Done. */ return token; }
PyObject* ctrlp_fuzzycomt_match(PyObject* self, PyObject* args) { PyObject *paths, *abbrev, *returnlist; Py_ssize_t limit; char *mmode; int i; int max; if (!PyArg_ParseTuple(args, "OOns", &paths, &abbrev, &limit, &mmode)) { return NULL; } returnlist = PyList_New(0); // Type checking if (PyList_Check(paths) != 1) { PyErr_SetString(PyExc_TypeError,"expected a list"); return 0; } if (PyString_Check(abbrev) != 1) { PyErr_SetString(PyExc_TypeError,"expected a string"); return 0; } matchobj_t matches[PyList_Size(paths)]; if ( (limit > PyList_Size(paths)) || (limit == 0) ) { limit = PyList_Size(paths); } if ( PyString_Size(abbrev) == 0) { // if string is empty - just return first (:param limit) lines PyObject *initlist; initlist = PyList_GetSlice(paths,0,limit); return initlist; } else { // find matches and place them into matches array. ctrlp_get_line_matches(paths,abbrev, matches, mmode); // sort array of struct by struct.score key qsort(matches, PyList_Size(paths), sizeof(matchobj_t), ctrlp_comp_score_alpha); } for (i = 0, max = PyList_Size(paths); i < max; i++) { if (i == limit) break; // generate python dicts { 'line' : line, 'value' : value } // and place dicts to list PyObject *container; container = PyDict_New(); // TODO it retuns non-encoded string. So cyrillic literals // arent properly showed. // There are PyString_AsDecodedObject, it works in interactive // session but it fails in Vim for some reason // (probably because we dont know what encoding vim returns) PyDict_SetItemString(container, "line", matches[i].str); PyDict_SetItemString(container, "value", PyFloat_FromDouble(matches[i].score)); PyList_Append(returnlist,container); } return returnlist; }
static PyObject *SVertexIterator_u_get(BPy_SVertexIterator *self, void *UNUSED(closure)) { return PyFloat_FromDouble(self->sv_it->u()); }
PyObject * PyMember_GetOne(char *addr, PyMemberDef *l) { PyObject *v; // if ((l->flags & READ_RESTRICTED) && // PyEval_GetRestricted()) { // PyErr_SetString(PyExc_RuntimeError, "restricted attribute"); // return NULL; // } addr += l->offset; switch (l->type) { case T_BYTE: v = PyInt_FromLong( (long) (((*(char*)addr & 0xff) ^ 0x80) - 0x80)); break; case T_UBYTE: v = PyInt_FromLong((long) *(char*)addr & 0xff); break; case T_SHORT: v = PyInt_FromLong((long) *(short*)addr); break; case T_USHORT: v = PyInt_FromLong((long) *(unsigned short*)addr); break; case T_INT: v = PyInt_FromLong((long) *(int*)addr); break; case T_UINT: v = PyInt_FromLong((long) *(unsigned int*)addr); break; case T_LONG: v = PyInt_FromLong(*(long*)addr); break; case T_ULONG: v = PyLong_FromDouble((double) *(unsigned long*)addr); break; case T_FLOAT: v = PyFloat_FromDouble((double)*(float*)addr); break; case T_DOUBLE: v = PyFloat_FromDouble(*(double*)addr); break; case T_STRING: if (*(char**)addr == NULL) { Py_INCREF(Py_None); v = Py_None; } else v = PyString_FromString(*(char**)addr); break; case T_STRING_INPLACE: v = PyString_FromString((char*)addr); break; #ifdef macintosh case T_PSTRING: if (*(char**)addr == NULL) { Py_INCREF(Py_None); v = Py_None; } else v = PyString_FromStringAndSize( (*(char**)addr)+1, **(unsigned char**)addr); break; case T_PSTRING_INPLACE: v = PyString_FromStringAndSize( ((char*)addr)+1, *(unsigned char*)addr); break; #endif /* macintosh */ case T_CHAR: v = PyString_FromStringAndSize((char*)addr, 1); break; case T_OBJECT: v = *(PyObject **)addr; if (v == NULL) v = Py_None; Py_INCREF(v); break; case T_OBJECT_EX: v = *(PyObject **)addr; if (v == NULL) PyErr_SetString(PyExc_AttributeError, l->name); Py_XINCREF(v); break; default: PyErr_SetString(PyExc_SystemError, "bad memberdescr type"); v = NULL; } return v; }
static PyObject* DCDynamicsSceneFixedFrameRate(DCDynamicsScene* self, void*) { DCOBJECT_VALIDATE(self->scene, NULL); return PyFloat_FromDouble(self->scene->FixedFrameRate()); }
PyObject* KX_VertexProxy::pyattr_get_v2(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) { KX_VertexProxy* self= static_cast<KX_VertexProxy*>(self_v); return PyFloat_FromDouble(self->m_vertex->getUV2()[1]); }
/** * pyg_value_as_pyobject: * @value: the GValue object. * @copy_boxed: true if boxed values should be copied. * * This function creates/returns a Python wrapper object that * represents the GValue passed as an argument. * * Returns: a PyObject representing the value. */ PyObject * pyg_value_as_pyobject(const GValue *value, gboolean copy_boxed) { gchar buf[128]; switch (G_TYPE_FUNDAMENTAL(G_VALUE_TYPE(value))) { case G_TYPE_INTERFACE: if (g_type_is_a(G_VALUE_TYPE(value), G_TYPE_OBJECT)) return pygobject_new(g_value_get_object(value)); else break; case G_TYPE_CHAR: { gint8 val = g_value_get_char(value); return PYGLIB_PyUnicode_FromStringAndSize((char *)&val, 1); } case G_TYPE_UCHAR: { guint8 val = g_value_get_uchar(value); return PYGLIB_PyBytes_FromStringAndSize((char *)&val, 1); } case G_TYPE_BOOLEAN: { return PyBool_FromLong(g_value_get_boolean(value)); } case G_TYPE_INT: return PYGLIB_PyLong_FromLong(g_value_get_int(value)); case G_TYPE_UINT: { /* in Python, the Int object is backed by a long. If a long can hold the whole value of an unsigned int, use an Int. Otherwise, use a Long object to avoid overflow. This matches the ULongArg behavior in codegen/argtypes.h */ #if (G_MAXUINT <= G_MAXLONG) return PYGLIB_PyLong_FromLong((glong) g_value_get_uint(value)); #else return PyLong_FromUnsignedLong((gulong) g_value_get_uint(value)); #endif } case G_TYPE_LONG: return PYGLIB_PyLong_FromLong(g_value_get_long(value)); case G_TYPE_ULONG: { gulong val = g_value_get_ulong(value); if (val <= G_MAXLONG) return PYGLIB_PyLong_FromLong((glong) val); else return PyLong_FromUnsignedLong(val); } case G_TYPE_INT64: { gint64 val = g_value_get_int64(value); if (G_MINLONG <= val && val <= G_MAXLONG) return PYGLIB_PyLong_FromLong((glong) val); else return PyLong_FromLongLong(val); } case G_TYPE_UINT64: { guint64 val = g_value_get_uint64(value); if (val <= G_MAXLONG) return PYGLIB_PyLong_FromLong((glong) val); else return PyLong_FromUnsignedLongLong(val); } case G_TYPE_ENUM: return pyg_enum_from_gtype(G_VALUE_TYPE(value), g_value_get_enum(value)); case G_TYPE_FLAGS: return pyg_flags_from_gtype(G_VALUE_TYPE(value), g_value_get_flags(value)); case G_TYPE_FLOAT: return PyFloat_FromDouble(g_value_get_float(value)); case G_TYPE_DOUBLE: return PyFloat_FromDouble(g_value_get_double(value)); case G_TYPE_STRING: { const gchar *str = g_value_get_string(value); if (str) return PYGLIB_PyUnicode_FromString(str); Py_INCREF(Py_None); return Py_None; } case G_TYPE_POINTER: return pyg_pointer_new(G_VALUE_TYPE(value), g_value_get_pointer(value)); case G_TYPE_BOXED: { PyGTypeMarshal *bm; if (G_VALUE_HOLDS(value, PY_TYPE_OBJECT)) { PyObject *ret = (PyObject *)g_value_dup_boxed(value); if (ret == NULL) { Py_INCREF(Py_None); return Py_None; } return ret; } else if (G_VALUE_HOLDS(value, G_TYPE_VALUE)) { GValue *n_value = g_value_get_boxed (value); return pyg_value_as_pyobject(n_value, copy_boxed); } else if (G_VALUE_HOLDS(value, G_TYPE_VALUE_ARRAY)) { GValueArray *array = (GValueArray *) g_value_get_boxed(value); PyObject *ret = PyList_New(array->n_values); int i; for (i = 0; i < array->n_values; ++i) PyList_SET_ITEM(ret, i, pyg_value_as_pyobject (array->values + i, copy_boxed)); return ret; } else if (G_VALUE_HOLDS(value, G_TYPE_GSTRING)) { GString *string = (GString *) g_value_get_boxed(value); PyObject *ret = PYGLIB_PyUnicode_FromStringAndSize(string->str, string->len); return ret; } bm = pyg_type_lookup(G_VALUE_TYPE(value)); if (bm) { return bm->fromvalue(value); } else { if (copy_boxed) return pyg_boxed_new(G_VALUE_TYPE(value), g_value_get_boxed(value), TRUE, TRUE); else return pyg_boxed_new(G_VALUE_TYPE(value), g_value_get_boxed(value),FALSE,FALSE); } } case G_TYPE_PARAM: return pyg_param_spec_new(g_value_get_param(value)); case G_TYPE_OBJECT: return pygobject_new(g_value_get_object(value)); default: { PyGTypeMarshal *bm; if ((bm = pyg_type_lookup(G_VALUE_TYPE(value)))) return bm->fromvalue(value); break; } } g_snprintf(buf, sizeof(buf), "unknown type %s", g_type_name(G_VALUE_TYPE(value))); PyErr_SetString(PyExc_TypeError, buf); return NULL; }
static PyObject* CameraInfo_GetPixelAspect(CameraInfoObj* self, void* closure) { return PyFloat_FromDouble(self->instance.paspect); }
static PyObject * do_mkvalue(char **p_format, va_list *p_va) { for (;;) { switch (*(*p_format)++) { case '(': return do_mktuple(p_format, p_va, ')', countformat(*p_format, ')')); case '[': return do_mklist(p_format, p_va, ']', countformat(*p_format, ']')); case '{': return do_mkdict(p_format, p_va, '}', countformat(*p_format, '}')); case 'b': case 'B': case 'h': case 'i': return PyInt_FromLong((long)va_arg(*p_va, int)); case 'H': return PyInt_FromLong((long)va_arg(*p_va, unsigned int)); case 'l': return PyInt_FromLong((long)va_arg(*p_va, long)); #ifdef HAVE_LONG_LONG case 'L': return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG)); #endif case 'u': { PyObject *v; Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *); int n; if (**p_format == '#') { ++*p_format; n = va_arg(*p_va, int); } else n = -1; if (u == NULL) { v = Py_None; Py_INCREF(v); } else { if (n < 0) n = _ustrlen(u); v = PyUnicode_FromUnicode(u, n); } return v; } case 'f': case 'd': return PyFloat_FromDouble( (double)va_arg(*p_va, va_double)); case 'c': { char p[1]; p[0] = va_arg(*p_va, int); return PyString_FromStringAndSize(p, 1); } case 's': case 'z': { PyObject *v; char *str = va_arg(*p_va, char *); int n; if (**p_format == '#') { ++*p_format; n = va_arg(*p_va, int); } else
static PyObject* pyEventCallbackMsg_getEventTime(pyEventCallbackMsg* self, void*) { return PyFloat_FromDouble(self->fThis->getEventTime()); }
NPY_NO_EXPORT PyObject * __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out, int variance, int num) { PyObject *obj1 = NULL, *obj2 = NULL, *obj3 = NULL; PyArrayObject *arr1 = NULL, *arr2 = NULL, *arrnew = NULL; PyObject *ret = NULL, *newshape = NULL; int i, n; npy_intp val; arrnew = (PyArrayObject *)PyArray_CheckAxis(self, &axis, 0); if (arrnew == NULL) { return NULL; } /* Compute and reshape mean */ arr1 = (PyArrayObject *)PyArray_EnsureAnyArray( PyArray_Mean(arrnew, axis, rtype, NULL)); if (arr1 == NULL) { Py_DECREF(arrnew); return NULL; } n = PyArray_NDIM(arrnew); newshape = PyTuple_New(n); if (newshape == NULL) { Py_DECREF(arr1); Py_DECREF(arrnew); return NULL; } for (i = 0; i < n; i++) { if (i == axis) { val = 1; } else { val = PyArray_DIM(arrnew,i); } PyTuple_SET_ITEM(newshape, i, PyInt_FromLong((long)val)); } arr2 = (PyArrayObject *)PyArray_Reshape(arr1, newshape); Py_DECREF(arr1); Py_DECREF(newshape); if (arr2 == NULL) { Py_DECREF(arrnew); return NULL; } /* Compute x = x - mx */ arr1 = (PyArrayObject *)PyArray_EnsureAnyArray( PyNumber_Subtract((PyObject *)arrnew, (PyObject *)arr2)); Py_DECREF(arr2); if (arr1 == NULL) { Py_DECREF(arrnew); return NULL; } /* Compute x * x */ if (PyArray_ISCOMPLEX(arr1)) { obj3 = PyArray_Conjugate(arr1, NULL); } else { obj3 = (PyObject *)arr1; Py_INCREF(arr1); } if (obj3 == NULL) { Py_DECREF(arrnew); return NULL; } arr2 = (PyArrayObject *)PyArray_EnsureAnyArray( PyArray_GenericBinaryFunction(arr1, obj3, n_ops.multiply)); Py_DECREF(arr1); Py_DECREF(obj3); if (arr2 == NULL) { Py_DECREF(arrnew); return NULL; } if (PyArray_ISCOMPLEX(arr2)) { obj3 = PyObject_GetAttrString((PyObject *)arr2, "real"); switch(rtype) { case NPY_CDOUBLE: rtype = NPY_DOUBLE; break; case NPY_CFLOAT: rtype = NPY_FLOAT; break; case NPY_CLONGDOUBLE: rtype = NPY_LONGDOUBLE; break; } } else { obj3 = (PyObject *)arr2; Py_INCREF(arr2); } if (obj3 == NULL) { Py_DECREF(arrnew); return NULL; } /* Compute add.reduce(x*x,axis) */ obj1 = PyArray_GenericReduceFunction((PyArrayObject *)obj3, n_ops.add, axis, rtype, NULL); Py_DECREF(obj3); Py_DECREF(arr2); if (obj1 == NULL) { Py_DECREF(arrnew); return NULL; } n = PyArray_DIM(arrnew,axis); Py_DECREF(arrnew); n = (n-num); if (n == 0) { n = 1; } obj2 = PyFloat_FromDouble(1.0/((double )n)); if (obj2 == NULL) { Py_DECREF(obj1); return NULL; } ret = PyNumber_Multiply(obj1, obj2); Py_DECREF(obj1); Py_DECREF(obj2); if (!variance) { arr1 = (PyArrayObject *)PyArray_EnsureAnyArray(ret); /* sqrt() */ ret = PyArray_GenericUnaryFunction(arr1, n_ops.sqrt); Py_DECREF(arr1); } if (ret == NULL) { return NULL; } if (PyArray_CheckExact(self)) { goto finish; } if (PyArray_Check(self) && Py_TYPE(self) == Py_TYPE(ret)) { goto finish; } arr1 = (PyArrayObject *)PyArray_EnsureArray(ret); if (arr1 == NULL) { return NULL; } ret = PyArray_View(arr1, NULL, Py_TYPE(self)); Py_DECREF(arr1); finish: if (out) { if (PyArray_AssignArray(out, (PyArrayObject *)ret, NULL, NPY_DEFAULT_ASSIGN_CASTING) < 0) { Py_DECREF(ret); return NULL; } Py_DECREF(ret); Py_INCREF(out); return (PyObject *)out; } return ret; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::pyGetVectorLengthSquared() { return PyFloat_FromDouble(KBEVec3LengthSq(&getVector())); }
/*NUMPY_API * Round */ NPY_NO_EXPORT PyObject * PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out) { PyObject *f, *ret = NULL, *tmp, *op1, *op2; int ret_int=0; PyArray_Descr *my_descr; if (out && (PyArray_SIZE(out) != PyArray_SIZE(a))) { PyErr_SetString(PyExc_ValueError, "invalid output shape"); return NULL; } if (PyArray_ISCOMPLEX(a)) { PyObject *part; PyObject *round_part; PyObject *arr; int res; if (out) { arr = (PyObject *)out; Py_INCREF(arr); } else { arr = PyArray_Copy(a); if (arr == NULL) { return NULL; } } /* arr.real = a.real.round(decimals) */ part = PyObject_GetAttrString(arr, "real"); if (part == NULL) { Py_DECREF(arr); return NULL; } part = PyArray_EnsureAnyArray(part); round_part = PyArray_Round((PyArrayObject *)part, decimals, NULL); Py_DECREF(part); if (round_part == NULL) { Py_DECREF(arr); return NULL; } res = PyObject_SetAttrString(arr, "real", round_part); Py_DECREF(round_part); if (res < 0) { Py_DECREF(arr); return NULL; } /* arr.imag = a.imag.round(decimals) */ part = PyObject_GetAttrString(arr, "imag"); if (part == NULL) { Py_DECREF(arr); return NULL; } part = PyArray_EnsureAnyArray(part); round_part = PyArray_Round((PyArrayObject *)part, decimals, NULL); Py_DECREF(part); if (round_part == NULL) { Py_DECREF(arr); return NULL; } res = PyObject_SetAttrString(arr, "imag", round_part); Py_DECREF(round_part); if (res < 0) { Py_DECREF(arr); return NULL; } return arr; } /* do the most common case first */ if (decimals >= 0) { if (PyArray_ISINTEGER(a)) { if (out) { if (PyArray_AssignArray(out, a, NULL, NPY_DEFAULT_ASSIGN_CASTING) < 0) { return NULL; } Py_INCREF(out); return (PyObject *)out; } else { Py_INCREF(a); return (PyObject *)a; } } if (decimals == 0) { if (out) { return PyObject_CallFunction(n_ops.rint, "OO", a, out); } return PyObject_CallFunction(n_ops.rint, "O", a); } op1 = n_ops.multiply; op2 = n_ops.true_divide; } else { op1 = n_ops.true_divide; op2 = n_ops.multiply; decimals = -decimals; } if (!out) { if (PyArray_ISINTEGER(a)) { ret_int = 1; my_descr = PyArray_DescrFromType(NPY_DOUBLE); } else { Py_INCREF(PyArray_DESCR(a)); my_descr = PyArray_DESCR(a); } out = (PyArrayObject *)PyArray_Empty(PyArray_NDIM(a), PyArray_DIMS(a), my_descr, PyArray_ISFORTRAN(a)); if (out == NULL) { return NULL; } } else { Py_INCREF(out); } f = PyFloat_FromDouble(power_of_ten(decimals)); if (f == NULL) { return NULL; } ret = PyObject_CallFunction(op1, "OOO", a, f, out); if (ret == NULL) { goto finish; } tmp = PyObject_CallFunction(n_ops.rint, "OO", ret, ret); if (tmp == NULL) { Py_DECREF(ret); ret = NULL; goto finish; } Py_DECREF(tmp); tmp = PyObject_CallFunction(op2, "OOO", ret, f, ret); if (tmp == NULL) { Py_DECREF(ret); ret = NULL; goto finish; } Py_DECREF(tmp); finish: Py_DECREF(f); Py_DECREF(out); if (ret_int) { Py_INCREF(PyArray_DESCR(a)); tmp = PyArray_CastToType((PyArrayObject *)ret, PyArray_DESCR(a), PyArray_ISFORTRAN(a)); Py_DECREF(ret); return tmp; } return ret; }
//------------------------------------------------------------------------------------- PyObject* ScriptVector3::pyGetZ() { return PyFloat_FromDouble(getVector().z); }
static PyObject * time_clock(PyObject *self, PyObject *unused) { return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC); }