static PyObject* from_long_kobject(K x) { PyObject* result; Py_ssize_t i, length ; length = (Py_ssize_t)(x->n); if(scalar(x)) { result = PyLong_FromDouble((double) (x->j)); } else { result = PyList_New(length); for(i = 0; i != length; ++i) { PyList_SetItem(result, i, PyLong_FromDouble((double)xJ[i])); } } return result; }
long _Py_HashDouble(double v) { double intpart, fractpart; int expo; long hipart; long x; /* the final hash value */ /* This is designed so that Python numbers of different types * that compare equal hash to the same value; otherwise comparisons * of mapping keys will turn out weird. */ fractpart = modf(v, &intpart); if (fractpart == 0.0) { /* This must return the same hash as an equal int or long. */ if (intpart > LONG_MAX || -intpart > LONG_MAX) { /* Convert to long and use its hash. */ PyObject *plong; /* converted to Python long */ if (Py_IS_INFINITY(intpart)) /* can't convert to long int -- arbitrary */ v = v < 0 ? -271828.0 : 314159.0; plong = PyLong_FromDouble(v); if (plong == NULL) return -1; x = PyObject_Hash(plong); Py_DECREF(plong); return x; } /* Fits in a C long == a Python int, so is its own hash. */ x = (long)intpart; if (x == -1) x = -2; return x; } /* The fractional part is non-zero, so we don't have to worry about * making this match the hash of some other type. * Use frexp to get at the bits in the double. * Since the VAX D double format has 56 mantissa bits, which is the * most of any double format in use, each of these parts may have as * many as (but no more than) 56 significant bits. * So, assuming sizeof(long) >= 4, each part can be broken into two * longs; frexp and multiplication are used to do that. * Also, since the Cray double format has 15 exponent bits, which is * the most of any double format in use, shifting the exponent field * left by 15 won't overflow a long (again assuming sizeof(long) >= 4). */ v = frexp(v, &expo); v *= 2147483648.0; /* 2**31 */ hipart = (long)v; /* take the top 32 bits */ v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ x = hipart + (long)v + (expo << 15); if (x == -1) x = -2; return x; }
static PyObject *bip_int(term_t t) { PyObject *pVal, *o; if (!PL_get_arg(1, t, t)) return NULL; pVal = term_to_python(t, true); #if PY_MAJOR_VERSION < 3 if (PyLong_Check(pVal)) { o = PyInt_FromLong(PyLong_AsLong(pVal)); } else if (PyInt_Check(pVal)) { return pVal; #else if (PyLong_Check(pVal)) { return pVal; #endif } else if (PyFloat_Check(pVal)) { #if PY_MAJOR_VERSION < 3 o = PyInt_FromLong(PyFloat_AsDouble(pVal)); #else o = PyLong_FromDouble(PyFloat_AsDouble(pVal)); #endif } else return NULL; Py_DECREF(pVal); return o; } static PyObject *bip_long(term_t t) { PyObject *pVal, *o; if (!PL_get_arg(1, t, t)) return NULL; pVal = term_to_python(t, true); if (PyLong_Check(pVal)) { return pVal; #if PY_MAJOR_VERSION < 3 } else if (PyInt_Check(pVal)) { o = PyLong_FromLong(PyInt_AsLong(pVal)); #endif } else if (PyFloat_Check(pVal)) { o = pVal; } else return NULL; Py_DECREF(pVal); return o; }
PyObject* EntityApp<E>::__py_getWatcher(PyObject* self, PyObject* args) { int argCount = PyTuple_Size(args); if(argCount != 1) { PyErr_Format(PyExc_TypeError, "KBEngine::getWatcher(): args[strpath] is error!"); PyErr_PrintEx(0); return 0; } char* path; if(PyArg_ParseTuple(args, "s", &path) == -1) { PyErr_Format(PyExc_TypeError, "KBEngine::getWatcher(): args[strpath] is error!"); PyErr_PrintEx(0); return 0; } //DebugHelper::getSingleton().setScriptMsgType(type); KBEShared_ptr< WatcherObject > pWobj = WatcherPaths::root().getWatcher(path); if(pWobj.get() == NULL) { PyErr_Format(PyExc_TypeError, "KBEngine::getWatcher(): not found watcher[%s]!", path); PyErr_PrintEx(0); return 0; } WATCHER_VALUE_TYPE wtype = pWobj->getType(); PyObject* pyval = NULL; MemoryStream* stream = MemoryStream::ObjPool().createObject(); pWobj->addToStream(stream); WATCHER_ID id; (*stream) >> id; switch(wtype) { case WATCHER_VALUE_TYPE_UINT8: { uint8 v; (*stream) >> v; pyval = PyLong_FromUnsignedLong(v); } break; case WATCHER_VALUE_TYPE_UINT16: { uint16 v; (*stream) >> v; pyval = PyLong_FromUnsignedLong(v); } break; case WATCHER_VALUE_TYPE_UINT32: { uint32 v; (*stream) >> v; pyval = PyLong_FromUnsignedLong(v); } break; case WATCHER_VALUE_TYPE_UINT64: { uint64 v; (*stream) >> v; pyval = PyLong_FromUnsignedLongLong(v); } break; case WATCHER_VALUE_TYPE_INT8: { int8 v; (*stream) >> v; pyval = PyLong_FromLong(v); } break; case WATCHER_VALUE_TYPE_INT16: { int16 v; (*stream) >> v; pyval = PyLong_FromLong(v); } break; case WATCHER_VALUE_TYPE_INT32: { int32 v; (*stream) >> v; pyval = PyLong_FromLong(v); } break; case WATCHER_VALUE_TYPE_INT64: { int64 v; (*stream) >> v; pyval = PyLong_FromLongLong(v); } break; case WATCHER_VALUE_TYPE_FLOAT: { float v; (*stream) >> v; pyval = PyLong_FromDouble(v); } break; case WATCHER_VALUE_TYPE_DOUBLE: { double v; (*stream) >> v; pyval = PyLong_FromDouble(v); } break; case WATCHER_VALUE_TYPE_CHAR: case WATCHER_VALUE_TYPE_STRING: { std::string v; (*stream) >> v; pyval = PyUnicode_FromString(v.c_str()); } break; case WATCHER_VALUE_TYPE_BOOL: { bool v; (*stream) >> v; pyval = PyBool_FromLong(v); } break; case WATCHER_VALUE_TYPE_COMPONENT_TYPE: { COMPONENT_TYPE v; (*stream) >> v; pyval = PyBool_FromLong(v); } break; default: KBE_ASSERT(false && "no support!\n"); }; MemoryStream::ObjPool().reclaimObject(stream); return pyval; }
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; }