diff -r c51045920410 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Tue Nov 22 19:43:11 2016 +0900 +++ b/Objects/unicodeobject.c Tue Nov 22 22:20:22 2016 +0800 @@ -6798,7 +6798,7 @@ goto onError; /* subtract preallocated bytes */ - writer.min_size -= 1; + writer.min_size -= newpos - collstart; if (PyBytes_Check(rep)) { /* Directly copy bytes result to output. */ @@ -6835,7 +6835,7 @@ ch = PyUnicode_READ_CHAR(rep, i); if (ch >= limit) { raise_encode_exception(&exc, encoding, unicode, - pos, pos+1, reason); + collstart, collend, reason); goto onError; } *str = (char)ch;