PyObject * _pyccn_cmd_content_to_bytes(PyObject *UNUSED(self), PyObject *arg) { PyObject *str; if (arg == Py_None) Py_RETURN_NONE; else if (PyFloat_Check(arg) || PyLong_Check(arg) || _pyccn_Int_Check(arg)) { PyObject *py_o; py_o = PyObject_Str(arg); if (!py_o) return NULL; #if PY_MAJOR_VERSION >= 3 str = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(py_o), PyUnicode_GET_SIZE(py_o), NULL); Py_DECREF(py_o); #else str = py_o; #endif return str; } else if (PyUnicode_Check(arg)) return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(arg), PyUnicode_GET_SIZE(arg), NULL); return PyObject_Bytes(arg); }
static PyObject* GetHash(PyObject* p) { #if PY_MAJOR_VERSION >= 3 Object bytes(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(p), PyUnicode_GET_SIZE(p), 0)); if (!bytes) return 0; p = bytes.Get(); #else Object bytes(PyUnicode_Check(p) ? PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(p), PyUnicode_GET_SIZE(p), 0) : 0); if (PyUnicode_Check(p)) { if (!bytes) return 0; p = bytes.Get(); } #endif Object hash(PyObject_CallMethod(hashlib, "new", "s", "sha1")); if (!hash.IsValid()) return 0; Object result(PyObject_CallMethodObjArgs(hash, update, p, 0)); if (!result.IsValid()) return 0; return PyObject_CallMethod(hash, "hexdigest", 0); }
PyObject * _pyccn_cmd_content_to_bytearray(PyObject *UNUSED(self), PyObject *arg) { PyObject *str, *result; if (arg == Py_None) Py_RETURN_NONE; else if (PyFloat_Check(arg) || PyLong_Check(arg) || _pyccn_Int_Check(arg)) { PyObject *py_o; py_o = PyObject_Str(arg); if (!py_o) return NULL; #if PY_MAJOR_VERSION >= 3 str = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(py_o), PyUnicode_GET_SIZE(py_o), NULL); Py_DECREF(py_o); #else str = py_o; #endif } else if (PyUnicode_Check(arg)) { str = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(arg), PyUnicode_GET_SIZE(arg), NULL); } else str = (Py_INCREF(arg), arg); if (!str) return NULL; result = PyByteArray_FromObject(str); Py_DECREF(str); return result; }
int Dict_iterNext(JSOBJ obj, JSONTypeContext *tc) { if (GET_TC(tc)->itemName) { Py_DECREF(GET_TC(tc)->itemName); GET_TC(tc)->itemName = NULL; } if (!PyDict_Next ( (PyObject *)obj, &GET_TC(tc)->index, &GET_TC(tc)->itemName, &GET_TC(tc)->itemValue)) { PRINTMARK(); return 0; } if (PyUnicode_Check(GET_TC(tc)->itemName)) { GET_TC(tc)->itemName = PyUnicode_EncodeUTF8 ( PyUnicode_AS_UNICODE(GET_TC(tc)->itemName), PyUnicode_GET_SIZE(GET_TC(tc)->itemName), NULL ); } else if (!PyString_Check(GET_TC(tc)->itemName)) { GET_TC(tc)->itemName = PyObject_Str(GET_TC(tc)->itemName); } else { Py_INCREF(GET_TC(tc)->itemName); } PRINTMARK(); return 1; }
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 ); } }
static PyObject* GetHash(PyObject* p) { #if PY_MAJOR_VERSION >= 3 Object bytes(PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(p), PyUnicode_GET_SIZE(p), 0)); if (!bytes) return 0; p = bytes.Get(); #endif if (hashlib) { Object hash(PyObject_CallMethod(hashlib, "new", "s", "sha1")); if (!hash.IsValid()) return 0; PyObject_CallMethodObjArgs(hash, update, p, 0); return PyObject_CallMethod(hash, "hexdigest", 0); } if (sha) { Object hash(PyObject_CallMethod(sha, "new", 0)); if (!hash.IsValid()) return 0; PyObject_CallMethodObjArgs(hash, update, p, 0); return PyObject_CallMethod(hash, "hexdigest", 0); } return 0; }
static void *PyUnicodeToUTF8(JSOBJ _obj, JSONTypeContext *tc, void *outValue, size_t *_outLen) { PyObject *obj = (PyObject *) _obj; PyObject *newObj = PyUnicode_EncodeUTF8 (PyUnicode_AS_UNICODE(obj), PyUnicode_GET_SIZE(obj), NULL); GET_TC(tc)->newObj = newObj; *_outLen = PyString_GET_SIZE(newObj); return PyString_AS_STRING(newObj); }
PyObject* _PYSTRING(const Char* s, int utf8) { int lens = (int)Strlen(s); #if defined(Py_UNICODE_WIDE) PyObject *u, *x; u = PyUnicode_DecodeUTF16((char*)s,2*lens,NULL,NULL); if(!utf8 || !u) return u; x = PyUnicode_AsUTF8String(u); Py_DECREF(u); return x; #else return utf8 ? PyUnicode_EncodeUTF8((Py_UNICODE*)s, lens, NULL) : PyUnicode_FromUnicode((Py_UNICODE*)s, lens); #endif }
/** * Evaluate the code and return the value * @return */ QVariant PythonScript::evaluateImpl() { ScopedPythonGIL lock; PyObject *compiledCode = this->compileToByteCode(true); if (!compiledCode) { return QVariant(""); } PyObject *pyret; beginStdoutRedirect(); if (PyCallable_Check(compiledCode)) { PyObject *empty_tuple = PyTuple_New(0); pyret = PyObject_Call(compiledCode, empty_tuple, localDict); Py_DECREF(empty_tuple); } else { pyret = PyEval_EvalCode(CODE_OBJECT(compiledCode), localDict, localDict); } endStdoutRedirect(); if (!pyret) { if (PyErr_ExceptionMatches(PyExc_ValueError) || PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) { PyErr_Clear(); // silently ignore errors return QVariant(""); } else { emit_error(); return QVariant(); } } QVariant qret = QVariant(); /* None */ if (pyret == Py_None) { qret = QVariant(""); } /* numeric types */ else if (PyFloat_Check(pyret)) { qret = QVariant(PyFloat_AS_DOUBLE(pyret)); } else if (INT_CHECK(pyret)) { qret = QVariant((qlonglong)TO_LONG(pyret)); } #if !defined(IS_PY3K) else if (PyLong_Check(pyret)) { qret = QVariant((qlonglong)PyLong_AsLongLong(pyret)); } #endif else if (PyNumber_Check(pyret)) { PyObject *number = PyNumber_Float(pyret); if (number) { qret = QVariant(PyFloat_AS_DOUBLE(number)); Py_DECREF(number); } } /* bool */ else if (PyBool_Check(pyret)) { qret = QVariant(pyret == Py_True); } // could handle advanced types (such as PyList->QValueList) here if needed /* fallback: try to convert to (unicode) string */ if (!qret.isValid()) { #if defined(IS_PY3K) // In 3 everything is unicode PyObject *pystring = PyObject_Str(pyret); if (pystring) { qret = QVariant(QString::fromUtf8(_PyUnicode_AsString(pystring))); } #else PyObject *pystring = PyObject_Unicode(pyret); if (pystring) { PyObject *asUTF8 = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(pystring), (int)PyUnicode_GET_DATA_SIZE(pystring), nullptr); Py_DECREF(pystring); if (asUTF8) { qret = QVariant(QString::fromUtf8(PyString_AS_STRING(asUTF8))); Py_DECREF(asUTF8); } else if ((pystring = PyObject_Str(pyret))) { qret = QVariant(QString(PyString_AS_STRING(pystring))); Py_DECREF(pystring); } } #endif } Py_DECREF(pyret); if (PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_ValueError) || PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) { PyErr_Clear(); // silently ignore errors return QVariant(""); } else { emit_error(); } return QVariant(); } return qret; }
QVariant PythonScript::eval() { if (!isFunction) compiled = notCompiled; if (compiled != isCompiled && !compile(true)) return QVariant(); PyObject *pyret; beginStdoutRedirect(); if (PyCallable_Check(PyCode)) { PyObject *empty_tuple = PyTuple_New(0); pyret = PyObject_Call(PyCode, empty_tuple, localDict); Py_DECREF(empty_tuple); } else pyret = PyEval_EvalCode((PyCodeObject*)PyCode, env()->globalDict(), localDict); endStdoutRedirect(); if (!pyret) { if (PyErr_ExceptionMatches(PyExc_ValueError) || PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) { PyErr_Clear(); // silently ignore errors return QVariant(""); } else { emit_error(env()->errorMsg(), 0); return QVariant(); } } QVariant qret = QVariant(); /* None */ if (pyret == Py_None) qret = QVariant(""); /* numeric types */ else if (PyFloat_Check(pyret)) qret = QVariant(PyFloat_AS_DOUBLE(pyret)); else if (PyInt_Check(pyret)) qret = QVariant((qlonglong)PyInt_AS_LONG(pyret)); else if (PyLong_Check(pyret)) qret = QVariant((qlonglong)PyLong_AsLongLong(pyret)); else if (PyNumber_Check(pyret)) { PyObject *number = PyNumber_Float(pyret); if (number) { qret = QVariant(PyFloat_AS_DOUBLE(number)); Py_DECREF(number); } /* bool */ } else if (PyBool_Check(pyret)) qret = QVariant(pyret==Py_True, 0); // could handle advanced types (such as PyList->QValueList) here if needed /* fallback: try to convert to (unicode) string */ if(!qret.isValid()) { PyObject *pystring = PyObject_Unicode(pyret); if (pystring) { PyObject *asUTF8 = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(pystring), PyUnicode_GET_DATA_SIZE(pystring), 0); Py_DECREF(pystring); if (asUTF8) { qret = QVariant(QString::fromUtf8(PyString_AS_STRING(asUTF8))); Py_DECREF(asUTF8); } else if (pystring = PyObject_Str(pyret)) { qret = QVariant(QString(PyString_AS_STRING(pystring))); Py_DECREF(pystring); } } } Py_DECREF(pyret); if (PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_ValueError) || PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) { PyErr_Clear(); // silently ignore errors return QVariant(""); } else { emit_error(env()->errorMsg(), 0); return QVariant(); } } else return qret; }