Implement PEP 393.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index a85bac8..2ae1947 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -90,6 +90,19 @@
 extern "C" {
 #endif
 
+#define _PyUnicode_WSTR(op) (((PyASCIIObject*)(op))->wstr)
+#define _PyUnicode_WSTR_LENGTH(op) (((PyCompactUnicodeObject*)(op))->wstr_length)
+#define _PyUnicode_LENGTH(op) (((PyASCIIObject *)(op))->length)
+#define _PyUnicode_STATE(op) (((PyASCIIObject *)(op))->state)
+#define _PyUnicode_HASH(op) (((PyASCIIObject *)(op))->hash)
+#define _PyUnicode_KIND(op) \
+    (assert(PyUnicode_Check(op)), \
+     ((PyASCIIObject *)(op))->state.kind)
+#define _PyUnicode_GET_LENGTH(op)                \
+    (assert(PyUnicode_Check(op)),               \
+     ((PyASCIIObject *)(op))->length)
+
+
 /* This dictionary holds all interned unicode strings.  Note that references
    to strings in this dictionary are *not* counted in the string's ob_refcnt.
    When the interned string reaches a refcnt of 0 the string deallocation
@@ -100,10 +113,6 @@
 */
 static PyObject *interned;
 
-/* Free list for Unicode objects */
-static PyUnicodeObject *free_list;
-static int numfree;
-
 /* The empty Unicode object is shared to improve performance. */
 static PyUnicodeObject *unicode_empty;
 
@@ -226,7 +235,7 @@
      (BLOOM(bloom_linebreak, (ch)) && Py_UNICODE_ISLINEBREAK(ch)))
 
 Py_LOCAL_INLINE(BLOOM_MASK)
-make_bloom_mask(Py_UNICODE* ptr, Py_ssize_t len)
+make_bloom_mask(int kind, void* ptr, Py_ssize_t len)
 {
     /* calculate simple bloom-style bitmask for a given unicode string */
 
@@ -235,36 +244,58 @@
 
     mask = 0;
     for (i = 0; i < len; i++)
-        BLOOM_ADD(mask, ptr[i]);
+        BLOOM_ADD(mask, PyUnicode_READ(kind, ptr, i));
 
     return mask;
 }
 
-Py_LOCAL_INLINE(int)
-unicode_member(Py_UNICODE chr, Py_UNICODE* set, Py_ssize_t setlen)
-{
-    Py_ssize_t i;
-
-    for (i = 0; i < setlen; i++)
-        if (set[i] == chr)
-            return 1;
-
-    return 0;
-}
-
-#define BLOOM_MEMBER(mask, chr, set, setlen)                    \
-    BLOOM(mask, chr) && unicode_member(chr, set, setlen)
+#define BLOOM_MEMBER(mask, chr, str) \
+    (BLOOM(mask, chr) \
+     && (PyUnicode_FindChar(str, chr, 0, PyUnicode_GET_LENGTH(str), 1) >= 0))
 
 /* --- Unicode Object ----------------------------------------------------- */
 
+static PyObject *
+substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len);
+
+static PyObject *
+fixup(PyUnicodeObject *self, Py_UCS4 (*fixfct)(PyUnicodeObject *s));
+
+Py_LOCAL_INLINE(char *) findchar(void *s, int kind,
+                                 Py_ssize_t size, Py_UCS4 ch,
+                                 int direction)
+{
+    /* like wcschr, but doesn't stop at NULL characters */
+    Py_ssize_t i;
+    if (direction == 1) {
+        for(i = 0; i < size; i++)
+            if (PyUnicode_READ(kind, s, i) == ch)
+                return (char*)s + PyUnicode_KIND_SIZE(kind, i);
+    }
+    else {
+        for(i = size-1; i >= 0; i--)
+            if (PyUnicode_READ(kind, s, i) == ch)
+                return (char*)s + PyUnicode_KIND_SIZE(kind, i);
+    }
+    return NULL;
+}
+
 static int
 unicode_resize(register PyUnicodeObject *unicode,
-	       Py_ssize_t length)
+               Py_ssize_t length)
 {
     void *oldstr;
 
+    /* Resizing is only supported for old unicode objects. */
+    assert(!PyUnicode_IS_COMPACT(unicode));
+    assert(_PyUnicode_WSTR(unicode) != NULL);
+
+    /* ... and only if they have not been readied yet, because
+       callees usually rely on the wstr representation when resizing. */
+    assert(unicode->data.any == NULL);
+
     /* Shortcut if there's nothing much to do. */
-    if (unicode->length == length)
+    if (_PyUnicode_WSTR_LENGTH(unicode) == length)
         goto reset;
 
     /* Resizing shared object (unicode_empty or single character
@@ -272,9 +303,9 @@
        instead ! */
 
     if (unicode == unicode_empty ||
-        (unicode->length == 1 &&
-         unicode->str[0] < 256U &&
-         unicode_latin1[unicode->str[0]] == unicode)) {
+        (_PyUnicode_WSTR_LENGTH(unicode) == 1 &&
+         _PyUnicode_WSTR(unicode)[0] < 256U &&
+         unicode_latin1[_PyUnicode_WSTR(unicode)[0]] == unicode)) {
         PyErr_SetString(PyExc_SystemError,
                         "can't resize shared str objects");
         return -1;
@@ -285,23 +316,31 @@
        safe to look at str[length] (without making any assumptions about what
        it contains). */
 
-    oldstr = unicode->str;
-    unicode->str = PyObject_REALLOC(unicode->str,
-                                    sizeof(Py_UNICODE) * (length + 1));
-    if (!unicode->str) {
-        unicode->str = (Py_UNICODE *)oldstr;
+    oldstr = _PyUnicode_WSTR(unicode);
+    _PyUnicode_WSTR(unicode) = PyObject_REALLOC(_PyUnicode_WSTR(unicode),
+                                     sizeof(Py_UNICODE) * (length + 1));
+    if (!_PyUnicode_WSTR(unicode)) {
+        _PyUnicode_WSTR(unicode) = (Py_UNICODE *)oldstr;
         PyErr_NoMemory();
         return -1;
     }
-    unicode->str[length] = 0;
-    unicode->length = length;
+    _PyUnicode_WSTR(unicode)[length] = 0;
+    _PyUnicode_WSTR_LENGTH(unicode) = length;
 
   reset:
-    /* Reset the object caches */
-    if (unicode->defenc) {
-        Py_CLEAR(unicode->defenc);
+    if (unicode->data.any != NULL) {
+        PyObject_FREE(unicode->data.any);
+        if (unicode->_base.utf8 && unicode->_base.utf8 != unicode->data.any) {
+            PyObject_FREE(unicode->_base.utf8);
+        }
+        unicode->_base.utf8 = NULL;
+        unicode->_base.utf8_length = 0;
+        unicode->data.any = NULL;
+        _PyUnicode_LENGTH(unicode) = 0;
+        _PyUnicode_STATE(unicode).interned = _PyUnicode_STATE(unicode).interned;
+        _PyUnicode_STATE(unicode).kind = PyUnicode_WCHAR_KIND;
     }
-    unicode->hash = -1;
+    _PyUnicode_HASH(unicode) = -1;
 
     return 0;
 }
@@ -315,10 +354,15 @@
 
 */
 
+#ifdef Py_DEBUG
+int unicode_old_new_calls = 0;
+#endif
+
 static PyUnicodeObject *
 _PyUnicode_New(Py_ssize_t length)
 {
     register PyUnicodeObject *unicode;
+    size_t new_size;
 
     /* Optimization for empty strings */
     if (length == 0 && unicode_empty != NULL) {
@@ -330,40 +374,26 @@
     if (length > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
         return (PyUnicodeObject *)PyErr_NoMemory();
     }
-
-    /* Unicode freelist & memory allocation */
-    if (free_list) {
-        unicode = free_list;
-        free_list = *(PyUnicodeObject **)unicode;
-        numfree--;
-        if (unicode->str) {
-            /* Keep-Alive optimization: we only upsize the buffer,
-               never downsize it. */
-            if ((unicode->length < length) &&
-                unicode_resize(unicode, length) < 0) {
-                PyObject_DEL(unicode->str);
-                unicode->str = NULL;
-            }
-        }
-        else {
-            size_t new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
-            unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);
-        }
-        PyObject_INIT(unicode, &PyUnicode_Type);
-    }
-    else {
-        size_t new_size;
-        unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
-        if (unicode == NULL)
-            return NULL;
-        new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
-        unicode->str = (Py_UNICODE*) PyObject_MALLOC(new_size);
+    if (length < 0) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Negative size passed to _PyUnicode_New");
+        return NULL;
     }
 
-    if (!unicode->str) {
+#ifdef Py_DEBUG
+    ++unicode_old_new_calls;
+#endif
+
+    unicode = PyObject_New(PyUnicodeObject, &PyUnicode_Type);
+    if (unicode == NULL)
+        return NULL;
+    new_size = sizeof(Py_UNICODE) * ((size_t)length + 1);
+    _PyUnicode_WSTR(unicode) = (Py_UNICODE*) PyObject_MALLOC(new_size);
+    if (!_PyUnicode_WSTR(unicode)) {
         PyErr_NoMemory();
         goto onError;
     }
+
     /* Initialize the first element to guard against cases where
      * the caller fails before initializing str -- unicode_resize()
      * reads str[0], and the Keep-Alive optimization can keep memory
@@ -371,12 +401,19 @@
      * We don't want unicode_resize to read uninitialized memory in
      * that case.
      */
-    unicode->str[0] = 0;
-    unicode->str[length] = 0;
-    unicode->length = length;
-    unicode->hash = -1;
-    unicode->state = 0;
-    unicode->defenc = NULL;
+    _PyUnicode_WSTR(unicode)[0] = 0;
+    _PyUnicode_WSTR(unicode)[length] = 0;
+    _PyUnicode_WSTR_LENGTH(unicode) = length;
+    _PyUnicode_HASH(unicode) = -1;
+    _PyUnicode_STATE(unicode).interned = 0;
+    _PyUnicode_STATE(unicode).kind = 0;
+    _PyUnicode_STATE(unicode).compact = 0;
+    _PyUnicode_STATE(unicode).ready = 0;
+    _PyUnicode_STATE(unicode).ascii = 0;
+    unicode->data.any = NULL;
+    _PyUnicode_LENGTH(unicode) = 0;
+    unicode->_base.utf8 = NULL;
+    unicode->_base.utf8_length = 0;
     return unicode;
 
   onError:
@@ -387,6 +424,467 @@
     return NULL;
 }
 
+#ifdef Py_DEBUG
+int unicode_new_new_calls = 0;
+
+/* Functions wrapping macros for use in debugger */
+char *_PyUnicode_utf8(void *unicode){
+    return _PyUnicode_UTF8(unicode);
+}
+
+void *_PyUnicode_compact_data(void *unicode) {
+    return _PyUnicode_COMPACT_DATA(unicode);
+}
+void *_PyUnicode_data(void *unicode){
+    printf("obj %p\n", unicode);
+    printf("compact %d\n", PyUnicode_IS_COMPACT(unicode));
+    printf("compact ascii %d\n", PyUnicode_IS_COMPACT_ASCII(unicode));
+    printf("ascii op %p\n", ((void*)((PyASCIIObject*)(unicode) + 1)));
+    printf("compact op %p\n", ((void*)((PyCompactUnicodeObject*)(unicode) + 1)));
+    printf("compact data %p\n", _PyUnicode_COMPACT_DATA(unicode));
+    return PyUnicode_DATA(unicode);
+}
+#endif
+
+PyObject *
+PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
+{
+    PyObject *obj;
+    PyCompactUnicodeObject *unicode;
+    void *data;
+    int kind_state;
+    int is_sharing = 0, is_ascii = 0;
+    Py_ssize_t char_size;
+    Py_ssize_t struct_size;
+
+    /* Optimization for empty strings */
+    if (size == 0 && unicode_empty != NULL) {
+        Py_INCREF(unicode_empty);
+        return (PyObject *)unicode_empty;
+    }
+
+#ifdef Py_DEBUG
+    ++unicode_new_new_calls;
+#endif
+
+    struct_size = sizeof(PyCompactUnicodeObject);
+    if (maxchar < 128) {
+        kind_state = PyUnicode_1BYTE_KIND;
+        char_size = 1;
+        is_ascii = 1;
+        struct_size = sizeof(PyASCIIObject);
+    }
+    else if (maxchar < 256) {
+        kind_state = PyUnicode_1BYTE_KIND;
+        char_size = 1;
+    }
+    else if (maxchar < 65536) {
+        kind_state = PyUnicode_2BYTE_KIND;
+        char_size = 2;
+        if (sizeof(wchar_t) == 2)
+            is_sharing = 1;
+    }
+    else {
+        kind_state = PyUnicode_4BYTE_KIND;
+        char_size = 4;
+        if (sizeof(wchar_t) == 4)
+            is_sharing = 1;
+    }
+
+    /* Ensure we won't overflow the size. */
+    if (size < 0) {
+        PyErr_SetString(PyExc_SystemError,
+                        "Negative size passed to PyUnicode_New");
+        return NULL;
+    }
+    if (size > ((PY_SSIZE_T_MAX - struct_size) / char_size - 1))
+        return PyErr_NoMemory();
+
+    /* Duplicated allocation code from _PyObject_New() instead of a call to
+     * PyObject_New() so we are able to allocate space for the object and
+     * it's data buffer.
+     */
+    obj = (PyObject *) PyObject_MALLOC(struct_size + (size + 1) * char_size);
+    if (obj == NULL)
+        return PyErr_NoMemory();
+    obj = PyObject_INIT(obj, &PyUnicode_Type);
+    if (obj == NULL)
+        return NULL;
+
+    unicode = (PyCompactUnicodeObject *)obj;
+    if (is_ascii)
+        data = ((PyASCIIObject*)obj) + 1;
+    else
+        data = unicode + 1;
+    _PyUnicode_LENGTH(unicode) = size;
+    _PyUnicode_HASH(unicode) = -1;
+    _PyUnicode_STATE(unicode).interned = 0;
+    _PyUnicode_STATE(unicode).kind = kind_state;
+    _PyUnicode_STATE(unicode).compact = 1;
+    _PyUnicode_STATE(unicode).ready = 1;
+    _PyUnicode_STATE(unicode).ascii = is_ascii;
+    if (is_ascii) {
+        ((char*)data)[size] = 0;
+        _PyUnicode_WSTR(unicode) = NULL;
+    }
+    else if (kind_state == PyUnicode_1BYTE_KIND) {
+        ((char*)data)[size] = 0;
+        _PyUnicode_WSTR(unicode) = NULL;
+        _PyUnicode_WSTR_LENGTH(unicode) = 0;
+        unicode->utf8_length = 0;
+        unicode->utf8 = NULL;
+        }
+    else {
+        unicode->utf8 = NULL;
+        if (kind_state == PyUnicode_2BYTE_KIND)
+            ((Py_UCS2*)data)[size] = 0;
+        else /* kind_state == PyUnicode_4BYTE_KIND */
+            ((Py_UCS4*)data)[size] = 0;
+        if (is_sharing) {
+            _PyUnicode_WSTR_LENGTH(unicode) = size;
+            _PyUnicode_WSTR(unicode) = (wchar_t *)data;
+        }
+        else {
+            _PyUnicode_WSTR_LENGTH(unicode) = 0;
+            _PyUnicode_WSTR(unicode) = NULL;
+        }
+    }
+    return obj;
+}
+
+#if SIZEOF_WCHAR_T == 2
+/* Helper function to convert a 16-bits wchar_t representation to UCS4, this
+   will decode surrogate pairs, the other conversions are implemented as macros
+   for efficency.
+
+   This function assumes that unicode can hold one more code point than wstr
+   characters for a terminating null character. */
+static int
+unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end,
+                              PyUnicodeObject *unicode)
+{
+    const wchar_t *iter;
+    Py_UCS4 *ucs4_out;
+
+    assert(unicode && PyUnicode_Check(unicode));
+    assert(_PyUnicode_KIND(unicode) == PyUnicode_4BYTE_KIND);
+    ucs4_out = PyUnicode_4BYTE_DATA(unicode);
+
+    for (iter = begin; iter < end; ) {
+        assert(ucs4_out < (PyUnicode_4BYTE_DATA(unicode) +
+                           _PyUnicode_GET_LENGTH(unicode)));
+        if (*iter >= 0xD800 && *iter <= 0xDBFF
+            && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF)
+        {
+            *ucs4_out++ = (((iter[0] & 0x3FF)<<10) | (iter[1] & 0x3FF)) + 0x10000;
+            iter += 2;
+        }
+        else {
+            *ucs4_out++ = *iter;
+            iter++;
+        }
+    }
+    assert(ucs4_out == (PyUnicode_4BYTE_DATA(unicode) +
+                        _PyUnicode_GET_LENGTH(unicode)));
+
+    return 0;
+}
+#endif
+
+int
+PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start,
+                         PyObject *from, Py_ssize_t from_start,
+                         Py_ssize_t how_many)
+{
+    int from_kind;
+    int to_kind;
+
+    assert(PyUnicode_Check(from));
+    assert(PyUnicode_Check(to));
+
+    if (PyUnicode_READY(from))
+        return -1;
+    if (PyUnicode_READY(to))
+        return -1;
+
+    from_kind = PyUnicode_KIND(from);
+    to_kind = PyUnicode_KIND(to);
+
+    if (from_kind == to_kind) {
+        const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(to);
+        Py_MEMCPY(PyUnicode_1BYTE_DATA(to) + (to_start * char_size),
+                  PyUnicode_1BYTE_DATA(from) + (from_start * char_size),
+                  how_many * char_size);
+        return 0;
+    }
+
+    switch (from_kind) {
+        case PyUnicode_1BYTE_KIND:
+            switch (to_kind) {
+                case PyUnicode_2BYTE_KIND:
+                    PyUnicode_CONVERT_BYTES(
+                        unsigned char, Py_UCS2,
+                        PyUnicode_1BYTE_DATA(from) + from_start,
+                        PyUnicode_1BYTE_DATA(from) + from_start + how_many,
+                        PyUnicode_2BYTE_DATA(to) + to_start
+                        );
+                    break;
+                case PyUnicode_4BYTE_KIND:
+                    PyUnicode_CONVERT_BYTES(
+                        unsigned char, Py_UCS4,
+                        PyUnicode_1BYTE_DATA(from) + from_start,
+                        PyUnicode_1BYTE_DATA(from) + from_start + how_many,
+                        PyUnicode_4BYTE_DATA(to) + to_start
+                        );
+                    break;
+                default:
+                    goto invalid_state;
+            }
+            break;
+        case PyUnicode_2BYTE_KIND:
+            switch (to_kind) {
+                case PyUnicode_1BYTE_KIND:
+                    PyUnicode_CONVERT_BYTES(
+                        Py_UCS2, unsigned char,
+                        PyUnicode_2BYTE_DATA(from) + from_start,
+                        PyUnicode_2BYTE_DATA(from) + from_start + how_many,
+                        PyUnicode_1BYTE_DATA(to) + to_start
+                        );
+                    break;
+                case PyUnicode_4BYTE_KIND:
+                    PyUnicode_CONVERT_BYTES(
+                        Py_UCS2, Py_UCS4,
+                        PyUnicode_2BYTE_DATA(from) + from_start,
+                        PyUnicode_2BYTE_DATA(from) + from_start + how_many,
+                        PyUnicode_4BYTE_DATA(to) + to_start
+                        );
+                    break;
+                default:
+                    goto invalid_state;
+            }
+            break;
+        case PyUnicode_4BYTE_KIND:
+            switch (to_kind) {
+                case PyUnicode_1BYTE_KIND:
+                    PyUnicode_CONVERT_BYTES(
+                        Py_UCS4, unsigned char,
+                        PyUnicode_4BYTE_DATA(from) + from_start,
+                        PyUnicode_4BYTE_DATA(from) + from_start + how_many,
+                        PyUnicode_1BYTE_DATA(to) + to_start
+                        );
+                    break;
+                case PyUnicode_2BYTE_KIND:
+                    PyUnicode_CONVERT_BYTES(
+                        Py_UCS4, Py_UCS2,
+                        PyUnicode_4BYTE_DATA(from) + from_start,
+                        PyUnicode_4BYTE_DATA(from) + from_start + how_many,
+                        PyUnicode_2BYTE_DATA(to) + to_start
+                        );
+                    break;
+                default:
+                    goto invalid_state;
+            }
+            break;
+        default:
+            goto invalid_state;
+    }
+    return 0;
+
+invalid_state:
+    PyErr_Format(PyExc_ValueError,
+                 "Impossible kind state (from=%i, to=%i) "
+                 "in PyUnicode_CopyCharacters",
+                 from_kind, to_kind);
+    return -1;
+}
+
+int
+_PyUnicode_FindMaxCharAndNumSurrogatePairs(const wchar_t *begin,
+                                           const wchar_t *end,
+                                           Py_UCS4 *maxchar,
+                                           Py_ssize_t *num_surrogates)
+{
+    const wchar_t *iter;
+
+    if (num_surrogates == NULL || maxchar == NULL) {
+        PyErr_SetString(PyExc_SystemError,
+                        "unexpected NULL arguments to "
+                        "PyUnicode_FindMaxCharAndNumSurrogatePairs");
+        return -1;
+    }
+
+    *num_surrogates = 0;
+    *maxchar = 0;
+
+    for (iter = begin; iter < end; ) {
+        if (*iter > *maxchar)
+            *maxchar = *iter;
+#if SIZEOF_WCHAR_T == 2
+        if (*iter >= 0xD800 && *iter <= 0xDBFF
+            && (iter+1) < end && iter[1] >= 0xDC00 && iter[1] <= 0xDFFF)
+        {
+            Py_UCS4 surrogate_val;
+            surrogate_val = (((iter[0] & 0x3FF)<<10)
+                             | (iter[1] & 0x3FF)) + 0x10000;
+            ++(*num_surrogates);
+            if (surrogate_val > *maxchar)
+                *maxchar = surrogate_val;
+            iter += 2;
+        }
+        else
+            iter++;
+#else
+        iter++;
+#endif
+    }
+    return 0;
+}
+
+#ifdef Py_DEBUG
+int unicode_ready_calls = 0;
+#endif
+
+int
+_PyUnicode_Ready(PyUnicodeObject *unicode)
+{
+    wchar_t *end;
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t num_surrogates;
+#if SIZEOF_WCHAR_T == 2
+    Py_ssize_t length_wo_surrogates;
+#endif
+
+    assert(PyUnicode_Check(unicode));
+
+    if (unicode->data.any != NULL) {
+        assert(PyUnicode_KIND(unicode) != PyUnicode_WCHAR_KIND);
+        return 0;
+    }
+
+    /* _PyUnicode_Ready() is only intented for old-style API usage where
+     * strings were created using _PyObject_New() and where no canonical
+     * representation (the str field) has been set yet aka strings
+     * which are not yet ready.
+     */
+    assert(_PyUnicode_WSTR(unicode) != NULL);
+    assert(_PyUnicode_KIND(unicode) == PyUnicode_WCHAR_KIND);
+    assert(!PyUnicode_IS_COMPACT(unicode));
+    assert(!PyUnicode_IS_READY(unicode));
+    /* Actually, it should neither be interned nor be anything else: */
+    assert(_PyUnicode_STATE(unicode).interned == 0);
+    assert(unicode->_base.utf8 == NULL);
+
+#ifdef Py_DEBUG
+    ++unicode_ready_calls;
+#endif
+
+    end = _PyUnicode_WSTR(unicode) + _PyUnicode_WSTR_LENGTH(unicode);
+    if (_PyUnicode_FindMaxCharAndNumSurrogatePairs(_PyUnicode_WSTR(unicode), end,
+                                                  &maxchar,
+                                                  &num_surrogates) == -1) {
+        assert(0 && "PyUnicode_FindMaxCharAndNumSurrogatePairs failed");
+        return -1;
+    }
+
+    if (maxchar < 256) {
+        unicode->data.any = PyObject_MALLOC(_PyUnicode_WSTR_LENGTH(unicode) + 1);
+        if (!unicode->data.any) {
+            PyErr_NoMemory();
+            return -1;
+        }
+        PyUnicode_CONVERT_BYTES(wchar_t, unsigned char,
+                                _PyUnicode_WSTR(unicode), end,
+                                PyUnicode_1BYTE_DATA(unicode));
+        PyUnicode_1BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
+        _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
+        _PyUnicode_STATE(unicode).kind = PyUnicode_1BYTE_KIND;
+        if (maxchar < 128) {
+            unicode->_base.utf8 = unicode->data.any;
+            unicode->_base.utf8_length = _PyUnicode_WSTR_LENGTH(unicode);
+        }
+        else {
+            unicode->_base.utf8 = NULL;
+            unicode->_base.utf8_length = 0;
+        }
+        PyObject_FREE(_PyUnicode_WSTR(unicode));
+        _PyUnicode_WSTR(unicode) = NULL;
+        _PyUnicode_WSTR_LENGTH(unicode) = 0;
+    }
+    /* In this case we might have to convert down from 4-byte native
+       wchar_t to 2-byte unicode. */
+    else if (maxchar < 65536) {
+        assert(num_surrogates == 0 &&
+               "FindMaxCharAndNumSurrogatePairs() messed up");
+
+        if (sizeof(wchar_t) == 2) {
+            /* We can share representations and are done. */
+            unicode->data.any = _PyUnicode_WSTR(unicode);
+            PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
+            _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
+            _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND;
+            unicode->_base.utf8 = NULL;
+            unicode->_base.utf8_length = 0;
+        }
+        else {
+            assert(sizeof(wchar_t) == 4);
+
+            unicode->data.any = PyObject_MALLOC(
+                2 * (_PyUnicode_WSTR_LENGTH(unicode) + 1));
+            if (!unicode->data.any) {
+                PyErr_NoMemory();
+                return -1;
+            }
+            PyUnicode_CONVERT_BYTES(wchar_t, Py_UCS2,
+                                    _PyUnicode_WSTR(unicode), end,
+                                    PyUnicode_2BYTE_DATA(unicode));
+            PyUnicode_2BYTE_DATA(unicode)[_PyUnicode_WSTR_LENGTH(unicode)] = '\0';
+            _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
+            _PyUnicode_STATE(unicode).kind = PyUnicode_2BYTE_KIND;
+            unicode->_base.utf8 = NULL;
+            unicode->_base.utf8_length = 0;
+            PyObject_FREE(_PyUnicode_WSTR(unicode));
+            _PyUnicode_WSTR(unicode) = NULL;
+            _PyUnicode_WSTR_LENGTH(unicode) = 0;
+        }
+    }
+    /* maxchar exeeds 16 bit, wee need 4 bytes for unicode characters */
+    else {
+#if SIZEOF_WCHAR_T == 2
+        /* in case the native representation is 2-bytes, we need to allocate a
+           new normalized 4-byte version. */
+        length_wo_surrogates = _PyUnicode_WSTR_LENGTH(unicode) - num_surrogates;
+        unicode->data.any = PyObject_MALLOC(4 * (length_wo_surrogates + 1));
+        if (!unicode->data.any) {
+            PyErr_NoMemory();
+            return -1;
+        }
+        _PyUnicode_LENGTH(unicode) = length_wo_surrogates;
+        _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND;
+        unicode->_base.utf8 = NULL;
+        unicode->_base.utf8_length = 0;
+        if (unicode_convert_wchar_to_ucs4(_PyUnicode_WSTR(unicode), end,
+                                          unicode) < 0) {
+            assert(0 && "ConvertWideCharToUCS4 failed");
+            return -1;
+        }
+        PyObject_FREE(_PyUnicode_WSTR(unicode));
+        _PyUnicode_WSTR(unicode) = NULL;
+        _PyUnicode_WSTR_LENGTH(unicode) = 0;
+#else
+        assert(num_surrogates == 0);
+
+        unicode->data.any = _PyUnicode_WSTR(unicode);
+        _PyUnicode_LENGTH(unicode) = _PyUnicode_WSTR_LENGTH(unicode);
+        unicode->_base.utf8 = NULL;
+        unicode->_base.utf8_length = 0;
+        _PyUnicode_STATE(unicode).kind = PyUnicode_4BYTE_KIND;
+#endif
+        PyUnicode_4BYTE_DATA(unicode)[_PyUnicode_LENGTH(unicode)] = '\0';
+    }
+    _PyUnicode_STATE(unicode).ready = 1;
+    return 0;
+}
+
 static void
 unicode_dealloc(register PyUnicodeObject *unicode)
 {
@@ -409,25 +907,19 @@
         Py_FatalError("Inconsistent interned string state.");
     }
 
-    if (PyUnicode_CheckExact(unicode) &&
-        numfree < PyUnicode_MAXFREELIST) {
-        /* Keep-Alive optimization */
-        if (unicode->length >= KEEPALIVE_SIZE_LIMIT) {
-            PyObject_DEL(unicode->str);
-            unicode->str = NULL;
-            unicode->length = 0;
-        }
-        if (unicode->defenc) {
-            Py_CLEAR(unicode->defenc);
-        }
-        /* Add to free list */
-        *(PyUnicodeObject **)unicode = free_list;
-        free_list = unicode;
-        numfree++;
+    if (_PyUnicode_WSTR(unicode) &&
+        (!PyUnicode_IS_READY(unicode) ||
+         _PyUnicode_WSTR(unicode) != PyUnicode_DATA(unicode)))
+        PyObject_DEL(_PyUnicode_WSTR(unicode));
+    if (_PyUnicode_UTF8(unicode) && _PyUnicode_UTF8(unicode) != PyUnicode_DATA(unicode))
+        PyObject_DEL(unicode->_base.utf8);
+
+    if (PyUnicode_IS_COMPACT(unicode)) {
+        Py_TYPE(unicode)->tp_free((PyObject *)unicode);
     }
     else {
-        PyObject_DEL(unicode->str);
-        Py_XDECREF(unicode->defenc);
+        if (unicode->data.any)
+            PyObject_DEL(unicode->data.any);
         Py_TYPE(unicode)->tp_free((PyObject *)unicode);
     }
 }
@@ -443,21 +935,26 @@
         return -1;
     }
     v = *unicode;
-    if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0) {
+    if (v == NULL || !PyUnicode_Check(v) || Py_REFCNT(v) != 1 || length < 0 ||
+        PyUnicode_IS_COMPACT(v) || _PyUnicode_WSTR(v) == NULL) {
         PyErr_BadInternalCall();
         return -1;
     }
 
     /* Resizing unicode_empty and single character objects is not
-       possible since these are being shared. We simply return a fresh
-       copy with the same Unicode content. */
-    if (v->length != length &&
-        (v == unicode_empty || v->length == 1)) {
+       possible since these are being shared.
+       The same goes for new-representation unicode objects or objects which
+       have already been readied.
+       For these, we simply return a fresh copy with the same Unicode content.
+       */
+    if ((_PyUnicode_WSTR_LENGTH(v) != length &&
+         (v == unicode_empty || _PyUnicode_WSTR_LENGTH(v) == 1)) ||
+        PyUnicode_IS_COMPACT(v) || v->data.any) {
         PyUnicodeObject *w = _PyUnicode_New(length);
         if (w == NULL)
             return -1;
-        Py_UNICODE_COPY(w->str, v->str,
-                        length < v->length ? length : v->length);
+        Py_UNICODE_COPY(_PyUnicode_WSTR(w), _PyUnicode_WSTR(v),
+                        length < _PyUnicode_WSTR_LENGTH(v) ? length : _PyUnicode_WSTR_LENGTH(v));
         Py_DECREF(*unicode);
         *unicode = w;
         return 0;
@@ -474,44 +971,85 @@
     return _PyUnicode_Resize((PyUnicodeObject **)unicode, length);
 }
 
+static PyObject*
+get_latin1_char(unsigned char ch)
+{
+    PyUnicodeObject *unicode = unicode_latin1[ch];
+    if (!unicode) {
+        unicode = (PyUnicodeObject *)PyUnicode_New(1, ch);
+        if (!unicode)
+            return NULL;
+        PyUnicode_1BYTE_DATA(unicode)[0] = ch;
+        unicode_latin1[ch] = unicode;
+    }
+    Py_INCREF(unicode);
+    return (PyObject *)unicode;
+}
+
 PyObject *
 PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
 {
     PyUnicodeObject *unicode;
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t num_surrogates;
+
+    if (u == NULL)
+        return (PyObject*)_PyUnicode_New(size);
 
     /* If the Unicode data is known at construction time, we can apply
        some optimizations which share commonly used objects. */
-    if (u != NULL) {
 
-        /* Optimization for empty strings */
-        if (size == 0 && unicode_empty != NULL) {
-            Py_INCREF(unicode_empty);
-            return (PyObject *)unicode_empty;
-        }
-
-        /* Single character Unicode objects in the Latin-1 range are
-           shared when using this constructor */
-        if (size == 1 && *u < 256) {
-            unicode = unicode_latin1[*u];
-            if (!unicode) {
-                unicode = _PyUnicode_New(1);
-                if (!unicode)
-                    return NULL;
-                unicode->str[0] = *u;
-                unicode_latin1[*u] = unicode;
-            }
-            Py_INCREF(unicode);
-            return (PyObject *)unicode;
-        }
+    /* Optimization for empty strings */
+    if (size == 0 && unicode_empty != NULL) {
+        Py_INCREF(unicode_empty);
+        return (PyObject *)unicode_empty;
     }
 
-    unicode = _PyUnicode_New(size);
+    /* Single character Unicode objects in the Latin-1 range are
+       shared when using this constructor */
+    if (size == 1 && *u < 256)
+        return get_latin1_char((unsigned char)*u);
+
+    /* If not empty and not single character, copy the Unicode data
+       into the new object */
+    if (_PyUnicode_FindMaxCharAndNumSurrogatePairs(u, u + size, &maxchar,
+                                                  &num_surrogates) == -1)
+        return NULL;
+
+    unicode = (PyUnicodeObject *) PyUnicode_New(size - num_surrogates,
+                                                maxchar);
     if (!unicode)
         return NULL;
 
-    /* Copy the Unicode data into the new object */
-    if (u != NULL)
-        Py_UNICODE_COPY(unicode->str, u, size);
+    switch (PyUnicode_KIND(unicode)) {
+    case PyUnicode_1BYTE_KIND:
+        PyUnicode_CONVERT_BYTES(Py_UNICODE, unsigned char,
+                                u, u + size, PyUnicode_1BYTE_DATA(unicode));
+        break;
+    case PyUnicode_2BYTE_KIND:
+#if Py_UNICODE_SIZE == 2
+        Py_MEMCPY(PyUnicode_2BYTE_DATA(unicode), u, size * 2);
+#else
+        PyUnicode_CONVERT_BYTES(Py_UNICODE, Py_UCS2,
+                                u, u + size, PyUnicode_2BYTE_DATA(unicode));
+#endif
+        break;
+    case PyUnicode_4BYTE_KIND:
+#if SIZEOF_WCHAR_T == 2
+        /* This is the only case which has to process surrogates, thus
+           a simple copy loop is not enough and we need a function. */
+        if (unicode_convert_wchar_to_ucs4(u, u + size, unicode) < 0) {
+            Py_DECREF(unicode);
+            return NULL;
+        }
+#else
+        assert(num_surrogates == 0);
+        Py_MEMCPY(PyUnicode_4BYTE_DATA(unicode), u, size * 4);
+#endif
+        break;
+    default:
+        assert(0 && "Impossible state");
+    }
 
     return (PyObject *)unicode;
 }
@@ -541,18 +1079,8 @@
 
         /* Single characters are shared when using this constructor.
            Restrict to ASCII, since the input must be UTF-8. */
-        if (size == 1 && Py_CHARMASK(*u) < 128) {
-            unicode = unicode_latin1[Py_CHARMASK(*u)];
-            if (!unicode) {
-                unicode = _PyUnicode_New(1);
-                if (!unicode)
-                    return NULL;
-                unicode->str[0] = Py_CHARMASK(*u);
-                unicode_latin1[Py_CHARMASK(*u)] = unicode;
-            }
-            Py_INCREF(unicode);
-            return (PyObject *)unicode;
-        }
+        if (size == 1 && Py_CHARMASK(*u) < 128)
+            return get_latin1_char(Py_CHARMASK(*u));
 
         return PyUnicode_DecodeUTF8(u, size, NULL);
     }
@@ -576,76 +1104,196 @@
     return PyUnicode_FromStringAndSize(u, size);
 }
 
-#ifdef HAVE_WCHAR_H
-
-#if (Py_UNICODE_SIZE == 2) && defined(SIZEOF_WCHAR_T) && (SIZEOF_WCHAR_T == 4)
-# define CONVERT_WCHAR_TO_SURROGATES
-#endif
-
-#ifdef CONVERT_WCHAR_TO_SURROGATES
-
-/* Here sizeof(wchar_t) is 4 but Py_UNICODE_SIZE == 2, so we need
-   to convert from UTF32 to UTF16. */
-
-PyObject *
-PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size)
+PyObject*
+PyUnicode_FromUCS1(const unsigned char* u, Py_ssize_t size)
 {
-    PyUnicodeObject *unicode;
-    register Py_ssize_t i;
-    Py_ssize_t alloc;
-    const wchar_t *orig_w;
-
-    if (w == NULL) {
-        if (size == 0)
-            return PyUnicode_FromStringAndSize(NULL, 0);
-        PyErr_BadInternalCall();
-        return NULL;
-    }
-
-    if (size == -1) {
-        size = wcslen(w);
-    }
-
-    alloc = size;
-    orig_w = w;
-    for (i = size; i > 0; i--) {
-        if (*w > 0xFFFF)
-            alloc++;
-        w++;
-    }
-    w = orig_w;
-    unicode = _PyUnicode_New(alloc);
-    if (!unicode)
-        return NULL;
-
-    /* Copy the wchar_t data into the new object */
-    {
-        register Py_UNICODE *u;
-        u = PyUnicode_AS_UNICODE(unicode);
-        for (i = size; i > 0; i--) {
-            if (*w > 0xFFFF) {
-                wchar_t ordinal = *w++;
-                ordinal -= 0x10000;
-                *u++ = 0xD800 | (ordinal >> 10);
-                *u++ = 0xDC00 | (ordinal & 0x3FF);
-            }
-            else
-                *u++ = *w++;
+    PyObject *res;
+    unsigned char max = 127;
+    Py_ssize_t i;
+    for (i = 0; i < size; i++) {
+        if (u[i] & 0x80) {
+            max = 255;
+            break;
         }
     }
-    return (PyObject *)unicode;
+    res = PyUnicode_New(size, max);
+    if (!res)
+        return NULL;
+    memcpy(PyUnicode_1BYTE_DATA(res), u, size);
+    return res;
 }
 
-#else
+PyObject*
+PyUnicode_FromUCS2(const Py_UCS2 *u, Py_ssize_t size)
+{
+    PyObject *res;
+    Py_UCS2 max = 0;
+    Py_ssize_t i;
+    for (i = 0; i < size; i++)
+        if (u[i] > max)
+            max = u[i];
+    res = PyUnicode_New(size, max);
+    if (!res)
+        return NULL;
+    if (max >= 256)
+        memcpy(PyUnicode_2BYTE_DATA(res), u, sizeof(Py_UCS2)*size);
+    else
+        for (i = 0; i < size; i++)
+            PyUnicode_1BYTE_DATA(res)[i] = (Py_UCS1)u[i];
+    return res;
+}
+
+PyObject*
+PyUnicode_FromUCS4(const Py_UCS4 *u, Py_ssize_t size)
+{
+    PyObject *res;
+    Py_UCS4 max = 0;
+    Py_ssize_t i;
+    for (i = 0; i < size; i++)
+        if (u[i] > max)
+            max = u[i];
+    res = PyUnicode_New(size, max);
+    if (!res)
+        return NULL;
+    if (max >= 0x10000)
+        memcpy(PyUnicode_4BYTE_DATA(res), u, sizeof(Py_UCS4)*size);
+    else {
+        int kind = PyUnicode_KIND(res);
+        void *data = PyUnicode_DATA(res);
+        for (i = 0; i < size; i++)
+            PyUnicode_WRITE(kind, data, i, u[i]);
+    }
+    return res;
+}
+
+PyObject*
+PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
+{
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        return PyUnicode_FromUCS1(buffer, size);
+    case PyUnicode_2BYTE_KIND:
+        return PyUnicode_FromUCS2(buffer, size);
+    case PyUnicode_4BYTE_KIND:
+        return PyUnicode_FromUCS4(buffer, size);
+    }
+    assert(0);
+    return NULL;
+}
+
+
+/* Widen Unicode objects to larger buffers.
+   Return NULL if the string is too wide already. */
+
+void*
+_PyUnicode_AsKind(PyObject *s, unsigned int kind)
+{
+    Py_ssize_t i;
+    Py_ssize_t len = PyUnicode_GET_LENGTH(s);
+    void *d = PyUnicode_DATA(s);
+    unsigned int skind = PyUnicode_KIND(s);
+    if (PyUnicode_KIND(s) >= kind) {
+        PyErr_SetString(PyExc_RuntimeError, "invalid widening attempt");
+        return NULL;
+    }
+    switch(kind) {
+    case PyUnicode_2BYTE_KIND: {
+        Py_UCS2 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS2));
+        if (!result) {
+            PyErr_NoMemory();
+            return 0;
+        }
+        for (i = 0; i < len; i++)
+            result[i] = ((Py_UCS1*)d)[i];
+        return result;
+    }
+    case PyUnicode_4BYTE_KIND: {
+        Py_UCS4 *result = PyMem_Malloc(PyUnicode_GET_LENGTH(s) * sizeof(Py_UCS4));
+        if (!result) {
+            PyErr_NoMemory();
+            return 0;
+        }
+        for (i = 0; i < len; i++)
+            result[i] = PyUnicode_READ(skind, d, i);
+        return result;
+    }
+    }
+    Py_FatalError("invalid kind");
+    return NULL;
+}
+
+static Py_UCS4*
+as_ucs4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
+        int copy_null)
+{
+    int kind;
+    void *data;
+    Py_ssize_t len, targetlen;
+    if (PyUnicode_READY(string) == -1)
+        return NULL;
+    kind = PyUnicode_KIND(string);
+    data = PyUnicode_DATA(string);
+    len = PyUnicode_GET_LENGTH(string);
+    targetlen = len;
+    if (copy_null)
+        targetlen++;
+    if (!target) {
+        if (PY_SSIZE_T_MAX / sizeof(Py_UCS4) < targetlen) {
+            PyErr_NoMemory();
+            return NULL;
+        }
+        target = PyMem_Malloc(targetlen * sizeof(Py_UCS4));
+        if (!target) {
+            PyErr_NoMemory();
+            return NULL;
+        }
+    }
+    else {
+        if (targetsize < targetlen) {
+            PyErr_Format(PyExc_SystemError,
+                         "string is longer than the buffer");
+            if (copy_null && 0 < targetsize)
+                target[0] = 0;
+            return NULL;
+        }
+    }
+    if (kind != PyUnicode_4BYTE_KIND) {
+        Py_ssize_t i;
+        for (i = 0; i < len; i++)
+            target[i] = PyUnicode_READ(kind, data, i);
+    }
+    else
+        Py_MEMCPY(target, data, len * sizeof(Py_UCS4));
+    if (copy_null)
+        target[len] = 0;
+    return target;
+}
+
+Py_UCS4*
+PyUnicode_AsUCS4(PyObject *string, Py_UCS4 *target, Py_ssize_t targetsize,
+                 int copy_null)
+{
+    if (target == NULL || targetsize < 1) {
+        PyErr_BadInternalCall();
+        return NULL;
+    }
+    return as_ucs4(string, target, targetsize, copy_null);
+}
+
+Py_UCS4*
+PyUnicode_AsUCS4Copy(PyObject *string)
+{
+    return as_ucs4(string, NULL, 0, 1);
+}
+
+#ifdef HAVE_WCHAR_H
 
 PyObject *
 PyUnicode_FromWideChar(register const wchar_t *w, Py_ssize_t size)
 {
-    PyUnicodeObject *unicode;
-
     if (w == NULL) {
         if (size == 0)
-            return PyUnicode_FromStringAndSize(NULL, 0);
+            return PyUnicode_New(0, 0);
         PyErr_BadInternalCall();
         return NULL;
     }
@@ -654,29 +1302,10 @@
         size = wcslen(w);
     }
 
