int _write_value_to_handle(const void *value, void *handle) { PyObject *py_value = (PyObject *)value, *py_marshalled = NULL; char *marshalled; Py_ssize_t length; int success = 0; #ifdef Py_MARSHAL_VERSION if(!(py_marshalled = PyMarshal_WriteObjectToString(py_value, Py_MARSHAL_VERSION))) goto _write_value_to_handle_cleanup; #else if(!(py_marshalled = PyMarshal_WriteObjectToString(py_value))) goto _write_value_to_handle_cleanup; #endif if(PyString_AsStringAndSize(py_marshalled, &marshalled, &length) == -1) goto _write_value_to_handle_cleanup; if(!_write_to_handle(&length, sizeof(length), handle)) goto _write_value_to_handle_cleanup; if (length != (int)length) goto _write_value_to_handle_cleanup; if(!_write_to_handle(marshalled, (int)length, handle)) goto _write_value_to_handle_cleanup; success = 1; _write_value_to_handle_cleanup: if(py_marshalled) { Py_DECREF(py_marshalled); } return success; }
static int _write_value_to_handle(const void *value, void *handle) { PyObject *py_value = (PyObject *)value, *py_marshalled = NULL, *bytes = NULL; char *marshalled; Py_ssize_t length; int success = 0; #ifdef Py_MARSHAL_VERSION if(!(py_marshalled = PyMarshal_WriteObjectToString(py_value, Py_MARSHAL_VERSION))) goto _write_value_to_handle_cleanup; #else if(!(py_marshalled = PyMarshal_WriteObjectToString(py_value))) goto _write_value_to_handle_cleanup; #endif #ifdef IS_PY3K if (!PyBytes_Check(py_marshalled)) { PyErr_SetString(PyExc_TypeError, "marshalled data expected to be bytes"); goto _write_value_to_handle_cleanup; } if(PyBytes_AsStringAndSize(py_marshalled, &marshalled, &length) == -1) #else if(PyString_AsStringAndSize(py_marshalled, &marshalled, &length) == -1) #endif goto _write_value_to_handle_cleanup; if(!_write_to_handle(&length, sizeof(length), handle)) goto _write_value_to_handle_cleanup; if (length != (int)length) goto _write_value_to_handle_cleanup; if(!_write_to_handle(marshalled, (int)length, handle)) goto _write_value_to_handle_cleanup; success = 1; _write_value_to_handle_cleanup: Py_XDECREF(py_marshalled); Py_XDECREF(bytes); return success; }