| 1 |
/* |
| 2 |
An implementation of Text I/O as defined by PEP 3116 - "New I/O" |
| 3 |
|
| 4 |
Classes defined here: TextIOBase, IncrementalNewlineDecoder, TextIOWrapper. |
| 5 |
|
| 6 |
Written by Amaury Forgeot d'Arc and Antoine Pitrou |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "Python.h" |
| 10 |
#include "pycore_call.h" // _PyObject_CallMethod() |
| 11 |
#include "pycore_codecs.h" // _PyCodecInfo_GetIncrementalDecoder() |
| 12 |
#include "pycore_fileutils.h" // _Py_GetLocaleEncoding() |
| 13 |
#include "pycore_interp.h" // PyInterpreterState.fs_codec |
| 14 |
#include "pycore_long.h" // _PyLong_GetZero() |
| 15 |
#include "pycore_object.h" // _PyObject_GC_UNTRACK() |
| 16 |
#include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() |
| 17 |
#include "pycore_pystate.h" // _PyInterpreterState_GET() |
| 18 |
#include "pycore_unicodeobject.h" // _PyUnicode_AsASCIIString() |
| 19 |
#include "pycore_weakref.h" // FT_CLEAR_WEAKREFS() |
| 20 |
|
| 21 |
#include "_iomodule.h" |
| 22 |
|
| 23 |
/*[clinic input] |
| 24 |
module _io |
| 25 |
class _io.IncrementalNewlineDecoder "nldecoder_object *" "clinic_state()->PyIncrementalNewlineDecoder_Type" |
| 26 |
class _io.TextIOWrapper "textio *" "clinic_state()->TextIOWrapper_Type" |
| 27 |
class _io._TextIOBase "PyObject *" "&PyTextIOBase_Type" |
| 28 |
[clinic start generated code]*/ |
| 29 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8b7f24fa13bfdd7f]*/ |
| 30 |
|
| 31 |
typedef struct nldecoder_object nldecoder_object; |
| 32 |
typedef struct textio textio; |
| 33 |
|
| 34 |
#define clinic_state() (find_io_state_by_def(Py_TYPE(self))) |
| 35 |
#include "clinic/textio.c.h" |
| 36 |
#undef clinic_state |
| 37 |
|
| 38 |
/* TextIOBase */ |
| 39 |
|
| 40 |
PyDoc_STRVAR(textiobase_doc, |
| 41 |
"Base class for text I/O.\n" |
| 42 |
"\n" |
| 43 |
"This class provides a character and line based interface to stream\n" |
| 44 |
"I/O. There is no readinto method because Python's character strings\n" |
| 45 |
"are immutable.\n" |
| 46 |
); |
| 47 |
|
| 48 |
static PyObject * |
| 49 |
_unsupported(_PyIO_State *state, const char *message) |
| 50 |
{ |
| 51 |
PyErr_SetString(state->unsupported_operation, message); |
| 52 |
return NULL; |
| 53 |
} |
| 54 |
|
| 55 |
/*[clinic input] |
| 56 |
_io._TextIOBase.detach |
| 57 |
cls: defining_class |
| 58 |
/ |
| 59 |
|
| 60 |
Separate the underlying buffer from the TextIOBase and return it. |
| 61 |
|
| 62 |
After the underlying buffer has been detached, the TextIO is in an unusable state. |
| 63 |
[clinic start generated code]*/ |
| 64 |
|
| 65 |
static PyObject * |
| 66 |
_io__TextIOBase_detach_impl(PyObject *self, PyTypeObject *cls) |
| 67 |
/*[clinic end generated code: output=50915f40c609eaa4 input=987ca3640d0a3776]*/ |
| 68 |
{ |
| 69 |
_PyIO_State *state = get_io_state_by_cls(cls); |
| 70 |
return _unsupported(state, "detach"); |
| 71 |
} |
| 72 |
|
| 73 |
/*[clinic input] |
| 74 |
_io._TextIOBase.read |
| 75 |
cls: defining_class |
| 76 |
size: int(unused=True) = -1 |
| 77 |
/ |
| 78 |
|
| 79 |
Read at most size characters from stream. |
| 80 |
|
| 81 |
Read from underlying buffer until we have size characters or we hit EOF. |
| 82 |
If size is negative or omitted, read until EOF. |
| 83 |
[clinic start generated code]*/ |
| 84 |
|
| 85 |
static PyObject * |
| 86 |
_io__TextIOBase_read_impl(PyObject *self, PyTypeObject *cls, |
| 87 |
int Py_UNUSED(size)) |
| 88 |
/*[clinic end generated code: output=51a5178a309ce647 input=f5e37720f9fc563f]*/ |
| 89 |
{ |
| 90 |
_PyIO_State *state = get_io_state_by_cls(cls); |
| 91 |
return _unsupported(state, "read"); |
| 92 |
} |
| 93 |
|
| 94 |
/*[clinic input] |
| 95 |
_io._TextIOBase.readline |
| 96 |
cls: defining_class |
| 97 |
size: int(unused=True) = -1 |
| 98 |
/ |
| 99 |
|
| 100 |
Read until newline or EOF. |
| 101 |
|
| 102 |
Return an empty string if EOF is hit immediately. |
| 103 |
If size is specified, at most size characters will be read. |
| 104 |
[clinic start generated code]*/ |
| 105 |
|
| 106 |
static PyObject * |
| 107 |
_io__TextIOBase_readline_impl(PyObject *self, PyTypeObject *cls, |
| 108 |
int Py_UNUSED(size)) |
| 109 |
/*[clinic end generated code: output=3f47d7966d6d074e input=42eafec94107fa27]*/ |
| 110 |
{ |
| 111 |
_PyIO_State *state = get_io_state_by_cls(cls); |
| 112 |
return _unsupported(state, "readline"); |
| 113 |
} |
| 114 |
|
| 115 |
/*[clinic input] |
| 116 |
_io._TextIOBase.write |
| 117 |
cls: defining_class |
| 118 |
s: str(unused=True) |
| 119 |
/ |
| 120 |
|
| 121 |
Write string s to stream. |
| 122 |
|
| 123 |
Return the number of characters written |
| 124 |
(which is always equal to the length of the string). |
| 125 |
[clinic start generated code]*/ |
| 126 |
|
| 127 |
static PyObject * |
| 128 |
_io__TextIOBase_write_impl(PyObject *self, PyTypeObject *cls, |
| 129 |
const char *Py_UNUSED(s)) |
| 130 |
/*[clinic end generated code: output=18b28231460275de input=e9cabaa5f6732b07]*/ |
| 131 |
{ |
| 132 |
_PyIO_State *state = get_io_state_by_cls(cls); |
| 133 |
return _unsupported(state, "write"); |
| 134 |
} |
| 135 |
|
| 136 |
/*[clinic input] |
| 137 |
@getter |
| 138 |
_io._TextIOBase.encoding |
| 139 |
|
| 140 |
Encoding of the text stream. |
| 141 |
|
| 142 |
Subclasses should override. |
| 143 |
[clinic start generated code]*/ |
| 144 |
|
| 145 |
static PyObject * |
| 146 |
_io__TextIOBase_encoding_get_impl(PyObject *self) |
| 147 |
/*[clinic end generated code: output=e0f5d8f548b92432 input=4736d7621dd38f43]*/ |
| 148 |
{ |
| 149 |
Py_RETURN_NONE; |
| 150 |
} |
| 151 |
|
| 152 |
/*[clinic input] |
| 153 |
@getter |
| 154 |
_io._TextIOBase.newlines |
| 155 |
|
| 156 |
Line endings translated so far. |
| 157 |
|
| 158 |
Only line endings translated during reading are considered. |
| 159 |
|
| 160 |
Subclasses should override. |
| 161 |
[clinic start generated code]*/ |
| 162 |
|
| 163 |
static PyObject * |
| 164 |
_io__TextIOBase_newlines_get_impl(PyObject *self) |
| 165 |
/*[clinic end generated code: output=46ec147fb9f00c2a input=a5b196d076af1164]*/ |
| 166 |
{ |
| 167 |
Py_RETURN_NONE; |
| 168 |
} |
| 169 |
|
| 170 |
/*[clinic input] |
| 171 |
@getter |
| 172 |
_io._TextIOBase.errors |
| 173 |
|
| 174 |
The error setting of the decoder or encoder. |
| 175 |
|
| 176 |
Subclasses should override. |
| 177 |
[clinic start generated code]*/ |
| 178 |
|
| 179 |
static PyObject * |
| 180 |
_io__TextIOBase_errors_get_impl(PyObject *self) |
| 181 |
/*[clinic end generated code: output=c6623d6addcd087d input=974aa52d1db93a82]*/ |
| 182 |
{ |
| 183 |
Py_RETURN_NONE; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
static PyMethodDef textiobase_methods[] = { |
| 188 |
_IO__TEXTIOBASE_DETACH_METHODDEF |
| 189 |
_IO__TEXTIOBASE_READ_METHODDEF |
| 190 |
_IO__TEXTIOBASE_READLINE_METHODDEF |
| 191 |
_IO__TEXTIOBASE_WRITE_METHODDEF |
| 192 |
{NULL, NULL} |
| 193 |
}; |
| 194 |
|
| 195 |
static PyGetSetDef textiobase_getset[] = { |
| 196 |
_IO__TEXTIOBASE_ENCODING_GETSETDEF |
| 197 |
_IO__TEXTIOBASE_NEWLINES_GETSETDEF |
| 198 |
_IO__TEXTIOBASE_ERRORS_GETSETDEF |
| 199 |
{NULL} |
| 200 |
}; |
| 201 |
|
| 202 |
static PyType_Slot textiobase_slots[] = { |
| 203 |
{Py_tp_doc, (void *)textiobase_doc}, |
| 204 |
{Py_tp_methods, textiobase_methods}, |
| 205 |
{Py_tp_getset, textiobase_getset}, |
| 206 |
{0, NULL}, |
| 207 |
}; |
| 208 |
|
| 209 |
/* Do not set Py_TPFLAGS_HAVE_GC so that tp_traverse and tp_clear are inherited */ |
| 210 |
PyType_Spec textiobase_spec = { |
| 211 |
.name = "_io._TextIOBase", |
| 212 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 213 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 214 |
.slots = textiobase_slots, |
| 215 |
}; |
| 216 |
|
| 217 |
/* IncrementalNewlineDecoder */ |
| 218 |
|
| 219 |
struct nldecoder_object { |
| 220 |
PyObject_HEAD |
| 221 |
PyObject *decoder; |
| 222 |
PyObject *errors; |
| 223 |
unsigned int pendingcr: 1; |
| 224 |
unsigned int translate: 1; |
| 225 |
unsigned int seennl: 3; |
| 226 |
}; |
| 227 |
|
| 228 |
#define nldecoder_object_CAST(op) ((nldecoder_object *)(op)) |
| 229 |
|
| 230 |
/*[clinic input] |
| 231 |
_io.IncrementalNewlineDecoder.__init__ |
| 232 |
decoder: object |
| 233 |
translate: bool |
| 234 |
errors: object(c_default="NULL") = "strict" |
| 235 |
|
| 236 |
Codec used when reading a file in universal newlines mode. |
| 237 |
|
| 238 |
It wraps another incremental decoder, translating \r\n and \r into \n. |
| 239 |
It also records the types of newlines encountered. When used with |
| 240 |
translate=False, it ensures that the newline sequence is returned in |
| 241 |
one piece. When used with decoder=None, it expects unicode strings as |
| 242 |
decode input and translates newlines without first invoking an external |
| 243 |
decoder. |
| 244 |
[clinic start generated code]*/ |
| 245 |
|
| 246 |
static int |
| 247 |
_io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self, |
| 248 |
PyObject *decoder, int translate, |
| 249 |
PyObject *errors) |
| 250 |
/*[clinic end generated code: output=fbd04d443e764ec2 input=ed547aa257616b0e]*/ |
| 251 |
{ |
| 252 |
|
| 253 |
if (errors == NULL) { |
| 254 |
errors = &_Py_ID(strict); |
| 255 |
} |
| 256 |
else { |
| 257 |
errors = Py_NewRef(errors); |
| 258 |
} |
| 259 |
|
| 260 |
Py_XSETREF(self->errors, errors); |
| 261 |
Py_XSETREF(self->decoder, Py_NewRef(decoder)); |
| 262 |
self->translate = translate ? 1 : 0; |
| 263 |
self->seennl = 0; |
| 264 |
self->pendingcr = 0; |
| 265 |
|
| 266 |
return 0; |
| 267 |
} |
| 268 |
|
| 269 |
static int |
| 270 |
incrementalnewlinedecoder_traverse(PyObject *op, visitproc visit, void *arg) |
| 271 |
{ |
| 272 |
nldecoder_object *self = nldecoder_object_CAST(op); |
| 273 |
Py_VISIT(Py_TYPE(self)); |
| 274 |
Py_VISIT(self->decoder); |
| 275 |
Py_VISIT(self->errors); |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
static int |
| 280 |
incrementalnewlinedecoder_clear(PyObject *op) |
| 281 |
{ |
| 282 |
nldecoder_object *self = nldecoder_object_CAST(op); |
| 283 |
Py_CLEAR(self->decoder); |
| 284 |
Py_CLEAR(self->errors); |
| 285 |
return 0; |
| 286 |
} |
| 287 |
|
| 288 |
static void |
| 289 |
incrementalnewlinedecoder_dealloc(PyObject *op) |
| 290 |
{ |
| 291 |
nldecoder_object *self = nldecoder_object_CAST(op); |
| 292 |
PyTypeObject *tp = Py_TYPE(self); |
| 293 |
_PyObject_GC_UNTRACK(self); |
| 294 |
(void)incrementalnewlinedecoder_clear(op); |
| 295 |
tp->tp_free(self); |
| 296 |
Py_DECREF(tp); |
| 297 |
} |
| 298 |
|
| 299 |
static int |
| 300 |
check_decoded(PyObject *decoded) |
| 301 |
{ |
| 302 |
if (decoded == NULL) |
| 303 |
return -1; |
| 304 |
if (!PyUnicode_Check(decoded)) { |
| 305 |
PyErr_Format(PyExc_TypeError, |
| 306 |
"decoder should return a string result, not '%.200s'", |
| 307 |
Py_TYPE(decoded)->tp_name); |
| 308 |
Py_DECREF(decoded); |
| 309 |
return -1; |
| 310 |
} |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
#define CHECK_INITIALIZED_DECODER(self) \ |
| 315 |
if (self->errors == NULL) { \ |
| 316 |
PyErr_SetString(PyExc_ValueError, \ |
| 317 |
"IncrementalNewlineDecoder.__init__() not called"); \ |
| 318 |
return NULL; \ |
| 319 |
} |
| 320 |
|
| 321 |
#define SEEN_CR 1 |
| 322 |
#define SEEN_LF 2 |
| 323 |
#define SEEN_CRLF 4 |
| 324 |
#define SEEN_ALL (SEEN_CR | SEEN_LF | SEEN_CRLF) |
| 325 |
|
| 326 |
PyObject * |
| 327 |
_PyIncrementalNewlineDecoder_decode(PyObject *myself, |
| 328 |
PyObject *input, int final) |
| 329 |
{ |
| 330 |
PyObject *output; |
| 331 |
Py_ssize_t output_len; |
| 332 |
nldecoder_object *self = nldecoder_object_CAST(myself); |
| 333 |
|
| 334 |
CHECK_INITIALIZED_DECODER(self); |
| 335 |
|
| 336 |
/* decode input (with the eventual \r from a previous pass) */ |
| 337 |
if (self->decoder != Py_None) { |
| 338 |
output = PyObject_CallMethodObjArgs(self->decoder, |
| 339 |
&_Py_ID(decode), input, final ? Py_True : Py_False, NULL); |
| 340 |
} |
| 341 |
else { |
| 342 |
output = Py_NewRef(input); |
| 343 |
} |
| 344 |
|
| 345 |
if (check_decoded(output) < 0) |
| 346 |
return NULL; |
| 347 |
|
| 348 |
output_len = PyUnicode_GET_LENGTH(output); |
| 349 |
if (self->pendingcr && (final || output_len > 0)) { |
| 350 |
/* Prefix output with CR */ |
| 351 |
int kind; |
| 352 |
PyObject *modified; |
| 353 |
char *out; |
| 354 |
|
| 355 |
modified = PyUnicode_New(output_len + 1, |
| 356 |
PyUnicode_MAX_CHAR_VALUE(output)); |
| 357 |
if (modified == NULL) |
| 358 |
goto error; |
| 359 |
kind = PyUnicode_KIND(modified); |
| 360 |
out = PyUnicode_DATA(modified); |
| 361 |
PyUnicode_WRITE(kind, out, 0, '\r'); |
| 362 |
memcpy(out + kind, PyUnicode_DATA(output), kind * output_len); |
| 363 |
Py_SETREF(output, modified); |
| 364 |
self->pendingcr = 0; |
| 365 |
output_len++; |
| 366 |
} |
| 367 |
|
| 368 |
/* retain last \r even when not translating data: |
| 369 |
* then readline() is sure to get \r\n in one pass |
| 370 |
*/ |
| 371 |
if (!final) { |
| 372 |
if (output_len > 0 |
| 373 |
&& PyUnicode_READ_CHAR(output, output_len - 1) == '\r') |
| 374 |
{ |
| 375 |
PyObject *modified = PyUnicode_Substring(output, 0, output_len -1); |
| 376 |
if (modified == NULL) |
| 377 |
goto error; |
| 378 |
Py_SETREF(output, modified); |
| 379 |
self->pendingcr = 1; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
/* Record which newlines are read and do newline translation if desired, |
| 384 |
all in one pass. */ |
| 385 |
{ |
| 386 |
const void *in_str; |
| 387 |
Py_ssize_t len; |
| 388 |
int seennl = self->seennl; |
| 389 |
int only_lf = 0; |
| 390 |
int kind; |
| 391 |
|
| 392 |
in_str = PyUnicode_DATA(output); |
| 393 |
len = PyUnicode_GET_LENGTH(output); |
| 394 |
kind = PyUnicode_KIND(output); |
| 395 |
|
| 396 |
if (len == 0) |
| 397 |
return output; |
| 398 |
|
| 399 |
/* If, up to now, newlines are consistently \n, do a quick check |
| 400 |
for the \r *byte* with the libc's optimized memchr. |
| 401 |
*/ |
| 402 |
if (seennl == SEEN_LF || seennl == 0) { |
| 403 |
only_lf = (memchr(in_str, '\r', kind * len) == NULL); |
| 404 |
} |
| 405 |
|
| 406 |
if (only_lf) { |
| 407 |
/* If not already seen, quick scan for a possible "\n" character. |
| 408 |
(there's nothing else to be done, even when in translation mode) |
| 409 |
*/ |
| 410 |
if (seennl == 0 && |
| 411 |
memchr(in_str, '\n', kind * len) != NULL) { |
| 412 |
if (kind == PyUnicode_1BYTE_KIND) |
| 413 |
seennl |= SEEN_LF; |
| 414 |
else { |
| 415 |
Py_ssize_t i = 0; |
| 416 |
for (;;) { |
| 417 |
Py_UCS4 c; |
| 418 |
/* Fast loop for non-control characters */ |
| 419 |
while (PyUnicode_READ(kind, in_str, i) > '\n') |
| 420 |
i++; |
| 421 |
c = PyUnicode_READ(kind, in_str, i++); |
| 422 |
if (c == '\n') { |
| 423 |
seennl |= SEEN_LF; |
| 424 |
break; |
| 425 |
} |
| 426 |
if (i >= len) |
| 427 |
break; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
/* Finished: we have scanned for newlines, and none of them |
| 432 |
need translating */ |
| 433 |
} |
| 434 |
else if (!self->translate) { |
| 435 |
Py_ssize_t i = 0; |
| 436 |
/* We have already seen all newline types, no need to scan again */ |
| 437 |
if (seennl == SEEN_ALL) |
| 438 |
goto endscan; |
| 439 |
for (;;) { |
| 440 |
Py_UCS4 c; |
| 441 |
/* Fast loop for non-control characters */ |
| 442 |
while (PyUnicode_READ(kind, in_str, i) > '\r') |
| 443 |
i++; |
| 444 |
c = PyUnicode_READ(kind, in_str, i++); |
| 445 |
if (c == '\n') |
| 446 |
seennl |= SEEN_LF; |
| 447 |
else if (c == '\r') { |
| 448 |
if (PyUnicode_READ(kind, in_str, i) == '\n') { |
| 449 |
seennl |= SEEN_CRLF; |
| 450 |
i++; |
| 451 |
} |
| 452 |
else |
| 453 |
seennl |= SEEN_CR; |
| 454 |
} |
| 455 |
if (i >= len) |
| 456 |
break; |
| 457 |
if (seennl == SEEN_ALL) |
| 458 |
break; |
| 459 |
} |
| 460 |
endscan: |
| 461 |
; |
| 462 |
} |
| 463 |
else { |
| 464 |
void *translated; |
| 465 |
int kind = PyUnicode_KIND(output); |
| 466 |
const void *in_str = PyUnicode_DATA(output); |
| 467 |
Py_ssize_t in, out; |
| 468 |
/* XXX: Previous in-place translation here is disabled as |
| 469 |
resizing is not possible anymore */ |
| 470 |
/* We could try to optimize this so that we only do a copy |
| 471 |
when there is something to translate. On the other hand, |
| 472 |
we already know there is a \r byte, so chances are high |
| 473 |
that something needs to be done. */ |
| 474 |
translated = PyMem_Malloc(kind * len); |
| 475 |
if (translated == NULL) { |
| 476 |
PyErr_NoMemory(); |
| 477 |
goto error; |
| 478 |
} |
| 479 |
in = out = 0; |
| 480 |
for (;;) { |
| 481 |
Py_UCS4 c; |
| 482 |
/* Fast loop for non-control characters */ |
| 483 |
while ((c = PyUnicode_READ(kind, in_str, in++)) > '\r') |
| 484 |
PyUnicode_WRITE(kind, translated, out++, c); |
| 485 |
if (c == '\n') { |
| 486 |
PyUnicode_WRITE(kind, translated, out++, c); |
| 487 |
seennl |= SEEN_LF; |
| 488 |
continue; |
| 489 |
} |
| 490 |
if (c == '\r') { |
| 491 |
if (PyUnicode_READ(kind, in_str, in) == '\n') { |
| 492 |
in++; |
| 493 |
seennl |= SEEN_CRLF; |
| 494 |
} |
| 495 |
else |
| 496 |
seennl |= SEEN_CR; |
| 497 |
PyUnicode_WRITE(kind, translated, out++, '\n'); |
| 498 |
continue; |
| 499 |
} |
| 500 |
if (in > len) |
| 501 |
break; |
| 502 |
PyUnicode_WRITE(kind, translated, out++, c); |
| 503 |
} |
| 504 |
Py_DECREF(output); |
| 505 |
output = PyUnicode_FromKindAndData(kind, translated, out); |
| 506 |
PyMem_Free(translated); |
| 507 |
if (!output) |
| 508 |
return NULL; |
| 509 |
} |
| 510 |
self->seennl |= seennl; |
| 511 |
} |
| 512 |
|
| 513 |
return output; |
| 514 |
|
| 515 |
error: |
| 516 |
Py_DECREF(output); |
| 517 |
return NULL; |
| 518 |
} |
| 519 |
|
| 520 |
/*[clinic input] |
| 521 |
@critical_section |
| 522 |
_io.IncrementalNewlineDecoder.decode |
| 523 |
input: object |
| 524 |
final: bool = False |
| 525 |
[clinic start generated code]*/ |
| 526 |
|
| 527 |
static PyObject * |
| 528 |
_io_IncrementalNewlineDecoder_decode_impl(nldecoder_object *self, |
| 529 |
PyObject *input, int final) |
| 530 |
/*[clinic end generated code: output=0d486755bb37a66e input=9475d16a73168504]*/ |
| 531 |
{ |
| 532 |
return _PyIncrementalNewlineDecoder_decode((PyObject *) self, input, final); |
| 533 |
} |
| 534 |
|
| 535 |
/*[clinic input] |
| 536 |
@critical_section |
| 537 |
_io.IncrementalNewlineDecoder.getstate |
| 538 |
[clinic start generated code]*/ |
| 539 |
|
| 540 |
static PyObject * |
| 541 |
_io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self) |
| 542 |
/*[clinic end generated code: output=f0d2c9c136f4e0d0 input=dc3e1f27aa850f12]*/ |
| 543 |
{ |
| 544 |
PyObject *buffer; |
| 545 |
unsigned long long flag; |
| 546 |
|
| 547 |
CHECK_INITIALIZED_DECODER(self); |
| 548 |
|
| 549 |
if (self->decoder != Py_None) { |
| 550 |
PyObject *state = PyObject_CallMethodNoArgs(self->decoder, |
| 551 |
&_Py_ID(getstate)); |
| 552 |
if (state == NULL) |
| 553 |
return NULL; |
| 554 |
if (!PyTuple_Check(state)) { |
| 555 |
PyErr_SetString(PyExc_TypeError, |
| 556 |
"illegal decoder state"); |
| 557 |
Py_DECREF(state); |
| 558 |
return NULL; |
| 559 |
} |
| 560 |
if (!PyArg_ParseTuple(state, "OK;illegal decoder state", |
| 561 |
&buffer, &flag)) |
| 562 |
{ |
| 563 |
Py_DECREF(state); |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
Py_INCREF(buffer); |
| 567 |
Py_DECREF(state); |
| 568 |
} |
| 569 |
else { |
| 570 |
buffer = Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); |
| 571 |
flag = 0; |
| 572 |
} |
| 573 |
flag <<= 1; |
| 574 |
if (self->pendingcr) |
| 575 |
flag |= 1; |
| 576 |
return Py_BuildValue("NK", buffer, flag); |
| 577 |
} |
| 578 |
|
| 579 |
/*[clinic input] |
| 580 |
@critical_section |
| 581 |
_io.IncrementalNewlineDecoder.setstate |
| 582 |
state: object |
| 583 |
/ |
| 584 |
[clinic start generated code]*/ |
| 585 |
|
| 586 |
static PyObject * |
| 587 |
_io_IncrementalNewlineDecoder_setstate_impl(nldecoder_object *self, |
| 588 |
PyObject *state) |
| 589 |
/*[clinic end generated code: output=09135cb6e78a1dc8 input=275fd3982d2b08cb]*/ |
| 590 |
{ |
| 591 |
PyObject *buffer; |
| 592 |
unsigned long long flag; |
| 593 |
|
| 594 |
CHECK_INITIALIZED_DECODER(self); |
| 595 |
|
| 596 |
if (!PyTuple_Check(state)) { |
| 597 |
PyErr_SetString(PyExc_TypeError, "state argument must be a tuple"); |
| 598 |
return NULL; |
| 599 |
} |
| 600 |
if (!PyArg_ParseTuple(state, "OK;setstate(): illegal state argument", |
| 601 |
&buffer, &flag)) |
| 602 |
{ |
| 603 |
return NULL; |
| 604 |
} |
| 605 |
|
| 606 |
self->pendingcr = (int) (flag & 1); |
| 607 |
flag >>= 1; |
| 608 |
|
| 609 |
if (self->decoder != Py_None) { |
| 610 |
return _PyObject_CallMethod(self->decoder, &_Py_ID(setstate), |
| 611 |
"((OK))", buffer, flag); |
| 612 |
} |
| 613 |
else { |
| 614 |
Py_RETURN_NONE; |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
/*[clinic input] |
| 619 |
@critical_section |
| 620 |
_io.IncrementalNewlineDecoder.reset |
| 621 |
[clinic start generated code]*/ |
| 622 |
|
| 623 |
static PyObject * |
| 624 |
_io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self) |
| 625 |
/*[clinic end generated code: output=32fa40c7462aa8ff input=31bd8ae4e36cec83]*/ |
| 626 |
{ |
| 627 |
CHECK_INITIALIZED_DECODER(self); |
| 628 |
|
| 629 |
self->seennl = 0; |
| 630 |
self->pendingcr = 0; |
| 631 |
if (self->decoder != Py_None) |
| 632 |
return PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); |
| 633 |
else |
| 634 |
Py_RETURN_NONE; |
| 635 |
} |
| 636 |
|
| 637 |
static PyObject * |
| 638 |
incrementalnewlinedecoder_newlines_get(PyObject *op, void *Py_UNUSED(context)) |
| 639 |
{ |
| 640 |
nldecoder_object *self = nldecoder_object_CAST(op); |
| 641 |
CHECK_INITIALIZED_DECODER(self); |
| 642 |
|
| 643 |
switch (self->seennl) { |
| 644 |
case SEEN_CR: |
| 645 |
return PyUnicode_FromString("\r"); |
| 646 |
case SEEN_LF: |
| 647 |
return PyUnicode_FromString("\n"); |
| 648 |
case SEEN_CRLF: |
| 649 |
return PyUnicode_FromString("\r\n"); |
| 650 |
case SEEN_CR | SEEN_LF: |
| 651 |
return Py_BuildValue("ss", "\r", "\n"); |
| 652 |
case SEEN_CR | SEEN_CRLF: |
| 653 |
return Py_BuildValue("ss", "\r", "\r\n"); |
| 654 |
case SEEN_LF | SEEN_CRLF: |
| 655 |
return Py_BuildValue("ss", "\n", "\r\n"); |
| 656 |
case SEEN_CR | SEEN_LF | SEEN_CRLF: |
| 657 |
return Py_BuildValue("sss", "\r", "\n", "\r\n"); |
| 658 |
default: |
| 659 |
Py_RETURN_NONE; |
| 660 |
} |
| 661 |
|
| 662 |
} |
| 663 |
|
| 664 |
/* TextIOWrapper */ |
| 665 |
|
| 666 |
typedef PyObject *(*encodefunc_t)(PyObject *, PyObject *); |
| 667 |
|
| 668 |
struct textio |
| 669 |
{ |
| 670 |
PyObject_HEAD |
| 671 |
int ok; /* initialized? */ |
| 672 |
int detached; |
| 673 |
Py_ssize_t chunk_size; |
| 674 |
PyObject *buffer; |
| 675 |
PyObject *encoding; |
| 676 |
PyObject *encoder; |
| 677 |
PyObject *decoder; |
| 678 |
PyObject *readnl; |
| 679 |
PyObject *errors; |
| 680 |
const char *writenl; /* ASCII-encoded; NULL stands for \n */ |
| 681 |
char line_buffering; |
| 682 |
char write_through; |
| 683 |
char readuniversal; |
| 684 |
char readtranslate; |
| 685 |
char writetranslate; |
| 686 |
char seekable; |
| 687 |
char has_read1; |
| 688 |
char telling; |
| 689 |
char finalizing; |
| 690 |
/* Specialized encoding func (see below) */ |
| 691 |
encodefunc_t encodefunc; |
| 692 |
/* Whether or not it's the start of the stream */ |
| 693 |
char encoding_start_of_stream; |
| 694 |
|
| 695 |
/* Reads and writes are internally buffered in order to speed things up. |
| 696 |
However, any read will first flush the write buffer if itsn't empty. |
| 697 |
|
| 698 |
Please also note that text to be written is first encoded before being |
| 699 |
buffered. This is necessary so that encoding errors are immediately |
| 700 |
reported to the caller, but it unfortunately means that the |
| 701 |
IncrementalEncoder (whose encode() method is always written in Python) |
| 702 |
becomes a bottleneck for small writes. |
| 703 |
*/ |
| 704 |
PyObject *decoded_chars; /* buffer for text returned from decoder */ |
| 705 |
Py_ssize_t decoded_chars_used; /* offset into _decoded_chars for read() */ |
| 706 |
PyObject *pending_bytes; // data waiting to be written. |
| 707 |
// ascii unicode, bytes, or list of them. |
| 708 |
Py_ssize_t pending_bytes_count; |
| 709 |
|
| 710 |
/* snapshot is either NULL, or a tuple (dec_flags, next_input) where |
| 711 |
* dec_flags is the second (integer) item of the decoder state and |
| 712 |
* next_input is the chunk of input bytes that comes next after the |
| 713 |
* snapshot point. We use this to reconstruct decoder states in tell(). |
| 714 |
*/ |
| 715 |
PyObject *snapshot; |
| 716 |
/* Bytes-to-characters ratio for the current chunk. Serves as input for |
| 717 |
the heuristic in tell(). */ |
| 718 |
double b2cratio; |
| 719 |
|
| 720 |
/* Cache raw object if it's a FileIO object */ |
| 721 |
PyObject *raw; |
| 722 |
|
| 723 |
PyObject *weakreflist; |
| 724 |
PyObject *dict; |
| 725 |
|
| 726 |
_PyIO_State *state; |
| 727 |
}; |
| 728 |
|
| 729 |
#define textio_CAST(op) ((textio *)(op)) |
| 730 |
|
| 731 |
static void |
| 732 |
textiowrapper_set_decoded_chars(textio *self, PyObject *chars); |
| 733 |
|
| 734 |
/* A couple of specialized cases in order to bypass the slow incremental |
| 735 |
encoding methods for the most popular encodings. */ |
| 736 |
|
| 737 |
static PyObject * |
| 738 |
ascii_encode(PyObject *op, PyObject *text) |
| 739 |
{ |
| 740 |
textio *self = textio_CAST(op); |
| 741 |
return _PyUnicode_AsASCIIString(text, PyUnicode_AsUTF8(self->errors)); |
| 742 |
} |
| 743 |
|
| 744 |
static PyObject * |
| 745 |
utf16be_encode(PyObject *op, PyObject *text) |
| 746 |
{ |
| 747 |
textio *self = textio_CAST(op); |
| 748 |
return _PyUnicode_EncodeUTF16(text, PyUnicode_AsUTF8(self->errors), 1); |
| 749 |
} |
| 750 |
|
| 751 |
static PyObject * |
| 752 |
utf16le_encode(PyObject *op, PyObject *text) |
| 753 |
{ |
| 754 |
textio *self = textio_CAST(op); |
| 755 |
return _PyUnicode_EncodeUTF16(text, PyUnicode_AsUTF8(self->errors), -1); |
| 756 |
} |
| 757 |
|
| 758 |
static PyObject * |
| 759 |
utf16_encode(PyObject *op, PyObject *text) |
| 760 |
{ |
| 761 |
textio *self = textio_CAST(op); |
| 762 |
if (!self->encoding_start_of_stream) { |
| 763 |
/* Skip the BOM and use native byte ordering */ |
| 764 |
#if PY_BIG_ENDIAN |
| 765 |
return utf16be_encode(op, text); |
| 766 |
#else |
| 767 |
return utf16le_encode(op, text); |
| 768 |
#endif |
| 769 |
} |
| 770 |
return _PyUnicode_EncodeUTF16(text, PyUnicode_AsUTF8(self->errors), 0); |
| 771 |
} |
| 772 |
|
| 773 |
static PyObject * |
| 774 |
utf32be_encode(PyObject *op, PyObject *text) |
| 775 |
{ |
| 776 |
textio *self = textio_CAST(op); |
| 777 |
return _PyUnicode_EncodeUTF32(text, PyUnicode_AsUTF8(self->errors), 1); |
| 778 |
} |
| 779 |
|
| 780 |
static PyObject * |
| 781 |
utf32le_encode(PyObject *op, PyObject *text) |
| 782 |
{ |
| 783 |
textio *self = textio_CAST(op); |
| 784 |
return _PyUnicode_EncodeUTF32(text, PyUnicode_AsUTF8(self->errors), -1); |
| 785 |
} |
| 786 |
|
| 787 |
static PyObject * |
| 788 |
utf32_encode(PyObject *op, PyObject *text) |
| 789 |
{ |
| 790 |
textio *self = textio_CAST(op); |
| 791 |
if (!self->encoding_start_of_stream) { |
| 792 |
/* Skip the BOM and use native byte ordering */ |
| 793 |
#if PY_BIG_ENDIAN |
| 794 |
return utf32be_encode(op, text); |
| 795 |
#else |
| 796 |
return utf32le_encode(op, text); |
| 797 |
#endif |
| 798 |
} |
| 799 |
return _PyUnicode_EncodeUTF32(text, PyUnicode_AsUTF8(self->errors), 0); |
| 800 |
} |
| 801 |
|
| 802 |
static PyObject * |
| 803 |
utf8_encode(PyObject *op, PyObject *text) |
| 804 |
{ |
| 805 |
textio *self = textio_CAST(op); |
| 806 |
return _PyUnicode_AsUTF8String(text, PyUnicode_AsUTF8(self->errors)); |
| 807 |
} |
| 808 |
|
| 809 |
static PyObject * |
| 810 |
latin1_encode(PyObject *op, PyObject *text) |
| 811 |
{ |
| 812 |
textio *self = textio_CAST(op); |
| 813 |
return _PyUnicode_AsLatin1String(text, PyUnicode_AsUTF8(self->errors)); |
| 814 |
} |
| 815 |
|
| 816 |
// Return true when encoding can be skipped when text is ascii. |
| 817 |
static inline int |
| 818 |
is_asciicompat_encoding(encodefunc_t f) |
| 819 |
{ |
| 820 |
return f == ascii_encode || f == latin1_encode || f == utf8_encode; |
| 821 |
} |
| 822 |
|
| 823 |
/* Map normalized encoding names onto the specialized encoding funcs */ |
| 824 |
|
| 825 |
typedef struct { |
| 826 |
const char *name; |
| 827 |
encodefunc_t encodefunc; |
| 828 |
} encodefuncentry; |
| 829 |
|
| 830 |
static const encodefuncentry encodefuncs[] = { |
| 831 |
{"ascii", ascii_encode}, |
| 832 |
{"iso8859-1", latin1_encode}, |
| 833 |
{"utf-8", utf8_encode}, |
| 834 |
{"utf-16-be", utf16be_encode}, |
| 835 |
{"utf-16-le", utf16le_encode}, |
| 836 |
{"utf-16", utf16_encode}, |
| 837 |
{"utf-32-be", utf32be_encode}, |
| 838 |
{"utf-32-le", utf32le_encode}, |
| 839 |
{"utf-32", utf32_encode}, |
| 840 |
{NULL, NULL} |
| 841 |
}; |
| 842 |
|
| 843 |
static int |
| 844 |
validate_newline(const char *newline) |
| 845 |
{ |
| 846 |
if (newline && newline[0] != '\0' |
| 847 |
&& !(newline[0] == '\n' && newline[1] == '\0') |
| 848 |
&& !(newline[0] == '\r' && newline[1] == '\0') |
| 849 |
&& !(newline[0] == '\r' && newline[1] == '\n' && newline[2] == '\0')) { |
| 850 |
PyErr_Format(PyExc_ValueError, |
| 851 |
"illegal newline value: %s", newline); |
| 852 |
return -1; |
| 853 |
} |
| 854 |
return 0; |
| 855 |
} |
| 856 |
|
| 857 |
static int |
| 858 |
set_newline(textio *self, const char *newline) |
| 859 |
{ |
| 860 |
PyObject *old = self->readnl; |
| 861 |
if (newline == NULL) { |
| 862 |
self->readnl = NULL; |
| 863 |
} |
| 864 |
else { |
| 865 |
self->readnl = PyUnicode_FromString(newline); |
| 866 |
if (self->readnl == NULL) { |
| 867 |
self->readnl = old; |
| 868 |
return -1; |
| 869 |
} |
| 870 |
} |
| 871 |
self->readuniversal = (newline == NULL || newline[0] == '\0'); |
| 872 |
self->readtranslate = (newline == NULL); |
| 873 |
self->writetranslate = (newline == NULL || newline[0] != '\0'); |
| 874 |
if (!self->readuniversal && self->readnl != NULL) { |
| 875 |
// validate_newline() accepts only ASCII newlines. |
| 876 |
assert(PyUnicode_KIND(self->readnl) == PyUnicode_1BYTE_KIND); |
| 877 |
self->writenl = (const char *)PyUnicode_1BYTE_DATA(self->readnl); |
| 878 |
if (strcmp(self->writenl, "\n") == 0) { |
| 879 |
self->writenl = NULL; |
| 880 |
} |
| 881 |
} |
| 882 |
else { |
| 883 |
#ifdef MS_WINDOWS |
| 884 |
self->writenl = "\r\n"; |
| 885 |
#else |
| 886 |
self->writenl = NULL; |
| 887 |
#endif |
| 888 |
} |
| 889 |
Py_XDECREF(old); |
| 890 |
return 0; |
| 891 |
} |
| 892 |
|
| 893 |
static int |
| 894 |
_textiowrapper_set_decoder(textio *self, PyObject *codec_info, |
| 895 |
const char *errors) |
| 896 |
{ |
| 897 |
PyObject *res; |
| 898 |
int r; |
| 899 |
|
| 900 |
res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable)); |
| 901 |
if (res == NULL) |
| 902 |
return -1; |
| 903 |
|
| 904 |
r = PyObject_IsTrue(res); |
| 905 |
Py_DECREF(res); |
| 906 |
if (r == -1) |
| 907 |
return -1; |
| 908 |
|
| 909 |
if (r != 1) |
| 910 |
return 0; |
| 911 |
|
| 912 |
Py_CLEAR(self->decoder); |
| 913 |
self->decoder = _PyCodecInfo_GetIncrementalDecoder(codec_info, errors); |
| 914 |
if (self->decoder == NULL) |
| 915 |
return -1; |
| 916 |
|
| 917 |
if (self->readuniversal) { |
| 918 |
_PyIO_State *state = self->state; |
| 919 |
PyObject *incrementalDecoder = PyObject_CallFunctionObjArgs( |
| 920 |
(PyObject *)state->PyIncrementalNewlineDecoder_Type, |
| 921 |
self->decoder, self->readtranslate ? Py_True : Py_False, NULL); |
| 922 |
if (incrementalDecoder == NULL) |
| 923 |
return -1; |
| 924 |
Py_XSETREF(self->decoder, incrementalDecoder); |
| 925 |
} |
| 926 |
|
| 927 |
return 0; |
| 928 |
} |
| 929 |
|
| 930 |
static PyObject* |
| 931 |
_textiowrapper_decode(_PyIO_State *state, PyObject *decoder, PyObject *bytes, |
| 932 |
int eof) |
| 933 |
{ |
| 934 |
PyObject *chars; |
| 935 |
|
| 936 |
if (Py_IS_TYPE(decoder, state->PyIncrementalNewlineDecoder_Type)) |
| 937 |
chars = _PyIncrementalNewlineDecoder_decode(decoder, bytes, eof); |
| 938 |
else |
| 939 |
chars = PyObject_CallMethodObjArgs(decoder, &_Py_ID(decode), bytes, |
| 940 |
eof ? Py_True : Py_False, NULL); |
| 941 |
|
| 942 |
if (check_decoded(chars) < 0) |
| 943 |
// check_decoded already decreases refcount |
| 944 |
return NULL; |
| 945 |
|
| 946 |
return chars; |
| 947 |
} |
| 948 |
|
| 949 |
static int |
| 950 |
_textiowrapper_set_encoder(textio *self, PyObject *codec_info, |
| 951 |
const char *errors) |
| 952 |
{ |
| 953 |
PyObject *res; |
| 954 |
int r; |
| 955 |
|
| 956 |
res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable)); |
| 957 |
if (res == NULL) |
| 958 |
return -1; |
| 959 |
|
| 960 |
r = PyObject_IsTrue(res); |
| 961 |
Py_DECREF(res); |
| 962 |
if (r == -1) |
| 963 |
return -1; |
| 964 |
|
| 965 |
if (r != 1) |
| 966 |
return 0; |
| 967 |
|
| 968 |
Py_CLEAR(self->encoder); |
| 969 |
self->encodefunc = NULL; |
| 970 |
self->encoder = _PyCodecInfo_GetIncrementalEncoder(codec_info, errors); |
| 971 |
if (self->encoder == NULL) |
| 972 |
return -1; |
| 973 |
|
| 974 |
/* Get the normalized named of the codec */ |
| 975 |
if (PyObject_GetOptionalAttr(codec_info, &_Py_ID(name), &res) < 0) { |
| 976 |
return -1; |
| 977 |
} |
| 978 |
if (res != NULL && PyUnicode_Check(res)) { |
| 979 |
const encodefuncentry *e = encodefuncs; |
| 980 |
while (e->name != NULL) { |
| 981 |
if (_PyUnicode_EqualToASCIIString(res, e->name)) { |
| 982 |
self->encodefunc = e->encodefunc; |
| 983 |
break; |
| 984 |
} |
| 985 |
e++; |
| 986 |
} |
| 987 |
} |
| 988 |
Py_XDECREF(res); |
| 989 |
|
| 990 |
return 0; |
| 991 |
} |
| 992 |
|
| 993 |
static int |
| 994 |
_textiowrapper_fix_encoder_state(textio *self) |
| 995 |
{ |
| 996 |
if (!self->seekable || !self->encoder) { |
| 997 |
return 0; |
| 998 |
} |
| 999 |
|
| 1000 |
self->encoding_start_of_stream = 1; |
| 1001 |
|
| 1002 |
PyObject *cookieObj = PyObject_CallMethodNoArgs( |
| 1003 |
self->buffer, &_Py_ID(tell)); |
| 1004 |
if (cookieObj == NULL) { |
| 1005 |
return -1; |
| 1006 |
} |
| 1007 |
|
| 1008 |
int cmp = PyObject_RichCompareBool(cookieObj, _PyLong_GetZero(), Py_EQ); |
| 1009 |
Py_DECREF(cookieObj); |
| 1010 |
if (cmp < 0) { |
| 1011 |
return -1; |
| 1012 |
} |
| 1013 |
|
| 1014 |
if (cmp == 0) { |
| 1015 |
self->encoding_start_of_stream = 0; |
| 1016 |
PyObject *res = PyObject_CallMethodOneArg( |
| 1017 |
self->encoder, &_Py_ID(setstate), _PyLong_GetZero()); |
| 1018 |
if (res == NULL) { |
| 1019 |
return -1; |
| 1020 |
} |
| 1021 |
Py_DECREF(res); |
| 1022 |
} |
| 1023 |
|
| 1024 |
return 0; |
| 1025 |
} |
| 1026 |
|
| 1027 |
static int |
| 1028 |
io_check_errors(PyObject *errors) |
| 1029 |
{ |
| 1030 |
assert(errors != NULL && errors != Py_None); |
| 1031 |
|
| 1032 |
PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 1033 |
#ifndef Py_DEBUG |
| 1034 |
/* In release mode, only check in development mode (-X dev) */ |
| 1035 |
if (!_PyInterpreterState_GetConfig(interp)->dev_mode) { |
| 1036 |
return 0; |
| 1037 |
} |
| 1038 |
#else |
| 1039 |
/* Always check in debug mode */ |
| 1040 |
#endif |
| 1041 |
|
| 1042 |
/* Avoid calling PyCodec_LookupError() before the codec registry is ready: |
| 1043 |
before_PyUnicode_InitEncodings() is called. */ |
| 1044 |
if (!interp->unicode.fs_codec.encoding) { |
| 1045 |
return 0; |
| 1046 |
} |
| 1047 |
|
| 1048 |
const char *name = _PyUnicode_AsUTF8NoNUL(errors); |
| 1049 |
if (name == NULL) { |
| 1050 |
return -1; |
| 1051 |
} |
| 1052 |
PyObject *handler = PyCodec_LookupError(name); |
| 1053 |
if (handler != NULL) { |
| 1054 |
Py_DECREF(handler); |
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
return -1; |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
/*[clinic input] |
| 1063 |
_io.TextIOWrapper.__init__ |
| 1064 |
buffer: object |
| 1065 |
encoding: str(accept={str, NoneType}) = None |
| 1066 |
errors: object = None |
| 1067 |
newline: str(accept={str, NoneType}) = None |
| 1068 |
line_buffering: bool = False |
| 1069 |
write_through: bool = False |
| 1070 |
|
| 1071 |
Character and line based layer over a BufferedIOBase object, buffer. |
| 1072 |
|
| 1073 |
encoding gives the name of the encoding that the stream will be |
| 1074 |
decoded or encoded with. It defaults to locale.getencoding(). |
| 1075 |
|
| 1076 |
errors determines the strictness of encoding and decoding (see |
| 1077 |
help(codecs.Codec) or the documentation for codecs.register) and |
| 1078 |
defaults to "strict". |
| 1079 |
|
| 1080 |
newline controls how line endings are handled. It can be None, '', |
| 1081 |
'\n', '\r', and '\r\n'. It works as follows: |
| 1082 |
|
| 1083 |
* On input, if newline is None, universal newlines mode is |
| 1084 |
enabled. Lines in the input can end in '\n', '\r', or '\r\n', and |
| 1085 |
these are translated into '\n' before being returned to the |
| 1086 |
caller. If it is '', universal newline mode is enabled, but line |
| 1087 |
endings are returned to the caller untranslated. If it has any of |
| 1088 |
the other legal values, input lines are only terminated by the given |
| 1089 |
string, and the line ending is returned to the caller untranslated. |
| 1090 |
|
| 1091 |
* On output, if newline is None, any '\n' characters written are |
| 1092 |
translated to the system default line separator, os.linesep. If |
| 1093 |
newline is '' or '\n', no translation takes place. If newline is any |
| 1094 |
of the other legal values, any '\n' characters written are translated |
| 1095 |
to the given string. |
| 1096 |
|
| 1097 |
If line_buffering is True, a call to flush is implied when a call to |
| 1098 |
write contains a newline character. |
| 1099 |
[clinic start generated code]*/ |
| 1100 |
|
| 1101 |
static int |
| 1102 |
_io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, |
| 1103 |
const char *encoding, PyObject *errors, |
| 1104 |
const char *newline, int line_buffering, |
| 1105 |
int write_through) |
| 1106 |
/*[clinic end generated code: output=72267c0c01032ed2 input=e6cfaaaf6059d4f5]*/ |
| 1107 |
{ |
| 1108 |
PyObject *raw, *codec_info = NULL; |
| 1109 |
PyObject *res; |
| 1110 |
int r; |
| 1111 |
|
| 1112 |
self->ok = 0; |
| 1113 |
self->detached = 0; |
| 1114 |
|
| 1115 |
if (encoding == NULL) { |
| 1116 |
PyInterpreterState *interp = _PyInterpreterState_GET(); |
| 1117 |
if (_PyInterpreterState_GetConfig(interp)->warn_default_encoding) { |
| 1118 |
if (PyErr_WarnEx(PyExc_EncodingWarning, |
| 1119 |
"'encoding' argument not specified", 1)) { |
| 1120 |
return -1; |
| 1121 |
} |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
if (errors == Py_None) { |
| 1126 |
errors = &_Py_ID(strict); |
| 1127 |
} |
| 1128 |
else if (!PyUnicode_Check(errors)) { |
| 1129 |
// Check 'errors' argument here because Argument Clinic doesn't support |
| 1130 |
// 'str(accept={str, NoneType})' converter. |
| 1131 |
PyErr_Format( |
| 1132 |
PyExc_TypeError, |
| 1133 |
"TextIOWrapper() argument 'errors' must be str or None, not %.50s", |
| 1134 |
Py_TYPE(errors)->tp_name); |
| 1135 |
return -1; |
| 1136 |
} |
| 1137 |
else if (io_check_errors(errors)) { |
| 1138 |
return -1; |
| 1139 |
} |
| 1140 |
const char *errors_str = _PyUnicode_AsUTF8NoNUL(errors); |
| 1141 |
if (errors_str == NULL) { |
| 1142 |
return -1; |
| 1143 |
} |
| 1144 |
|
| 1145 |
if (validate_newline(newline) < 0) { |
| 1146 |
return -1; |
| 1147 |
} |
| 1148 |
|
| 1149 |
Py_CLEAR(self->buffer); |
| 1150 |
Py_CLEAR(self->encoding); |
| 1151 |
Py_CLEAR(self->encoder); |
| 1152 |
Py_CLEAR(self->decoder); |
| 1153 |
Py_CLEAR(self->readnl); |
| 1154 |
Py_CLEAR(self->decoded_chars); |
| 1155 |
Py_CLEAR(self->pending_bytes); |
| 1156 |
Py_CLEAR(self->snapshot); |
| 1157 |
Py_CLEAR(self->errors); |
| 1158 |
Py_CLEAR(self->raw); |
| 1159 |
self->decoded_chars_used = 0; |
| 1160 |
self->pending_bytes_count = 0; |
| 1161 |
self->encodefunc = NULL; |
| 1162 |
self->b2cratio = 0.0; |
| 1163 |
|
| 1164 |
if (encoding == NULL && _PyRuntime.preconfig.utf8_mode) { |
| 1165 |
_Py_DECLARE_STR(utf_8, "utf-8"); |
| 1166 |
self->encoding = &_Py_STR(utf_8); |
| 1167 |
} |
| 1168 |
else if (encoding == NULL || (strcmp(encoding, "locale") == 0)) { |
| 1169 |
self->encoding = _Py_GetLocaleEncodingObject(); |
| 1170 |
if (self->encoding == NULL) { |
| 1171 |
goto error; |
| 1172 |
} |
| 1173 |
assert(PyUnicode_Check(self->encoding)); |
| 1174 |
} |
| 1175 |
|
| 1176 |
if (self->encoding != NULL) { |
| 1177 |
encoding = PyUnicode_AsUTF8(self->encoding); |
| 1178 |
if (encoding == NULL) |
| 1179 |
goto error; |
| 1180 |
} |
| 1181 |
else if (encoding != NULL) { |
| 1182 |
self->encoding = PyUnicode_FromString(encoding); |
| 1183 |
if (self->encoding == NULL) |
| 1184 |
goto error; |
| 1185 |
} |
| 1186 |
else { |
| 1187 |
PyErr_SetString(PyExc_OSError, |
| 1188 |
"could not determine default encoding"); |
| 1189 |
goto error; |
| 1190 |
} |
| 1191 |
|
| 1192 |
/* Check we have been asked for a real text encoding */ |
| 1193 |
codec_info = _PyCodec_LookupTextEncoding(encoding, NULL); |
| 1194 |
if (codec_info == NULL) { |
| 1195 |
Py_CLEAR(self->encoding); |
| 1196 |
goto error; |
| 1197 |
} |
| 1198 |
|
| 1199 |
/* XXX: Failures beyond this point have the potential to leak elements |
| 1200 |
* of the partially constructed object (like self->encoding) |
| 1201 |
*/ |
| 1202 |
|
| 1203 |
self->errors = Py_NewRef(errors); |
| 1204 |
self->chunk_size = 8192; |
| 1205 |
self->line_buffering = line_buffering; |
| 1206 |
self->write_through = write_through; |
| 1207 |
if (set_newline(self, newline) < 0) { |
| 1208 |
goto error; |
| 1209 |
} |
| 1210 |
|
| 1211 |
self->buffer = Py_NewRef(buffer); |
| 1212 |
|
| 1213 |
/* Build the decoder object */ |
| 1214 |
_PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); |
| 1215 |
self->state = state; |
| 1216 |
if (_textiowrapper_set_decoder(self, codec_info, errors_str) != 0) |
| 1217 |
goto error; |
| 1218 |
|
| 1219 |
/* Build the encoder object */ |
| 1220 |
if (_textiowrapper_set_encoder(self, codec_info, errors_str) != 0) |
| 1221 |
goto error; |
| 1222 |
|
| 1223 |
/* Finished sorting out the codec details */ |
| 1224 |
Py_CLEAR(codec_info); |
| 1225 |
|
| 1226 |
if (Py_IS_TYPE(buffer, state->PyBufferedReader_Type) || |
| 1227 |
Py_IS_TYPE(buffer, state->PyBufferedWriter_Type) || |
| 1228 |
Py_IS_TYPE(buffer, state->PyBufferedRandom_Type)) |
| 1229 |
{ |
| 1230 |
if (PyObject_GetOptionalAttr(buffer, &_Py_ID(raw), &raw) < 0) |
| 1231 |
goto error; |
| 1232 |
/* Cache the raw FileIO object to speed up 'closed' checks */ |
| 1233 |
if (raw != NULL) { |
| 1234 |
if (Py_IS_TYPE(raw, state->PyFileIO_Type)) |
| 1235 |
self->raw = raw; |
| 1236 |
else |
| 1237 |
Py_DECREF(raw); |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
res = PyObject_CallMethodNoArgs(buffer, &_Py_ID(seekable)); |
| 1242 |
if (res == NULL) |
| 1243 |
goto error; |
| 1244 |
r = PyObject_IsTrue(res); |
| 1245 |
Py_DECREF(res); |
| 1246 |
if (r < 0) |
| 1247 |
goto error; |
| 1248 |
self->seekable = self->telling = r; |
| 1249 |
|
| 1250 |
r = PyObject_HasAttrWithError(buffer, &_Py_ID(read1)); |
| 1251 |
if (r < 0) { |
| 1252 |
goto error; |
| 1253 |
} |
| 1254 |
self->has_read1 = r; |
| 1255 |
|
| 1256 |
self->encoding_start_of_stream = 0; |
| 1257 |
if (_textiowrapper_fix_encoder_state(self) < 0) { |
| 1258 |
goto error; |
| 1259 |
} |
| 1260 |
|
| 1261 |
self->ok = 1; |
| 1262 |
return 0; |
| 1263 |
|
| 1264 |
error: |
| 1265 |
Py_XDECREF(codec_info); |
| 1266 |
return -1; |
| 1267 |
} |
| 1268 |
|
| 1269 |
/* Return *default_value* if ob is None, 0 if ob is false, 1 if ob is true, |
| 1270 |
* -1 on error. |
| 1271 |
*/ |
| 1272 |
static int |
| 1273 |
convert_optional_bool(PyObject *obj, int default_value) |
| 1274 |
{ |
| 1275 |
long v; |
| 1276 |
if (obj == Py_None) { |
| 1277 |
v = default_value; |
| 1278 |
} |
| 1279 |
else { |
| 1280 |
v = PyLong_AsLong(obj); |
| 1281 |
if (v == -1 && PyErr_Occurred()) |
| 1282 |
return -1; |
| 1283 |
} |
| 1284 |
return v != 0; |
| 1285 |
} |
| 1286 |
|
| 1287 |
static int |
| 1288 |
textiowrapper_change_encoding(textio *self, PyObject *encoding, |
| 1289 |
PyObject *errors, int newline_changed) |
| 1290 |
{ |
| 1291 |
/* Use existing settings where new settings are not specified */ |
| 1292 |
if (encoding == Py_None && errors == Py_None && !newline_changed) { |
| 1293 |
return 0; // no change |
| 1294 |
} |
| 1295 |
|
| 1296 |
if (encoding == Py_None) { |
| 1297 |
encoding = self->encoding; |
| 1298 |
if (errors == Py_None) { |
| 1299 |
errors = self->errors; |
| 1300 |
} |
| 1301 |
Py_INCREF(encoding); |
| 1302 |
} |
| 1303 |
else { |
| 1304 |
if (_PyUnicode_EqualToASCIIString(encoding, "locale")) { |
| 1305 |
encoding = _Py_GetLocaleEncodingObject(); |
| 1306 |
if (encoding == NULL) { |
| 1307 |
return -1; |
| 1308 |
} |
| 1309 |
} else { |
| 1310 |
Py_INCREF(encoding); |
| 1311 |
} |
| 1312 |
if (errors == Py_None) { |
| 1313 |
errors = &_Py_ID(strict); |
| 1314 |
} |
| 1315 |
} |
| 1316 |
Py_INCREF(errors); |
| 1317 |
|
| 1318 |
const char *c_encoding = PyUnicode_AsUTF8(encoding); |
| 1319 |
if (c_encoding == NULL) { |
| 1320 |
Py_DECREF(encoding); |
| 1321 |
Py_DECREF(errors); |
| 1322 |
return -1; |
| 1323 |
} |
| 1324 |
const char *c_errors = PyUnicode_AsUTF8(errors); |
| 1325 |
if (c_errors == NULL) { |
| 1326 |
Py_DECREF(encoding); |
| 1327 |
Py_DECREF(errors); |
| 1328 |
return -1; |
| 1329 |
} |
| 1330 |
|
| 1331 |
// Create new encoder & decoder |
| 1332 |
PyObject *codec_info = _PyCodec_LookupTextEncoding(c_encoding, NULL); |
| 1333 |
if (codec_info == NULL) { |
| 1334 |
Py_DECREF(encoding); |
| 1335 |
Py_DECREF(errors); |
| 1336 |
return -1; |
| 1337 |
} |
| 1338 |
if (_textiowrapper_set_decoder(self, codec_info, c_errors) != 0 || |
| 1339 |
_textiowrapper_set_encoder(self, codec_info, c_errors) != 0) { |
| 1340 |
Py_DECREF(codec_info); |
| 1341 |
Py_DECREF(encoding); |
| 1342 |
Py_DECREF(errors); |
| 1343 |
return -1; |
| 1344 |
} |
| 1345 |
Py_DECREF(codec_info); |
| 1346 |
|
| 1347 |
Py_SETREF(self->encoding, encoding); |
| 1348 |
Py_SETREF(self->errors, errors); |
| 1349 |
|
| 1350 |
return _textiowrapper_fix_encoder_state(self); |
| 1351 |
} |
| 1352 |
|
| 1353 |
/*[clinic input] |
| 1354 |
@critical_section |
| 1355 |
_io.TextIOWrapper.reconfigure |
| 1356 |
* |
| 1357 |
encoding: object = None |
| 1358 |
errors: object = None |
| 1359 |
newline as newline_obj: object(c_default="NULL") = None |
| 1360 |
line_buffering as line_buffering_obj: object = None |
| 1361 |
write_through as write_through_obj: object = None |
| 1362 |
|
| 1363 |
Reconfigure the text stream with new parameters. |
| 1364 |
|
| 1365 |
This also does an implicit stream flush. |
| 1366 |
|
| 1367 |
[clinic start generated code]*/ |
| 1368 |
|
| 1369 |
static PyObject * |
| 1370 |
_io_TextIOWrapper_reconfigure_impl(textio *self, PyObject *encoding, |
| 1371 |
PyObject *errors, PyObject *newline_obj, |
| 1372 |
PyObject *line_buffering_obj, |
| 1373 |
PyObject *write_through_obj) |
| 1374 |
/*[clinic end generated code: output=52b812ff4b3d4b0f input=dc3bd35ebda702a7]*/ |
| 1375 |
{ |
| 1376 |
int line_buffering; |
| 1377 |
int write_through; |
| 1378 |
const char *newline = NULL; |
| 1379 |
|
| 1380 |
if (encoding != Py_None && !PyUnicode_Check(encoding)) { |
| 1381 |
PyErr_Format(PyExc_TypeError, |
| 1382 |
"reconfigure() argument 'encoding' must be str or None, not %s", |
| 1383 |
Py_TYPE(encoding)->tp_name); |
| 1384 |
return NULL; |
| 1385 |
} |
| 1386 |
if (errors != Py_None && !PyUnicode_Check(errors)) { |
| 1387 |
PyErr_Format(PyExc_TypeError, |
| 1388 |
"reconfigure() argument 'errors' must be str or None, not %s", |
| 1389 |
Py_TYPE(errors)->tp_name); |
| 1390 |
return NULL; |
| 1391 |
} |
| 1392 |
if (newline_obj != NULL && newline_obj != Py_None && |
| 1393 |
!PyUnicode_Check(newline_obj)) |
| 1394 |
{ |
| 1395 |
PyErr_Format(PyExc_TypeError, |
| 1396 |
"reconfigure() argument 'newline' must be str or None, not %s", |
| 1397 |
Py_TYPE(newline_obj)->tp_name); |
| 1398 |
return NULL; |
| 1399 |
} |
| 1400 |
/* Check if something is in the read buffer */ |
| 1401 |
if (self->decoded_chars != NULL) { |
| 1402 |
if (encoding != Py_None || errors != Py_None || newline_obj != NULL) { |
| 1403 |
_unsupported(self->state, |
| 1404 |
"It is not possible to set the encoding or newline " |
| 1405 |
"of stream after the first read"); |
| 1406 |
return NULL; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
|
| 1410 |
if (newline_obj != NULL && newline_obj != Py_None) { |
| 1411 |
newline = PyUnicode_AsUTF8(newline_obj); |
| 1412 |
if (newline == NULL || validate_newline(newline) < 0) { |
| 1413 |
return NULL; |
| 1414 |
} |
| 1415 |
} |
| 1416 |
|
| 1417 |
line_buffering = convert_optional_bool(line_buffering_obj, |
| 1418 |
self->line_buffering); |
| 1419 |
if (line_buffering < 0) { |
| 1420 |
return NULL; |
| 1421 |
} |
| 1422 |
write_through = convert_optional_bool(write_through_obj, |
| 1423 |
self->write_through); |
| 1424 |
if (write_through < 0) { |
| 1425 |
return NULL; |
| 1426 |
} |
| 1427 |
|
| 1428 |
if (_PyFile_Flush((PyObject *)self) < 0) { |
| 1429 |
return NULL; |
| 1430 |
} |
| 1431 |
self->b2cratio = 0; |
| 1432 |
|
| 1433 |
if (newline_obj != NULL && set_newline(self, newline) < 0) { |
| 1434 |
return NULL; |
| 1435 |
} |
| 1436 |
|
| 1437 |
if (textiowrapper_change_encoding( |
| 1438 |
self, encoding, errors, newline_obj != NULL) < 0) { |
| 1439 |
return NULL; |
| 1440 |
} |
| 1441 |
|
| 1442 |
self->line_buffering = line_buffering; |
| 1443 |
self->write_through = write_through; |
| 1444 |
Py_RETURN_NONE; |
| 1445 |
} |
| 1446 |
|
| 1447 |
static int |
| 1448 |
textiowrapper_clear(PyObject *op) |
| 1449 |
{ |
| 1450 |
textio *self = textio_CAST(op); |
| 1451 |
self->ok = 0; |
| 1452 |
Py_CLEAR(self->buffer); |
| 1453 |
Py_CLEAR(self->encoding); |
| 1454 |
Py_CLEAR(self->encoder); |
| 1455 |
Py_CLEAR(self->decoder); |
| 1456 |
Py_CLEAR(self->readnl); |
| 1457 |
Py_CLEAR(self->decoded_chars); |
| 1458 |
Py_CLEAR(self->pending_bytes); |
| 1459 |
Py_CLEAR(self->snapshot); |
| 1460 |
Py_CLEAR(self->errors); |
| 1461 |
Py_CLEAR(self->raw); |
| 1462 |
|
| 1463 |
Py_CLEAR(self->dict); |
| 1464 |
return 0; |
| 1465 |
} |
| 1466 |
|
| 1467 |
static void |
| 1468 |
textiowrapper_dealloc(PyObject *op) |
| 1469 |
{ |
| 1470 |
textio *self = textio_CAST(op); |
| 1471 |
PyTypeObject *tp = Py_TYPE(self); |
| 1472 |
self->finalizing = 1; |
| 1473 |
if (_PyIOBase_finalize(op) < 0) |
| 1474 |
return; |
| 1475 |
self->ok = 0; |
| 1476 |
_PyObject_GC_UNTRACK(self); |
| 1477 |
FT_CLEAR_WEAKREFS(op, self->weakreflist); |
| 1478 |
(void)textiowrapper_clear(op); |
| 1479 |
tp->tp_free(self); |
| 1480 |
Py_DECREF(tp); |
| 1481 |
} |
| 1482 |
|
| 1483 |
static int |
| 1484 |
textiowrapper_traverse(PyObject *op, visitproc visit, void *arg) |
| 1485 |
{ |
| 1486 |
textio *self = textio_CAST(op); |
| 1487 |
Py_VISIT(Py_TYPE(self)); |
| 1488 |
Py_VISIT(self->buffer); |
| 1489 |
Py_VISIT(self->encoding); |
| 1490 |
Py_VISIT(self->encoder); |
| 1491 |
Py_VISIT(self->decoder); |
| 1492 |
Py_VISIT(self->readnl); |
| 1493 |
Py_VISIT(self->decoded_chars); |
| 1494 |
Py_VISIT(self->pending_bytes); |
| 1495 |
Py_VISIT(self->snapshot); |
| 1496 |
Py_VISIT(self->errors); |
| 1497 |
Py_VISIT(self->raw); |
| 1498 |
|
| 1499 |
Py_VISIT(self->dict); |
| 1500 |
return 0; |
| 1501 |
} |
| 1502 |
|
| 1503 |
static PyObject * |
| 1504 |
_io_TextIOWrapper_closed_get_impl(textio *self); |
| 1505 |
|
| 1506 |
/* This macro takes some shortcuts to make the common case faster. */ |
| 1507 |
#define CHECK_CLOSED(self) \ |
| 1508 |
do { \ |
| 1509 |
int r; \ |
| 1510 |
PyObject *_res; \ |
| 1511 |
if (Py_IS_TYPE(self, self->state->PyTextIOWrapper_Type)) { \ |
| 1512 |
if (self->raw != NULL) \ |
| 1513 |
r = _PyFileIO_closed(self->raw); \ |
| 1514 |
else { \ |
| 1515 |
_res = _io_TextIOWrapper_closed_get_impl(self); \ |
| 1516 |
if (_res == NULL) \ |
| 1517 |
return NULL; \ |
| 1518 |
r = PyObject_IsTrue(_res); \ |
| 1519 |
Py_DECREF(_res); \ |
| 1520 |
if (r < 0) \ |
| 1521 |
return NULL; \ |
| 1522 |
} \ |
| 1523 |
if (r > 0) { \ |
| 1524 |
PyErr_SetString(PyExc_ValueError, \ |
| 1525 |
"I/O operation on closed file."); \ |
| 1526 |
return NULL; \ |
| 1527 |
} \ |
| 1528 |
} \ |
| 1529 |
else if (_PyIOBase_check_closed((PyObject *)self, Py_True) == NULL) \ |
| 1530 |
return NULL; \ |
| 1531 |
} while (0) |
| 1532 |
|
| 1533 |
#define CHECK_INITIALIZED(self) \ |
| 1534 |
if (self->ok <= 0) { \ |
| 1535 |
PyErr_SetString(PyExc_ValueError, \ |
| 1536 |
"I/O operation on uninitialized object"); \ |
| 1537 |
return NULL; \ |
| 1538 |
} |
| 1539 |
|
| 1540 |
#define CHECK_ATTACHED(self) \ |
| 1541 |
CHECK_INITIALIZED(self); \ |
| 1542 |
if (self->detached) { \ |
| 1543 |
PyErr_SetString(PyExc_ValueError, \ |
| 1544 |
"underlying buffer has been detached"); \ |
| 1545 |
return NULL; \ |
| 1546 |
} |
| 1547 |
|
| 1548 |
#define CHECK_ATTACHED_INT(self) \ |
| 1549 |
if (self->ok <= 0) { \ |
| 1550 |
PyErr_SetString(PyExc_ValueError, \ |
| 1551 |
"I/O operation on uninitialized object"); \ |
| 1552 |
return -1; \ |
| 1553 |
} else if (self->detached) { \ |
| 1554 |
PyErr_SetString(PyExc_ValueError, \ |
| 1555 |
"underlying buffer has been detached"); \ |
| 1556 |
return -1; \ |
| 1557 |
} |
| 1558 |
|
| 1559 |
|
| 1560 |
/*[clinic input] |
| 1561 |
@critical_section |
| 1562 |
_io.TextIOWrapper.detach |
| 1563 |
[clinic start generated code]*/ |
| 1564 |
|
| 1565 |
static PyObject * |
| 1566 |
_io_TextIOWrapper_detach_impl(textio *self) |
| 1567 |
/*[clinic end generated code: output=7ba3715cd032d5f2 input=c908a3b4ef203b0f]*/ |
| 1568 |
{ |
| 1569 |
PyObject *buffer; |
| 1570 |
CHECK_ATTACHED(self); |
| 1571 |
if (_PyFile_Flush((PyObject *)self) < 0) { |
| 1572 |
return NULL; |
| 1573 |
} |
| 1574 |
buffer = self->buffer; |
| 1575 |
self->buffer = NULL; |
| 1576 |
self->detached = 1; |
| 1577 |
return buffer; |
| 1578 |
} |
| 1579 |
|
| 1580 |
/* Flush the internal write buffer. This doesn't explicitly flush the |
| 1581 |
underlying buffered object, though. */ |
| 1582 |
static int |
| 1583 |
_textiowrapper_writeflush(textio *self) |
| 1584 |
{ |
| 1585 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 1586 |
|
| 1587 |
if (self->pending_bytes == NULL) |
| 1588 |
return 0; |
| 1589 |
|
| 1590 |
PyObject *pending = self->pending_bytes; |
| 1591 |
PyObject *b; |
| 1592 |
|
| 1593 |
if (PyBytes_Check(pending)) { |
| 1594 |
b = Py_NewRef(pending); |
| 1595 |
} |
| 1596 |
else if (PyUnicode_Check(pending)) { |
| 1597 |
assert(PyUnicode_IS_ASCII(pending)); |
| 1598 |
assert(PyUnicode_GET_LENGTH(pending) == self->pending_bytes_count); |
| 1599 |
b = PyBytes_FromStringAndSize( |
| 1600 |
PyUnicode_DATA(pending), PyUnicode_GET_LENGTH(pending)); |
| 1601 |
if (b == NULL) { |
| 1602 |
return -1; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
else { |
| 1606 |
assert(PyList_Check(pending)); |
| 1607 |
b = PyBytes_FromStringAndSize(NULL, self->pending_bytes_count); |
| 1608 |
if (b == NULL) { |
| 1609 |
return -1; |
| 1610 |
} |
| 1611 |
|
| 1612 |
char *buf = PyBytes_AsString(b); |
| 1613 |
Py_ssize_t pos = 0; |
| 1614 |
|
| 1615 |
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(pending); i++) { |
| 1616 |
PyObject *obj = PyList_GET_ITEM(pending, i); |
| 1617 |
char *src; |
| 1618 |
Py_ssize_t len; |
| 1619 |
if (PyUnicode_Check(obj)) { |
| 1620 |
assert(PyUnicode_IS_ASCII(obj)); |
| 1621 |
src = PyUnicode_DATA(obj); |
| 1622 |
len = PyUnicode_GET_LENGTH(obj); |
| 1623 |
} |
| 1624 |
else { |
| 1625 |
assert(PyBytes_Check(obj)); |
| 1626 |
if (PyBytes_AsStringAndSize(obj, &src, &len) < 0) { |
| 1627 |
Py_DECREF(b); |
| 1628 |
return -1; |
| 1629 |
} |
| 1630 |
} |
| 1631 |
memcpy(buf + pos, src, len); |
| 1632 |
pos += len; |
| 1633 |
} |
| 1634 |
assert(pos == self->pending_bytes_count); |
| 1635 |
} |
| 1636 |
|
| 1637 |
self->pending_bytes_count = 0; |
| 1638 |
self->pending_bytes = NULL; |
| 1639 |
Py_DECREF(pending); |
| 1640 |
|
| 1641 |
PyObject *ret; |
| 1642 |
do { |
| 1643 |
ret = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(write), b); |
| 1644 |
} while (ret == NULL && _PyIO_trap_eintr()); |
| 1645 |
Py_DECREF(b); |
| 1646 |
// NOTE: We cleared buffer but we don't know how many bytes are actually written |
| 1647 |
// when an error occurred. |
| 1648 |
if (ret == NULL) |
| 1649 |
return -1; |
| 1650 |
Py_DECREF(ret); |
| 1651 |
return 0; |
| 1652 |
} |
| 1653 |
|
| 1654 |
/*[clinic input] |
| 1655 |
@critical_section |
| 1656 |
_io.TextIOWrapper.write |
| 1657 |
text: unicode |
| 1658 |
/ |
| 1659 |
[clinic start generated code]*/ |
| 1660 |
|
| 1661 |
static PyObject * |
| 1662 |
_io_TextIOWrapper_write_impl(textio *self, PyObject *text) |
| 1663 |
/*[clinic end generated code: output=d2deb0d50771fcec input=73ec95c5c4a3489c]*/ |
| 1664 |
{ |
| 1665 |
PyObject *ret; |
| 1666 |
PyObject *b; |
| 1667 |
Py_ssize_t textlen; |
| 1668 |
int haslf = 0; |
| 1669 |
int needflush = 0, text_needflush = 0; |
| 1670 |
|
| 1671 |
CHECK_ATTACHED(self); |
| 1672 |
CHECK_CLOSED(self); |
| 1673 |
|
| 1674 |
if (self->encoder == NULL) { |
| 1675 |
return _unsupported(self->state, "not writable"); |
| 1676 |
} |
| 1677 |
|
| 1678 |
Py_INCREF(text); |
| 1679 |
|
| 1680 |
textlen = PyUnicode_GET_LENGTH(text); |
| 1681 |
|
| 1682 |
if ((self->writetranslate && self->writenl != NULL) || self->line_buffering) |
| 1683 |
if (PyUnicode_FindChar(text, '\n', 0, PyUnicode_GET_LENGTH(text), 1) != -1) |
| 1684 |
haslf = 1; |
| 1685 |
|
| 1686 |
if (haslf && self->writetranslate && self->writenl != NULL) { |
| 1687 |
PyObject *newtext = _PyObject_CallMethod(text, &_Py_ID(replace), |
| 1688 |
"ss", "\n", self->writenl); |
| 1689 |
Py_DECREF(text); |
| 1690 |
if (newtext == NULL) |
| 1691 |
return NULL; |
| 1692 |
text = newtext; |
| 1693 |
} |
| 1694 |
|
| 1695 |
if (self->write_through) |
| 1696 |
text_needflush = 1; |
| 1697 |
if (self->line_buffering && |
| 1698 |
(haslf || |
| 1699 |
PyUnicode_FindChar(text, '\r', 0, PyUnicode_GET_LENGTH(text), 1) != -1)) |
| 1700 |
needflush = 1; |
| 1701 |
|
| 1702 |
/* XXX What if we were just reading? */ |
| 1703 |
if (self->encodefunc != NULL) { |
| 1704 |
if (PyUnicode_IS_ASCII(text) && |
| 1705 |
// See bpo-43260 |
| 1706 |
PyUnicode_GET_LENGTH(text) <= self->chunk_size && |
| 1707 |
is_asciicompat_encoding(self->encodefunc)) { |
| 1708 |
b = Py_NewRef(text); |
| 1709 |
} |
| 1710 |
else { |
| 1711 |
b = (*self->encodefunc)((PyObject *) self, text); |
| 1712 |
} |
| 1713 |
self->encoding_start_of_stream = 0; |
| 1714 |
} |
| 1715 |
else { |
| 1716 |
b = PyObject_CallMethodOneArg(self->encoder, &_Py_ID(encode), text); |
| 1717 |
} |
| 1718 |
|
| 1719 |
Py_DECREF(text); |
| 1720 |
if (b == NULL) |
| 1721 |
return NULL; |
| 1722 |
if (b != text && !PyBytes_Check(b)) { |
| 1723 |
PyErr_Format(PyExc_TypeError, |
| 1724 |
"encoder should return a bytes object, not '%.200s'", |
| 1725 |
Py_TYPE(b)->tp_name); |
| 1726 |
Py_DECREF(b); |
| 1727 |
return NULL; |
| 1728 |
} |
| 1729 |
|
| 1730 |
Py_ssize_t bytes_len; |
| 1731 |
if (b == text) { |
| 1732 |
bytes_len = PyUnicode_GET_LENGTH(b); |
| 1733 |
} |
| 1734 |
else { |
| 1735 |
bytes_len = PyBytes_GET_SIZE(b); |
| 1736 |
} |
| 1737 |
|
| 1738 |
// We should avoid concatenating huge data. |
| 1739 |
// Flush the buffer before adding b to the buffer if b is not small. |
| 1740 |
// https://github.com/python/cpython/issues/87426 |
| 1741 |
if (bytes_len >= self->chunk_size) { |
| 1742 |
// _textiowrapper_writeflush() calls buffer.write(). |
| 1743 |
// self->pending_bytes can be appended during buffer->write() |
| 1744 |
// or other thread. |
| 1745 |
// We need to loop until buffer becomes empty. |
| 1746 |
// https://github.com/python/cpython/issues/118138 |
| 1747 |
// https://github.com/python/cpython/issues/119506 |
| 1748 |
while (self->pending_bytes != NULL) { |
| 1749 |
if (_textiowrapper_writeflush(self) < 0) { |
| 1750 |
Py_DECREF(b); |
| 1751 |
return NULL; |
| 1752 |
} |
| 1753 |
} |
| 1754 |
} |
| 1755 |
|
| 1756 |
if (self->pending_bytes == NULL) { |
| 1757 |
assert(self->pending_bytes_count == 0); |
| 1758 |
self->pending_bytes = b; |
| 1759 |
} |
| 1760 |
else if (!PyList_CheckExact(self->pending_bytes)) { |
| 1761 |
PyObject *list = PyList_New(2); |
| 1762 |
if (list == NULL) { |
| 1763 |
Py_DECREF(b); |
| 1764 |
return NULL; |
| 1765 |
} |
| 1766 |
// Since Python 3.12, allocating GC object won't trigger GC and release |
| 1767 |
// GIL. See https://github.com/python/cpython/issues/97922 |
| 1768 |
assert(!PyList_CheckExact(self->pending_bytes)); |
| 1769 |
PyList_SET_ITEM(list, 0, self->pending_bytes); |
| 1770 |
PyList_SET_ITEM(list, 1, b); |
| 1771 |
self->pending_bytes = list; |
| 1772 |
} |
| 1773 |
else { |
| 1774 |
if (PyList_Append(self->pending_bytes, b) < 0) { |
| 1775 |
Py_DECREF(b); |
| 1776 |
return NULL; |
| 1777 |
} |
| 1778 |
Py_DECREF(b); |
| 1779 |
} |
| 1780 |
|
| 1781 |
self->pending_bytes_count += bytes_len; |
| 1782 |
if (self->pending_bytes_count >= self->chunk_size || needflush || |
| 1783 |
text_needflush) { |
| 1784 |
if (_textiowrapper_writeflush(self) < 0) |
| 1785 |
return NULL; |
| 1786 |
} |
| 1787 |
|
| 1788 |
if (needflush) { |
| 1789 |
if (_PyFile_Flush(self->buffer) < 0) { |
| 1790 |
return NULL; |
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
if (self->snapshot != NULL) { |
| 1795 |
textiowrapper_set_decoded_chars(self, NULL); |
| 1796 |
Py_CLEAR(self->snapshot); |
| 1797 |
} |
| 1798 |
|
| 1799 |
if (self->decoder) { |
| 1800 |
ret = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); |
| 1801 |
if (ret == NULL) |
| 1802 |
return NULL; |
| 1803 |
Py_DECREF(ret); |
| 1804 |
} |
| 1805 |
|
| 1806 |
return PyLong_FromSsize_t(textlen); |
| 1807 |
} |
| 1808 |
|
| 1809 |
/* Steal a reference to chars and store it in the decoded_char buffer; |
| 1810 |
*/ |
| 1811 |
static void |
| 1812 |
textiowrapper_set_decoded_chars(textio *self, PyObject *chars) |
| 1813 |
{ |
| 1814 |
Py_XSETREF(self->decoded_chars, chars); |
| 1815 |
self->decoded_chars_used = 0; |
| 1816 |
} |
| 1817 |
|
| 1818 |
static PyObject * |
| 1819 |
textiowrapper_get_decoded_chars(textio *self, Py_ssize_t n) |
| 1820 |
{ |
| 1821 |
PyObject *chars; |
| 1822 |
Py_ssize_t avail; |
| 1823 |
|
| 1824 |
if (self->decoded_chars == NULL) |
| 1825 |
return Py_GetConstant(Py_CONSTANT_EMPTY_STR); |
| 1826 |
|
| 1827 |
avail = (PyUnicode_GET_LENGTH(self->decoded_chars) |
| 1828 |
- self->decoded_chars_used); |
| 1829 |
|
| 1830 |
assert(avail >= 0); |
| 1831 |
|
| 1832 |
if (n < 0 || n > avail) |
| 1833 |
n = avail; |
| 1834 |
|
| 1835 |
if (self->decoded_chars_used > 0 || n < avail) { |
| 1836 |
chars = PyUnicode_Substring(self->decoded_chars, |
| 1837 |
self->decoded_chars_used, |
| 1838 |
self->decoded_chars_used + n); |
| 1839 |
if (chars == NULL) |
| 1840 |
return NULL; |
| 1841 |
} |
| 1842 |
else { |
| 1843 |
chars = Py_NewRef(self->decoded_chars); |
| 1844 |
} |
| 1845 |
|
| 1846 |
self->decoded_chars_used += n; |
| 1847 |
return chars; |
| 1848 |
} |
| 1849 |
|
| 1850 |
/* Read and decode the next chunk of data from the BufferedReader. |
| 1851 |
*/ |
| 1852 |
static int |
| 1853 |
textiowrapper_read_chunk(textio *self, Py_ssize_t size_hint) |
| 1854 |
{ |
| 1855 |
PyObject *dec_buffer = NULL; |
| 1856 |
PyObject *dec_flags = NULL; |
| 1857 |
PyObject *input_chunk = NULL; |
| 1858 |
Py_buffer input_chunk_buf; |
| 1859 |
PyObject *decoded_chars, *chunk_size; |
| 1860 |
Py_ssize_t nbytes, nchars; |
| 1861 |
int eof; |
| 1862 |
|
| 1863 |
/* The return value is True unless EOF was reached. The decoded string is |
| 1864 |
* placed in self._decoded_chars (replacing its previous value). The |
| 1865 |
* entire input chunk is sent to the decoder, though some of it may remain |
| 1866 |
* buffered in the decoder, yet to be converted. |
| 1867 |
*/ |
| 1868 |
|
| 1869 |
if (self->decoder == NULL) { |
| 1870 |
_unsupported(self->state, "not readable"); |
| 1871 |
return -1; |
| 1872 |
} |
| 1873 |
|
| 1874 |
if (self->telling) { |
| 1875 |
/* To prepare for tell(), we need to snapshot a point in the file |
| 1876 |
* where the decoder's input buffer is empty. |
| 1877 |
*/ |
| 1878 |
PyObject *state = PyObject_CallMethodNoArgs(self->decoder, |
| 1879 |
&_Py_ID(getstate)); |
| 1880 |
if (state == NULL) |
| 1881 |
return -1; |
| 1882 |
/* Given this, we know there was a valid snapshot point |
| 1883 |
* len(dec_buffer) bytes ago with decoder state (b'', dec_flags). |
| 1884 |
*/ |
| 1885 |
if (!PyTuple_Check(state)) { |
| 1886 |
PyErr_SetString(PyExc_TypeError, |
| 1887 |
"illegal decoder state"); |
| 1888 |
Py_DECREF(state); |
| 1889 |
return -1; |
| 1890 |
} |
| 1891 |
if (!PyArg_ParseTuple(state, |
| 1892 |
"OO;illegal decoder state", &dec_buffer, &dec_flags)) |
| 1893 |
{ |
| 1894 |
Py_DECREF(state); |
| 1895 |
return -1; |
| 1896 |
} |
| 1897 |
|
| 1898 |
if (!PyBytes_Check(dec_buffer)) { |
| 1899 |
PyErr_Format(PyExc_TypeError, |
| 1900 |
"illegal decoder state: the first item should be a " |
| 1901 |
"bytes object, not '%.200s'", |
| 1902 |
Py_TYPE(dec_buffer)->tp_name); |
| 1903 |
Py_DECREF(state); |
| 1904 |
return -1; |
| 1905 |
} |
| 1906 |
Py_INCREF(dec_buffer); |
| 1907 |
Py_INCREF(dec_flags); |
| 1908 |
Py_DECREF(state); |
| 1909 |
} |
| 1910 |
|
| 1911 |
/* Read a chunk, decode it, and put the result in self._decoded_chars. */ |
| 1912 |
if (size_hint > 0) { |
| 1913 |
size_hint = (Py_ssize_t)(Py_MAX(self->b2cratio, 1.0) * size_hint); |
| 1914 |
} |
| 1915 |
chunk_size = PyLong_FromSsize_t(Py_MAX(self->chunk_size, size_hint)); |
| 1916 |
if (chunk_size == NULL) |
| 1917 |
goto fail; |
| 1918 |
|
| 1919 |
input_chunk = PyObject_CallMethodOneArg(self->buffer, |
| 1920 |
(self->has_read1 ? &_Py_ID(read1): &_Py_ID(read)), |
| 1921 |
chunk_size); |
| 1922 |
Py_DECREF(chunk_size); |
| 1923 |
if (input_chunk == NULL) |
| 1924 |
goto fail; |
| 1925 |
|
| 1926 |
if (PyObject_GetBuffer(input_chunk, &input_chunk_buf, 0) != 0) { |
| 1927 |
PyErr_Format(PyExc_TypeError, |
| 1928 |
"underlying %s() should have returned a bytes-like object, " |
| 1929 |
"not '%.200s'", (self->has_read1 ? "read1": "read"), |
| 1930 |
Py_TYPE(input_chunk)->tp_name); |
| 1931 |
goto fail; |
| 1932 |
} |
| 1933 |
|
| 1934 |
nbytes = input_chunk_buf.len; |
| 1935 |
eof = (nbytes == 0); |
| 1936 |
|
| 1937 |
decoded_chars = _textiowrapper_decode(self->state, self->decoder, |
| 1938 |
input_chunk, eof); |
| 1939 |
PyBuffer_Release(&input_chunk_buf); |
| 1940 |
if (decoded_chars == NULL) |
| 1941 |
goto fail; |
| 1942 |
|
| 1943 |
textiowrapper_set_decoded_chars(self, decoded_chars); |
| 1944 |
nchars = PyUnicode_GET_LENGTH(decoded_chars); |
| 1945 |
if (nchars > 0) |
| 1946 |
self->b2cratio = (double) nbytes / nchars; |
| 1947 |
else |
| 1948 |
self->b2cratio = 0.0; |
| 1949 |
if (nchars > 0) |
| 1950 |
eof = 0; |
| 1951 |
|
| 1952 |
if (self->telling) { |
| 1953 |
/* At the snapshot point, len(dec_buffer) bytes before the read, the |
| 1954 |
* next input to be decoded is dec_buffer + input_chunk. |
| 1955 |
*/ |
| 1956 |
PyObject *next_input = dec_buffer; |
| 1957 |
PyBytes_Concat(&next_input, input_chunk); |
| 1958 |
dec_buffer = NULL; /* Reference lost to PyBytes_Concat */ |
| 1959 |
if (next_input == NULL) { |
| 1960 |
goto fail; |
| 1961 |
} |
| 1962 |
PyObject *snapshot = Py_BuildValue("NN", dec_flags, next_input); |
| 1963 |
if (snapshot == NULL) { |
| 1964 |
dec_flags = NULL; |
| 1965 |
goto fail; |
| 1966 |
} |
| 1967 |
Py_XSETREF(self->snapshot, snapshot); |
| 1968 |
} |
| 1969 |
Py_DECREF(input_chunk); |
| 1970 |
|
| 1971 |
return (eof == 0); |
| 1972 |
|
| 1973 |
fail: |
| 1974 |
Py_XDECREF(dec_buffer); |
| 1975 |
Py_XDECREF(dec_flags); |
| 1976 |
Py_XDECREF(input_chunk); |
| 1977 |
return -1; |
| 1978 |
} |
| 1979 |
|
| 1980 |
/*[clinic input] |
| 1981 |
@critical_section |
| 1982 |
_io.TextIOWrapper.read |
| 1983 |
size as n: Py_ssize_t(accept={int, NoneType}) = -1 |
| 1984 |
/ |
| 1985 |
[clinic start generated code]*/ |
| 1986 |
|
| 1987 |
static PyObject * |
| 1988 |
_io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n) |
| 1989 |
/*[clinic end generated code: output=7e651ce6cc6a25a6 input=67d14c5661121377]*/ |
| 1990 |
{ |
| 1991 |
PyObject *result = NULL, *chunks = NULL; |
| 1992 |
|
| 1993 |
CHECK_ATTACHED(self); |
| 1994 |
CHECK_CLOSED(self); |
| 1995 |
|
| 1996 |
if (self->decoder == NULL) { |
| 1997 |
return _unsupported(self->state, "not readable"); |
| 1998 |
} |
| 1999 |
|
| 2000 |
if (_textiowrapper_writeflush(self) < 0) |
| 2001 |
return NULL; |
| 2002 |
|
| 2003 |
if (n < 0) { |
| 2004 |
/* Read everything */ |
| 2005 |
PyObject *bytes = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(read)); |
| 2006 |
PyObject *decoded; |
| 2007 |
if (bytes == NULL) |
| 2008 |
goto fail; |
| 2009 |
|
| 2010 |
if (bytes == Py_None){ |
| 2011 |
Py_DECREF(bytes); |
| 2012 |
PyErr_SetString(PyExc_BlockingIOError, "Read returned None."); |
| 2013 |
return NULL; |
| 2014 |
} |
| 2015 |
|
| 2016 |
_PyIO_State *state = self->state; |
| 2017 |
if (Py_IS_TYPE(self->decoder, state->PyIncrementalNewlineDecoder_Type)) |
| 2018 |
decoded = _PyIncrementalNewlineDecoder_decode(self->decoder, |
| 2019 |
bytes, 1); |
| 2020 |
else |
| 2021 |
decoded = PyObject_CallMethodObjArgs( |
| 2022 |
self->decoder, &_Py_ID(decode), bytes, Py_True, NULL); |
| 2023 |
Py_DECREF(bytes); |
| 2024 |
if (check_decoded(decoded) < 0) |
| 2025 |
goto fail; |
| 2026 |
|
| 2027 |
result = textiowrapper_get_decoded_chars(self, -1); |
| 2028 |
|
| 2029 |
if (result == NULL) { |
| 2030 |
Py_DECREF(decoded); |
| 2031 |
return NULL; |
| 2032 |
} |
| 2033 |
|
| 2034 |
PyUnicode_AppendAndDel(&result, decoded); |
| 2035 |
if (result == NULL) |
| 2036 |
goto fail; |
| 2037 |
|
| 2038 |
if (self->snapshot != NULL) { |
| 2039 |
textiowrapper_set_decoded_chars(self, NULL); |
| 2040 |
Py_CLEAR(self->snapshot); |
| 2041 |
} |
| 2042 |
return result; |
| 2043 |
} |
| 2044 |
else { |
| 2045 |
int res = 1; |
| 2046 |
Py_ssize_t remaining = n; |
| 2047 |
|
| 2048 |
result = textiowrapper_get_decoded_chars(self, n); |
| 2049 |
if (result == NULL) |
| 2050 |
goto fail; |
| 2051 |
remaining -= PyUnicode_GET_LENGTH(result); |
| 2052 |
|
| 2053 |
/* Keep reading chunks until we have n characters to return */ |
| 2054 |
while (remaining > 0) { |
| 2055 |
res = textiowrapper_read_chunk(self, remaining); |
| 2056 |
if (res < 0) { |
| 2057 |
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 2058 |
when EINTR occurs so we needn't do it ourselves. */ |
| 2059 |
if (_PyIO_trap_eintr()) { |
| 2060 |
continue; |
| 2061 |
} |
| 2062 |
goto fail; |
| 2063 |
} |
| 2064 |
if (res == 0) /* EOF */ |
| 2065 |
break; |
| 2066 |
if (chunks == NULL) { |
| 2067 |
chunks = PyList_New(0); |
| 2068 |
if (chunks == NULL) |
| 2069 |
goto fail; |
| 2070 |
} |
| 2071 |
if (PyUnicode_GET_LENGTH(result) > 0 && |
| 2072 |
PyList_Append(chunks, result) < 0) |
| 2073 |
goto fail; |
| 2074 |
Py_DECREF(result); |
| 2075 |
result = textiowrapper_get_decoded_chars(self, remaining); |
| 2076 |
if (result == NULL) |
| 2077 |
goto fail; |
| 2078 |
remaining -= PyUnicode_GET_LENGTH(result); |
| 2079 |
} |
| 2080 |
if (chunks != NULL) { |
| 2081 |
if (result != NULL && PyList_Append(chunks, result) < 0) |
| 2082 |
goto fail; |
| 2083 |
_Py_DECLARE_STR(empty, ""); |
| 2084 |
Py_XSETREF(result, PyUnicode_Join(&_Py_STR(empty), chunks)); |
| 2085 |
if (result == NULL) |
| 2086 |
goto fail; |
| 2087 |
Py_CLEAR(chunks); |
| 2088 |
} |
| 2089 |
return result; |
| 2090 |
} |
| 2091 |
fail: |
| 2092 |
Py_XDECREF(result); |
| 2093 |
Py_XDECREF(chunks); |
| 2094 |
return NULL; |
| 2095 |
} |
| 2096 |
|
| 2097 |
|
| 2098 |
/* NOTE: `end` must point to the real end of the Py_UCS4 storage, |
| 2099 |
that is to the NUL character. Otherwise the function will produce |
| 2100 |
incorrect results. */ |
| 2101 |
static const char * |
| 2102 |
find_control_char(int kind, const char *s, const char *end, Py_UCS4 ch) |
| 2103 |
{ |
| 2104 |
if (kind == PyUnicode_1BYTE_KIND) { |
| 2105 |
assert(ch < 256); |
| 2106 |
return (char *) memchr((const void *) s, (char) ch, end - s); |
| 2107 |
} |
| 2108 |
for (;;) { |
| 2109 |
while (PyUnicode_READ(kind, s, 0) > ch) |
| 2110 |
s += kind; |
| 2111 |
if (PyUnicode_READ(kind, s, 0) == ch) |
| 2112 |
return s; |
| 2113 |
if (s == end) |
| 2114 |
return NULL; |
| 2115 |
s += kind; |
| 2116 |
} |
| 2117 |
} |
| 2118 |
|
| 2119 |
Py_ssize_t |
| 2120 |
_PyIO_find_line_ending( |
| 2121 |
int translated, int universal, PyObject *readnl, |
| 2122 |
int kind, const char *start, const char *end, Py_ssize_t *consumed) |
| 2123 |
{ |
| 2124 |
Py_ssize_t len = (end - start)/kind; |
| 2125 |
|
| 2126 |
if (translated) { |
| 2127 |
/* Newlines are already translated, only search for \n */ |
| 2128 |
const char *pos = find_control_char(kind, start, end, '\n'); |
| 2129 |
if (pos != NULL) |
| 2130 |
return (pos - start)/kind + 1; |
| 2131 |
else { |
| 2132 |
*consumed = len; |
| 2133 |
return -1; |
| 2134 |
} |
| 2135 |
} |
| 2136 |
else if (universal) { |
| 2137 |
/* Universal newline search. Find any of \r, \r\n, \n |
| 2138 |
* The decoder ensures that \r\n are not split in two pieces |
| 2139 |
*/ |
| 2140 |
const char *s = start; |
| 2141 |
for (;;) { |
| 2142 |
Py_UCS4 ch; |
| 2143 |
/* Fast path for non-control chars. The loop always ends |
| 2144 |
since the Unicode string is NUL-terminated. */ |
| 2145 |
while (PyUnicode_READ(kind, s, 0) > '\r') |
| 2146 |
s += kind; |
| 2147 |
if (s >= end) { |
| 2148 |
*consumed = len; |
| 2149 |
return -1; |
| 2150 |
} |
| 2151 |
ch = PyUnicode_READ(kind, s, 0); |
| 2152 |
s += kind; |
| 2153 |
if (ch == '\n') |
| 2154 |
return (s - start)/kind; |
| 2155 |
if (ch == '\r') { |
| 2156 |
if (PyUnicode_READ(kind, s, 0) == '\n') |
| 2157 |
return (s - start)/kind + 1; |
| 2158 |
else |
| 2159 |
return (s - start)/kind; |
| 2160 |
} |
| 2161 |
} |
| 2162 |
} |
| 2163 |
else { |
| 2164 |
/* Non-universal mode. */ |
| 2165 |
Py_ssize_t readnl_len = PyUnicode_GET_LENGTH(readnl); |
| 2166 |
const Py_UCS1 *nl = PyUnicode_1BYTE_DATA(readnl); |
| 2167 |
/* Assume that readnl is an ASCII character. */ |
| 2168 |
assert(PyUnicode_KIND(readnl) == PyUnicode_1BYTE_KIND); |
| 2169 |
if (readnl_len == 1) { |
| 2170 |
const char *pos = find_control_char(kind, start, end, nl[0]); |
| 2171 |
if (pos != NULL) |
| 2172 |
return (pos - start)/kind + 1; |
| 2173 |
*consumed = len; |
| 2174 |
return -1; |
| 2175 |
} |
| 2176 |
else { |
| 2177 |
const char *s = start; |
| 2178 |
const char *e = end - (readnl_len - 1)*kind; |
| 2179 |
const char *pos; |
| 2180 |
if (e < s) |
| 2181 |
e = s; |
| 2182 |
while (s < e) { |
| 2183 |
Py_ssize_t i; |
| 2184 |
const char *pos = find_control_char(kind, s, end, nl[0]); |
| 2185 |
if (pos == NULL || pos >= e) |
| 2186 |
break; |
| 2187 |
for (i = 1; i < readnl_len; i++) { |
| 2188 |
if (PyUnicode_READ(kind, pos, i) != nl[i]) |
| 2189 |
break; |
| 2190 |
} |
| 2191 |
if (i == readnl_len) |
| 2192 |
return (pos - start)/kind + readnl_len; |
| 2193 |
s = pos + kind; |
| 2194 |
} |
| 2195 |
pos = find_control_char(kind, e, end, nl[0]); |
| 2196 |
if (pos == NULL) |
| 2197 |
*consumed = len; |
| 2198 |
else |
| 2199 |
*consumed = (pos - start)/kind; |
| 2200 |
return -1; |
| 2201 |
} |
| 2202 |
} |
| 2203 |
} |
| 2204 |
|
| 2205 |
static PyObject * |
| 2206 |
_textiowrapper_readline(textio *self, Py_ssize_t limit) |
| 2207 |
{ |
| 2208 |
PyObject *line = NULL, *chunks = NULL, *remaining = NULL; |
| 2209 |
Py_ssize_t start, endpos, chunked, offset_to_buffer; |
| 2210 |
int res; |
| 2211 |
|
| 2212 |
CHECK_CLOSED(self); |
| 2213 |
|
| 2214 |
if (_textiowrapper_writeflush(self) < 0) |
| 2215 |
return NULL; |
| 2216 |
|
| 2217 |
chunked = 0; |
| 2218 |
|
| 2219 |
while (1) { |
| 2220 |
const char *ptr; |
| 2221 |
Py_ssize_t line_len; |
| 2222 |
int kind; |
| 2223 |
Py_ssize_t consumed = 0; |
| 2224 |
|
| 2225 |
/* First, get some data if necessary */ |
| 2226 |
res = 1; |
| 2227 |
while (!self->decoded_chars || |
| 2228 |
!PyUnicode_GET_LENGTH(self->decoded_chars)) { |
| 2229 |
res = textiowrapper_read_chunk(self, 0); |
| 2230 |
if (res < 0) { |
| 2231 |
/* NOTE: PyErr_SetFromErrno() calls PyErr_CheckSignals() |
| 2232 |
when EINTR occurs so we needn't do it ourselves. */ |
| 2233 |
if (_PyIO_trap_eintr()) { |
| 2234 |
continue; |
| 2235 |
} |
| 2236 |
goto error; |
| 2237 |
} |
| 2238 |
if (res == 0) |
| 2239 |
break; |
| 2240 |
} |
| 2241 |
if (res == 0) { |
| 2242 |
/* end of file */ |
| 2243 |
textiowrapper_set_decoded_chars(self, NULL); |
| 2244 |
Py_CLEAR(self->snapshot); |
| 2245 |
start = endpos = offset_to_buffer = 0; |
| 2246 |
break; |
| 2247 |
} |
| 2248 |
|
| 2249 |
if (remaining == NULL) { |
| 2250 |
line = Py_NewRef(self->decoded_chars); |
| 2251 |
start = self->decoded_chars_used; |
| 2252 |
offset_to_buffer = 0; |
| 2253 |
} |
| 2254 |
else { |
| 2255 |
assert(self->decoded_chars_used == 0); |
| 2256 |
line = PyUnicode_Concat(remaining, self->decoded_chars); |
| 2257 |
start = 0; |
| 2258 |
offset_to_buffer = PyUnicode_GET_LENGTH(remaining); |
| 2259 |
Py_CLEAR(remaining); |
| 2260 |
if (line == NULL) |
| 2261 |
goto error; |
| 2262 |
} |
| 2263 |
|
| 2264 |
ptr = PyUnicode_DATA(line); |
| 2265 |
line_len = PyUnicode_GET_LENGTH(line); |
| 2266 |
kind = PyUnicode_KIND(line); |
| 2267 |
|
| 2268 |
endpos = _PyIO_find_line_ending( |
| 2269 |
self->readtranslate, self->readuniversal, self->readnl, |
| 2270 |
kind, |
| 2271 |
ptr + kind * start, |
| 2272 |
ptr + kind * line_len, |
| 2273 |
&consumed); |
| 2274 |
if (endpos >= 0) { |
| 2275 |
endpos += start; |
| 2276 |
if (limit >= 0 && (endpos - start) + chunked >= limit) |
| 2277 |
endpos = start + limit - chunked; |
| 2278 |
break; |
| 2279 |
} |
| 2280 |
|
| 2281 |
/* We can put aside up to `endpos` */ |
| 2282 |
endpos = consumed + start; |
| 2283 |
if (limit >= 0 && (endpos - start) + chunked >= limit) { |
| 2284 |
/* Didn't find line ending, but reached length limit */ |
| 2285 |
endpos = start + limit - chunked; |
| 2286 |
break; |
| 2287 |
} |
| 2288 |
|
| 2289 |
if (endpos > start) { |
| 2290 |
/* No line ending seen yet - put aside current data */ |
| 2291 |
PyObject *s; |
| 2292 |
if (chunks == NULL) { |
| 2293 |
chunks = PyList_New(0); |
| 2294 |
if (chunks == NULL) |
| 2295 |
goto error; |
| 2296 |
} |
| 2297 |
s = PyUnicode_Substring(line, start, endpos); |
| 2298 |
if (s == NULL) |
| 2299 |
goto error; |
| 2300 |
if (PyList_Append(chunks, s) < 0) { |
| 2301 |
Py_DECREF(s); |
| 2302 |
goto error; |
| 2303 |
} |
| 2304 |
chunked += PyUnicode_GET_LENGTH(s); |
| 2305 |
Py_DECREF(s); |
| 2306 |
} |
| 2307 |
/* There may be some remaining bytes we'll have to prepend to the |
| 2308 |
next chunk of data */ |
| 2309 |
if (endpos < line_len) { |
| 2310 |
remaining = PyUnicode_Substring(line, endpos, line_len); |
| 2311 |
if (remaining == NULL) |
| 2312 |
goto error; |
| 2313 |
} |
| 2314 |
Py_CLEAR(line); |
| 2315 |
/* We have consumed the buffer */ |
| 2316 |
textiowrapper_set_decoded_chars(self, NULL); |
| 2317 |
} |
| 2318 |
|
| 2319 |
if (line != NULL) { |
| 2320 |
/* Our line ends in the current buffer */ |
| 2321 |
self->decoded_chars_used = endpos - offset_to_buffer; |
| 2322 |
if (start > 0 || endpos < PyUnicode_GET_LENGTH(line)) { |
| 2323 |
PyObject *s = PyUnicode_Substring(line, start, endpos); |
| 2324 |
Py_CLEAR(line); |
| 2325 |
if (s == NULL) |
| 2326 |
goto error; |
| 2327 |
line = s; |
| 2328 |
} |
| 2329 |
} |
| 2330 |
if (remaining != NULL) { |
| 2331 |
if (chunks == NULL) { |
| 2332 |
chunks = PyList_New(0); |
| 2333 |
if (chunks == NULL) |
| 2334 |
goto error; |
| 2335 |
} |
| 2336 |
if (PyList_Append(chunks, remaining) < 0) |
| 2337 |
goto error; |
| 2338 |
Py_CLEAR(remaining); |
| 2339 |
} |
| 2340 |
if (chunks != NULL) { |
| 2341 |
if (line != NULL) { |
| 2342 |
if (PyList_Append(chunks, line) < 0) |
| 2343 |
goto error; |
| 2344 |
Py_DECREF(line); |
| 2345 |
} |
| 2346 |
line = PyUnicode_Join(&_Py_STR(empty), chunks); |
| 2347 |
if (line == NULL) |
| 2348 |
goto error; |
| 2349 |
Py_CLEAR(chunks); |
| 2350 |
} |
| 2351 |
if (line == NULL) { |
| 2352 |
line = &_Py_STR(empty); |
| 2353 |
} |
| 2354 |
|
| 2355 |
return line; |
| 2356 |
|
| 2357 |
error: |
| 2358 |
Py_XDECREF(chunks); |
| 2359 |
Py_XDECREF(remaining); |
| 2360 |
Py_XDECREF(line); |
| 2361 |
return NULL; |
| 2362 |
} |
| 2363 |
|
| 2364 |
/*[clinic input] |
| 2365 |
@critical_section |
| 2366 |
_io.TextIOWrapper.readline |
| 2367 |
size: Py_ssize_t = -1 |
| 2368 |
/ |
| 2369 |
[clinic start generated code]*/ |
| 2370 |
|
| 2371 |
static PyObject * |
| 2372 |
_io_TextIOWrapper_readline_impl(textio *self, Py_ssize_t size) |
| 2373 |
/*[clinic end generated code: output=344afa98804e8b25 input=b65bab871dc3ddba]*/ |
| 2374 |
{ |
| 2375 |
CHECK_ATTACHED(self); |
| 2376 |
return _textiowrapper_readline(self, size); |
| 2377 |
} |
| 2378 |
|
| 2379 |
/* Seek and Tell */ |
| 2380 |
|
| 2381 |
typedef struct { |
| 2382 |
Py_off_t start_pos; |
| 2383 |
int dec_flags; |
| 2384 |
int bytes_to_feed; |
| 2385 |
int chars_to_skip; |
| 2386 |
char need_eof; |
| 2387 |
} cookie_type; |
| 2388 |
|
| 2389 |
/* |
| 2390 |
To speed up cookie packing/unpacking, we store the fields in a temporary |
| 2391 |
string and call _PyLong_FromByteArray() or _PyLong_AsByteArray (resp.). |
| 2392 |
The following macros define at which offsets in the intermediary byte |
| 2393 |
string the various CookieStruct fields will be stored. |
| 2394 |
*/ |
| 2395 |
|
| 2396 |
#define COOKIE_BUF_LEN (sizeof(Py_off_t) + 3 * sizeof(int) + sizeof(char)) |
| 2397 |
|
| 2398 |
#if PY_BIG_ENDIAN |
| 2399 |
/* We want the least significant byte of start_pos to also be the least |
| 2400 |
significant byte of the cookie, which means that in big-endian mode we |
| 2401 |
must copy the fields in reverse order. */ |
| 2402 |
|
| 2403 |
# define OFF_START_POS (sizeof(char) + 3 * sizeof(int)) |
| 2404 |
# define OFF_DEC_FLAGS (sizeof(char) + 2 * sizeof(int)) |
| 2405 |
# define OFF_BYTES_TO_FEED (sizeof(char) + sizeof(int)) |
| 2406 |
# define OFF_CHARS_TO_SKIP (sizeof(char)) |
| 2407 |
# define OFF_NEED_EOF 0 |
| 2408 |
|
| 2409 |
#else |
| 2410 |
/* Little-endian mode: the least significant byte of start_pos will |
| 2411 |
naturally end up the least significant byte of the cookie. */ |
| 2412 |
|
| 2413 |
# define OFF_START_POS 0 |
| 2414 |
# define OFF_DEC_FLAGS (sizeof(Py_off_t)) |
| 2415 |
# define OFF_BYTES_TO_FEED (sizeof(Py_off_t) + sizeof(int)) |
| 2416 |
# define OFF_CHARS_TO_SKIP (sizeof(Py_off_t) + 2 * sizeof(int)) |
| 2417 |
# define OFF_NEED_EOF (sizeof(Py_off_t) + 3 * sizeof(int)) |
| 2418 |
|
| 2419 |
#endif |
| 2420 |
|
| 2421 |
static int |
| 2422 |
textiowrapper_parse_cookie(cookie_type *cookie, PyObject *cookieObj) |
| 2423 |
{ |
| 2424 |
unsigned char buffer[COOKIE_BUF_LEN]; |
| 2425 |
PyLongObject *cookieLong = (PyLongObject *)PyNumber_Long(cookieObj); |
| 2426 |
if (cookieLong == NULL) |
| 2427 |
return -1; |
| 2428 |
|
| 2429 |
if (_PyLong_AsByteArray(cookieLong, buffer, sizeof(buffer), |
| 2430 |
PY_LITTLE_ENDIAN, 0, 1) < 0) { |
| 2431 |
Py_DECREF(cookieLong); |
| 2432 |
return -1; |
| 2433 |
} |
| 2434 |
Py_DECREF(cookieLong); |
| 2435 |
|
| 2436 |
memcpy(&cookie->start_pos, buffer + OFF_START_POS, sizeof(cookie->start_pos)); |
| 2437 |
memcpy(&cookie->dec_flags, buffer + OFF_DEC_FLAGS, sizeof(cookie->dec_flags)); |
| 2438 |
memcpy(&cookie->bytes_to_feed, buffer + OFF_BYTES_TO_FEED, sizeof(cookie->bytes_to_feed)); |
| 2439 |
memcpy(&cookie->chars_to_skip, buffer + OFF_CHARS_TO_SKIP, sizeof(cookie->chars_to_skip)); |
| 2440 |
memcpy(&cookie->need_eof, buffer + OFF_NEED_EOF, sizeof(cookie->need_eof)); |
| 2441 |
|
| 2442 |
return 0; |
| 2443 |
} |
| 2444 |
|
| 2445 |
static PyObject * |
| 2446 |
textiowrapper_build_cookie(cookie_type *cookie) |
| 2447 |
{ |
| 2448 |
unsigned char buffer[COOKIE_BUF_LEN]; |
| 2449 |
|
| 2450 |
memcpy(buffer + OFF_START_POS, &cookie->start_pos, sizeof(cookie->start_pos)); |
| 2451 |
memcpy(buffer + OFF_DEC_FLAGS, &cookie->dec_flags, sizeof(cookie->dec_flags)); |
| 2452 |
memcpy(buffer + OFF_BYTES_TO_FEED, &cookie->bytes_to_feed, sizeof(cookie->bytes_to_feed)); |
| 2453 |
memcpy(buffer + OFF_CHARS_TO_SKIP, &cookie->chars_to_skip, sizeof(cookie->chars_to_skip)); |
| 2454 |
memcpy(buffer + OFF_NEED_EOF, &cookie->need_eof, sizeof(cookie->need_eof)); |
| 2455 |
|
| 2456 |
return _PyLong_FromByteArray(buffer, sizeof(buffer), |
| 2457 |
PY_LITTLE_ENDIAN, 0); |
| 2458 |
} |
| 2459 |
|
| 2460 |
static int |
| 2461 |
_textiowrapper_decoder_setstate(textio *self, cookie_type *cookie) |
| 2462 |
{ |
| 2463 |
PyObject *res; |
| 2464 |
/* When seeking to the start of the stream, we call decoder.reset() |
| 2465 |
rather than decoder.getstate(). |
| 2466 |
This is for a few decoders such as utf-16 for which the state value |
| 2467 |
at start is not (b"", 0) but e.g. (b"", 2) (meaning, in the case of |
| 2468 |
utf-16, that we are expecting a BOM). |
| 2469 |
*/ |
| 2470 |
if (cookie->start_pos == 0 && cookie->dec_flags == 0) { |
| 2471 |
res = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); |
| 2472 |
} |
| 2473 |
else { |
| 2474 |
res = _PyObject_CallMethod(self->decoder, &_Py_ID(setstate), |
| 2475 |
"((yi))", "", cookie->dec_flags); |
| 2476 |
} |
| 2477 |
if (res == NULL) { |
| 2478 |
return -1; |
| 2479 |
} |
| 2480 |
Py_DECREF(res); |
| 2481 |
return 0; |
| 2482 |
} |
| 2483 |
|
| 2484 |
static int |
| 2485 |
_textiowrapper_encoder_reset(textio *self, int start_of_stream) |
| 2486 |
{ |
| 2487 |
PyObject *res; |
| 2488 |
if (start_of_stream) { |
| 2489 |
res = PyObject_CallMethodNoArgs(self->encoder, &_Py_ID(reset)); |
| 2490 |
self->encoding_start_of_stream = 1; |
| 2491 |
} |
| 2492 |
else { |
| 2493 |
res = PyObject_CallMethodOneArg(self->encoder, &_Py_ID(setstate), |
| 2494 |
_PyLong_GetZero()); |
| 2495 |
self->encoding_start_of_stream = 0; |
| 2496 |
} |
| 2497 |
if (res == NULL) |
| 2498 |
return -1; |
| 2499 |
Py_DECREF(res); |
| 2500 |
return 0; |
| 2501 |
} |
| 2502 |
|
| 2503 |
static int |
| 2504 |
_textiowrapper_encoder_setstate(textio *self, cookie_type *cookie) |
| 2505 |
{ |
| 2506 |
/* Same as _textiowrapper_decoder_setstate() above. */ |
| 2507 |
return _textiowrapper_encoder_reset( |
| 2508 |
self, cookie->start_pos == 0 && cookie->dec_flags == 0); |
| 2509 |
} |
| 2510 |
|
| 2511 |
/*[clinic input] |
| 2512 |
@critical_section |
| 2513 |
_io.TextIOWrapper.seek |
| 2514 |
cookie as cookieObj: object |
| 2515 |
Zero or an opaque number returned by tell(). |
| 2516 |
whence: int(c_default='0') = os.SEEK_SET |
| 2517 |
The relative position to seek from. |
| 2518 |
/ |
| 2519 |
|
| 2520 |
Set the stream position, and return the new stream position. |
| 2521 |
|
| 2522 |
Four operations are supported, given by the following argument |
| 2523 |
combinations: |
| 2524 |
|
| 2525 |
- seek(0, SEEK_SET): Rewind to the start of the stream. |
| 2526 |
- seek(cookie, SEEK_SET): Restore a previous position; |
| 2527 |
'cookie' must be a number returned by tell(). |
| 2528 |
- seek(0, SEEK_END): Fast-forward to the end of the stream. |
| 2529 |
- seek(0, SEEK_CUR): Leave the current stream position unchanged. |
| 2530 |
|
| 2531 |
Any other argument combinations are invalid, |
| 2532 |
and may raise exceptions. |
| 2533 |
[clinic start generated code]*/ |
| 2534 |
|
| 2535 |
static PyObject * |
| 2536 |
_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence) |
| 2537 |
/*[clinic end generated code: output=0a15679764e2d04d input=4bea78698be23d7e]*/ |
| 2538 |
{ |
| 2539 |
PyObject *posobj; |
| 2540 |
cookie_type cookie; |
| 2541 |
PyObject *res; |
| 2542 |
int cmp; |
| 2543 |
PyObject *snapshot; |
| 2544 |
|
| 2545 |
CHECK_ATTACHED(self); |
| 2546 |
CHECK_CLOSED(self); |
| 2547 |
|
| 2548 |
Py_INCREF(cookieObj); |
| 2549 |
|
| 2550 |
if (!self->seekable) { |
| 2551 |
_unsupported(self->state, "underlying stream is not seekable"); |
| 2552 |
goto fail; |
| 2553 |
} |
| 2554 |
|
| 2555 |
PyObject *zero = _PyLong_GetZero(); // borrowed reference |
| 2556 |
|
| 2557 |
switch (whence) { |
| 2558 |
case SEEK_CUR: |
| 2559 |
/* seek relative to current position */ |
| 2560 |
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ); |
| 2561 |
if (cmp < 0) |
| 2562 |
goto fail; |
| 2563 |
|
| 2564 |
if (cmp == 0) { |
| 2565 |
_unsupported(self->state, "can't do nonzero cur-relative seeks"); |
| 2566 |
goto fail; |
| 2567 |
} |
| 2568 |
|
| 2569 |
/* Seeking to the current position should attempt to |
| 2570 |
* sync the underlying buffer with the current position. |
| 2571 |
*/ |
| 2572 |
Py_DECREF(cookieObj); |
| 2573 |
cookieObj = PyObject_CallMethodNoArgs((PyObject *)self, &_Py_ID(tell)); |
| 2574 |
if (cookieObj == NULL) |
| 2575 |
goto fail; |
| 2576 |
break; |
| 2577 |
|
| 2578 |
case SEEK_END: |
| 2579 |
/* seek relative to end of file */ |
| 2580 |
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_EQ); |
| 2581 |
if (cmp < 0) |
| 2582 |
goto fail; |
| 2583 |
|
| 2584 |
if (cmp == 0) { |
| 2585 |
_unsupported(self->state, "can't do nonzero end-relative seeks"); |
| 2586 |
goto fail; |
| 2587 |
} |
| 2588 |
|
| 2589 |
if (_PyFile_Flush((PyObject *)self) < 0) { |
| 2590 |
goto fail; |
| 2591 |
} |
| 2592 |
|
| 2593 |
textiowrapper_set_decoded_chars(self, NULL); |
| 2594 |
Py_CLEAR(self->snapshot); |
| 2595 |
if (self->decoder) { |
| 2596 |
res = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(reset)); |
| 2597 |
if (res == NULL) |
| 2598 |
goto fail; |
| 2599 |
Py_DECREF(res); |
| 2600 |
} |
| 2601 |
|
| 2602 |
res = _PyObject_CallMethod(self->buffer, &_Py_ID(seek), "ii", 0, 2); |
| 2603 |
Py_CLEAR(cookieObj); |
| 2604 |
if (res == NULL) |
| 2605 |
goto fail; |
| 2606 |
if (self->encoder) { |
| 2607 |
/* If seek() == 0, we are at the start of stream, otherwise not */ |
| 2608 |
cmp = PyObject_RichCompareBool(res, zero, Py_EQ); |
| 2609 |
if (cmp < 0 || _textiowrapper_encoder_reset(self, cmp)) { |
| 2610 |
Py_DECREF(res); |
| 2611 |
goto fail; |
| 2612 |
} |
| 2613 |
} |
| 2614 |
return res; |
| 2615 |
|
| 2616 |
case SEEK_SET: |
| 2617 |
break; |
| 2618 |
|
| 2619 |
default: |
| 2620 |
PyErr_Format(PyExc_ValueError, |
| 2621 |
"invalid whence (%d, should be %d, %d or %d)", whence, |
| 2622 |
SEEK_SET, SEEK_CUR, SEEK_END); |
| 2623 |
goto fail; |
| 2624 |
} |
| 2625 |
|
| 2626 |
cmp = PyObject_RichCompareBool(cookieObj, zero, Py_LT); |
| 2627 |
if (cmp < 0) |
| 2628 |
goto fail; |
| 2629 |
|
| 2630 |
if (cmp == 1) { |
| 2631 |
PyErr_Format(PyExc_ValueError, |
| 2632 |
"negative seek position %R", cookieObj); |
| 2633 |
goto fail; |
| 2634 |
} |
| 2635 |
|
| 2636 |
if (_PyFile_Flush((PyObject *)self) < 0) { |
| 2637 |
goto fail; |
| 2638 |
} |
| 2639 |
|
| 2640 |
/* The strategy of seek() is to go back to the safe start point |
| 2641 |
* and replay the effect of read(chars_to_skip) from there. |
| 2642 |
*/ |
| 2643 |
if (textiowrapper_parse_cookie(&cookie, cookieObj) < 0) |
| 2644 |
goto fail; |
| 2645 |
|
| 2646 |
/* Seek back to the safe start point. */ |
| 2647 |
posobj = PyLong_FromOff_t(cookie.start_pos); |
| 2648 |
if (posobj == NULL) |
| 2649 |
goto fail; |
| 2650 |
res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(seek), posobj); |
| 2651 |
Py_DECREF(posobj); |
| 2652 |
if (res == NULL) |
| 2653 |
goto fail; |
| 2654 |
Py_DECREF(res); |
| 2655 |
|
| 2656 |
textiowrapper_set_decoded_chars(self, NULL); |
| 2657 |
Py_CLEAR(self->snapshot); |
| 2658 |
|
| 2659 |
/* Restore the decoder to its state from the safe start point. */ |
| 2660 |
if (self->decoder) { |
| 2661 |
if (_textiowrapper_decoder_setstate(self, &cookie) < 0) |
| 2662 |
goto fail; |
| 2663 |
} |
| 2664 |
|
| 2665 |
if (cookie.chars_to_skip) { |
| 2666 |
/* Just like _read_chunk, feed the decoder and save a snapshot. */ |
| 2667 |
PyObject *input_chunk = _PyObject_CallMethod(self->buffer, &_Py_ID(read), |
| 2668 |
"i", cookie.bytes_to_feed); |
| 2669 |
PyObject *decoded; |
| 2670 |
|
| 2671 |
if (input_chunk == NULL) |
| 2672 |
goto fail; |
| 2673 |
|
| 2674 |
if (!PyBytes_Check(input_chunk)) { |
| 2675 |
PyErr_Format(PyExc_TypeError, |
| 2676 |
"underlying read() should have returned a bytes " |
| 2677 |
"object, not '%.200s'", |
| 2678 |
Py_TYPE(input_chunk)->tp_name); |
| 2679 |
Py_DECREF(input_chunk); |
| 2680 |
goto fail; |
| 2681 |
} |
| 2682 |
|
| 2683 |
snapshot = Py_BuildValue("iN", cookie.dec_flags, input_chunk); |
| 2684 |
if (snapshot == NULL) { |
| 2685 |
goto fail; |
| 2686 |
} |
| 2687 |
Py_XSETREF(self->snapshot, snapshot); |
| 2688 |
|
| 2689 |
decoded = PyObject_CallMethodObjArgs(self->decoder, &_Py_ID(decode), |
| 2690 |
input_chunk, cookie.need_eof ? Py_True : Py_False, NULL); |
| 2691 |
|
| 2692 |
if (check_decoded(decoded) < 0) |
| 2693 |
goto fail; |
| 2694 |
|
| 2695 |
textiowrapper_set_decoded_chars(self, decoded); |
| 2696 |
|
| 2697 |
/* Skip chars_to_skip of the decoded characters. */ |
| 2698 |
if (PyUnicode_GetLength(self->decoded_chars) < cookie.chars_to_skip) { |
| 2699 |
PyErr_SetString(PyExc_OSError, "can't restore logical file position"); |
| 2700 |
goto fail; |
| 2701 |
} |
| 2702 |
self->decoded_chars_used = cookie.chars_to_skip; |
| 2703 |
} |
| 2704 |
else { |
| 2705 |
snapshot = Py_BuildValue("iy", cookie.dec_flags, ""); |
| 2706 |
if (snapshot == NULL) |
| 2707 |
goto fail; |
| 2708 |
Py_XSETREF(self->snapshot, snapshot); |
| 2709 |
} |
| 2710 |
|
| 2711 |
/* Finally, reset the encoder (merely useful for proper BOM handling) */ |
| 2712 |
if (self->encoder) { |
| 2713 |
if (_textiowrapper_encoder_setstate(self, &cookie) < 0) |
| 2714 |
goto fail; |
| 2715 |
} |
| 2716 |
return cookieObj; |
| 2717 |
fail: |
| 2718 |
Py_XDECREF(cookieObj); |
| 2719 |
return NULL; |
| 2720 |
|
| 2721 |
} |
| 2722 |
|
| 2723 |
/*[clinic input] |
| 2724 |
@critical_section |
| 2725 |
_io.TextIOWrapper.tell |
| 2726 |
|
| 2727 |
Return the stream position as an opaque number. |
| 2728 |
|
| 2729 |
The return value of tell() can be given as input to seek(), to restore a |
| 2730 |
previous stream position. |
| 2731 |
[clinic start generated code]*/ |
| 2732 |
|
| 2733 |
static PyObject * |
| 2734 |
_io_TextIOWrapper_tell_impl(textio *self) |
| 2735 |
/*[clinic end generated code: output=4f168c08bf34ad5f input=415d6b4e4f8e6e8c]*/ |
| 2736 |
{ |
| 2737 |
PyObject *res; |
| 2738 |
PyObject *posobj = NULL; |
| 2739 |
cookie_type cookie = {0,0,0,0,0}; |
| 2740 |
PyObject *next_input; |
| 2741 |
Py_ssize_t chars_to_skip, chars_decoded; |
| 2742 |
Py_ssize_t skip_bytes, skip_back; |
| 2743 |
PyObject *saved_state = NULL; |
| 2744 |
const char *input, *input_end; |
| 2745 |
Py_ssize_t dec_buffer_len; |
| 2746 |
int dec_flags; |
| 2747 |
|
| 2748 |
CHECK_ATTACHED(self); |
| 2749 |
CHECK_CLOSED(self); |
| 2750 |
|
| 2751 |
if (!self->seekable) { |
| 2752 |
_unsupported(self->state, "underlying stream is not seekable"); |
| 2753 |
goto fail; |
| 2754 |
} |
| 2755 |
if (!self->telling) { |
| 2756 |
PyErr_SetString(PyExc_OSError, |
| 2757 |
"telling position disabled by next() call"); |
| 2758 |
goto fail; |
| 2759 |
} |
| 2760 |
|
| 2761 |
if (_textiowrapper_writeflush(self) < 0) |
| 2762 |
return NULL; |
| 2763 |
if (_PyFile_Flush((PyObject *)self) < 0) { |
| 2764 |
goto fail; |
| 2765 |
} |
| 2766 |
|
| 2767 |
posobj = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(tell)); |
| 2768 |
if (posobj == NULL) |
| 2769 |
goto fail; |
| 2770 |
|
| 2771 |
if (self->decoder == NULL || self->snapshot == NULL) { |
| 2772 |
assert (self->decoded_chars == NULL || PyUnicode_GetLength(self->decoded_chars) == 0); |
| 2773 |
return posobj; |
| 2774 |
} |
| 2775 |
|
| 2776 |
#if defined(HAVE_LARGEFILE_SUPPORT) |
| 2777 |
cookie.start_pos = PyLong_AsLongLong(posobj); |
| 2778 |
#else |
| 2779 |
cookie.start_pos = PyLong_AsLong(posobj); |
| 2780 |
#endif |
| 2781 |
Py_DECREF(posobj); |
| 2782 |
if (PyErr_Occurred()) |
| 2783 |
goto fail; |
| 2784 |
|
| 2785 |
/* Skip backward to the snapshot point (see _read_chunk). */ |
| 2786 |
assert(PyTuple_Check(self->snapshot)); |
| 2787 |
if (!PyArg_ParseTuple(self->snapshot, "iO", &cookie.dec_flags, &next_input)) |
| 2788 |
goto fail; |
| 2789 |
|
| 2790 |
assert (PyBytes_Check(next_input)); |
| 2791 |
|
| 2792 |
cookie.start_pos -= PyBytes_GET_SIZE(next_input); |
| 2793 |
|
| 2794 |
/* How many decoded characters have been used up since the snapshot? */ |
| 2795 |
if (self->decoded_chars_used == 0) { |
| 2796 |
/* We haven't moved from the snapshot point. */ |
| 2797 |
return textiowrapper_build_cookie(&cookie); |
| 2798 |
} |
| 2799 |
|
| 2800 |
chars_to_skip = self->decoded_chars_used; |
| 2801 |
|
| 2802 |
/* Decoder state will be restored at the end */ |
| 2803 |
saved_state = PyObject_CallMethodNoArgs(self->decoder, |
| 2804 |
&_Py_ID(getstate)); |
| 2805 |
if (saved_state == NULL) |
| 2806 |
goto fail; |
| 2807 |
|
| 2808 |
#define DECODER_GETSTATE() do { \ |
| 2809 |
PyObject *dec_buffer; \ |
| 2810 |
PyObject *_state = PyObject_CallMethodNoArgs(self->decoder, \ |
| 2811 |
&_Py_ID(getstate)); \ |
| 2812 |
if (_state == NULL) \ |
| 2813 |
goto fail; \ |
| 2814 |
if (!PyTuple_Check(_state)) { \ |
| 2815 |
PyErr_SetString(PyExc_TypeError, \ |
| 2816 |
"illegal decoder state"); \ |
| 2817 |
Py_DECREF(_state); \ |
| 2818 |
goto fail; \ |
| 2819 |
} \ |
| 2820 |
if (!PyArg_ParseTuple(_state, "Oi;illegal decoder state", \ |
| 2821 |
&dec_buffer, &dec_flags)) \ |
| 2822 |
{ \ |
| 2823 |
Py_DECREF(_state); \ |
| 2824 |
goto fail; \ |
| 2825 |
} \ |
| 2826 |
if (!PyBytes_Check(dec_buffer)) { \ |
| 2827 |
PyErr_Format(PyExc_TypeError, \ |
| 2828 |
"illegal decoder state: the first item should be a " \ |
| 2829 |
"bytes object, not '%.200s'", \ |
| 2830 |
Py_TYPE(dec_buffer)->tp_name); \ |
| 2831 |
Py_DECREF(_state); \ |
| 2832 |
goto fail; \ |
| 2833 |
} \ |
| 2834 |
dec_buffer_len = PyBytes_GET_SIZE(dec_buffer); \ |
| 2835 |
Py_DECREF(_state); \ |
| 2836 |
} while (0) |
| 2837 |
|
| 2838 |
#define DECODER_DECODE(start, len, res) do { \ |
| 2839 |
PyObject *_decoded = _PyObject_CallMethod( \ |
| 2840 |
self->decoder, &_Py_ID(decode), "y#", start, len); \ |
| 2841 |
if (check_decoded(_decoded) < 0) \ |
| 2842 |
goto fail; \ |
| 2843 |
res = PyUnicode_GET_LENGTH(_decoded); \ |
| 2844 |
Py_DECREF(_decoded); \ |
| 2845 |
} while (0) |
| 2846 |
|
| 2847 |
/* Fast search for an acceptable start point, close to our |
| 2848 |
current pos */ |
| 2849 |
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip); |
| 2850 |
skip_back = 1; |
| 2851 |
assert(skip_bytes <= PyBytes_GET_SIZE(next_input)); |
| 2852 |
input = PyBytes_AS_STRING(next_input); |
| 2853 |
while (skip_bytes > 0) { |
| 2854 |
/* Decode up to temptative start point */ |
| 2855 |
if (_textiowrapper_decoder_setstate(self, &cookie) < 0) |
| 2856 |
goto fail; |
| 2857 |
DECODER_DECODE(input, skip_bytes, chars_decoded); |
| 2858 |
if (chars_decoded <= chars_to_skip) { |
| 2859 |
DECODER_GETSTATE(); |
| 2860 |
if (dec_buffer_len == 0) { |
| 2861 |
/* Before pos and no bytes buffered in decoder => OK */ |
| 2862 |
cookie.dec_flags = dec_flags; |
| 2863 |
chars_to_skip -= chars_decoded; |
| 2864 |
break; |
| 2865 |
} |
| 2866 |
/* Skip back by buffered amount and reset heuristic */ |
| 2867 |
skip_bytes -= dec_buffer_len; |
| 2868 |
skip_back = 1; |
| 2869 |
} |
| 2870 |
else { |
| 2871 |
/* We're too far ahead, skip back a bit */ |
| 2872 |
skip_bytes -= skip_back; |
| 2873 |
skip_back *= 2; |
| 2874 |
} |
| 2875 |
} |
| 2876 |
if (skip_bytes <= 0) { |
| 2877 |
skip_bytes = 0; |
| 2878 |
if (_textiowrapper_decoder_setstate(self, &cookie) < 0) |
| 2879 |
goto fail; |
| 2880 |
} |
| 2881 |
|
| 2882 |
/* Note our initial start point. */ |
| 2883 |
cookie.start_pos += skip_bytes; |
| 2884 |
cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int); |
| 2885 |
if (chars_to_skip == 0) |
| 2886 |
goto finally; |
| 2887 |
|
| 2888 |
/* We should be close to the desired position. Now feed the decoder one |
| 2889 |
* byte at a time until we reach the `chars_to_skip` target. |
| 2890 |
* As we go, note the nearest "safe start point" before the current |
| 2891 |
* location (a point where the decoder has nothing buffered, so seek() |
| 2892 |
* can safely start from there and advance to this location). |
| 2893 |
*/ |
| 2894 |
chars_decoded = 0; |
| 2895 |
input = PyBytes_AS_STRING(next_input); |
| 2896 |
input_end = input + PyBytes_GET_SIZE(next_input); |
| 2897 |
input += skip_bytes; |
| 2898 |
while (input < input_end) { |
| 2899 |
Py_ssize_t n; |
| 2900 |
|
| 2901 |
DECODER_DECODE(input, (Py_ssize_t)1, n); |
| 2902 |
/* We got n chars for 1 byte */ |
| 2903 |
chars_decoded += n; |
| 2904 |
cookie.bytes_to_feed += 1; |
| 2905 |
DECODER_GETSTATE(); |
| 2906 |
|
| 2907 |
if (dec_buffer_len == 0 && chars_decoded <= chars_to_skip) { |
| 2908 |
/* Decoder buffer is empty, so this is a safe start point. */ |
| 2909 |
cookie.start_pos += cookie.bytes_to_feed; |
| 2910 |
chars_to_skip -= chars_decoded; |
| 2911 |
cookie.dec_flags = dec_flags; |
| 2912 |
cookie.bytes_to_feed = 0; |
| 2913 |
chars_decoded = 0; |
| 2914 |
} |
| 2915 |
if (chars_decoded >= chars_to_skip) |
| 2916 |
break; |
| 2917 |
input++; |
| 2918 |
} |
| 2919 |
if (input == input_end) { |
| 2920 |
/* We didn't get enough decoded data; signal EOF to get more. */ |
| 2921 |
PyObject *decoded = _PyObject_CallMethod( |
| 2922 |
self->decoder, &_Py_ID(decode), "yO", "", /* final = */ Py_True); |
| 2923 |
if (check_decoded(decoded) < 0) |
| 2924 |
goto fail; |
| 2925 |
chars_decoded += PyUnicode_GET_LENGTH(decoded); |
| 2926 |
Py_DECREF(decoded); |
| 2927 |
cookie.need_eof = 1; |
| 2928 |
|
| 2929 |
if (chars_decoded < chars_to_skip) { |
| 2930 |
PyErr_SetString(PyExc_OSError, |
| 2931 |
"can't reconstruct logical file position"); |
| 2932 |
goto fail; |
| 2933 |
} |
| 2934 |
} |
| 2935 |
|
| 2936 |
finally: |
| 2937 |
res = PyObject_CallMethodOneArg( |
| 2938 |
self->decoder, &_Py_ID(setstate), saved_state); |
| 2939 |
Py_DECREF(saved_state); |
| 2940 |
if (res == NULL) |
| 2941 |
return NULL; |
| 2942 |
Py_DECREF(res); |
| 2943 |
|
| 2944 |
/* The returned cookie corresponds to the last safe start point. */ |
| 2945 |
cookie.chars_to_skip = Py_SAFE_DOWNCAST(chars_to_skip, Py_ssize_t, int); |
| 2946 |
return textiowrapper_build_cookie(&cookie); |
| 2947 |
|
| 2948 |
fail: |
| 2949 |
if (saved_state) { |
| 2950 |
PyObject *exc = PyErr_GetRaisedException(); |
| 2951 |
res = PyObject_CallMethodOneArg( |
| 2952 |
self->decoder, &_Py_ID(setstate), saved_state); |
| 2953 |
_PyErr_ChainExceptions1(exc); |
| 2954 |
Py_DECREF(saved_state); |
| 2955 |
Py_XDECREF(res); |
| 2956 |
} |
| 2957 |
return NULL; |
| 2958 |
} |
| 2959 |
|
| 2960 |
/*[clinic input] |
| 2961 |
@critical_section |
| 2962 |
_io.TextIOWrapper.truncate |
| 2963 |
pos: object = None |
| 2964 |
/ |
| 2965 |
[clinic start generated code]*/ |
| 2966 |
|
| 2967 |
static PyObject * |
| 2968 |
_io_TextIOWrapper_truncate_impl(textio *self, PyObject *pos) |
| 2969 |
/*[clinic end generated code: output=90ec2afb9bb7745f input=8bddb320834c93ee]*/ |
| 2970 |
{ |
| 2971 |
CHECK_ATTACHED(self) |
| 2972 |
|
| 2973 |
if (_PyFile_Flush((PyObject *)self) < 0) { |
| 2974 |
return NULL; |
| 2975 |
} |
| 2976 |
|
| 2977 |
return PyObject_CallMethodOneArg(self->buffer, &_Py_ID(truncate), pos); |
| 2978 |
} |
| 2979 |
|
| 2980 |
static PyObject * |
| 2981 |
textiowrapper_repr(PyObject *op) |
| 2982 |
{ |
| 2983 |
PyObject *nameobj, *modeobj, *res, *s; |
| 2984 |
int status; |
| 2985 |
textio *self = textio_CAST(op); |
| 2986 |
const char *type_name = Py_TYPE(self)->tp_name; |
| 2987 |
|
| 2988 |
CHECK_INITIALIZED(self); |
| 2989 |
|
| 2990 |
res = PyUnicode_FromFormat("<%.100s", type_name); |
| 2991 |
if (res == NULL) |
| 2992 |
return NULL; |
| 2993 |
|
| 2994 |
status = Py_ReprEnter(op); |
| 2995 |
if (status != 0) { |
| 2996 |
if (status > 0) { |
| 2997 |
PyErr_Format(PyExc_RuntimeError, |
| 2998 |
"reentrant call inside %.100s.__repr__", |
| 2999 |
type_name); |
| 3000 |
} |
| 3001 |
goto error; |
| 3002 |
} |
| 3003 |
if (PyObject_GetOptionalAttr(op, &_Py_ID(name), &nameobj) < 0) { |
| 3004 |
if (!PyErr_ExceptionMatches(PyExc_ValueError)) { |
| 3005 |
goto error; |
| 3006 |
} |
| 3007 |
/* Ignore ValueError raised if the underlying stream was detached */ |
| 3008 |
PyErr_Clear(); |
| 3009 |
} |
| 3010 |
if (nameobj != NULL) { |
| 3011 |
s = PyUnicode_FromFormat(" name=%R", nameobj); |
| 3012 |
Py_DECREF(nameobj); |
| 3013 |
if (s == NULL) |
| 3014 |
goto error; |
| 3015 |
PyUnicode_AppendAndDel(&res, s); |
| 3016 |
if (res == NULL) |
| 3017 |
goto error; |
| 3018 |
} |
| 3019 |
if (PyObject_GetOptionalAttr(op, &_Py_ID(mode), &modeobj) < 0) { |
| 3020 |
goto error; |
| 3021 |
} |
| 3022 |
if (modeobj != NULL) { |
| 3023 |
s = PyUnicode_FromFormat(" mode=%R", modeobj); |
| 3024 |
Py_DECREF(modeobj); |
| 3025 |
if (s == NULL) |
| 3026 |
goto error; |
| 3027 |
PyUnicode_AppendAndDel(&res, s); |
| 3028 |
if (res == NULL) |
| 3029 |
goto error; |
| 3030 |
} |
| 3031 |
s = PyUnicode_FromFormat("%U encoding=%R>", |
| 3032 |
res, self->encoding); |
| 3033 |
Py_DECREF(res); |
| 3034 |
if (status == 0) { |
| 3035 |
Py_ReprLeave(op); |
| 3036 |
} |
| 3037 |
return s; |
| 3038 |
|
| 3039 |
error: |
| 3040 |
Py_XDECREF(res); |
| 3041 |
if (status == 0) { |
| 3042 |
Py_ReprLeave(op); |
| 3043 |
} |
| 3044 |
return NULL; |
| 3045 |
} |
| 3046 |
|
| 3047 |
|
| 3048 |
/* Inquiries */ |
| 3049 |
|
| 3050 |
/*[clinic input] |
| 3051 |
@critical_section |
| 3052 |
_io.TextIOWrapper.fileno |
| 3053 |
[clinic start generated code]*/ |
| 3054 |
|
| 3055 |
static PyObject * |
| 3056 |
_io_TextIOWrapper_fileno_impl(textio *self) |
| 3057 |
/*[clinic end generated code: output=21490a4c3da13e6c input=515e1196aceb97ab]*/ |
| 3058 |
{ |
| 3059 |
CHECK_ATTACHED(self); |
| 3060 |
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(fileno)); |
| 3061 |
} |
| 3062 |
|
| 3063 |
/*[clinic input] |
| 3064 |
@critical_section |
| 3065 |
_io.TextIOWrapper.seekable |
| 3066 |
[clinic start generated code]*/ |
| 3067 |
|
| 3068 |
static PyObject * |
| 3069 |
_io_TextIOWrapper_seekable_impl(textio *self) |
| 3070 |
/*[clinic end generated code: output=ab223dbbcffc0f00 input=71c4c092736c549b]*/ |
| 3071 |
{ |
| 3072 |
CHECK_ATTACHED(self); |
| 3073 |
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(seekable)); |
| 3074 |
} |
| 3075 |
|
| 3076 |
/*[clinic input] |
| 3077 |
@critical_section |
| 3078 |
_io.TextIOWrapper.readable |
| 3079 |
[clinic start generated code]*/ |
| 3080 |
|
| 3081 |
static PyObject * |
| 3082 |
_io_TextIOWrapper_readable_impl(textio *self) |
| 3083 |
/*[clinic end generated code: output=72ff7ba289a8a91b input=80438d1f01b0a89b]*/ |
| 3084 |
{ |
| 3085 |
CHECK_ATTACHED(self); |
| 3086 |
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(readable)); |
| 3087 |
} |
| 3088 |
|
| 3089 |
/*[clinic input] |
| 3090 |
@critical_section |
| 3091 |
_io.TextIOWrapper.writable |
| 3092 |
[clinic start generated code]*/ |
| 3093 |
|
| 3094 |
static PyObject * |
| 3095 |
_io_TextIOWrapper_writable_impl(textio *self) |
| 3096 |
/*[clinic end generated code: output=a728c71790d03200 input=9d6c22befb0c340a]*/ |
| 3097 |
{ |
| 3098 |
CHECK_ATTACHED(self); |
| 3099 |
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(writable)); |
| 3100 |
} |
| 3101 |
|
| 3102 |
/*[clinic input] |
| 3103 |
@critical_section |
| 3104 |
_io.TextIOWrapper.isatty |
| 3105 |
[clinic start generated code]*/ |
| 3106 |
|
| 3107 |
static PyObject * |
| 3108 |
_io_TextIOWrapper_isatty_impl(textio *self) |
| 3109 |
/*[clinic end generated code: output=12be1a35bace882e input=7f83ff04d4d1733d]*/ |
| 3110 |
{ |
| 3111 |
CHECK_ATTACHED(self); |
| 3112 |
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(isatty)); |
| 3113 |
} |
| 3114 |
|
| 3115 |
/*[clinic input] |
| 3116 |
@critical_section |
| 3117 |
_io.TextIOWrapper.flush |
| 3118 |
[clinic start generated code]*/ |
| 3119 |
|
| 3120 |
static PyObject * |
| 3121 |
_io_TextIOWrapper_flush_impl(textio *self) |
| 3122 |
/*[clinic end generated code: output=59de9165f9c2e4d2 input=3ac3bf521bfed59d]*/ |
| 3123 |
{ |
| 3124 |
CHECK_ATTACHED(self); |
| 3125 |
CHECK_CLOSED(self); |
| 3126 |
self->telling = self->seekable; |
| 3127 |
if (_textiowrapper_writeflush(self) < 0) |
| 3128 |
return NULL; |
| 3129 |
return PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(flush)); |
| 3130 |
} |
| 3131 |
|
| 3132 |
/*[clinic input] |
| 3133 |
@critical_section |
| 3134 |
_io.TextIOWrapper.close |
| 3135 |
[clinic start generated code]*/ |
| 3136 |
|
| 3137 |
static PyObject * |
| 3138 |
_io_TextIOWrapper_close_impl(textio *self) |
| 3139 |
/*[clinic end generated code: output=056ccf8b4876e4f4 input=8e12d7079d5ac5c1]*/ |
| 3140 |
{ |
| 3141 |
PyObject *res; |
| 3142 |
int r; |
| 3143 |
CHECK_ATTACHED(self); |
| 3144 |
|
| 3145 |
res = _io_TextIOWrapper_closed_get_impl(self); |
| 3146 |
if (res == NULL) |
| 3147 |
return NULL; |
| 3148 |
r = PyObject_IsTrue(res); |
| 3149 |
Py_DECREF(res); |
| 3150 |
if (r < 0) |
| 3151 |
return NULL; |
| 3152 |
|
| 3153 |
if (r > 0) { |
| 3154 |
Py_RETURN_NONE; /* stream already closed */ |
| 3155 |
} |
| 3156 |
if (self->detached) { |
| 3157 |
Py_RETURN_NONE; /* gh-142594 null pointer issue */ |
| 3158 |
} |
| 3159 |
else { |
| 3160 |
PyObject *exc = NULL; |
| 3161 |
if (self->finalizing) { |
| 3162 |
res = PyObject_CallMethodOneArg(self->buffer, &_Py_ID(_dealloc_warn), |
| 3163 |
(PyObject *)self); |
| 3164 |
if (res) { |
| 3165 |
Py_DECREF(res); |
| 3166 |
} |
| 3167 |
else { |
| 3168 |
PyErr_Clear(); |
| 3169 |
} |
| 3170 |
} |
| 3171 |
if (_PyFile_Flush((PyObject *)self) < 0) { |
| 3172 |
exc = PyErr_GetRaisedException(); |
| 3173 |
} |
| 3174 |
|
| 3175 |
res = PyObject_CallMethodNoArgs(self->buffer, &_Py_ID(close)); |
| 3176 |
if (exc != NULL) { |
| 3177 |
_PyErr_ChainExceptions1(exc); |
| 3178 |
Py_CLEAR(res); |
| 3179 |
} |
| 3180 |
return res; |
| 3181 |
} |
| 3182 |
} |
| 3183 |
|
| 3184 |
static PyObject * |
| 3185 |
textiowrapper_iternext_lock_held(PyObject *op) |
| 3186 |
{ |
| 3187 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); |
| 3188 |
PyObject *line; |
| 3189 |
textio *self = textio_CAST(op); |
| 3190 |
|
| 3191 |
CHECK_ATTACHED(self); |
| 3192 |
|
| 3193 |
self->telling = 0; |
| 3194 |
if (Py_IS_TYPE(self, self->state->PyTextIOWrapper_Type)) { |
| 3195 |
/* Skip method call overhead for speed */ |
| 3196 |
line = _textiowrapper_readline(self, -1); |
| 3197 |
} |
| 3198 |
else { |
| 3199 |
line = PyObject_CallMethodNoArgs(op, &_Py_ID(readline)); |
| 3200 |
if (line && !PyUnicode_Check(line)) { |
| 3201 |
PyErr_Format(PyExc_OSError, |
| 3202 |
"readline() should have returned a str object, " |
| 3203 |
"not '%.200s'", Py_TYPE(line)->tp_name); |
| 3204 |
Py_DECREF(line); |
| 3205 |
return NULL; |
| 3206 |
} |
| 3207 |
} |
| 3208 |
|
| 3209 |
if (line == NULL) |
| 3210 |
return NULL; |
| 3211 |
|
| 3212 |
if (PyUnicode_GET_LENGTH(line) == 0) { |
| 3213 |
/* Reached EOF or would have blocked */ |
| 3214 |
Py_DECREF(line); |
| 3215 |
Py_CLEAR(self->snapshot); |
| 3216 |
self->telling = self->seekable; |
| 3217 |
return NULL; |
| 3218 |
} |
| 3219 |
|
| 3220 |
return line; |
| 3221 |
} |
| 3222 |
|
| 3223 |
static PyObject * |
| 3224 |
textiowrapper_iternext(PyObject *op) |
| 3225 |
{ |
| 3226 |
PyObject *result; |
| 3227 |
Py_BEGIN_CRITICAL_SECTION(op); |
| 3228 |
result = textiowrapper_iternext_lock_held(op); |
| 3229 |
Py_END_CRITICAL_SECTION(); |
| 3230 |
return result; |
| 3231 |
} |
| 3232 |
|
| 3233 |
/*[clinic input] |
| 3234 |
@critical_section |
| 3235 |
@getter |
| 3236 |
_io.TextIOWrapper.name |
| 3237 |
[clinic start generated code]*/ |
| 3238 |
|
| 3239 |
static PyObject * |
| 3240 |
_io_TextIOWrapper_name_get_impl(textio *self) |
| 3241 |
/*[clinic end generated code: output=8c2f1d6d8756af40 input=26ecec9b39e30e07]*/ |
| 3242 |
{ |
| 3243 |
CHECK_ATTACHED(self); |
| 3244 |
return PyObject_GetAttr(self->buffer, &_Py_ID(name)); |
| 3245 |
} |
| 3246 |
|
| 3247 |
/*[clinic input] |
| 3248 |
@critical_section |
| 3249 |
@getter |
| 3250 |
_io.TextIOWrapper.closed |
| 3251 |
[clinic start generated code]*/ |
| 3252 |
|
| 3253 |
static PyObject * |
| 3254 |
_io_TextIOWrapper_closed_get_impl(textio *self) |
| 3255 |
/*[clinic end generated code: output=b49b68f443a85e3c input=7dfcf43f63c7003d]*/ |
| 3256 |
{ |
| 3257 |
CHECK_ATTACHED(self); |
| 3258 |
return PyObject_GetAttr(self->buffer, &_Py_ID(closed)); |
| 3259 |
} |
| 3260 |
|
| 3261 |
/*[clinic input] |
| 3262 |
@critical_section |
| 3263 |
@getter |
| 3264 |
_io.TextIOWrapper.newlines |
| 3265 |
[clinic start generated code]*/ |
| 3266 |
|
| 3267 |
static PyObject * |
| 3268 |
_io_TextIOWrapper_newlines_get_impl(textio *self) |
| 3269 |
/*[clinic end generated code: output=53aa03ac35573180 input=610df647e514b3e8]*/ |
| 3270 |
{ |
| 3271 |
PyObject *res; |
| 3272 |
CHECK_ATTACHED(self); |
| 3273 |
if (self->decoder == NULL || |
| 3274 |
PyObject_GetOptionalAttr(self->decoder, &_Py_ID(newlines), &res) == 0) |
| 3275 |
{ |
| 3276 |
Py_RETURN_NONE; |
| 3277 |
} |
| 3278 |
return res; |
| 3279 |
} |
| 3280 |
|
| 3281 |
/*[clinic input] |
| 3282 |
@critical_section |
| 3283 |
@getter |
| 3284 |
_io.TextIOWrapper.errors |
| 3285 |
[clinic start generated code]*/ |
| 3286 |
|
| 3287 |
static PyObject * |
| 3288 |
_io_TextIOWrapper_errors_get_impl(textio *self) |
| 3289 |
/*[clinic end generated code: output=dca3a3ef21b09484 input=b45f983e6d43c4d8]*/ |
| 3290 |
{ |
| 3291 |
CHECK_INITIALIZED(self); |
| 3292 |
return Py_NewRef(self->errors); |
| 3293 |
} |
| 3294 |
|
| 3295 |
/*[clinic input] |
| 3296 |
@critical_section |
| 3297 |
@getter |
| 3298 |
_io.TextIOWrapper._CHUNK_SIZE |
| 3299 |
[clinic start generated code]*/ |
| 3300 |
|
| 3301 |
static PyObject * |
| 3302 |
_io_TextIOWrapper__CHUNK_SIZE_get_impl(textio *self) |
| 3303 |
/*[clinic end generated code: output=039925cd2df375bc input=e9715b0e06ff0fa6]*/ |
| 3304 |
{ |
| 3305 |
CHECK_ATTACHED(self); |
| 3306 |
return PyLong_FromSsize_t(self->chunk_size); |
| 3307 |
} |
| 3308 |
|
| 3309 |
/*[clinic input] |
| 3310 |
@critical_section |
| 3311 |
@setter |
| 3312 |
_io.TextIOWrapper._CHUNK_SIZE |
| 3313 |
[clinic start generated code]*/ |
| 3314 |
|
| 3315 |
static int |
| 3316 |
_io_TextIOWrapper__CHUNK_SIZE_set_impl(textio *self, PyObject *value) |
| 3317 |
/*[clinic end generated code: output=edb86d2db660a5ab input=32fc99861db02a0a]*/ |
| 3318 |
{ |
| 3319 |
Py_ssize_t n; |
| 3320 |
CHECK_ATTACHED_INT(self); |
| 3321 |
if (value == NULL) { |
| 3322 |
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 3323 |
return -1; |
| 3324 |
} |
| 3325 |
n = PyNumber_AsSsize_t(value, PyExc_ValueError); |
| 3326 |
if (n == -1 && PyErr_Occurred()) |
| 3327 |
return -1; |
| 3328 |
if (n <= 0) { |
| 3329 |
PyErr_SetString(PyExc_ValueError, |
| 3330 |
"a strictly positive integer is required"); |
| 3331 |
return -1; |
| 3332 |
} |
| 3333 |
self->chunk_size = n; |
| 3334 |
return 0; |
| 3335 |
} |
| 3336 |
|
| 3337 |
static PyMethodDef incrementalnewlinedecoder_methods[] = { |
| 3338 |
_IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF |
| 3339 |
_IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF |
| 3340 |
_IO_INCREMENTALNEWLINEDECODER_SETSTATE_METHODDEF |
| 3341 |
_IO_INCREMENTALNEWLINEDECODER_RESET_METHODDEF |
| 3342 |
{NULL} |
| 3343 |
}; |
| 3344 |
|
| 3345 |
static PyGetSetDef incrementalnewlinedecoder_getset[] = { |
| 3346 |
{"newlines", incrementalnewlinedecoder_newlines_get, NULL, NULL}, |
| 3347 |
{NULL} |
| 3348 |
}; |
| 3349 |
|
| 3350 |
static PyType_Slot nldecoder_slots[] = { |
| 3351 |
{Py_tp_dealloc, incrementalnewlinedecoder_dealloc}, |
| 3352 |
{Py_tp_doc, (void *)_io_IncrementalNewlineDecoder___init____doc__}, |
| 3353 |
{Py_tp_methods, incrementalnewlinedecoder_methods}, |
| 3354 |
{Py_tp_getset, incrementalnewlinedecoder_getset}, |
| 3355 |
{Py_tp_traverse, incrementalnewlinedecoder_traverse}, |
| 3356 |
{Py_tp_clear, incrementalnewlinedecoder_clear}, |
| 3357 |
{Py_tp_init, _io_IncrementalNewlineDecoder___init__}, |
| 3358 |
{0, NULL}, |
| 3359 |
}; |
| 3360 |
|
| 3361 |
PyType_Spec nldecoder_spec = { |
| 3362 |
.name = "_io.IncrementalNewlineDecoder", |
| 3363 |
.basicsize = sizeof(nldecoder_object), |
| 3364 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 3365 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 3366 |
.slots = nldecoder_slots, |
| 3367 |
}; |
| 3368 |
|
| 3369 |
|
| 3370 |
static PyMethodDef textiowrapper_methods[] = { |
| 3371 |
_IO_TEXTIOWRAPPER_DETACH_METHODDEF |
| 3372 |
_IO_TEXTIOWRAPPER_RECONFIGURE_METHODDEF |
| 3373 |
_IO_TEXTIOWRAPPER_WRITE_METHODDEF |
| 3374 |
_IO_TEXTIOWRAPPER_READ_METHODDEF |
| 3375 |
_IO_TEXTIOWRAPPER_READLINE_METHODDEF |
| 3376 |
_IO_TEXTIOWRAPPER_FLUSH_METHODDEF |
| 3377 |
_IO_TEXTIOWRAPPER_CLOSE_METHODDEF |
| 3378 |
|
| 3379 |
_IO_TEXTIOWRAPPER_FILENO_METHODDEF |
| 3380 |
_IO_TEXTIOWRAPPER_SEEKABLE_METHODDEF |
| 3381 |
_IO_TEXTIOWRAPPER_READABLE_METHODDEF |
| 3382 |
_IO_TEXTIOWRAPPER_WRITABLE_METHODDEF |
| 3383 |
_IO_TEXTIOWRAPPER_ISATTY_METHODDEF |
| 3384 |
|
| 3385 |
_IO_TEXTIOWRAPPER_SEEK_METHODDEF |
| 3386 |
_IO_TEXTIOWRAPPER_TELL_METHODDEF |
| 3387 |
_IO_TEXTIOWRAPPER_TRUNCATE_METHODDEF |
| 3388 |
|
| 3389 |
{"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS}, |
| 3390 |
{NULL, NULL} |
| 3391 |
}; |
| 3392 |
|
| 3393 |
static PyMemberDef textiowrapper_members[] = { |
| 3394 |
{"encoding", _Py_T_OBJECT, offsetof(textio, encoding), Py_READONLY}, |
| 3395 |
{"buffer", _Py_T_OBJECT, offsetof(textio, buffer), Py_READONLY}, |
| 3396 |
{"line_buffering", Py_T_BOOL, offsetof(textio, line_buffering), Py_READONLY}, |
| 3397 |
{"write_through", Py_T_BOOL, offsetof(textio, write_through), Py_READONLY}, |
| 3398 |
{"_finalizing", Py_T_BOOL, offsetof(textio, finalizing), 0}, |
| 3399 |
{"__weaklistoffset__", Py_T_PYSSIZET, offsetof(textio, weakreflist), Py_READONLY}, |
| 3400 |
{"__dictoffset__", Py_T_PYSSIZET, offsetof(textio, dict), Py_READONLY}, |
| 3401 |
{NULL} |
| 3402 |
}; |
| 3403 |
|
| 3404 |
static PyGetSetDef textiowrapper_getset[] = { |
| 3405 |
_IO_TEXTIOWRAPPER_NAME_GETSETDEF |
| 3406 |
_IO_TEXTIOWRAPPER_CLOSED_GETSETDEF |
| 3407 |
_IO_TEXTIOWRAPPER_NEWLINES_GETSETDEF |
| 3408 |
_IO_TEXTIOWRAPPER_ERRORS_GETSETDEF |
| 3409 |
_IO_TEXTIOWRAPPER__CHUNK_SIZE_GETSETDEF |
| 3410 |
{NULL} |
| 3411 |
}; |
| 3412 |
|
| 3413 |
PyType_Slot textiowrapper_slots[] = { |
| 3414 |
{Py_tp_dealloc, textiowrapper_dealloc}, |
| 3415 |
{Py_tp_repr, textiowrapper_repr}, |
| 3416 |
{Py_tp_doc, (void *)_io_TextIOWrapper___init____doc__}, |
| 3417 |
{Py_tp_traverse, textiowrapper_traverse}, |
| 3418 |
{Py_tp_clear, textiowrapper_clear}, |
| 3419 |
{Py_tp_iternext, textiowrapper_iternext}, |
| 3420 |
{Py_tp_methods, textiowrapper_methods}, |
| 3421 |
{Py_tp_members, textiowrapper_members}, |
| 3422 |
{Py_tp_getset, textiowrapper_getset}, |
| 3423 |
{Py_tp_init, _io_TextIOWrapper___init__}, |
| 3424 |
{0, NULL}, |
| 3425 |
}; |
| 3426 |
|
| 3427 |
PyType_Spec textiowrapper_spec = { |
| 3428 |
.name = "_io.TextIOWrapper", |
| 3429 |
.basicsize = sizeof(textio), |
| 3430 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 3431 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 3432 |
.slots = textiowrapper_slots, |
| 3433 |
}; |