-    unicode = _PyUnicode_New(size);
-    if (!unicode)
-        return NULL;
-
-    /* Copy the wchar_t data into the new object */
-#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
-    memcpy(unicode->str, w, size * sizeof(wchar_t));
-#else
-    {
-        register Py_UNICODE *u;
-        register Py_ssize_t i;
-        u = PyUnicode_AS_UNICODE(unicode);
-        for (i = size; i > 0; i--)
-            *u++ = *w++;
-    }
-#endif
-
-    return (PyObject *)unicode;
+    return PyUnicode_FromUnicode(w, size);
 }
 
-#endif /* CONVERT_WCHAR_TO_SURROGATES */
-
-#undef CONVERT_WCHAR_TO_SURROGATES
+#endif /* HAVE_WCHAR_H */
 
 static void
 makefmt(char *fmt, int longflag, int longlongflag, int size_tflag,
@@ -779,10 +1408,6 @@
     return f;
 }
 
-#define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;}
-
-/* size of fixed-size buffer for formatting single arguments */
-#define ITEM_BUFFER_LEN 21
 /* maximum number of characters required for output of %ld.  21 characters
    allows for 64-bit integers (in decimal) and an optional sign. */
 #define MAX_LONG_CHARS 21
@@ -803,80 +1428,35 @@
     int precision = 0;
     int zeropad;
     const char* f;
-    Py_UNICODE *s;
-    PyObject *string;
+    PyUnicodeObject *string;
     /* used by sprintf */
-    char buffer[ITEM_BUFFER_LEN+1];
-    /* use abuffer instead of buffer, if we need more space
-     * (which can happen if there's a format specifier with width). */
-    char *abuffer = NULL;
-    char *realbuffer;
-    Py_ssize_t abuffersize = 0;
     char fmt[61]; /* should be enough for %0width.precisionlld */
-    const char *copy;
+    Py_UCS4 maxchar = 127; /* result is ASCII by default */
+    Py_UCS4 argmaxchar;
+    Py_ssize_t numbersize = 0;
+    char *numberresults = NULL;
+    char *numberresult = NULL;
+    Py_ssize_t i;
+    int kind;
+    void *data;
 
     Py_VA_COPY(count, vargs);
     /* step 1: count the number of %S/%R/%A/%s format specifications
      * (we call PyObject_Str()/PyObject_Repr()/PyObject_ASCII()/
      * PyUnicode_DecodeUTF8() for these objects once during step 3 and put the
-     * result in an array) */
-    for (f = format; *f; f++) {
-         if (*f == '%') {
-             /* skip width or width.precision (eg. "1.2" of "%1.2f") */
-             f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL);
-             if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V')
-                 ++callcount;
-         }
-         else if (128 <= (unsigned char)*f) {
-             PyErr_Format(PyExc_ValueError,
-                "PyUnicode_FromFormatV() expects an ASCII-encoded format "
-                "string, got a non-ASCII byte: 0x%02x",
-                (unsigned char)*f);
-             return NULL;
-         }
-    }
-    /* step 2: allocate memory for the results of
-     * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */
-    if (callcount) {
-        callresults = PyObject_Malloc(sizeof(PyObject *)*callcount);
-        if (!callresults) {
-            PyErr_NoMemory();
-            return NULL;
-        }
-        callresult = callresults;
-    }
-    /* step 3: figure out how large a buffer we need */
+     * result in an array)
+     * also esimate a upper bound for all the number formats in the string,
+     * numbers will be formated in step 3 and be keept in a '\0'-separated
+     * buffer before putting everything together. */
     for (f = format; *f; f++) {
         if (*f == '%') {
-#ifdef HAVE_LONG_LONG
             int longlongflag;
-#endif
-            const char* p;
+            /* skip width or width.precision (eg. "1.2" of "%1.2f") */
+            f = parse_format_flags(f, &width, NULL, NULL, &longlongflag, NULL);
+            if (*f == 's' || *f=='S' || *f=='R' || *f=='A' || *f=='V')
+                ++callcount;
 
-            p = f;
-            f = parse_format_flags(f, &width, NULL,
-                                   NULL, &longlongflag, NULL);
-
-            switch (*f) {
-            case 'c':
-            {
-#ifndef Py_UNICODE_WIDE
-                int ordinal = va_arg(count, int);
-                if (ordinal > 0xffff)
-                    n += 2;
-                else
-                    n++;
-#else
-                (void)va_arg(count, int);
-                n++;
-#endif
-                break;
-            }
-            case '%':
-                n++;
-                break;
-            case 'd': case 'u': case 'i': case 'x':
-                (void) va_arg(count, int);
+            else if (*f == 'd' || *f=='u' || *f=='i' || *f=='x' || *f=='p') {
 #ifdef HAVE_LONG_LONG
                 if (longlongflag) {
                     if (width < MAX_LONG_LONG_CHARS)
@@ -890,10 +1470,142 @@
                        need more (which we allocate later). */
                     if (width < MAX_LONG_CHARS)
                         width = MAX_LONG_CHARS;
-                n += width;
-                /* XXX should allow for large precision here too. */
-                if (abuffersize < width)
-                    abuffersize = width;
+
+                /* account for the size + '\0' to separate numbers
+                   inside of the numberresults buffer */
+                numbersize += (width + 1);
+            }
+        }
+        else if ((unsigned char)*f > 127) {
+            PyErr_Format(PyExc_ValueError,
+                "PyUnicode_FromFormatV() expects an ASCII-encoded format "
+                "string, got a non-ASCII byte: 0x%02x",
+                (unsigned char)*f);
+            return NULL;
+        }
+    }
+    /* step 2: allocate memory for the results of
+     * PyObject_Str()/PyObject_Repr()/PyUnicode_DecodeUTF8() calls */
+    if (callcount) {
+        callresults = PyObject_Malloc(sizeof(PyObject *) * callcount);
+        if (!callresults) {
+            PyErr_NoMemory();
+            return NULL;
+        }
+        callresult = callresults;
+    }
+    /* step 2.5: allocate memory for the results of formating numbers */
+    if (numbersize) {
+        numberresults = PyObject_Malloc(numbersize);
+        if (!numberresults) {
+            PyErr_NoMemory();
+            goto fail;
+        }
+        numberresult = numberresults;
+    }
+
+    /* step 3: format numbers and figure out how large a buffer we need */
+    for (f = format; *f; f++) {
+        if (*f == '%') {
+            const char* p;
+            int longflag;
+            int longlongflag;
+            int size_tflag;
+            int numprinted;
+
+            p = f;
+            zeropad = (f[1] == '0');
+            f = parse_format_flags(f, &width, &precision,
+                                   &longflag, &longlongflag, &size_tflag);
+            switch (*f) {
+            case 'c':
+            {
+                Py_UCS4 ordinal = va_arg(count, int);
+                maxchar = PY_MAX(maxchar, ordinal);
+                n++;
+                break;
+            }
+            case '%':
+                n++;
+                break;
+            case 'i':
+            case 'd':
+                makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
+                        width, precision, *f);
+                if (longflag)
+                    numprinted = sprintf(numberresult, fmt,
+                                         va_arg(count, long));
+#ifdef HAVE_LONG_LONG
+                else if (longlongflag)
+                    numprinted = sprintf(numberresult, fmt,
+                                         va_arg(count, PY_LONG_LONG));
+#endif
+                else if (size_tflag)
+                    numprinted = sprintf(numberresult, fmt,
+                                         va_arg(count, Py_ssize_t));
+                else
+                    numprinted = sprintf(numberresult, fmt,
+                                         va_arg(count, int));
+                n += numprinted;
+                /* advance by +1 to skip over the '\0' */
+                numberresult += (numprinted + 1);
+                assert(*(numberresult - 1) == '\0');
+                assert(*(numberresult - 2) != '\0');
+                assert(numprinted >= 0);
+                assert(numberresult <= numberresults + numbersize);
+                break;
+            case 'u':
+                makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
+                        width, precision, 'u');
+                if (longflag)
+                    numprinted = sprintf(numberresult, fmt,
+                                         va_arg(count, unsigned long));
+#ifdef HAVE_LONG_LONG
+                else if (longlongflag)
+                    numprinted = sprintf(numberresult, fmt,
+                                         va_arg(count, unsigned PY_LONG_LONG));
+#endif
+                else if (size_tflag)
+                    numprinted = sprintf(numberresult, fmt,
+                                         va_arg(count, size_t));
+                else
+                    numprinted = sprintf(numberresult, fmt,
+                                         va_arg(count, unsigned int));
+                n += numprinted;
+                numberresult += (numprinted + 1);
+                assert(*(numberresult - 1) == '\0');
+                assert(*(numberresult - 2) != '\0');
+                assert(numprinted >= 0);
+                assert(numberresult <= numberresults + numbersize);
+                break;
+            case 'x':
+                makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x');
+                numprinted = sprintf(numberresult, fmt, va_arg(count, int));
+                n += numprinted;
+                numberresult += (numprinted + 1);
+                assert(*(numberresult - 1) == '\0');
+                assert(*(numberresult - 2) != '\0');
+                assert(numprinted >= 0);
+                assert(numberresult <= numberresults + numbersize);
+                break;
+            case 'p':
+                numprinted = sprintf(numberresult, "%p", va_arg(count, void*));
+                /* %p is ill-defined:  ensure leading 0x. */
+                if (numberresult[1] == 'X')
+                    numberresult[1] = 'x';
+                else if (numberresult[1] != 'x') {
+                    memmove(numberresult + 2, numberresult,
+                            strlen(numberresult) + 1);
+                    numberresult[0] = '0';
+                    numberresult[1] = 'x';
+                    numprinted += 2;
+                }
+                n += numprinted;
+                numberresult += (numprinted + 1);
+                assert(*(numberresult - 1) == '\0');
+                assert(*(numberresult - 2) != '\0');
+                assert(numprinted >= 0);
+                assert(numberresult <= numberresults + numbersize);
                 break;
             case 's':
             {
@@ -902,7 +1614,11 @@
                 PyObject *str = PyUnicode_DecodeUTF8(s, strlen(s), "replace");
                 if (!str)
                     goto fail;
-                n += PyUnicode_GET_SIZE(str);
+                /* since PyUnicode_DecodeUTF8 returns already flexible
+                   unicode objects, there is no need to call ready on them */
+                argmaxchar = PyUnicode_MAX_CHAR_VALUE(str);
+                maxchar = PY_MAX(maxchar, argmaxchar);
+                n += PyUnicode_GET_LENGTH(str);
                 /* Remember the str and switch to the next slot */
                 *callresult++ = str;
                 break;
@@ -911,7 +1627,11 @@
             {
                 PyObject *obj = va_arg(count, PyObject *);
                 assert(obj && PyUnicode_Check(obj));
-                n += PyUnicode_GET_SIZE(obj);
+                if (PyUnicode_READY(obj) == -1)
+                    goto fail;
+                argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj);
+                maxchar = PY_MAX(maxchar, argmaxchar);
+                n += PyUnicode_GET_LENGTH(obj);
                 break;
             }
             case 'V':
@@ -922,14 +1642,20 @@
                 assert(obj || str);
                 assert(!obj || PyUnicode_Check(obj));
                 if (obj) {
-                    n += PyUnicode_GET_SIZE(obj);
+                    if (PyUnicode_READY(obj) == -1)
+                        goto fail;
+                    argmaxchar = PyUnicode_MAX_CHAR_VALUE(obj);
+                    maxchar = PY_MAX(maxchar, argmaxchar);
+                    n += PyUnicode_GET_LENGTH(obj);
                     *callresult++ = NULL;
                 }
                 else {
                     str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace");
                     if (!str_obj)
                         goto fail;
-                    n += PyUnicode_GET_SIZE(str_obj);
+                    argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj);
+                    maxchar = PY_MAX(maxchar, argmaxchar);
+                    n += PyUnicode_GET_LENGTH(str_obj);
                     *callresult++ = str_obj;
                 }
                 break;
@@ -940,9 +1666,11 @@
                 PyObject *str;
                 assert(obj);
                 str = PyObject_Str(obj);
-                if (!str)
+                if (!str || PyUnicode_READY(str) == -1)
                     goto fail;
-                n += PyUnicode_GET_SIZE(str);
+                argmaxchar = PyUnicode_MAX_CHAR_VALUE(str);
+                maxchar = PY_MAX(maxchar, argmaxchar);
+                n += PyUnicode_GET_LENGTH(str);
                 /* Remember the str and switch to the next slot */
                 *callresult++ = str;
                 break;
@@ -953,9 +1681,11 @@
                 PyObject *repr;
                 assert(obj);
                 repr = PyObject_Repr(obj);
-                if (!repr)
+                if (!repr || PyUnicode_READY(repr) == -1)
                     goto fail;
-                n += PyUnicode_GET_SIZE(repr);
+                argmaxchar = PyUnicode_MAX_CHAR_VALUE(repr);
+                maxchar = PY_MAX(maxchar, argmaxchar);
+                n += PyUnicode_GET_LENGTH(repr);
                 /* Remember the repr and switch to the next slot */
                 *callresult++ = repr;
                 break;
@@ -966,22 +1696,15 @@
                 PyObject *ascii;
                 assert(obj);
                 ascii = PyObject_ASCII(obj);
-                if (!ascii)
+                if (!ascii || PyUnicode_READY(ascii) == -1)
                     goto fail;
-                n += PyUnicode_GET_SIZE(ascii);
+                argmaxchar = PyUnicode_MAX_CHAR_VALUE(ascii);
+                maxchar = PY_MAX(maxchar, argmaxchar);
+                n += PyUnicode_GET_LENGTH(ascii);
                 /* Remember the repr and switch to the next slot */
                 *callresult++ = ascii;
                 break;
             }
-            case 'p':
-                (void) va_arg(count, int);
-                /* maximum 64-bit pointer representation:
-                 * 0xffffffffffffffff
-                 * so 19 characters is enough.
-                 * XXX I count 18 -- what's the extra for?
-                 */
-                n += 19;
-                break;
             default:
                 /* if we stumble upon an unknown
                    formatting code, copy the rest of
@@ -996,98 +1719,65 @@
             n++;
     }
   expand:
-    if (abuffersize > ITEM_BUFFER_LEN) {
-        /* add 1 for sprintf's trailing null byte */
-        abuffer = PyObject_Malloc(abuffersize + 1);
-        if (!abuffer) {
-            PyErr_NoMemory();
-            goto fail;
-        }
-        realbuffer = abuffer;
-    }
-    else
-        realbuffer = buffer;
     /* step 4: fill the buffer */
-    /* Since we've analyzed how much space we need for the worst case,
+    /* Since we've analyzed how much space we need,
        we don't have to resize the string.
        There can be no errors beyond this point. */
-    string = PyUnicode_FromUnicode(NULL, n);
+    string = (PyUnicodeObject *)PyUnicode_New(n, maxchar);
     if (!string)
         goto fail;
-
-    s = PyUnicode_AS_UNICODE(string);
+    kind = PyUnicode_KIND(string);
+    data = PyUnicode_DATA(string);
     callresult = callresults;
+    numberresult = numberresults;
 
-    for (f = format; *f; f++) {
+    for (i = 0, f = format; *f; f++) {
         if (*f == '%') {
             const char* p;
-            int longflag;
-            int longlongflag;
-            int size_tflag;
 
             p = f;
-            zeropad = (f[1] == '0');
-            f = parse_format_flags(f, &width, &precision,
-                                   &longflag, &longlongflag, &size_tflag);
+            f = parse_format_flags(f, NULL, NULL, NULL, NULL, NULL);
+            /* checking for == because the last argument could be a empty
+               string, which causes i to point to end, the assert at the end of
+               the loop */
+            assert(i <= PyUnicode_GET_LENGTH(string));
 
             switch (*f) {
             case 'c':
             {
-                int ordinal = va_arg(vargs, int);
-#ifndef Py_UNICODE_WIDE
-                if (ordinal > 0xffff) {
-                    ordinal -= 0x10000;
-                    *s++ = 0xD800 | (ordinal >> 10);
-                    *s++ = 0xDC00 | (ordinal & 0x3FF);
-                } else
-#endif
-                *s++ = ordinal;
+                const int ordinal = va_arg(vargs, int);
+                PyUnicode_WRITE(kind, data, i++, ordinal);
                 break;
             }
             case 'i':
             case 'd':
-                makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
-                        width, precision, *f);
-                if (longflag)
-                    sprintf(realbuffer, fmt, va_arg(vargs, long));
-#ifdef HAVE_LONG_LONG
-                else if (longlongflag)
-                    sprintf(realbuffer, fmt, va_arg(vargs, PY_LONG_LONG));
-#endif
-                else if (size_tflag)
-                    sprintf(realbuffer, fmt, va_arg(vargs, Py_ssize_t));
-                else
-                    sprintf(realbuffer, fmt, va_arg(vargs, int));
-                appendstring(realbuffer);
-                break;
             case 'u':
-                makefmt(fmt, longflag, longlongflag, size_tflag, zeropad,
-                        width, precision, 'u');
-                if (longflag)
-                    sprintf(realbuffer, fmt, va_arg(vargs, unsigned long));
-#ifdef HAVE_LONG_LONG
-                else if (longlongflag)
-                    sprintf(realbuffer, fmt, va_arg(vargs,
-                                                    unsigned PY_LONG_LONG));
-#endif
-                else if (size_tflag)
-                    sprintf(realbuffer, fmt, va_arg(vargs, size_t));
-                else
-                    sprintf(realbuffer, fmt, va_arg(vargs, unsigned int));
-                appendstring(realbuffer);
-                break;
             case 'x':
-                makefmt(fmt, 0, 0, 0, zeropad, width, precision, 'x');
-                sprintf(realbuffer, fmt, va_arg(vargs, int));
-                appendstring(realbuffer);
+            case 'p':
+                /* unused, since we already have the result */
+                if (*f == 'p')
+                    (void) va_arg(vargs, void *);
+                else
+                    (void) va_arg(vargs, int);
+                /* extract the result from numberresults and append. */
+                for (; *numberresult; ++i, ++numberresult)
+                    PyUnicode_WRITE(kind, data, i, *numberresult);
+                /* skip over the separating '\0' */
+                assert(*numberresult == '\0');
+                numberresult++;
+                assert(numberresult <= numberresults + numbersize);
                 break;
             case 's':
             {
                 /* unused, since we already have the result */
+                Py_ssize_t size;
                 (void) va_arg(vargs, char *);
-                Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult),
-                                PyUnicode_GET_SIZE(*callresult));
-                s += PyUnicode_GET_SIZE(*callresult);
+                size = PyUnicode_GET_LENGTH(*callresult);
+                assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
+                PyUnicode_CopyCharacters((PyObject*)string, i,
+                                         *callresult, 0,
+                                         size);
+                i += size;
                 /* We're done with the unicode()/repr() => forget it */
                 Py_DECREF(*callresult);
                 /* switch to next unicode()/repr() result */
@@ -1097,23 +1787,35 @@
             case 'U':
             {
                 PyObject *obj = va_arg(vargs, PyObject *);
-                Py_ssize_t size = PyUnicode_GET_SIZE(obj);
-                Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);
-                s += size;
+                Py_ssize_t size;
+                assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
+                size = PyUnicode_GET_LENGTH(obj);
+                PyUnicode_CopyCharacters((PyObject*)string, i,
+                                         obj, 0,
+                                         size);
+                i += size;
                 break;
             }
             case 'V':
             {
+                Py_ssize_t size;
                 PyObject *obj = va_arg(vargs, PyObject *);
                 va_arg(vargs, const char *);
                 if (obj) {
-                    Py_ssize_t size = PyUnicode_GET_SIZE(obj);
-                    Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(obj), size);
-                    s += size;
+                    size = PyUnicode_GET_LENGTH(obj);
+                    assert(PyUnicode_KIND(obj) <= PyUnicode_KIND(string));
+                    PyUnicode_CopyCharacters((PyObject*)string, i,
+                                             obj, 0,
+                                             size);
+                    i += size;
                 } else {
-                    Py_UNICODE_COPY(s, PyUnicode_AS_UNICODE(*callresult),
-                                    PyUnicode_GET_SIZE(*callresult));
-                    s += PyUnicode_GET_SIZE(*callresult);
+                    size = PyUnicode_GET_LENGTH(*callresult);
+                    assert(PyUnicode_KIND(*callresult) <=
+                           PyUnicode_KIND(string));
+                    PyUnicode_CopyCharacters((PyObject*)string, i,
+                                             *callresult,
+                                             0, size);
+                    i += size;
                     Py_DECREF(*callresult);
                 }
                 ++callresult;
@@ -1123,52 +1825,42 @@
             case 'R':
             case 'A':
             {
-                Py_UNICODE *ucopy;
-                Py_ssize_t usize;
-                Py_ssize_t upos;
                 /* unused, since we already have the result */
                 (void) va_arg(vargs, PyObject *);
-                ucopy = PyUnicode_AS_UNICODE(*callresult);
-                usize = PyUnicode_GET_SIZE(*callresult);
-                for (upos = 0; upos<usize;)
-                    *s++ = ucopy[upos++];
+                assert(PyUnicode_KIND(*callresult) <= PyUnicode_KIND(string));
+                PyUnicode_CopyCharacters((PyObject*)string, i,
+                                         *callresult, 0,
+                                         PyUnicode_GET_LENGTH(*callresult));
+                i += PyUnicode_GET_LENGTH(*callresult);
                 /* We're done with the unicode()/repr() => forget it */
                 Py_DECREF(*callresult);
                 /* switch to next unicode()/repr() result */
                 ++callresult;
                 break;
             }
-            case 'p':
-                sprintf(buffer, "%p", va_arg(vargs, void*));
-                /* %p is ill-defined:  ensure leading 0x. */
-                if (buffer[1] == 'X')
-                    buffer[1] = 'x';
-                else if (buffer[1] != 'x') {
-                    memmove(buffer+2, buffer, strlen(buffer)+1);
-                    buffer[0] = '0';
-                    buffer[1] = 'x';
-                }
-                appendstring(buffer);
-                break;
             case '%':
-                *s++ = '%';
+                PyUnicode_WRITE(kind, data, i++, '%');
                 break;
             default:
-                appendstring(p);
+                for (; *p; ++p, ++i)
+                    PyUnicode_WRITE(kind, data, i, *p);
+                assert(i == PyUnicode_GET_LENGTH(string));
                 goto end;
             }
         }
-        else
-            *s++ = *f;
+        else {
+            assert(i < PyUnicode_GET_LENGTH(string));
+            PyUnicode_WRITE(kind, data, i++, *f);
+        }
     }
+    assert(i == PyUnicode_GET_LENGTH(string));
 
   end:
     if (callresults)
         PyObject_Free(callresults);
-    if (abuffer)
-        PyObject_Free(abuffer);
-    PyUnicode_Resize(&string, s - PyUnicode_AS_UNICODE(string));
-    return string;
+    if (numberresults)
+        PyObject_Free(numberresults);
+    return (PyObject *)string;
   fail:
     if (callresults) {
         PyObject **callresult2 = callresults;
@@ -1178,13 +1870,11 @@
         }
         PyObject_Free(callresults);
     }
-    if (abuffer)
-        PyObject_Free(abuffer);
+    if (numberresults)
+        PyObject_Free(numberresults);
     return NULL;
 }
 
-#undef appendstring
-
 PyObject *
 PyUnicode_FromFormat(const char *format, ...)
 {
@@ -1201,6 +1891,8 @@
     return ret;
 }
 
+#ifdef HAVE_WCHAR_H
+
 /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString():
    convert a Unicode object to a wide character string.
 
@@ -1215,99 +1907,23 @@
                    wchar_t *w,
                    Py_ssize_t size)
 {
-#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
     Py_ssize_t res;
+    const wchar_t *wstr;
+
+    wstr = PyUnicode_AsUnicodeAndSize((PyObject *)unicode, &res);
+    if (wstr == NULL)
+        return -1;
+
     if (w != NULL) {
-        res = PyUnicode_GET_SIZE(unicode);
         if (size > res)
             size = res + 1;
         else
             res = size;
-        memcpy(w, unicode->str, size * sizeof(wchar_t));
+        Py_MEMCPY(w, wstr, size * sizeof(wchar_t));
         return res;
     }
     else
-        return PyUnicode_GET_SIZE(unicode) + 1;
-#elif Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4
-    register const Py_UNICODE *u;
-    const Py_UNICODE *uend;
-    const wchar_t *worig, *wend;
-    Py_ssize_t nchar;
-
-    u = PyUnicode_AS_UNICODE(unicode);
-    uend = u + PyUnicode_GET_SIZE(unicode);
-    if (w != NULL) {
-        worig = w;
-        wend = w + size;
-        while (u != uend && w != wend) {
-            if (0xD800 <= u[0] && u[0] <= 0xDBFF
-                && 0xDC00 <= u[1] && u[1] <= 0xDFFF)
-            {
-                *w = (((u[0] & 0x3FF) << 10) | (u[1] & 0x3FF)) + 0x10000;
-                u += 2;
-            }
-            else {
-                *w = *u;
-                u++;
-            }
-            w++;
-        }
-        if (w != wend)
-            *w = L'\0';
-        return w - worig;
-    }
-    else {
-        nchar = 1; /* null character at the end */
-        while (u != uend) {
-            if (0xD800 <= u[0] && u[0] <= 0xDBFF
-                && 0xDC00 <= u[1] && u[1] <= 0xDFFF)
-                u += 2;
-            else
-                u++;
-            nchar++;
-        }
-    }
-    return nchar;
-#elif Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2
-    register Py_UNICODE *u, *uend, ordinal;
-    register Py_ssize_t i;
-    wchar_t *worig, *wend;
-    Py_ssize_t nchar;
-
-    u = PyUnicode_AS_UNICODE(unicode);
-    uend = u + PyUnicode_GET_SIZE(u);
-    if (w != NULL) {
-        worig = w;
-        wend = w + size;
-        while (u != uend && w != wend) {
-            ordinal = *u;
-            if (ordinal > 0xffff) {
-                ordinal -= 0x10000;
-                *w++ = 0xD800 | (ordinal >> 10);
-                *w++ = 0xDC00 | (ordinal & 0x3FF);
-            }
-            else
-                *w++ = ordinal;
-            u++;
-        }
-        if (w != wend)
-            *w = 0;
-        return w - worig;
-    }
-    else {
-        nchar = 1; /* null character */
-        while (u != uend) {
-            if (*u > 0xffff)
-                nchar += 2;
-            else
-                nchar++;
-            u++;
-        }
-        return nchar;
-    }
-#else
-#  error "unsupported wchar_t and Py_UNICODE sizes, see issue #8670"
-#endif
+        return res + 1;
 }
 
 Py_ssize_t
@@ -1335,6 +1951,8 @@
     }
 
     buflen = unicode_aswidechar((PyUnicodeObject *)unicode, NULL, 0);
+    if (buflen == -1)
+        return NULL;
     if (PY_SSIZE_T_MAX / sizeof(wchar_t) < buflen) {
         PyErr_NoMemory();
         return NULL;
@@ -1346,35 +1964,33 @@
         return NULL;
     }
     buflen = unicode_aswidechar((PyUnicodeObject *)unicode, buffer, buflen);
+    if (buflen == -1)
+        return NULL;
     if (size != NULL)
         *size = buflen;
     return buffer;
 }
 
-#endif
+#endif /* HAVE_WCHAR_H */
 
 PyObject *
 PyUnicode_FromOrdinal(int ordinal)
 {
-    Py_UNICODE s[2];
-
+    PyObject *v;
     if (ordinal < 0 || ordinal > 0x10ffff) {
         PyErr_SetString(PyExc_ValueError,
                         "chr() arg not in range(0x110000)");
         return NULL;
     }
 
-#ifndef Py_UNICODE_WIDE
-    if (ordinal > 0xffff) {
-        ordinal -= 0x10000;
-        s[0] = 0xD800 | (ordinal >> 10);
-        s[1] = 0xDC00 | (ordinal & 0x3FF);
-        return PyUnicode_FromUnicode(s, 2);
-    }
-#endif
+    if (ordinal < 256)
+        return get_latin1_char(ordinal);
 
-    s[0] = (Py_UNICODE)ordinal;
-    return PyUnicode_FromUnicode(s, 1);
+    v = PyUnicode_New(1, ordinal);
+    if (v == NULL)
+        return NULL;
+    PyUnicode_WRITE(PyUnicode_KIND(v), PyUnicode_DATA(v), 0, ordinal);
+    return v;
 }
 
 PyObject *
@@ -1389,8 +2005,9 @@
     if (PyUnicode_Check(obj)) {
         /* For a Unicode subtype that's not a Unicode object,
            return a true Unicode object with the same data. */
-        return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(obj),
-                                     PyUnicode_GET_SIZE(obj));
+        if (PyUnicode_READY(obj) == -1)
+            return NULL;
+        return substring((PyUnicodeObject *)obj, 0, PyUnicode_GET_LENGTH(obj));
     }
     PyErr_Format(PyExc_TypeError,
                  "Can't convert '%.100s' object to str implicitly",
@@ -1536,6 +2153,10 @@
         goto onError;
     }
     Py_DECREF(buffer);
+    if (PyUnicode_READY(unicode)) {
+        Py_DECREF(unicode);
+        return NULL;
+    }
     return unicode;
 
   onError:
@@ -1649,9 +2270,7 @@
                                 PyUnicode_GET_SIZE(unicode),
                                 NULL);
 #elif defined(__APPLE__)
-    return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
-                                PyUnicode_GET_SIZE(unicode),
-                                "surrogateescape");
+    return _PyUnicode_AsUTF8String(unicode, "surrogateescape");
 #else
     PyInterpreterState *interp = PyThreadState_GET()->interp;
     /* Bootstrap check: if the filesystem codec is implemented in Python, we
@@ -1721,11 +2340,9 @@
 
     if (encoding == NULL) {
         if (errors == NULL || strcmp(errors, "strict") == 0)
-            return PyUnicode_AsUTF8String(unicode);
+            return _PyUnicode_AsUTF8String(unicode, NULL);
         else
-            return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
-                                        PyUnicode_GET_SIZE(unicode),
-                                        errors);
+            return _PyUnicode_AsUTF8String(unicode, errors);
     }
 
     /* Shortcuts for common default encodings */
