annotate Modules/_io/textio.c @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents ba1c48f8b571
children 751a91e332d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2 An implementation of Text I/O as defined by PEP 3116 - "New I/O"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
3
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
4 Classes defined here: TextIOBase, IncrementalNewlineDecoder, TextIOWrapper.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
5
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
6 Written by Amaury Forgeot d'Arc and Antoine Pitrou
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
7 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
8
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
9 #define PY_SSIZE_T_CLEAN
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
10 #include "Python.h"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
11 #include "structmember.h"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
12 #include "_iomodule.h"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
13
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
14 /* TextIOBase */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
15
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
16 PyDoc_STRVAR(textiobase_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
17 "Base class for text I/O.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
18 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
19 "This class provides a character and line based interface to stream\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
20 "I/O. There is no readinto method because Python's character strings\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
21 "are immutable. There is no public constructor.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
22 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
23
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
24 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
25 _unsupported(const char *message)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
26 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
27 PyErr_SetString(_PyIO_unsupported_operation, message);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
28 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
29 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
30
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
31 PyDoc_STRVAR(textiobase_detach_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
32 "Separate the underlying buffer from the TextIOBase and return it.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
33 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
34 "After the underlying buffer has been detached, the TextIO is in an\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
35 "unusable state.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
36 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
37
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
38 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
39 textiobase_detach(PyObject *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
40 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
41 return _unsupported("detach");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
42 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
43
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
44 PyDoc_STRVAR(textiobase_read_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
45 "Read at most n characters from stream.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
46 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
47 "Read from underlying buffer until we have n characters or we hit EOF.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
48 "If n is negative or omitted, read until EOF.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
49 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
50
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
51 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
52 textiobase_read(PyObject *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
53 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
54 return _unsupported("read");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
55 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
56
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
57 PyDoc_STRVAR(textiobase_readline_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
58 "Read until newline or EOF.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
59 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
60 "Returns an empty string if EOF is hit immediately.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
61 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
62
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
63 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
64 textiobase_readline(PyObject *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
65 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
66 return _unsupported("readline");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
67 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
68
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
69 PyDoc_STRVAR(textiobase_write_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
70 "Write string to stream.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
71 "Returns the number of characters written (which is always equal to\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
72 "the length of the string).\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
73 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
74
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
75 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
76 textiobase_write(PyObject *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
77 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
78 return _unsupported("write");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
79 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
80
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
81 PyDoc_STRVAR(textiobase_encoding_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
82 "Encoding of the text stream.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
83 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
84 "Subclasses should override.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
85 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
86
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
87 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
88 textiobase_encoding_get(PyObject *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
89 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
90 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
91 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
92
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
93 PyDoc_STRVAR(textiobase_newlines_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
94 "Line endings translated so far.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
95 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
96 "Only line endings translated during reading are considered.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
97 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
98 "Subclasses should override.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
99 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
100
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
101 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
102 textiobase_newlines_get(PyObject *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
103 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
104 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
105 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
106
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
107 PyDoc_STRVAR(textiobase_errors_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
108 "The error setting of the decoder or encoder.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
109 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
110 "Subclasses should override.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
111 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
112
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
113 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
114 textiobase_errors_get(PyObject *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
115 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
116 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
117 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
118
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
119
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
120 static PyMethodDef textiobase_methods[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
121 {"detach", (PyCFunction)textiobase_detach, METH_NOARGS, textiobase_detach_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
122 {"read", textiobase_read, METH_VARARGS, textiobase_read_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
123 {"readline", textiobase_readline, METH_VARARGS, textiobase_readline_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
124 {"write", textiobase_write, METH_VARARGS, textiobase_write_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
125 {NULL, NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
126 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
127
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
128 static PyGetSetDef textiobase_getset[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
129 {"encoding", (getter)textiobase_encoding_get, NULL, textiobase_encoding_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
130 {"newlines", (getter)textiobase_newlines_get, NULL, textiobase_newlines_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
131 {"errors", (getter)textiobase_errors_get, NULL, textiobase_errors_doc},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
132 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
133 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
134
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
135 PyTypeObject PyTextIOBase_Type = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
136 PyVarObject_HEAD_INIT(NULL, 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
137 "_io._TextIOBase", /*tp_name*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
138 0, /*tp_basicsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
139 0, /*tp_itemsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
140 0, /*tp_dealloc*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
141 0, /*tp_print*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
142 0, /*tp_getattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
143 0, /*tp_setattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
144 0, /*tp_compare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
145 0, /*tp_repr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
146 0, /*tp_as_number*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
147 0, /*tp_as_sequence*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
148 0, /*tp_as_mapping*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
149 0, /*tp_hash */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
150 0, /*tp_call*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
151 0, /*tp_str*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
152 0, /*tp_getattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
153 0, /*tp_setattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
154 0, /*tp_as_buffer*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
155 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
156 textiobase_doc, /* tp_doc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
157 0, /* tp_traverse */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
158 0, /* tp_clear */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
159 0, /* tp_richcompare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
160 0, /* tp_weaklistoffset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
161 0, /* tp_iter */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
162 0, /* tp_iternext */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
163 textiobase_methods, /* tp_methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
164 0, /* tp_members */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
165 textiobase_getset, /* tp_getset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
166 &PyIOBase_Type, /* tp_base */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
167 0, /* tp_dict */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
168 0, /* tp_descr_get */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
169 0, /* tp_descr_set */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
170 0, /* tp_dictoffset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
171 0, /* tp_init */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
172 0, /* tp_alloc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
173 0, /* tp_new */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
174 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
175
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
176
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
177 /* IncrementalNewlineDecoder */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
178
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
179 PyDoc_STRVAR(incrementalnewlinedecoder_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
180 "Codec used when reading a file in universal newlines mode. It wraps\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
181 "another incremental decoder, translating \\r\\n and \\r into \\n. It also\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
182 "records the types of newlines encountered. When used with\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
183 "translate=False, it ensures that the newline sequence is returned in\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
184 "one piece. When used with decoder=None, it expects unicode strings as\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
185 "decode input and translates newlines without first invoking an external\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
186 "decoder.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
187 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
188
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
189 typedef struct {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
190 PyObject_HEAD
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
191 PyObject *decoder;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
192 PyObject *errors;
56171
f73cdae197c4 Issue #6236, #6348: Fix various failures in the io module under AIX
Antoine Pitrou <solipsis@pitrou.net>
parents: 55756
diff changeset
193 signed int pendingcr: 1;
f73cdae197c4 Issue #6236, #6348: Fix various failures in the io module under AIX
Antoine Pitrou <solipsis@pitrou.net>
parents: 55756
diff changeset
194 signed int translate: 1;
f73cdae197c4 Issue #6236, #6348: Fix various failures in the io module under AIX
Antoine Pitrou <solipsis@pitrou.net>
parents: 55756
diff changeset
195 unsigned int seennl: 3;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
196 } nldecoder_object;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
197
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
198 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
199 incrementalnewlinedecoder_init(nldecoder_object *self,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
200 PyObject *args, PyObject *kwds)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
201 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
202 PyObject *decoder;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
203 int translate;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
204 PyObject *errors = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
205 char *kwlist[] = {"decoder", "translate", "errors", NULL};
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
206
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
207 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oi|O:IncrementalNewlineDecoder",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
208 kwlist, &decoder, &translate, &errors))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
209 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
210
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
211 self->decoder = decoder;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
212 Py_INCREF(decoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
213
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
214 if (errors == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
215 self->errors = PyUnicode_FromString("strict");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
216 if (self->errors == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
217 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
218 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
219 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
220 Py_INCREF(errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
221 self->errors = errors;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
222 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
223
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
224 self->translate = translate;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
225 self->seennl = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
226 self->pendingcr = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
227
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
228 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
229 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
230
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
231 static void
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
232 incrementalnewlinedecoder_dealloc(nldecoder_object *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
233 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
234 Py_CLEAR(self->decoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
235 Py_CLEAR(self->errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
236 Py_TYPE(self)->tp_free((PyObject *)self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
237 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
238
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
239 #define SEEN_CR 1
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
240 #define SEEN_LF 2
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
241 #define SEEN_CRLF 4
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
242 #define SEEN_ALL (SEEN_CR | SEEN_LF | SEEN_CRLF)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
243
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
244 PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
245 _PyIncrementalNewlineDecoder_decode(PyObject *_self,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
246 PyObject *input, int final)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
247 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
248 PyObject *output;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
249 Py_ssize_t output_len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
250 nldecoder_object *self = (nldecoder_object *) _self;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
251
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
252 if (self->decoder == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
253 PyErr_SetString(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
254 "IncrementalNewlineDecoder.__init__ not called");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
255 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
256 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
257
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
258 /* decode input (with the eventual \r from a previous pass) */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
259 if (self->decoder != Py_None) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
260 output = PyObject_CallMethodObjArgs(self->decoder,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
261 _PyIO_str_decode, input, final ? Py_True : Py_False, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
262 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
263 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
264 output = input;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
265 Py_INCREF(output);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
266 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
267
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
268 if (output == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
269 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
270
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
271 if (!PyUnicode_Check(output)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
272 PyErr_SetString(PyExc_TypeError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
273 "decoder should return a string result");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
274 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
275 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
276
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
277 output_len = PyUnicode_GET_SIZE(output);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
278 if (self->pendingcr && (final || output_len > 0)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
279 Py_UNICODE *out;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
280 PyObject *modified = PyUnicode_FromUnicode(NULL, output_len + 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
281 if (modified == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
282 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
283 out = PyUnicode_AS_UNICODE(modified);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
284 out[0] = '\r';
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
285 memcpy(out + 1, PyUnicode_AS_UNICODE(output),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
286 output_len * sizeof(Py_UNICODE));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
287 Py_DECREF(output);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
288 output = modified;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
289 self->pendingcr = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
290 output_len++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
291 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
292
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
293 /* retain last \r even when not translating data:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
294 * then readline() is sure to get \r\n in one pass
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
295 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
296 if (!final) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
297 if (output_len > 0
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
298 && PyUnicode_AS_UNICODE(output)[output_len - 1] == '\r') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
299
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
300 if (Py_REFCNT(output) == 1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
301 if (PyUnicode_Resize(&output, output_len - 1) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
302 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
303 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
304 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
305 PyObject *modified = PyUnicode_FromUnicode(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
306 PyUnicode_AS_UNICODE(output),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
307 output_len - 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
308 if (modified == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
309 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
310 Py_DECREF(output);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
311 output = modified;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
312 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
313 self->pendingcr = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
314 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
315 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
316
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
317 /* Record which newlines are read and do newline translation if desired,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
318 all in one pass. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
319 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
320 Py_UNICODE *in_str;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
321 Py_ssize_t len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
322 int seennl = self->seennl;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
323 int only_lf = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
324
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
325 in_str = PyUnicode_AS_UNICODE(output);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
326 len = PyUnicode_GET_SIZE(output);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
327
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
328 if (len == 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
329 return output;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
330
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
331 /* If, up to now, newlines are consistently \n, do a quick check
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
332 for the \r *byte* with the libc's optimized memchr.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
333 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
334 if (seennl == SEEN_LF || seennl == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
335 only_lf = (memchr(in_str, '\r', len * sizeof(Py_UNICODE)) == NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
336 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
337
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
338 if (only_lf) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
339 /* If not already seen, quick scan for a possible "\n" character.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
340 (there's nothing else to be done, even when in translation mode)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
341 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
342 if (seennl == 0 &&
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
343 memchr(in_str, '\n', len * sizeof(Py_UNICODE)) != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
344 Py_UNICODE *s, *end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
345 s = in_str;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
346 end = in_str + len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
347 for (;;) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
348 Py_UNICODE c;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
349 /* Fast loop for non-control characters */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
350 while (*s > '\n')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
351 s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
352 c = *s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
353 if (c == '\n') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
354 seennl |= SEEN_LF;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
355 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
356 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
357 if (s > end)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
358 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
359 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
360 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
361 /* Finished: we have scanned for newlines, and none of them
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
362 need translating */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
363 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
364 else if (!self->translate) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
365 Py_UNICODE *s, *end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
366 /* We have already seen all newline types, no need to scan again */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
367 if (seennl == SEEN_ALL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
368 goto endscan;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
369 s = in_str;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
370 end = in_str + len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
371 for (;;) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
372 Py_UNICODE c;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
373 /* Fast loop for non-control characters */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
374 while (*s > '\r')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
375 s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
376 c = *s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
377 if (c == '\n')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
378 seennl |= SEEN_LF;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
379 else if (c == '\r') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
380 if (*s == '\n') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
381 seennl |= SEEN_CRLF;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
382 s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
383 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
384 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
385 seennl |= SEEN_CR;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
386 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
387 if (s > end)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
388 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
389 if (seennl == SEEN_ALL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
390 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
391 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
392 endscan:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
393 ;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
394 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
395 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
396 PyObject *translated = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
397 Py_UNICODE *out_str;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
398 Py_UNICODE *in, *out, *end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
399 if (Py_REFCNT(output) != 1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
400 /* We could try to optimize this so that we only do a copy
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
401 when there is something to translate. On the other hand,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
402 most decoders should only output non-shared strings, i.e.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
403 translation is done in place. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
404 translated = PyUnicode_FromUnicode(NULL, len);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
405 if (translated == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
406 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
407 assert(Py_REFCNT(translated) == 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
408 memcpy(PyUnicode_AS_UNICODE(translated),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
409 PyUnicode_AS_UNICODE(output),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
410 len * sizeof(Py_UNICODE));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
411 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
412 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
413 translated = output;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
414 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
415 out_str = PyUnicode_AS_UNICODE(translated);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
416 in = in_str;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
417 out = out_str;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
418 end = in_str + len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
419 for (;;) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
420 Py_UNICODE c;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
421 /* Fast loop for non-control characters */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
422 while ((c = *in++) > '\r')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
423 *out++ = c;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
424 if (c == '\n') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
425 *out++ = c;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
426 seennl |= SEEN_LF;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
427 continue;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
428 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
429 if (c == '\r') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
430 if (*in == '\n') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
431 in++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
432 seennl |= SEEN_CRLF;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
433 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
434 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
435 seennl |= SEEN_CR;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
436 *out++ = '\n';
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
437 continue;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
438 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
439 if (in > end)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
440 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
441 *out++ = c;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
442 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
443 if (translated != output) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
444 Py_DECREF(output);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
445 output = translated;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
446 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
447 if (out - out_str != len) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
448 if (PyUnicode_Resize(&output, out - out_str) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
449 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
450 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
451 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
452 self->seennl |= seennl;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
453 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
454
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
455 return output;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
456
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
457 error:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
458 Py_DECREF(output);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
459 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
460 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
461
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
462 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
463 incrementalnewlinedecoder_decode(nldecoder_object *self,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
464 PyObject *args, PyObject *kwds)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
465 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
466 char *kwlist[] = {"input", "final", NULL};
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
467 PyObject *input;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
468 int final = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
469
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
470 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|i:IncrementalNewlineDecoder",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
471 kwlist, &input, &final))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
472 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
473 return _PyIncrementalNewlineDecoder_decode((PyObject *) self, input, final);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
474 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
475
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
476 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
477 incrementalnewlinedecoder_getstate(nldecoder_object *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
478 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
479 PyObject *buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
480 unsigned PY_LONG_LONG flag;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
481
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
482 if (self->decoder != Py_None) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
483 PyObject *state = PyObject_CallMethodObjArgs(self->decoder,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
484 _PyIO_str_getstate, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
485 if (state == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
486 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
487 if (!PyArg_Parse(state, "(OK)", &buffer, &flag)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
488 Py_DECREF(state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
489 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
490 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
491 Py_INCREF(buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
492 Py_DECREF(state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
493 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
494 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
495 buffer = PyBytes_FromString("");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
496 flag = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
497 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
498 flag <<= 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
499 if (self->pendingcr)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
500 flag |= 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
501 return Py_BuildValue("NK", buffer, flag);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
502 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
503
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
504 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
505 incrementalnewlinedecoder_setstate(nldecoder_object *self, PyObject *state)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
506 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
507 PyObject *buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
508 unsigned PY_LONG_LONG flag;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
509
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
510 if (!PyArg_Parse(state, "(OK)", &buffer, &flag))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
511 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
512
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
513 self->pendingcr = (int) flag & 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
514 flag >>= 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
515
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
516 if (self->decoder != Py_None)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
517 return PyObject_CallMethod(self->decoder,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
518 "setstate", "((OK))", buffer, flag);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
519 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
520 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
521 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
522
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
523 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
524 incrementalnewlinedecoder_reset(nldecoder_object *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
525 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
526 self->seennl = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
527 self->pendingcr = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
528 if (self->decoder != Py_None)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
529 return PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_reset, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
530 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
531 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
532 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
533
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
534 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
535 incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
536 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
537 switch (self->seennl) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
538 case SEEN_CR:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
539 return PyUnicode_FromString("\r");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
540 case SEEN_LF:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
541 return PyUnicode_FromString("\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
542 case SEEN_CRLF:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
543 return PyUnicode_FromString("\r\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
544 case SEEN_CR | SEEN_LF:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
545 return Py_BuildValue("ss", "\r", "\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
546 case SEEN_CR | SEEN_CRLF:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
547 return Py_BuildValue("ss", "\r", "\r\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
548 case SEEN_LF | SEEN_CRLF:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
549 return Py_BuildValue("ss", "\n", "\r\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
550 case SEEN_CR | SEEN_LF | SEEN_CRLF:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
551 return Py_BuildValue("sss", "\r", "\n", "\r\n");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
552 default:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
553 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
554 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
555
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
556 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
557
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
558
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
559 static PyMethodDef incrementalnewlinedecoder_methods[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
560 {"decode", (PyCFunction)incrementalnewlinedecoder_decode, METH_VARARGS|METH_KEYWORDS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
561 {"getstate", (PyCFunction)incrementalnewlinedecoder_getstate, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
562 {"setstate", (PyCFunction)incrementalnewlinedecoder_setstate, METH_O},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
563 {"reset", (PyCFunction)incrementalnewlinedecoder_reset, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
564 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
565 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
566
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
567 static PyGetSetDef incrementalnewlinedecoder_getset[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
568 {"newlines", (getter)incrementalnewlinedecoder_newlines_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
569 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
570 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
571
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
572 PyTypeObject PyIncrementalNewlineDecoder_Type = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
573 PyVarObject_HEAD_INIT(NULL, 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
574 "_io.IncrementalNewlineDecoder", /*tp_name*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
575 sizeof(nldecoder_object), /*tp_basicsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
576 0, /*tp_itemsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
577 (destructor)incrementalnewlinedecoder_dealloc, /*tp_dealloc*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
578 0, /*tp_print*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
579 0, /*tp_getattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
580 0, /*tp_setattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
581 0, /*tp_compare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
582 0, /*tp_repr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
583 0, /*tp_as_number*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
584 0, /*tp_as_sequence*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
585 0, /*tp_as_mapping*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
586 0, /*tp_hash */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
587 0, /*tp_call*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
588 0, /*tp_str*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
589 0, /*tp_getattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
590 0, /*tp_setattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
591 0, /*tp_as_buffer*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
592 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
593 incrementalnewlinedecoder_doc, /* tp_doc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
594 0, /* tp_traverse */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
595 0, /* tp_clear */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
596 0, /* tp_richcompare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
597 0, /*tp_weaklistoffset*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
598 0, /* tp_iter */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
599 0, /* tp_iternext */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
600 incrementalnewlinedecoder_methods, /* tp_methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
601 0, /* tp_members */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
602 incrementalnewlinedecoder_getset, /* tp_getset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
603 0, /* tp_base */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
604 0, /* tp_dict */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
605 0, /* tp_descr_get */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
606 0, /* tp_descr_set */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
607 0, /* tp_dictoffset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
608 (initproc)incrementalnewlinedecoder_init, /* tp_init */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
609 0, /* tp_alloc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
610 PyType_GenericNew, /* tp_new */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
611 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
612
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
613
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
614 /* TextIOWrapper */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
615
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
616 PyDoc_STRVAR(textiowrapper_doc,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
617 "Character and line based layer over a BufferedIOBase object, buffer.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
618 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
619 "encoding gives the name of the encoding that the stream will be\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
620 "decoded or encoded with. It defaults to locale.getpreferredencoding.\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
621 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
622 "errors determines the strictness of encoding and decoding (see the\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
623 "codecs.register) and defaults to \"strict\".\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
624 "\n"
78402
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
625 "newline controls how line endings are handled. It can be None, '',\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
626 "'\\n', '\\r', and '\\r\\n'. It works as follows:\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
627 "\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
628 "* On input, if newline is None, universal newlines mode is\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
629 " enabled. Lines in the input can end in '\\n', '\\r', or '\\r\\n', and\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
630 " these are translated into '\\n' before being returned to the\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
631 " caller. If it is '', universal newline mode is enabled, but line\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
632 " endings are returned to the caller untranslated. If it has any of\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
633 " the other legal values, input lines are only terminated by the given\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
634 " string, and the line ending is returned to the caller untranslated.\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
635 "\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
636 "* On output, if newline is None, any '\\n' characters written are\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
637 " translated to the system default line separator, os.linesep. If\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
638 " newline is '', no translation takes place. If newline is any of the\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
639 " other legal values, any '\\n' characters written are translated to\n"
c52e709577b3 Make TextIOWrapper's documentation clearer by copying the newline argument's description from open().
Antoine Pitrou <solipsis@pitrou.net>
parents: 75276
diff changeset
640 " the given string.\n"
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
641 "\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
642 "If line_buffering is True, a call to flush is implied when a call to\n"
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
643 "write contains a newline character."
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
644 );
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
645
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
646 typedef PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
647 (*encodefunc_t)(PyObject *, PyObject *);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
648
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
649 typedef struct
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
650 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
651 PyObject_HEAD
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
652 int ok; /* initialized? */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
653 int detached;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
654 Py_ssize_t chunk_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
655 PyObject *buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
656 PyObject *encoding;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
657 PyObject *encoder;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
658 PyObject *decoder;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
659 PyObject *readnl;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
660 PyObject *errors;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
661 const char *writenl; /* utf-8 encoded, NULL stands for \n */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
662 char line_buffering;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
663 char readuniversal;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
664 char readtranslate;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
665 char writetranslate;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
666 char seekable;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
667 char telling;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
668 /* Specialized encoding func (see below) */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
669 encodefunc_t encodefunc;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
670 /* Whether or not it's the start of the stream */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
671 char encoding_start_of_stream;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
672
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
673 /* Reads and writes are internally buffered in order to speed things up.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
674 However, any read will first flush the write buffer if itsn't empty.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
675
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
676 Please also note that text to be written is first encoded before being
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
677 buffered. This is necessary so that encoding errors are immediately
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
678 reported to the caller, but it unfortunately means that the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
679 IncrementalEncoder (whose encode() method is always written in Python)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
680 becomes a bottleneck for small writes.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
681 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
682 PyObject *decoded_chars; /* buffer for text returned from decoder */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
683 Py_ssize_t decoded_chars_used; /* offset into _decoded_chars for read() */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
684 PyObject *pending_bytes; /* list of bytes objects waiting to be
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
685 written, or NULL */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
686 Py_ssize_t pending_bytes_count;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
687 PyObject *snapshot;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
688 /* snapshot is either None, or a tuple (dec_flags, next_input) where
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
689 * dec_flags is the second (integer) item of the decoder state and
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
690 * next_input is the chunk of input bytes that comes next after the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
691 * snapshot point. We use this to reconstruct decoder states in tell().
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
692 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
693
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
694 /* Cache raw object if it's a FileIO object */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
695 PyObject *raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
696
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
697 PyObject *weakreflist;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
698 PyObject *dict;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
699 } textio;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
700
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
701
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
702 /* A couple of specialized cases in order to bypass the slow incremental
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
703 encoding methods for the most popular encodings. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
704
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
705 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
706 ascii_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
707 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
708 return PyUnicode_EncodeASCII(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
709 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
710 PyBytes_AS_STRING(self->errors));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
711 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
712
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
713 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
714 utf16be_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
715 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
716 return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
717 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
718 PyBytes_AS_STRING(self->errors), 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
719 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
720
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
721 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
722 utf16le_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
723 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
724 return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
725 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
726 PyBytes_AS_STRING(self->errors), -1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
727 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
728
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
729 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
730 utf16_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
731 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
732 if (!self->encoding_start_of_stream) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
733 /* Skip the BOM and use native byte ordering */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
734 #if defined(WORDS_BIGENDIAN)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
735 return utf16be_encode(self, text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
736 #else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
737 return utf16le_encode(self, text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
738 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
739 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
740 return PyUnicode_EncodeUTF16(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
741 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
742 PyBytes_AS_STRING(self->errors), 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
743 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
744
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
745 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
746 utf32be_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
747 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
748 return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
749 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
750 PyBytes_AS_STRING(self->errors), 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
751 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
752
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
753 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
754 utf32le_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
755 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
756 return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
757 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
758 PyBytes_AS_STRING(self->errors), -1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
759 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
760
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
761 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
762 utf32_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
763 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
764 if (!self->encoding_start_of_stream) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
765 /* Skip the BOM and use native byte ordering */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
766 #if defined(WORDS_BIGENDIAN)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
767 return utf32be_encode(self, text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
768 #else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
769 return utf32le_encode(self, text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
770 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
771 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
772 return PyUnicode_EncodeUTF32(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
773 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
774 PyBytes_AS_STRING(self->errors), 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
775 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
776
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
777 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
778 utf8_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
779 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
780 return PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
781 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
782 PyBytes_AS_STRING(self->errors));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
783 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
784
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
785 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
786 latin1_encode(textio *self, PyObject *text)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
787 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
788 return PyUnicode_EncodeLatin1(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
789 PyUnicode_GET_SIZE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
790 PyBytes_AS_STRING(self->errors));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
791 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
792
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
793 /* Map normalized encoding names onto the specialized encoding funcs */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
794
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
795 typedef struct {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
796 const char *name;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
797 encodefunc_t encodefunc;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
798 } encodefuncentry;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
799
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
800 static encodefuncentry encodefuncs[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
801 {"ascii", (encodefunc_t) ascii_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
802 {"iso8859-1", (encodefunc_t) latin1_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
803 {"utf-8", (encodefunc_t) utf8_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
804 {"utf-16-be", (encodefunc_t) utf16be_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
805 {"utf-16-le", (encodefunc_t) utf16le_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
806 {"utf-16", (encodefunc_t) utf16_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
807 {"utf-32-be", (encodefunc_t) utf32be_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
808 {"utf-32-le", (encodefunc_t) utf32le_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
809 {"utf-32", (encodefunc_t) utf32_encode},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
810 {NULL, NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
811 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
812
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
813
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
814 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
815 textiowrapper_init(textio *self, PyObject *args, PyObject *kwds)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
816 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
817 char *kwlist[] = {"buffer", "encoding", "errors",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
818 "newline", "line_buffering",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
819 NULL};
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
820 PyObject *buffer, *raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
821 char *encoding = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
822 char *errors = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
823 char *newline = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
824 int line_buffering = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
825
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
826 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
827 int r;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
828
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
829 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
830 self->detached = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
831 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|zzzi:fileio",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
832 kwlist, &buffer, &encoding, &errors,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
833 &newline, &line_buffering))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
834 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
835
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
836 if (newline && newline[0] != '\0'
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
837 && !(newline[0] == '\n' && newline[1] == '\0')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
838 && !(newline[0] == '\r' && newline[1] == '\0')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
839 && !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
840 PyErr_Format(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
841 "illegal newline value: %s", newline);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
842 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
843 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
844
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
845 Py_CLEAR(self->buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
846 Py_CLEAR(self->encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
847 Py_CLEAR(self->encoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
848 Py_CLEAR(self->decoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
849 Py_CLEAR(self->readnl);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
850 Py_CLEAR(self->decoded_chars);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
851 Py_CLEAR(self->pending_bytes);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
852 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
853 Py_CLEAR(self->errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
854 Py_CLEAR(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
855 self->decoded_chars_used = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
856 self->pending_bytes_count = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
857 self->encodefunc = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
858 self->writenl = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
859
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
860 if (encoding == NULL && self->encoding == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
861 if (_PyIO_locale_module == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
862 _PyIO_locale_module = PyImport_ImportModule("locale");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
863 if (_PyIO_locale_module == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
864 goto catch_ImportError;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
865 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
866 goto use_locale;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
867 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
868 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
869 use_locale:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
870 self->encoding = PyObject_CallMethod(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
871 _PyIO_locale_module, "getpreferredencoding", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
872 if (self->encoding == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
873 catch_ImportError:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
874 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
875 Importing locale can raise a ImportError because of
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
876 _functools, and locale.getpreferredencoding can raise a
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
877 ImportError if _locale is not available. These will happen
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
878 during module building.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
879 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
880 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
881 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
882 self->encoding = PyString_FromString("ascii");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
883 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
884 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
885 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
886 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
887 else if (!PyString_Check(self->encoding))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
888 Py_CLEAR(self->encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
889 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
890 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
891 if (self->encoding != NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
892 encoding = PyString_AsString(self->encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
893 else if (encoding != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
894 self->encoding = PyString_FromString(encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
895 if (self->encoding == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
896 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
897 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
898 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
899 PyErr_SetString(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
900 "could not determine default encoding");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
901 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
902
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
903 if (errors == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
904 errors = "strict";
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
905 self->errors = PyBytes_FromString(errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
906 if (self->errors == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
907 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
908
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
909 self->chunk_size = 8192;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
910 self->readuniversal = (newline == NULL || newline[0] == '\0');
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
911 self->line_buffering = line_buffering;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
912 self->readtranslate = (newline == NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
913 if (newline) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
914 self->readnl = PyString_FromString(newline);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
915 if (self->readnl == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
916 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
917 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
918 self->writetranslate = (newline == NULL || newline[0] != '\0');
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
919 if (!self->readuniversal && self->writetranslate) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
920 self->writenl = PyString_AsString(self->readnl);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
921 if (!strcmp(self->writenl, "\n"))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
922 self->writenl = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
923 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
924 #ifdef MS_WINDOWS
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
925 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
926 self->writenl = "\r\n";
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
927 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
928
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
929 /* Build the decoder object */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
930 res = PyObject_CallMethod(buffer, "readable", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
931 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
932 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
933 r = PyObject_IsTrue(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
934 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
935 if (r == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
936 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
937 if (r == 1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
938 self->decoder = PyCodec_IncrementalDecoder(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
939 encoding, errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
940 if (self->decoder == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
941 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
942
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
943 if (self->readuniversal) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
944 PyObject *incrementalDecoder = PyObject_CallFunction(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
945 (PyObject *)&PyIncrementalNewlineDecoder_Type,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
946 "Oi", self->decoder, (int)self->readtranslate);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
947 if (incrementalDecoder == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
948 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
949 Py_CLEAR(self->decoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
950 self->decoder = incrementalDecoder;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
951 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
952 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
953
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
954 /* Build the encoder object */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
955 res = PyObject_CallMethod(buffer, "writable", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
956 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
957 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
958 r = PyObject_IsTrue(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
959 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
960 if (r == -1)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
961 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
962 if (r == 1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
963 PyObject *ci;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
964 self->encoder = PyCodec_IncrementalEncoder(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
965 encoding, errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
966 if (self->encoder == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
967 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
968 /* Get the normalized named of the codec */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
969 ci = _PyCodec_Lookup(encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
970 if (ci == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
971 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
972 res = PyObject_GetAttrString(ci, "name");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
973 Py_DECREF(ci);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
974 if (res == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
975 if (PyErr_ExceptionMatches(PyExc_AttributeError))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
976 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
977 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
978 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
979 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
980 else if (PyString_Check(res)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
981 encodefuncentry *e = encodefuncs;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
982 while (e->name != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
983 if (!strcmp(PyString_AS_STRING(res), e->name)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
984 self->encodefunc = e->encodefunc;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
985 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
986 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
987 e++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
988 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
989 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
990 Py_XDECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
991 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
992
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
993 self->buffer = buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
994 Py_INCREF(buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
995
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
996 if (Py_TYPE(buffer) == &PyBufferedReader_Type ||
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
997 Py_TYPE(buffer) == &PyBufferedWriter_Type ||
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
998 Py_TYPE(buffer) == &PyBufferedRandom_Type) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
999 raw = PyObject_GetAttrString(buffer, "raw");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1000 /* Cache the raw FileIO object to speed up 'closed' checks */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1001 if (raw == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1002 if (PyErr_ExceptionMatches(PyExc_AttributeError))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1003 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1004 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1005 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1006 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1007 else if (Py_TYPE(raw) == &PyFileIO_Type)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1008 self->raw = raw;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1009 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1010 Py_DECREF(raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1011 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1012
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1013 res = PyObject_CallMethod(buffer, "seekable", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1014 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1015 goto error;
78603
ba1c48f8b571 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78402
diff changeset
1016 r = PyObject_IsTrue(res);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1017 Py_DECREF(res);
78603
ba1c48f8b571 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78402
diff changeset
1018 if (r < 0)
ba1c48f8b571 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78402
diff changeset
1019 goto error;
ba1c48f8b571 Issue #15604: Update uses of PyObject_IsTrue() to check for and handle errors correctly.
Antoine Pitrou <solipsis@pitrou.net>
parents: 78402
diff changeset
1020 self->seekable = self->telling = r;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1021
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1022 self->encoding_start_of_stream = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1023 if (self->seekable && self->encoder) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1024 PyObject *cookieObj;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1025 int cmp;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1026
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1027 self->encoding_start_of_stream = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1028
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1029 cookieObj = PyObject_CallMethodObjArgs(buffer, _PyIO_str_tell, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1030 if (cookieObj == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1031 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1032
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1033 cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1034 Py_DECREF(cookieObj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1035 if (cmp < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1036 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1037 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1038
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1039 if (cmp == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1040 self->encoding_start_of_stream = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1041 res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1042 _PyIO_zero, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1043 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1044 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1045 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1046 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1047 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1048
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1049 self->ok = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1050 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1051
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1052 error:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1053 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1054 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1055
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1056 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1057 _textiowrapper_clear(textio *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1058 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1059 if (self->ok && _PyIOBase_finalize((PyObject *) self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1060 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1061 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1062 Py_CLEAR(self->buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1063 Py_CLEAR(self->encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1064 Py_CLEAR(self->encoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1065 Py_CLEAR(self->decoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1066 Py_CLEAR(self->readnl);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1067 Py_CLEAR(self->decoded_chars);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1068 Py_CLEAR(self->pending_bytes);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1069 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1070 Py_CLEAR(self->errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1071 Py_CLEAR(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1072 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1073 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1074
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1075 static void
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1076 textiowrapper_dealloc(textio *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1077 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1078 if (_textiowrapper_clear(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1079 return;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1080 _PyObject_GC_UNTRACK(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1081 if (self->weakreflist != NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1082 PyObject_ClearWeakRefs((PyObject *)self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1083 Py_CLEAR(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1084 Py_TYPE(self)->tp_free((PyObject *)self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1085 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1086
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1087 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1088 textiowrapper_traverse(textio *self, visitproc visit, void *arg)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1089 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1090 Py_VISIT(self->buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1091 Py_VISIT(self->encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1092 Py_VISIT(self->encoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1093 Py_VISIT(self->decoder);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1094 Py_VISIT(self->readnl);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1095 Py_VISIT(self->decoded_chars);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1096 Py_VISIT(self->pending_bytes);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1097 Py_VISIT(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1098 Py_VISIT(self->errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1099 Py_VISIT(self->raw);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1100
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1101 Py_VISIT(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1102 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1103 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1104
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1105 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1106 textiowrapper_clear(textio *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1107 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1108 if (_textiowrapper_clear(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1109 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1110 Py_CLEAR(self->dict);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1111 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1112 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1113
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1114 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1115 textiowrapper_closed_get(textio *self, void *context);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1116
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1117 /* This macro takes some shortcuts to make the common case faster. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1118 #define CHECK_CLOSED(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1119 do { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1120 int r; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1121 PyObject *_res; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1122 if (Py_TYPE(self) == &PyTextIOWrapper_Type) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1123 if (self->raw != NULL) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1124 r = _PyFileIO_closed(self->raw); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1125 else { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1126 _res = textiowrapper_closed_get(self, NULL); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1127 if (_res == NULL) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1128 return NULL; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1129 r = PyObject_IsTrue(_res); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1130 Py_DECREF(_res); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1131 if (r < 0) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1132 return NULL; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1133 } \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1134 if (r > 0) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1135 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1136 "I/O operation on closed file."); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1137 return NULL; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1138 } \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1139 } \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1140 else if (_PyIOBase_check_closed((PyObject *)self, Py_True) == NULL) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1141 return NULL; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1142 } while (0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1143
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1144 #define CHECK_INITIALIZED(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1145 if (self->ok <= 0) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1146 if (self->detached) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1147 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1148 "underlying buffer has been detached"); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1149 } else { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1150 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1151 "I/O operation on uninitialized object"); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1152 } \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1153 return NULL; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1154 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1155
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1156 #define CHECK_INITIALIZED_INT(self) \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1157 if (self->ok <= 0) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1158 if (self->detached) { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1159 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1160 "underlying buffer has been detached"); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1161 } else { \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1162 PyErr_SetString(PyExc_ValueError, \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1163 "I/O operation on uninitialized object"); \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1164 } \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1165 return -1; \
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1166 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1167
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1168
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1169 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1170 textiowrapper_detach(textio *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1171 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1172 PyObject *buffer, *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1173 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1174 res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1175 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1176 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1177 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1178 buffer = self->buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1179 self->buffer = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1180 self->detached = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1181 self->ok = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1182 return buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1183 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1184
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1185 Py_LOCAL_INLINE(const Py_UNICODE *)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1186 findchar(const Py_UNICODE *s, Py_ssize_t size, Py_UNICODE ch)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1187 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1188 /* like wcschr, but doesn't stop at NULL characters */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1189 while (size-- > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1190 if (*s == ch)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1191 return s;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1192 s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1193 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1194 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1195 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1196
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1197 /* Flush the internal write buffer. This doesn't explicitly flush the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1198 underlying buffered object, though. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1199 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1200 _textiowrapper_writeflush(textio *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1201 {
55756
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1202 PyObject *pending, *b, *ret;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1203
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1204 if (self->pending_bytes == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1205 return 0;
55756
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1206
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1207 pending = self->pending_bytes;
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1208 Py_INCREF(pending);
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1209 self->pending_bytes_count = 0;
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1210 Py_CLEAR(self->pending_bytes);
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1211
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1212 b = _PyBytes_Join(_PyIO_empty_bytes, pending);
d4037fb856d2 #6750: TextIOWrapped could duplicate output when several threads write to it.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 54717
diff changeset
1213 Py_DECREF(pending);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1214 if (b == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1215 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1216 ret = PyObject_CallMethodObjArgs(self->buffer,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1217 _PyIO_str_write, b, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1218 Py_DECREF(b);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1219 if (ret == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1220 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1221 Py_DECREF(ret);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1222 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1223 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1224
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1225 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1226 textiowrapper_write(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1227 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1228 PyObject *ret;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1229 PyObject *text; /* owned reference */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1230 PyObject *b;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1231 Py_ssize_t textlen;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1232 int haslf = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1233 int needflush = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1234
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1235 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1236
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1237 if (!PyArg_ParseTuple(args, "U:write", &text)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1238 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1239 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1240
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1241 CHECK_CLOSED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1242
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1243 if (self->encoder == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1244 PyErr_SetString(PyExc_IOError, "not writable");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1245 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1246 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1247
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1248 Py_INCREF(text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1249
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1250 textlen = PyUnicode_GetSize(text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1251
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1252 if ((self->writetranslate && self->writenl != NULL) || self->line_buffering)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1253 if (findchar(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1254 PyUnicode_GET_SIZE(text), '\n'))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1255 haslf = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1256
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1257 if (haslf && self->writetranslate && self->writenl != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1258 PyObject *newtext = PyObject_CallMethod(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1259 text, "replace", "ss", "\n", self->writenl);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1260 Py_DECREF(text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1261 if (newtext == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1262 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1263 text = newtext;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1264 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1265
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1266 if (self->line_buffering &&
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1267 (haslf ||
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1268 findchar(PyUnicode_AS_UNICODE(text),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1269 PyUnicode_GET_SIZE(text), '\r')))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1270 needflush = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1271
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1272 /* XXX What if we were just reading? */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1273 if (self->encodefunc != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1274 b = (*self->encodefunc)((PyObject *) self, text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1275 self->encoding_start_of_stream = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1276 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1277 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1278 b = PyObject_CallMethodObjArgs(self->encoder,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1279 _PyIO_str_encode, text, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1280 Py_DECREF(text);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1281 if (b == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1282 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1283
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1284 if (self->pending_bytes == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1285 self->pending_bytes = PyList_New(0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1286 if (self->pending_bytes == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1287 Py_DECREF(b);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1288 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1289 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1290 self->pending_bytes_count = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1291 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1292 if (PyList_Append(self->pending_bytes, b) < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1293 Py_DECREF(b);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1294 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1295 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1296 self->pending_bytes_count += PyBytes_GET_SIZE(b);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1297 Py_DECREF(b);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1298 if (self->pending_bytes_count > self->chunk_size || needflush) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1299 if (_textiowrapper_writeflush(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1300 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1301 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1302
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1303 if (needflush) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1304 ret = PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_flush, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1305 if (ret == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1306 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1307 Py_DECREF(ret);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1308 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1309
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1310 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1311
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1312 if (self->decoder) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1313 ret = PyObject_CallMethod(self->decoder, "reset", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1314 if (ret == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1315 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1316 Py_DECREF(ret);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1317 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1318
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1319 return PyLong_FromSsize_t(textlen);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1320 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1321
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1322 /* Steal a reference to chars and store it in the decoded_char buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1323 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1324 static void
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1325 textiowrapper_set_decoded_chars(textio *self, PyObject *chars)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1326 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1327 Py_CLEAR(self->decoded_chars);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1328 self->decoded_chars = chars;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1329 self->decoded_chars_used = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1330 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1331
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1332 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1333 textiowrapper_get_decoded_chars(textio *self, Py_ssize_t n)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1334 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1335 PyObject *chars;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1336 Py_ssize_t avail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1337
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1338 if (self->decoded_chars == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1339 return PyUnicode_FromStringAndSize(NULL, 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1340
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1341 avail = (PyUnicode_GET_SIZE(self->decoded_chars)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1342 - self->decoded_chars_used);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1343
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1344 assert(avail >= 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1345
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1346 if (n < 0 || n > avail)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1347 n = avail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1348
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1349 if (self->decoded_chars_used > 0 || n < avail) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1350 chars = PyUnicode_FromUnicode(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1351 PyUnicode_AS_UNICODE(self->decoded_chars)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1352 + self->decoded_chars_used, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1353 if (chars == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1354 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1355 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1356 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1357 chars = self->decoded_chars;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1358 Py_INCREF(chars);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1359 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1360
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1361 self->decoded_chars_used += n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1362 return chars;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1363 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1364
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1365 /* Read and decode the next chunk of data from the BufferedReader.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1366 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1367 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1368 textiowrapper_read_chunk(textio *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1369 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1370 PyObject *dec_buffer = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1371 PyObject *dec_flags = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1372 PyObject *input_chunk = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1373 PyObject *decoded_chars, *chunk_size;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1374 int eof;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1375
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1376 /* The return value is True unless EOF was reached. The decoded string is
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1377 * placed in self._decoded_chars (replacing its previous value). The
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1378 * entire input chunk is sent to the decoder, though some of it may remain
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1379 * buffered in the decoder, yet to be converted.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1380 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1381
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1382 if (self->decoder == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1383 PyErr_SetString(PyExc_IOError, "not readable");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1384 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1385 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1386
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1387 if (self->telling) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1388 /* To prepare for tell(), we need to snapshot a point in the file
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1389 * where the decoder's input buffer is empty.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1390 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1391
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1392 PyObject *state = PyObject_CallMethodObjArgs(self->decoder,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1393 _PyIO_str_getstate, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1394 if (state == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1395 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1396 /* Given this, we know there was a valid snapshot point
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1397 * len(dec_buffer) bytes ago with decoder state (b'', dec_flags).
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1398 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1399 if (PyArg_Parse(state, "(OO)", &dec_buffer, &dec_flags) < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1400 Py_DECREF(state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1401 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1402 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1403 Py_INCREF(dec_buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1404 Py_INCREF(dec_flags);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1405 Py_DECREF(state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1406 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1407
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1408 /* Read a chunk, decode it, and put the result in self._decoded_chars. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1409 chunk_size = PyLong_FromSsize_t(self->chunk_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1410 if (chunk_size == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1411 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1412 input_chunk = PyObject_CallMethodObjArgs(self->buffer,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1413 _PyIO_str_read1, chunk_size, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1414 Py_DECREF(chunk_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1415 if (input_chunk == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1416 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1417 assert(PyBytes_Check(input_chunk));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1418
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1419 eof = (PyBytes_Size(input_chunk) == 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1420
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1421 if (Py_TYPE(self->decoder) == &PyIncrementalNewlineDecoder_Type) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1422 decoded_chars = _PyIncrementalNewlineDecoder_decode(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1423 self->decoder, input_chunk, eof);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1424 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1425 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1426 decoded_chars = PyObject_CallMethodObjArgs(self->decoder,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1427 _PyIO_str_decode, input_chunk, eof ? Py_True : Py_False, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1428 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1429
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1430 /* TODO sanity check: isinstance(decoded_chars, unicode) */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1431 if (decoded_chars == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1432 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1433 textiowrapper_set_decoded_chars(self, decoded_chars);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1434 if (PyUnicode_GET_SIZE(decoded_chars) > 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1435 eof = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1436
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1437 if (self->telling) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1438 /* At the snapshot point, len(dec_buffer) bytes before the read, the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1439 * next input to be decoded is dec_buffer + input_chunk.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1440 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1441 PyObject *next_input = PyNumber_Add(dec_buffer, input_chunk);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1442 if (next_input == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1443 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1444 assert (PyBytes_Check(next_input));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1445 Py_DECREF(dec_buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1446 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1447 self->snapshot = Py_BuildValue("NN", dec_flags, next_input);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1448 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1449 Py_DECREF(input_chunk);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1450
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1451 return (eof == 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1452
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1453 fail:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1454 Py_XDECREF(dec_buffer);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1455 Py_XDECREF(dec_flags);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1456 Py_XDECREF(input_chunk);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1457 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1458 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1459
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1460 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1461 textiowrapper_read(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1462 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1463 Py_ssize_t n = -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1464 PyObject *result = NULL, *chunks = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1465
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1466 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1467
57703
54f84558ad2b accept None as the same as having passed no argument in file types #7349
Benjamin Peterson <benjamin@python.org>
parents: 56171
diff changeset
1468 if (!PyArg_ParseTuple(args, "|O&:read", &_PyIO_ConvertSsize_t, &n))
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1469 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1470
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1471 CHECK_CLOSED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1472
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1473 if (self->decoder == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1474 PyErr_SetString(PyExc_IOError, "not readable");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1475 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1476 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1477
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1478 if (_textiowrapper_writeflush(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1479 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1480
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1481 if (n < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1482 /* Read everything */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1483 PyObject *bytes = PyObject_CallMethod(self->buffer, "read", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1484 PyObject *decoded, *final;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1485 if (bytes == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1486 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1487 decoded = PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_decode,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1488 bytes, Py_True, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1489 Py_DECREF(bytes);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1490 if (decoded == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1491 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1492
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1493 result = textiowrapper_get_decoded_chars(self, -1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1494
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1495 if (result == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1496 Py_DECREF(decoded);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1497 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1498 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1499
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1500 final = PyUnicode_Concat(result, decoded);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1501 Py_DECREF(result);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1502 Py_DECREF(decoded);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1503 if (final == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1504 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1505
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1506 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1507 return final;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1508 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1509 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1510 int res = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1511 Py_ssize_t remaining = n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1512
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1513 result = textiowrapper_get_decoded_chars(self, n);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1514 if (result == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1515 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1516 remaining -= PyUnicode_GET_SIZE(result);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1517
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1518 /* Keep reading chunks until we have n characters to return */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1519 while (remaining > 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1520 res = textiowrapper_read_chunk(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1521 if (res < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1522 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1523 if (res == 0) /* EOF */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1524 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1525 if (chunks == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1526 chunks = PyList_New(0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1527 if (chunks == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1528 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1529 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1530 if (PyList_Append(chunks, result) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1531 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1532 Py_DECREF(result);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1533 result = textiowrapper_get_decoded_chars(self, remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1534 if (result == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1535 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1536 remaining -= PyUnicode_GET_SIZE(result);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1537 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1538 if (chunks != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1539 if (result != NULL && PyList_Append(chunks, result) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1540 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1541 Py_CLEAR(result);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1542 result = PyUnicode_Join(_PyIO_empty_str, chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1543 if (result == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1544 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1545 Py_CLEAR(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1546 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1547 return result;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1548 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1549 fail:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1550 Py_XDECREF(result);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1551 Py_XDECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1552 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1553 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1554
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1555
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1556 /* NOTE: `end` must point to the real end of the Py_UNICODE storage,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1557 that is to the NUL character. Otherwise the function will produce
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1558 incorrect results. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1559 static Py_UNICODE *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1560 find_control_char(Py_UNICODE *start, Py_UNICODE *end, Py_UNICODE ch)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1561 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1562 Py_UNICODE *s = start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1563 for (;;) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1564 while (*s > ch)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1565 s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1566 if (*s == ch)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1567 return s;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1568 if (s == end)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1569 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1570 s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1571 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1572 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1573
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1574 Py_ssize_t
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1575 _PyIO_find_line_ending(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1576 int translated, int universal, PyObject *readnl,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1577 Py_UNICODE *start, Py_UNICODE *end, Py_ssize_t *consumed)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1578 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1579 Py_ssize_t len = end - start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1580
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1581 if (translated) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1582 /* Newlines are already translated, only search for \n */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1583 Py_UNICODE *pos = find_control_char(start, end, '\n');
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1584 if (pos != NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1585 return pos - start + 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1586 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1587 *consumed = len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1588 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1589 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1590 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1591 else if (universal) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1592 /* Universal newline search. Find any of \r, \r\n, \n
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1593 * The decoder ensures that \r\n are not split in two pieces
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1594 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1595 Py_UNICODE *s = start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1596 for (;;) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1597 Py_UNICODE ch;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1598 /* Fast path for non-control chars. The loop always ends
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1599 since the Py_UNICODE storage is NUL-terminated. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1600 while (*s > '\r')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1601 s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1602 if (s >= end) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1603 *consumed = len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1604 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1605 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1606 ch = *s++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1607 if (ch == '\n')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1608 return s - start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1609 if (ch == '\r') {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1610 if (*s == '\n')
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1611 return s - start + 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1612 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1613 return s - start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1614 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1615 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1616 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1617 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1618 /* Non-universal mode. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1619 Py_ssize_t readnl_len = PyString_GET_SIZE(readnl);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1620 unsigned char *nl = (unsigned char *) PyString_AS_STRING(readnl);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1621 if (readnl_len == 1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1622 Py_UNICODE *pos = find_control_char(start, end, nl[0]);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1623 if (pos != NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1624 return pos - start + 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1625 *consumed = len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1626 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1627 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1628 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1629 Py_UNICODE *s = start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1630 Py_UNICODE *e = end - readnl_len + 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1631 Py_UNICODE *pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1632 if (e < s)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1633 e = s;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1634 while (s < e) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1635 Py_ssize_t i;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1636 Py_UNICODE *pos = find_control_char(s, end, nl[0]);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1637 if (pos == NULL || pos >= e)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1638 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1639 for (i = 1; i < readnl_len; i++) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1640 if (pos[i] != nl[i])
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1641 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1642 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1643 if (i == readnl_len)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1644 return pos - start + readnl_len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1645 s = pos + 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1646 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1647 pos = find_control_char(e, end, nl[0]);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1648 if (pos == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1649 *consumed = len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1650 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1651 *consumed = pos - start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1652 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1653 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1654 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1655 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1656
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1657 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1658 _textiowrapper_readline(textio *self, Py_ssize_t limit)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1659 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1660 PyObject *line = NULL, *chunks = NULL, *remaining = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1661 Py_ssize_t start, endpos, chunked, offset_to_buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1662 int res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1663
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1664 CHECK_CLOSED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1665
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1666 if (_textiowrapper_writeflush(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1667 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1668
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1669 chunked = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1670
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1671 while (1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1672 Py_UNICODE *ptr;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1673 Py_ssize_t line_len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1674 Py_ssize_t consumed = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1675
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1676 /* First, get some data if necessary */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1677 res = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1678 while (!self->decoded_chars ||
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1679 !PyUnicode_GET_SIZE(self->decoded_chars)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1680 res = textiowrapper_read_chunk(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1681 if (res < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1682 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1683 if (res == 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1684 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1685 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1686 if (res == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1687 /* end of file */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1688 textiowrapper_set_decoded_chars(self, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1689 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1690 start = endpos = offset_to_buffer = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1691 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1692 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1693
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1694 if (remaining == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1695 line = self->decoded_chars;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1696 start = self->decoded_chars_used;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1697 offset_to_buffer = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1698 Py_INCREF(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1699 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1700 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1701 assert(self->decoded_chars_used == 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1702 line = PyUnicode_Concat(remaining, self->decoded_chars);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1703 start = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1704 offset_to_buffer = PyUnicode_GET_SIZE(remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1705 Py_CLEAR(remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1706 if (line == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1707 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1708 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1709
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1710 ptr = PyUnicode_AS_UNICODE(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1711 line_len = PyUnicode_GET_SIZE(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1712
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1713 endpos = _PyIO_find_line_ending(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1714 self->readtranslate, self->readuniversal, self->readnl,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1715 ptr + start, ptr + line_len, &consumed);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1716 if (endpos >= 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1717 endpos += start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1718 if (limit >= 0 && (endpos - start) + chunked >= limit)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1719 endpos = start + limit - chunked;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1720 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1721 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1722
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1723 /* We can put aside up to `endpos` */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1724 endpos = consumed + start;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1725 if (limit >= 0 && (endpos - start) + chunked >= limit) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1726 /* Didn't find line ending, but reached length limit */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1727 endpos = start + limit - chunked;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1728 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1729 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1730
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1731 if (endpos > start) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1732 /* No line ending seen yet - put aside current data */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1733 PyObject *s;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1734 if (chunks == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1735 chunks = PyList_New(0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1736 if (chunks == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1737 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1738 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1739 s = PyUnicode_FromUnicode(ptr + start, endpos - start);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1740 if (s == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1741 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1742 if (PyList_Append(chunks, s) < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1743 Py_DECREF(s);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1744 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1745 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1746 chunked += PyUnicode_GET_SIZE(s);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1747 Py_DECREF(s);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1748 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1749 /* There may be some remaining bytes we'll have to prepend to the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1750 next chunk of data */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1751 if (endpos < line_len) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1752 remaining = PyUnicode_FromUnicode(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1753 ptr + endpos, line_len - endpos);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1754 if (remaining == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1755 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1756 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1757 Py_CLEAR(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1758 /* We have consumed the buffer */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1759 textiowrapper_set_decoded_chars(self, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1760 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1761
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1762 if (line != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1763 /* Our line ends in the current buffer */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1764 self->decoded_chars_used = endpos - offset_to_buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1765 if (start > 0 || endpos < PyUnicode_GET_SIZE(line)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1766 if (start == 0 && Py_REFCNT(line) == 1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1767 if (PyUnicode_Resize(&line, endpos) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1768 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1769 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1770 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1771 PyObject *s = PyUnicode_FromUnicode(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1772 PyUnicode_AS_UNICODE(line) + start, endpos - start);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1773 Py_CLEAR(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1774 if (s == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1775 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1776 line = s;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1777 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1778 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1779 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1780 if (remaining != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1781 if (chunks == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1782 chunks = PyList_New(0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1783 if (chunks == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1784 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1785 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1786 if (PyList_Append(chunks, remaining) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1787 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1788 Py_CLEAR(remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1789 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1790 if (chunks != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1791 if (line != NULL && PyList_Append(chunks, line) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1792 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1793 Py_CLEAR(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1794 line = PyUnicode_Join(_PyIO_empty_str, chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1795 if (line == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1796 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1797 Py_DECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1798 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1799 if (line == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1800 line = PyUnicode_FromStringAndSize(NULL, 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1801
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1802 return line;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1803
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1804 error:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1805 Py_XDECREF(chunks);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1806 Py_XDECREF(remaining);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1807 Py_XDECREF(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1808 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1809 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1810
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1811 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1812 textiowrapper_readline(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1813 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1814 PyObject *limitobj = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1815 Py_ssize_t limit = -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1816
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1817 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1818 if (!PyArg_ParseTuple(args, "|O:readline", &limitobj)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1819 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1820 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1821 if (limitobj) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1822 if (!PyNumber_Check(limitobj)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1823 PyErr_Format(PyExc_TypeError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1824 "integer argument expected, got '%.200s'",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1825 Py_TYPE(limitobj)->tp_name);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1826 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1827 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1828 limit = PyNumber_AsSsize_t(limitobj, PyExc_OverflowError);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1829 if (limit == -1 && PyErr_Occurred())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1830 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1831 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1832 return _textiowrapper_readline(self, limit);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1833 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1834
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1835 /* Seek and Tell */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1836
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1837 typedef struct {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1838 Py_off_t start_pos;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1839 int dec_flags;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1840 int bytes_to_feed;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1841 int chars_to_skip;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1842 char need_eof;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1843 } cookie_type;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1844
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1845 /*
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1846 To speed up cookie packing/unpacking, we store the fields in a temporary
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1847 string and call _PyLong_FromByteArray() or _PyLong_AsByteArray (resp.).
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1848 The following macros define at which offsets in the intermediary byte
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1849 string the various CookieStruct fields will be stored.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1850 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1851
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1852 #define COOKIE_BUF_LEN (sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1853
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1854 #if defined(WORDS_BIGENDIAN)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1855
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1856 # define IS_LITTLE_ENDIAN 0
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1857
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1858 /* We want the least significant byte of start_pos to also be the least
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1859 significant byte of the cookie, which means that in big-endian mode we
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1860 must copy the fields in reverse order. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1861
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1862 # define OFF_START_POS (sizeof(char) + 3 * sizeof(int))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1863 # define OFF_DEC_FLAGS (sizeof(char) + 2 * sizeof(int))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1864 # define OFF_BYTES_TO_FEED (sizeof(char) + sizeof(int))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1865 # define OFF_CHARS_TO_SKIP (sizeof(char))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1866 # define OFF_NEED_EOF 0
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1867
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1868 #else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1869
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1870 # define IS_LITTLE_ENDIAN 1
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1871
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1872 /* Little-endian mode: the least significant byte of start_pos will
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1873 naturally end up the least significant byte of the cookie. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1874
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1875 # define OFF_START_POS 0
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1876 # define OFF_DEC_FLAGS (sizeof(Py_off_t))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1877 # define OFF_BYTES_TO_FEED (sizeof(Py_off_t) + sizeof(int))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1878 # define OFF_CHARS_TO_SKIP (sizeof(Py_off_t) + 2 * sizeof(int))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1879 # define OFF_NEED_EOF (sizeof(Py_off_t) + 3 * sizeof(int))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1880
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1881 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1882
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1883 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1884 textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1885 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1886 unsigned char buffer[COOKIE_BUF_LEN];
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1887 PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1888 if (cookieLong == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1889 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1890
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1891 if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1892 IS_LITTLE_ENDIAN, 0) < 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1893 Py_DECREF(cookieLong);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1894 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1895 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1896 Py_DECREF(cookieLong);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1897
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1898 memcpy(&cookie->start_pos, buffer + OFF_START_POS, sizeof(cookie->start_pos));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1899 memcpy(&cookie->dec_flags, buffer + OFF_DEC_FLAGS, sizeof(cookie->dec_flags));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1900 memcpy(&cookie->bytes_to_feed, buffer + OFF_BYTES_TO_FEED, sizeof(cookie->bytes_to_feed));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1901 memcpy(&cookie->chars_to_skip, buffer + OFF_CHARS_TO_SKIP, sizeof(cookie->chars_to_skip));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1902 memcpy(&cookie->need_eof, buffer + OFF_NEED_EOF, sizeof(cookie->need_eof));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1903
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1904 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1905 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1906
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1907 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1908 textiowrapper_build_cookie(cookie_type *cookie)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1909 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1910 unsigned char buffer[COOKIE_BUF_LEN];
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1911
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1912 memcpy(buffer + OFF_START_POS, &cookie->start_pos, sizeof(cookie->start_pos));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1913 memcpy(buffer + OFF_DEC_FLAGS, &cookie->dec_flags, sizeof(cookie->dec_flags));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1914 memcpy(buffer + OFF_BYTES_TO_FEED, &cookie->bytes_to_feed, sizeof(cookie->bytes_to_feed));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1915 memcpy(buffer + OFF_CHARS_TO_SKIP, &cookie->chars_to_skip, sizeof(cookie->chars_to_skip));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1916 memcpy(buffer + OFF_NEED_EOF, &cookie->need_eof, sizeof(cookie->need_eof));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1917
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1918 return _PyLong_FromByteArray(buffer, sizeof(buffer), IS_LITTLE_ENDIAN, 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1919 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1920 #undef IS_LITTLE_ENDIAN
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1921
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1922 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1923 _textiowrapper_decoder_setstate(textio *self, cookie_type *cookie)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1924 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1925 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1926 /* When seeking to the start of the stream, we call decoder.reset()
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1927 rather than decoder.getstate().
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1928 This is for a few decoders such as utf-16 for which the state value
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1929 at start is not (b"", 0) but e.g. (b"", 2) (meaning, in the case of
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1930 utf-16, that we are expecting a BOM).
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1931 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1932 if (cookie->start_pos == 0 && cookie->dec_flags == 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1933 res = PyObject_CallMethodObjArgs(self->decoder, _PyIO_str_reset, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1934 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1935 res = PyObject_CallMethod(self->decoder, "setstate",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1936 "((si))", "", cookie->dec_flags);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1937 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1938 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1939 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1940 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1941 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1942
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1943 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1944 _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1945 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1946 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1947 /* Same as _textiowrapper_decoder_setstate() above. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1948 if (cookie->start_pos == 0 && cookie->dec_flags == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1949 res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_reset, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1950 self->encoding_start_of_stream = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1951 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1952 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1953 res = PyObject_CallMethodObjArgs(self->encoder, _PyIO_str_setstate,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1954 _PyIO_zero, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1955 self->encoding_start_of_stream = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1956 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1957 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1958 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1959 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1960 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1961 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1962
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1963 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1964 textiowrapper_seek(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1965 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1966 PyObject *cookieObj, *posobj;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1967 cookie_type cookie;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1968 int whence = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1969 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1970 int cmp;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1971
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1972 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1973
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1974 if (!PyArg_ParseTuple(args, "O|i:seek", &cookieObj, &whence))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1975 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1976 CHECK_CLOSED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1977
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1978 Py_INCREF(cookieObj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1979
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1980 if (!self->seekable) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1981 PyErr_SetString(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1982 "underlying stream is not seekable");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1983 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1984 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1985
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1986 if (whence == 1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1987 /* seek relative to current position */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1988 cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1989 if (cmp < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1990 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1991
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1992 if (cmp == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1993 PyErr_SetString(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1994 "can't do nonzero cur-relative seeks");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1995 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1996 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1997
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1998 /* Seeking to the current position should attempt to
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
1999 * sync the underlying buffer with the current position.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2000 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2001 Py_DECREF(cookieObj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2002 cookieObj = PyObject_CallMethod((PyObject *)self, "tell", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2003 if (cookieObj == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2004 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2005 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2006 else if (whence == 2) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2007 /* seek relative to end of file */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2008
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2009 cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_EQ);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2010 if (cmp < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2011 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2012
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2013 if (cmp == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2014 PyErr_SetString(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2015 "can't do nonzero end-relative seeks");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2016 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2017 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2018
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2019 res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2020 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2021 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2022 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2023
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2024 textiowrapper_set_decoded_chars(self, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2025 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2026 if (self->decoder) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2027 res = PyObject_CallMethod(self->decoder, "reset", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2028 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2029 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2030 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2031 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2032
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2033 res = PyObject_CallMethod(self->buffer, "seek", "ii", 0, 2);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2034 Py_XDECREF(cookieObj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2035 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2036 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2037 else if (whence != 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2038 PyErr_Format(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2039 "invalid whence (%d, should be 0, 1 or 2)", whence);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2040 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2041 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2042
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2043 cmp = PyObject_RichCompareBool(cookieObj, _PyIO_zero, Py_LT);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2044 if (cmp < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2045 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2046
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2047 if (cmp == 1) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2048 PyObject *repr = PyObject_Repr(cookieObj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2049 if (repr != NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2050 PyErr_Format(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2051 "negative seek position %s",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2052 PyString_AS_STRING(repr));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2053 Py_DECREF(repr);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2054 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2055 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2056 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2057
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2058 res = PyObject_CallMethodObjArgs((PyObject *)self, _PyIO_str_flush, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2059 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2060 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2061 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2062
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2063 /* The strategy of seek() is to go back to the safe start point
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2064 * and replay the effect of read(chars_to_skip) from there.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2065 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2066 if (textiowrapper_parse_cookie(&cookie, cookieObj) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2067 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2068
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2069 /* Seek back to the safe start point. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2070 posobj = PyLong_FromOff_t(cookie.start_pos);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2071 if (posobj == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2072 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2073 res = PyObject_CallMethodObjArgs(self->buffer,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2074 _PyIO_str_seek, posobj, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2075 Py_DECREF(posobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2076 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2077 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2078 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2079
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2080 textiowrapper_set_decoded_chars(self, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2081 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2082
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2083 /* Restore the decoder to its state from the safe start point. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2084 if (self->decoder) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2085 if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2086 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2087 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2088
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2089 if (cookie.chars_to_skip) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2090 /* Just like _read_chunk, feed the decoder and save a snapshot. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2091 PyObject *input_chunk = PyObject_CallMethod(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2092 self->buffer, "read", "i", cookie.bytes_to_feed);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2093 PyObject *decoded;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2094
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2095 if (input_chunk == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2096 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2097
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2098 assert (PyBytes_Check(input_chunk));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2099
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2100 self->snapshot = Py_BuildValue("iN", cookie.dec_flags, input_chunk);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2101 if (self->snapshot == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2102 Py_DECREF(input_chunk);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2103 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2104 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2105
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2106 decoded = PyObject_CallMethod(self->decoder, "decode",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2107 "Oi", input_chunk, (int)cookie.need_eof);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2108
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2109 if (decoded == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2110 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2111
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2112 textiowrapper_set_decoded_chars(self, decoded);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2113
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2114 /* Skip chars_to_skip of the decoded characters. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2115 if (PyUnicode_GetSize(self->decoded_chars) < cookie.chars_to_skip) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2116 PyErr_SetString(PyExc_IOError, "can't restore logical file position");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2117 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2118 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2119 self->decoded_chars_used = cookie.chars_to_skip;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2120 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2121 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2122 self->snapshot = Py_BuildValue("is", cookie.dec_flags, "");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2123 if (self->snapshot == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2124 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2125 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2126
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2127 /* Finally, reset the encoder (merely useful for proper BOM handling) */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2128 if (self->encoder) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2129 if (_textiowrapper_encoder_setstate(self, &cookie) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2130 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2131 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2132 return cookieObj;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2133 fail:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2134 Py_XDECREF(cookieObj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2135 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2136
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2137 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2138
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2139 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2140 textiowrapper_tell(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2141 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2142 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2143 PyObject *posobj = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2144 cookie_type cookie = {0,0,0,0,0};
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2145 PyObject *next_input;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2146 Py_ssize_t chars_to_skip, chars_decoded;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2147 PyObject *saved_state = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2148 char *input, *input_end;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2149
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2150 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2151 CHECK_CLOSED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2152
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2153 if (!self->seekable) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2154 PyErr_SetString(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2155 "underlying stream is not seekable");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2156 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2157 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2158 if (!self->telling) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2159 PyErr_SetString(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2160 "telling position disabled by next() call");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2161 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2162 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2163
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2164 if (_textiowrapper_writeflush(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2165 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2166 res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2167 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2168 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2169 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2170
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2171 posobj = PyObject_CallMethod(self->buffer, "tell", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2172 if (posobj == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2173 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2174
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2175 if (self->decoder == NULL || self->snapshot == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2176 assert (self->decoded_chars == NULL || PyUnicode_GetSize(self->decoded_chars) == 0);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2177 return posobj;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2178 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2179
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2180 #if defined(HAVE_LARGEFILE_SUPPORT)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2181 cookie.start_pos = PyLong_AsLongLong(posobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2182 #else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2183 cookie.start_pos = PyLong_AsLong(posobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2184 #endif
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2185 if (PyErr_Occurred())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2186 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2187
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2188 /* Skip backward to the snapshot point (see _read_chunk). */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2189 if (!PyArg_Parse(self->snapshot, "(iO)", &cookie.dec_flags, &next_input))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2190 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2191
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2192 assert (PyBytes_Check(next_input));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2193
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2194 cookie.start_pos -= PyBytes_GET_SIZE(next_input);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2195
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2196 /* How many decoded characters have been used up since the snapshot? */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2197 if (self->decoded_chars_used == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2198 /* We haven't moved from the snapshot point. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2199 Py_DECREF(posobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2200 return textiowrapper_build_cookie(&cookie);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2201 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2202
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2203 chars_to_skip = self->decoded_chars_used;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2204
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2205 /* Starting from the snapshot position, we will walk the decoder
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2206 * forward until it gives us enough decoded characters.
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2207 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2208 saved_state = PyObject_CallMethodObjArgs(self->decoder,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2209 _PyIO_str_getstate, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2210 if (saved_state == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2211 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2212
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2213 /* Note our initial start point. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2214 if (_textiowrapper_decoder_setstate(self, &cookie) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2215 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2216
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2217 /* Feed the decoder one byte at a time. As we go, note the
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2218 * nearest "safe start point" before the current location
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2219 * (a point where the decoder has nothing buffered, so seek()
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2220 * can safely start from there and advance to this location).
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2221 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2222 chars_decoded = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2223 input = PyBytes_AS_STRING(next_input);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2224 input_end = input + PyBytes_GET_SIZE(next_input);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2225 while (input < input_end) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2226 PyObject *state;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2227 char *dec_buffer;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2228 Py_ssize_t dec_buffer_len;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2229 int dec_flags;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2230
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2231 PyObject *decoded = PyObject_CallMethod(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2232 self->decoder, "decode", "s#", input, 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2233 if (decoded == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2234 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2235 assert (PyUnicode_Check(decoded));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2236 chars_decoded += PyUnicode_GET_SIZE(decoded);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2237 Py_DECREF(decoded);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2238
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2239 cookie.bytes_to_feed += 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2240
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2241 state = PyObject_CallMethodObjArgs(self->decoder,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2242 _PyIO_str_getstate, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2243 if (state == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2244 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2245 if (!PyArg_Parse(state, "(s#i)", &dec_buffer, &dec_buffer_len, &dec_flags)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2246 Py_DECREF(state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2247 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2248 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2249 Py_DECREF(state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2250
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2251 if (dec_buffer_len == 0 && chars_decoded <= chars_to_skip) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2252 /* Decoder buffer is empty, so this is a safe start point. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2253 cookie.start_pos += cookie.bytes_to_feed;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2254 chars_to_skip -= chars_decoded;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2255 cookie.dec_flags = dec_flags;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2256 cookie.bytes_to_feed = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2257 chars_decoded = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2258 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2259 if (chars_decoded >= chars_to_skip)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2260 break;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2261 input++;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2262 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2263 if (input == input_end) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2264 /* We didn't get enough decoded data; signal EOF to get more. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2265 PyObject *decoded = PyObject_CallMethod(
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2266 self->decoder, "decode", "si", "", /* final = */ 1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2267 if (decoded == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2268 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2269 assert (PyUnicode_Check(decoded));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2270 chars_decoded += PyUnicode_GET_SIZE(decoded);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2271 Py_DECREF(decoded);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2272 cookie.need_eof = 1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2273
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2274 if (chars_decoded < chars_to_skip) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2275 PyErr_SetString(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2276 "can't reconstruct logical file position");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2277 goto fail;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2278 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2279 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2280
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2281 /* finally */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2282 Py_XDECREF(posobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2283 res = PyObject_CallMethod(self->decoder, "setstate", "(O)", saved_state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2284 Py_DECREF(saved_state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2285 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2286 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2287 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2288
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2289 /* The returned cookie corresponds to the last safe start point. */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2290 cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2291 return textiowrapper_build_cookie(&cookie);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2292
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2293 fail:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2294 Py_XDECREF(posobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2295 if (saved_state) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2296 PyObject *type, *value, *traceback;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2297 PyErr_Fetch(&type, &value, &traceback);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2298
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2299 res = PyObject_CallMethod(self->decoder, "setstate", "(O)", saved_state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2300 Py_DECREF(saved_state);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2301 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2302 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2303 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2304
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2305 PyErr_Restore(type, value, traceback);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2306 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2307 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2308 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2309
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2310 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2311 textiowrapper_truncate(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2312 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2313 PyObject *pos = Py_None;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2314 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2315
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2316 CHECK_INITIALIZED(self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2317 if (!PyArg_ParseTuple(args, "|O:truncate", &pos)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2318 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2319 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2320
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2321 res = PyObject_CallMethodObjArgs((PyObject *) self, _PyIO_str_flush, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2322 if (res == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2323 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2324 Py_DECREF(res);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2325
58624
77130f2fe944 - Issue #6939: Fix file I/O objects in the `io` module to keep the original
Antoine Pitrou <solipsis@pitrou.net>
parents: 57703
diff changeset
2326 return PyObject_CallMethodObjArgs(self->buffer, _PyIO_str_truncate, pos, NULL);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2327 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2328
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2329 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2330 textiowrapper_repr(textio *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2331 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2332 PyObject *nameobj, *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2333 PyObject *namerepr = NULL, *encrepr = NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2334
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2335 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2336
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2337 nameobj = PyObject_GetAttrString((PyObject *) self, "name");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2338 if (nameobj == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2339 if (PyErr_ExceptionMatches(PyExc_AttributeError))
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2340 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2341 else
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2342 goto error;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2343 encrepr = PyObject_Repr(self->encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2344 res = PyString_FromFormat("<_io.TextIOWrapper encoding=%s>",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2345 PyString_AS_STRING(encrepr));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2346 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2347 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2348 encrepr = PyObject_Repr(self->encoding);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2349 namerepr = PyObject_Repr(nameobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2350 res = PyString_FromFormat("<_io.TextIOWrapper name=%s encoding=%s>",
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2351 PyString_AS_STRING(namerepr),
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2352 PyString_AS_STRING(encrepr));
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2353 Py_DECREF(nameobj);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2354 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2355 Py_XDECREF(namerepr);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2356 Py_XDECREF(encrepr);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2357 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2358
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2359 error:
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2360 Py_XDECREF(namerepr);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2361 Py_XDECREF(encrepr);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2362 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2363 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2364
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2365
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2366 /* Inquiries */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2367
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2368 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2369 textiowrapper_fileno(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2370 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2371 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2372 return PyObject_CallMethod(self->buffer, "fileno", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2373 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2374
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2375 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2376 textiowrapper_seekable(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2377 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2378 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2379 return PyObject_CallMethod(self->buffer, "seekable", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2380 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2381
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2382 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2383 textiowrapper_readable(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2384 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2385 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2386 return PyObject_CallMethod(self->buffer, "readable", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2387 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2388
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2389 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2390 textiowrapper_writable(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2391 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2392 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2393 return PyObject_CallMethod(self->buffer, "writable", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2394 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2395
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2396 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2397 textiowrapper_isatty(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2398 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2399 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2400 return PyObject_CallMethod(self->buffer, "isatty", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2401 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2402
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2403 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2404 textiowrapper_flush(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2405 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2406 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2407 CHECK_CLOSED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2408 self->telling = self->seekable;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2409 if (_textiowrapper_writeflush(self) < 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2410 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2411 return PyObject_CallMethod(self->buffer, "flush", NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2412 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2413
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2414 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2415 textiowrapper_close(textio *self, PyObject *args)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2416 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2417 PyObject *res;
61180
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2418 int r;
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2419 CHECK_INITIALIZED(self);
61180
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2420
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2421 res = textiowrapper_closed_get(self, NULL);
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2422 if (res == NULL)
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2423 return NULL;
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2424 r = PyObject_IsTrue(res);
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2425 Py_DECREF(res);
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2426 if (r < 0)
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2427 return NULL;
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2428
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2429 if (r > 0) {
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2430 Py_RETURN_NONE; /* stream already closed */
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2431 }
61180
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2432 else {
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2433 res = PyObject_CallMethod((PyObject *)self, "flush", NULL);
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2434 if (res == NULL) {
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2435 return NULL;
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2436 }
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2437 else
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2438 Py_DECREF(res);
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2439
61180
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2440 return PyObject_CallMethod(self->buffer, "close", NULL);
9b438bb06d4d Issue #7865: The close() method of :mod:`io` objects should not swallow
Antoine Pitrou <solipsis@pitrou.net>
parents: 58624
diff changeset
2441 }
54717
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2442 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2443
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2444 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2445 textiowrapper_iternext(textio *self)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2446 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2447 PyObject *line;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2448
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2449 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2450
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2451 self->telling = 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2452 if (Py_TYPE(self) == &PyTextIOWrapper_Type) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2453 /* Skip method call overhead for speed */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2454 line = _textiowrapper_readline(self, -1);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2455 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2456 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2457 line = PyObject_CallMethodObjArgs((PyObject *)self,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2458 _PyIO_str_readline, NULL);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2459 if (line && !PyUnicode_Check(line)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2460 PyErr_Format(PyExc_IOError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2461 "readline() should have returned an str object, "
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2462 "not '%.200s'", Py_TYPE(line)->tp_name);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2463 Py_DECREF(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2464 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2465 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2466 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2467
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2468 if (line == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2469 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2470
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2471 if (PyUnicode_GET_SIZE(line) == 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2472 /* Reached EOF or would have blocked */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2473 Py_DECREF(line);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2474 Py_CLEAR(self->snapshot);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2475 self->telling = self->seekable;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2476 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2477 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2478
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2479 return line;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2480 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2481
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2482 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2483 textiowrapper_name_get(textio *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2484 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2485 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2486 return PyObject_GetAttrString(self->buffer, "name");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2487 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2488
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2489 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2490 textiowrapper_closed_get(textio *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2491 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2492 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2493 return PyObject_GetAttr(self->buffer, _PyIO_str_closed);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2494 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2495
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2496 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2497 textiowrapper_newlines_get(textio *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2498 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2499 PyObject *res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2500 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2501 if (self->decoder == NULL)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2502 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2503 res = PyObject_GetAttr(self->decoder, _PyIO_str_newlines);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2504 if (res == NULL) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2505 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2506 PyErr_Clear();
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2507 Py_RETURN_NONE;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2508 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2509 else {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2510 return NULL;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2511 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2512 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2513 return res;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2514 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2515
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2516 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2517 textiowrapper_errors_get(textio *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2518 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2519 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2520 Py_INCREF(self->errors);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2521 return self->errors;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2522 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2523
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2524 static PyObject *
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2525 textiowrapper_chunk_size_get(textio *self, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2526 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2527 CHECK_INITIALIZED(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2528 return PyLong_FromSsize_t(self->chunk_size);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2529 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2530
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2531 static int
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2532 textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2533 {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2534 Py_ssize_t n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2535 CHECK_INITIALIZED_INT(self);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2536 n = PyNumber_AsSsize_t(arg, PyExc_TypeError);
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2537 if (n == -1 && PyErr_Occurred())
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2538 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2539 if (n <= 0) {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2540 PyErr_SetString(PyExc_ValueError,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2541 "a strictly positive integer is required");
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2542 return -1;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2543 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2544 self->chunk_size = n;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2545 return 0;
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2546 }
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2547
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2548 static PyMethodDef textiowrapper_methods[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2549 {"detach", (PyCFunction)textiowrapper_detach, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2550 {"write", (PyCFunction)textiowrapper_write, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2551 {"read", (PyCFunction)textiowrapper_read, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2552 {"readline", (PyCFunction)textiowrapper_readline, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2553 {"flush", (PyCFunction)textiowrapper_flush, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2554 {"close", (PyCFunction)textiowrapper_close, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2555
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2556 {"fileno", (PyCFunction)textiowrapper_fileno, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2557 {"seekable", (PyCFunction)textiowrapper_seekable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2558 {"readable", (PyCFunction)textiowrapper_readable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2559 {"writable", (PyCFunction)textiowrapper_writable, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2560 {"isatty", (PyCFunction)textiowrapper_isatty, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2561
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2562 {"seek", (PyCFunction)textiowrapper_seek, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2563 {"tell", (PyCFunction)textiowrapper_tell, METH_NOARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2564 {"truncate", (PyCFunction)textiowrapper_truncate, METH_VARARGS},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2565 {NULL, NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2566 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2567
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2568 static PyMemberDef textiowrapper_members[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2569 {"encoding", T_OBJECT, offsetof(textio, encoding), READONLY},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2570 {"buffer", T_OBJECT, offsetof(textio, buffer), READONLY},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2571 {"line_buffering", T_BOOL, offsetof(textio, line_buffering), READONLY},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2572 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2573 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2574
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2575 static PyGetSetDef textiowrapper_getset[] = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2576 {"name", (getter)textiowrapper_name_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2577 {"closed", (getter)textiowrapper_closed_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2578 /* {"mode", (getter)TextIOWrapper_mode_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2579 */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2580 {"newlines", (getter)textiowrapper_newlines_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2581 {"errors", (getter)textiowrapper_errors_get, NULL, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2582 {"_CHUNK_SIZE", (getter)textiowrapper_chunk_size_get,
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2583 (setter)textiowrapper_chunk_size_set, NULL},
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2584 {NULL}
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2585 };
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2586
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2587 PyTypeObject PyTextIOWrapper_Type = {
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2588 PyVarObject_HEAD_INIT(NULL, 0)
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2589 "_io.TextIOWrapper", /*tp_name*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2590 sizeof(textio), /*tp_basicsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2591 0, /*tp_itemsize*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2592 (destructor)textiowrapper_dealloc, /*tp_dealloc*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2593 0, /*tp_print*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2594 0, /*tp_getattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2595 0, /*tps_etattr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2596 0, /*tp_compare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2597 (reprfunc)textiowrapper_repr,/*tp_repr*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2598 0, /*tp_as_number*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2599 0, /*tp_as_sequence*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2600 0, /*tp_as_mapping*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2601 0, /*tp_hash */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2602 0, /*tp_call*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2603 0, /*tp_str*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2604 0, /*tp_getattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2605 0, /*tp_setattro*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2606 0, /*tp_as_buffer*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2607 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2608 | Py_TPFLAGS_HAVE_GC, /*tp_flags*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2609 textiowrapper_doc, /* tp_doc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2610 (traverseproc)textiowrapper_traverse, /* tp_traverse */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2611 (inquiry)textiowrapper_clear, /* tp_clear */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2612 0, /* tp_richcompare */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2613 offsetof(textio, weakreflist), /*tp_weaklistoffset*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2614 0, /* tp_iter */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2615 (iternextfunc)textiowrapper_iternext, /* tp_iternext */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2616 textiowrapper_methods, /* tp_methods */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2617 textiowrapper_members, /* tp_members */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2618 textiowrapper_getset, /* tp_getset */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2619 0, /* tp_base */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2620 0, /* tp_dict */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2621 0, /* tp_descr_get */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2622 0, /* tp_descr_set */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2623 offsetof(textio, dict), /*tp_dictoffset*/
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2624 (initproc)textiowrapper_init, /* tp_init */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2625 0, /* tp_alloc */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2626 PyType_GenericNew, /* tp_new */
760a710eb6c1 Issue #6215: backport the 3.1 io lib
Antoine Pitrou <solipsis@pitrou.net>
parents:
diff changeset
2627 };