]> git.proxmox.com Git - mirror_edk2.git/blobdiff - AppPkg/Applications/Python/Python-2.7.10/Modules/_io/stringio.c
AppPkg/Applications/Python/Python-2.7.10: Initial Checkin part 2/5.
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.10 / Modules / _io / stringio.c
diff --git a/AppPkg/Applications/Python/Python-2.7.10/Modules/_io/stringio.c b/AppPkg/Applications/Python/Python-2.7.10/Modules/_io/stringio.c
new file mode 100644 (file)
index 0000000..605a980
--- /dev/null
@@ -0,0 +1,895 @@
+#define PY_SSIZE_T_CLEAN\r
+#include "Python.h"\r
+#include "structmember.h"\r
+#include "_iomodule.h"\r
+\r
+/* Implementation note: the buffer is always at least one character longer\r
+   than the enclosed string, for proper functioning of _PyIO_find_line_ending.\r
+*/\r
+\r
+typedef struct {\r
+    PyObject_HEAD\r
+    Py_UNICODE *buf;\r
+    Py_ssize_t pos;\r
+    Py_ssize_t string_size;\r
+    size_t buf_size;\r
+\r
+    char ok; /* initialized? */\r
+    char closed;\r
+    char readuniversal;\r
+    char readtranslate;\r
+    PyObject *decoder;\r
+    PyObject *readnl;\r
+    PyObject *writenl;\r
+    \r
+    PyObject *dict;\r
+    PyObject *weakreflist;\r
+} stringio;\r
+\r
+#define CHECK_INITIALIZED(self) \\r
+    if (self->ok <= 0) { \\r
+        PyErr_SetString(PyExc_ValueError, \\r
+            "I/O operation on uninitialized object"); \\r
+        return NULL; \\r
+    }\r
+\r
+#define CHECK_CLOSED(self) \\r
+    if (self->closed) { \\r
+        PyErr_SetString(PyExc_ValueError, \\r
+            "I/O operation on closed file"); \\r
+        return NULL; \\r
+    }\r
+\r
+PyDoc_STRVAR(stringio_doc,\r
+    "Text I/O implementation using an in-memory buffer.\n"\r
+    "\n"\r
+    "The initial_value argument sets the value of object.  The newline\n"\r
+    "argument is like the one of TextIOWrapper's constructor.");\r
+\r
+\r
+/* Internal routine for changing the size, in terms of characters, of the\r
+   buffer of StringIO objects.  The caller should ensure that the 'size'\r
+   argument is non-negative.  Returns 0 on success, -1 otherwise. */\r
+static int\r
+resize_buffer(stringio *self, size_t size)\r
+{\r
+    /* Here, unsigned types are used to avoid dealing with signed integer\r
+       overflow, which is undefined in C. */\r
+    size_t alloc = self->buf_size;\r
+    Py_UNICODE *new_buf = NULL;\r
+\r
+    assert(self->buf != NULL);\r
+\r
+    /* Reserve one more char for line ending detection. */\r
+    size = size + 1;\r
+    /* For simplicity, stay in the range of the signed type. Anyway, Python\r
+       doesn't allow strings to be longer than this. */\r
+    if (size > PY_SSIZE_T_MAX)\r
+        goto overflow;\r
+\r
+    if (size < alloc / 2) {\r
+        /* Major downsize; resize down to exact size. */\r
+        alloc = size + 1;\r
+    }\r
+    else if (size < alloc) {\r
+        /* Within allocated size; quick exit */\r
+        return 0;\r
+    }\r
+    else if (size <= alloc * 1.125) {\r
+        /* Moderate upsize; overallocate similar to list_resize() */\r
+        alloc = size + (size >> 3) + (size < 9 ? 3 : 6);\r
+    }\r
+    else {\r
+        /* Major upsize; resize up to exact size */\r
+        alloc = size + 1;\r
+    }\r
+\r
+    if (alloc > ((size_t)-1) / sizeof(Py_UNICODE))\r
+        goto overflow;\r
+    new_buf = (Py_UNICODE *)PyMem_Realloc(self->buf,\r
+                                          alloc * sizeof(Py_UNICODE));\r
+    if (new_buf == NULL) {\r
+        PyErr_NoMemory();\r
+        return -1;\r
+    }\r
+    self->buf_size = alloc;\r
+    self->buf = new_buf;\r
+\r
+    return 0;\r
+\r
+  overflow:\r
+    PyErr_SetString(PyExc_OverflowError,\r
+                    "new buffer size too large");\r
+    return -1;\r
+}\r
+\r
+/* Internal routine for writing a whole PyUnicode object to the buffer of a\r
+   StringIO object. Returns 0 on success, or -1 on error. */\r
+static Py_ssize_t\r
+write_str(stringio *self, PyObject *obj)\r
+{\r
+    Py_UNICODE *str;\r
+    Py_ssize_t len;\r
+    PyObject *decoded = NULL;\r
+    assert(self->buf != NULL);\r
+    assert(self->pos >= 0);\r
+\r
+    if (self->decoder != NULL) {\r
+        decoded = _PyIncrementalNewlineDecoder_decode(\r
+            self->decoder, obj, 1 /* always final */);\r
+    }\r
+    else {\r
+        decoded = obj;\r
+        Py_INCREF(decoded);\r
+    }\r
+    if (self->writenl) {\r
+        PyObject *translated = PyUnicode_Replace(\r
+            decoded, _PyIO_str_nl, self->writenl, -1);\r
+        Py_DECREF(decoded);\r
+        decoded = translated;\r
+    }\r
+    if (decoded == NULL)\r
+        return -1;\r
+\r
+    assert(PyUnicode_Check(decoded));\r
+    str = PyUnicode_AS_UNICODE(decoded);\r
+    len = PyUnicode_GET_SIZE(decoded);\r
+\r
+    assert(len >= 0);\r
+\r
+    /* This overflow check is not strictly necessary. However, it avoids us to\r
+       deal with funky things like comparing an unsigned and a signed\r
+       integer. */\r
+    if (self->pos > PY_SSIZE_T_MAX - len) {\r
+        PyErr_SetString(PyExc_OverflowError,\r
+                        "new position too large");\r
+        goto fail;\r
+    }\r
+    if (self->pos + len > self->string_size) {\r
+        if (resize_buffer(self, self->pos + len) < 0)\r
+            goto fail;\r
+    }\r
+\r
+    if (self->pos > self->string_size) {\r
+        /* In case of overseek, pad with null bytes the buffer region between\r
+           the end of stream and the current position.\r
+\r
+          0   lo      string_size                           hi\r
+          |   |<---used--->|<----------available----------->|\r
+          |   |            <--to pad-->|<---to write--->    |\r
+          0   buf                   position\r
+\r
+        */\r
+        memset(self->buf + self->string_size, '\0',\r
+               (self->pos - self->string_size) * sizeof(Py_UNICODE));\r
+    }\r
+\r
+    /* Copy the data to the internal buffer, overwriting some of the\r
+       existing data if self->pos < self->string_size. */\r
+    memcpy(self->buf + self->pos, str, len * sizeof(Py_UNICODE));\r
+    self->pos += len;\r
+\r
+    /* Set the new length of the internal string if it has changed. */\r
+    if (self->string_size < self->pos) {\r
+        self->string_size = self->pos;\r
+    }\r
+\r
+    Py_DECREF(decoded);\r
+    return 0;\r
+\r
+fail:\r
+    Py_XDECREF(decoded);\r
+    return -1;\r
+}\r
+\r
+PyDoc_STRVAR(stringio_getvalue_doc,\r
+    "Retrieve the entire contents of the object.");\r
+\r
+static PyObject *\r
+stringio_getvalue(stringio *self)\r
+{\r
+    CHECK_INITIALIZED(self);\r
+    CHECK_CLOSED(self);\r
+    return PyUnicode_FromUnicode(self->buf, self->string_size);\r
+}\r
+\r
+PyDoc_STRVAR(stringio_tell_doc,\r
+    "Tell the current file position.");\r
+\r
+static PyObject *\r
+stringio_tell(stringio *self)\r
+{\r
+    CHECK_INITIALIZED(self);\r
+    CHECK_CLOSED(self);\r
+    return PyLong_FromSsize_t(self->pos);\r
+}\r
+\r
+PyDoc_STRVAR(stringio_read_doc,\r
+    "Read at most n characters, returned as a string.\n"\r
+    "\n"\r
+    "If the argument is negative or omitted, read until EOF\n"\r
+    "is reached. Return an empty string at EOF.\n");\r
+\r
+static PyObject *\r
+stringio_read(stringio *self, PyObject *args)\r
+{\r
+    Py_ssize_t size, n;\r
+    Py_UNICODE *output;\r
+    PyObject *arg = Py_None;\r
+\r
+    CHECK_INITIALIZED(self);\r
+    if (!PyArg_ParseTuple(args, "|O:read", &arg))\r
+        return NULL;\r
+    CHECK_CLOSED(self);\r
+\r
+    if (PyNumber_Check(arg)) {\r
+        size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);\r
+        if (size == -1 && PyErr_Occurred())\r
+            return NULL;\r
+    }\r
+    else if (arg == Py_None) {\r
+        /* Read until EOF is reached, by default. */\r
+        size = -1;\r
+    }\r
+    else {\r
+        PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",\r
+                     Py_TYPE(arg)->tp_name);\r
+        return NULL;\r
+    }\r
+\r
+    /* adjust invalid sizes */\r
+    n = self->string_size - self->pos;\r
+    if (size < 0 || size > n) {\r
+        size = n;\r
+        if (size < 0)\r
+            size = 0;\r
+    }\r
+\r
+    output = self->buf + self->pos;\r
+    self->pos += size;\r
+    return PyUnicode_FromUnicode(output, size);\r
+}\r
+\r
+/* Internal helper, used by stringio_readline and stringio_iternext */\r
+static PyObject *\r
+_stringio_readline(stringio *self, Py_ssize_t limit)\r
+{\r
+    Py_UNICODE *start, *end, old_char;\r
+    Py_ssize_t len, consumed;\r
+\r
+    /* In case of overseek, return the empty string */\r
+    if (self->pos >= self->string_size)\r
+        return PyUnicode_FromString("");\r
+\r
+    start = self->buf + self->pos;\r
+    if (limit < 0 || limit > self->string_size - self->pos)\r
+        limit = self->string_size - self->pos;\r
+\r
+    end = start + limit;\r
+    old_char = *end;\r
+    *end = '\0';\r
+    len = _PyIO_find_line_ending(\r
+        self->readtranslate, self->readuniversal, self->readnl,\r
+        start, end, &consumed);\r
+    *end = old_char;\r
+    /* If we haven't found any line ending, we just return everything\r
+       (`consumed` is ignored). */\r
+    if (len < 0)\r
+        len = limit;\r
+    self->pos += len;\r
+    return PyUnicode_FromUnicode(start, len);\r
+}\r
+\r
+PyDoc_STRVAR(stringio_readline_doc,\r
+    "Read until newline or EOF.\n"\r
+    "\n"\r
+    "Returns an empty string if EOF is hit immediately.\n");\r
+\r
+static PyObject *\r
+stringio_readline(stringio *self, PyObject *args)\r
+{\r
+    PyObject *arg = Py_None;\r
+    Py_ssize_t limit = -1;\r
+\r
+    CHECK_INITIALIZED(self);\r
+    if (!PyArg_ParseTuple(args, "|O:readline", &arg))\r
+        return NULL;\r
+    CHECK_CLOSED(self);\r
+\r
+    if (PyNumber_Check(arg)) {\r
+        limit = PyNumber_AsSsize_t(arg, PyExc_OverflowError);\r
+        if (limit == -1 && PyErr_Occurred())\r
+            return NULL;\r
+    }\r
+    else if (arg != Py_None) {\r
+        PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",\r
+                     Py_TYPE(arg)->tp_name);\r
+        return NULL;\r
+    }\r
+    return _stringio_readline(self, limit);\r
+}\r
+\r
+static PyObject *\r
+stringio_iternext(stringio *self)\r
+{\r
+    PyObject *line;\r
+\r
+    CHECK_INITIALIZED(self);\r
+    CHECK_CLOSED(self);\r
+\r
+    if (Py_TYPE(self) == &PyStringIO_Type) {\r
+        /* Skip method call overhead for speed */\r
+        line = _stringio_readline(self, -1);\r
+    }\r
+    else {\r
+        /* XXX is subclassing StringIO really supported? */\r
+        line = PyObject_CallMethodObjArgs((PyObject *)self,\r
+                                           _PyIO_str_readline, NULL);\r
+        if (line && !PyUnicode_Check(line)) {\r
+            PyErr_Format(PyExc_IOError,\r
+                         "readline() should have returned an str object, "\r
+                         "not '%.200s'", Py_TYPE(line)->tp_name);\r
+            Py_DECREF(line);\r
+            return NULL;\r
+        }\r
+    }\r
+\r
+    if (line == NULL)\r
+        return NULL;\r
+\r
+    if (PyUnicode_GET_SIZE(line) == 0) {\r
+        /* Reached EOF */\r
+        Py_DECREF(line);\r
+        return NULL;\r
+    }\r
+\r
+    return line;\r
+}\r
+\r
+PyDoc_STRVAR(stringio_truncate_doc,\r
+    "Truncate size to pos.\n"\r
+    "\n"\r
+    "The pos argument defaults to the current file position, as\n"\r
+    "returned by tell().  The current file position is unchanged.\n"\r
+    "Returns the new absolute position.\n");\r
+\r
+static PyObject *\r
+stringio_truncate(stringio *self, PyObject *args)\r
+{\r
+    Py_ssize_t size;\r
+    PyObject *arg = Py_None;\r
+\r
+    CHECK_INITIALIZED(self);\r
+    if (!PyArg_ParseTuple(args, "|O:truncate", &arg))\r
+        return NULL;\r
+    CHECK_CLOSED(self);\r
+\r
+    if (PyNumber_Check(arg)) {\r
+        size = PyNumber_AsSsize_t(arg, PyExc_OverflowError);\r
+        if (size == -1 && PyErr_Occurred())\r
+            return NULL;\r
+    }\r
+    else if (arg == Py_None) {\r
+        /* Truncate to current position if no argument is passed. */\r
+        size = self->pos;\r
+    }\r
+    else {\r
+        PyErr_Format(PyExc_TypeError, "integer argument expected, got '%s'",\r
+                     Py_TYPE(arg)->tp_name);\r
+        return NULL;\r
+    }\r
+\r
+    if (size < 0) {\r
+        PyErr_Format(PyExc_ValueError,\r
+                     "Negative size value %zd", size);\r
+        return NULL;\r
+    }\r
+\r
+    if (size < self->string_size) {\r
+        if (resize_buffer(self, size) < 0)\r
+            return NULL;\r
+        self->string_size = size;\r
+    }\r
+\r
+    return PyLong_FromSsize_t(size);\r
+}\r
+\r
+PyDoc_STRVAR(stringio_seek_doc,\r
+    "Change stream position.\n"\r
+    "\n"\r
+    "Seek to character offset pos relative to position indicated by whence:\n"\r
+    "    0  Start of stream (the default).  pos should be >= 0;\n"\r
+    "    1  Current position - pos must be 0;\n"\r
+    "    2  End of stream - pos must be 0.\n"\r
+    "Returns the new absolute position.\n");\r
+\r
+static PyObject *\r
+stringio_seek(stringio *self, PyObject *args)\r
+{\r
+    PyObject *posobj;\r
+    Py_ssize_t pos;\r
+    int mode = 0;\r
+\r
+    CHECK_INITIALIZED(self);\r
+    if (!PyArg_ParseTuple(args, "O|i:seek", &posobj, &mode))\r
+        return NULL;\r
+\r
+    pos = PyNumber_AsSsize_t(posobj, PyExc_OverflowError);\r
+    if (pos == -1 && PyErr_Occurred())\r
+        return NULL;\r
+    \r
+    CHECK_CLOSED(self);\r
+\r
+    if (mode != 0 && mode != 1 && mode != 2) {\r
+        PyErr_Format(PyExc_ValueError,\r
+                     "Invalid whence (%i, should be 0, 1 or 2)", mode);\r
+        return NULL;\r
+    }\r
+    else if (pos < 0 && mode == 0) {\r
+        PyErr_Format(PyExc_ValueError,\r
+                     "Negative seek position %zd", pos);\r
+        return NULL;\r
+    }\r
+    else if (mode != 0 && pos != 0) {\r
+        PyErr_SetString(PyExc_IOError,\r
+                        "Can't do nonzero cur-relative seeks");\r
+        return NULL;\r
+    }\r
+\r
+    /* mode 0: offset relative to beginning of the string.\r
+       mode 1: no change to current position.\r
+       mode 2: change position to end of file. */\r
+    if (mode == 1) {\r
+        pos = self->pos;\r
+    }\r
+    else if (mode == 2) {\r
+        pos = self->string_size;\r
+    }\r
+\r
+    self->pos = pos;\r
+\r
+    return PyLong_FromSsize_t(self->pos);\r
+}\r
+\r
+PyDoc_STRVAR(stringio_write_doc,\r
+    "Write string to file.\n"\r
+    "\n"\r
+    "Returns the number of characters written, which is always equal to\n"\r
+    "the length of the string.\n");\r
+\r
+static PyObject *\r
+stringio_write(stringio *self, PyObject *obj)\r
+{\r
+    Py_ssize_t size;\r
+\r
+    CHECK_INITIALIZED(self);\r
+    if (!PyUnicode_Check(obj)) {\r
+        PyErr_Format(PyExc_TypeError, "unicode argument expected, got '%s'",\r
+                     Py_TYPE(obj)->tp_name);\r
+        return NULL;\r
+    }\r
+    CHECK_CLOSED(self);\r
+    size = PyUnicode_GET_SIZE(obj);\r
+\r
+    if (size > 0 && write_str(self, obj) < 0)\r
+        return NULL;\r
+\r
+    return PyLong_FromSsize_t(size);\r
+}\r
+\r
+PyDoc_STRVAR(stringio_close_doc,\r
+    "Close the IO object. Attempting any further operation after the\n"\r
+    "object is closed will raise a ValueError.\n"\r
+    "\n"\r
+    "This method has no effect if the file is already closed.\n");\r
+\r
+static PyObject *\r
+stringio_close(stringio *self)\r
+{\r
+    self->closed = 1;\r
+    /* Free up some memory */\r
+    if (resize_buffer(self, 0) < 0)\r
+        return NULL;\r
+    Py_CLEAR(self->readnl);\r
+    Py_CLEAR(self->writenl);\r
+    Py_CLEAR(self->decoder);\r
+    Py_RETURN_NONE;\r
+}\r
+\r
+static int\r
+stringio_traverse(stringio *self, visitproc visit, void *arg)\r
+{\r
+    Py_VISIT(self->dict);\r
+    return 0;\r
+}\r
+\r
+static int\r
+stringio_clear(stringio *self)\r
+{\r
+    Py_CLEAR(self->dict);\r
+    return 0;\r
+}\r
+\r
+static void\r
+stringio_dealloc(stringio *self)\r
+{\r
+    _PyObject_GC_UNTRACK(self);\r
+    self->ok = 0;\r
+    if (self->buf) {\r
+        PyMem_Free(self->buf);\r
+        self->buf = NULL;\r
+    }\r
+    Py_CLEAR(self->readnl);\r
+    Py_CLEAR(self->writenl);\r
+    Py_CLEAR(self->decoder);\r
+    Py_CLEAR(self->dict);\r
+    if (self->weakreflist != NULL)\r
+        PyObject_ClearWeakRefs((PyObject *) self);\r
+    Py_TYPE(self)->tp_free(self);\r
+}\r
+\r
+static PyObject *\r
+stringio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+    stringio *self;\r
+\r
+    assert(type != NULL && type->tp_alloc != NULL);\r
+    self = (stringio *)type->tp_alloc(type, 0);\r
+    if (self == NULL)\r
+        return NULL;\r
+\r
+    /* tp_alloc initializes all the fields to zero. So we don't have to\r
+       initialize them here. */\r
+\r
+    self->buf = (Py_UNICODE *)PyMem_Malloc(0);\r
+    if (self->buf == NULL) {\r
+        Py_DECREF(self);\r
+        return PyErr_NoMemory();\r
+    }\r
+\r
+    return (PyObject *)self;\r
+}\r
+\r
+static int\r
+stringio_init(stringio *self, PyObject *args, PyObject *kwds)\r
+{\r
+    char *kwlist[] = {"initial_value", "newline", NULL};\r
+    PyObject *value = NULL;\r
+    char *newline = "\n";\r
+\r
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oz:__init__", kwlist,\r
+                                     &value, &newline))\r
+        return -1;\r
+\r
+    if (newline && newline[0] != '\0'\r
+        && !(newline[0] == '\n' && newline[1] == '\0')\r
+        && !(newline[0] == '\r' && newline[1] == '\0')\r
+        && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {\r
+        PyErr_Format(PyExc_ValueError,\r
+                     "illegal newline value: %s", newline);\r
+        return -1;\r
+    }\r
+    if (value && value != Py_None && !PyUnicode_Check(value)) {\r
+        PyErr_Format(PyExc_TypeError,\r
+                     "initial_value must be unicode or None, not %.200s",\r
+                     Py_TYPE(value)->tp_name);\r
+        return -1;\r
+    }\r
+\r
+    self->ok = 0;\r
+\r
+    Py_CLEAR(self->readnl);\r
+    Py_CLEAR(self->writenl);\r
+    Py_CLEAR(self->decoder);\r
+\r
+    if (newline) {\r
+        self->readnl = PyString_FromString(newline);\r
+        if (self->readnl == NULL)\r
+            return -1;\r
+    }\r
+    self->readuniversal = (newline == NULL || newline[0] == '\0');\r
+    self->readtranslate = (newline == NULL);\r
+    /* If newline == "", we don't translate anything.\r
+       If newline == "\n" or newline == None, we translate to "\n", which is\r
+       a no-op.\r
+       (for newline == None, TextIOWrapper translates to os.sepline, but it\r
+       is pointless for StringIO)\r
+    */\r
+    if (newline != NULL && newline[0] == '\r') {\r
+        self->writenl = PyUnicode_FromString(newline);\r
+    }\r
+\r
+    if (self->readuniversal) {\r
+        self->decoder = PyObject_CallFunction(\r
+            (PyObject *)&PyIncrementalNewlineDecoder_Type,\r
+            "Oi", Py_None, (int) self->readtranslate);\r
+        if (self->decoder == NULL)\r
+            return -1;\r
+    }\r
+\r
+    /* Now everything is set up, resize buffer to size of initial value,\r
+       and copy it */\r
+    self->string_size = 0;\r
+    if (value && value != Py_None) {\r
+        Py_ssize_t len = PyUnicode_GetSize(value);\r
+        /* This is a heuristic, for newline translation might change\r
+           the string length. */\r
+        if (resize_buffer(self, len) < 0)\r
+            return -1;\r
+        self->pos = 0;\r
+        if (write_str(self, value) < 0)\r
+            return -1;\r
+    }\r
+    else {\r
+        if (resize_buffer(self, 0) < 0)\r
+            return -1;\r
+    }\r
+    self->pos = 0;\r
+\r
+    self->closed = 0;\r
+    self->ok = 1;\r
+    return 0;\r
+}\r
+\r
+/* Properties and pseudo-properties */\r
+\r
+PyDoc_STRVAR(stringio_readable_doc,\r
+"readable() -> bool. Returns True if the IO object can be read.");\r
+\r
+PyDoc_STRVAR(stringio_writable_doc,\r
+"writable() -> bool. Returns True if the IO object can be written.");\r
+\r
+PyDoc_STRVAR(stringio_seekable_doc,\r
+"seekable() -> bool. Returns True if the IO object can be seeked.");\r
+\r
+static PyObject *\r
+stringio_seekable(stringio *self, PyObject *args)\r
+{\r
+    CHECK_INITIALIZED(self);\r
+    CHECK_CLOSED(self);\r
+    Py_RETURN_TRUE;\r
+}\r
+\r
+static PyObject *\r
+stringio_readable(stringio *self, PyObject *args)\r
+{\r
+    CHECK_INITIALIZED(self);\r
+    CHECK_CLOSED(self);\r
+    Py_RETURN_TRUE;\r
+}\r
+\r
+static PyObject *\r
+stringio_writable(stringio *self, PyObject *args)\r
+{\r
+    CHECK_INITIALIZED(self);\r
+    CHECK_CLOSED(self);\r
+    Py_RETURN_TRUE;\r
+}\r
+\r
+/* Pickling support.\r
+\r
+   The implementation of __getstate__ is similar to the one for BytesIO,\r
+   except that we also save the newline parameter. For __setstate__ and unlike\r
+   BytesIO, we call __init__ to restore the object's state. Doing so allows us\r
+   to avoid decoding the complex newline state while keeping the object\r
+   representation compact.\r
+\r
+   See comment in bytesio.c regarding why only pickle protocols and onward are\r
+   supported.\r
+*/\r
+\r
+static PyObject *\r
+stringio_getstate(stringio *self)\r
+{\r
+    PyObject *initvalue = stringio_getvalue(self);\r
+    PyObject *dict;\r
+    PyObject *state;\r
+\r
+    if (initvalue == NULL)\r
+        return NULL;\r
+    if (self->dict == NULL) {\r
+        Py_INCREF(Py_None);\r
+        dict = Py_None;\r
+    }\r
+    else {\r
+        dict = PyDict_Copy(self->dict);\r
+        if (dict == NULL)\r
+            return NULL;\r
+    }\r
+\r
+    state = Py_BuildValue("(OOnN)", initvalue,\r
+                          self->readnl ? self->readnl : Py_None,\r
+                          self->pos, dict);\r
+    Py_DECREF(initvalue);\r
+    return state;\r
+}\r
+\r
+static PyObject *\r
+stringio_setstate(stringio *self, PyObject *state)\r
+{\r
+    PyObject *initarg;\r
+    PyObject *position_obj;\r
+    PyObject *dict;\r
+    Py_ssize_t pos;\r
+\r
+    assert(state != NULL);\r
+    CHECK_CLOSED(self);\r
+\r
+    /* We allow the state tuple to be longer than 4, because we may need\r
+       someday to extend the object's state without breaking\r
+       backward-compatibility. */\r
+    if (!PyTuple_Check(state) || Py_SIZE(state) < 4) {\r
+        PyErr_Format(PyExc_TypeError,\r
+                     "%.200s.__setstate__ argument should be 4-tuple, got %.200s",\r
+                     Py_TYPE(self)->tp_name, Py_TYPE(state)->tp_name);\r
+        return NULL;\r
+    }\r
+\r
+    /* Initialize the object's state. */\r
+    initarg = PyTuple_GetSlice(state, 0, 2);\r
+    if (initarg == NULL)\r
+        return NULL;\r
+    if (stringio_init(self, initarg, NULL) < 0) {\r
+        Py_DECREF(initarg);\r
+        return NULL;\r
+    }\r
+    Py_DECREF(initarg);\r
+\r
+    /* Restore the buffer state. Even if __init__ did initialize the buffer,\r
+       we have to initialize it again since __init__ may translates the\r
+       newlines in the inital_value string. We clearly do not want that\r
+       because the string value in the state tuple has already been translated\r
+       once by __init__. So we do not take any chance and replace object's\r
+       buffer completely. */\r
+    {\r
+        Py_UNICODE *buf = PyUnicode_AS_UNICODE(PyTuple_GET_ITEM(state, 0));\r
+        Py_ssize_t bufsize = PyUnicode_GET_SIZE(PyTuple_GET_ITEM(state, 0));\r
+        if (resize_buffer(self, bufsize) < 0)\r
+            return NULL;\r
+        memcpy(self->buf, buf, bufsize * sizeof(Py_UNICODE));\r
+        self->string_size = bufsize;\r
+    }\r
+\r
+    /* Set carefully the position value. Alternatively, we could use the seek\r
+       method instead of modifying self->pos directly to better protect the\r
+       object internal state against errneous (or malicious) inputs. */\r
+    position_obj = PyTuple_GET_ITEM(state, 2);\r
+    if (!PyIndex_Check(position_obj)) {\r
+        PyErr_Format(PyExc_TypeError,\r
+                     "third item of state must be an integer, got %.200s",\r
+                     Py_TYPE(position_obj)->tp_name);\r
+        return NULL;\r
+    }\r
+    pos = PyNumber_AsSsize_t(position_obj, PyExc_OverflowError);\r
+    if (pos == -1 && PyErr_Occurred())\r
+        return NULL;\r
+    if (pos < 0) {\r
+        PyErr_SetString(PyExc_ValueError,\r
+                        "position value cannot be negative");\r
+        return NULL;\r
+    }\r
+    self->pos = pos;\r
+\r
+    /* Set the dictionary of the instance variables. */\r
+    dict = PyTuple_GET_ITEM(state, 3);\r
+    if (dict != Py_None) {\r
+        if (!PyDict_Check(dict)) {\r
+            PyErr_Format(PyExc_TypeError,\r
+                         "fourth item of state should be a dict, got a %.200s",\r
+                         Py_TYPE(dict)->tp_name);\r
+            return NULL;\r
+        }\r
+        if (self->dict) {\r
+            /* Alternatively, we could replace the internal dictionary\r
+               completely. However, it seems more practical to just update it. */\r
+            if (PyDict_Update(self->dict, dict) < 0)\r
+                return NULL;\r
+        }\r
+        else {\r
+            Py_INCREF(dict);\r
+            self->dict = dict;\r
+        }\r
+    }\r
+\r
+    Py_RETURN_NONE;\r
+}\r
+\r
+\r
+static PyObject *\r
+stringio_closed(stringio *self, void *context)\r
+{\r
+    CHECK_INITIALIZED(self);\r
+    return PyBool_FromLong(self->closed);\r
+}\r
+\r
+static PyObject *\r
+stringio_line_buffering(stringio *self, void *context)\r
+{\r
+    CHECK_INITIALIZED(self);\r
+    CHECK_CLOSED(self);\r
+    Py_RETURN_FALSE;\r
+}\r
+\r
+static PyObject *\r
+stringio_newlines(stringio *self, void *context)\r
+{\r
+    CHECK_INITIALIZED(self);\r
+    CHECK_CLOSED(self);\r
+    if (self->decoder == NULL)\r
+        Py_RETURN_NONE;\r
+    return PyObject_GetAttr(self->decoder, _PyIO_str_newlines);\r
+}\r
+\r
+static struct PyMethodDef stringio_methods[] = {\r
+    {"close",    (PyCFunction)stringio_close,    METH_NOARGS,  stringio_close_doc},\r
+    {"getvalue", (PyCFunction)stringio_getvalue, METH_NOARGS,  stringio_getvalue_doc},\r
+    {"read",     (PyCFunction)stringio_read,     METH_VARARGS, stringio_read_doc},\r
+    {"readline", (PyCFunction)stringio_readline, METH_VARARGS, stringio_readline_doc},\r
+    {"tell",     (PyCFunction)stringio_tell,     METH_NOARGS,  stringio_tell_doc},\r
+    {"truncate", (PyCFunction)stringio_truncate, METH_VARARGS, stringio_truncate_doc},\r
+    {"seek",     (PyCFunction)stringio_seek,     METH_VARARGS, stringio_seek_doc},\r
+    {"write",    (PyCFunction)stringio_write,    METH_O,       stringio_write_doc},\r
+\r
+    {"seekable", (PyCFunction)stringio_seekable, METH_NOARGS, stringio_seekable_doc},\r
+    {"readable", (PyCFunction)stringio_readable, METH_NOARGS, stringio_readable_doc},\r
+    {"writable", (PyCFunction)stringio_writable, METH_NOARGS, stringio_writable_doc},\r
+\r
+    {"__getstate__", (PyCFunction)stringio_getstate, METH_NOARGS},\r
+    {"__setstate__", (PyCFunction)stringio_setstate, METH_O},\r
+    {NULL, NULL}        /* sentinel */\r
+};\r
+\r
+static PyGetSetDef stringio_getset[] = {\r
+    {"closed",         (getter)stringio_closed,         NULL, NULL},\r
+    {"newlines",       (getter)stringio_newlines,       NULL, NULL},\r
+    /*  (following comments straight off of the original Python wrapper:)\r
+        XXX Cruft to support the TextIOWrapper API. This would only\r
+        be meaningful if StringIO supported the buffer attribute.\r
+        Hopefully, a better solution, than adding these pseudo-attributes,\r
+        will be found.\r
+    */\r
+    {"line_buffering", (getter)stringio_line_buffering, NULL, NULL},\r
+    {NULL}\r
+};\r
+\r
+PyTypeObject PyStringIO_Type = {\r
+    PyVarObject_HEAD_INIT(NULL, 0)\r
+    "_io.StringIO",                            /*tp_name*/\r
+    sizeof(stringio),                    /*tp_basicsize*/\r
+    0,                                         /*tp_itemsize*/\r
+    (destructor)stringio_dealloc,              /*tp_dealloc*/\r
+    0,                                         /*tp_print*/\r
+    0,                                         /*tp_getattr*/\r
+    0,                                         /*tp_setattr*/\r
+    0,                                         /*tp_reserved*/\r
+    0,                                         /*tp_repr*/\r
+    0,                                         /*tp_as_number*/\r
+    0,                                         /*tp_as_sequence*/\r
+    0,                                         /*tp_as_mapping*/\r
+    0,                                         /*tp_hash*/\r
+    0,                                         /*tp_call*/\r
+    0,                                         /*tp_str*/\r
+    0,                                         /*tp_getattro*/\r
+    0,                                         /*tp_setattro*/\r
+    0,                                         /*tp_as_buffer*/\r
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE\r
+                       | Py_TPFLAGS_HAVE_GC,   /*tp_flags*/\r
+    stringio_doc,                              /*tp_doc*/\r
+    (traverseproc)stringio_traverse,           /*tp_traverse*/\r
+    (inquiry)stringio_clear,                   /*tp_clear*/\r
+    0,                                         /*tp_richcompare*/\r
+    offsetof(stringio, weakreflist),            /*tp_weaklistoffset*/\r
+    0,                                         /*tp_iter*/\r
+    (iternextfunc)stringio_iternext,           /*tp_iternext*/\r
+    stringio_methods,                          /*tp_methods*/\r
+    0,                                         /*tp_members*/\r
+    stringio_getset,                           /*tp_getset*/\r
+    0,                                         /*tp_base*/\r
+    0,                                         /*tp_dict*/\r
+    0,                                         /*tp_descr_get*/\r
+    0,                                         /*tp_descr_set*/\r
+    offsetof(stringio, dict),                  /*tp_dictoffset*/\r
+    (initproc)stringio_init,                   /*tp_init*/\r
+    0,                                         /*tp_alloc*/\r
+    stringio_new,                              /*tp_new*/\r
+};\r