@@ -1734,18 +2351,14 @@
             (strcmp(lower, "utf8") == 0))
         {
             if (errors == NULL || strcmp(errors, "strict") == 0)
-                return PyUnicode_AsUTF8String(unicode);
+                return _PyUnicode_AsUTF8String(unicode, NULL);
             else
-                return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
-                                            PyUnicode_GET_SIZE(unicode),
-                                            errors);
+                return _PyUnicode_AsUTF8String(unicode, errors);
         }
         else if ((strcmp(lower, "latin-1") == 0) ||
                  (strcmp(lower, "latin1") == 0) ||
                  (strcmp(lower, "iso-8859-1") == 0))
-            return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode),
-                                          PyUnicode_GET_SIZE(unicode),
-                                          errors);
+            return _PyUnicode_AsLatin1String(unicode, errors);
 #ifdef HAVE_MBCS
         else if (strcmp(lower, "mbcs") == 0)
             return PyUnicode_EncodeMBCS(PyUnicode_AS_UNICODE(unicode),
@@ -1753,9 +2366,7 @@
                                         errors);
 #endif
         else if (strcmp(lower, "ascii") == 0)
-            return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode),
-                                         PyUnicode_GET_SIZE(unicode),
-                                         errors);
+            return _PyUnicode_AsASCIIString(unicode, errors);
     }
 
     /* Encode via the codec registry */
@@ -1824,21 +2435,6 @@
     return NULL;
 }
 
-PyObject *
-_PyUnicode_AsDefaultEncodedString(PyObject *unicode)
-{
-    PyObject *v = ((PyUnicodeObject *)unicode)->defenc;
-    if (v)
-        return v;
-    v = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
-                             PyUnicode_GET_SIZE(unicode),
-                             NULL);
-    if (!v)
-        return NULL;
-    ((PyUnicodeObject *)unicode)->defenc = v;
-    return v;
-}
-
 PyObject*
 PyUnicode_DecodeFSDefault(const char *s) {
     Py_ssize_t size = (Py_ssize_t)strlen(s);
@@ -1935,13 +2531,13 @@
 PyUnicode_FSDecoder(PyObject* arg, void* addr)
 {
     PyObject *output = NULL;
-    Py_ssize_t size;
-    void *data;
     if (arg == NULL) {
         Py_DECREF(*(PyObject**)addr);
         return 1;
     }
     if (PyUnicode_Check(arg)) {
+        if (PyUnicode_READY(arg))
+            return 0;
         output = arg;
         Py_INCREF(output);
     }
@@ -1960,9 +2556,8 @@
             return 0;
         }
     }
-    size = PyUnicode_GET_SIZE(output);
-    data = PyUnicode_AS_UNICODE(output);
-    if (size != Py_UNICODE_strlen(data)) {
+    if (findchar(PyUnicode_DATA(output), PyUnicode_KIND(output),
+                 PyUnicode_GET_LENGTH(output), 0, 1)) {
         PyErr_SetString(PyExc_TypeError, "embedded NUL character");
         Py_DECREF(output);
         return 0;
@@ -1973,40 +2568,172 @@
 
 
 char*
-_PyUnicode_AsStringAndSize(PyObject *unicode, Py_ssize_t *psize)
+PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
 {
     PyObject *bytes;
+    PyUnicodeObject *u = (PyUnicodeObject *)unicode;
+
     if (!PyUnicode_Check(unicode)) {
         PyErr_BadArgument();
         return NULL;
     }
-    bytes = _PyUnicode_AsDefaultEncodedString(unicode);
-    if (bytes == NULL)
+    if (PyUnicode_READY(u) == -1)
         return NULL;
-    if (psize != NULL)
-        *psize = PyBytes_GET_SIZE(bytes);
-    return PyBytes_AS_STRING(bytes);
+
+    if (_PyUnicode_UTF8(unicode) == NULL) {
+        bytes = _PyUnicode_AsUTF8String(unicode, "strict");
+        if (bytes == NULL)
+            return NULL;
+        u->_base.utf8 = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1);
+        if (u->_base.utf8 == NULL) {
+            Py_DECREF(bytes);
+            return NULL;
+        }
+        u->_base.utf8_length = PyBytes_GET_SIZE(bytes);
+        Py_MEMCPY(u->_base.utf8, PyBytes_AS_STRING(bytes), u->_base.utf8_length + 1);
+        Py_DECREF(bytes);
+    }
+
+    if (psize)
+        *psize = _PyUnicode_UTF8_LENGTH(unicode);
+    return _PyUnicode_UTF8(unicode);
 }
 
 char*
-_PyUnicode_AsString(PyObject *unicode)
+PyUnicode_AsUTF8(PyObject *unicode)
 {
-    return _PyUnicode_AsStringAndSize(unicode, NULL);
+    return PyUnicode_AsUTF8AndSize(unicode, NULL);
+}
+
+#ifdef Py_DEBUG
+int unicode_as_unicode_calls = 0;
+#endif
+
+
+Py_UNICODE *
+PyUnicode_AsUnicodeAndSize(PyObject *unicode, Py_ssize_t *size)
+{
+    PyUnicodeObject *u;
+    const unsigned char *one_byte;
+#if SIZEOF_WCHAR_T == 4
+    const Py_UCS2 *two_bytes;
+#else
+    const Py_UCS4 *four_bytes;
+    const Py_UCS4 *ucs4_end;
+    Py_ssize_t num_surrogates;
+#endif
+    wchar_t *w;
+    wchar_t *wchar_end;
+
+    if (!PyUnicode_Check(unicode)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+    u = (PyUnicodeObject*)unicode;
+    if (_PyUnicode_WSTR(u) == NULL) {
+        /* Non-ASCII compact unicode object */
+        assert(_PyUnicode_KIND(u) != 0);
+        assert(PyUnicode_IS_READY(u));
+
+#ifdef Py_DEBUG
+        ++unicode_as_unicode_calls;
+#endif
+
+        if (PyUnicode_KIND(u) == PyUnicode_4BYTE_KIND) {
+#if SIZEOF_WCHAR_T == 2
+            four_bytes = PyUnicode_4BYTE_DATA(u);
+            ucs4_end = four_bytes + _PyUnicode_LENGTH(u);
+            num_surrogates = 0;
+
+            for (; four_bytes < ucs4_end; ++four_bytes) {
+                if (*four_bytes > 0xFFFF)
+                    ++num_surrogates;
+            }
+
+            _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(
+                    sizeof(wchar_t) * (_PyUnicode_LENGTH(u) + 1 + num_surrogates));
+            if (!_PyUnicode_WSTR(u)) {
+                PyErr_NoMemory();
+                return NULL;
+            }
+            _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u) + num_surrogates;
+
+            w = _PyUnicode_WSTR(u);
+            wchar_end = w + _PyUnicode_WSTR_LENGTH(u);
+            four_bytes = PyUnicode_4BYTE_DATA(u);
+            for (; four_bytes < ucs4_end; ++four_bytes, ++w) {
+                if (*four_bytes > 0xFFFF) {
+                    /* encode surrogate pair in this case */
+                    *w++ = 0xD800 | ((*four_bytes - 0x10000) >> 10);
+                    *w   = 0xDC00 | ((*four_bytes - 0x10000) & 0x3FF);
+                }
+                else
+                    *w = *four_bytes;
+
+                if (w > wchar_end) {
+                    assert(0 && "Miscalculated string end");
+                }
+            }
+            *w = 0;
+#else
+            /* sizeof(wchar_t) == 4 */
+            Py_FatalError("Impossible unicode object state, wstr and str "
+                          "should share memory already.");
+            return NULL;
+#endif
+        }
+        else {
+            _PyUnicode_WSTR(u) = (wchar_t *) PyObject_MALLOC(sizeof(wchar_t) *
+                                                  (_PyUnicode_LENGTH(u) + 1));
+            if (!_PyUnicode_WSTR(u)) {
+                PyErr_NoMemory();
+                return NULL;
+            }
+            if (!PyUnicode_IS_COMPACT_ASCII(u))
+                _PyUnicode_WSTR_LENGTH(u) = _PyUnicode_LENGTH(u);
+            w = _PyUnicode_WSTR(u);
+            wchar_end = w + _PyUnicode_LENGTH(u);
+
+            if (PyUnicode_KIND(u) == PyUnicode_1BYTE_KIND) {
+                one_byte = PyUnicode_1BYTE_DATA(u);
+                for (; w < wchar_end; ++one_byte, ++w)
+                    *w = *one_byte;
+                /* null-terminate the wstr */
+                *w = 0;
+            }
+            else if (PyUnicode_KIND(u) == PyUnicode_2BYTE_KIND) {
+#if SIZEOF_WCHAR_T == 4
+                two_bytes = PyUnicode_2BYTE_DATA(u);
+                for (; w < wchar_end; ++two_bytes, ++w)
+                    *w = *two_bytes;
+                /* null-terminate the wstr */
+                *w = 0;
+#else
+                /* sizeof(wchar_t) == 2 */
+                PyObject_FREE(_PyUnicode_WSTR(u));
+                _PyUnicode_WSTR(u) = NULL;
+                Py_FatalError("Impossible unicode object state, wstr "
+                              "and str should share memory already.");
+                return NULL;
+#endif
+            }
+            else {
+                assert(0 && "This should never happen.");
+            }
+        }
+    }
+    if (size != NULL)
+        *size = PyUnicode_WSTR_LENGTH(u);
+    return _PyUnicode_WSTR(u);
 }
 
 Py_UNICODE *
 PyUnicode_AsUnicode(PyObject *unicode)
 {
-    if (!PyUnicode_Check(unicode)) {
-        PyErr_BadArgument();
-        goto onError;
-    }
-    return PyUnicode_AS_UNICODE(unicode);
-
-  onError:
-    return NULL;
+    return PyUnicode_AsUnicodeAndSize(unicode, NULL);
 }
 
+
 Py_ssize_t
 PyUnicode_GetSize(PyObject *unicode)
 {
@@ -2020,6 +2747,40 @@
     return -1;
 }
 
+Py_ssize_t
+PyUnicode_GetLength(PyObject *unicode)
+{
+    if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) {
+        PyErr_BadArgument();
+        return -1;
+    }
+
+    return PyUnicode_GET_LENGTH(unicode);
+}
+
+Py_UCS4
+PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
+{
+    if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) {
+        return PyErr_BadArgument();
+        return (Py_UCS4)-1;
+    }
+    return PyUnicode_READ_CHAR(unicode, index);
+}
+
+int
+PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch)
+{
+    if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) {
+        return PyErr_BadArgument();
+        return -1;
+    }
+
+    PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode),
+                    index, ch);
+    return 0;
+}
+
 const char *
 PyUnicode_GetDefaultEncoding(void)
 {
@@ -2075,7 +2836,7 @@
     Py_ssize_t insize;
     Py_ssize_t requiredsize;
     Py_ssize_t newpos;
-    Py_UNICODE *repptr;
+    const Py_UNICODE *repptr;
     PyObject *inputobj = NULL;
     Py_ssize_t repsize;
     int res = -1;
@@ -2281,7 +3042,7 @@
         return (PyObject *)unicode;
     }
 
-    p = unicode->str;
+    p = PyUnicode_AS_UNICODE(unicode);
     shiftOutStart = p;
     e = s + size;
 
@@ -2431,6 +3192,10 @@
 
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
+    if (PyUnicode_READY(unicode) == -1) {
+        Py_DECREF(unicode);
+        return NULL;
+    }
     return (PyObject *)unicode;
 
   onError:
@@ -2588,38 +3353,197 @@
 # error C 'long' size should be either 4 or 8!
 #endif
 
+/* Scans a UTF-8 string and returns the maximum character to be expected,
+   the size of the decoded unicode string and if any major errors were
+   encountered.
+
+   This function does check basic UTF-8 sanity, it does however NOT CHECK
+   if the string contains surrogates, and if all continuation bytes are
+   within the correct ranges, these checks are performed in
+   PyUnicode_DecodeUTF8Stateful.
+
+   If it sets has_errors to 1, it means the value of unicode_size and max_char
+   will be bogus and you should not rely on useful information in them.
+   */
+static Py_UCS4
+utf8_max_char_size_and_has_errors(const char *s, Py_ssize_t string_size,
+                                  Py_ssize_t *unicode_size, Py_ssize_t* consumed,
+                                  int *has_errors)
+{
+    Py_ssize_t n;
+    Py_ssize_t char_count = 0;
+    Py_UCS4 max_char = 127, new_max;
+    Py_UCS4 upper_bound;
+    const unsigned char *p = (const unsigned char *)s;
+    const unsigned char *end = p + string_size;
+    const unsigned char *aligned_end = (const unsigned char *) ((size_t) end & ~LONG_PTR_MASK);
+    int err = 0;
+
+    for (; p < end && !err; ++p, ++char_count) {
+        /* Only check value if it's not a ASCII char... */
+        if (*p < 0x80) {
+            /* Fast path, see below in PyUnicode_DecodeUTF8Stateful for
+               an explanation. */
+            if (!((size_t) p & LONG_PTR_MASK)) {
+                /* Help register allocation */
+                register const unsigned char *_p = p;
+                while (_p < aligned_end) {
+                    unsigned long value = *(unsigned long *) _p;
+                    if (value & ASCII_CHAR_MASK)
+                        break;
+                    _p += SIZEOF_LONG;
+                    char_count += SIZEOF_LONG;
+                }
+                p = _p;
+                if (p == end)
+                    break;
+            }
+        }
+        if (*p >= 0x80) {
+            n = utf8_code_length[*p];
+            new_max = max_char;
+            switch (n) {
+            /* invalid start byte */
+            case 0:
+                err = 1;
+                break;
+            case 2:
+                /* Code points between 0x00FF and 0x07FF inclusive.
+                   Approximate the upper bound of the code point,
+                   if this flips over 255 we can be sure it will be more
+                   than 255 and the string will need 2 bytes per code coint,
+                   if it stays under or equal to 255, we can be sure 1 byte
+                   is enough.
+                   ((*p & 0b00011111) << 6) | 0b00111111 */
+                upper_bound = ((*p & 0x1F) << 6) | 0x3F;
+                if (max_char < upper_bound)
+                    new_max = upper_bound;
+                /* Ensure we track at least that we left ASCII space. */
+                if (new_max < 128)
+                    new_max = 128;
+                break;
+            case 3:
+                /* Between 0x0FFF and 0xFFFF inclusive, so values are
+                   always > 255 and <= 65535 and will always need 2 bytes. */
+                if (max_char < 65535)
+                    new_max = 65535;
+                break;
+            case 4:
+                /* Code point will be above 0xFFFF for sure in this case. */
+                new_max = 65537;
+                break;
+            /* Internal error, this should be caught by the first if */
+            case 1:
+            default:
+                assert(0 && "Impossible case in utf8_max_char_and_size");
+                err = 1;
+            }
+            /* Instead of number of overall bytes for this code point,
+               n containts the number of following bytes: */
+            --n;
+            /* Check if the follow up chars are all valid continuation bytes */
+            if (n >= 1) {
+                const unsigned char *cont;
+                if ((p + n) >= end) {
+                    if (consumed == 0)
+                        /* incomplete data, non-incremental decoding */
+                        err = 1;
+                    break;
+                }
+                for (cont = p + 1; cont < (p + n); ++cont) {
+                    if ((*cont & 0xc0) != 0x80) {
+                        err = 1;
+                        break;
+                    }
+                }
+                p += n;
+            }
+            else
+                err = 1;
+            max_char = new_max;
+        }
+    }
+
+    if (unicode_size)
+        *unicode_size = char_count;
+    if (has_errors)
+        *has_errors = err;
+    return max_char;
+}
+
+/* Similar to PyUnicode_WRITE but can also write into wstr field
+   of the legacy unicode representation */
+#define WRITE_FLEXIBLE_OR_WSTR(kind, buf, index, value) \
+    do { \
+        const int k_ = (kind); \
+        if (k_ == PyUnicode_WCHAR_KIND) \
+            ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \
+        else if (k_ == PyUnicode_1BYTE_KIND) \
+            ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \
+        else if (k_ == PyUnicode_2BYTE_KIND) \
+            ((Py_UCS2 *)(buf))[(index)] = (Py_UCS2)(value); \
+        else \
+            ((Py_UCS4 *)(buf))[(index)] = (Py_UCS4)(value); \
+    } while (0)
+
 PyObject *
 PyUnicode_DecodeUTF8Stateful(const char *s,
-			     Py_ssize_t size,
-			     const char *errors,
-			     Py_ssize_t *consumed)
+                             Py_ssize_t size,
+                             const char *errors,
+                             Py_ssize_t *consumed)
 {
     const char *starts = s;
     int n;
     int k;
     Py_ssize_t startinpos;
     Py_ssize_t endinpos;
-    Py_ssize_t outpos;
     const char *e, *aligned_end;
     PyUnicodeObject *unicode;
-    Py_UNICODE *p;
     const char *errmsg = "";
     PyObject *errorHandler = NULL;
     PyObject *exc = NULL;
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t unicode_size;
+    Py_ssize_t i;
+    int kind;
+    void *data;
+    int has_errors;
+    Py_UNICODE *error_outptr;
+#if SIZEOF_WCHAR_T == 2
+    Py_ssize_t wchar_offset = 0;
+#endif
 
-    /* Note: size will always be longer than the resulting Unicode
-       character count */
-    unicode = _PyUnicode_New(size);
-    if (!unicode)
-        return NULL;
     if (size == 0) {
         if (consumed)
             *consumed = 0;
-        return (PyObject *)unicode;
+        return (PyObject *)PyUnicode_New(0, 0);
     }
-
+    maxchar = utf8_max_char_size_and_has_errors(s, size, &unicode_size,
+                                                consumed, &has_errors);
+    if (has_errors) {
+        unicode = _PyUnicode_New(size);
+        if (!unicode)
+            return NULL;
+        kind = PyUnicode_WCHAR_KIND;
+        data = PyUnicode_AS_UNICODE(unicode);
+        assert(data != NULL);
+    }
+    else {
+        unicode = (PyUnicodeObject *)PyUnicode_New(unicode_size, maxchar);
+        if (!unicode)
+            return NULL;
+        /* When the string is ASCII only, just use memcpy and return.
+           unicode_size may be != size if there is an incomplete UTF-8
+           sequence at the end of the ASCII block.  */
+        if (maxchar < 128 && size == unicode_size) {
+            Py_MEMCPY(PyUnicode_1BYTE_DATA(unicode), s, unicode_size);
+            return (PyObject *)unicode;
+        }
+        kind = PyUnicode_KIND(unicode);
+        data = PyUnicode_DATA(unicode);
+    }
     /* Unpack UTF-8 encoded data */
-    p = unicode->str;
+    i = 0;
     e = s + size;
     aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK);
 
@@ -2637,29 +3561,29 @@
             if (!((size_t) s & LONG_PTR_MASK)) {
                 /* Help register allocation */
                 register const char *_s = s;
-                register Py_UNICODE *_p = p;
+                register Py_ssize_t _i = i;
                 while (_s < aligned_end) {
                     /* Read a whole long at a time (either 4 or 8 bytes),
                        and do a fast unrolled copy if it only contains ASCII
                        characters. */
-                    unsigned long data = *(unsigned long *) _s;
-                    if (data & ASCII_CHAR_MASK)
+                    unsigned long value = *(unsigned long *) _s;
+                    if (value & ASCII_CHAR_MASK)
                         break;
-                    _p[0] = (unsigned char) _s[0];
-                    _p[1] = (unsigned char) _s[1];
-                    _p[2] = (unsigned char) _s[2];
-                    _p[3] = (unsigned char) _s[3];
+                    WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+0, _s[0]);
+                    WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+1, _s[1]);
+                    WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+2, _s[2]);
+                    WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+3, _s[3]);
 #if (SIZEOF_LONG == 8)
-                    _p[4] = (unsigned char) _s[4];
-                    _p[5] = (unsigned char) _s[5];
-                    _p[6] = (unsigned char) _s[6];
-                    _p[7] = (unsigned char) _s[7];
+                    WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+4, _s[4]);
+                    WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+5, _s[5]);
+                    WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+6, _s[6]);
+                    WRITE_FLEXIBLE_OR_WSTR(kind, data, _i+7, _s[7]);
 #endif
                     _s += SIZEOF_LONG;
-                    _p += SIZEOF_LONG;
+                    _i += SIZEOF_LONG;
                 }
                 s = _s;
-                p = _p;
+                i = _i;
                 if (s == e)
                     break;
                 ch = (unsigned char)*s;
@@ -2667,7 +3591,7 @@
         }
 
         if (ch < 0x80) {
-            *p++ = (Py_UNICODE)ch;
+            WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch);
             s++;
             continue;
         }
@@ -2710,7 +3634,7 @@
             }
             ch = ((s[0] & 0x1f) << 6) + (s[1] & 0x3f);
             assert ((ch > 0x007F) && (ch <= 0x07FF));
-            *p++ = (Py_UNICODE)ch;
+            WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch);
             break;
 
         case 3:
@@ -2739,7 +3663,7 @@
             }
             ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f);
             assert ((ch > 0x07FF) && (ch <= 0xFFFF));
-            *p++ = (Py_UNICODE)ch;
+            WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch);
             break;
 
         case 4:
@@ -2764,19 +3688,27 @@
                  ((s[2] & 0x3f) << 6) + (s[3] & 0x3f);
             assert ((ch > 0xFFFF) && (ch <= 0x10ffff));
 
-#ifdef Py_UNICODE_WIDE
-            *p++ = (Py_UNICODE)ch;
-#else
-            /*  compute and append the two surrogates: */
+            /* If the string is flexible or we have native UCS-4, write
+               directly.. */
+            if (sizeof(Py_UNICODE) > 2 || kind != PyUnicode_WCHAR_KIND)
+                WRITE_FLEXIBLE_OR_WSTR(kind, data, i++, ch);
 
-            /*  translate from 10000..10FFFF to 0..FFFF */
-            ch -= 0x10000;
+            else {
+                /* compute and append the two surrogates: */
 
-            /*  high surrogate = top 10 bits added to D800 */
-            *p++ = (Py_UNICODE)(0xD800 + (ch >> 10));
+                /* translate from 10000..10FFFF to 0..FFFF */
+                ch -= 0x10000;
 
-            /*  low surrogate = bottom 10 bits added to DC00 */
-            *p++ = (Py_UNICODE)(0xDC00 + (ch & 0x03FF));
+                /* high surrogate = top 10 bits added to D800 */
+                WRITE_FLEXIBLE_OR_WSTR(kind, data, i++,
+                                       (Py_UNICODE)(0xD800 + (ch >> 10)));
+
+                /* low surrogate = bottom 10 bits added to DC00 */
+                WRITE_FLEXIBLE_OR_WSTR(kind, data, i++,
+                                       (Py_UNICODE)(0xDC00 + (ch & 0x03FF)));
+            }
+#if SIZEOF_WCHAR_T == 2
+            wchar_offset++;
 #endif
             break;
         }
@@ -2784,24 +3716,57 @@
         continue;
 
       utf8Error:
-        outpos = p-PyUnicode_AS_UNICODE(unicode);
+        /* If this is not yet a resizable string, make it one.. */
+        if (kind != PyUnicode_WCHAR_KIND) {
+            const Py_UNICODE *u;
+            PyUnicodeObject *new_unicode = _PyUnicode_New(size);
+            if (!new_unicode)
+                goto onError;
+            u = PyUnicode_AsUnicode((PyObject *)unicode);
+            if (!u)
+                goto onError;
+#if SIZEOF_WCHAR_T == 2
+            i += wchar_offset;
+#endif
+            Py_UNICODE_COPY(PyUnicode_AS_UNICODE(new_unicode), u, i);
+            Py_DECREF(unicode);
+            unicode = new_unicode;
+            kind = 0;
+            data = PyUnicode_AS_UNICODE(new_unicode);
+            assert(data != NULL);
+        }
+        error_outptr = PyUnicode_AS_UNICODE(unicode) + i;
         if (unicode_decode_call_errorhandler(
                 errors, &errorHandler,
                 "utf8", errmsg,
                 &starts, &e, &startinpos, &endinpos, &exc, &s,
-                &unicode, &outpos, &p))
+                &unicode, &i, &error_outptr))
             goto onError;
+        /* Update data because unicode_decode_call_errorhandler might have
+           re-created or resized the unicode object. */
+        data = PyUnicode_AS_UNICODE(unicode);
         aligned_end = (const char *) ((size_t) e & ~LONG_PTR_MASK);
     }
+    /* Ensure the unicode_size calculation above was correct: */
+    assert(kind == PyUnicode_WCHAR_KIND || i == unicode_size);
+
     if (consumed)
         *consumed = s-starts;
 
-    /* Adjust length */
-    if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
-        goto onError;
+    /* Adjust length and ready string when it contained errors and
+       is of the old resizable kind. */
+    if (kind == PyUnicode_WCHAR_KIND) {
+        if (_PyUnicode_Resize(&unicode, i) < 0 ||
+            PyUnicode_READY(unicode) == -1)
+            goto onError;
+    }
 
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
+    if (PyUnicode_READY(unicode) == -1) {
+        Py_DECREF(unicode);
+        return NULL;
+    }
     return (PyObject *)unicode;
 
   onError:
@@ -2811,7 +3776,7 @@
     return NULL;
 }
 
-#undef ASCII_CHAR_MASK
+#undef WRITE_FLEXIBLE_OR_WSTR
 
 #ifdef __APPLE__
 
@@ -2882,7 +3847,7 @@
             }
             ch = ((s[0] & 0x0f) << 12) + ((s[1] & 0x3f) << 6) + (s[2] & 0x3f);
             assert ((ch > 0x07FF) && (ch <= 0xFFFF));
-            *p++ = (Py_UNICODE)ch;
+            *p++ = (wchar_t)ch;
             break;
 
         case 4:
@@ -2928,15 +3893,15 @@
 
 #endif /* __APPLE__ */
 
-/* Allocation strategy:  if the string is short, convert into a stack buffer
+/* Primary internal function which creates utf8 encoded bytes objects.
+
+   Allocation strategy:  if the string is short, convert into a stack buffer
    and allocate exactly as much space needed at the end.  Else allocate the
    maximum possible needed (4 result bytes per Unicode character), and return
    the excess memory at the end.
 */
 PyObject *
-PyUnicode_EncodeUTF8(const Py_UNICODE *s,
-                     Py_ssize_t size,
-                     const char *errors)
+_PyUnicode_AsUTF8String(PyObject *obj, const char *errors)
 {
 #define MAX_SHORT_UNICHARS 300  /* largest size we'll do on the stack */
 
@@ -2948,8 +3913,30 @@
     char stackbuf[MAX_SHORT_UNICHARS * 4];
     PyObject *errorHandler = NULL;
     PyObject *exc = NULL;
+    int kind;
+    void *data;
+    Py_ssize_t size;
+    PyUnicodeObject *unicode = (PyUnicodeObject *)obj;
+#if SIZEOF_WCHAR_T == 2
+    Py_ssize_t wchar_offset = 0;
+#endif
 
-    assert(s != NULL);
+    if (!PyUnicode_Check(unicode)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
+
+    if (PyUnicode_READY(unicode) == -1)
+        return NULL;
+
+    if (_PyUnicode_UTF8(unicode))
+        return PyBytes_FromStringAndSize(_PyUnicode_UTF8(unicode),
+                                         _PyUnicode_UTF8_LENGTH(unicode));
+
+    kind = PyUnicode_KIND(unicode);
+    data = PyUnicode_DATA(unicode);
+    size = PyUnicode_GET_LENGTH(unicode);
+
     assert(size >= 0);
 
     if (size <= MAX_SHORT_UNICHARS) {
@@ -2973,7 +3960,7 @@
     }
 
     for (i = 0; i < size;) {
-        Py_UCS4 ch = s[i++];
+        Py_UCS4 ch = PyUnicode_READ(kind, data, i++);
 
         if (ch < 0x80)
             /* Encode ASCII */
@@ -2984,83 +3971,72 @@
             *p++ = (char)(0xc0 | (ch >> 6));
             *p++ = (char)(0x80 | (ch & 0x3f));
         } else if (0xD800 <= ch && ch <= 0xDFFF) {
-#ifndef Py_UNICODE_WIDE
-            /* Special case: check for high and low surrogate */
-            if (ch <= 0xDBFF && i != size && 0xDC00 <= s[i] && s[i] <= 0xDFFF) {
-                Py_UCS4 ch2 = s[i];
-                /* Combine the two surrogates to form a UCS4 value */
-                ch = ((ch - 0xD800) << 10 | (ch2 - 0xDC00)) + 0x10000;
-                i++;
-
-                /* Encode UCS4 Unicode ordinals */
-                *p++ = (char)(0xf0 | (ch >> 18));
-                *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
-                *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
-                *p++ = (char)(0x80 | (ch & 0x3f));
-            } else {
+            Py_ssize_t newpos;
+            PyObject *rep;
+            Py_ssize_t repsize, k, startpos;
+            startpos = i-1;
+#if SIZEOF_WCHAR_T == 2
+            startpos += wchar_offset;
 #endif
-                Py_ssize_t newpos;
-                PyObject *rep;
-                Py_ssize_t repsize, k;
-                rep = unicode_encode_call_errorhandler
-                    (errors, &errorHandler, "utf-8", "surrogates not allowed",
-                     s, size, &exc, i-1, i, &newpos);
-                if (!rep)
-                    goto error;
+            rep = unicode_encode_call_errorhandler(
+                  errors, &errorHandler, "utf-8", "surrogates not allowed",
+                  PyUnicode_AS_UNICODE(unicode), PyUnicode_GET_SIZE(unicode),
+                  &exc, startpos, startpos+1, &newpos);
+            if (!rep)
+                goto error;
 
-                if (PyBytes_Check(rep))
-                    repsize = PyBytes_GET_SIZE(rep);
+            if (PyBytes_Check(rep))
+                repsize = PyBytes_GET_SIZE(rep);
+            else
+                repsize = PyUnicode_GET_SIZE(rep);
+
+            if (repsize > 4) {
+                Py_ssize_t offset;
+
+                if (result == NULL)
+                    offset = p - stackbuf;
                 else
-                    repsize = PyUnicode_GET_SIZE(rep);
+                    offset = p - PyBytes_AS_STRING(result);
 
-                if (repsize > 4) {
-                    Py_ssize_t offset;
-
+                if (nallocated > PY_SSIZE_T_MAX - repsize + 4) {
+                    /* integer overflow */
+                    PyErr_NoMemory();
+                    goto error;
+                }
+                nallocated += repsize - 4;
+                if (result != NULL) {
+                    if (_PyBytes_Resize(&result, nallocated) < 0)
+                        goto error;
+                } else {
+                    result = PyBytes_FromStringAndSize(NULL, nallocated);
                     if (result == NULL)
-                        offset = p - stackbuf;
-                    else
-                        offset = p - PyBytes_AS_STRING(result);
+                        goto error;
+                    Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset);
+                }
+                p = PyBytes_AS_STRING(result) + offset;
+            }
 
-                    if (nallocated > PY_SSIZE_T_MAX - repsize + 4) {
-                        /* integer overflow */
-                        PyErr_NoMemory();
+            if (PyBytes_Check(rep)) {
+                char *prep = PyBytes_AS_STRING(rep);
+                for(k = repsize; k > 0; k--)
+                    *p++ = *prep++;
+            } else /* rep is unicode */ {
+                const Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep);
+                Py_UNICODE c;
+
+                for(k=0; k<repsize; k++) {
+                    c = prep[k];
+                    if (0x80 <= c) {
+                        raise_encode_exception(&exc, "utf-8",
+                                               PyUnicode_AS_UNICODE(unicode),
+                                               size, i-1, i,
+                                               "surrogates not allowed");
                         goto error;
                     }
-                    nallocated += repsize - 4;
-                    if (result != NULL) {
-                        if (_PyBytes_Resize(&result, nallocated) < 0)
-                            goto error;
-                    } else {
-                        result = PyBytes_FromStringAndSize(NULL, nallocated);
-                        if (result == NULL)
-                            goto error;
-                        Py_MEMCPY(PyBytes_AS_STRING(result), stackbuf, offset);
-                    }
-                    p = PyBytes_AS_STRING(result) + offset;
+                    *p++ = (char)prep[k];
                 }
-
-                if (PyBytes_Check(rep)) {
-                    char *prep = PyBytes_AS_STRING(rep);
-                    for(k = repsize; k > 0; k--)
-                        *p++ = *prep++;
-                } else /* rep is unicode */ {
-                    Py_UNICODE *prep = PyUnicode_AS_UNICODE(rep);
-                    Py_UNICODE c;
-
-                    for(k=0; k<repsize; k++) {
-                        c = prep[k];
-                        if (0x80 <= c) {
-                            raise_encode_exception(&exc, "utf-8", s, size,
-                                                   i-1, i, "surrogates not allowed");
-                            goto error;
-                        }
-                        *p++ = (char)prep[k];
-                    }
-                }
-                Py_DECREF(rep);
-#ifndef Py_UNICODE_WIDE
             }
-#endif
+            Py_DECREF(rep);
         } else if (ch < 0x10000) {
             *p++ = (char)(0xe0 | (ch >> 12));
             *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
@@ -3071,6 +4047,9 @@
             *p++ = (char)(0x80 | ((ch >> 12) & 0x3f));
             *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
             *p++ = (char)(0x80 | (ch & 0x3f));
+#if SIZEOF_WCHAR_T == 2
+            wchar_offset++;
+#endif
         }
     }
 
@@ -3086,6 +4065,7 @@
         assert(nneeded <= nallocated);
         _PyBytes_Resize(&result, nneeded);
     }
+
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
     return result;
@@ -3099,18 +4079,24 @@
 }
 
 PyObject *
+PyUnicode_EncodeUTF8(const Py_UNICODE *s,
+                     Py_ssize_t size,
+                     const char *errors)
+{
+    PyObject *v, *unicode;
+
+    unicode = PyUnicode_FromUnicode(s, size);
+    if (unicode == NULL)
+        return NULL;
+    v = _PyUnicode_AsUTF8String(unicode, errors);
+    Py_DECREF(unicode);
+    return v;
+}
+
+PyObject *
 PyUnicode_AsUTF8String(PyObject *unicode)
 {
-    PyObject *utf8;
-    if (!PyUnicode_Check(unicode)) {
-        PyErr_BadArgument();
-        return NULL;
-    }
-    utf8 = _PyUnicode_AsDefaultEncodedString(unicode);
-    if (utf8 == NULL)
-        return NULL;
-    Py_INCREF(utf8);
-    return utf8;
+    return _PyUnicode_AsUTF8String(unicode, NULL);
 }
 
 /* --- UTF-32 Codec ------------------------------------------------------- */
@@ -3222,7 +4208,7 @@
         return (PyObject *)unicode;
 
     /* Unpack UTF-32 encoded data */
-    p = unicode->str;
+    p = PyUnicode_AS_UNICODE(unicode);
 
     while (q < e) {
         Py_UCS4 ch;
@@ -3275,11 +4261,15 @@
         *consumed = (const char *)q-starts;
 
     /* Adjust length */
-    if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
+    if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0)
         goto onError;
 
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
+    if (PyUnicode_READY(unicode) == -1) {
+        Py_DECREF(unicode);
+        return NULL;
+    }
     return (PyObject *)unicode;
 
   onError:
@@ -3452,7 +4442,7 @@
         return (PyObject *)unicode;
 
     /* Unpack UTF-16 encoded data */
-    p = unicode->str;
+    p = PyUnicode_AS_UNICODE(unicode);
     q = (unsigned char *)s;
     e = q + size - 1;
 
@@ -3669,11 +4659,15 @@
         *consumed = (const char *)q-starts;
 
     /* Adjust length */
-    if (_PyUnicode_Resize(&unicode, p - unicode->str) < 0)
+    if (_PyUnicode_Resize(&unicode, p - PyUnicode_AS_UNICODE(unicode)) < 0)
         goto onError;
 
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
+    if (PyUnicode_READY(unicode) == -1) {
+        Py_DECREF(unicode);
+        return NULL;
+    }
     return (PyObject *)unicode;
 
   onError:
@@ -3782,6 +4776,76 @@
 
 /* --- Unicode Escape Codec ----------------------------------------------- */
 
