bool BINARY_OPERATION_ADD_OBJECT_FLOAT_INPLACE(PyObject **operand1, PyObject *operand2) { assert(operand1); CHECK_OBJECT(*operand1); CHECK_OBJECT(operand2); assert(PyFloat_CheckExact(operand2)); if (PyFloat_CheckExact(*operand1)) { // Adding floats to a new float could get special code too. if (Py_REFCNT(*operand1) == 1) { return FLOAT_ADD_INCREMENTAL(operand1, operand2); } } PyObject *result = PyNumber_InPlaceAdd(*operand1, operand2); if (unlikely(result == NULL)) { return false; } // We got an object handed, that we have to release. Py_DECREF(*operand1); // That's our return value then. As we use a dedicated variable, it's // OK that way. *operand1 = result; return true; }
NUITKA_MAY_BE_UNUSED static bool BINARY_OPERATION_MUL_INPLACE( PyObject **operand1, PyObject *operand2 ) { assert( operand1 ); CHECK_OBJECT( *operand1 ); CHECK_OBJECT( operand2 ); if ( Py_REFCNT( *operand1 ) == 1 ) { if ( PyFloat_CheckExact( *operand1 ) && PyFloat_CheckExact( operand2 ) ) { return FLOAT_MUL_INCREMENTAL( operand1, operand2 ); } } PyObject *result = PyNumber_InPlaceMultiply( *operand1, operand2 ); if (unlikely( result == NULL )) { return false; } // We got an object handed, that we have to release. Py_DECREF( *operand1 ); // That's our return value then. As we use a dedicated variable, it's // OK that way. *operand1 = result; return true; }
static bool FLOAT_ADD_INCREMENTAL(PyObject **operand1, PyObject *operand2) { assert(PyFloat_CheckExact(*operand1)); assert(PyFloat_CheckExact(operand2)); PyFPE_START_PROTECT("add", return false); PyFloat_AS_DOUBLE(*operand1) += PyFloat_AS_DOUBLE(operand2); PyFPE_END_PROTECT(*operand1); return true; }
static void py_variable_to_json_internal( PyObject *obj, json_writer_t *writer ) { if ( PyString_CheckExact( obj ) ) { json_writer_write_str( writer, PyString_AS_STRING( obj ) ); } else if ( PyInt_CheckExact( obj ) ) { json_writer_write_integer( writer, PyInt_AS_LONG( obj ) ); } else if ( PyFloat_CheckExact( obj ) ) { json_writer_write_number( writer, PyFloat_AS_DOUBLE( obj ) ); } else if ( PyBool_Check( obj ) ) { json_writer_write_boolean( writer, ( obj == Py_True ) ); } else if ( PyUnicode_CheckExact( obj ) ) { /* Create a new string object that is UTF-8 encoded. */ Py_UNICODE *unicode = PyUnicode_AS_UNICODE( obj ); Py_ssize_t size = PyUnicode_GET_SIZE( obj ); PyObject *str_obj = PyUnicode_EncodeUTF8( unicode, size, NULL ); py_variable_to_json_internal( str_obj, writer ); PyObject_Free( str_obj ); } else if ( PyDict_CheckExact( obj ) ) { py_dict_to_json( obj, writer ); } else if ( PyList_CheckExact( obj ) ) { py_list_to_json( obj, writer ); } else if ( PyTuple_CheckExact( obj ) ) { py_tuple_to_json( obj, writer ); } }
int isDate(PyObject *object) { if (PyFloat_CheckExact(object)) return 1; return PyDateTime_CheckExact(object); }
static PyObject * DecimalResultProcessor_process(DecimalResultProcessor *self, PyObject *value) { PyObject *str, *result, *args; if (value == Py_None) Py_RETURN_NONE; if (PyFloat_CheckExact(value)) { /* Decimal does not accept float values directly */ args = PyTuple_Pack(1, value); if (args == NULL) return NULL; str = PyString_Format(self->format, args); if (str == NULL) return NULL; result = PyObject_CallFunctionObjArgs(self->type, str, NULL); Py_DECREF(str); return result; } else { return PyObject_CallFunctionObjArgs(self->type, value, NULL); } }
static PyObject * Vector3D_rotatez(PyVector3D * self, PyObject * arg) { if (!PyFloat_CheckExact(arg)) { PyErr_SetString(PyExc_TypeError, "Can only rotatez with a float"); return NULL; } double angle = PyFloat_AsDouble(arg); self->coords.rotateZ(angle); Py_INCREF(Py_None); return Py_None; }
/* * PyArray_GetAttrString_SuppressException: * * Stripped down version of PyObject_GetAttrString, * avoids lookups for None, tuple, and List objects, * and doesn't create a PyErr since this code ignores it. * * This can be much faster then PyObject_GetAttrString where * exceptions are not used by caller. * * 'obj' is the object to search for attribute. * * 'name' is the attribute to search for. * * Returns attribute value on success, 0 on failure. */ PyObject * PyArray_GetAttrString_SuppressException(PyObject *obj, char *name) { PyTypeObject *tp = Py_TYPE(obj); PyObject *res = (PyObject *)NULL; /* We do not need to check for special attributes on trivial types */ if (obj == Py_None || /* Basic number types */ #if !defined(NPY_PY3K) PyInt_CheckExact(obj) || #endif PyLong_CheckExact(obj) || PyFloat_CheckExact(obj) || /* Basic sequence types */ PyList_CheckExact(obj) || PyTuple_CheckExact(obj)) { return NULL; } /* Attribute referenced by (char *)name */ if (tp->tp_getattr != NULL) { res = (*tp->tp_getattr)(obj, name); if (res == NULL) { PyErr_Clear(); } } /* Attribute referenced by (PyObject *)name */ else if (tp->tp_getattro != NULL) { #if defined(NPY_PY3K) PyObject *w = PyUnicode_InternFromString(name); #else PyObject *w = PyString_InternFromString(name); #endif if (w == NULL) { return (PyObject *)NULL; } res = (*tp->tp_getattro)(obj, w); Py_DECREF(w); if (res == NULL) { PyErr_Clear(); } } return res; }
static PyObject* escape(PyObject *self, PyObject *text) { PyObject *s = NULL, *rv = NULL, *html; /* we don't have to escape integers, bools or floats */ if (PyLong_CheckExact(text) || #if PY_MAJOR_VERSION < 3 PyInt_CheckExact(text) || #endif PyFloat_CheckExact(text) || PyBool_Check(text) || text == Py_None) return PyObject_CallFunctionObjArgs(markup, text, NULL); /* if the object has an __html__ method that performs the escaping */ html = PyObject_GetAttrString(text, "__html__"); if (html) { rv = PyObject_CallObject(html, NULL); Py_DECREF(html); return rv; } /* otherwise make the object unicode if it isn't, then escape */ PyErr_Clear(); if (!PyUnicode_Check(text)) { #if PY_MAJOR_VERSION < 3 PyObject *unicode = PyObject_Unicode(text); #else PyObject *unicode = PyObject_Str(text); #endif if (!unicode) return NULL; s = escape_unicode((PyUnicodeObject*)unicode); Py_DECREF(unicode); } else s = escape_unicode((PyUnicodeObject*)text); /* convert the unicode string into a markup object. */ rv = PyObject_CallFunctionObjArgs(markup, (PyObject*)s, NULL); Py_DECREF(s); return rv; }
static PyObject* convert_nested(PyObject *ob, convert_func convert_string) { /* dict. */ if (PyDict_CheckExact(ob)) { return convert_dict(ob, convert_string); } /* sequence. */ if (PyTuple_CheckExact(ob) || PyList_CheckExact(ob)) { return convert_seq(ob, convert_string); } /* numbers. */ if (PyInt_CheckExact(ob) || PyLong_CheckExact(ob) || PyFloat_CheckExact(ob)) { Py_INCREF(ob); return ob; } /* bool. */ if (PyBool_Check(ob)) { Py_INCREF(ob); return ob; } /* none. */ if (ob == Py_None) { Py_INCREF(ob); return ob; } if (PyString_CheckExact(ob) || PyUnicode_CheckExact(ob)) { return convert_string(ob); } return PyErr_Format( PyExc_TypeError, "Got wrong type: %s", ob->ob_type->tp_name); }
PyObject* _PyCode_ConstantKey(PyObject *op) { PyObject *key; /* Py_None and Py_Ellipsis are singleton */ if (op == Py_None || op == Py_Ellipsis || PyLong_CheckExact(op) || PyBool_Check(op) || PyBytes_CheckExact(op) || PyUnicode_CheckExact(op) /* code_richcompare() uses _PyCode_ConstantKey() internally */ || PyCode_Check(op)) { key = PyTuple_Pack(2, Py_TYPE(op), op); } else if (PyFloat_CheckExact(op)) { double d = PyFloat_AS_DOUBLE(op); /* all we need is to make the tuple different in either the 0.0 * or -0.0 case from all others, just to avoid the "coercion". */ if (d == 0.0 && copysign(1.0, d) < 0.0) key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); else key = PyTuple_Pack(2, Py_TYPE(op), op); } else if (PyComplex_CheckExact(op)) { Py_complex z; int real_negzero, imag_negzero; /* For the complex case we must make complex(x, 0.) different from complex(x, -0.) and complex(0., y) different from complex(-0., y), for any x and y. All four complex zeros must be distinguished.*/ z = PyComplex_AsCComplex(op); real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; /* use True, False and None singleton as tags for the real and imag * sign, to make tuples different */ if (real_negzero && imag_negzero) { key = PyTuple_Pack(3, Py_TYPE(op), op, Py_True); } else if (imag_negzero) { key = PyTuple_Pack(3, Py_TYPE(op), op, Py_False); } else if (real_negzero) { key = PyTuple_Pack(3, Py_TYPE(op), op, Py_None); } else { key = PyTuple_Pack(2, Py_TYPE(op), op); } } else if (PyTuple_CheckExact(op)) { Py_ssize_t i, len; PyObject *tuple; len = PyTuple_GET_SIZE(op); tuple = PyTuple_New(len); if (tuple == NULL) return NULL; for (i=0; i < len; i++) { PyObject *item, *item_key; item = PyTuple_GET_ITEM(op, i); item_key = _PyCode_ConstantKey(item); if (item_key == NULL) { Py_DECREF(tuple); return NULL; } PyTuple_SET_ITEM(tuple, i, item_key); } key = PyTuple_Pack(3, Py_TYPE(op), op, tuple); Py_DECREF(tuple); } else if (PyFrozenSet_CheckExact(op)) { Py_ssize_t pos = 0; PyObject *item; Py_hash_t hash; Py_ssize_t i, len; PyObject *tuple, *set; len = PySet_GET_SIZE(op); tuple = PyTuple_New(len); if (tuple == NULL) return NULL; i = 0; while (_PySet_NextEntry(op, &pos, &item, &hash)) { PyObject *item_key; item_key = _PyCode_ConstantKey(item); if (item_key == NULL) { Py_DECREF(tuple); return NULL; } assert(i < len); PyTuple_SET_ITEM(tuple, i, item_key); i++; } set = PyFrozenSet_New(tuple); Py_DECREF(tuple); if (set == NULL) return NULL; key = PyTuple_Pack(3, Py_TYPE(op), op, set); Py_DECREF(set); return key; } else { /* for other types, use the object identifier as a unique identifier * to ensure that they are seen as unequal. */ PyObject *obj_id = PyLong_FromVoidPtr(op); if (obj_id == NULL) return NULL; key = PyTuple_Pack(3, Py_TYPE(op), op, obj_id); Py_DECREF(obj_id); } return key; }
NUITKA_MAY_BE_UNUSED static bool BINARY_OPERATION_ADD_INPLACE( PyObject **operand1, PyObject *operand2 ) { assert( operand1 ); CHECK_OBJECT( *operand1 ); CHECK_OBJECT( operand2 ); #if PYTHON_VERSION < 300 // Something similar for Python3 should exist too. if ( PyInt_CheckExact( *operand1 ) && PyInt_CheckExact( operand2 ) ) { long a, b, i; a = PyInt_AS_LONG( *operand1 ); b = PyInt_AS_LONG( operand2 ); i = a + b; // Detect overflow, in which case, a "long" object would have to be // created, which we won't handle here. TODO: Add an else for that // case. if (likely(!( (i^a) < 0 && (i^b) < 0 ) )) { PyObject *result = PyInt_FromLong( i ); Py_DECREF( *operand1 ); *operand1 = result; return true; } } #endif #if PYTHON_VERSION < 300 if ( Py_REFCNT( *operand1 ) == 1 ) { // We more or less own the operand, so we might re-use its storage and // execute stuff in-place. if ( PyString_CheckExact( *operand1 ) && !PyString_CHECK_INTERNED( *operand1 ) && PyString_CheckExact( operand2 ) ) { return STRING_ADD_INCREMENTAL( operand1, operand2 ); } else if ( PyFloat_CheckExact( *operand1 ) && PyFloat_CheckExact( operand2 ) ) { return FLOAT_ADD_INCREMENTAL( operand1, operand2 ); } } // Strings are to be treated differently. if ( PyString_CheckExact( *operand1 ) && PyString_CheckExact( operand2 ) ) { PyString_Concat( operand1, operand2 ); return !ERROR_OCCURRED(); } #else if ( Py_REFCNT( *operand1 ) == 1 ) { // We more or less own the operand, so we might re-use its storage and // execute stuff in-place. if ( PyUnicode_CheckExact( *operand1 ) && !PyUnicode_CHECK_INTERNED( *operand1 ) && PyUnicode_CheckExact( operand2 ) ) { return UNICODE_ADD_INCREMENTAL( operand1, operand2 ); } else if ( PyFloat_CheckExact( *operand1 ) && PyFloat_CheckExact( operand2 ) ) { return FLOAT_ADD_INCREMENTAL( operand1, operand2 ); } } // Strings are to be treated differently. if ( PyUnicode_CheckExact( *operand1 ) && PyUnicode_CheckExact( operand2 ) ) { PyObject *result = PyUnicode_Concat( *operand1, operand2 ); if (unlikely( result == NULL )) { return false; } Py_DECREF( *operand1 ); *operand1 = result; return true; } #endif PyObject *result = PyNumber_InPlaceAdd( *operand1, operand2 ); if (unlikely( result == NULL )) { return false; } // We got an object handed, that we have to release. Py_DECREF( *operand1 ); // That's our return value then. As we use a dedicated variable, it's // OK that way. *operand1 = result; return true; }
static PyObject * MidiIn_getMessage(MidiIn *self, PyObject *args) { PyObject *timeout = NULL; PyObject *result = NULL; if(!PyArg_ParseTuple(args, "|O:getMessage", &timeout)) return NULL; long ms = -1; if(timeout) { if(PyFloat_CheckExact(timeout)) ms = (long) PyFloat_AS_DOUBLE(timeout); #if ! PK_PYTHON3 else if(PyInt_CheckExact(timeout)) ms = PyInt_AS_LONG(timeout); #endif else if(PyLong_CheckExact(timeout)) ms = PyLong_AsLong(timeout); else { PyErr_Format(RtMidiError, "timeout value must be a number, not %s", timeout->ob_type->tp_name); return NULL; } if(ms < 0) { PyErr_SetString(RtMidiError, "timeout value must be a positive number"); return NULL; } } #if PK_WINDOWS WaitForSingleObject(self->mutex); #else pthread_mutex_lock(&self->mutex); #endif if(ms > -1 && self->triggered == false) { PyThreadState *_save = PyEval_SaveThread(); #if PK_WINDOWS WaitForSingleObject(self->cond, ms < 0 ? INFINITE : ms); #else if(ms < 0) { pthread_cond_wait(&self->cond, &self->mutex); } else { struct timeval now; gettimeofday(&now, 0); struct timespec time; TIMEVAL_TO_TIMESPEC(&now, &time); unsigned long sec = ms / 1000; unsigned long nsec = (ms % 1000) * 1000000; time.tv_sec += sec; time.tv_nsec += nsec; while(time.tv_nsec >= 1000000000) { time.tv_sec++; time.tv_nsec -= 1000000000; } pthread_cond_timedwait(&self->cond, &self->mutex, &time); } #endif PyEval_RestoreThread(_save); } if(self->m_q->size() > 0) { result = (PyObject *) PyMidiMessage_new(); MidiMessage *inMidi = self->m_q->front(); (*((PyMidiMessage *)result)->m) = (*inMidi); self->m_q->pop(); self->triggered = false; delete inMidi; } #if PK_WINDOWS ReleaseMutex(self->mutex); #else pthread_mutex_unlock(&self->mutex); #endif if(result) return result; Py_RETURN_NONE; }
LLBC_PacketHeaderParts *pyllbc_Service::BuildCLayerParts(PyObject *pyLayerParts) { // Python layer parts(dict type) convert rules describe: // python type c++ type // -------------------------- // int/long/bool --> sint64 // float4/8 --> float/double // str/bytearray --> LLBC_String if (!PyDict_Check(pyLayerParts)) { pyllbc_SetError("parts instance not dict type"); return NULL; } LLBC_PacketHeaderParts *cLayerParts = LLBC_New(LLBC_PacketHeaderParts); Py_ssize_t pos = 0; PyObject *key, *value; while (PyDict_Next(pyLayerParts, &pos, &key, &value)) // key & value are borrowed. { const int serialNo = static_cast<int>(PyInt_AsLong(key)); if (UNLIKELY(serialNo == -1 && PyErr_Occurred())) { pyllbc_TransferPyError("When fetch header part serial no"); LLBC_Delete(cLayerParts); return NULL; } // Value type check order: // int-> // str-> // float-> // long-> // bool-> // bytearray-> // other objects if (PyInt_CheckExact(value)) { const sint64 cValue = PyInt_AS_LONG(value); cLayerParts->SetPart<sint64>(serialNo, cValue); } else if (PyString_CheckExact(value)) { char *strBeg; Py_ssize_t strLen; if (UNLIKELY(PyString_AsStringAndSize(value, &strBeg, &strLen) == -1)) { pyllbc_TransferPyError("When fetch header part value"); LLBC_Delete(cLayerParts); return NULL; } cLayerParts->SetPart(serialNo, strBeg, strLen); } else if (PyFloat_CheckExact(value)) { const double cValue = PyFloat_AS_DOUBLE(value); cLayerParts->SetPart<double>(serialNo, cValue); } else if (PyLong_CheckExact(value)) { const sint64 cValue = PyLong_AsLongLong(value); cLayerParts->SetPart<sint64>(serialNo, cValue); } else if (PyBool_Check(value)) { const int pyBoolCheck = PyObject_IsTrue(value); if (UNLIKELY(pyBoolCheck == -1)) { pyllbc_TransferPyError("when fetch header part value"); LLBC_Delete(cLayerParts); return NULL; } cLayerParts->SetPart<uint8>(serialNo, pyBoolCheck); } else if (PyByteArray_CheckExact(value)) { char *bytesBeg = PyByteArray_AS_STRING(value); Py_ssize_t bytesLen = PyByteArray_GET_SIZE(value); cLayerParts->SetPart(serialNo, bytesBeg, bytesLen); } else // Other types, we simple get the object string representations. { LLBC_String strRepr = pyllbc_ObjUtil::GetObjStr(value); if (UNLIKELY(strRepr.empty() && PyErr_Occurred())) { LLBC_Delete(cLayerParts); return NULL; } cLayerParts->SetPart(serialNo, strRepr.data(), strRepr.size()); } } return cLayerParts; }
EXPORT UDate PyObject_AsUDate(PyObject *object) { if (PyFloat_CheckExact(object)) return (UDate) (PyFloat_AsDouble(object) * 1000.0); else { if (PyDateTime_CheckExact(object)) { PyObject *tzinfo = PyObject_GetAttrString(object, "tzinfo"); PyObject *utcoffset, *ordinal; if (tzinfo == Py_None) { PyObject *m = PyImport_ImportModule("icu"); PyObject *cls = PyObject_GetAttrString(m, "ICUtzinfo"); tzinfo = PyObject_CallMethodObjArgs(cls, getDefault_NAME, NULL); Py_DECREF(cls); Py_DECREF(m); utcoffset = PyObject_CallMethodObjArgs(tzinfo, utcoffset_NAME, object, NULL); Py_DECREF(tzinfo); } else { utcoffset = PyObject_CallMethodObjArgs(object, utcoffset_NAME, NULL); Py_DECREF(tzinfo); } ordinal = PyObject_CallMethodObjArgs(object, toordinal_NAME, NULL); if (utcoffset != NULL && PyDelta_CheckExact(utcoffset) && ordinal != NULL && PyInt_CheckExact(ordinal)) { #if PY_MAJOR_VERSION >= 3 double ordinalValue = PyLong_AsDouble(ordinal); #else long ordinalValue = PyInt_AsLong(ordinal); #endif double timestamp = (ordinalValue - 719163) * 86400.0 + PyDateTime_DATE_GET_HOUR(object) * 3600.0 + PyDateTime_DATE_GET_MINUTE(object) * 60.0 + (double) PyDateTime_DATE_GET_SECOND(object) + PyDateTime_DATE_GET_MICROSECOND(object) / 1e6 - #ifndef PYPY_VERSION (((PyDateTime_Delta *) utcoffset)->days * 86400.0 + (double) ((PyDateTime_Delta *) utcoffset)->seconds); #else (PyDateTime_DELTA_GET_DAYS( (PyDateTime_Delta *) utcoffset) * 86400.0 + (double) PyDateTime_DELTA_GET_SECONDS( (PyDateTime_Delta *) utcoffset)); #endif Py_DECREF(utcoffset); Py_DECREF(ordinal); return (UDate) (timestamp * 1000.0); } Py_XDECREF(utcoffset); Py_XDECREF(ordinal); } } PyErr_SetObject(PyExc_TypeError, object); throw ICUException(); }