static void * _read_value_from_handle(void *handle) { Py_ssize_t length; char* KEY; PyObject* VALUE; if(!_read_from_handle(&length, sizeof(length), handle)) return NULL; if(length < 0) { return NULL; } KEY = malloc(length); if(length < 0) { PyErr_SetString(PyExc_MemoryError, "insufficient memory to read value"); return NULL; } VALUE = NULL; if(_read_from_handle(KEY, length, handle)) VALUE = PyMarshal_ReadObjectFromString(KEY, length); free(KEY); return VALUE; }
static int _read_from_handle(void *wasread, const int length, void *handle) { PyObject *py_handle = (PyObject *)handle; PyObject *py_retval = NULL; int success = 0; char* buffer; if(!length) { PyErr_SetString(PyExc_RuntimeError, "data length is zero"); return 0; } py_retval = PyObject_CallMethod(py_handle, "read", "i", length); #ifdef IS_PY3K if(!PyBytes_Check(py_retval)) { #else if(!PyString_Check(py_retval)) { #endif PyErr_SetString(PyExc_TypeError, "expected a bytes string"); goto error; } #ifdef IS_PY3K buffer = PyBytes_AS_STRING(py_retval); #else buffer = PyString_AS_STRING(py_retval); #endif memcpy(wasread, buffer, length); success = 1; error: Py_XDECREF(py_retval); return success; } static void * _read_value_from_handle(void *handle) { Py_ssize_t length; char* KEY; PyObject* VALUE; if(!_read_from_handle(&length, sizeof(length), handle)) return NULL; if(length < 0) { return NULL; } KEY = malloc(length); if(length < 0) { PyErr_SetString(PyExc_MemoryError, "insufficient memory to read value"); return NULL; } VALUE = NULL; if(_read_from_handle(KEY, length, handle)) VALUE = PyMarshal_ReadObjectFromString(KEY, length); free(KEY); return VALUE; }
static void * _read_value_from_handle(void *handle) { Py_ssize_t length; char KEY[MAX_KEY_LENGTH]; if(!_read_from_handle((void *)&length, sizeof(length), (void *)handle)) return NULL; if(length < 0 || length >= MAX_KEY_LENGTH) return NULL; if(!_read_from_handle((void *)KEY, length, (void *)handle)) return NULL; return PyMarshal_ReadObjectFromString(KEY, length); }