+/* Helper function for PyUnicode_DecodeUnicodeEscape, determines
+   if all the escapes in the string make it still a valid ASCII string.
+   Returns -1 if any escapes were found which cause the string to
+   pop out of ASCII range.  Otherwise returns the length of the
+   required buffer to hold the string.
+   */
+Py_ssize_t
+length_of_escaped_ascii_string(const char *s, Py_ssize_t size)
+{
+    const unsigned char *p = (const unsigned char *)s;
+    const unsigned char *end = p + size;
+    Py_ssize_t length = 0;
+
+    if (size < 0)
+        return -1;
+
+    for (; p < end; ++p) {
+        if (*p > 127) {
+            /* Non-ASCII */
+            return -1;
+        }
+        else if (*p != '\\') {
+            /* Normal character */
+            ++length;
+        }
+        else {
+            /* Backslash-escape, check next char */
+            ++p;
+            /* Escape sequence reaches till end of string or
+               non-ASCII follow-up. */
+            if (p >= end || *p > 127)
+                return -1;
+            switch (*p) {
+            case '\n':
+                /* backslash + \n result in zero characters */
+                break;
+            case '\\': case '\'': case '\"':
+            case 'b': case 'f': case 't':
+            case 'n': case 'r': case 'v': case 'a':
+                ++length;
+                break;
+            case '0': case '1': case '2': case '3':
+            case '4': case '5': case '6': case '7':
+            case 'x': case 'u': case 'U': case 'N':
+                /* these do not guarantee ASCII characters */
+                return -1;
+            default:
+                /* count the backslash + the other character */
+                length += 2;
+            }
+        }
+    }
+    return length;
+}
+
+/* Similar to PyUnicode_WRITE but either write into wstr field
+   or treat string as ASCII. */
+#define WRITE_ASCII_OR_WSTR(kind, buf, index, value) \
+    do { \
+        if ((kind) != PyUnicode_WCHAR_KIND) \
+            ((unsigned char *)(buf))[(index)] = (unsigned char)(value); \
+        else \
+            ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value); \
+    } while (0)
+
+#define WRITE_WSTR(buf, index, value) \
+    assert(kind == PyUnicode_WCHAR_KIND), \
+    ((Py_UNICODE *)(buf))[(index)] = (Py_UNICODE)(value)
+
+
 static _PyUnicode_Name_CAPI *ucnhash_CAPI = NULL;
 
 PyObject *
@@ -3792,8 +4856,7 @@
     const char *starts = s;
     Py_ssize_t startinpos;
     Py_ssize_t endinpos;
-    Py_ssize_t outpos;
-    int i;
+    int j;
     PyUnicodeObject *v;
     Py_UNICODE *p;
     const char *end;
@@ -3801,19 +4864,42 @@
     Py_UCS4 chr = 0xffffffff; /* in case 'getcode' messes up */
     PyObject *errorHandler = NULL;
     PyObject *exc = NULL;
+    Py_ssize_t ascii_length;
+    Py_ssize_t i;
+    int kind;
+    void *data;
 
-    /* Escaped strings will always be longer than the resulting
-       Unicode string, so we start with size here and then reduce the
-       length after conversion to the true value.
-       (but if the error callback returns a long replacement string
-       we'll have to allocate more space) */
-    v = _PyUnicode_New(size);
-    if (v == NULL)
-        goto onError;
+    ascii_length = length_of_escaped_ascii_string(s, size);
+
+    /* After length_of_escaped_ascii_string() there are two alternatives,
+       either the string is pure ASCII with named escapes like \n, etc.
+       and we determined it's exact size (common case)
+       or it contains \x, \u, ... escape sequences.  then we create a
+       legacy wchar string and resize it at the end of this function. */
+    if (ascii_length >= 0) {
+        v = (PyUnicodeObject *)PyUnicode_New(ascii_length, 127);
+        if (!v)
+            goto onError;
+        assert(PyUnicode_KIND(v) == PyUnicode_1BYTE_KIND);
+        kind = PyUnicode_1BYTE_KIND;
+        data = PyUnicode_DATA(v);
+    }
+    else {
+        /* Escaped strings will always be longer than the resulting
+           Unicode string, so we start with size here and then reduce the
+           length after conversion to the true value.
+           (but if the error callback returns a long replacement string
+           we'll have to allocate more space) */
+        v = _PyUnicode_New(size);
+        if (!v)
+            goto onError;
+        kind = PyUnicode_WCHAR_KIND;
+        data = PyUnicode_AS_UNICODE(v);
+    }
+
     if (size == 0)
         return (PyObject *)v;
-
-    p = PyUnicode_AS_UNICODE(v);
+    i = 0;
     end = s + size;
 
     while (s < end) {
@@ -3821,9 +4907,18 @@
         Py_UNICODE x;
         int digits;
 
+        if (kind == PyUnicode_WCHAR_KIND) {
+            assert(i < _PyUnicode_WSTR_LENGTH(v));
+        }
+        else {
+            /* The only case in which i == ascii_length is a backslash
+               followed by a newline. */
+            assert(i <= ascii_length);
+        }
+
         /* Non-escape characters are interpreted as Unicode ordinals */
         if (*s != '\\') {
-            *p++ = (unsigned char) *s++;
+            WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char) *s++);
             continue;
         }
 
@@ -3833,20 +4928,33 @@
         c = *s++;
         if (s > end)
             c = '\0'; /* Invalid after \ */
+
+        if (kind == PyUnicode_WCHAR_KIND) {
+            assert(i < _PyUnicode_WSTR_LENGTH(v));
+        }
+        else {
+            /* The only case in which i == ascii_length is a backslash
+               followed by a newline. */
+            assert(i < ascii_length || (i == ascii_length && c == '\n'));
+        }
+
         switch (c) {
 
             /* \x escapes */
         case '\n': break;
-        case '\\': *p++ = '\\'; break;
-        case '\'': *p++ = '\''; break;
-        case '\"': *p++ = '\"'; break;
-        case 'b': *p++ = '\b'; break;
-        case 'f': *p++ = '\014'; break; /* FF */
-        case 't': *p++ = '\t'; break;
-        case 'n': *p++ = '\n'; break;
-        case 'r': *p++ = '\r'; break;
-        case 'v': *p++ = '\013'; break; /* VT */
-        case 'a': *p++ = '\007'; break; /* BEL, not classic C */
+        case '\\': WRITE_ASCII_OR_WSTR(kind, data, i++, '\\'); break;
+        case '\'': WRITE_ASCII_OR_WSTR(kind, data, i++, '\''); break;
+        case '\"': WRITE_ASCII_OR_WSTR(kind, data, i++, '\"'); break;
+        case 'b': WRITE_ASCII_OR_WSTR(kind, data, i++, '\b'); break;
+        /* FF */
+        case 'f': WRITE_ASCII_OR_WSTR(kind, data, i++, '\014'); break;
+        case 't': WRITE_ASCII_OR_WSTR(kind, data, i++, '\t'); break;
+        case 'n': WRITE_ASCII_OR_WSTR(kind, data, i++, '\n'); break;
+        case 'r': WRITE_ASCII_OR_WSTR(kind, data, i++, '\r'); break;
+        /* VT */
+        case 'v': WRITE_ASCII_OR_WSTR(kind, data, i++, '\013'); break;
+        /* BEL, not classic C */
+        case 'a': WRITE_ASCII_OR_WSTR(kind, data, i++, '\007'); break;
 
             /* \OOO (octal) escapes */
         case '0': case '1': case '2': case '3':
@@ -3857,7 +4965,7 @@
                 if (s < end && '0' <= *s && *s <= '7')
                     x = (x<<3) + *s++ - '0';
             }
-            *p++ = x;
+            WRITE_WSTR(data, i++, x);
             break;
 
             /* hex escapes */
@@ -3879,27 +4987,30 @@
             message = "truncated \\UXXXXXXXX escape";
         hexescape:
             chr = 0;
-            outpos = p-PyUnicode_AS_UNICODE(v);
+            p = PyUnicode_AS_UNICODE(v) + i;
             if (s+digits>end) {
                 endinpos = size;
                 if (unicode_decode_call_errorhandler(
                         errors, &errorHandler,
                         "unicodeescape", "end of string in escape sequence",
                         &starts, &end, &startinpos, &endinpos, &exc, &s,
-                        &v, &outpos, &p))
+                        &v, &i, &p))
                     goto onError;
+                data = PyUnicode_AS_UNICODE(v);
                 goto nextByte;
             }
-            for (i = 0; i < digits; ++i) {
-                c = (unsigned char) s[i];
+            for (j = 0; j < digits; ++j) {
+                c = (unsigned char) s[j];
                 if (!Py_ISXDIGIT(c)) {
-                    endinpos = (s+i+1)-starts;
+                    endinpos = (s+j+1)-starts;
+                    p = PyUnicode_AS_UNICODE(v) + i;
                     if (unicode_decode_call_errorhandler(
                             errors, &errorHandler,
                             "unicodeescape", message,
                             &starts, &end, &startinpos, &endinpos, &exc, &s,
-                            &v, &outpos, &p))
+                            &v, &i, &p))
                         goto onError;
+                    data = PyUnicode_AS_UNICODE(v);
                     goto nextByte;
                 }
                 chr = (chr<<4) & ~0xF;
@@ -3910,7 +5021,7 @@
                 else
                     chr += 10 + c - 'A';
             }
-            s += i;
+            s += j;
             if (chr == 0xffffffff && PyErr_Occurred())
                 /* _decoding_error will have already written into the
                    target buffer. */
@@ -3919,26 +5030,27 @@
             /* when we get here, chr is a 32-bit unicode character */
             if (chr <= 0xffff)
                 /* UCS-2 character */
-                *p++ = (Py_UNICODE) chr;
+                WRITE_WSTR(data, i++, chr);
             else if (chr <= 0x10ffff) {
                 /* UCS-4 character. Either store directly, or as
                    surrogate pair. */
 #ifdef Py_UNICODE_WIDE
-                *p++ = chr;
+                WRITE_WSTR(data, i++, chr);
 #else
                 chr -= 0x10000L;
-                *p++ = 0xD800 + (Py_UNICODE) (chr >> 10);
-                *p++ = 0xDC00 + (Py_UNICODE) (chr & 0x03FF);
+                WRITE_WSTR(data, i++, 0xD800 + (Py_UNICODE) (chr >> 10));
+                WRITE_WSTR(data, i++, 0xDC00 + (Py_UNICODE) (chr & 0x03FF));
 #endif
             } else {
                 endinpos = s-starts;
-                outpos = p-PyUnicode_AS_UNICODE(v);
+                p = PyUnicode_AS_UNICODE(v) + i;
                 if (unicode_decode_call_errorhandler(
                         errors, &errorHandler,
                         "unicodeescape", "illegal Unicode character",
                         &starts, &end, &startinpos, &endinpos, &exc, &s,
-                        &v, &outpos, &p))
+                        &v, &i, &p))
                     goto onError;
+                data = PyUnicode_AS_UNICODE(v);
             }
             break;
 
@@ -3947,7 +5059,8 @@
             message = "malformed \\N character escape";
             if (ucnhash_CAPI == NULL) {
                 /* load the unicode data module */
-                ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(PyUnicodeData_CAPSULE_NAME, 1);
+                ucnhash_CAPI = (_PyUnicode_Name_CAPI *)PyCapsule_Import(
+                                                PyUnicodeData_CAPSULE_NAME, 1);
                 if (ucnhash_CAPI == NULL)
                     goto ucnhashError;
             }
@@ -3960,43 +5073,51 @@
                     /* found a name.  look it up in the unicode database */
                     message = "unknown Unicode character name";
                     s++;
-                    if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1), &chr))
+                    if (ucnhash_CAPI->getcode(NULL, start, (int)(s-start-1),
+                                              &chr))
                         goto store;
                 }
             }
             endinpos = s-starts;
-            outpos = p-PyUnicode_AS_UNICODE(v);
+            p = PyUnicode_AS_UNICODE(v) + i;
             if (unicode_decode_call_errorhandler(
                     errors, &errorHandler,
                     "unicodeescape", message,
                     &starts, &end, &startinpos, &endinpos, &exc, &s,
-                    &v, &outpos, &p))
+                    &v, &i, &p))
                 goto onError;
+            data = PyUnicode_AS_UNICODE(v);
             break;
 
         default:
             if (s > end) {
+                assert(kind == PyUnicode_WCHAR_KIND);
                 message = "\\ at end of string";
                 s--;
                 endinpos = s-starts;
-                outpos = p-PyUnicode_AS_UNICODE(v);
+                p = PyUnicode_AS_UNICODE(v) + i;
                 if (unicode_decode_call_errorhandler(
                         errors, &errorHandler,
                         "unicodeescape", message,
                         &starts, &end, &startinpos, &endinpos, &exc, &s,
-                        &v, &outpos, &p))
+                        &v, &i, &p))
                     goto onError;
+                data = PyUnicode_AS_UNICODE(v);
             }
             else {
-                *p++ = '\\';
-                *p++ = (unsigned char)s[-1];
+                WRITE_ASCII_OR_WSTR(kind, data, i++, '\\');
+                WRITE_ASCII_OR_WSTR(kind, data, i++, (unsigned char)s[-1]);
             }
             break;
         }
       nextByte:
         ;
     }
-    if (_PyUnicode_Resize(&v, p - PyUnicode_AS_UNICODE(v)) < 0)
+    /* Ensure the length prediction worked in case of ASCII strings */
+    assert(kind == PyUnicode_WCHAR_KIND || i == ascii_length);
+
+    if (kind == PyUnicode_WCHAR_KIND && (_PyUnicode_Resize(&v, i) < 0 ||
+                                         PyUnicode_READY(v) == -1))
         goto onError;
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
@@ -4019,6 +5140,9 @@
     return NULL;
 }
 
+#undef WRITE_ASCII_OR_WSTR
+#undef WRITE_WSTR
+
 /* Return a Unicode-Escape string version of the Unicode object.
 
    If quotes is true, the string is enclosed in u"" or u'' quotes as
@@ -4026,21 +5150,6 @@
 
 */
 
-Py_LOCAL_INLINE(const Py_UNICODE *) findchar(const Py_UNICODE *s,
-                                             Py_ssize_t size,
-                                             Py_UNICODE ch)
-{
-    /* like wcschr, but doesn't stop at NULL characters */
-
-    while (size-- > 0) {
-        if (*s == ch)
-            return s;
-        s++;
-    }
-
-    return NULL;
-}
-
 static const char *hexdigits = "0123456789abcdef";
 
 PyObject *
@@ -4309,6 +5418,10 @@
         goto onError;
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
+    if (PyUnicode_READY(v) == -1) {
+        Py_DECREF(v);
+        return NULL;
+    }
     return (PyObject *)v;
 
   onError:
@@ -4447,7 +5560,9 @@
     v = _PyUnicode_New((size+Py_UNICODE_SIZE-1)/ Py_UNICODE_SIZE);
     if (v == NULL)
         goto onError;
-    if (PyUnicode_GetSize((PyObject *)v) == 0)
+    /* Intentionally PyUnicode_GET_SIZE instead of PyUnicode_GET_LENGTH
+       as string was created with the old API. */
+    if (PyUnicode_GET_SIZE(v) == 0)
         return (PyObject *)v;
     p = PyUnicode_AS_UNICODE(v);
     end = s + size;
@@ -4491,6 +5606,10 @@
         goto onError;
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
+    if (PyUnicode_READY(v) == -1) {
+        Py_DECREF(v);
+        return NULL;
+    }
     return (PyObject *)v;
 
   onError:
@@ -4507,41 +5626,8 @@
 		       Py_ssize_t size,
 		       const char *errors)
 {
-    PyUnicodeObject *v;
-    Py_UNICODE *p;
-    const char *e, *unrolled_end;
-
     /* Latin-1 is equivalent to the first 256 ordinals in Unicode. */
-    if (size == 1) {
-        Py_UNICODE r = *(unsigned char*)s;
-        return PyUnicode_FromUnicode(&r, 1);
-    }
-
-    v = _PyUnicode_New(size);
-    if (v == NULL)
-        goto onError;
-    if (size == 0)
-        return (PyObject *)v;
-    p = PyUnicode_AS_UNICODE(v);
-    e = s + size;
-    /* Unrolling the copy makes it much faster by reducing the looping
-       overhead. This is similar to what many memcpy() implementations do. */
-    unrolled_end = e - 4;
-    while (s < unrolled_end) {
-        p[0] = (unsigned char) s[0];
-        p[1] = (unsigned char) s[1];
-        p[2] = (unsigned char) s[2];
-        p[3] = (unsigned char) s[3];
-        s += 4;
-        p += 4;
-    }
-    while (s < e)
-        *p++ = (unsigned char) *s++;
-    return (PyObject *)v;
-
-  onError:
-    Py_XDECREF(v);
-    return NULL;
+    return PyUnicode_FromUCS1((unsigned char*)s, size);
 }
 
 /* create or adjust a UnicodeEncodeError */
@@ -4849,15 +5935,30 @@
 }
 
 PyObject *
-PyUnicode_AsLatin1String(PyObject *unicode)
+_PyUnicode_AsLatin1String(PyObject *unicode, const char *errors)
 {
     if (!PyUnicode_Check(unicode)) {
         PyErr_BadArgument();
         return NULL;
     }
+    if (PyUnicode_READY(unicode) == -1)
+        return NULL;
+    /* Fast path: if it is a one-byte string, construct
+       bytes object directly. */
+    if (PyUnicode_KIND(unicode) == PyUnicode_1BYTE_KIND)
+        return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
+                                         PyUnicode_GET_LENGTH(unicode));
+    /* Non-Latin-1 characters present. Defer to above function to
+       raise the exception. */
     return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(unicode),
                                   PyUnicode_GET_SIZE(unicode),
-                                  NULL);
+                                  errors);
+}
+
+PyObject*
+PyUnicode_AsLatin1String(PyObject *unicode)
+{
+    return _PyUnicode_AsLatin1String(unicode, NULL);
 }
 
 /* --- 7-bit ASCII Codec -------------------------------------------------- */
@@ -4874,14 +5975,32 @@
     Py_ssize_t endinpos;
     Py_ssize_t outpos;
     const char *e;
+    unsigned char* d;
     PyObject *errorHandler = NULL;
     PyObject *exc = NULL;
+    Py_ssize_t i;
 
     /* ASCII is equivalent to the first 128 ordinals in Unicode. */
-    if (size == 1 && *(unsigned char*)s < 128) {
-        Py_UNICODE r = *(unsigned char*)s;
-        return PyUnicode_FromUnicode(&r, 1);
+    if (size == 1 && *(unsigned char*)s < 128)
+        return PyUnicode_FromOrdinal(*(unsigned char*)s);
+
+    /* Fast path. Assume the input actually *is* ASCII, and allocate
+       a single-block Unicode object with that assumption. If there is
+       an error, drop the object and start over. */
+    v = (PyUnicodeObject*)PyUnicode_New(size, 127);
+    if (v == NULL)
+        goto onError;
+    d = PyUnicode_1BYTE_DATA(v);
+    for (i = 0; i < size; i++) {
+        unsigned char ch = ((unsigned char*)s)[i];
+        if (ch < 128)
+            d[i] = ch;
+        else
+            break;
     }
+    if (i == size)
+        return (PyObject*)v;
+    Py_DECREF(v); /* start over */
 
     v = _PyUnicode_New(size);
     if (v == NULL)
@@ -4913,6 +6032,10 @@
             goto onError;
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
+    if (PyUnicode_READY(v) == -1) {
+        Py_DECREF(v);
+        return NULL;
+    }
     return (PyObject *)v;
 
   onError:
@@ -4931,15 +6054,28 @@
 }
 
 PyObject *
-PyUnicode_AsASCIIString(PyObject *unicode)
+_PyUnicode_AsASCIIString(PyObject *unicode, const char *errors)
 {
     if (!PyUnicode_Check(unicode)) {
         PyErr_BadArgument();
         return NULL;
     }
+    if (PyUnicode_READY(unicode) == -1)
+        return NULL;
+    /* Fast path: if it is an ASCII-only string, construct bytes object
+       directly. Else defer to above function to raise the exception. */
+    if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128)
+        return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
+                                         PyUnicode_GET_LENGTH(unicode));
     return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(unicode),
                                  PyUnicode_GET_SIZE(unicode),
-                                 NULL);
+                                 errors);
+}
+
+PyObject *
+PyUnicode_AsASCIIString(PyObject *unicode)
+{
+    return _PyUnicode_AsASCIIString(unicode, NULL);
 }
 
 #ifdef HAVE_MBCS
@@ -5090,7 +6226,10 @@
         goto retry;
     }
 #endif
-
+    if (PyUnicode_READY(v) == -1) {
+        Py_DECREF(v);
+        return NULL;
+    }
     return (PyObject *)v;
 }
 
@@ -5386,6 +6525,10 @@
             goto onError;
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
+    if (PyUnicode_READY(v) == -1) {
+        Py_DECREF(v);
+        return NULL;
+    }
     return (PyObject *)v;
 
   onError:
