static long __Pyx__PyObject_Ord(PyObject* c) { Py_ssize_t size; if (PyBytes_Check(c)) { size = PyBytes_GET_SIZE(c); if (likely(size == 1)) { return (unsigned char) PyBytes_AS_STRING(c)[0]; } #if PY_MAJOR_VERSION < 3 } else if (PyUnicode_Check(c)) { return (long)__Pyx_PyUnicode_AsPy_UCS4(c); #endif #if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE)) } else if (PyByteArray_Check(c)) { size = PyByteArray_GET_SIZE(c); if (likely(size == 1)) { return (unsigned char) PyByteArray_AS_STRING(c)[0]; } #endif } else { // FIXME: support character buffers - but CPython doesn't support them either PyErr_Format(PyExc_TypeError, "ord() expected string of length 1, but %.200s found", c->ob_type->tp_name); return (long)(Py_UCS4)-1; } PyErr_Format(PyExc_TypeError, "ord() expected a character, but string of length %zd found", size); return (long)(Py_UCS4)-1; }
static PyObject *kdumpfile_read (PyObject *_self, PyObject *args, PyObject *kw) { kdumpfile_object *self = (kdumpfile_object*)_self; PyObject *obj; kdump_paddr_t addr; kdump_status status; int addrspace; unsigned long size; static char *keywords[] = {"addrspace", "address", "size", NULL}; size_t r; if (!PyArg_ParseTupleAndKeywords(args, kw, "ikk:", keywords, &addrspace, &addr, &size)) return NULL; if (!size) { PyErr_SetString(PyExc_ValueError, "Zero size buffer"); return NULL; } obj = PyByteArray_FromStringAndSize(0, size); if (!obj) return NULL; r = size; status = kdump_readp(self->ctx, addrspace, addr, PyByteArray_AS_STRING(obj), &r); if (status != kdump_ok) { Py_XDECREF(obj); PyErr_Format(exception_map(status), kdump_err_str(self->ctx)); return NULL; } return obj; }
// from Python/bltinmodule.c static const char * source_as_string(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy) { const char *str; Py_ssize_t size; Py_buffer view; *cmd_copy = NULL; if (PyUnicode_Check(cmd)) { cf->cf_flags |= PyCF_IGNORE_COOKIE; str = PyUnicode_AsUTF8AndSize(cmd, &size); if (str == NULL) return NULL; } else if (PyBytes_Check(cmd)) { str = PyBytes_AS_STRING(cmd); size = PyBytes_GET_SIZE(cmd); } else if (PyByteArray_Check(cmd)) { str = PyByteArray_AS_STRING(cmd); size = PyByteArray_GET_SIZE(cmd); } else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) { /* Copy to NUL-terminated buffer. */ *cmd_copy = PyBytes_FromStringAndSize( (const char *)view.buf, view.len); PyBuffer_Release(&view); if (*cmd_copy == NULL) { return NULL; } str = PyBytes_AS_STRING(*cmd_copy); size = PyBytes_GET_SIZE(*cmd_copy); } else { PyErr_Format(PyExc_TypeError, "%s() arg 1 must be a %s object", funcname, what); return NULL; } if (strlen(str) != (size_t)size) { PyErr_SetString(PyExc_ValueError, "source code string cannot contain null bytes"); Py_CLEAR(*cmd_copy); return NULL; } return str; }
static PyObject *py_JpegObject_setBlock(py_JpegObject *self, PyObject *args) { int x,y,comp; PyObject *coefObj; if (!PyArg_ParseTuple(args, "iiiO!", &x, &y, &comp, &PyByteArray_Type, &coefObj)) { return NULL; } Py_ssize_t size = PyByteArray_GET_SIZE(coefObj); if ((uint)size < BLOCKSIZE) { PyErr_SetString(PyExc_ValueError, "Byte array too small"); return NULL; } JCOEFPTR ptr = get_coeff_block(&self->stuff, x, y, comp); if (ptr == NULL) { PyErr_SetString(PyExc_ValueError, "Coordinates or component out of range"); return NULL; } char *coeffs = PyByteArray_AS_STRING(coefObj); memcpy(ptr, coeffs, BLOCKSIZE); Py_RETURN_NONE; }
static PyObject * c_set(void *ptr, PyObject *value, Py_ssize_t size) { if (PyUnicode_Check(value)) { value = PyUnicode_AsEncodedString(value, _ctypes_conversion_encoding, _ctypes_conversion_errors); if (value == NULL) return NULL; if (PyBytes_GET_SIZE(value) != 1) { Py_DECREF(value); goto error; } *(char *)ptr = PyBytes_AS_STRING(value)[0]; Py_DECREF(value); _RET(value); } if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) { *(char *)ptr = PyBytes_AS_STRING(value)[0]; _RET(value); } if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) { *(char *)ptr = PyByteArray_AS_STRING(value)[0]; _RET(value); } if (PyLong_Check(value)) { long longval = PyLong_AS_LONG(value); if (longval < 0 || longval >= 256) goto error; *(char *)ptr = (char)longval; _RET(value); } error: PyErr_Format(PyExc_TypeError, "one character string expected"); return NULL; }
static PyObject * c_set(void *ptr, PyObject *value, Py_ssize_t size) { if (PyBytes_Check(value) && PyBytes_GET_SIZE(value) == 1) { *(char *)ptr = PyBytes_AS_STRING(value)[0]; _RET(value); } if (PyByteArray_Check(value) && PyByteArray_GET_SIZE(value) == 1) { *(char *)ptr = PyByteArray_AS_STRING(value)[0]; _RET(value); } if (PyLong_Check(value)) { long longval = PyLong_AS_LONG(value); if (longval < 0 || longval >= 256) goto error; *(char *)ptr = (char)longval; _RET(value); } error: PyErr_Format(PyExc_TypeError, "one character string expected"); return NULL; }
static PyObject * _io__IOBase_readline_impl(PyObject *self, Py_ssize_t limit) /*[clinic end generated code: output=4479f79b58187840 input=d0c596794e877bff]*/ { /* For backwards compatibility, a (slowish) readline(). */ PyObject *peek, *buffer, *result; Py_ssize_t old_size = -1; if (_PyObject_LookupAttr(self, _PyIO_str_peek, &peek) < 0) { return NULL; } buffer = PyByteArray_FromStringAndSize(NULL, 0); if (buffer == NULL) { Py_XDECREF(peek); return NULL; } while (limit < 0 || PyByteArray_GET_SIZE(buffer) < limit) { Py_ssize_t nreadahead = 1; PyObject *b; if (peek != NULL) { PyObject *readahead = PyObject_CallFunctionObjArgs(peek, _PyLong_One, NULL); if (readahead == NULL) { /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR occurs so we needn't do it ourselves. */ if (_PyIO_trap_eintr()) { continue; } goto fail; } if (!PyBytes_Check(readahead)) { PyErr_Format(PyExc_OSError, "peek() should have returned a bytes object, " "not '%.200s'", Py_TYPE(readahead)->tp_name); Py_DECREF(readahead); goto fail; } if (PyBytes_GET_SIZE(readahead) > 0) { Py_ssize_t n = 0; const char *buf = PyBytes_AS_STRING(readahead); if (limit >= 0) { do { if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) break; if (buf[n++] == '\n') break; } while (1); } else { do { if (n >= PyBytes_GET_SIZE(readahead)) break; if (buf[n++] == '\n') break; } while (1); } nreadahead = n; } Py_DECREF(readahead); } b = _PyObject_CallMethodId(self, &PyId_read, "n", nreadahead); if (b == NULL) { /* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() when EINTR occurs so we needn't do it ourselves. */ if (_PyIO_trap_eintr()) { continue; } goto fail; } if (!PyBytes_Check(b)) { PyErr_Format(PyExc_OSError, "read() should have returned a bytes object, " "not '%.200s'", Py_TYPE(b)->tp_name); Py_DECREF(b); goto fail; } if (PyBytes_GET_SIZE(b) == 0) { Py_DECREF(b); break; } old_size = PyByteArray_GET_SIZE(buffer); if (PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)) < 0) { Py_DECREF(b); goto fail; } memcpy(PyByteArray_AS_STRING(buffer) + old_size, PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); Py_DECREF(b); if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') break; } result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), PyByteArray_GET_SIZE(buffer)); Py_XDECREF(peek); Py_DECREF(buffer); return result; fail: Py_XDECREF(peek); Py_DECREF(buffer); return NULL; }
static nxweb_result python_on_request(nxweb_http_server_connection* conn, nxweb_http_request* req, nxweb_http_response* resp) { nxb_buffer* nxb=req->nxb; nxweb_handler* handler=conn->handler; const char* request_uri=req->uri; char* query_string=strchr(request_uri, '?'); int ulen=query_string? (query_string-request_uri) : strlen(request_uri); if (query_string) query_string++; int pfxlen=req->path_info? (req->path_info - req->uri) : 0; int plen=ulen-pfxlen; const char* path_info=request_uri+pfxlen; if (handler->uri && *handler->uri) { pfxlen=strlen(handler->uri); ulen=pfxlen+plen; char* u=nxb_alloc_obj(nxb, ulen+1); memcpy(u, handler->uri, pfxlen); memcpy(u+pfxlen, path_info, plen); u[ulen]='\0'; request_uri=u; path_info=request_uri+pfxlen; } const char* host_port=req->host? strchr(req->host, ':') : 0; int content_fd=0; if (req->content_length) { nxd_fwbuffer* fwb=nxweb_get_request_data(req, PYTHON_HANDLER_KEY).ptr; if (fwb) { if (fwb->error || fwb->size > fwb->max_size) { nxweb_send_http_error(resp, 413, "Request Entity Too Large"); // most likely cause return NXWEB_ERROR; } else if (req->content_received!=fwb->size) { nxweb_log_error("content_received does not match upload stored size for %s", req->uri); nxweb_send_http_error(resp, 500, "Internal Server Error"); return NXWEB_ERROR; } else { content_fd=fwb->fd; if (lseek(content_fd, 0, SEEK_SET)==-1) { nxweb_log_error("can't lseek() temp upload file for %s", req->uri); nxweb_send_http_error(resp, 500, "Internal Server Error"); return NXWEB_ERROR; } } } } nxweb_log_debug("invoke python"); PyGILState_STATE gstate=PyGILState_Ensure(); PyObject* py_func_args=PyTuple_New(1); PyObject* py_environ=PyDict_New(); assert(PyDict_Check(py_environ)); dict_set(py_environ, "SERVER_NAME", PyString_FromStringAndSize(req->host, host_port? (host_port-req->host) : strlen(req->host))); dict_set(py_environ, "SERVER_PORT", PyString_FromString(host_port? host_port+1 : "")); dict_set(py_environ, "SERVER_PROTOCOL", PyString_FromString(req->http11? "HTTP/1.1" : "HTTP/1.0")); dict_set(py_environ, "SERVER_SOFTWARE", PyString_FromString(PACKAGE_STRING)); dict_set(py_environ, "GATEWAY_INTERFACE", PyString_FromString("CGI/1.1")); dict_set(py_environ, "REQUEST_METHOD", PyString_FromString(req->method)); dict_set(py_environ, "REQUEST_URI", PyString_FromStringAndSize(request_uri, ulen)); dict_set(py_environ, "SCRIPT_NAME", PyString_FromStringAndSize(request_uri, pfxlen)); dict_set(py_environ, "PATH_INFO", PyString_FromStringAndSize(path_info, plen)); dict_set(py_environ, "QUERY_STRING", PyString_FromString(query_string? query_string : "")); dict_set(py_environ, "REMOTE_ADDR", PyString_FromString(conn->remote_addr)); dict_set(py_environ, "CONTENT_TYPE", PyString_FromString(req->content_type? req->content_type : "")); dict_set(py_environ, "CONTENT_LENGTH", PyInt_FromLong(req->content_received)); if (req->cookie) dict_set(py_environ, "HTTP_COOKIE", PyString_FromString(req->cookie)); if (req->host) dict_set(py_environ, "HTTP_HOST", PyString_FromString(req->host)); if (req->user_agent) dict_set(py_environ, "HTTP_USER_AGENT", PyString_FromString(req->user_agent)); if (req->if_modified_since) { struct tm tm; gmtime_r(&req->if_modified_since, &tm); char ims[32]; nxweb_format_http_time(ims, &tm); dict_set(py_environ, "HTTP_IF_MODIFIED_SINCE", PyString_FromString(ims)); } if (req->headers) { // write added headers // encode http headers into CGI variables; see 4.1.18 in https://tools.ietf.org/html/rfc3875 char hname[256]; memcpy(hname, "HTTP_", 5); char* h=hname+5; nx_simple_map_entry* itr; for (itr=nx_simple_map_itr_begin(req->headers); itr; itr=nx_simple_map_itr_next(itr)) { nx_strtoupper(h, itr->name); char* p; for (p=h; *p; p++) { if (*p=='-') *p='_'; } dict_set(py_environ, hname, PyString_FromString(itr->value)); } } dict_set(py_environ, "wsgi.url_scheme", PyString_FromString(conn->secure? "https" : "http")); if (req->content_length) { if (content_fd) { dict_set(py_environ, "nxweb.req.content_fd", PyInt_FromLong(content_fd)); } else { dict_set(py_environ, "nxweb.req.content", PyByteArray_FromStringAndSize(req->content? req->content : "", req->content_received)); } } if (req->if_modified_since) dict_set(py_environ, "nxweb.req.if_modified_since", PyLong_FromLong(req->if_modified_since)); dict_set(py_environ, "nxweb.req.uid", PyLong_FromLongLong(req->uid)); if (req->parent_req) { nxweb_http_request* preq=req->parent_req; while (preq->parent_req) preq=preq->parent_req; // find root request if (preq->uid) { dict_set(py_environ, "nxweb.req.root_uid", PyLong_FromLongLong(preq->uid)); } } // call python PyTuple_SetItem(py_func_args, 0, py_environ); PyObject* py_result=PyObject_CallObject(py_nxweb_on_request_func, py_func_args); Py_DECREF(py_func_args); if (py_result && PyTuple_Check(py_result) && PyTuple_Size(py_result)==3) { PyObject* py_status=PyTuple_GET_ITEM(py_result, 0); PyObject* py_headers=PyTuple_GET_ITEM(py_result, 1); PyObject* py_body=PyTuple_GET_ITEM(py_result, 2); if (py_status && PyString_Check(py_status)) { const char* status_string=PyString_AS_STRING(py_status); int status_code=0; const char* p=status_string; while (*p && *p>='0' && *p<='9') { status_code=status_code*10+(*p-'0'); p++; } while (*p && *p==' ') p++; if (status_code>=200 && status_code<600 && *p) { resp->status_code=status_code; resp->status=nxb_copy_str(nxb, p); } } if (py_headers && PyList_Check(py_headers)) { const int size=PyList_Size(py_headers); int i; for (i=0; i<size; i++) { PyObject* py_header_tuple=PyList_GET_ITEM(py_headers, i); if (py_header_tuple && PyTuple_Check(py_header_tuple) && PyTuple_Size(py_header_tuple)==2) { PyObject* py_name=PyTuple_GET_ITEM(py_header_tuple, 0); PyObject* py_value=PyTuple_GET_ITEM(py_header_tuple, 1); if (py_name && PyString_Check(py_name) && py_value && PyString_Check(py_value)) { nxweb_add_response_header_safe(resp, PyString_AS_STRING(py_name), PyString_AS_STRING(py_value)); } } } } if ((!resp->status_code || resp->status_code==200) && !resp->content_type) resp->content_type="text/html"; char* rcontent=0; nxe_ssize_t rsize=0; if (PyByteArray_Check(py_body)) { rcontent=PyByteArray_AS_STRING(py_body); rsize=PyByteArray_Size(py_body); } else if (PyString_Check(py_body)) { rcontent=PyString_AS_STRING(py_body); rsize=PyString_Size(py_body); } if (rcontent && rsize>0) nxweb_response_append_data(resp, rcontent, rsize); } else if (py_result && PyString_Check(py_result)) { resp->status_code=500; resp->status="Internal Server Error"; resp->content_type="text/html"; nxweb_log_error("python call failed: %s", PyString_AS_STRING(py_result)); nxweb_response_printf(resp, "python call failed: %H", PyString_AS_STRING(py_result)); } else { PyErr_Print(); nxweb_log_error("python call failed"); nxweb_response_printf(resp, "python call failed"); } Py_XDECREF(py_result); // Release the thread. No Python API allowed beyond this point. PyGILState_Release(gstate); nxweb_log_debug("invoke python complete"); return NXWEB_OK; }
static int encode_common(PyObject **o, void **buf, size_t *nbuf, lcb_uint32_t flags) { PyObject *bytesobj; Py_ssize_t plen; int rv; if (flags == PYCBC_FMT_UTF8) { #if PY_MAJOR_VERSION == 2 if (PyString_Check(*o)) { #else if (0) { #endif bytesobj = *o; Py_INCREF(*o); } else { if (!PyUnicode_Check(*o)) { PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ENCODING, 0, "Must be unicode or string", *o); return -1; } bytesobj = PyUnicode_AsUTF8String(*o); } } else if (flags == PYCBC_FMT_BYTES) { if (PyBytes_Check(*o) || PyByteArray_Check(*o)) { bytesobj = *o; Py_INCREF(*o); } else { PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ENCODING, 0, "Must be bytes or bytearray", *o); return -1; } } else { PyObject *args = NULL; PyObject *helper; if (flags == PYCBC_FMT_PICKLE) { helper = pycbc_helpers.pickle_encode; } else if (flags == PYCBC_FMT_JSON) { helper = pycbc_helpers.json_encode; } else { PYCBC_EXC_WRAP(PYCBC_EXC_ARGUMENTS, 0, "Unrecognized format"); return -1; } args = PyTuple_Pack(1, *o); bytesobj = PyObject_CallObject(helper, args); Py_DECREF(args); if (!bytesobj) { PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ENCODING, 0, "Couldn't encode value", *o); return -1; } if (!PyBytes_Check(bytesobj)) { PyObject *old = bytesobj; bytesobj = convert_to_bytesobj(old); Py_DECREF(old); if (!bytesobj) { return -1; } } } if (PyByteArray_Check(bytesobj)) { *buf = PyByteArray_AS_STRING(bytesobj); plen = PyByteArray_GET_SIZE(bytesobj); rv = 0; } else { rv = PyBytes_AsStringAndSize(bytesobj, (char**)buf, &plen); } if (rv < 0) { Py_DECREF(bytesobj); PYCBC_EXC_WRAP(PYCBC_EXC_ENCODING, 0, "Couldn't encode value"); return -1; } *nbuf = plen; *o = bytesobj; return 0; } static int decode_common(PyObject **vp, const char *buf, size_t nbuf, lcb_uint32_t flags) { PyObject *decoded = NULL; lcb_U32 c_flags, l_flags; c_flags = flags & PYCBC_FMT_COMMON_MASK; l_flags = flags & PYCBC_FMT_LEGACY_MASK; #define FMT_MATCHES(fmtbase) \ (c_flags == PYCBC_FMT_COMMON_##fmtbase || l_flags == PYCBC_FMT_LEGACY_##fmtbase) if (FMT_MATCHES(UTF8)) { decoded = convert_to_string(buf, nbuf, CONVERT_MODE_UTF8_ONLY); if (!decoded) { return -1; } } else if (FMT_MATCHES(BYTES)) { GT_BYTES: decoded = convert_to_string(buf, nbuf, CONVERT_MODE_BYTES_ONLY); pycbc_assert(decoded); } else { PyObject *converter = NULL; PyObject *args = NULL; PyObject *first_arg = NULL; if (FMT_MATCHES(PICKLE)) { converter = pycbc_helpers.pickle_decode; first_arg = convert_to_string(buf, nbuf, CONVERT_MODE_BYTES_ONLY); pycbc_assert(first_arg); } else if (FMT_MATCHES(JSON)) { converter = pycbc_helpers.json_decode; first_arg = convert_to_string(buf, nbuf, CONVERT_MODE_UTF8_ONLY); if (!first_arg) { return -1; } } else { PyErr_Warn(PyExc_UserWarning, "Unrecognized flags. Forcing bytes"); goto GT_BYTES; } pycbc_assert(first_arg); args = PyTuple_Pack(1, first_arg); decoded = PyObject_CallObject(converter, args); Py_DECREF(args); Py_DECREF(first_arg); } if (!decoded) { PyObject *bytes_tmp = PyBytes_FromStringAndSize(buf, nbuf); PYCBC_EXC_WRAP_OBJ(PYCBC_EXC_ENCODING, 0, "Failed to decode bytes", bytes_tmp); Py_XDECREF(bytes_tmp); return -1; } *vp = decoded; return 0; #undef FMT_MATCHES }
bool AllocateMore(SQLLEN cbAdd) { // cbAdd // The number of bytes (cb --> count of bytes) to add. if (cbAdd == 0) return true; SQLLEN newSize = bufferSize + cbAdd; if (usingStack) { // This is the first call and `buffer` points to stack memory. Allocate a new object and copy the stack // data into it. char* stackBuffer = buffer; if (dataType == SQL_C_CHAR) { bufferOwner = PyBytes_FromStringAndSize(0, newSize); buffer = bufferOwner ? PyBytes_AS_STRING(bufferOwner) : 0; } else if (dataType == SQL_C_BINARY) { #if PY_VERSION_HEX >= 0x02060000 bufferOwner = PyByteArray_FromStringAndSize(0, newSize); buffer = bufferOwner ? PyByteArray_AS_STRING(bufferOwner) : 0; #else bufferOwner = PyBytes_FromStringAndSize(0, newSize); buffer = bufferOwner ? PyBytes_AS_STRING(bufferOwner) : 0; #endif } else if (sizeof(SQLWCHAR) == Py_UNICODE_SIZE) { // Allocate directly into a Unicode object. bufferOwner = PyUnicode_FromUnicode(0, newSize / element_size); buffer = bufferOwner ? (char*)PyUnicode_AsUnicode(bufferOwner) : 0; } else { // We're Unicode, but SQLWCHAR and Py_UNICODE don't match, so maintain our own SQLWCHAR buffer. bufferOwner = 0; buffer = (char*)pyodbc_malloc((size_t)newSize); } if (buffer == 0) return false; usingStack = false; memcpy(buffer, stackBuffer, (size_t)bufferSize); bufferSize = newSize; return true; } if (bufferOwner && PyUnicode_CheckExact(bufferOwner)) { if (PyUnicode_Resize(&bufferOwner, newSize / element_size) == -1) return false; buffer = (char*)PyUnicode_AsUnicode(bufferOwner); } #if PY_VERSION_HEX >= 0x02060000 else if (bufferOwner && PyByteArray_CheckExact(bufferOwner)) { if (PyByteArray_Resize(bufferOwner, newSize) == -1) return false; buffer = PyByteArray_AS_STRING(bufferOwner); } #else else if (bufferOwner && PyBytes_CheckExact(bufferOwner)) { if (_PyBytes_Resize(&bufferOwner, newSize) == -1) return false; buffer = PyBytes_AS_STRING(bufferOwner); } #endif else { char* tmp = (char*)realloc(buffer, (size_t)newSize); if (tmp == 0) return false; buffer = tmp; } bufferSize = newSize; return true; }
PyObject* Effect::wrapSetImage(PyObject *self, PyObject *args) { // get the effect Effect * effect = getEffect(); // check if we have aborted already if (effect->_abortRequested) { return Py_BuildValue(""); } // determine the timeout int timeout = effect->_timeout; if (timeout > 0) { timeout = effect->_endTime - QDateTime::currentMSecsSinceEpoch(); // we are done if the time has passed if (timeout <= 0) { return Py_BuildValue(""); } } // bytearray of values int width, height; PyObject * bytearray = nullptr; if (PyArg_ParseTuple(args, "iiO", &width, &height, &bytearray)) { if (PyByteArray_Check(bytearray)) { int length = PyByteArray_Size(bytearray); if (length == 3 * width * height) { Image<ColorRgb> image(width, height); char * data = PyByteArray_AS_STRING(bytearray); memcpy(image.memptr(), data, length); effect->_imageProcessor->process(image, effect->_colors); effect->setColors(effect->_priority, effect->_colors, timeout, false); return Py_BuildValue(""); } else { PyErr_SetString(PyExc_RuntimeError, "Length of bytearray argument should be 3*width*height"); return nullptr; } } else { PyErr_SetString(PyExc_RuntimeError, "Argument 3 is not a bytearray"); return nullptr; } } else { return nullptr; } // error PyErr_SetString(PyExc_RuntimeError, "Unknown error"); return nullptr; }
static CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) { #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT if ( #if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII __Pyx_sys_getdefaultencoding_not_ascii && #endif PyUnicode_Check(o)) { #if PY_VERSION_HEX < 0x03030000 char* defenc_c; // borrowed, cached reference PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); if (!defenc) return NULL; defenc_c = PyBytes_AS_STRING(defenc); #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII { char* end = defenc_c + PyBytes_GET_SIZE(defenc); char* c; for (c = defenc_c; c < end; c++) { if ((unsigned char) (*c) >= 128) { // raise the error PyUnicode_AsASCIIString(o); return NULL; } } } #endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/ *length = PyBytes_GET_SIZE(defenc); return defenc_c; #else /* PY_VERSION_HEX < 0x03030000 */ if (PyUnicode_READY(o) == -1) return NULL; #if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII if (PyUnicode_IS_ASCII(o)) { // cached for the lifetime of the object *length = PyUnicode_GET_DATA_SIZE(o); return PyUnicode_AsUTF8(o); } else { // raise the error PyUnicode_AsASCIIString(o); return NULL; } #else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */ return PyUnicode_AsUTF8AndSize(o, length); #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */ #endif /* PY_VERSION_HEX < 0x03030000 */ } else #endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */ #if PY_VERSION_HEX >= 0x02060000 if (PyByteArray_Check(o)) { *length = PyByteArray_GET_SIZE(o); return PyByteArray_AS_STRING(o); } else #endif { char* result; int r = PyBytes_AsStringAndSize(o, &result, length); if (unlikely(r < 0)) { return NULL; } else { return result; } } }
NUITKA_MAY_BE_UNUSED static PyObject *TO_INT2( PyObject *value, PyObject *base ) { // TODO: Need to check if 3.4 is really the first version to do this. #if PYTHON_VERSION < 340 long base_int = PyInt_AsLong( base ); #else Py_ssize_t base_int = PyNumber_AsSsize_t( base, NULL ); #endif if (unlikely( base_int == -1 )) { PyObject *error = GET_ERROR_OCCURRED(); if (likely( error )) { #if PYTHON_VERSION >= 300 if ( EXCEPTION_MATCH_BOOL_SINGLE( error, PyExc_OverflowError ) ) { PyErr_Format( PyExc_ValueError, #if PYTHON_VERSION < 324 "int() arg 2 must be >= 2 and <= 36" #else "int() base must be >= 2 and <= 36" #endif ); } #endif return NULL; } } #if PYTHON_VERSION >= 300 if (unlikely( ( base_int != 0 && base_int < 2 ) || base_int > 36 )) { PyErr_Format( PyExc_ValueError, #if PYTHON_VERSION < 324 "int() arg 2 must be >= 2 and <= 36" #else "int() base must be >= 2 and <= 36" #endif ); return NULL; } #endif #if PYTHON_VERSION < 300 if (unlikely( !Nuitka_String_Check( value ) && !PyUnicode_Check( value ) )) { PyErr_Format( PyExc_TypeError, "int() can't convert non-string with explicit base" ); return NULL; } char *value_str = Nuitka_String_AsString( value ); if (unlikely( value_str == NULL )) { return NULL; } PyObject *result = PyInt_FromString( value_str, NULL, base_int ); if (unlikely( result == NULL )) { return NULL; } return result; #else if ( PyUnicode_Check( value ) ) { #if PYTHON_VERSION < 330 char *value_str = Nuitka_String_AsString( value ); if (unlikely( value_str == NULL )) { return NULL; } PyObject *result = PyInt_FromString( value_str, NULL, base_int ); if (unlikely( result == NULL )) { return NULL; } return result; #else return PyLong_FromUnicodeObject( value, (int)base_int ); #endif } else if ( PyBytes_Check( value ) || PyByteArray_Check( value ) ) { // Check for "NUL" as PyLong_FromString has no length parameter, Py_ssize_t size = Py_SIZE( value ); char *value_str; if ( PyByteArray_Check( value ) ) { value_str = PyByteArray_AS_STRING( value ); } else { value_str = PyBytes_AS_STRING( value ); } PyObject *result = NULL; if ( size != 0 && strlen( value_str ) == (size_t)size ) { result = PyInt_FromString( value_str, NULL, (int)base_int ); } if (unlikely( result == NULL )) { PyErr_Format( PyExc_ValueError, "invalid literal for int() with base %d: %R", base_int, value ); return NULL; } return result; } else { PyErr_Format( PyExc_TypeError, "int() can't convert non-string with explicit base" ); return NULL; } #endif }
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; }
static uint32_t pointless_export_py_rec(pointless_export_state_t* state, PyObject* py_object, uint32_t depth) { // don't go too deep if (depth >= POINTLESS_MAX_DEPTH) { PyErr_SetString(PyExc_ValueError, "structure is too deep"); state->is_error = 1; printf("line: %i\n", __LINE__); state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } // check simple types first uint32_t handle = POINTLESS_CREATE_VALUE_FAIL; // return an error on failure #define RETURN_OOM(state) {PyErr_NoMemory(); (state)->is_error = 1; printf("line: %i\n", __LINE__); state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL;} #define RETURN_OOM_IF_FAIL(handle, state) if ((handle) == POINTLESS_CREATE_VALUE_FAIL) RETURN_OOM(state); // booleans, need this above integer check, cause PyInt_Check return 1 for booleans if (PyBool_Check(py_object)) { if (py_object == Py_True) handle = pointless_create_boolean_true(&state->c); else handle = pointless_create_boolean_false(&state->c); RETURN_OOM_IF_FAIL(handle, state); // integer } else if (PyInt_Check(py_object)) { long v = PyInt_AS_LONG(py_object); // unsigned if (v >= 0) { if (v > UINT32_MAX) { PyErr_Format(PyExc_ValueError, "integer too large for mere 32 bits"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_u32(&state->c, (uint32_t)v); // signed } else { if (!(INT32_MIN <= v && v <= INT32_MAX)) { PyErr_Format(PyExc_ValueError, "integer too large for mere 32 bits with a sign"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_i32(&state->c, (int32_t)v); } RETURN_OOM_IF_FAIL(handle, state); // long } else if (PyLong_Check(py_object)) { // this will raise an overflow error if number is outside the legal range of PY_LONG_LONG PY_LONG_LONG v = PyLong_AsLongLong(py_object); // if there was an exception, clear it, and set our own if (PyErr_Occurred()) { PyErr_Clear(); PyErr_SetString(PyExc_ValueError, "value of long is way beyond what we can store right now"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } // unsigned if (v >= 0) { if (v > UINT32_MAX) { PyErr_Format(PyExc_ValueError, "long too large for mere 32 bits"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_u32(&state->c, (uint32_t)v); // signed } else { if (!(INT32_MIN <= v && v <= INT32_MAX)) { PyErr_Format(PyExc_ValueError, "long too large for mere 32 bits with a sign"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_i32(&state->c, (int32_t)v); } RETURN_OOM_IF_FAIL(handle, state); // None object } else if (py_object == Py_None) { handle = pointless_create_null(&state->c); RETURN_OOM_IF_FAIL(handle, state); } else if (PyFloat_Check(py_object)) { handle = pointless_create_float(&state->c, (float)PyFloat_AS_DOUBLE(py_object)); RETURN_OOM_IF_FAIL(handle, state); } if (handle != POINTLESS_CREATE_VALUE_FAIL) return handle; // remaining types are containers/big-values, which we track // either for space-savings or maintaining circular references // if object has been seen before, return its handle handle = pointless_export_get_seen(state, py_object); if (handle != POINTLESS_CREATE_VALUE_FAIL) return handle; // list/tuple object if (PyList_Check(py_object) || PyTuple_Check(py_object)) { // create and cache handle assert(is_container(py_object)); handle = pointless_create_vector_value(&state->c); RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // populate vector Py_ssize_t i, n_items = PyList_Check(py_object) ? PyList_GET_SIZE(py_object) : PyTuple_GET_SIZE(py_object); for (i = 0; i < n_items; i++) { PyObject* child = PyList_Check(py_object) ? PyList_GET_ITEM(py_object, i) : PyTuple_GET_ITEM(py_object, i); uint32_t child_handle = pointless_export_py_rec(state, child, depth + 1); if (child_handle == POINTLESS_CREATE_VALUE_FAIL) return child_handle; if (pointless_create_vector_value_append(&state->c, handle, child_handle) == POINTLESS_CREATE_VALUE_FAIL) { RETURN_OOM(state); } } // pointless value vectors } else if (PyPointlessVector_Check(py_object)) { // currently, we only support value vectors, they are simple PyPointlessVector* v = (PyPointlessVector*)py_object; const char* error = 0; switch(v->v->type) { case POINTLESS_VECTOR_VALUE: case POINTLESS_VECTOR_VALUE_HASHABLE: handle = pointless_recreate_value(&v->pp->p, v->v, &state->c, &error); if (handle == POINTLESS_CREATE_VALUE_FAIL) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; PyErr_Format(PyExc_ValueError, "pointless_recreate_value(): %s", error); return POINTLESS_CREATE_VALUE_FAIL; } break; case POINTLESS_VECTOR_I8: handle = pointless_create_vector_i8_owner(&state->c, pointless_reader_vector_i8(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_U8: handle = pointless_create_vector_u8_owner(&state->c, pointless_reader_vector_u8(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_I16: handle = pointless_create_vector_i16_owner(&state->c, pointless_reader_vector_i16(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_U16: handle = pointless_create_vector_u16_owner(&state->c, pointless_reader_vector_u16(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_I32: handle = pointless_create_vector_i32_owner(&state->c, pointless_reader_vector_i32(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_U32: handle = pointless_create_vector_u32_owner(&state->c, pointless_reader_vector_u32(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_I64: handle = pointless_create_vector_i64_owner(&state->c, pointless_reader_vector_i64(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_U64: handle = pointless_create_vector_u64_owner(&state->c, pointless_reader_vector_u64(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_FLOAT: handle = pointless_create_vector_float_owner(&state->c, pointless_reader_vector_float(&v->pp->p, v->v) + v->slice_i, v->slice_n); break; case POINTLESS_VECTOR_EMPTY: handle = pointless_create_vector_value(&state->c); break; default: state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; PyErr_SetString(PyExc_ValueError, "internal error: illegal type for primitive vector"); return POINTLESS_CREATE_VALUE_FAIL; } RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // python bytearray } else if (PyByteArray_Check(py_object)) { // create handle and hand over the memory Py_ssize_t n_items = PyByteArray_GET_SIZE(py_object); if (n_items > UINT32_MAX) { PyErr_SetString(PyExc_ValueError, "bytearray has too many items"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_vector_u8_owner(&state->c, (uint8_t*)PyByteArray_AS_STRING(py_object), (uint32_t)n_items); RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // primitive vectors } else if (PyPointlessPrimVector_Check(py_object)) { // we just hand over the memory PyPointlessPrimVector* prim_vector = (PyPointlessPrimVector*)py_object; uint32_t n_items = pointless_dynarray_n_items(&prim_vector->array); void* data = prim_vector->array._data; switch (prim_vector->type) { case POINTLESS_PRIM_VECTOR_TYPE_I8: handle = pointless_create_vector_i8_owner(&state->c, (int8_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_U8: handle = pointless_create_vector_u8_owner(&state->c, (uint8_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_I16: handle = pointless_create_vector_i16_owner(&state->c, (int16_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_U16: handle = pointless_create_vector_u16_owner(&state->c, (uint16_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_I32: handle = pointless_create_vector_i32_owner(&state->c, (int32_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_U32: handle = pointless_create_vector_u32_owner(&state->c, (uint32_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_I64: handle = pointless_create_vector_i64_owner(&state->c, (int64_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_U64: handle = pointless_create_vector_u64_owner(&state->c, (uint64_t*)data, n_items); break; case POINTLESS_PRIM_VECTOR_TYPE_FLOAT: handle = pointless_create_vector_float_owner(&state->c, (float*)data, n_items); break; default: PyErr_SetString(PyExc_ValueError, "internal error: illegal type for primitive vector"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; return POINTLESS_CREATE_VALUE_FAIL; } RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // unicode object } else if (PyUnicode_Check(py_object)) { // get it from python Py_UNICODE* python_buffer = PyUnicode_AS_UNICODE(py_object); // string must not contain zero's Py_ssize_t s_len_python = PyUnicode_GET_SIZE(py_object); #if Py_UNICODE_SIZE == 4 uint32_t s_len_pointless = pointless_ucs4_len(python_buffer); #else uint32_t s_len_pointless = pointless_ucs2_len(python_buffer); #endif if (s_len_python < 0 || (uint64_t)s_len_python != s_len_pointless) { PyErr_SetString(PyExc_ValueError, "unicode string contains a zero, where it shouldn't"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; return POINTLESS_CREATE_VALUE_FAIL; } #if Py_UNICODE_SIZE == 4 if (state->unwiden_strings && pointless_is_ucs4_ascii((uint32_t*)python_buffer)) handle = pointless_create_string_ucs4(&state->c, python_buffer); else handle = pointless_create_unicode_ucs4(&state->c, python_buffer); #else if (state->unwiden_strings && pointless_is_ucs2_ascii(python_buffer)) handle = pointless_create_string_ucs2(&state->c, python_buffer); else handle = pointless_create_unicode_ucs2(&state->c, python_buffer); #endif RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // string object } else if (PyString_Check(py_object)) { // get it from python uint8_t* python_buffer = (uint8_t*)PyString_AS_STRING(py_object); // string must not contain zero's Py_ssize_t s_len_python = PyString_GET_SIZE(py_object); uint32_t s_len_pointless = pointless_ascii_len(python_buffer); if (s_len_python < 0 || (uint64_t)s_len_python != s_len_pointless) { PyErr_SetString(PyExc_ValueError, "string contains a zero, where it shouldn't"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; return POINTLESS_CREATE_VALUE_FAIL; } handle = pointless_create_string_ascii(&state->c, python_buffer); RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // dict object } else if (PyDict_Check(py_object)) { handle = pointless_create_map(&state->c); RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } PyObject* key = 0; PyObject* value = 0; Py_ssize_t pos = 0; while (PyDict_Next(py_object, &pos, &key, &value)) { uint32_t key_handle = pointless_export_py_rec(state, key, depth + 1); uint32_t value_handle = pointless_export_py_rec(state, value, depth + 1); if (key_handle == POINTLESS_CREATE_VALUE_FAIL || value_handle == POINTLESS_CREATE_VALUE_FAIL) break; if (pointless_create_map_add(&state->c, handle, key_handle, value_handle) == POINTLESS_CREATE_VALUE_FAIL) { PyErr_SetString(PyExc_ValueError, "error adding key/value pair to map"); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; break; } } if (state->is_error) { return POINTLESS_CREATE_VALUE_FAIL; } // set object } else if (PyAnySet_Check(py_object)) { PyObject* iterator = PyObject_GetIter(py_object); PyObject* item = 0; if (iterator == 0) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } // get a handle handle = pointless_create_set(&state->c); RETURN_OOM_IF_FAIL(handle, state); // cache object if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // iterate over it while ((item = PyIter_Next(iterator)) != 0) { uint32_t item_handle = pointless_export_py_rec(state, item, depth + 1); if (item_handle == POINTLESS_CREATE_VALUE_FAIL) break; if (pointless_create_set_add(&state->c, handle, item_handle) == POINTLESS_CREATE_VALUE_FAIL) { PyErr_SetString(PyExc_ValueError, "error adding item to set"); printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; break; } } Py_DECREF(iterator); if (PyErr_Occurred()) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; return POINTLESS_CREATE_VALUE_FAIL; } // bitvector } else if (PyPointlessBitvector_Check(py_object)) { PyPointlessBitvector* bitvector = (PyPointlessBitvector*)py_object; if (bitvector->is_pointless) { uint32_t i, n_bits = pointless_reader_bitvector_n_bits(&bitvector->pointless_pp->p, bitvector->pointless_v); void* bits = pointless_calloc(ICEIL(n_bits, 8), 1); if (bits == 0) { RETURN_OOM(state); } for (i = 0; i < n_bits; i++) { if (pointless_reader_bitvector_is_set(&bitvector->pointless_pp->p, bitvector->pointless_v, i)) bm_set_(bits, i); } if (state->normalize_bitvector) handle = pointless_create_bitvector(&state->c, bits, n_bits); else handle = pointless_create_bitvector_no_normalize(&state->c, bits, n_bits); pointless_free(bits); bits = 0; } else { if (state->normalize_bitvector) handle = pointless_create_bitvector(&state->c, bitvector->primitive_bits, bitvector->primitive_n_bits); else handle = pointless_create_bitvector_no_normalize(&state->c, bitvector->primitive_bits, bitvector->primitive_n_bits); } RETURN_OOM_IF_FAIL(handle, state); if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } } else if (PyPointlessSet_Check(py_object)) { PyPointlessSet* set = (PyPointlessSet*)py_object; const char* error = 0; handle = pointless_recreate_value(&set->pp->p, set->v, &state->c, &error); if (handle == POINTLESS_CREATE_VALUE_FAIL) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; PyErr_Format(PyExc_ValueError, "pointless_recreate_value(): %s", error); return POINTLESS_CREATE_VALUE_FAIL; } if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } } else if (PyPointlessMap_Check(py_object)) { PyPointlessMap* map = (PyPointlessMap*)py_object; const char* error = 0; handle = pointless_recreate_value(&map->pp->p, map->v, &state->c, &error); if (handle == POINTLESS_CREATE_VALUE_FAIL) { printf("line: %i\n", __LINE__); state->is_error = 1; state->error_line = __LINE__; PyErr_Format(PyExc_ValueError, "pointless_recreate_value(): %s", error); return POINTLESS_CREATE_VALUE_FAIL; } if (!pointless_export_set_seen(state, py_object, handle)) { RETURN_OOM(state); } // type not supported } else { PyErr_Format(PyExc_ValueError, "type <%s> not supported", py_object->ob_type->tp_name); state->error_line = __LINE__; printf("line: %i\n", __LINE__); state->is_error = 1; } #undef RETURN_OOM #undef RETURN_IF_OOM return handle; }
static PyObject * iobase_readline(PyObject *self, PyObject *args) { /* For backwards compatibility, a (slowish) readline(). */ Py_ssize_t limit = -1; int has_peek = 0; PyObject *buffer, *result; Py_ssize_t old_size = -1; if (!PyArg_ParseTuple(args, "|O&:readline", &_PyIO_ConvertSsize_t, &limit)) { return NULL; } if (PyObject_HasAttrString(self, "peek")) has_peek = 1; buffer = PyByteArray_FromStringAndSize(NULL, 0); if (buffer == NULL) return NULL; while (limit < 0 || Py_SIZE(buffer) < limit) { Py_ssize_t nreadahead = 1; PyObject *b; if (has_peek) { PyObject *readahead = PyObject_CallMethod(self, "peek", "i", 1); if (readahead == NULL) goto fail; if (!PyBytes_Check(readahead)) { PyErr_Format(PyExc_IOError, "peek() should have returned a bytes object, " "not '%.200s'", Py_TYPE(readahead)->tp_name); Py_DECREF(readahead); goto fail; } if (PyBytes_GET_SIZE(readahead) > 0) { Py_ssize_t n = 0; const char *buf = PyBytes_AS_STRING(readahead); if (limit >= 0) { do { if (n >= PyBytes_GET_SIZE(readahead) || n >= limit) break; if (buf[n++] == '\n') break; } while (1); } else { do { if (n >= PyBytes_GET_SIZE(readahead)) break; if (buf[n++] == '\n') break; } while (1); } nreadahead = n; } Py_DECREF(readahead); } b = PyObject_CallMethod(self, "read", "n", nreadahead); if (b == NULL) goto fail; if (!PyBytes_Check(b)) { PyErr_Format(PyExc_IOError, "read() should have returned a bytes object, " "not '%.200s'", Py_TYPE(b)->tp_name); Py_DECREF(b); goto fail; } if (PyBytes_GET_SIZE(b) == 0) { Py_DECREF(b); break; } old_size = PyByteArray_GET_SIZE(buffer); PyByteArray_Resize(buffer, old_size + PyBytes_GET_SIZE(b)); memcpy(PyByteArray_AS_STRING(buffer) + old_size, PyBytes_AS_STRING(b), PyBytes_GET_SIZE(b)); Py_DECREF(b); if (PyByteArray_AS_STRING(buffer)[PyByteArray_GET_SIZE(buffer) - 1] == '\n') break; } result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(buffer), PyByteArray_GET_SIZE(buffer)); Py_DECREF(buffer); return result; fail: Py_DECREF(buffer); return NULL; }
static PyObject *PySSL_SSLread(PySSLObject *self, PyObject *args) { PyObject *dest = NULL; Py_buffer buf; int buf_passed = 0; int count = -1; char *mem; /* XXX this should use Py_ssize_t */ int len = 1024; int sockstate; int err; int nonblocking; PySocketSockObject *sock = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); if (((PyObject*)sock) == Py_None) { _setSSLError("Underlying socket connection gone", PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); return NULL; } if (!PyArg_ParseTuple(args, "|Oi:read", &dest, &count)) return NULL; if ((dest == NULL) || (dest == Py_None)) { if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len))) return NULL; mem = PyByteArray_AS_STRING(dest); } else if (PyLong_Check(dest)) { len = PyLong_AS_LONG(dest); if (!(dest = PyByteArray_FromStringAndSize((char *) 0, len))) return NULL; mem = PyByteArray_AS_STRING(dest); } else { if (PyObject_GetBuffer(dest, &buf, PyBUF_CONTIG) < 0) return NULL; mem = buf.buf; len = buf.len; if ((count > 0) && (count <= len)) len = count; buf_passed = 1; } /* just in case the blocking state of the socket has been changed */ nonblocking = (sock->sock_timeout >= 0.0); BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking); BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking); /* first check if there are bytes ready to be read */ PySSL_BEGIN_ALLOW_THREADS count = SSL_pending(self->ssl); PySSL_END_ALLOW_THREADS if (!count) { sockstate = check_socket_and_wait_for_timeout(sock, 0); if (sockstate == SOCKET_HAS_TIMED_OUT) { PyErr_SetString(PySSLErrorObject, "The read operation timed out"); goto error; } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { PyErr_SetString(PySSLErrorObject, "Underlying socket too large for select()."); goto error; } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { count = 0; goto done; } } do { err = 0; PySSL_BEGIN_ALLOW_THREADS count = SSL_read(self->ssl, mem, len); err = SSL_get_error(self->ssl, count); PySSL_END_ALLOW_THREADS if (PyErr_CheckSignals()) goto error; if (err == SSL_ERROR_WANT_READ) { sockstate = check_socket_and_wait_for_timeout(sock, 0); } else if (err == SSL_ERROR_WANT_WRITE) { sockstate = check_socket_and_wait_for_timeout(sock, 1); } else if ((err == SSL_ERROR_ZERO_RETURN) && (SSL_get_shutdown(self->ssl) == SSL_RECEIVED_SHUTDOWN)) { count = 0; goto done; } else { sockstate = SOCKET_OPERATION_OK; } if (sockstate == SOCKET_HAS_TIMED_OUT) { PyErr_SetString(PySSLErrorObject, "The read operation timed out"); goto error; } else if (sockstate == SOCKET_IS_NONBLOCKING) { break; } } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); if (count <= 0) { PySSL_SetError(self, count, __FILE__, __LINE__); goto error; } done: if (!buf_passed) { PyObject *res = PyBytes_FromStringAndSize(mem, count); Py_DECREF(dest); return res; } else { PyBuffer_Release(&buf); return PyLong_FromLong(count); } error: if (!buf_passed) { Py_DECREF(dest); } else { PyBuffer_Release(&buf); } return NULL; }
PyObject* Effect::wrapSetColor(PyObject *self, PyObject *args) { // get the effect Effect * effect = getEffect(); // check if we have aborted already if (effect->_abortRequested) { return Py_BuildValue(""); } // determine the timeout int timeout = effect->_timeout; if (timeout > 0) { timeout = effect->_endTime - QDateTime::currentMSecsSinceEpoch(); // we are done if the time has passed if (timeout <= 0) { return Py_BuildValue(""); } } // check the number of arguments int argCount = PyTuple_Size(args); if (argCount == 3) { // three seperate arguments for red, green, and blue ColorRgb color; if (PyArg_ParseTuple(args, "bbb", &color.red, &color.green, &color.blue)) { std::fill(effect->_colors.begin(), effect->_colors.end(), color); effect->setColors(effect->_priority, effect->_colors, timeout, false); return Py_BuildValue(""); } else { return nullptr; } } else if (argCount == 1) { // bytearray of values PyObject * bytearray = nullptr; if (PyArg_ParseTuple(args, "O", &bytearray)) { if (PyByteArray_Check(bytearray)) { size_t length = PyByteArray_Size(bytearray); if (length == 3 * effect->_imageProcessor->getLedCount()) { char * data = PyByteArray_AS_STRING(bytearray); memcpy(effect->_colors.data(), data, length); effect->setColors(effect->_priority, effect->_colors, timeout, false); return Py_BuildValue(""); } else { PyErr_SetString(PyExc_RuntimeError, "Length of bytearray argument should be 3*ledCount"); return nullptr; } } else { PyErr_SetString(PyExc_RuntimeError, "Argument is not a bytearray"); return nullptr; } } else { return nullptr; } } else { PyErr_SetString(PyExc_RuntimeError, "Function expect 1 or 3 arguments"); return nullptr; } // error PyErr_SetString(PyExc_RuntimeError, "Unknown error"); return nullptr; }