Index: Objects/unicodeobject.c =================================================================== --- Objects/unicodeobject.c (revision 77001) +++ Objects/unicodeobject.c (working copy) @@ -1638,7 +1638,7 @@ size = PyByteArray_GET_SIZE(output); data = PyByteArray_AS_STRING(output); } - if (size != strlen(data)) { + if (size && (size != strlen(data))) { PyErr_SetString(PyExc_TypeError, "embedded NUL character"); Py_DECREF(output); return 0; Index: Modules/posixmodule.c =================================================================== --- Modules/posixmodule.c (revision 77001) +++ Modules/posixmodule.c (working copy) @@ -555,17 +555,20 @@ /* Convert a bytes object to a char*. Optionally lock the buffer if it is a bytes array. */ +static char _null_char = 0; static char* bytes2str(PyObject* o, int lock) { + char *rv; if(PyBytes_Check(o)) return PyBytes_AsString(o); else if(PyByteArray_Check(o)) { if (lock && PyObject_GetBuffer(o, NULL, 0) < 0) /* On a bytearray, this should not fail. */ PyErr_BadInternalCall(); - return PyByteArray_AsString(o); + rv = PyByteArray_AsString(o); + return rv ? rv : &_null_char; } else { /* The FS converter should have verified that this is either bytes or bytearray. */ @@ -580,7 +583,7 @@ release_bytes(PyObject* o) { if (PyByteArray_Check(o)) - o->ob_type->tp_as_buffer->bf_releasebuffer(NULL, 0); + o->ob_type->tp_as_buffer->bf_releasebuffer(o, 0); Py_DECREF(o); }