@@ -5471,7 +6614,6 @@
 PyObject*
 PyUnicode_BuildEncodingMap(PyObject* string)
 {
-    Py_UNICODE *decode;
     PyObject *result;
     struct encoding_map *mresult;
     int i;
@@ -5480,35 +6622,36 @@
     unsigned char level2[512];
     unsigned char *mlevel1, *mlevel2, *mlevel3;
     int count2 = 0, count3 = 0;
+    int kind;
+    void *data;
+    Py_UCS4 ch;
 
-    if (!PyUnicode_Check(string) || PyUnicode_GetSize(string) != 256) {
+    if (!PyUnicode_Check(string) || PyUnicode_GET_LENGTH(string) != 256) {
         PyErr_BadArgument();
         return NULL;
     }
-    decode = PyUnicode_AS_UNICODE(string);
+    kind = PyUnicode_KIND(string);
+    data = PyUnicode_DATA(string);
     memset(level1, 0xFF, sizeof level1);
     memset(level2, 0xFF, sizeof level2);
 
     /* If there isn't a one-to-one mapping of NULL to \0,
        or if there are non-BMP characters, we need to use
        a mapping dictionary. */
-    if (decode[0] != 0)
+    if (PyUnicode_READ(kind, data, 0) != 0)
         need_dict = 1;
     for (i = 1; i < 256; i++) {
         int l1, l2;
-        if (decode[i] == 0
-#ifdef Py_UNICODE_WIDE
-            || decode[i] > 0xFFFF
-#endif
-            ) {
+        ch = PyUnicode_READ(kind, data, i);
+        if (ch == 0 || ch > 0xFFFF) {
             need_dict = 1;
             break;
         }
-        if (decode[i] == 0xFFFE)
+        if (ch == 0xFFFE)
             /* unmapped character */
             continue;
-        l1 = decode[i] >> 11;
-        l2 = decode[i] >> 7;
+        l1 = ch >> 11;
+        l2 = ch >> 7;
         if (level1[l1] == 0xFF)
             level1[l1] = count2++;
         if (level2[l2] == 0xFF)
@@ -5524,7 +6667,7 @@
         if (!result)
             return NULL;
         for (i = 0; i < 256; i++) {
-            key = PyLong_FromLong(decode[i]);
+            key = PyLong_FromLong(PyUnicode_READ(kind, data, i));
             value = PyLong_FromLong(i);
             if (!key || !value)
                 goto failed1;
@@ -5559,15 +6702,15 @@
     count3 = 0;
     for (i = 1; i < 256; i++) {
         int o1, o2, o3, i2, i3;
-        if (decode[i] == 0xFFFE)
+        if (PyUnicode_READ(kind, data, i) == 0xFFFE)
             /* unmapped character */
             continue;
-        o1 = decode[i]>>11;
-        o2 = (decode[i]>>7) & 0xF;
+        o1 = PyUnicode_READ(kind, data, i)>>11;
+        o2 = (PyUnicode_READ(kind, data, i)>>7) & 0xF;
         i2 = 16*mlevel1[o1] + o2;
         if (mlevel2[i2] == 0xFF)
             mlevel2[i2] = count3++;
-        o3 = decode[i] & 0x7F;
+        o3 = PyUnicode_READ(kind, data, i) & 0x7F;
         i3 = 128*mlevel2[i2] + o3;
         mlevel3[i3] = i;
     }
@@ -5951,13 +7094,13 @@
 /* create or adjust a UnicodeTranslateError */
 static void
 make_translate_exception(PyObject **exceptionObject,
-                         const Py_UNICODE *unicode, Py_ssize_t size,
+                         PyObject *unicode,
                          Py_ssize_t startpos, Py_ssize_t endpos,
                          const char *reason)
 {
     if (*exceptionObject == NULL) {
-        *exceptionObject = PyUnicodeTranslateError_Create(
-            unicode, size, startpos, endpos, reason);
+        *exceptionObject = _PyUnicodeTranslateError_Create(
+            unicode, startpos, endpos, reason);
     }
     else {
         if (PyUnicodeTranslateError_SetStart(*exceptionObject, startpos))
@@ -5976,12 +7119,12 @@
 /* raises a UnicodeTranslateError */
 static void
 raise_translate_exception(PyObject **exceptionObject,
-                          const Py_UNICODE *unicode, Py_ssize_t size,
+                          PyObject *unicode,
                           Py_ssize_t startpos, Py_ssize_t endpos,
                           const char *reason)
 {
     make_translate_exception(exceptionObject,
-                             unicode, size, startpos, endpos, reason);
+                             unicode, startpos, endpos, reason);
     if (*exceptionObject != NULL)
         PyCodec_StrictErrors(*exceptionObject);
 }
@@ -5994,7 +7137,7 @@
 unicode_translate_call_errorhandler(const char *errors,
                                     PyObject **errorHandler,
                                     const char *reason,
-                                    const Py_UNICODE *unicode, Py_ssize_t size, PyObject **exceptionObject,
+                                    PyObject *unicode, PyObject **exceptionObject,
                                     Py_ssize_t startpos, Py_ssize_t endpos,
                                     Py_ssize_t *newpos)
 {
@@ -6011,7 +7154,7 @@
     }
 
     make_translate_exception(exceptionObject,
-                             unicode, size, startpos, endpos, reason);
+                             unicode, startpos, endpos, reason);
     if (*exceptionObject == NULL)
         return NULL;
 
@@ -6030,10 +7173,10 @@
         return NULL;
     }
     if (i_newpos<0)
-        *newpos = size+i_newpos;
+        *newpos = PyUnicode_GET_LENGTH(unicode)+i_newpos;
     else
         *newpos = i_newpos;
-    if (*newpos<0 || *newpos>size) {
+    if (*newpos<0 || *newpos>PyUnicode_GET_LENGTH(unicode)) {
         PyErr_Format(PyExc_IndexError, "position %zd from error handler out of bounds", *newpos);
         Py_DECREF(restuple);
         return NULL;
@@ -6047,7 +7190,7 @@
    which must be decrefed by the caller.
    Return 0 on success, -1 on error */
 static int
-charmaptranslate_lookup(Py_UNICODE c, PyObject *mapping, PyObject **result)
+charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result)
 {
     PyObject *w = PyLong_FromLong((long)c);
     PyObject *x;
@@ -6097,19 +7240,18 @@
    if not reallocate and adjust various state variables.
    Return 0 on success, -1 on error */
 static int
-charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp,
+charmaptranslate_makespace(Py_UCS4 **outobj, Py_ssize_t *psize,
                                Py_ssize_t requiredsize)
 {
-    Py_ssize_t oldsize = PyUnicode_GET_SIZE(*outobj);
+    Py_ssize_t oldsize = *psize;
     if (requiredsize > oldsize) {
-        /* remember old output position */
-        Py_ssize_t outpos = *outp-PyUnicode_AS_UNICODE(*outobj);
         /* exponentially overallocate to minimize reallocations */
         if (requiredsize < 2 * oldsize)
             requiredsize = 2 * oldsize;
-        if (PyUnicode_Resize(outobj, requiredsize) < 0)
+        *outobj = PyMem_Realloc(*outobj, requiredsize * sizeof(Py_UCS4));
+        if (*outobj == 0)
             return -1;
-        *outp = PyUnicode_AS_UNICODE(*outobj) + outpos;
+        *psize = requiredsize;
     }
     return 0;
 }
@@ -6120,37 +7262,43 @@
    The called must decref result.
    Return 0 on success, -1 on error. */
 static int
-charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp,
-                        Py_ssize_t insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp,
+charmaptranslate_output(PyObject *input, Py_ssize_t ipos,
+                        PyObject *mapping, Py_UCS4 **output,
+                        Py_ssize_t *osize, Py_ssize_t *opos,
                         PyObject **res)
 {
-    if (charmaptranslate_lookup(*curinp, mapping, res))
+    Py_UCS4 curinp = PyUnicode_READ_CHAR(input, ipos);
+    if (charmaptranslate_lookup(curinp, mapping, res))
         return -1;
     if (*res==NULL) {
         /* not found => default to 1:1 mapping */
-        *(*outp)++ = *curinp;
+        (*output)[(*opos)++] = curinp;
     }
     else if (*res==Py_None)
         ;
     else if (PyLong_Check(*res)) {
         /* no overflow check, because we know that the space is enough */
-        *(*outp)++ = (Py_UNICODE)PyLong_AS_LONG(*res);
+        (*output)[(*opos)++] = (Py_UCS4)PyLong_AS_LONG(*res);
     }
     else if (PyUnicode_Check(*res)) {
-        Py_ssize_t repsize = PyUnicode_GET_SIZE(*res);
+        Py_ssize_t repsize;
+        if (PyUnicode_READY(*res) == -1)
+            return -1;
+        repsize = PyUnicode_GET_LENGTH(*res);
         if (repsize==1) {
             /* no overflow check, because we know that the space is enough */
-            *(*outp)++ = *PyUnicode_AS_UNICODE(*res);
+            (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, 0);
         }
         else if (repsize!=0) {
             /* more than one character */
-            Py_ssize_t requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) +
-                (insize - (curinp-startinp)) +
+            Py_ssize_t requiredsize = *opos +
+                (PyUnicode_GET_LENGTH(input) - ipos) +
                 repsize - 1;
-            if (charmaptranslate_makespace(outobj, outp, requiredsize))
+            Py_ssize_t i;
+            if (charmaptranslate_makespace(output, osize, requiredsize))
                 return -1;
-            memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize);
-            *outp += repsize;
+            for(i = 0; i < repsize; i++)
+                (*output)[(*opos)++] = PyUnicode_READ_CHAR(*res, i);
         }
     }
     else
@@ -6159,20 +7307,20 @@
 }
 
 PyObject *
-PyUnicode_TranslateCharmap(const Py_UNICODE *p,
-                           Py_ssize_t size,
-                           PyObject *mapping,
-                           const char *errors)
+_PyUnicode_TranslateCharmap(PyObject *input,
+                            PyObject *mapping,
+                            const char *errors)
 {
-    /* output object */
-    PyObject *res = NULL;
-    /* pointers to the beginning and end+1 of input */
-    const Py_UNICODE *startp = p;
-    const Py_UNICODE *endp = p + size;
-    /* pointer into the output */
-    Py_UNICODE *str;
+    /* input object */
+    char *idata;
+    Py_ssize_t size, i;
+    int kind;
+    /* output buffer */
+    Py_UCS4 *output = NULL;
+    Py_ssize_t osize;
+    PyObject *res;
     /* current output position */
-    Py_ssize_t respos = 0;
+    Py_ssize_t opos;
     char *reason = "character maps to <undefined>";
     PyObject *errorHandler = NULL;
     PyObject *exc = NULL;
@@ -6186,38 +7334,52 @@
         return NULL;
     }
 
+    if (PyUnicode_READY(input) == -1)
+        return NULL;
+    idata = (char*)PyUnicode_DATA(input);
+    kind = PyUnicode_KIND(input);
+    size = PyUnicode_GET_LENGTH(input);
+    i = 0;
+
+    if (size == 0) {
+        Py_INCREF(input);
+        return input;
+    }
+
     /* allocate enough for a simple 1:1 translation without
        replacements, if we need more, we'll resize */
-    res = PyUnicode_FromUnicode(NULL, size);
-    if (res == NULL)
+    osize = size;
+    output = PyMem_Malloc(osize * sizeof(Py_UCS4));
+    opos = 0;
+    if (output == NULL) {
+        PyErr_NoMemory();
         goto onError;
-    if (size == 0)
-        return res;
-    str = PyUnicode_AS_UNICODE(res);
+    }
 
-    while (p<endp) {
+    while (i<size) {
         /* try to encode it */
         PyObject *x = NULL;
-        if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) {
+        if (charmaptranslate_output(input, i, mapping,
+                                    &output, &osize, &opos, &x)) {
             Py_XDECREF(x);
             goto onError;
         }
         Py_XDECREF(x);
         if (x!=Py_None) /* it worked => adjust input pointer */
-            ++p;
+            ++i;
         else { /* untranslatable character */
             PyObject *repunicode = NULL; /* initialize to prevent gcc warning */
             Py_ssize_t repsize;
             Py_ssize_t newpos;
-            Py_UNICODE *uni2;
+            Py_ssize_t uni2;
             /* startpos for collecting untranslatable chars */
-            const Py_UNICODE *collstart = p;
-            const Py_UNICODE *collend = p+1;
-            const Py_UNICODE *coll;
+            Py_ssize_t collstart = i;
+            Py_ssize_t collend = i+1;
+            Py_ssize_t coll;
 
             /* find all untranslatable characters */
-            while (collend < endp) {
-                if (charmaptranslate_lookup(*collend, mapping, &x))
+            while (collend < size) {
+                if (charmaptranslate_lookup(PyUnicode_READ(kind,idata, collend), mapping, &x))
                     goto onError;
                 Py_XDECREF(x);
                 if (x!=Py_None)
@@ -6240,67 +7402,79 @@
             }
             switch (known_errorHandler) {
             case 1: /* strict */
-                raise_translate_exception(&exc, startp, size, collstart-startp, collend-startp, reason);
+                raise_translate_exception(&exc, input, collstart,
+                                          collend, reason);
                 goto onError;
             case 2: /* replace */
                 /* No need to check for space, this is a 1:1 replacement */
-                for (coll = collstart; coll<collend; ++coll)
-                    *str++ = '?';
+                for (coll = collstart; coll<collend; coll++)
+                    output[opos++] = '?';
                 /* fall through */
             case 3: /* ignore */
-                p = collend;
+                i = collend;
                 break;
             case 4: /* xmlcharrefreplace */
-                /* generate replacement (temporarily (mis)uses p) */
-                for (p = collstart; p < collend; ++p) {
+                /* generate replacement (temporarily (mis)uses i) */
+                for (i = collstart; i < collend; ++i) {
                     char buffer[2+29+1+1];
                     char *cp;
-                    sprintf(buffer, "&#%d;", (int)*p);
-                    if (charmaptranslate_makespace(&res, &str,
-                                                   (str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))
+                    sprintf(buffer, "&#%d;", PyUnicode_READ(kind, idata, i));
+                    if (charmaptranslate_makespace(&output, &osize,
+                                                   opos+strlen(buffer)+(size-collend)))
                         goto onError;
                     for (cp = buffer; *cp; ++cp)
-                        *str++ = *cp;
+                        output[opos++] = *cp;
                 }
-                p = collend;
+                i = collend;
                 break;
             default:
                 repunicode = unicode_translate_call_errorhandler(errors, &errorHandler,
-                                                                 reason, startp, size, &exc,
-                                                                 collstart-startp, collend-startp, &newpos);
-                if (repunicode == NULL)
+                                                                 reason, input, &exc,
+                                                                 collstart, collend, &newpos);
+                if (repunicode == NULL || PyUnicode_READY(repunicode) == -1)
                     goto onError;
                 /* generate replacement  */
-                repsize = PyUnicode_GET_SIZE(repunicode);
-                if (charmaptranslate_makespace(&res, &str,
-                                               (str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) {
+                repsize = PyUnicode_GET_LENGTH(repunicode);
+                if (charmaptranslate_makespace(&output, &osize,
+                                               opos+repsize+(size-collend))) {
                     Py_DECREF(repunicode);
                     goto onError;
                 }
-                for (uni2 = PyUnicode_AS_UNICODE(repunicode); repsize-->0; ++uni2)
-                    *str++ = *uni2;
-                p = startp + newpos;
+                for (uni2 = 0; repsize-->0; ++uni2)
+                    output[opos++] = PyUnicode_READ_CHAR(repunicode, uni2);
+                i = newpos;
                 Py_DECREF(repunicode);
             }
         }
     }
-    /* Resize if we allocated to much */
-    respos = str-PyUnicode_AS_UNICODE(res);
-    if (respos<PyUnicode_GET_SIZE(res)) {
-        if (PyUnicode_Resize(&res, respos) < 0)
-            goto onError;
-    }
+    res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, output, opos);
+    if (!res)
+        goto onError;
+    PyMem_Free(output);
     Py_XDECREF(exc);
     Py_XDECREF(errorHandler);
     return res;
 
   onError:
-    Py_XDECREF(res);
+    PyMem_Free(output);
     Py_XDECREF(exc);
     Py_XDECREF(errorHandler);
     return NULL;
 }
 
+/* Deprecated. Use PyUnicode_Translate instead. */
+PyObject *
+PyUnicode_TranslateCharmap(const Py_UNICODE *p,
+                           Py_ssize_t size,
+                           PyObject *mapping,
+                           const char *errors)
+{
+    PyObject *unicode = PyUnicode_FromUnicode(p, size);
+    if (!unicode)
+        return NULL;
+    return _PyUnicode_TranslateCharmap(unicode, mapping, errors);
+}
+
 PyObject *
 PyUnicode_Translate(PyObject *str,
                     PyObject *mapping,
@@ -6311,10 +7485,7 @@
     str = PyUnicode_FromObject(str);
     if (str == NULL)
         goto onError;
-    result = PyUnicode_TranslateCharmap(PyUnicode_AS_UNICODE(str),
-                                        PyUnicode_GET_SIZE(str),
-                                        mapping,
-                                        errors);
+    result = _PyUnicode_TranslateCharmap(str, mapping, errors);
     Py_DECREF(str);
     return result;
 
@@ -6323,6 +7494,60 @@
     return NULL;
 }
 
+static Py_UCS4
+fix_decimal_and_space_to_ascii(PyUnicodeObject *self)
+{
+    /* No need to call PyUnicode_READY(self) because this function is only
+       called as a callback from fixup() which does it already. */
+    const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+    const int kind = PyUnicode_KIND(self);
+    void *data = PyUnicode_DATA(self);
+    Py_UCS4 maxchar = 0, ch, fixed;
+    Py_ssize_t i;
+
+    for (i = 0; i < len; ++i) {
+        ch = PyUnicode_READ(kind, data, i);
+        fixed = 0;
+        if (ch > 127) {
+            if (Py_UNICODE_ISSPACE(ch))
+                fixed = ' ';
+            else {
+                const int decimal = Py_UNICODE_TODECIMAL(ch);
+                if (decimal >= 0)
+                    fixed = '0' + decimal;
+            }
+            if (fixed != 0) {
+                if (fixed > maxchar)
+                    maxchar = fixed;
+                PyUnicode_WRITE(kind, data, i, fixed);
+            }
+            else if (ch > maxchar)
+                maxchar = ch;
+        }
+        else if (ch > maxchar)
+            maxchar = ch;
+    }
+
+    return maxchar;
+}
+
+PyObject *
+_PyUnicode_TransformDecimalAndSpaceToASCII(PyObject *unicode)
+{
+    if (!PyUnicode_Check(unicode)) {
+        PyErr_BadInternalCall();
+        return NULL;
+    }
+    if (PyUnicode_READY(unicode) == -1)
+        return NULL;
+    if (PyUnicode_MAX_CHAR_VALUE(unicode) <= 127) {
+        /* If the string is already ASCII, just return the same string */
+        Py_INCREF(unicode);
+        return unicode;
+    }
+    return fixup((PyUnicodeObject *)unicode, fix_decimal_and_space_to_ascii);
+}
+
 PyObject *
 PyUnicode_TransformDecimalToASCII(Py_UNICODE *s,
                                   Py_ssize_t length)
@@ -6345,6 +7570,10 @@
                 p[i] = '0' + decimal;
         }
     }
+    if (PyUnicode_READY((PyUnicodeObject*)result) == -1) {
+        Py_DECREF(result);
+        return NULL;
+    }
     return result;
 }
 /* --- Decimal Encoder ---------------------------------------------------- */
@@ -6487,17 +7716,123 @@
 
 /* --- Helpers ------------------------------------------------------------ */
 
+#include "stringlib/ucs1lib.h"
+#include "stringlib/fastsearch.h"
+#include "stringlib/partition.h"
+#include "stringlib/split.h"
+#include "stringlib/count.h"
+#include "stringlib/find.h"
+#include "stringlib/localeutil.h"
+#include "stringlib/undef.h"
+
+#include "stringlib/ucs2lib.h"
+#include "stringlib/fastsearch.h"
+#include "stringlib/partition.h"
+#include "stringlib/split.h"
+#include "stringlib/count.h"
+#include "stringlib/find.h"
+#include "stringlib/localeutil.h"
+#include "stringlib/undef.h"
+
+#include "stringlib/ucs4lib.h"
+#include "stringlib/fastsearch.h"
+#include "stringlib/partition.h"
+#include "stringlib/split.h"
+#include "stringlib/count.h"
+#include "stringlib/find.h"
+#include "stringlib/localeutil.h"
+#include "stringlib/undef.h"
+
+static Py_ssize_t
+any_find_slice(Py_ssize_t Py_LOCAL_CALLBACK(ucs1)(const Py_UCS1*, Py_ssize_t,
+                                  const Py_UCS1*, Py_ssize_t,
+                                  Py_ssize_t, Py_ssize_t),
+               Py_ssize_t Py_LOCAL_CALLBACK(ucs2)(const Py_UCS2*, Py_ssize_t,
+                                  const Py_UCS2*, Py_ssize_t,
+                                  Py_ssize_t, Py_ssize_t),
+               Py_ssize_t Py_LOCAL_CALLBACK(ucs4)(const Py_UCS4*, Py_ssize_t,
+                                  const Py_UCS4*, Py_ssize_t,
+                                  Py_ssize_t, Py_ssize_t),
+               PyObject* s1, PyObject* s2,
+               Py_ssize_t start,
+               Py_ssize_t end)
+{
+    int kind1, kind2, kind;
+    void *buf1, *buf2;
+    Py_ssize_t len1, len2, result;
+
+    kind1 = PyUnicode_KIND(s1);
+    kind2 = PyUnicode_KIND(s2);
+    kind = kind1 > kind2 ? kind1 : kind2;
+    buf1 = PyUnicode_DATA(s1);
+    buf2 = PyUnicode_DATA(s2);
+    if (kind1 != kind)
+        buf1 = _PyUnicode_AsKind(s1, kind);
+    if (!buf1)
+        return -2;
+    if (kind2 != kind)
+        buf2 = _PyUnicode_AsKind(s2, kind);
+    if (!buf2) {
+        if (kind1 != kind) PyMem_Free(buf1);
+        return -2;
+    }
+    len1 = PyUnicode_GET_LENGTH(s1);
+    len2 = PyUnicode_GET_LENGTH(s2);
+
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        result = ucs1(buf1, len1, buf2, len2, start, end);
+        break;
+    case PyUnicode_2BYTE_KIND:
+        result = ucs2(buf1, len1, buf2, len2, start, end);
+        break;
+    case PyUnicode_4BYTE_KIND:
+        result = ucs4(buf1, len1, buf2, len2, start, end);
+        break;
+    default:
+        assert(0); result = -2;
+    }
+
+    if (kind1 != kind)
+        PyMem_Free(buf1);
+    if (kind2 != kind)
+        PyMem_Free(buf2);
+
+    return result;
+}
+
+Py_ssize_t
+_PyUnicode_InsertThousandsGrouping(int kind, void *data,
+                                   Py_ssize_t n_buffer,
+                                   void *digits, Py_ssize_t n_digits,
+                                   Py_ssize_t min_width,
+                                   const char *grouping,
+                                   const char *thousands_sep)
+{
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        return _PyUnicode_ucs1_InsertThousandsGrouping(
+            (Py_UCS1*)data, n_buffer, (Py_UCS1*)digits, n_digits,
+            min_width, grouping, thousands_sep);
+    case PyUnicode_2BYTE_KIND:
+        return _PyUnicode_ucs2_InsertThousandsGrouping(
+            (Py_UCS2*)data, n_buffer, (Py_UCS2*)digits, n_digits,
+            min_width, grouping, thousands_sep);
+    case PyUnicode_4BYTE_KIND:
+        return _PyUnicode_ucs4_InsertThousandsGrouping(
+            (Py_UCS4*)data, n_buffer, (Py_UCS4*)digits, n_digits,
+            min_width, grouping, thousands_sep);
+    }
+    assert(0);
+    return -1;
+}
+
+
 #include "stringlib/unicodedefs.h"
 #include "stringlib/fastsearch.h"
 
 #include "stringlib/count.h"
 #include "stringlib/find.h"
-#include "stringlib/partition.h"
-#include "stringlib/split.h"
-
-#define _Py_InsertThousandsGrouping _PyUnicode_InsertThousandsGrouping
-#define _Py_InsertThousandsGroupingLocale _PyUnicode_InsertThousandsGroupingLocale
-#include "stringlib/localeutil.h"
 
 /* helper macro to fixup start/end slice values */
 #define ADJUST_INDICES(start, end, len)         \
@@ -6514,28 +7849,6 @@
             start = 0;                          \
     }
 
-/* _Py_UNICODE_NEXT is a private macro used to retrieve the character pointed
- * by 'ptr', possibly combining surrogate pairs on narrow builds.
- * 'ptr' and 'end' must be Py_UNICODE*, with 'ptr' pointing at the character
- * that should be returned and 'end' pointing to the end of the buffer.
- * ('end' is used on narrow builds to detect a lone surrogate at the
- * end of the buffer that should be returned unchanged.)
- * The ptr and end arguments should be side-effect free and ptr must an lvalue.
- * The type of the returned char is always Py_UCS4.
- *
- * Note: the macro advances ptr to next char, so it might have side-effects
- *       (especially if used with other macros).
- */
-#ifdef Py_UNICODE_WIDE
-#define _Py_UNICODE_NEXT(ptr, end) *(ptr)++
-#else
-#define _Py_UNICODE_NEXT(ptr, end)                                     \
-     (((Py_UNICODE_IS_HIGH_SURROGATE(*(ptr)) && (ptr) < (end)) &&      \
-        Py_UNICODE_IS_LOW_SURROGATE((ptr)[1])) ?                       \
-       ((ptr) += 2,Py_UNICODE_JOIN_SURROGATES((ptr)[-2], (ptr)[-1])) : \
-       (Py_UCS4)*(ptr)++)
-#endif
-
 Py_ssize_t
 PyUnicode_Count(PyObject *str,
                 PyObject *substr,
@@ -6545,26 +7858,76 @@
     Py_ssize_t result;
     PyUnicodeObject* str_obj;
     PyUnicodeObject* sub_obj;
+    int kind1, kind2, kind;
+    void *buf1 = NULL, *buf2 = NULL;
+    Py_ssize_t len1, len2;
 
     str_obj = (PyUnicodeObject*) PyUnicode_FromObject(str);
-    if (!str_obj)
+    if (!str_obj || PyUnicode_READY(str_obj) == -1)
         return -1;
     sub_obj = (PyUnicodeObject*) PyUnicode_FromObject(substr);
-    if (!sub_obj) {
+    if (!sub_obj || PyUnicode_READY(str_obj) == -1) {
         Py_DECREF(str_obj);
         return -1;
     }
 
-    ADJUST_INDICES(start, end, str_obj->length);
-    result = stringlib_count(
-        str_obj->str + start, end - start, sub_obj->str, sub_obj->length,
-        PY_SSIZE_T_MAX
-        );
+    kind1 = PyUnicode_KIND(str_obj);
+    kind2 = PyUnicode_KIND(sub_obj);
+    kind = kind1 > kind2 ? kind1 : kind2;
+    buf1 = PyUnicode_DATA(str_obj);
+    if (kind1 != kind)
+        buf1 = _PyUnicode_AsKind((PyObject*)str_obj, kind);
+    if (!buf1)
+        goto onError;
+    buf2 = PyUnicode_DATA(sub_obj);
+    if (kind2 != kind)
+        buf2 = _PyUnicode_AsKind((PyObject*)sub_obj, kind);
+    if (!buf2)
+        goto onError;
+    len1 = PyUnicode_GET_LENGTH(str_obj);
+    len2 = PyUnicode_GET_LENGTH(sub_obj);
+
+    ADJUST_INDICES(start, end, len1);
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        result = ucs1lib_count(
+            ((Py_UCS1*)buf1) + start, end - start,
+            buf2, len2, PY_SSIZE_T_MAX
+            );
+        break;
+    case PyUnicode_2BYTE_KIND:
+        result = ucs2lib_count(
+            ((Py_UCS2*)buf1) + start, end - start,
+            buf2, len2, PY_SSIZE_T_MAX
+            );
+        break;
+    case PyUnicode_4BYTE_KIND:
+        result = ucs4lib_count(
+            ((Py_UCS4*)buf1) + start, end - start,
+            buf2, len2, PY_SSIZE_T_MAX
+            );
+        break;
+    default:
+        assert(0); result = 0;
+    }
 
     Py_DECREF(sub_obj);
     Py_DECREF(str_obj);
 
+    if (kind1 != kind)
+        PyMem_Free(buf1);
+    if (kind2 != kind)
+        PyMem_Free(buf2);
+
     return result;
+  onError:
+    Py_DECREF(sub_obj);
+    Py_DECREF(str_obj);
+    if (kind1 != kind && buf1)
+        PyMem_Free(buf1);
+    if (kind2 != kind && buf2)
+        PyMem_Free(buf2);
+    return -1;
 }
 
 Py_ssize_t
@@ -6577,25 +7940,23 @@
     Py_ssize_t result;
 
     str = PyUnicode_FromObject(str);
-    if (!str)
+    if (!str || PyUnicode_READY(str) == -1)
         return -2;
     sub = PyUnicode_FromObject(sub);
-    if (!sub) {
+    if (!sub || PyUnicode_READY(sub) == -1) {
         Py_DECREF(str);
         return -2;
     }
 
     if (direction > 0)
-        result = stringlib_find_slice(
-            PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str),
-            PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub),
-            start, end
+        result = any_find_slice(
+            ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice,
+            str, sub, start, end
             );
     else
-        result = stringlib_rfind_slice(
-            PyUnicode_AS_UNICODE(str), PyUnicode_GET_SIZE(str),
-            PyUnicode_AS_UNICODE(sub), PyUnicode_GET_SIZE(sub),
-            start, end
+        result = any_find_slice(
+            ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice,
+            str, sub, start, end
             );
 
     Py_DECREF(str);
@@ -6604,6 +7965,27 @@
     return result;
 }
 
+Py_ssize_t
+PyUnicode_FindChar(PyObject *str, Py_UCS4 ch,
+                   Py_ssize_t start, Py_ssize_t end,
+                   int direction)
+{
+    char *result;
+    int kind;
+    if (PyUnicode_READY(str) == -1)
+        return -2;
+    if (end > PyUnicode_GET_LENGTH(str))
+        end = PyUnicode_GET_LENGTH(str);
+    kind = PyUnicode_KIND(str);
+    result = findchar(PyUnicode_1BYTE_DATA(str)
+                      + PyUnicode_KIND_SIZE(kind, start),
+                      kind,
+                      end-start, ch, direction);
+    if (!result)
+        return -1;
+    return (result-(char*)PyUnicode_DATA(str)) >> (kind-1);
+}
+
 static int
 tailmatch(PyUnicodeObject *self,
           PyUnicodeObject *substring,
@@ -6611,20 +7993,62 @@
           Py_ssize_t end,
           int direction)
 {
-    if (substring->length == 0)
+    int kind_self;
+    int kind_sub;
+    void *data_self;
+    void *data_sub;
+    Py_ssize_t offset;
+    Py_ssize_t i;
+    Py_ssize_t end_sub;
+
+    if (PyUnicode_READY(self) == -1 ||
+        PyUnicode_READY(substring) == -1)
+        return 0;
+
+    if (PyUnicode_GET_LENGTH(substring) == 0)
         return 1;
 
-    ADJUST_INDICES(start, end, self->length);
-    end -= substring->length;
+    ADJUST_INDICES(start, end, PyUnicode_GET_LENGTH(self));
+    end -= PyUnicode_GET_LENGTH(substring);
     if (end < start)
         return 0;
 
-    if (direction > 0) {
-        if (Py_UNICODE_MATCH(self, end, substring))
+    kind_self = PyUnicode_KIND(self);
+    data_self = PyUnicode_DATA(self);
+    kind_sub = PyUnicode_KIND(substring);
+    data_sub = PyUnicode_DATA(substring);
+    end_sub = PyUnicode_GET_LENGTH(substring) - 1;
+
+    if (direction > 0)
+        offset = end;
+    else
+        offset = start;
+
+    if (PyUnicode_READ(kind_self, data_self, offset) ==
+        PyUnicode_READ(kind_sub, data_sub, 0) &&
+        PyUnicode_READ(kind_self, data_self, offset + end_sub) ==
+        PyUnicode_READ(kind_sub, data_sub, end_sub)) {
+        /* If both are of the same kind, memcmp is sufficient */
+        if (kind_self == kind_sub) {
+            return ! memcmp((char *)data_self +
+                                (offset * PyUnicode_CHARACTER_SIZE(substring)),
+                            data_sub,
+                            PyUnicode_GET_LENGTH(substring) *
+                                PyUnicode_CHARACTER_SIZE(substring));
+        }
+        /* otherwise we have to compare each character by first accesing it */
+        else {
+            /* We do not need to compare 0 and len(substring)-1 because
+               the if statement above ensured already that they are equal
+               when we end up here. */
+            // TODO: honor direction and do a forward or backwards search
+            for (i = 1; i < end_sub; ++i) {
+                if (PyUnicode_READ(kind_self, data_self, offset + i) !=
+                    PyUnicode_READ(kind_sub, data_sub, i))
+                    return 0;
+            }
             return 1;
-    } else {
-        if (Py_UNICODE_MATCH(self, start, substring))
-            return 1;
+        }
     }
 
     return 0;
@@ -6661,18 +8085,39 @@
 
 static PyObject *
 fixup(PyUnicodeObject *self,
-      int (*fixfct)(PyUnicodeObject *s))
+      Py_UCS4 (*fixfct)(PyUnicodeObject *s))
 {
+    PyObject *u;
+    Py_UCS4 maxchar_old, maxchar_new = 0;
 
-    PyUnicodeObject *u;
-
-    u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    maxchar_old = PyUnicode_MAX_CHAR_VALUE(self);
+    u = PyUnicode_New(PyUnicode_GET_LENGTH(self),
+                      maxchar_old);
     if (u == NULL)
         return NULL;
 
-    Py_UNICODE_COPY(u->str, self->str, self->length);
+    Py_MEMCPY(PyUnicode_1BYTE_DATA(u), PyUnicode_1BYTE_DATA(self),
+              PyUnicode_GET_LENGTH(u) * PyUnicode_CHARACTER_SIZE(u));
 
-    if (!fixfct(u) && PyUnicode_CheckExact(self)) {
+    /* fix functions return the new maximum character in a string,
+       if the kind of the resulting unicode object does not change,
+       everything is fine.  Otherwise we need to change the string kind
+       and re-run the fix function. */
+    maxchar_new = fixfct((PyUnicodeObject*)u);
+    if (maxchar_new == 0)
+        /* do nothing, keep maxchar_new at 0 which means no changes. */;
+    else if (maxchar_new <= 127)
+        maxchar_new = 127;
+    else if (maxchar_new <= 255)
+        maxchar_new = 255;
+    else if (maxchar_new <= 65535)
+        maxchar_new = 65535;
+    else
+        maxchar_new = 1114111; /* 0x10ffff */
+
+    if (!maxchar_new && PyUnicode_CheckExact(self)) {
         /* fixfct should return TRUE if it modified the buffer. If
            FALSE, return a reference to the original buffer instead
            (to save space, not time) */
@@ -6680,123 +8125,207 @@
         Py_DECREF(u);
         return (PyObject*) self;
     }
-    return (PyObject*) u;
+    else if (maxchar_new == maxchar_old) {
+        return u;
+    }
+    else {
+        /* In case the maximum character changed, we need to
+           convert the string to the new category. */
+        PyObject *v = PyUnicode_New(
+                             PyUnicode_GET_LENGTH(self), maxchar_new);
+        if (v == NULL) {
+            Py_DECREF(u);
+            return NULL;
+        }
+        if (maxchar_new > maxchar_old) {
+            /* If the maxchar increased so that the kind changed, not all
+               characters are representable anymore and we need to fix the
+               string again. This only happens in very few cases. */
+            PyUnicode_CopyCharacters(v, 0, (PyObject*)self, 0, PyUnicode_GET_LENGTH(self));
+            maxchar_old = fixfct((PyUnicodeObject*)v);
+            assert(maxchar_old > 0 && maxchar_old <= maxchar_new);
+        }
+        else
+            PyUnicode_CopyCharacters(v, 0, u, 0, PyUnicode_GET_LENGTH(self));
+
+        Py_DECREF(u);
+        return v;
+    }
 }
 
-static int
+static Py_UCS4
 fixupper(PyUnicodeObject *self)
 {
-    Py_ssize_t len = self->length;
-    Py_UNICODE *s = self->str;
-    int status = 0;
+    /* No need to call PyUnicode_READY(self) because this function is only
+       called as a callback from fixup() which does it already. */
+    const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+    const int kind = PyUnicode_KIND(self);
+    void *data = PyUnicode_DATA(self);
+    int touched = 0;
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t i;
 
-    while (len-- > 0) {
-        register Py_UNICODE ch;
-
-        ch = Py_UNICODE_TOUPPER(*s);
-        if (ch != *s) {
-            status = 1;
-            *s = ch;
+    for (i = 0; i < len; ++i) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+        const Py_UCS4 up = Py_UNICODE_TOUPPER(ch);
+        if (up != ch) {
+            if (up > maxchar)
+                maxchar = up;
+            PyUnicode_WRITE(kind, data, i, up);
+            touched = 1;
         }
-        s++;
+        else if (ch > maxchar)
+            maxchar = ch;
     }
 
-    return status;
+    if (touched)
+        return maxchar;
+    else
+        return 0;
 }
 
-static int
+static Py_UCS4
 fixlower(PyUnicodeObject *self)
 {
-    Py_ssize_t len = self->length;
-    Py_UNICODE *s = self->str;
-    int status = 0;
+    /* No need to call PyUnicode_READY(self) because fixup() which does it. */
+    const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+    const int kind = PyUnicode_KIND(self);
+    void *data = PyUnicode_DATA(self);
+    int touched = 0;
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t i;
 
-    while (len-- > 0) {
-        register Py_UNICODE ch;
-
-        ch = Py_UNICODE_TOLOWER(*s);
-        if (ch != *s) {
-            status = 1;
-            *s = ch;
+    for(i = 0; i < len; ++i) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+        const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch);
+        if (lo != ch) {
+            if (lo > maxchar)
+                maxchar = lo;
+            PyUnicode_WRITE(kind, data, i, lo);
+            touched = 1;
         }
-        s++;
+        else if (ch > maxchar)
+            maxchar = ch;
     }
 
-    return status;
+    if (touched)
+        return maxchar;
+    else
+        return 0;
 }
 
-static int
+static Py_UCS4
 fixswapcase(PyUnicodeObject *self)
 {
-    Py_ssize_t len = self->length;
-    Py_UNICODE *s = self->str;
-    int status = 0;
+    /* No need to call PyUnicode_READY(self) because fixup() which does it. */
+    const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+    const int kind = PyUnicode_KIND(self);
+    void *data = PyUnicode_DATA(self);
+    int touched = 0;
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t i;
 
-    while (len-- > 0) {
-        if (Py_UNICODE_ISUPPER(*s)) {
-            *s = Py_UNICODE_TOLOWER(*s);
-            status = 1;
-        } else if (Py_UNICODE_ISLOWER(*s)) {
-            *s = Py_UNICODE_TOUPPER(*s);
-            status = 1;
+    for(i = 0; i < len; ++i) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+        Py_UCS4 nu = 0;
+
+        if (Py_UNICODE_ISUPPER(ch))
+            nu = Py_UNICODE_TOLOWER(ch);
+        else if (Py_UNICODE_ISLOWER(ch))
+            nu = Py_UNICODE_TOUPPER(ch);
+
+        if (nu != 0) {
+            if (nu > maxchar)
+                maxchar = nu;
+            PyUnicode_WRITE(kind, data, i, nu);
+            touched = 1;
         }
-        s++;
+        else if (ch > maxchar)
+            maxchar = ch;
     }
 
-    return status;
+    if (touched)
+        return maxchar;
+    else
+        return 0;
 }
 
-static int
+static Py_UCS4
 fixcapitalize(PyUnicodeObject *self)
 {
-    Py_ssize_t len = self->length;
-    Py_UNICODE *s = self->str;
-    int status = 0;
+    /* No need to call PyUnicode_READY(self) because fixup() which does it. */
+    const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+    const int kind = PyUnicode_KIND(self);
+    void *data = PyUnicode_DATA(self);
+    int touched = 0;
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t i = 0;
+    Py_UCS4 ch;
 
     if (len == 0)
         return 0;
-    if (!Py_UNICODE_ISUPPER(*s)) {
-        *s = Py_UNICODE_TOUPPER(*s);
-        status = 1;
+
+    ch = PyUnicode_READ(kind, data, i);
+    if (!Py_UNICODE_ISUPPER(ch)) {
+        maxchar = Py_UNICODE_TOUPPER(ch);
+        PyUnicode_WRITE(kind, data, i, maxchar);
+        touched = 1;
     }
-    s++;
-    while (--len > 0) {
-        if (!Py_UNICODE_ISLOWER(*s)) {
-            *s = Py_UNICODE_TOLOWER(*s);
-            status = 1;
+    ++i;
+    for(; i < len; ++i) {
+        ch = PyUnicode_READ(kind, data, i);
+        if (!Py_UNICODE_ISLOWER(ch)) {
+            const Py_UCS4 lo = Py_UNICODE_TOLOWER(ch);
+            if (lo > maxchar)
+                maxchar = lo;
+            PyUnicode_WRITE(kind, data, i, lo);
+            touched = 1;
         }
-        s++;
+        else if (ch > maxchar)
+            maxchar = ch;
     }
-    return status;
+
+    if (touched)
+        return maxchar;
+    else
+        return 0;
 }
 
-static int
+static Py_UCS4
 fixtitle(PyUnicodeObject *self)
 {
-    register Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register Py_UNICODE *e;
+    /* No need to call PyUnicode_READY(self) because fixup() which does it. */
+    const Py_ssize_t len = PyUnicode_GET_LENGTH(self);
+    const int kind = PyUnicode_KIND(self);
+    void *data = PyUnicode_DATA(self);
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t i = 0;
     int previous_is_cased;
 
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1) {
-        Py_UNICODE ch = Py_UNICODE_TOTITLE(*p);
-        if (*p != ch) {
-            *p = ch;
-            return 1;
+    if (len == 1) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+        const Py_UCS4 ti = Py_UNICODE_TOTITLE(ch);
+        if (ti != ch) {
+            PyUnicode_WRITE(kind, data, i, ti);
+            return ti;
         }
         else
             return 0;
     }
-
-    e = p + PyUnicode_GET_SIZE(self);
     previous_is_cased = 0;
-    for (; p < e; p++) {
-        register const Py_UNICODE ch = *p;
+    for(; i < len; ++i) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
+        Py_UCS4 nu;
 
         if (previous_is_cased)
-            *p = Py_UNICODE_TOLOWER(ch);
+            nu = Py_UNICODE_TOLOWER(ch);
         else
-            *p = Py_UNICODE_TOTITLE(ch);
+            nu = Py_UNICODE_TOTITLE(ch);
+
+        if (nu > maxchar)
+            maxchar = nu;
+        PyUnicode_WRITE(kind, data, i, nu);
 
         if (Py_UNICODE_ISLOWER(ch) ||
             Py_UNICODE_ISUPPER(ch) ||
@@ -6805,22 +8334,22 @@
         else
             previous_is_cased = 0;
     }
-    return 1;
+    return maxchar;
 }
 
 PyObject *
 PyUnicode_Join(PyObject *separator, PyObject *seq)
 {
-    const Py_UNICODE blank = ' ';
-    const Py_UNICODE *sep = &blank;
+    PyObject *sep = NULL;
     Py_ssize_t seplen = 1;
-    PyUnicodeObject *res = NULL; /* the result */
-    Py_UNICODE *res_p;       /* pointer to free byte in res's string area */
+    PyObject *res = NULL; /* the result */
     PyObject *fseq;          /* PySequence_Fast(seq) */
     Py_ssize_t seqlen;       /* len(fseq) -- number of items in sequence */
     PyObject **items;
     PyObject *item;
-    Py_ssize_t sz, i;
+    Py_ssize_t sz, i, res_offset;
+    Py_UCS4 maxchar = 0;
+    Py_UCS4 item_maxchar;
 
     fseq = PySequence_Fast(seq, "");
     if (fseq == NULL) {
@@ -6834,7 +8363,7 @@
     seqlen = PySequence_Fast_GET_SIZE(fseq);
     /* If empty sequence, return u"". */
     if (seqlen == 0) {
-        res = _PyUnicode_New(0);  /* empty sequence; return u"" */
+        res = PyUnicode_New(0, 0);
         goto Done;
     }
     items = PySequence_Fast_ITEMS(fseq);
@@ -6843,15 +8372,17 @@
         item = items[0];
         if (PyUnicode_CheckExact(item)) {
             Py_INCREF(item);
-            res = (PyUnicodeObject *)item;
+            res = item;
             goto Done;
         }
     }
     else {
         /* Set up sep and seplen */
         if (separator == NULL) {
-            sep = &blank;
-            seplen = 1;
+            /* fall back to a blank space separator */
+            sep = PyUnicode_FromOrdinal(' ');
+            if (!sep || PyUnicode_READY(sep) == -1)
+                goto onError;
         }
         else {
             if (!PyUnicode_Check(separator)) {
@@ -6861,8 +8392,14 @@
                              Py_TYPE(separator)->tp_name);
                 goto onError;
             }
-            sep = PyUnicode_AS_UNICODE(separator);
-            seplen = PyUnicode_GET_SIZE(separator);
+            if (PyUnicode_READY(separator) == -1)
+                goto onError;
+            sep = separator;
+            seplen = PyUnicode_GET_LENGTH(separator);
+            maxchar = PyUnicode_MAX_CHAR_VALUE(separator);
+            /* inc refcount to keep this code path symetric with the
+               above case of a blank separator */
+            Py_INCREF(sep);
         }
     }
 
@@ -6882,7 +8419,12 @@
                          i, Py_TYPE(item)->tp_name);
             goto onError;
         }
-        sz += PyUnicode_GET_SIZE(item);
+        if (PyUnicode_READY(item) == -1)
+            goto onError;
+        sz += PyUnicode_GET_LENGTH(item);
+        item_maxchar = PyUnicode_MAX_CHAR_VALUE(item);
+        if (item_maxchar > maxchar)
+            maxchar = item_maxchar;
         if (i != 0)
             sz += seplen;
         if (sz < old_sz || sz > PY_SSIZE_T_MAX) {
@@ -6892,42 +8434,70 @@
         }
     }
 
-    res = _PyUnicode_New(sz);
+    res = PyUnicode_New(sz, maxchar);
     if (res == NULL)
         goto onError;
 
     /* Catenate everything. */
-    res_p = PyUnicode_AS_UNICODE(res);
-    for (i = 0; i < seqlen; ++i) {
+    for (i = 0, res_offset = 0; i < seqlen; ++i) {
         Py_ssize_t itemlen;
         item = items[i];
-        itemlen = PyUnicode_GET_SIZE(item);
+        itemlen = PyUnicode_GET_LENGTH(item);
         /* Copy item, and maybe the separator. */
         if (i) {
-            Py_UNICODE_COPY(res_p, sep, seplen);
-            res_p += seplen;
+            PyUnicode_CopyCharacters(res, res_offset,
+                                     sep, 0, seplen);
+            res_offset += seplen;
         }
-        Py_UNICODE_COPY(res_p, PyUnicode_AS_UNICODE(item), itemlen);
-        res_p += itemlen;
+        PyUnicode_CopyCharacters(res, res_offset,
+                                 item, 0, itemlen);
+        res_offset += itemlen;
     }
+    assert(res_offset == PyUnicode_GET_LENGTH(res));
 
   Done:
     Py_DECREF(fseq);
-    return (PyObject *)res;
+    Py_XDECREF(sep);
+    return res;
 
   onError:
     Py_DECREF(fseq);
+    Py_XDECREF(sep);
     Py_XDECREF(res);
     return NULL;
 }
 
+#define FILL(kind, data, value, start, length) \
+    do { \
+        Py_ssize_t i_ = 0; \
+        assert(kind != PyUnicode_WCHAR_KIND); \
+        switch ((kind)) { \
+        case PyUnicode_1BYTE_KIND: { \
+            unsigned char * to_ = (unsigned char *)((data)) + (start); \
+            memset(to_, (unsigned char)value, length); \
+            break; \
+        } \
+        case PyUnicode_2BYTE_KIND: { \
+            Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \
+            for (; i_ < (length); ++i_, ++to_) *to_ = (value); \
+            break; \
+        } \
+        default: { \
+            Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \
+            for (; i_ < (length); ++i_, ++to_) *to_ = (value); \
+            break; \
+        } \
+        } \
+    } while (0)
+
 static PyUnicodeObject *
 pad(PyUnicodeObject *self,
     Py_ssize_t left,
     Py_ssize_t right,
-    Py_UNICODE fill)
+    Py_UCS4 fill)
 {
-    PyUnicodeObject *u;
+    PyObject *u;
+    Py_UCS4 maxchar;
 
     if (left < 0)
         left = 0;
@@ -6939,22 +8509,28 @@
         return self;
     }
 
-    if (left > PY_SSIZE_T_MAX - self->length ||
-        right > PY_SSIZE_T_MAX - (left + self->length)) {
+    if (left > PY_SSIZE_T_MAX - _PyUnicode_LENGTH(self) ||
+        right > PY_SSIZE_T_MAX - (left + _PyUnicode_LENGTH(self))) {
         PyErr_SetString(PyExc_OverflowError, "padded string is too long");
         return NULL;
     }
-    u = _PyUnicode_New(left + self->length + right);
+    maxchar = PyUnicode_MAX_CHAR_VALUE(self);
+    if (fill > maxchar)
+        maxchar = fill;
+    u = PyUnicode_New(left + _PyUnicode_LENGTH(self) + right, maxchar);
     if (u) {
+        int kind = PyUnicode_KIND(u);
+        void *data = PyUnicode_DATA(u);
         if (left)
-            Py_UNICODE_FILL(u->str, fill, left);
-        Py_UNICODE_COPY(u->str + left, self->str, self->length);
+            FILL(kind, data, fill, 0, left);
         if (right)
-            Py_UNICODE_FILL(u->str + left + self->length, fill, right);
+            FILL(kind, data, fill, left + _PyUnicode_LENGTH(self), right);
+        PyUnicode_CopyCharacters(u, left, (PyObject*)self, 0, _PyUnicode_LENGTH(self));
     }
 
-    return u;
+    return (PyUnicodeObject*)u;
 }
+#undef FILL
 
 PyObject *
 PyUnicode_Splitlines(PyObject *string, int keepends)
@@ -6962,13 +8538,29 @@
     PyObject *list;
 
     string = PyUnicode_FromObject(string);
-    if (string == NULL)
+    if (string == NULL || PyUnicode_READY(string) == -1)
         return NULL;
 
-    list = stringlib_splitlines(
-        (PyObject*) string, PyUnicode_AS_UNICODE(string),
-        PyUnicode_GET_SIZE(string), keepends);
-
+    switch(PyUnicode_KIND(string)) {
+    case PyUnicode_1BYTE_KIND:
+        list = ucs1lib_splitlines(
+            (PyObject*) string, PyUnicode_1BYTE_DATA(string),
+            PyUnicode_GET_LENGTH(string), keepends);
+        break;
+    case PyUnicode_2BYTE_KIND:
+        list = ucs2lib_splitlines(
+            (PyObject*) string, PyUnicode_2BYTE_DATA(string),
+            PyUnicode_GET_LENGTH(string), keepends);
+        break;
+    case PyUnicode_4BYTE_KIND:
+        list = ucs4lib_splitlines(
+            (PyObject*) string, PyUnicode_4BYTE_DATA(string),
+            PyUnicode_GET_LENGTH(string), keepends);
+        break;
+    default:
+        assert(0);
+        list = 0;
+    }
     Py_DECREF(string);
     return list;
 }
@@ -6978,19 +8570,81 @@
       PyUnicodeObject *substring,
       Py_ssize_t maxcount)
 {
+    int kind1, kind2, kind;
+    void *buf1, *buf2;
+    Py_ssize_t len1, len2;
+    PyObject* out;
+
     if (maxcount < 0)
         maxcount = PY_SSIZE_T_MAX;
 
-    if (substring == NULL)
-        return stringlib_split_whitespace(
-            (PyObject*) self,  self->str, self->length, maxcount
-            );
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
 
-    return stringlib_split(
-        (PyObject*) self,  self->str, self->length,
-        substring->str, substring->length,
-        maxcount
-        );
+    if (substring == NULL)
+        switch(PyUnicode_KIND(self)) {
+        case PyUnicode_1BYTE_KIND:
+            return ucs1lib_split_whitespace(
+                (PyObject*) self,  PyUnicode_1BYTE_DATA(self),
+                PyUnicode_GET_LENGTH(self), maxcount
+                );
+        case PyUnicode_2BYTE_KIND:
+            return ucs2lib_split_whitespace(
+                (PyObject*) self,  PyUnicode_2BYTE_DATA(self),
+                PyUnicode_GET_LENGTH(self), maxcount
+                );
+        case PyUnicode_4BYTE_KIND:
+            return ucs4lib_split_whitespace(
+                (PyObject*) self,  PyUnicode_4BYTE_DATA(self),
+                PyUnicode_GET_LENGTH(self), maxcount
+                );
+        default:
+            assert(0);
+            return NULL;
+        }
+
+    if (PyUnicode_READY(substring) == -1)
+        return NULL;
+
+    kind1 = PyUnicode_KIND(self);
+    kind2 = PyUnicode_KIND(substring);
+    kind = kind1 > kind2 ? kind1 : kind2;
+    buf1 = PyUnicode_DATA(self);
+    buf2 = PyUnicode_DATA(substring);
+    if (kind1 != kind)
+        buf1 = _PyUnicode_AsKind((PyObject*)self, kind);
+    if (!buf1)
+        return NULL;
+    if (kind2 != kind)
+        buf2 = _PyUnicode_AsKind((PyObject*)substring, kind);
+    if (!buf2) {
+        if (kind1 != kind) PyMem_Free(buf1);
+        return NULL;
+    }
+    len1 = PyUnicode_GET_LENGTH(self);
+    len2 = PyUnicode_GET_LENGTH(substring);
+
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        out = ucs1lib_split(
+            (PyObject*) self,  buf1, len1, buf2, len2, maxcount);
+        break;
+    case PyUnicode_2BYTE_KIND:
+        out = ucs2lib_split(
+            (PyObject*) self,  buf1, len1, buf2, len2, maxcount);
+        break;
+    case PyUnicode_4BYTE_KIND:
+        out = ucs4lib_split(
+            (PyObject*) self,  buf1, len1, buf2, len2, maxcount);
+        break;
+    default:
+        out = NULL;
+    }
+    if (kind1 != kind)
+        PyMem_Free(buf1);
+    if (kind2 != kind)
+        PyMem_Free(buf2);
+    return out;
 }
 
 static PyObject *
@@ -6998,159 +8652,366 @@
        PyUnicodeObject *substring,
        Py_ssize_t maxcount)
 {
+    int kind1, kind2, kind;
+    void *buf1, *buf2;
+    Py_ssize_t len1, len2;
+    PyObject* out;
+
     if (maxcount < 0)
         maxcount = PY_SSIZE_T_MAX;
 
-    if (substring == NULL)
-        return stringlib_rsplit_whitespace(
-            (PyObject*) self,  self->str, self->length, maxcount
-            );
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
 
-    return stringlib_rsplit(
-        (PyObject*) self,  self->str, self->length,
-        substring->str, substring->length,
-        maxcount
-        );
+    if (substring == NULL)
+        switch(PyUnicode_KIND(self)) {
+        case PyUnicode_1BYTE_KIND:
+            return ucs1lib_rsplit_whitespace(
+                (PyObject*) self,  PyUnicode_1BYTE_DATA(self),
+                PyUnicode_GET_LENGTH(self), maxcount
+                );
+        case PyUnicode_2BYTE_KIND:
+            return ucs2lib_rsplit_whitespace(
+                (PyObject*) self,  PyUnicode_2BYTE_DATA(self),
+                PyUnicode_GET_LENGTH(self), maxcount
+                );
+        case PyUnicode_4BYTE_KIND:
+            return ucs4lib_rsplit_whitespace(
+                (PyObject*) self,  PyUnicode_4BYTE_DATA(self),
+                PyUnicode_GET_LENGTH(self), maxcount
+                );
+        default:
+            assert(0);
+            return NULL;
+        }
+
+    if (PyUnicode_READY(substring) == -1)
+        return NULL;
+
+    kind1 = PyUnicode_KIND(self);
+    kind2 = PyUnicode_KIND(substring);
+    kind = kind1 > kind2 ? kind1 : kind2;
+    buf1 = PyUnicode_DATA(self);
+    buf2 = PyUnicode_DATA(substring);
+    if (kind1 != kind)
+        buf1 = _PyUnicode_AsKind((PyObject*)self, kind);
+    if (!buf1)
+        return NULL;
+    if (kind2 != kind)
+        buf2 = _PyUnicode_AsKind((PyObject*)substring, kind);
+    if (!buf2) {
+        if (kind1 != kind) PyMem_Free(buf1);
+        return NULL;
+    }
+    len1 = PyUnicode_GET_LENGTH(self);
+    len2 = PyUnicode_GET_LENGTH(substring);
+
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        out = ucs1lib_rsplit(
+            (PyObject*) self,  buf1, len1, buf2, len2, maxcount);
+        break;
+    case PyUnicode_2BYTE_KIND:
+        out = ucs2lib_rsplit(
+            (PyObject*) self,  buf1, len1, buf2, len2, maxcount);
+        break;
+    case PyUnicode_4BYTE_KIND:
+        out = ucs4lib_rsplit(
+            (PyObject*) self,  buf1, len1, buf2, len2, maxcount);
+        break;
+    default:
+        out = NULL;
+    }
+    if (kind1 != kind)
+        PyMem_Free(buf1);
+    if (kind2 != kind)
+        PyMem_Free(buf2);
+    return out;
+}
+
+static Py_ssize_t
+anylib_find(int kind, void *buf1, Py_ssize_t len1,
+            void *buf2, Py_ssize_t len2, Py_ssize_t offset)
+{
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        return ucs1lib_find(buf1, len1, buf2, len2, offset);
+    case PyUnicode_2BYTE_KIND:
+        return ucs2lib_find(buf1, len1, buf2, len2, offset);
+    case PyUnicode_4BYTE_KIND:
+        return ucs4lib_find(buf1, len1, buf2, len2, offset);
+    }
+    assert(0);
+    return -1;
+}
+
+static Py_ssize_t
+anylib_count(int kind, void* sbuf, Py_ssize_t slen,
+             void *buf1, Py_ssize_t len1, Py_ssize_t maxcount)
+{
+        switch(kind) {
+        case PyUnicode_1BYTE_KIND:
+            return ucs1lib_count(sbuf, slen, buf1, len1, maxcount);
+        case PyUnicode_2BYTE_KIND:
+            return ucs2lib_count(sbuf, slen, buf1, len1, maxcount);
+        case PyUnicode_4BYTE_KIND:
+            return ucs4lib_count(sbuf, slen, buf1, len1, maxcount);
+        }
+        assert(0);
+        return 0;
 }
 
 static PyObject *
-replace(PyUnicodeObject *self,
-        PyUnicodeObject *str1,
-        PyUnicodeObject *str2,
-        Py_ssize_t maxcount)
+replace(PyObject *self, PyObject *str1,
+        PyObject *str2, Py_ssize_t maxcount)
 {
-    PyUnicodeObject *u;
+    PyObject *u;
+    char *sbuf = PyUnicode_DATA(self);
+    char *buf1 = PyUnicode_DATA(str1);
+    char *buf2 = PyUnicode_DATA(str2);
+    int srelease = 0, release1 = 0, release2 = 0;
+    int skind = PyUnicode_KIND(self);
+    int kind1 = PyUnicode_KIND(str1);
+    int kind2 = PyUnicode_KIND(str2);
+    Py_ssize_t slen = PyUnicode_GET_LENGTH(self);
+    Py_ssize_t len1 = PyUnicode_GET_LENGTH(str1);
+    Py_ssize_t len2 = PyUnicode_GET_LENGTH(str2);
 
     if (maxcount < 0)
         maxcount = PY_SSIZE_T_MAX;
-    else if (maxcount == 0 || self->length == 0)
+    else if (maxcount == 0 || slen == 0)
         goto nothing;
 
-    if (str1->length == str2->length) {
+    if (skind < kind1)
+        /* substring too wide to be present */
+        goto nothing;
+
+    if (len1 == len2) {
         Py_ssize_t i;
         /* same length */
-        if (str1->length == 0)
+        if (len1 == 0)
             goto nothing;
-        if (str1->length == 1) {
+        if (len1 == 1) {
             /* replace characters */
-            Py_UNICODE u1, u2;
-            if (!findchar(self->str, self->length, str1->str[0]))
+            Py_UCS4 u1, u2, maxchar;
+            int mayshrink, rkind;
+            u1 = PyUnicode_READ_CHAR(str1, 0);
+            if (!findchar(sbuf, PyUnicode_KIND(self),
+                          slen, u1, 1))
                 goto nothing;
-            u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
+            u2 = PyUnicode_READ_CHAR(str2, 0);
+            maxchar = PyUnicode_MAX_CHAR_VALUE(self);
+            /* Replacing u1 with u2 may cause a maxchar reduction in the
+               result string. */
+            mayshrink = maxchar > 127;
+            if (u2 > maxchar) {
+                maxchar = u2;
+                mayshrink = 0;
+            }
+            u = PyUnicode_New(slen, maxchar);
             if (!u)
-                return NULL;
-            Py_UNICODE_COPY(u->str, self->str, self->length);
-            u1 = str1->str[0];
-            u2 = str2->str[0];
-            for (i = 0; i < u->length; i++)
-                if (u->str[i] == u1) {
+                goto error;
+            PyUnicode_CopyCharacters(u, 0,
+                                     (PyObject*)self, 0, slen);
+            rkind = PyUnicode_KIND(u);
+            for (i = 0; i < PyUnicode_GET_LENGTH(u); i++)
+                if (PyUnicode_READ(rkind, PyUnicode_DATA(u), i) == u1) {
                     if (--maxcount < 0)
                         break;
-                    u->str[i] = u2;
+                    PyUnicode_WRITE(rkind, PyUnicode_DATA(u), i, u2);
                 }
+            if (mayshrink) {
+                PyObject *tmp = u;
+                u = PyUnicode_FromKindAndData(rkind, PyUnicode_DATA(tmp),
+                                              PyUnicode_GET_LENGTH(tmp));
+                Py_DECREF(tmp);
+            }
         } else {
-            i = stringlib_find(
-                self->str, self->length, str1->str, str1->length, 0
-                );
+            int rkind = skind;
+            char *res;
+            if (kind1 < rkind) {
+                /* widen substring */
+                buf1 = _PyUnicode_AsKind(str1, rkind);
+                if (!buf1) goto error;
+                release1 = 1;
+            }
+            i = anylib_find(rkind, sbuf, slen, buf1, len1, 0);
             if (i < 0)
                 goto nothing;
-            u = (PyUnicodeObject*) PyUnicode_FromUnicode(NULL, self->length);
-            if (!u)
-                return NULL;
-            Py_UNICODE_COPY(u->str, self->str, self->length);
-
+            if (rkind > kind2) {
+                /* widen replacement */
+                buf2 = _PyUnicode_AsKind(str2, rkind);
+                if (!buf2) goto error;
+                release2 = 1;
+            }
+            else if (rkind < kind2) {
+                /* widen self and buf1 */
+                rkind = kind2;
+                if (release1) PyMem_Free(buf1);
+                sbuf = _PyUnicode_AsKind(self, rkind);
+                if (!sbuf) goto error;
+                srelease = 1;
+                buf1 = _PyUnicode_AsKind(str1, rkind);
+                if (!buf1) goto error;
+                release1 = 1;
+            }
+            res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, slen));
+            if (!res) {
+                PyErr_NoMemory();
+                goto error;
+            }
+            memcpy(res, sbuf, PyUnicode_KIND_SIZE(rkind, slen));
             /* change everything in-place, starting with this one */
-            Py_UNICODE_COPY(u->str+i, str2->str, str2->length);
-            i += str1->length;
+            memcpy(res + PyUnicode_KIND_SIZE(rkind, i),
+                   buf2,
+                   PyUnicode_KIND_SIZE(rkind, len2));
+            i += len1;
 
             while ( --maxcount > 0) {
-                i = stringlib_find(self->str+i, self->length-i,
-                                   str1->str, str1->length,
-                                   i);
+                i = anylib_find(rkind, sbuf+PyUnicode_KIND_SIZE(rkind, i),
+                                slen-i,
+                                buf1, len1, i);
                 if (i == -1)
                     break;
-                Py_UNICODE_COPY(u->str+i, str2->str, str2->length);
-                i += str1->length;
+                memcpy(res + PyUnicode_KIND_SIZE(rkind, i),
+                       buf2,
+                       PyUnicode_KIND_SIZE(rkind, len2));
+                i += len1;
             }
+
+            u = PyUnicode_FromKindAndData(rkind, res, slen);
+            PyMem_Free(res);
+            if (!u) goto error;
         }
     } else {
 
-        Py_ssize_t n, i, j;
-        Py_ssize_t product, new_size, delta;
-        Py_UNICODE *p;
+        Py_ssize_t n, i, j, ires;
+        Py_ssize_t product, new_size;
+        int rkind = skind;
+        char *res;
 
-        /* replace strings */
-        n = stringlib_count(self->str, self->length, str1->str, str1->length,
-                            maxcount);
+        if (kind1 < rkind) {
+            buf1 = _PyUnicode_AsKind(str1, rkind);
+            if (!buf1) goto error;
+            release1 = 1;
+        }
+        n = anylib_count(rkind, sbuf, slen, buf1, len1, maxcount);
         if (n == 0)
             goto nothing;
-        /* new_size = self->length + n * (str2->length - str1->length)); */
-        delta = (str2->length - str1->length);
-        if (delta == 0) {
-            new_size = self->length;
-        } else {
-            product = n * (str2->length - str1->length);
-            if ((product / (str2->length - str1->length)) != n) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "replace string is too long");
-                return NULL;
-            }
-            new_size = self->length + product;
-            if (new_size < 0) {
-                PyErr_SetString(PyExc_OverflowError,
-                                "replace string is too long");
-                return NULL;
-            }
+        if (kind2 < rkind) {
+            buf2 = _PyUnicode_AsKind(str2, rkind);
+            if (!buf2) goto error;
+            release2 = 1;
         }
-        u = _PyUnicode_New(new_size);
-        if (!u)
-            return NULL;
-        i = 0;
-        p = u->str;
-        if (str1->length > 0) {
+        else if (kind2 > rkind) {
+            rkind = kind2;
+            sbuf = _PyUnicode_AsKind(self, rkind);
+            if (!sbuf) goto error;
+            srelease = 1;
+            if (release1) PyMem_Free(buf1);
+            buf1 = _PyUnicode_AsKind(str1, rkind);
+            if (!buf1) goto error;
+            release1 = 1;
+        }
+        /* new_size = PyUnicode_GET_LENGTH(self) + n * (PyUnicode_GET_LENGTH(str2) -
+           PyUnicode_GET_LENGTH(str1))); */
+        product = n * (len2-len1);
+        if ((product / (len2-len1)) != n) {
+                PyErr_SetString(PyExc_OverflowError,
+                                "replace string is too long");
+                goto error;
+        }
+        new_size = slen + product;
+        if (new_size < 0 || new_size > (PY_SSIZE_T_MAX >> (rkind-1))) {
+            PyErr_SetString(PyExc_OverflowError,
+                            "replace string is too long");
+            goto error;
+        }
+        res = PyMem_Malloc(PyUnicode_KIND_SIZE(rkind, new_size));
+        if (!res)
+            goto error;
+        ires = i = 0;
+        if (len1 > 0) {
             while (n-- > 0) {
                 /* look for next match */
-                j = stringlib_find(self->str+i, self->length-i,
-                                   str1->str, str1->length,
-                                   i);
+                j = anylib_find(rkind,
+                                sbuf + PyUnicode_KIND_SIZE(rkind, i),
+                                slen-i, buf1, len1, i);
                 if (j == -1)
                     break;
                 else if (j > i) {
                     /* copy unchanged part [i:j] */
-                    Py_UNICODE_COPY(p, self->str+i, j-i);
-                    p += j - i;
+                    memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+                           sbuf + PyUnicode_KIND_SIZE(rkind, i),
+                           PyUnicode_KIND_SIZE(rkind, j-i));
+                    ires += j - i;
                 }
                 /* copy substitution string */
-                if (str2->length > 0) {
-                    Py_UNICODE_COPY(p, str2->str, str2->length);
-                    p += str2->length;
+                if (len2 > 0) {
+                    memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+                           buf2,
+                           PyUnicode_KIND_SIZE(rkind, len2));
+                    ires += len2;
                 }
-                i = j + str1->length;
+                i = j + len1;
             }
-            if (i < self->length)
+            if (i < slen)
                 /* copy tail [i:] */
-                Py_UNICODE_COPY(p, self->str+i, self->length-i);
+                memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+                       sbuf + PyUnicode_KIND_SIZE(rkind, i),
+                       PyUnicode_KIND_SIZE(rkind, slen-i));
         } else {
             /* interleave */
             while (n > 0) {
-                Py_UNICODE_COPY(p, str2->str, str2->length);
-                p += str2->length;
+                memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+                       buf2,
+                       PyUnicode_KIND_SIZE(rkind, len2));
+                ires += len2;
                 if (--n <= 0)
                     break;
-                *p++ = self->str[i++];
+                memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+                       sbuf + PyUnicode_KIND_SIZE(rkind, i),
+                       PyUnicode_KIND_SIZE(rkind, 1));
+                ires++;
+                i++;
             }
-            Py_UNICODE_COPY(p, self->str+i, self->length-i);
+            memcpy(res + PyUnicode_KIND_SIZE(rkind, ires),
+                   sbuf + PyUnicode_KIND_SIZE(rkind, i),
+                   PyUnicode_KIND_SIZE(rkind, slen-i));
         }
+        u = PyUnicode_FromKindAndData(rkind, res, new_size);
     }
-    return (PyObject *) u;
+    if (srelease)
+        PyMem_FREE(sbuf);
+    if (release1)
+        PyMem_FREE(buf1);
+    if (release2)
+        PyMem_FREE(buf2);
+    return u;
 
   nothing:
     /* nothing to replace; return original string (when possible) */
+    if (srelease)
+        PyMem_FREE(sbuf);
+    if (release1)
+        PyMem_FREE(buf1);
+    if (release2)
+        PyMem_FREE(buf2);
     if (PyUnicode_CheckExact(self)) {
         Py_INCREF(self);
         return (PyObject *) self;
     }
-    return PyUnicode_FromUnicode(self->str, self->length);
+    return PyUnicode_FromKindAndData(PyUnicode_KIND(self),
+                                     PyUnicode_DATA(self),
+                                     PyUnicode_GET_LENGTH(self));
+  error:
+    if (srelease && sbuf)
+        PyMem_FREE(sbuf);
+    if (release1 && buf1)
+        PyMem_FREE(buf1);
+    if (release2 && buf2)
+        PyMem_FREE(buf2);
+    return NULL;
 }
 
 /* --- Unicode Object Methods --------------------------------------------- */
@@ -7222,9 +9083,8 @@
 static int
 convert_uc(PyObject *obj, void *addr)
 {
-    Py_UNICODE *fillcharloc = (Py_UNICODE *)addr;
+    Py_UCS4 *fillcharloc = (Py_UCS4 *)addr;
     PyObject *uniobj;
-    Py_UNICODE *unistr;
 
     uniobj = PyUnicode_FromObject(obj);
     if (uniobj == NULL) {
@@ -7232,14 +9092,17 @@
                         "The fill character cannot be converted to Unicode");
         return 0;
     }
-    if (PyUnicode_GET_SIZE(uniobj) != 1) {
+    if (PyUnicode_GET_LENGTH(uniobj) != 1) {
         PyErr_SetString(PyExc_TypeError,
                         "The fill character must be exactly one character long");
         Py_DECREF(uniobj);
         return 0;
     }
-    unistr = PyUnicode_AS_UNICODE(uniobj);
-    *fillcharloc = unistr[0];
+    if (PyUnicode_READY(uniobj)) {
+        Py_DECREF(uniobj);
+        return 0;
+    }
+    *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0);
     Py_DECREF(uniobj);
     return 1;
 }
@@ -7255,17 +9118,20 @@
 {
     Py_ssize_t marg, left;
     Py_ssize_t width;
-    Py_UNICODE fillchar = ' ';
+    Py_UCS4 fillchar = ' ';
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
 
     if (!PyArg_ParseTuple(args, "n|O&:center", &width, convert_uc, &fillchar))
         return NULL;
 
-    if (self->length >= width && PyUnicode_CheckExact(self)) {
+    if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) {
         Py_INCREF(self);
         return (PyObject*) self;
     }
 
-    marg = width - self->length;
+    marg = width - _PyUnicode_LENGTH(self);
     left = marg / 2 + (marg & width & 1);
 
     return (PyObject*) pad(self, left, marg - left, fillchar);
@@ -7297,8 +9163,8 @@
     Py_UNICODE *s1 = str1->str;
     Py_UNICODE *s2 = str2->str;
 
-    len1 = str1->length;
-    len2 = str2->length;
+    len1 = str1->_base._base.length;
+    len2 = str2->_base._base.length;
 
     while (len1 > 0 && len2 > 0) {
         Py_UNICODE c1, c2;
@@ -7323,27 +9189,29 @@
 
 #else
 
+/* This function assumes that str1 and str2 are readied by the caller. */
+
 static int
 unicode_compare(PyUnicodeObject *str1, PyUnicodeObject *str2)
 {
-    register Py_ssize_t len1, len2;
+    int kind1, kind2;
+    void *data1, *data2;
+    Py_ssize_t len1, len2, i;
 
-    Py_UNICODE *s1 = str1->str;
-    Py_UNICODE *s2 = str2->str;
+    kind1 = PyUnicode_KIND(str1);
+    kind2 = PyUnicode_KIND(str2);
+    data1 = PyUnicode_DATA(str1);
+    data2 = PyUnicode_DATA(str2);
+    len1 = PyUnicode_GET_LENGTH(str1);
+    len2 = PyUnicode_GET_LENGTH(str2);
 
-    len1 = str1->length;
-    len2 = str2->length;
-
-    while (len1 > 0 && len2 > 0) {
-        Py_UNICODE c1, c2;
-
-        c1 = *s1++;
-        c2 = *s2++;
+    for (i = 0; i < len1 && i < len2; ++i) {
+        Py_UCS4 c1, c2;
+        c1 = PyUnicode_READ(kind1, data1, i);
+        c2 = PyUnicode_READ(kind2, data2, i);
 
         if (c1 != c2)
             return (c1 < c2) ? -1 : 1;
-
-        len1--; len2--;
     }
 
     return (len1 < len2) ? -1 : (len1 != len2);
@@ -7354,9 +9222,13 @@
 int
 PyUnicode_Compare(PyObject *left, PyObject *right)
 {
-    if (PyUnicode_Check(left) && PyUnicode_Check(right))
+    if (PyUnicode_Check(left) && PyUnicode_Check(right)) {
+        if (PyUnicode_READY(left) == -1 ||
+            PyUnicode_READY(right) == -1)
+            return -1;
         return unicode_compare((PyUnicodeObject *)left,
                                (PyUnicodeObject *)right);
+    }
     PyErr_Format(PyExc_TypeError,
                  "Can't compare %.100s and %.100s",
                  left->ob_type->tp_name,
@@ -7367,17 +9239,23 @@
 int
 PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
 {
-    int i;
-    Py_UNICODE *id;
+    Py_ssize_t i;
+    int kind;
+    void *data;
+    Py_UCS4 chr;
+
     assert(PyUnicode_Check(uni));
-    id = PyUnicode_AS_UNICODE(uni);
+    if (PyUnicode_READY(uni) == -1)
+        return -1;
+    kind = PyUnicode_KIND(uni);
+    data = PyUnicode_DATA(uni);
     /* Compare Unicode string and source character set string */
-    for (i = 0; id[i] && str[i]; i++)
-        if (id[i] != str[i])
-            return ((int)id[i] < (int)str[i]) ? -1 : 1;
+    for (i = 0; (chr = PyUnicode_READ(kind, data, i)) && str[i]; i++)
+        if (chr != str[i])
+            return (chr < (unsigned char)(str[i])) ? -1 : 1;
     /* This check keeps Python strings that end in '\0' from comparing equal
      to C strings identical up to that point. */
-    if (PyUnicode_GET_SIZE(uni) != i || id[i])
+    if (PyUnicode_GET_LENGTH(uni) != i || chr)
         return 1; /* uni is longer */
     if (str[i])
         return -1; /* str is longer */
@@ -7395,7 +9273,11 @@
 
     if (PyUnicode_Check(left) && PyUnicode_Check(right)) {
         PyObject *v;
-        if (PyUnicode_GET_SIZE(left) != PyUnicode_GET_SIZE(right)) {
+        if (PyUnicode_READY(left) == -1 ||
+            PyUnicode_READY(right) == -1)
+            return NULL;
+        if (PyUnicode_GET_LENGTH(left) != PyUnicode_GET_LENGTH(right) ||
+            PyUnicode_KIND(left) != PyUnicode_KIND(right)) {
             if (op == Py_EQ) {
                 Py_INCREF(Py_False);
                 return Py_False;
@@ -7446,6 +9328,9 @@
 PyUnicode_Contains(PyObject *container, PyObject *element)
 {
     PyObject *str, *sub;
+    int kind1, kind2, kind;
+    void *buf1, *buf2;
+    Py_ssize_t len1, len2;
     int result;
 
     /* Coerce the two arguments */
@@ -7456,18 +9341,59 @@
                      element->ob_type->tp_name);
         return -1;
     }
+    if (PyUnicode_READY(sub) == -1)
+        return -1;
 
     str = PyUnicode_FromObject(container);
-    if (!str) {
+    if (!str || PyUnicode_READY(container) == -1) {
         Py_DECREF(sub);
         return -1;
     }
 
-    result = stringlib_contains_obj(str, sub);
+    kind1 = PyUnicode_KIND(str);
+    kind2 = PyUnicode_KIND(sub);
+    kind = kind1 > kind2 ? kind1 : kind2;
+    buf1 = PyUnicode_DATA(str);
+    buf2 = PyUnicode_DATA(sub);
+    if (kind1 != kind)
+        buf1 = _PyUnicode_AsKind((PyObject*)str, kind);
+    if (!buf1) {
+        Py_DECREF(sub);
+        return -1;
+    }
+    if (kind2 != kind)
+        buf2 = _PyUnicode_AsKind((PyObject*)sub, kind);
+    if (!buf2) {
+        Py_DECREF(sub);
+        if (kind1 != kind) PyMem_Free(buf1);
+        return -1;
+    }
+    len1 = PyUnicode_GET_LENGTH(str);
+    len2 = PyUnicode_GET_LENGTH(sub);
+
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        result = ucs1lib_find(buf1, len1, buf2, len2, 0) != -1;
+        break;
+    case PyUnicode_2BYTE_KIND:
+        result = ucs2lib_find(buf1, len1, buf2, len2, 0) != -1;
+        break;
+    case PyUnicode_4BYTE_KIND:
+        result = ucs4lib_find(buf1, len1, buf2, len2, 0) != -1;
+        break;
+    default:
+        result = -1;
+        assert(0);
+    }
 
     Py_DECREF(str);
     Py_DECREF(sub);
 
+    if (kind1 != kind)
+        PyMem_Free(buf1);
+    if (kind2 != kind)
+        PyMem_Free(buf2);
+
     return result;
 }
 
@@ -7476,36 +9402,46 @@
 PyObject *
 PyUnicode_Concat(PyObject *left, PyObject *right)
 {
-    PyUnicodeObject *u = NULL, *v = NULL, *w;
+    PyObject *u = NULL, *v = NULL, *w;
+    Py_UCS4 maxchar;
 
     /* Coerce the two arguments */
-    u = (PyUnicodeObject *)PyUnicode_FromObject(left);
+    u = PyUnicode_FromObject(left);
     if (u == NULL)
         goto onError;
-    v = (PyUnicodeObject *)PyUnicode_FromObject(right);
+    v = PyUnicode_FromObject(right);
     if (v == NULL)
         goto onError;
 
     /* Shortcuts */
-    if (v == unicode_empty) {
+    if (v == (PyObject*)unicode_empty) {
         Py_DECREF(v);
-        return (PyObject *)u;
+        return u;
     }
-    if (u == unicode_empty) {
+    if (u == (PyObject*)unicode_empty) {
         Py_DECREF(u);
-        return (PyObject *)v;
+        return v;
     }
 
+    if (PyUnicode_READY(u) == -1 || PyUnicode_READY(v) == -1)
+        goto onError;
+
+    maxchar = PyUnicode_MAX_CHAR_VALUE(u);
+    if (PyUnicode_MAX_CHAR_VALUE(v) > maxchar)
+        maxchar = PyUnicode_MAX_CHAR_VALUE(v);
+
     /* Concat the two Unicode strings */
-    w = _PyUnicode_New(u->length + v->length);
+    w = PyUnicode_New(
+        PyUnicode_GET_LENGTH(u) + PyUnicode_GET_LENGTH(v),
+        maxchar);
     if (w == NULL)
         goto onError;
-    Py_UNICODE_COPY(w->str, u->str, u->length);
-    Py_UNICODE_COPY(w->str + u->length, v->str, v->length);
-
+    PyUnicode_CopyCharacters(w, 0, u, 0, PyUnicode_GET_LENGTH(u));
+    PyUnicode_CopyCharacters(w, PyUnicode_GET_LENGTH(u), v, 0,
+                             PyUnicode_GET_LENGTH(v));
     Py_DECREF(u);
     Py_DECREF(v);
-    return (PyObject *)w;
+    return w;
 
   onError:
     Py_XDECREF(u);
@@ -7550,17 +9486,65 @@
     Py_ssize_t start = 0;
     Py_ssize_t end = PY_SSIZE_T_MAX;
     PyObject *result;
+    int kind1, kind2, kind;
+    void *buf1, *buf2;
+    Py_ssize_t len1, len2, iresult;
 
     if (!stringlib_parse_args_finds_unicode("count", args, &substring,
                                             &start, &end))
         return NULL;
 
-    ADJUST_INDICES(start, end, self->length);
-    result = PyLong_FromSsize_t(
-        stringlib_count(self->str + start, end - start,
-                        substring->str, substring->length,
-                        PY_SSIZE_T_MAX)
-        );
+    kind1 = PyUnicode_KIND(self);
+    kind2 = PyUnicode_KIND(substring);
+    kind = kind1 > kind2 ? kind1 : kind2;
+    buf1 = PyUnicode_DATA(self);
+    buf2 = PyUnicode_DATA(substring);
+    if (kind1 != kind)
+        buf1 = _PyUnicode_AsKind((PyObject*)self, kind);
+    if (!buf1) {
+        Py_DECREF(substring);
+        return NULL;
+    }
+    if (kind2 != kind)
+        buf2 = _PyUnicode_AsKind((PyObject*)substring, kind);
+    if (!buf2) {
+        Py_DECREF(substring);
+        if (kind1 != kind) PyMem_Free(buf1);
+        return NULL;
+    }
+    len1 = PyUnicode_GET_LENGTH(self);
+    len2 = PyUnicode_GET_LENGTH(substring);
+
+    ADJUST_INDICES(start, end, len1);
+    switch(kind) {
+    case PyUnicode_1BYTE_KIND:
+        iresult = ucs1lib_count(
+            ((Py_UCS1*)buf1) + start, end - start,
+            buf2, len2, PY_SSIZE_T_MAX
+            );
+        break;
+    case PyUnicode_2BYTE_KIND:
+        iresult = ucs2lib_count(
+            ((Py_UCS2*)buf1) + start, end - start,
+            buf2, len2, PY_SSIZE_T_MAX
+            );
+        break;
+    case PyUnicode_4BYTE_KIND:
+        iresult = ucs4lib_count(
+            ((Py_UCS4*)buf1) + start, end - start,
+            buf2, len2, PY_SSIZE_T_MAX
+            );
+        break;
+    default:
+        assert(0); iresult = 0;
+    }
+
+    result = PyLong_FromSsize_t(iresult);
+
+    if (kind1 != kind)
+        PyMem_Free(buf1);
+    if (kind2 != kind)
+        PyMem_Free(buf2);
 
     Py_DECREF(substring);
 
@@ -7603,18 +9587,21 @@
     Py_UNICODE *p;
     Py_UNICODE *q;
     Py_UNICODE *qe;
-    Py_ssize_t i, j, incr;
+    Py_ssize_t i, j, incr, wstr_length;
     PyUnicodeObject *u;
     int tabsize = 8;
 
     if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize))
         return NULL;
 
+    if (PyUnicode_AsUnicodeAndSize((PyObject *)self, &wstr_length) == NULL)
+        return NULL;
+
     /* First pass: determine size of output string */
     i = 0; /* chars up to and including most recent \n or \r */
     j = 0; /* chars since most recent \n or \r (use in tab calculations) */
-    e = self->str + self->length; /* end of input */
-    for (p = self->str; p < e; p++)
+    e = _PyUnicode_WSTR(self) + wstr_length; /* end of input */
+    for (p = _PyUnicode_WSTR(self); p < e; p++)
         if (*p == '\t') {
             if (tabsize > 0) {
                 incr = tabsize - (j % tabsize); /* cannot overflow */
@@ -7644,10 +9631,10 @@
         return NULL;
 
     j = 0; /* same as in first pass */
-    q = u->str; /* next output char */
-    qe = u->str + u->length; /* end of output */
+    q = _PyUnicode_WSTR(u); /* next output char */
+    qe = _PyUnicode_WSTR(u) + PyUnicode_GET_SIZE(u); /* end of output */
 
-    for (p = self->str; p < e; p++)
+    for (p = _PyUnicode_WSTR(self); p < e; p++)
         if (*p == '\t') {
             if (tabsize > 0) {
                 i = tabsize - (j % tabsize);
@@ -7668,6 +9655,10 @@
                 j = 0;
         }
 
+    if (PyUnicode_READY(u) == -1) {
+        Py_DECREF(u);
+        return NULL;
+    }
     return (PyObject*) u;
 
   overflow2:
@@ -7687,7 +9678,7 @@
 Return -1 on failure.");
 
 static PyObject *
-unicode_find(PyUnicodeObject *self, PyObject *args)
+unicode_find(PyObject *self, PyObject *args)
 {
     PyUnicodeObject *substring;
     Py_ssize_t start;
@@ -7698,26 +9689,38 @@
                                             &start, &end))
         return NULL;
 
-    result = stringlib_find_slice(
-        PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
-        PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
-        start, end
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    if (PyUnicode_READY(substring) == -1)
+        return NULL;
+
+    result = any_find_slice(
+        ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice,
+        self, (PyObject*)substring, start, end
         );
 
     Py_DECREF(substring);
 
+    if (result == -2)
+        return NULL;
+
     return PyLong_FromSsize_t(result);
 }
 
 static PyObject *
 unicode_getitem(PyUnicodeObject *self, Py_ssize_t index)
 {
-    if (index < 0 || index >= self->length) {
+    Py_UCS4 ch;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    if (index < 0 || index >= _PyUnicode_LENGTH(self)) {
         PyErr_SetString(PyExc_IndexError, "string index out of range");
         return NULL;
     }
 
-    return (PyObject*) PyUnicode_FromUnicode(&self->str[index], 1);
+    ch = PyUnicode_READ(PyUnicode_KIND(self), PyUnicode_DATA(self), index);
+    return PyUnicode_FromOrdinal(ch);
 }
 
 /* Believe it or not, this produces the same value for ASCII strings
@@ -7726,22 +9729,48 @@
 unicode_hash(PyUnicodeObject *self)
 {
     Py_ssize_t len;
-    Py_UNICODE *p;
     Py_uhash_t x;
 
-    if (self->hash != -1)
-        return self->hash;
-    len = Py_SIZE(self);
-    p = self->str;
-    x = (Py_uhash_t)*p << 7;
-    while (--len >= 0)
-        x = (1000003U*x) ^ (Py_uhash_t)*p++;
-    x ^= (Py_uhash_t)Py_SIZE(self);
+    if (_PyUnicode_HASH(self) != -1)
+        return _PyUnicode_HASH(self);
+    if (PyUnicode_READY(self) == -1)
+        return -1;
+    len = PyUnicode_GET_LENGTH(self);
+
+    /* The hash function as a macro, gets expanded three times below. */
+#define HASH(P) \
+    x = (Py_uhash_t)*P << 7; \
+    while (--len >= 0) \
+        x = (1000003*x) ^ (Py_uhash_t)*P++;
+
+    switch (PyUnicode_KIND(self)) {
+    case PyUnicode_1BYTE_KIND: {
+        const unsigned char *c = PyUnicode_1BYTE_DATA(self);
+        HASH(c);
+        break;
+    }
+    case PyUnicode_2BYTE_KIND: {
+        const Py_UCS2 *s = PyUnicode_2BYTE_DATA(self);
+        HASH(s);
+        break;
+    }
+    default: {
+        Py_UCS4 *l;
+        assert(PyUnicode_KIND(self) == PyUnicode_4BYTE_KIND &&
+               "Impossible switch case in unicode_hash");
+        l = PyUnicode_4BYTE_DATA(self);
+        HASH(l);
+        break;
+    }
+    }
+    x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self);
+
     if (x == -1)
         x = -2;
-    self->hash = x;
+    _PyUnicode_HASH(self) = x;
     return x;
 }
+#undef HASH
 
 PyDoc_STRVAR(index__doc__,
              "S.index(sub[, start[, end]]) -> int\n\
@@ -7749,7 +9778,7 @@
 Like S.find() but raise ValueError when the substring is not found.");
 
 static PyObject *
-unicode_index(PyUnicodeObject *self, PyObject *args)
+unicode_index(PyObject *self, PyObject *args)
 {
     Py_ssize_t result;
     PyUnicodeObject *substring;
@@ -7760,14 +9789,21 @@
                                             &start, &end))
         return NULL;
 
-    result = stringlib_find_slice(
-        PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
-        PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
-        start, end
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    if (PyUnicode_READY(substring) == -1)
+        return NULL;
+
+    result = any_find_slice(
+        ucs1lib_find_slice, ucs2lib_find_slice, ucs4lib_find_slice,
+        self, (PyObject*)substring, start, end
         );
 
     Py_DECREF(substring);
 
+    if (result == -2)
+        return NULL;
+
     if (result < 0) {
         PyErr_SetString(PyExc_ValueError, "substring not found");
         return NULL;
@@ -7785,22 +9821,29 @@
 static PyObject*
 unicode_islower(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
     int cased;
 
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
+
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1)
-        return PyBool_FromLong(Py_UNICODE_ISLOWER(*p));
+    if (length == 1)
+        return PyBool_FromLong(
+            Py_UNICODE_ISLOWER(PyUnicode_READ(kind, data, 0)));
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (length == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
     cased = 0;
-    while (p < e) {
-        const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+    for (i = 0; i < length; i++) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
 
         if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch))
             return PyBool_FromLong(0);
@@ -7819,22 +9862,29 @@
 static PyObject*
 unicode_isupper(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
     int cased;
 
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
+
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1)
-        return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0);
+    if (length == 1)
+        return PyBool_FromLong(
+            Py_UNICODE_ISUPPER(PyUnicode_READ(kind, data, 0)) != 0);
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (length == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
     cased = 0;
-    while (p < e) {
-        const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+    for (i = 0; i < length; i++) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
 
         if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch))
             return PyBool_FromLong(0);
@@ -7855,24 +9905,32 @@
 static PyObject*
 unicode_istitle(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
     int cased, previous_is_cased;
 
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
+
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1)
-        return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) ||
-                               (Py_UNICODE_ISUPPER(*p) != 0));
+    if (length == 1) {
+        Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
+        return PyBool_FromLong((Py_UNICODE_ISTITLE(ch) != 0) ||
+                               (Py_UNICODE_ISUPPER(ch) != 0));
+    }
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (length == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
     cased = 0;
     previous_is_cased = 0;
-    while (p < e) {
-        const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+    for (i = 0; i < length; i++) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
 
         if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) {
             if (previous_is_cased)
@@ -7901,21 +9959,27 @@
 static PyObject*
 unicode_isspace(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
 
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1 &&
-        Py_UNICODE_ISSPACE(*p))
-        return PyBool_FromLong(1);
+    if (length == 1)
+        return PyBool_FromLong(
+            Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, 0)));
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (length == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
-    while (p < e) {
-        const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+    for (i = 0; i < length; i++) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
         if (!Py_UNICODE_ISSPACE(ch))
             return PyBool_FromLong(0);
     }
@@ -7931,21 +9995,27 @@
 static PyObject*
 unicode_isalpha(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
 
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1 &&
-        Py_UNICODE_ISALPHA(*p))
-        return PyBool_FromLong(1);
+    if (length == 1)
+        return PyBool_FromLong(
+            Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, 0)));
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (length == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
-    while (p < e) {
-        if (!Py_UNICODE_ISALPHA(_Py_UNICODE_NEXT(p, e)))
+    for (i = 0; i < length; i++) {
+        if (!Py_UNICODE_ISALPHA(PyUnicode_READ(kind, data, i)))
             return PyBool_FromLong(0);
     }
     return PyBool_FromLong(1);
@@ -7960,21 +10030,29 @@
 static PyObject*
 unicode_isalnum(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    int kind;
+    void *data;
+    Py_ssize_t len, i;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
+    len = PyUnicode_GET_LENGTH(self);
 
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1 &&
-        Py_UNICODE_ISALNUM(*p))
-        return PyBool_FromLong(1);
+    if (len == 1) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
+        return PyBool_FromLong(Py_UNICODE_ISALNUM(ch));
+    }
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (len == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
-    while (p < e) {
-        const Py_UCS4 ch = _Py_UNICODE_NEXT(p, e);
+    for (i = 0; i < len; i++) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, i);
         if (!Py_UNICODE_ISALNUM(ch))
             return PyBool_FromLong(0);
     }
@@ -7990,21 +10068,27 @@
 static PyObject*
 unicode_isdecimal(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
 
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1 &&
-        Py_UNICODE_ISDECIMAL(*p))
-        return PyBool_FromLong(1);
+    if (length == 1)
+        return PyBool_FromLong(
+            Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, 0)));
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (length == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
-    while (p < e) {
-        if (!Py_UNICODE_ISDECIMAL(_Py_UNICODE_NEXT(p, e)))
+    for (i = 0; i < length; i++) {
+        if (!Py_UNICODE_ISDECIMAL(PyUnicode_READ(kind, data, i)))
             return PyBool_FromLong(0);
     }
     return PyBool_FromLong(1);
@@ -8019,21 +10103,28 @@
 static PyObject*
 unicode_isdigit(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
 
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1 &&
-        Py_UNICODE_ISDIGIT(*p))
-        return PyBool_FromLong(1);
+    if (length == 1) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, 0);
+        return PyBool_FromLong(Py_UNICODE_ISDIGIT(ch));
+    }
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (length == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
-    while (p < e) {
-        if (!Py_UNICODE_ISDIGIT(_Py_UNICODE_NEXT(p, e)))
+    for (i = 0; i < length; i++) {
+        if (!Py_UNICODE_ISDIGIT(PyUnicode_READ(kind, data, i)))
             return PyBool_FromLong(0);
     }
     return PyBool_FromLong(1);
@@ -8048,21 +10139,27 @@
 static PyObject*
 unicode_isnumeric(PyUnicodeObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
 
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1 &&
-        Py_UNICODE_ISNUMERIC(*p))
-        return PyBool_FromLong(1);
+    if (length == 1)
+        return PyBool_FromLong(
+            Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, 0)));
 
     /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (length == 0)
         return PyBool_FromLong(0);
 
-    e = p + PyUnicode_GET_SIZE(self);
-    while (p < e) {
-        if (!Py_UNICODE_ISNUMERIC(_Py_UNICODE_NEXT(p, e)))
+    for (i = 0; i < length; i++) {
+        if (!Py_UNICODE_ISNUMERIC(PyUnicode_READ(kind, data, i)))
             return PyBool_FromLong(0);
     }
     return PyBool_FromLong(1);
@@ -8071,13 +10168,21 @@
 int
 PyUnicode_IsIdentifier(PyObject *self)
 {
-    const Py_UNICODE *p = PyUnicode_AS_UNICODE((PyUnicodeObject*)self);
-    const Py_UNICODE *e;
+    int kind;
+    void *data;
+    Py_ssize_t i;
     Py_UCS4 first;
 
-    /* Special case for empty strings */
-    if (PyUnicode_GET_SIZE(self) == 0)
+    if (PyUnicode_READY(self) == -1) {
+        Py_FatalError("identifier not ready");
         return 0;
+    }
+
+    /* Special case for empty strings */
+    if (PyUnicode_GET_LENGTH(self) == 0)
+        return 0;
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
 
     /* PEP 3131 says that the first character must be in
        XID_Start and subsequent characters in XID_Continue,
@@ -8087,13 +10192,12 @@
        definition of XID_Start and XID_Continue, it is sufficient
        to check just for these, except that _ must be allowed
        as starting an identifier.  */
-    e = p + PyUnicode_GET_SIZE(self);
-    first = _Py_UNICODE_NEXT(p, e);
+    first = PyUnicode_READ(kind, data, 0);
     if (!_PyUnicode_IsXidStart(first) && first != 0x5F /* LOW LINE */)
         return 0;
 
-    while (p < e)
-        if (!_PyUnicode_IsXidContinue(_Py_UNICODE_NEXT(p, e)))
+    for (i = 0; i < PyUnicode_GET_LENGTH(self); i++)
+        if (!_PyUnicode_IsXidContinue(PyUnicode_READ(kind, data, i)))
             return 0;
     return 1;
 }
@@ -8119,17 +10223,23 @@
 static PyObject*
 unicode_isprintable(PyObject *self)
 {
-    register const Py_UNICODE *p = PyUnicode_AS_UNICODE(self);
-    register const Py_UNICODE *e;
+    Py_ssize_t i, length;
+    int kind;
+    void *data;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    length = PyUnicode_GET_LENGTH(self);
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
 
     /* Shortcut for single character strings */
-    if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISPRINTABLE(*p)) {
-        Py_RETURN_TRUE;
-    }
+    if (length == 1)
+        return PyBool_FromLong(
+            Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, 0)));
 
-    e = p + PyUnicode_GET_SIZE(self);
-    while (p < e) {
-        if (!Py_UNICODE_ISPRINTABLE(_Py_UNICODE_NEXT(p, e))) {
+    for (i = 0; i < length; i++) {
+        if (!Py_UNICODE_ISPRINTABLE(PyUnicode_READ(kind, data, i))) {
             Py_RETURN_FALSE;
         }
     }
@@ -8151,7 +10261,9 @@
 static Py_ssize_t
 unicode_length(PyUnicodeObject *self)
 {
-    return self->length;
+    if (PyUnicode_READY(self) == -1)
+        return -1;
+    return PyUnicode_GET_LENGTH(self);
 }
 
 PyDoc_STRVAR(ljust__doc__,
@@ -8164,17 +10276,20 @@
 unicode_ljust(PyUnicodeObject *self, PyObject *args)
 {
     Py_ssize_t width;
-    Py_UNICODE fillchar = ' ';
+    Py_UCS4 fillchar = ' ';
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
 
     if (!PyArg_ParseTuple(args, "n|O&:ljust", &width, convert_uc, &fillchar))
         return NULL;
 
-    if (self->length >= width && PyUnicode_CheckExact(self)) {
+    if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) {
         Py_INCREF(self);
         return (PyObject*) self;
     }
 
-    return (PyObject*) pad(self, 0, width - self->length, fillchar);
+    return (PyObject*) pad(self, 0, width - _PyUnicode_LENGTH(self), fillchar);
 }
 
 PyDoc_STRVAR(lower__doc__,
@@ -8201,17 +10316,25 @@
 PyObject *
 _PyUnicode_XStrip(PyUnicodeObject *self, int striptype, PyObject *sepobj)
 {
-    Py_UNICODE *s = PyUnicode_AS_UNICODE(self);
-    Py_ssize_t len = PyUnicode_GET_SIZE(self);
-    Py_UNICODE *sep = PyUnicode_AS_UNICODE(sepobj);
-    Py_ssize_t seplen = PyUnicode_GET_SIZE(sepobj);
-    Py_ssize_t i, j;
+    void *data;
+    int kind;
+    Py_ssize_t i, j, len;
+    BLOOM_MASK sepmask;
 
-    BLOOM_MASK sepmask = make_bloom_mask(sep, seplen);
+    if (PyUnicode_READY(self) == -1 || PyUnicode_READY(sepobj) == -1)
+        return NULL;
+
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
+    len = PyUnicode_GET_LENGTH(self);
+    sepmask = make_bloom_mask(PyUnicode_KIND(sepobj),
+                              PyUnicode_DATA(sepobj),
+                              PyUnicode_GET_LENGTH(sepobj));
 
     i = 0;
     if (striptype != RIGHTSTRIP) {
-        while (i < len && BLOOM_MEMBER(sepmask, s[i], sep, seplen)) {
+        while (i < len &&
+               BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, i), sepobj)) {
             i++;
         }
     }
@@ -8220,7 +10343,8 @@
     if (striptype != LEFTSTRIP) {
         do {
             j--;
-        } while (j >= i && BLOOM_MEMBER(sepmask, s[j], sep, seplen));
+        } while (j >= i &&
+                 BLOOM_MEMBER(sepmask, PyUnicode_READ(kind, data, j), sepobj));
         j++;
     }
 
@@ -8229,19 +10353,85 @@
         return (PyObject*)self;
     }
     else
-        return PyUnicode_FromUnicode(s+i, j-i);
+        return PyUnicode_Substring((PyObject*)self, i, j);
 }
 
+/* Assumes an already ready self string. */
+
+static PyObject *
+substring(PyUnicodeObject *self, Py_ssize_t start, Py_ssize_t len)
+{
+    const int kind = PyUnicode_KIND(self);
+    void *data = PyUnicode_DATA(self);
+    Py_UCS4 maxchar = 0;
+    Py_ssize_t i;
+    PyObject *unicode;
+
+    if (start < 0 || len < 0 || (start + len) > PyUnicode_GET_LENGTH(self)) {
+        PyErr_BadInternalCall();
+        return NULL;
+    }
+
+    if (len == PyUnicode_GET_LENGTH(self) && PyUnicode_CheckExact(self)) {
+        Py_INCREF(self);
+        return (PyObject*)self;
+    }
+
+    for (i = 0; i < len; ++i) {
+        const Py_UCS4 ch = PyUnicode_READ(kind, data, start + i);
+        if (ch > maxchar)
+            maxchar = ch;
+    }
+
+    unicode = PyUnicode_New(len, maxchar);
+    if (unicode == NULL)
+        return NULL;
+    PyUnicode_CopyCharacters(unicode, 0,
+                             (PyObject*)self, start, len);
+    return unicode;
+}
+
+PyObject*
+PyUnicode_Substring(PyObject *self, Py_ssize_t start, Py_ssize_t end)
+{
+    unsigned char *data;
+    int kind;
+
+    if (start == 0 && end == PyUnicode_GET_LENGTH(self)
+        && PyUnicode_CheckExact(self))
+    {
+        Py_INCREF(self);
+        return (PyObject *)self;
+    }
+
+    if ((end - start) == 1)
+        return unicode_getitem((PyUnicodeObject*)self, start);
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_1BYTE_DATA(self);
+    return PyUnicode_FromKindAndData(kind, data + PyUnicode_KIND_SIZE(kind, start),
+                                     end-start);
+}
 
 static PyObject *
 do_strip(PyUnicodeObject *self, int striptype)
 {
-    Py_UNICODE *s = PyUnicode_AS_UNICODE(self);
-    Py_ssize_t len = PyUnicode_GET_SIZE(self), i, j;
+    int kind;
+    void *data;
+    Py_ssize_t len, i, j;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+
+    kind = PyUnicode_KIND(self);
+    data = PyUnicode_DATA(self);
+    len = PyUnicode_GET_LENGTH(self);
 
     i = 0;
     if (striptype != RIGHTSTRIP) {
-        while (i < len && Py_UNICODE_ISSPACE(s[i])) {
+        while (i < len && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) {
             i++;
         }
     }
@@ -8250,7 +10440,7 @@
     if (striptype != LEFTSTRIP) {
         do {
             j--;
-        } while (j >= i && Py_UNICODE_ISSPACE(s[j]));
+        } while (j >= i && Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, j)));
         j++;
     }
 
@@ -8259,7 +10449,7 @@
         return (PyObject*)self;
     }
     else
-        return PyUnicode_FromUnicode(s+i, j-i);
+        return substring(self, i, j-i);
 }
 
 
@@ -8339,9 +10529,8 @@
 unicode_repeat(PyUnicodeObject *str, Py_ssize_t len)
 {
     PyUnicodeObject *u;
-    Py_UNICODE *p;
-    Py_ssize_t nchars;
-    size_t nbytes;
+    Py_ssize_t nchars, n;
+    size_t nbytes, char_size;
 
     if (len < 1) {
         Py_INCREF(unicode_empty);
@@ -8354,35 +10543,46 @@
         return (PyObject*) str;
     }
 
+    if (PyUnicode_READY(str) == -1)
+        return NULL;
+
     /* ensure # of chars needed doesn't overflow int and # of bytes
      * needed doesn't overflow size_t
      */
-    nchars = len * str->length;
-    if (nchars / len != str->length) {
+    nchars = len * PyUnicode_GET_LENGTH(str);
+    if (nchars / len != PyUnicode_GET_LENGTH(str)) {
         PyErr_SetString(PyExc_OverflowError,
                         "repeated string is too long");
         return NULL;
     }
-    nbytes = (nchars + 1) * sizeof(Py_UNICODE);
-    if (nbytes / sizeof(Py_UNICODE) != (size_t)(nchars + 1)) {
+    char_size = PyUnicode_CHARACTER_SIZE(str);
+    nbytes = (nchars + 1) * char_size;
+    if (nbytes / char_size != (size_t)(nchars + 1)) {
         PyErr_SetString(PyExc_OverflowError,
                         "repeated string is too long");
         return NULL;
     }
-    u = _PyUnicode_New(nchars);
+    u = (PyUnicodeObject *)PyUnicode_New(nchars, PyUnicode_MAX_CHAR_VALUE(str));
     if (!u)
         return NULL;
 
-    p = u->str;
-
-    if (str->length == 1) {
-        Py_UNICODE_FILL(p, str->str[0], len);
-    } else {
-        Py_ssize_t done = str->length; /* number of characters copied this far */
-        Py_UNICODE_COPY(p, str->str, str->length);
+    if (PyUnicode_GET_LENGTH(str) == 1) {
+        const int kind = PyUnicode_KIND(str);
+        const Py_UCS4 fill_char = PyUnicode_READ(kind, PyUnicode_DATA(str), 0);
+        void *to = PyUnicode_DATA(u);
+        for (n = 0; n < len; ++n)
+            PyUnicode_WRITE(kind, to, n, fill_char);
+    }
+    else {
+        /* number of characters copied this far */
+        Py_ssize_t done = PyUnicode_GET_LENGTH(str);
+        const Py_ssize_t char_size = PyUnicode_CHARACTER_SIZE(str);
+        char *to = (char *) PyUnicode_DATA(u);
+        Py_MEMCPY(to, PyUnicode_DATA(str),
+                  PyUnicode_GET_LENGTH(str) * char_size);
         while (done < nchars) {
-            Py_ssize_t n = (done <= nchars-done) ? done : nchars-done;
-            Py_UNICODE_COPY(p+done, p, n);
+            n = (done <= nchars-done) ? done : nchars-done;
+            Py_MEMCPY(to + (done * char_size), to, n * char_size);
             done += n;
         }
     }
@@ -8402,23 +10602,20 @@
     PyObject *result;
 
     self = PyUnicode_FromObject(obj);
-    if (self == NULL)
+    if (self == NULL || PyUnicode_READY(obj) == -1)
         return NULL;
     str1 = PyUnicode_FromObject(subobj);
-    if (str1 == NULL) {
+    if (str1 == NULL || PyUnicode_READY(obj) == -1) {
         Py_DECREF(self);
         return NULL;
     }
     str2 = PyUnicode_FromObject(replobj);
-    if (str2 == NULL) {
+    if (str2 == NULL || PyUnicode_READY(obj)) {
         Py_DECREF(self);
         Py_DECREF(str1);
         return NULL;
     }
-    result = replace((PyUnicodeObject *)self,
-                     (PyUnicodeObject *)str1,
-                     (PyUnicodeObject *)str2,
-                     maxcount);
+    result = replace(self, str1, str2, maxcount);
     Py_DECREF(self);
     Py_DECREF(str1);
     Py_DECREF(str2);
@@ -8433,20 +10630,22 @@
 given, only the first count occurrences are replaced.");
 
 static PyObject*
-unicode_replace(PyUnicodeObject *self, PyObject *args)
+unicode_replace(PyObject *self, PyObject *args)
 {
-    PyUnicodeObject *str1;
-    PyUnicodeObject *str2;
+    PyObject *str1;
+    PyObject *str2;
     Py_ssize_t maxcount = -1;
     PyObject *result;
 
     if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount))
         return NULL;
-    str1 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str1);
-    if (str1 == NULL)
+    if (!PyUnicode_READY(self) == -1)
         return NULL;
-    str2 = (PyUnicodeObject *)PyUnicode_FromObject((PyObject *)str2);
-    if (str2 == NULL) {
+    str1 = PyUnicode_FromObject(str1);
+    if (str1 == NULL || PyUnicode_READY(str1) == -1)
+        return NULL;
+    str2 = PyUnicode_FromObject(str2);
+    if (str2 == NULL || PyUnicode_READY(str1) == -1) {
         Py_DECREF(str1);
         return NULL;
     }
@@ -8462,148 +10661,149 @@
 unicode_repr(PyObject *unicode)
 {
     PyObject *repr;
-    Py_UNICODE *p;
-    Py_UNICODE *s = PyUnicode_AS_UNICODE(unicode);
-    Py_ssize_t size = PyUnicode_GET_SIZE(unicode);
+    Py_ssize_t isize;
+    Py_ssize_t osize, squote, dquote, i, o;
+    Py_UCS4 max, quote;
+    int ikind, okind;
+    void *idata, *odata;
 
-    /* XXX(nnorwitz): rather than over-allocating, it would be
-       better to choose a different scheme.  Perhaps scan the
-       first N-chars of the string and allocate based on that size.
-    */
-    /* Initial allocation is based on the longest-possible unichr
-       escape.
-
-       In wide (UTF-32) builds '\U00xxxxxx' is 10 chars per source
-       unichr, so in this case it's the longest unichr escape. In
-       narrow (UTF-16) builds this is five chars per source unichr
-       since there are two unichrs in the surrogate pair, so in narrow
-       (UTF-16) builds it's not the longest unichr escape.
-
-       In wide or narrow builds '\uxxxx' is 6 chars per source unichr,
-       so in the narrow (UTF-16) build case it's the longest unichr
-       escape.
-    */
-
-    repr = PyUnicode_FromUnicode(NULL,
-                                 2 /* quotes */
-#ifdef Py_UNICODE_WIDE
-                                 + 10*size
-#else
-                                 + 6*size
-#endif
-                                 + 1);
-    if (repr == NULL)
+    if (PyUnicode_READY(unicode) == -1)
         return NULL;
 
-    p = PyUnicode_AS_UNICODE(repr);
+    isize = PyUnicode_GET_LENGTH(unicode);
+    idata = PyUnicode_DATA(unicode);
 
-    /* Add quote */
-    *p++ = (findchar(s, size, '\'') &&
-            !findchar(s, size, '"')) ? '"' : '\'';
-    while (size-- > 0) {
-        Py_UNICODE ch = *s++;
+    /* Compute length of output, quote characters, and
+       maximum character */
+    osize = 2; /* quotes */
+    max = 127;
+    squote = dquote = 0;
+    ikind = PyUnicode_KIND(unicode);
+    for (i = 0; i < isize; i++) {
+        Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);
+        switch (ch) {
+        case '\'': squote++; osize++; break;
+        case '"':  dquote++; osize++; break;
+        case '\\': case '\t': case '\r': case '\n':
+            osize += 2; break;
+        default:
+            /* Fast-path ASCII */
+            if (ch < ' ' || ch == 0x7f)
+                osize += 4; /* \xHH */
+            else if (ch < 0x7f)
+                osize++;
+            else if (Py_UNICODE_ISPRINTABLE(ch)) {
+                osize++;
+                max = ch > max ? ch : max;
+            }
+            else if (ch < 0x100)
+                osize += 4; /* \xHH */
+            else if (ch < 0x10000)
+                osize += 6; /* \uHHHH */
+            else
+                osize += 10; /* \uHHHHHHHH */
+        }
+    }
+
+    quote = '\'';
+    if (squote) {
+        if (dquote)
+            /* Both squote and dquote present. Use squote,
+               and escape them */
+            osize += squote;
+        else
+            quote = '"';
+    }
+
+    repr = PyUnicode_New(osize, max);
+    if (repr == NULL)
+        return NULL;
+    okind = PyUnicode_KIND(repr);
+    odata = PyUnicode_DATA(repr);
+
+    PyUnicode_WRITE(okind, odata, 0, quote);
+    PyUnicode_WRITE(okind, odata, osize-1, quote);
+
+    for (i = 0, o = 1; i < isize; i++) {
+        Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);
 
         /* Escape quotes and backslashes */
-        if ((ch == PyUnicode_AS_UNICODE(repr)[0]) || (ch == '\\')) {
-            *p++ = '\\';
-            *p++ = ch;
+        if ((ch == quote) || (ch == '\\')) {
+            PyUnicode_WRITE(okind, odata, o++, '\\');
+            PyUnicode_WRITE(okind, odata, o++, ch);
             continue;
         }
 
         /* Map special whitespace to '\t', \n', '\r' */
         if (ch == '\t') {
-            *p++ = '\\';
-            *p++ = 't';
+            PyUnicode_WRITE(okind, odata, o++, '\\');
+            PyUnicode_WRITE(okind, odata, o++, 't');
         }
         else if (ch == '\n') {
-            *p++ = '\\';
-            *p++ = 'n';
+            PyUnicode_WRITE(okind, odata, o++, '\\');
+            PyUnicode_WRITE(okind, odata, o++, 'n');
         }
         else if (ch == '\r') {
-            *p++ = '\\';
-            *p++ = 'r';
+            PyUnicode_WRITE(okind, odata, o++, '\\');
+            PyUnicode_WRITE(okind, odata, o++, 'r');
         }
 
         /* Map non-printable US ASCII to '\xhh' */
         else if (ch < ' ' || ch == 0x7F) {
-            *p++ = '\\';
-            *p++ = 'x';
-            *p++ = hexdigits[(ch >> 4) & 0x000F];
-            *p++ = hexdigits[ch & 0x000F];
+            PyUnicode_WRITE(okind, odata, o++, '\\');
+            PyUnicode_WRITE(okind, odata, o++, 'x');
+            PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]);
+            PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]);
         }
 
         /* Copy ASCII characters as-is */
         else if (ch < 0x7F) {
-            *p++ = ch;
+            PyUnicode_WRITE(okind, odata, o++, ch);
         }
 
         /* Non-ASCII characters */
         else {
-            Py_UCS4 ucs = ch;
-
-#ifndef Py_UNICODE_WIDE
-            Py_UNICODE ch2 = 0;
-            /* Get code point from surrogate pair */
-            if (size > 0) {
-                ch2 = *s;
-                if (ch >= 0xD800 && ch < 0xDC00 && ch2 >= 0xDC00
-                    && ch2 <= 0xDFFF) {
-                    ucs = (((ch & 0x03FF) << 10) | (ch2 & 0x03FF))
-                        + 0x00010000;
-                    s++;
-                    size--;
-                }
-            }
-#endif
             /* Map Unicode whitespace and control characters
                (categories Z* and C* except ASCII space)
             */
-            if (!Py_UNICODE_ISPRINTABLE(ucs)) {
+            if (!Py_UNICODE_ISPRINTABLE(ch)) {
                 /* Map 8-bit characters to '\xhh' */
-                if (ucs <= 0xff) {
-                    *p++ = '\\';
-                    *p++ = 'x';
-                    *p++ = hexdigits[(ch >> 4) & 0x000F];
-                    *p++ = hexdigits[ch & 0x000F];
+                if (ch <= 0xff) {
+                    PyUnicode_WRITE(okind, odata, o++, '\\');
+                    PyUnicode_WRITE(okind, odata, o++, 'x');
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0x000F]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0x000F]);
                 }
                 /* Map 21-bit characters to '\U00xxxxxx' */
-                else if (ucs >= 0x10000) {
-                    *p++ = '\\';
-                    *p++ = 'U';
-                    *p++ = hexdigits[(ucs >> 28) & 0x0000000F];
-                    *p++ = hexdigits[(ucs >> 24) & 0x0000000F];
-                    *p++ = hexdigits[(ucs >> 20) & 0x0000000F];
-                    *p++ = hexdigits[(ucs >> 16) & 0x0000000F];
-                    *p++ = hexdigits[(ucs >> 12) & 0x0000000F];
-                    *p++ = hexdigits[(ucs >> 8) & 0x0000000F];
-                    *p++ = hexdigits[(ucs >> 4) & 0x0000000F];
-                    *p++ = hexdigits[ucs & 0x0000000F];
+                else if (ch >= 0x10000) {
+                    PyUnicode_WRITE(okind, odata, o++, '\\');
+                    PyUnicode_WRITE(okind, odata, o++, 'U');
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 28) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 24) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 20) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 16) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]);
                 }
                 /* Map 16-bit characters to '\uxxxx' */
                 else {
-                    *p++ = '\\';
-                    *p++ = 'u';
-                    *p++ = hexdigits[(ucs >> 12) & 0x000F];
-                    *p++ = hexdigits[(ucs >> 8) & 0x000F];
-                    *p++ = hexdigits[(ucs >> 4) & 0x000F];
-                    *p++ = hexdigits[ucs & 0x000F];
+                    PyUnicode_WRITE(okind, odata, o++, '\\');
+                    PyUnicode_WRITE(okind, odata, o++, 'u');
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 12) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 8) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[(ch >> 4) & 0xF]);
+                    PyUnicode_WRITE(okind, odata, o++, hexdigits[ch & 0xF]);
                 }
             }
             /* Copy characters as-is */
             else {
-                *p++ = ch;
-#ifndef Py_UNICODE_WIDE
-                if (ucs >= 0x10000)
-                    *p++ = ch2;
-#endif
+                PyUnicode_WRITE(okind, odata, o++, ch);
             }
         }
     }
-    /* Add quote */
-    *p++ = PyUnicode_AS_UNICODE(repr)[0];
-
-    *p = '\0';
-    PyUnicode_Resize(&repr, p - PyUnicode_AS_UNICODE(repr));
+    /* Closing quote already added at the beginning */
     return repr;
 }
 
@@ -8617,7 +10817,7 @@
 Return -1 on failure.");
 
 static PyObject *
-unicode_rfind(PyUnicodeObject *self, PyObject *args)
+unicode_rfind(PyObject *self, PyObject *args)
 {
     PyUnicodeObject *substring;
     Py_ssize_t start;
@@ -8628,14 +10828,21 @@
                                             &start, &end))
         return NULL;
 
-    result = stringlib_rfind_slice(
-        PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
-        PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
-        start, end
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    if (PyUnicode_READY(substring) == -1)
+        return NULL;
+
+    result = any_find_slice(
+        ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice,
+        self, (PyObject*)substring, start, end
         );
 
     Py_DECREF(substring);
 
+    if (result == -2)
+        return NULL;
+
     return PyLong_FromSsize_t(result);
 }
 
@@ -8645,7 +10852,7 @@
 Like S.rfind() but raise ValueError when the substring is not found.");
 
 static PyObject *
-unicode_rindex(PyUnicodeObject *self, PyObject *args)
+unicode_rindex(PyObject *self, PyObject *args)
 {
     PyUnicodeObject *substring;
     Py_ssize_t start;
@@ -8656,18 +10863,26 @@
                                             &start, &end))
         return NULL;
 
-    result = stringlib_rfind_slice(
-        PyUnicode_AS_UNICODE(self), PyUnicode_GET_SIZE(self),
-        PyUnicode_AS_UNICODE(substring), PyUnicode_GET_SIZE(substring),
-        start, end
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+    if (PyUnicode_READY(substring) == -1)
+        return NULL;
+
+    result = any_find_slice(
+        ucs1lib_rfind_slice, ucs2lib_rfind_slice, ucs4lib_rfind_slice,
+        self, (PyObject*)substring, start, end
         );
 
     Py_DECREF(substring);
 
+    if (result == -2)
+        return NULL;
+
     if (result < 0) {
         PyErr_SetString(PyExc_ValueError, "substring not found");
         return NULL;
     }
+
     return PyLong_FromSsize_t(result);
 }
 
@@ -8681,17 +10896,20 @@
 unicode_rjust(PyUnicodeObject *self, PyObject *args)
 {
     Py_ssize_t width;
-    Py_UNICODE fillchar = ' ';
+    Py_UCS4 fillchar = ' ';
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
 
     if (!PyArg_ParseTuple(args, "n|O&:rjust", &width, convert_uc, &fillchar))
         return NULL;
 
-    if (self->length >= width && PyUnicode_CheckExact(self)) {
+    if (_PyUnicode_LENGTH(self) >= width && PyUnicode_CheckExact(self)) {
         Py_INCREF(self);
         return (PyObject*) self;
     }
 
-    return (PyObject*) pad(self, width - self->length, 0, fillchar);
+    return (PyObject*) pad(self, width - _PyUnicode_LENGTH(self), 0, fillchar);
 }
 
 PyObject *
@@ -8749,25 +10967,66 @@
     PyObject* str_obj;
     PyObject* sep_obj;
     PyObject* out;
+    int kind1, kind2, kind;
+    void *buf1 = NULL, *buf2 = NULL;
+    Py_ssize_t len1, len2;
 
     str_obj = PyUnicode_FromObject(str_in);
-    if (!str_obj)
+    if (!str_obj || PyUnicode_READY(str_in) == -1)
         return NULL;
     sep_obj = PyUnicode_FromObject(sep_in);
-    if (!sep_obj) {
+    if (!sep_obj || PyUnicode_READY(sep_obj) == -1) {
         Py_DECREF(str_obj);
         return NULL;
     }
 
-    out = stringlib_partition(
-        str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
-        sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
-        );
+    kind1 = PyUnicode_KIND(str_in);
+    kind2 = PyUnicode_KIND(sep_obj);
+    kind = kind1 > kind2 ? kind1 : kind2;
+    buf1 = PyUnicode_DATA(str_in);
+    if (kind1 != kind)
+        buf1 = _PyUnicode_AsKind(str_in, kind);
+    if (!buf1)
+        goto onError;
+    buf2 = PyUnicode_DATA(sep_obj);
+    if (kind2 != kind)
+        buf2 = _PyUnicode_AsKind(sep_obj, kind);
+    if (!buf2)
+        goto onError;
+    len1 = PyUnicode_GET_LENGTH(str_obj);
+    len2 = PyUnicode_GET_LENGTH(sep_obj);
+
+    switch(PyUnicode_KIND(str_in)) {
+    case PyUnicode_1BYTE_KIND:
+        out = ucs1lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
+        break;
+    case PyUnicode_2BYTE_KIND:
+        out = ucs2lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
+        break;
+    case PyUnicode_4BYTE_KIND:
+        out = ucs4lib_partition(str_obj, buf1, len1, sep_obj, buf2, len2);
+        break;
+    default:
+        assert(0);
+        out = 0;
+    }
 
     Py_DECREF(sep_obj);
     Py_DECREF(str_obj);
+    if (kind1 != kind)
+        PyMem_Free(buf1);
+    if (kind2 != kind)
+        PyMem_Free(buf2);
 
     return out;
+  onError:
+    Py_DECREF(sep_obj);
+    Py_DECREF(str_obj);
+    if (kind1 != kind && buf1)
+        PyMem_Free(buf1);
+    if (kind2 != kind && buf2)
+        PyMem_Free(buf2);
+    return NULL;
 }
 
 
@@ -8777,6 +11036,9 @@
     PyObject* str_obj;
     PyObject* sep_obj;
     PyObject* out;
+    int kind1, kind2, kind;
+    void *buf1 = NULL, *buf2 = NULL;
+    Py_ssize_t len1, len2;
 
     str_obj = PyUnicode_FromObject(str_in);
     if (!str_obj)
@@ -8787,15 +11049,53 @@
         return NULL;
     }
 
-    out = stringlib_rpartition(
-        str_obj, PyUnicode_AS_UNICODE(str_obj), PyUnicode_GET_SIZE(str_obj),
-        sep_obj, PyUnicode_AS_UNICODE(sep_obj), PyUnicode_GET_SIZE(sep_obj)
-        );
+    kind1 = PyUnicode_KIND(str_in);
+    kind2 = PyUnicode_KIND(sep_obj);
+    kind = PY_MAX(kind1, kind2);
+    buf1 = PyUnicode_DATA(str_in);
+    if (kind1 != kind)
+        buf1 = _PyUnicode_AsKind(str_in, kind);
+    if (!buf1)
+        goto onError;
+    buf2 = PyUnicode_DATA(sep_obj);
+    if (kind2 != kind)
+        buf2 = _PyUnicode_AsKind(sep_obj, kind);
+    if (!buf2)
+        goto onError;
+    len1 = PyUnicode_GET_LENGTH(str_obj);
+    len2 = PyUnicode_GET_LENGTH(sep_obj);
+
+    switch(PyUnicode_KIND(str_in)) {
+    case PyUnicode_1BYTE_KIND:
+        out = ucs1lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
+        break;
+    case PyUnicode_2BYTE_KIND:
+        out = ucs2lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
+        break;
+    case PyUnicode_4BYTE_KIND:
+        out = ucs4lib_rpartition(str_obj, buf1, len1, sep_obj, buf2, len2);
+        break;
+    default:
+        assert(0);
+        out = 0;
+    }
 
     Py_DECREF(sep_obj);
     Py_DECREF(str_obj);
+    if (kind1 != kind)
+        PyMem_Free(buf1);
+    if (kind2 != kind)
+        PyMem_Free(buf2);
 
     return out;
+  onError:
+    Py_DECREF(sep_obj);
+    Py_DECREF(str_obj);
+    if (kind1 != kind && buf1)
+        PyMem_Free(buf1);
+    if (kind2 != kind && buf2)
+        PyMem_Free(buf2);
+    return NULL;
 }
 
 PyDoc_STRVAR(partition__doc__,
@@ -8943,22 +11243,28 @@
     if (!new)
         return NULL;
     if (y != NULL) {
+        int x_kind, y_kind, z_kind;
+        void *x_data, *y_data, *z_data;
+
         /* x must be a string too, of equal length */
-        Py_ssize_t ylen = PyUnicode_GET_SIZE(y);
         if (!PyUnicode_Check(x)) {
             PyErr_SetString(PyExc_TypeError, "first maketrans argument must "
                             "be a string if there is a second argument");
             goto err;
         }
-        if (PyUnicode_GET_SIZE(x) != ylen) {
+        if (PyUnicode_GET_LENGTH(x) != PyUnicode_GET_LENGTH(y)) {
             PyErr_SetString(PyExc_ValueError, "the first two maketrans "
                             "arguments must have equal length");
             goto err;
         }
         /* create entries for translating chars in x to those in y */
-        for (i = 0; i < PyUnicode_GET_SIZE(x); i++) {
-            key = PyLong_FromLong(PyUnicode_AS_UNICODE(x)[i]);
-            value = PyLong_FromLong(PyUnicode_AS_UNICODE(y)[i]);
+        x_kind = PyUnicode_KIND(x);
+        y_kind = PyUnicode_KIND(y);
+        x_data = PyUnicode_DATA(x);
+        y_data = PyUnicode_DATA(y);
+        for (i = 0; i < PyUnicode_GET_LENGTH(x); i++) {
+            key = PyLong_FromLong(PyUnicode_READ(x_kind, x_data, i));
+            value = PyLong_FromLong(PyUnicode_READ(y_kind, y_data, i));
             if (!key || !value)
                 goto err;
             res = PyDict_SetItem(new, key, value);
@@ -8969,8 +11275,10 @@
         }
         /* create entries for deleting chars in z */
         if (z != NULL) {
+            z_kind = PyUnicode_KIND(z);
+            z_data = PyUnicode_DATA(z);
             for (i = 0; i < PyUnicode_GET_SIZE(z); i++) {
-                key = PyLong_FromLong(PyUnicode_AS_UNICODE(z)[i]);
+                key = PyLong_FromLong(PyUnicode_READ(z_kind, z_data, i));
                 if (!key)
                     goto err;
                 res = PyDict_SetItem(new, key, Py_None);
@@ -8980,6 +11288,9 @@
             }
         }
     } else {
+        int kind;
+        void *data;
+
         /* x must be a dict */
         if (!PyDict_CheckExact(x)) {
             PyErr_SetString(PyExc_TypeError, "if you give only one argument "
@@ -8996,7 +11307,9 @@
                                     "table must be of length 1");
                     goto err;
                 }
-                newkey = PyLong_FromLong(PyUnicode_AS_UNICODE(key)[0]);
+                kind = PyUnicode_KIND(key);
+                data = PyUnicode_DATA(key);
+                newkey = PyLong_FromLong(PyUnicode_READ(kind, data, 0));
                 if (!newkey)
                     goto err;
                 res = PyDict_SetItem(new, newkey, value);
@@ -9030,9 +11343,9 @@
 are deleted.");
 
 static PyObject*
-unicode_translate(PyUnicodeObject *self, PyObject *table)
+unicode_translate(PyObject *self, PyObject *table)
 {
-    return PyUnicode_TranslateCharmap(self->str, self->length, table, "ignore");
+    return _PyUnicode_TranslateCharmap(self, table, "ignore");
 }
 
 PyDoc_STRVAR(upper__doc__,
@@ -9057,12 +11370,18 @@
 {
     Py_ssize_t fill;
     PyUnicodeObject *u;
-
     Py_ssize_t width;
+    int kind;
+    void *data;
+    Py_UCS4 chr;
+
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+
     if (!PyArg_ParseTuple(args, "n:zfill", &width))
         return NULL;
 
-    if (self->length >= width) {
+    if (PyUnicode_GET_LENGTH(self) >= width) {
         if (PyUnicode_CheckExact(self)) {
             Py_INCREF(self);
             return (PyObject*) self;
@@ -9074,34 +11393,31 @@
                 );
     }
 
-    fill = width - self->length;
+    fill = width - _PyUnicode_LENGTH(self);
 
     u = pad(self, fill, 0, '0');
 
     if (u == NULL)
         return NULL;
 
-    if (u->str[fill] == '+' || u->str[fill] == '-') {
+    kind = PyUnicode_KIND(u);
+    data = PyUnicode_DATA(u);
+    chr = PyUnicode_READ(kind, data, fill);
+
+    if (chr == '+' || chr == '-') {
         /* move sign to beginning of string */
-        u->str[0] = u->str[fill];
-        u->str[fill] = '0';
+        PyUnicode_WRITE(kind, data, 0, chr);
+        PyUnicode_WRITE(kind, data, fill, '0');
     }
 
     return (PyObject*) u;
 }
 
 #if 0
-static PyObject*
-unicode_freelistsize(PyUnicodeObject *self)
-{
-    return PyLong_FromLong(numfree);
-}
-
 static PyObject *
 unicode__decimal2ascii(PyObject *self)
 {
-    return PyUnicode_TransformDecimalToASCII(PyUnicode_AS_UNICODE(self),
-                                             PyUnicode_GET_SIZE(self));
+    return PyUnicode_TransformDecimalAndSpaceToASCII(self);
 }
 #endif
 
@@ -9201,7 +11517,7 @@
     return PyBool_FromLong(result);
 }
 
-#include "stringlib/string_format.h"
+#include "stringlib/unicode_format.h"
 
 PyDoc_STRVAR(format__doc__,
              "S.format(*args, **kwargs) -> str\n\
@@ -9223,9 +11539,8 @@
     if (!PyArg_ParseTuple(args, "U:__format__", &format_spec))
         return NULL;
 
-    return _PyUnicode_FormatAdvanced(self,
-                                     PyUnicode_AS_UNICODE(format_spec),
-                                     PyUnicode_GET_SIZE(format_spec));
+    return _PyUnicode_FormatAdvanced(self, format_spec, 0,
+                                     PyUnicode_GET_LENGTH(format_spec));
 }
 
 PyDoc_STRVAR(p_format__doc__,
@@ -9236,8 +11551,35 @@
 static PyObject *
 unicode__sizeof__(PyUnicodeObject *v)
 {
-    return PyLong_FromSsize_t(sizeof(PyUnicodeObject) +
-                              sizeof(Py_UNICODE) * (v->length + 1));
+    Py_ssize_t size;
+
+    /* If it's a compact object, account for base structure +
+       character data. */
+    if (PyUnicode_IS_COMPACT_ASCII(v))
+        size = sizeof(PyASCIIObject) + PyUnicode_GET_LENGTH(v) + 1;
+    else if (PyUnicode_IS_COMPACT(v))
+        size = sizeof(PyCompactUnicodeObject) +
+            (PyUnicode_GET_LENGTH(v) + 1) * PyUnicode_CHARACTER_SIZE(v);
+    else {
+        /* If it is a two-block object, account for base object, and
+           for character block if present. */
+        size = sizeof(PyUnicodeObject);
+        if (v->data.any)
+            size += (PyUnicode_GET_LENGTH(v) + 1) *
+                PyUnicode_CHARACTER_SIZE(v);
+    }
+    /* If the wstr pointer is present, account for it unless it is shared
+       with the data pointer. Since PyUnicode_DATA will crash if the object
+       is not ready, check whether it's either not ready (in which case the
+       data is entirely in wstr) or if the data is not shared. */
+    if (_PyUnicode_WSTR(v) &&
+        (!PyUnicode_IS_READY(v) ||
+         (PyUnicode_DATA(v) != _PyUnicode_WSTR(v))))
+        size += (PyUnicode_WSTR_LENGTH(v) + 1) * sizeof(wchar_t);
+    if (_PyUnicode_UTF8(v) && _PyUnicode_UTF8(v) != PyUnicode_DATA(v))
+        size += _PyUnicode_UTF8_LENGTH(v) + 1;
+
+    return PyLong_FromSsize_t(size);
 }
 
 PyDoc_STRVAR(sizeof__doc__,
@@ -9246,7 +11588,17 @@
 static PyObject *
 unicode_getnewargs(PyUnicodeObject *v)
 {
-    return Py_BuildValue("(u#)", v->str, v->length);
+    PyObject *copy;
+    unsigned char *data;
+    int kind;
+    if (PyUnicode_READY(v) == -1)
+        return NULL;
+    kind = PyUnicode_KIND(v);
+    data = PyUnicode_1BYTE_DATA(v);
+    copy = PyUnicode_FromKindAndData(kind, data, PyUnicode_GET_LENGTH(v));
+    if (!copy)
+        return NULL;
+    return Py_BuildValue("(N)", copy);
 }
 
 static PyMethodDef unicode_methods[] = {
@@ -9306,7 +11658,6 @@
 
 #if 0
     /* These methods are just used for debugging the implementation. */
-    {"freelistsize", (PyCFunction) unicode_freelistsize, METH_NOARGS},
     {"_decimal2ascii", (PyCFunction) unicode__decimal2ascii, METH_NOARGS},
 #endif
 
@@ -9343,32 +11694,36 @@
 static PyObject*
 unicode_subscript(PyUnicodeObject* self, PyObject* item)
 {
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+
     if (PyIndex_Check(item)) {
         Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
         if (i == -1 && PyErr_Occurred())
             return NULL;
         if (i < 0)
-            i += PyUnicode_GET_SIZE(self);
+            i += PyUnicode_GET_LENGTH(self);
         return unicode_getitem(self, i);
     } else if (PySlice_Check(item)) {
         Py_ssize_t start, stop, step, slicelength, cur, i;
-        Py_UNICODE* source_buf;
+        const Py_UNICODE* source_buf;
         Py_UNICODE* result_buf;
         PyObject* result;
 
-        if (PySlice_GetIndicesEx(item, PyUnicode_GET_SIZE(self),
+        if (PySlice_GetIndicesEx(item, PyUnicode_GET_LENGTH(self),
                                  &start, &stop, &step, &slicelength) < 0) {
             return NULL;
         }
 
         if (slicelength <= 0) {
-            return PyUnicode_FromUnicode(NULL, 0);
-        } else if (start == 0 && step == 1 && slicelength == self->length &&
+            return PyUnicode_New(0, 0);
+        } else if (start == 0 && step == 1 &&
+                   slicelength == PyUnicode_GET_LENGTH(self) &&
                    PyUnicode_CheckExact(self)) {
             Py_INCREF(self);
             return (PyObject *)self;
         } else if (step == 1) {
-            return PyUnicode_FromUnicode(self->str + start, slicelength);
+            return substring(self, start, slicelength);
         } else {
             source_buf = PyUnicode_AS_UNICODE((PyObject*)self);
             result_buf = (Py_UNICODE *)PyObject_MALLOC(slicelength*
@@ -9436,7 +11791,7 @@
                               (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL);
     if (p == NULL)
         return NULL;
-    result = PyUnicode_FromStringAndSize(p, strlen(p));
+    result = PyUnicode_DecodeASCII(p, strlen(p), NULL);
     PyMem_Free(p);
     return result;
 }
@@ -9452,37 +11807,23 @@
     str = _PyBytes_FormatLong(val, flags, prec, type, &buf, &len);
     if (!str)
         return NULL;
-    result = PyUnicode_FromStringAndSize(buf, len);
+    result = PyUnicode_DecodeASCII(buf, len, NULL);
     Py_DECREF(str);
     return result;
 }
 
 static int
-formatchar(Py_UNICODE *buf,
+formatchar(Py_UCS4 *buf,
            size_t buflen,
            PyObject *v)
 {
     /* presume that the buffer is at least 3 characters long */
     if (PyUnicode_Check(v)) {
-        if (PyUnicode_GET_SIZE(v) == 1) {
-            buf[0] = PyUnicode_AS_UNICODE(v)[0];
+        if (PyUnicode_GET_LENGTH(v) == 1) {
+            buf[0] = PyUnicode_READ_CHAR(v, 0);
             buf[1] = '\0';
             return 1;
         }
-#ifndef Py_UNICODE_WIDE
-        if (PyUnicode_GET_SIZE(v) == 2) {
-            /* Decode a valid surrogate pair */
-            int c0 = PyUnicode_AS_UNICODE(v)[0];
-            int c1 = PyUnicode_AS_UNICODE(v)[1];
-            if (0xD800 <= c0 && c0 <= 0xDBFF &&
-                0xDC00 <= c1 && c1 <= 0xDFFF) {
-                buf[0] = c0;
-                buf[1] = c1;
-                buf[2] = '\0';
-                return 2;
-            }
-        }
-#endif
         goto onError;
     }
     else {
@@ -9498,15 +11839,7 @@
             return -1;
         }
 
-#ifndef Py_UNICODE_WIDE
-        if (x > 0xffff) {
-            x -= 0x10000;
-            buf[0] = (Py_UNICODE)(0xD800 | (x >> 10));
-            buf[1] = (Py_UNICODE)(0xDC00 | (x & 0x3FF));
-            return 2;
-        }
-#endif
-        buf[0] = (Py_UNICODE) x;
+        buf[0] = (Py_UCS4) x;
         buf[1] = '\0';
         return 1;
     }
@@ -9525,28 +11858,35 @@
 PyObject *
 PyUnicode_Format(PyObject *format, PyObject *args)
 {
-    Py_UNICODE *fmt, *res;
-    Py_ssize_t fmtcnt, rescnt, reslen, arglen, argidx;
+    void *fmt;
+    int fmtkind;
+    PyObject *result;
+    Py_UCS4 *res, *res0;
+    Py_UCS4 max;
+    int kind;
+    Py_ssize_t fmtcnt, fmtpos, rescnt, reslen, arglen, argidx;
     int args_owned = 0;
-    PyUnicodeObject *result = NULL;
     PyObject *dict = NULL;
-    PyObject *uformat;
+    PyUnicodeObject *uformat;
 
     if (format == NULL || args == NULL) {
         PyErr_BadInternalCall();
         return NULL;
     }
-    uformat = PyUnicode_FromObject(format);
-    if (uformat == NULL)
+    uformat = (PyUnicodeObject*)PyUnicode_FromObject(format);
+    if (uformat == NULL || PyUnicode_READY(uformat) == -1)
         return NULL;
-    fmt = PyUnicode_AS_UNICODE(uformat);
-    fmtcnt = PyUnicode_GET_SIZE(uformat);
+    fmt = PyUnicode_DATA(uformat);
+    fmtkind = PyUnicode_KIND(uformat);
+    fmtcnt = PyUnicode_GET_LENGTH(uformat);
+    fmtpos = 0;
 
     reslen = rescnt = fmtcnt + 100;
-    result = _PyUnicode_New(reslen);
-    if (result == NULL)
+    res = res0 = PyMem_Malloc(reslen * sizeof(Py_UCS4));
+    if (res0 == NULL) {
+        PyErr_NoMemory();
         goto onError;
-    res = PyUnicode_AS_UNICODE(result);
+    }
 
     if (PyTuple_Check(args)) {
         arglen = PyTuple_Size(args);
@@ -9561,35 +11901,39 @@
         dict = args;
 
     while (--fmtcnt >= 0) {
-        if (*fmt != '%') {
+        if (PyUnicode_READ(fmtkind, fmt, fmtpos) != '%') {
             if (--rescnt < 0) {
                 rescnt = fmtcnt + 100;
                 reslen += rescnt;
-                if (_PyUnicode_Resize(&result, reslen) < 0)
+                res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4));
+                if (res0 == NULL){
+                    PyErr_NoMemory();
                     goto onError;
-                res = PyUnicode_AS_UNICODE(result) + reslen - rescnt;
+                }
+                res = res0 + reslen - rescnt;
                 --rescnt;
             }
-            *res++ = *fmt++;
+            *res++ = PyUnicode_READ(fmtkind, fmt, fmtpos++);
         }
         else {
             /* Got a format specifier */
             int flags = 0;
             Py_ssize_t width = -1;
             int prec = -1;
-            Py_UNICODE c = '\0';
-            Py_UNICODE fill;
+            Py_UCS4 c = '\0';
+            Py_UCS4 fill;
             int isnumok;
             PyObject *v = NULL;
             PyObject *temp = NULL;
-            Py_UNICODE *pbuf;
+            void *pbuf;
+            Py_ssize_t pindex;
             Py_UNICODE sign;
-            Py_ssize_t len;
-            Py_UNICODE formatbuf[FORMATBUFLEN]; /* For formatchar() */
+            Py_ssize_t len, len1;
+            Py_UCS4 formatbuf[FORMATBUFLEN]; /* For formatchar() */
 
-            fmt++;
-            if (*fmt == '(') {
-                Py_UNICODE *keystart;
+            fmtpos++;
+            if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(') {
+                Py_ssize_t keystart;
                 Py_ssize_t keylen;
                 PyObject *key;
                 int pcount = 1;
@@ -9599,34 +11943,24 @@
                                     "format requires a mapping");
                     goto onError;
                 }
-                ++fmt;
+                ++fmtpos;
                 --fmtcnt;
-                keystart = fmt;
+                keystart = fmtpos;
                 /* Skip over balanced parentheses */
                 while (pcount > 0 && --fmtcnt >= 0) {
-                    if (*fmt == ')')
+                    if (PyUnicode_READ(fmtkind, fmt, fmtpos) == ')')
                         --pcount;
-                    else if (*fmt == '(')
+                    else if (PyUnicode_READ(fmtkind, fmt, fmtpos) == '(')
                         ++pcount;
-                    fmt++;
+                    fmtpos++;
                 }
-                keylen = fmt - keystart - 1;
+                keylen = fmtpos - keystart - 1;
                 if (fmtcnt < 0 || pcount > 0) {
                     PyErr_SetString(PyExc_ValueError,
                                     "incomplete format key");
                     goto onError;
                 }
-#if 0
-                /* keys are converted to strings using UTF-8 and
-                   then looked up since Python uses strings to hold
-                   variables names etc. in its namespaces and we
-                   wouldn't want to break common idioms. */
-                key = PyUnicode_EncodeUTF8(keystart,
-                                           keylen,
-                                           NULL);
-#else
-                key = PyUnicode_FromUnicode(keystart, keylen);
-#endif
+                key = substring(uformat, keystart, keylen);
                 if (key == NULL)
                     goto onError;
                 if (args_owned) {
@@ -9643,7 +11977,7 @@
                 argidx = -2;
             }
             while (--fmtcnt >= 0) {
-                switch (c = *fmt++) {
+                switch (c = PyUnicode_READ(fmtkind, fmt, fmtpos++)) {
                 case '-': flags |= F_LJUST; continue;
                 case '+': flags |= F_SIGN; continue;
                 case ' ': flags |= F_BLANK; continue;
@@ -9669,12 +12003,12 @@
                     width = -width;
                 }
                 if (--fmtcnt >= 0)
-                    c = *fmt++;
+                    c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
             }
             else if (c >= '0' && c <= '9') {
                 width = c - '0';
                 while (--fmtcnt >= 0) {
-                    c = *fmt++;
+                    c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
                     if (c < '0' || c > '9')
                         break;
                     if ((width*10) / 10 != width) {
@@ -9688,7 +12022,7 @@
             if (c == '.') {
                 prec = 0;
                 if (--fmtcnt >= 0)
-                    c = *fmt++;
+                    c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
                 if (c == '*') {
                     v = getnextarg(args, arglen, &argidx);
                     if (v == NULL)
@@ -9704,12 +12038,12 @@
                     if (prec < 0)
                         prec = 0;
                     if (--fmtcnt >= 0)
-                        c = *fmt++;
+                        c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
                 }
                 else if (c >= '0' && c <= '9') {
                     prec = c - '0';
                     while (--fmtcnt >= 0) {
-                        c = *fmt++;
+                        c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
                         if (c < '0' || c > '9')
                             break;
                         if ((prec*10) / 10 != prec) {
@@ -9724,7 +12058,7 @@
             if (fmtcnt >= 0) {
                 if (c == 'h' || c == 'l' || c == 'L') {
                     if (--fmtcnt >= 0)
-                        c = *fmt++;
+                        c = PyUnicode_READ(fmtkind, fmt, fmtpos++);
                 }
             }
             if (fmtcnt < 0) {
@@ -9743,8 +12077,9 @@
 
             case '%':
                 pbuf = formatbuf;
+                kind = PyUnicode_4BYTE_KIND;
                 /* presume that buffer length is at least 1 */
-                pbuf[0] = '%';
+                PyUnicode_WRITE(kind, pbuf, 0, '%');
                 len = 1;
                 break;
 
@@ -9773,8 +12108,13 @@
                         goto onError;
                     }
                 }
-                pbuf = PyUnicode_AS_UNICODE(temp);
-                len = PyUnicode_GET_SIZE(temp);
+                if (PyUnicode_READY(temp) == -1) {
+                    Py_CLEAR(temp);
+                    goto onError;
+                }
+                pbuf = PyUnicode_DATA(temp);
+                kind = PyUnicode_KIND(temp);
+                len = PyUnicode_GET_LENGTH(temp);
                 if (prec >= 0 && len > prec)
                     len = prec;
                 break;
@@ -9803,8 +12143,13 @@
                             Py_DECREF(iobj);
                             if (!temp)
                                 goto onError;
-                            pbuf = PyUnicode_AS_UNICODE(temp);
-                            len = PyUnicode_GET_SIZE(temp);
+                            if (PyUnicode_READY(temp) == -1) {
+                                Py_CLEAR(temp);
+                                goto onError;
+                            }
+                            pbuf = PyUnicode_DATA(temp);
+                            kind = PyUnicode_KIND(temp);
+                            len = PyUnicode_GET_LENGTH(temp);
                             sign = 1;
                         }
                         else {
@@ -9831,8 +12176,13 @@
                 temp = formatfloat(v, flags, prec, c);
                 if (!temp)
                     goto onError;
-                pbuf = PyUnicode_AS_UNICODE(temp);
-                len = PyUnicode_GET_SIZE(temp);
+                if (PyUnicode_READY(temp) == -1) {
+                    Py_CLEAR(temp);
+                    goto onError;
+                }
+                pbuf = PyUnicode_DATA(temp);
+                kind = PyUnicode_KIND(temp);
+                len = PyUnicode_GET_LENGTH(temp);
                 sign = 1;
                 if (flags & F_ZERO)
                     fill = '0';
@@ -9840,6 +12190,7 @@
 
             case 'c':
                 pbuf = formatbuf;
+                kind = PyUnicode_4BYTE_KIND;
                 len = formatchar(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE), v);
                 if (len < 0)
                     goto onError;
@@ -9851,13 +12202,15 @@
                              "at index %zd",
                              (31<=c && c<=126) ? (char)c : '?',
                              (int)c,
-                             (Py_ssize_t)(fmt - 1 -
-                                          PyUnicode_AS_UNICODE(uformat)));
+                             fmtpos - 1);
                 goto onError;
             }
+            /* pbuf is initialized here. */
+            pindex = 0;
             if (sign) {
-                if (*pbuf == '-' || *pbuf == '+') {
-                    sign = *pbuf++;
+                if (PyUnicode_READ(kind, pbuf, pindex) == '-' ||
+                    PyUnicode_READ(kind, pbuf, pindex) == '+') {
+                    sign = PyUnicode_READ(kind, pbuf, pindex++);
                     len--;
                 }
                 else if (flags & F_SIGN)
@@ -9878,12 +12231,13 @@
                     PyErr_NoMemory();
                     goto onError;
                 }
-                if (_PyUnicode_Resize(&result, reslen) < 0) {
+                res0 = PyMem_Realloc(res0, reslen*sizeof(Py_UCS4));
+                if (res0 == 0) {
+                    PyErr_NoMemory();
                     Py_XDECREF(temp);
                     goto onError;
                 }
-                res = PyUnicode_AS_UNICODE(result)
-                    + reslen - rescnt;
+                res = res0 + reslen - rescnt;
             }
             if (sign) {
                 if (fill != ' ')
@@ -9893,11 +12247,11 @@
                     width--;
             }
             if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) {
-                assert(pbuf[0] == '0');
-                assert(pbuf[1] == c);
+                assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
+                assert(PyUnicode_READ(kind, pbuf, pindex+1) == c);
                 if (fill != ' ') {
-                    *res++ = *pbuf++;
-                    *res++ = *pbuf++;
+                    *res++ = PyUnicode_READ(kind, pbuf, pindex++);
+                    *res++ = PyUnicode_READ(kind, pbuf, pindex++);
                 }
                 rescnt -= 2;
                 width -= 2;
@@ -9915,15 +12269,18 @@
                 if (sign)
                     *res++ = sign;
                 if ((flags & F_ALT) && (c == 'x' || c == 'X' || c == 'o')) {
-                    assert(pbuf[0] == '0');
-                    assert(pbuf[1] == c);
-                    *res++ = *pbuf++;
-                    *res++ = *pbuf++;
+                    assert(PyUnicode_READ(kind, pbuf, pindex) == '0');
+                    assert(PyUnicode_READ(kind, pbuf, pindex+1) == c);
+                    *res++ = PyUnicode_READ(kind, pbuf, pindex++);
+                    *res++ = PyUnicode_READ(kind, pbuf, pindex++);
                 }
             }
-            Py_UNICODE_COPY(res, pbuf, len);
-            res += len;
-            rescnt -= len;
+            /* Copy all characters, preserving len */
+            len1 = len;
+            while (len1--) {
+                *res++ = PyUnicode_READ(kind, pbuf, pindex++);
+                rescnt--;
+            }
             while (--width >= len) {
                 --rescnt;
                 *res++ = ' ';
@@ -9943,8 +12300,17 @@
         goto onError;
     }
 
-    if (_PyUnicode_Resize(&result, reslen - rescnt) < 0)
+
+    for (max=0, res = res0; res < res0+reslen-rescnt; res++)
+        if (*res > max)
+            max = *res;
+    result = PyUnicode_New(reslen - rescnt, max);
+    if (!result)
         goto onError;
+    kind = PyUnicode_KIND(result);
+    for (res = res0; res < res0+reslen-rescnt; res++)
+        PyUnicode_WRITE(kind, PyUnicode_DATA(result), res-res0, *res);
+    PyMem_Free(res0);
     if (args_owned) {
         Py_DECREF(args);
     }
@@ -9952,7 +12318,7 @@
     return (PyObject *)result;
 
   onError:
-    Py_XDECREF(result);
+    PyMem_Free(res0);
     Py_DECREF(uformat);
     if (args_owned) {
         Py_DECREF(args);
@@ -9977,7 +12343,7 @@
                                      kwlist, &x, &encoding, &errors))
         return NULL;
     if (x == NULL)
-        return (PyObject *)_PyUnicode_New(0);
+        return (PyObject *)PyUnicode_New(0, 0);
     if (encoding == NULL && errors == NULL)
         return PyObject_Str(x);
     else
@@ -9989,29 +12355,54 @@
 {
     PyUnicodeObject *tmp, *pnew;
     Py_ssize_t n;
+    PyObject *err = NULL;
 
     assert(PyType_IsSubtype(type, &PyUnicode_Type));
     tmp = (PyUnicodeObject *)unicode_new(&PyUnicode_Type, args, kwds);
     if (tmp == NULL)
         return NULL;
     assert(PyUnicode_Check(tmp));
-    pnew = (PyUnicodeObject *) type->tp_alloc(type, n = tmp->length);
+    // TODO: Verify the PyUnicode_GET_SIZE does the right thing.
+    //       it seems kind of strange that tp_alloc gets passed the size
+    //       of the unicode string because there will follow another
+    //       malloc.
+    pnew = (PyUnicodeObject *) type->tp_alloc(type,
+                                              n = PyUnicode_GET_SIZE(tmp));
     if (pnew == NULL) {
         Py_DECREF(tmp);
         return NULL;
     }
-    pnew->str = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1));
-    if (pnew->str == NULL) {
-        _Py_ForgetReference((PyObject *)pnew);
-        PyObject_Del(pnew);
-        Py_DECREF(tmp);
-        return PyErr_NoMemory();
+    _PyUnicode_WSTR(pnew) = (Py_UNICODE*) PyObject_MALLOC(sizeof(Py_UNICODE) * (n+1));
+    if (_PyUnicode_WSTR(pnew) == NULL) {
+        err = PyErr_NoMemory();
+        goto onError;
     }
-    Py_UNICODE_COPY(pnew->str, tmp->str, n+1);
-    pnew->length = n;
-    pnew->hash = tmp->hash;
+    Py_UNICODE_COPY(_PyUnicode_WSTR(pnew), PyUnicode_AS_UNICODE(tmp), n+1);
+    _PyUnicode_WSTR_LENGTH(pnew) = n;
+    _PyUnicode_HASH(pnew) = _PyUnicode_HASH(tmp);
+    _PyUnicode_STATE(pnew).interned = 0;
+    _PyUnicode_STATE(pnew).kind = 0;
+    _PyUnicode_STATE(pnew).compact = 0;
+    _PyUnicode_STATE(pnew).ready = 0;
+    _PyUnicode_STATE(pnew).ascii = 0;
+    pnew->data.any = NULL;
+    _PyUnicode_LENGTH(pnew) = 0;
+    pnew->_base.utf8 = NULL;
+    pnew->_base.utf8_length = 0;
+
+    if (PyUnicode_READY(pnew) == -1) {
+        PyObject_FREE(_PyUnicode_WSTR(pnew));
+        goto onError;
+    }
+
     Py_DECREF(tmp);
     return (PyObject *)pnew;
+
+  onError:
+    _Py_ForgetReference((PyObject *)pnew);
+    PyObject_Del(pnew);
+    Py_DECREF(tmp);
+    return err;
 }
 
 PyDoc_STRVAR(unicode_doc,
@@ -10074,7 +12465,7 @@
     int i;
 
     /* XXX - move this array to unicodectype.c ? */
-    Py_UNICODE linebreak[] = {
+    Py_UCS2 linebreak[] = {
         0x000A, /* LINE FEED */
         0x000D, /* CARRIAGE RETURN */
         0x001C, /* FILE SEPARATOR */
@@ -10086,11 +12477,9 @@
     };
 
     /* Init the implementation */
-    free_list = NULL;
-    numfree = 0;
-    unicode_empty = _PyUnicode_New(0);
+    unicode_empty = (PyUnicodeObject *) PyUnicode_New(0, 0);
     if (!unicode_empty)
-        return;
+        Py_FatalError("Can't create empty string");
 
     for (i = 0; i < 256; i++)
         unicode_latin1[i] = NULL;
@@ -10099,8 +12488,8 @@
 
     /* initialize the linebreak bloom filter */
     bloom_linebreak = make_bloom_mask(
-        linebreak, sizeof(linebreak) / sizeof(linebreak[0])
-        );
+        PyUnicode_2BYTE_KIND, linebreak,
+        sizeof(linebreak) / sizeof(linebreak[0]));
 
     PyType_Ready(&EncodingMapType);
 }
@@ -10110,21 +12499,7 @@
 int
 PyUnicode_ClearFreeList(void)
 {
-    int freelist_size = numfree;
-    PyUnicodeObject *u;
-
-    for (u = free_list; u != NULL;) {
-        PyUnicodeObject *v = u;
-        u = *(PyUnicodeObject **)u;
-        if (v->str)
-            PyObject_DEL(v->str);
-        Py_XDECREF(v->defenc);
-        PyObject_Del(v);
-        numfree--;
-    }
-    free_list = NULL;
-    assert(numfree == 0);
-    return freelist_size;
+    return 0;
 }
 
 void
@@ -10158,6 +12533,10 @@
         return;
     if (PyUnicode_CHECK_INTERNED(s))
         return;
+    if (PyUnicode_READY(s) == -1) {
+        assert(0 && "ready fail in intern...");
+        return;
+    }
     if (interned == NULL) {
         interned = PyDict_New();
         if (interned == NULL) {
@@ -10189,15 +12568,17 @@
     /* The two references in interned are not counted by refcnt.
        The deallocator will take care of this */
     Py_REFCNT(s) -= 2;
-    PyUnicode_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
+    _PyUnicode_STATE(s).interned = SSTATE_INTERNED_MORTAL;
 }
 
 void
 PyUnicode_InternImmortal(PyObject **p)
 {
+    PyUnicodeObject *u = (PyUnicodeObject *)*p;
+
     PyUnicode_InternInPlace(p);
     if (PyUnicode_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) {
-        PyUnicode_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL;
+        _PyUnicode_STATE(u).interned = SSTATE_INTERNED_IMMORTAL;
         Py_INCREF(*p);
     }
 }
@@ -10238,22 +12619,24 @@
             n);
     for (i = 0; i < n; i++) {
         s = (PyUnicodeObject *) PyList_GET_ITEM(keys, i);
-        switch (s->state) {
+        if (PyUnicode_READY(s) == -1)
+            fprintf(stderr, "could not ready string\n");
+        switch (PyUnicode_CHECK_INTERNED(s)) {
         case SSTATE_NOT_INTERNED:
             /* XXX Shouldn't happen */
             break;
         case SSTATE_INTERNED_IMMORTAL:
             Py_REFCNT(s) += 1;
-            immortal_size += s->length;
+            immortal_size += PyUnicode_GET_LENGTH(s);
             break;
         case SSTATE_INTERNED_MORTAL:
             Py_REFCNT(s) += 2;
-            mortal_size += s->length;
+            mortal_size += PyUnicode_GET_LENGTH(s);
             break;
         default:
             Py_FatalError("Inconsistent interned string state.");
         }
-        s->state = SSTATE_NOT_INTERNED;
+        _PyUnicode_STATE(s).interned = SSTATE_NOT_INTERNED;
     }
     fprintf(stderr, "total size of all interned strings: "
             "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d "
@@ -10300,9 +12683,11 @@
         return NULL;
     assert(PyUnicode_Check(seq));
 
-    if (it->it_index < PyUnicode_GET_SIZE(seq)) {
-        item = PyUnicode_FromUnicode(
-            PyUnicode_AS_UNICODE(seq)+it->it_index, 1);
+    if (it->it_index < PyUnicode_GET_LENGTH(seq)) {
+        int kind = PyUnicode_KIND(seq);
+        void *data = PyUnicode_DATA(seq);
+        Py_UCS4 chr = PyUnicode_READ(kind, data, it->it_index);
+        item = PyUnicode_FromOrdinal(chr);
         if (item != NULL)
             ++it->it_index;
         return item;
@@ -10372,6 +12757,8 @@
         PyErr_BadInternalCall();
         return NULL;
     }
+    if (PyUnicode_READY(seq) == -1)
+        return NULL;
     it = PyObject_GC_New(unicodeiterobject, &PyUnicodeIter_Type);
     if (it == NULL)
         return NULL;
@@ -10382,95 +12769,16 @@
     return (PyObject *)it;
 }
 
-size_t
-Py_UNICODE_strlen(const Py_UNICODE *u)
-{
-    int res = 0;
-    while(*u++)
-        res++;
-    return res;
-}
-
-Py_UNICODE*
-Py_UNICODE_strcpy(Py_UNICODE *s1, const Py_UNICODE *s2)
-{
-    Py_UNICODE *u = s1;
-    while ((*u++ = *s2++));
-    return s1;
-}
-
-Py_UNICODE*
-Py_UNICODE_strncpy(Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
-{
-    Py_UNICODE *u = s1;
-    while ((*u++ = *s2++))
-        if (n-- == 0)
-            break;
-    return s1;
-}
-
-Py_UNICODE*
-Py_UNICODE_strcat(Py_UNICODE *s1, const Py_UNICODE *s2)
-{
-    Py_UNICODE *u1 = s1;
-    u1 += Py_UNICODE_strlen(u1);
-    Py_UNICODE_strcpy(u1, s2);
-    return s1;
-}
-
-int
-Py_UNICODE_strcmp(const Py_UNICODE *s1, const Py_UNICODE *s2)
-{
-    while (*s1 && *s2 && *s1 == *s2)
-        s1++, s2++;
-    if (*s1 && *s2)
-        return (*s1 < *s2) ? -1 : +1;
-    if (*s1)
-        return 1;
-    if (*s2)
-        return -1;
-    return 0;
-}
-
-int
-Py_UNICODE_strncmp(const Py_UNICODE *s1, const Py_UNICODE *s2, size_t n)
-{
-    register Py_UNICODE u1, u2;
-    for (; n != 0; n--) {
-        u1 = *s1;
-        u2 = *s2;
-        if (u1 != u2)
-            return (u1 < u2) ? -1 : +1;
-        if (u1 == '\0')
-            return 0;
-        s1++;
-        s2++;
-    }
-    return 0;
-}
-
-Py_UNICODE*
-Py_UNICODE_strchr(const Py_UNICODE *s, Py_UNICODE c)
-{
-    const Py_UNICODE *p;
-    for (p = s; *p; p++)
-        if (*p == c)
-            return (Py_UNICODE*)p;
-    return NULL;
-}
-
-Py_UNICODE*
-Py_UNICODE_strrchr(const Py_UNICODE *s, Py_UNICODE c)
-{
-    const Py_UNICODE *p;
-    p = s + Py_UNICODE_strlen(s);
-    while (p != s) {
-        p--;
-        if (*p == c)
-            return (Py_UNICODE*)p;
-    }
-    return NULL;
-}
+#define UNIOP(x) Py_UNICODE_##x
+#define UNIOP_t Py_UNICODE
+#include "uniops.h"
+#undef UNIOP
+#undef UNIOP_t
+#define UNIOP(x) Py_UCS4_##x
+#define UNIOP_t Py_UCS4
+#include "uniops.h"
+#undef UNIOP
+#undef UNIOP_t
 
 Py_UNICODE*
 PyUnicode_AsUnicodeCopy(PyObject *object)
@@ -10479,6 +12787,10 @@
     Py_UNICODE *copy;
     Py_ssize_t size;
 
+    if (!PyUnicode_Check(unicode)) {
+        PyErr_BadArgument();
+        return NULL;
+    }
     /* Ensure we won't overflow the size. */
     if (PyUnicode_GET_SIZE(unicode) > ((PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) - 1)) {
         PyErr_NoMemory();