| 1 |
import sys |
| 2 |
|
| 3 |
from pypy.interpreter.baseobjspace import W_Root, BufferInterfaceNotFound |
| 4 |
from pypy.interpreter.error import OperationError, oefmt |
| 5 |
from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec |
| 6 |
from pypy.interpreter.typedef import ( |
| 7 |
GetSetProperty, TypeDef, generic_new_descr, interp_attrproperty, |
| 8 |
interp_attrproperty_w) |
| 9 |
from pypy.module._codecs import interp_codecs |
| 10 |
from pypy.module._io.interp_iobase import W_IOBase, convert_size, trap_eintr |
| 11 |
from pypy.module.posix.interp_posix import device_encoding |
| 12 |
from rpython.rlib.rarithmetic import intmask, r_uint, r_ulonglong |
| 13 |
from rpython.rlib.rbigint import rbigint |
| 14 |
from rpython.rlib.rstring import StringBuilder |
| 15 |
from rpython.rlib.rutf8 import (check_utf8, next_codepoint_pos, |
| 16 |
codepoints_in_utf8, codepoints_in_utf8, |
| 17 |
Utf8StringBuilder) |
| 18 |
from rpython.rlib import jit |
| 19 |
from rpython.rlib.objectmodel import always_inline |
| 20 |
|
| 21 |
|
| 22 |
STATE_ZERO, STATE_OK, STATE_DETACHED = range(3) |
| 23 |
|
| 24 |
SEEN_CR = 1 |
| 25 |
SEEN_LF = 2 |
| 26 |
SEEN_CRLF = 4 |
| 27 |
SEEN_ALL = SEEN_CR | SEEN_LF | SEEN_CRLF |
| 28 |
|
| 29 |
_WINDOWS = sys.platform == 'win32' |
| 30 |
|
| 31 |
def make_newlines_dict(space): |
| 32 |
return { |
| 33 |
SEEN_CR: space.newtext("\r", 1), |
| 34 |
SEEN_LF: space.newtext("\n", 1), |
| 35 |
SEEN_CRLF: space.newtext("\r\n", 2), |
| 36 |
SEEN_CR | SEEN_LF: space.newtuple( |
| 37 |
[space.newtext("\r", 1), |
| 38 |
space.newtext("\n", 1)]), |
| 39 |
SEEN_CR | SEEN_CRLF: space.newtuple( |
| 40 |
[space.newtext("\r", 1), |
| 41 |
space.newtext("\r\n", 2)]), |
| 42 |
SEEN_LF | SEEN_CRLF: space.newtuple( |
| 43 |
[space.newtext("\n", 1), |
| 44 |
space.newtext("\r\n", 2)]), |
| 45 |
SEEN_CR | SEEN_LF | SEEN_CRLF: space.newtuple( |
| 46 |
[space.newtext("\r", 1), |
| 47 |
space.newtext("\n", 1), |
| 48 |
space.newtext("\r\n", 2)]), |
| 49 |
} |
| 50 |
|
| 51 |
def io_check_errors(space, w_errors): |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
errors = space.text0_w(w_errors) |
| 57 |
space.encode_unicode_object(w_errors, 'utf8', 'strict') |
| 58 |
if not space.sys.get_flag('dev_mode'): |
| 59 |
return |
| 60 |
interp_codecs.lookup_error(space, errors) |
| 61 |
|
| 62 |
class W_IncrementalNewlineDecoder(W_Root): |
| 63 |
seennl = 0 |
| 64 |
pendingcr = False |
| 65 |
w_decoder = None |
| 66 |
|
| 67 |
def __init__(self, space): |
| 68 |
pass |
| 69 |
|
| 70 |
@unwrap_spec(translate=int) |
| 71 |
def descr_init(self, space, w_decoder, translate, w_errors=None): |
| 72 |
self.w_decoder = w_decoder |
| 73 |
self.translate = translate |
| 74 |
if space.is_none(w_errors) or w_errors is None: |
| 75 |
w_errors = space.newtext("strict") |
| 76 |
if not space.isinstance_w(w_errors, space.w_text): |
| 77 |
raise oefmt(space.w_TypeError, "TextIOWrapper() argument 'errors' " |
| 78 |
"must be str or None, not %N", w_errors) |
| 79 |
io_check_errors(space, w_errors) |
| 80 |
self.w_errors = w_errors |
| 81 |
|
| 82 |
self.seennl = 0 |
| 83 |
|
| 84 |
def newlines_get_w(self, space): |
| 85 |
return space.fromcache(make_newlines_dict).get(self.seennl, space.w_None) |
| 86 |
|
| 87 |
@unwrap_spec(final=int) |
| 88 |
def decode_w(self, space, w_input, final=False): |
| 89 |
if self.w_decoder is None: |
| 90 |
raise oefmt(space.w_ValueError, |
| 91 |
"IncrementalNewlineDecoder.__init__ not called") |
| 92 |
|
| 93 |
|
| 94 |
if not space.is_w(self.w_decoder, space.w_None): |
| 95 |
w_output = space.call_method(self.w_decoder, "decode", |
| 96 |
w_input, space.newbool(bool(final))) |
| 97 |
else: |
| 98 |
w_output = w_input |
| 99 |
|
| 100 |
if not space.isinstance_w(w_output, space.w_unicode): |
| 101 |
raise oefmt(space.w_TypeError, |
| 102 |
"decoder should return a string result") |
| 103 |
|
| 104 |
output, output_len = space.utf8_len_w(w_output) |
| 105 |
w_result = self._decoder_w_unboxed(space, output, output_len, final) |
| 106 |
if w_result is None: |
| 107 |
return w_output |
| 108 |
return w_result |
| 109 |
|
| 110 |
def _decoder_w_unboxed(self, space, output, output_len, final): |
| 111 |
changed = False |
| 112 |
if self.pendingcr and (final or output_len): |
| 113 |
output = '\r' + output |
| 114 |
self.pendingcr = False |
| 115 |
output_len += 1 |
| 116 |
changed = True |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if not final and output_len > 0: |
| 121 |
last = len(output) - 1 |
| 122 |
assert last >= 0 |
| 123 |
if output[last] == '\r': |
| 124 |
output = output[:last] |
| 125 |
self.pendingcr = True |
| 126 |
output_len -= 1 |
| 127 |
changed = True |
| 128 |
|
| 129 |
if output_len == 0: |
| 130 |
return space.newutf8("", 0) |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
seennl = self.seennl |
| 135 |
|
| 136 |
rpos = output.find('\r') |
| 137 |
if rpos < 0: |
| 138 |
|
| 139 |
|
| 140 |
if not (seennl & SEEN_LF) and output.find('\n') >= 0: |
| 141 |
self.seennl |= SEEN_LF |
| 142 |
|
| 143 |
|
| 144 |
if not changed: |
| 145 |
return None |
| 146 |
elif not self.translate: |
| 147 |
i = 0 |
| 148 |
while i < len(output): |
| 149 |
if seennl == SEEN_ALL: |
| 150 |
break |
| 151 |
c = output[i] |
| 152 |
i += 1 |
| 153 |
if c == '\n': |
| 154 |
seennl |= SEEN_LF |
| 155 |
elif c == '\r': |
| 156 |
if i < len(output) and output[i] == '\n': |
| 157 |
seennl |= SEEN_CRLF |
| 158 |
i += 1 |
| 159 |
else: |
| 160 |
seennl |= SEEN_CR |
| 161 |
if not changed: |
| 162 |
self.seennl |= seennl |
| 163 |
return None |
| 164 |
else: |
| 165 |
changed = True |
| 166 |
assert rpos >= 0 |
| 167 |
|
| 168 |
builder = StringBuilder(len(output)) |
| 169 |
if output.find("\n", 0, rpos) >= 0: |
| 170 |
seennl |= SEEN_LF |
| 171 |
builder.append_slice(output, 0, rpos) |
| 172 |
i = rpos |
| 173 |
while i < len(output): |
| 174 |
c = output[i] |
| 175 |
i += 1 |
| 176 |
if c == '\n': |
| 177 |
seennl |= SEEN_LF |
| 178 |
elif c == '\r': |
| 179 |
if i < len(output) and output[i] == '\n': |
| 180 |
seennl |= SEEN_CRLF |
| 181 |
i += 1 |
| 182 |
output_len -= 1 |
| 183 |
else: |
| 184 |
seennl |= SEEN_CR |
| 185 |
builder.append('\n') |
| 186 |
continue |
| 187 |
builder.append(c) |
| 188 |
output = builder.build() |
| 189 |
self.seennl |= seennl |
| 190 |
return space.newutf8(output, output_len) |
| 191 |
|
| 192 |
def reset_w(self, space): |
| 193 |
self.seennl = 0 |
| 194 |
self.pendingcr = False |
| 195 |
if self.w_decoder and not space.is_w(self.w_decoder, space.w_None): |
| 196 |
space.call_method(self.w_decoder, "reset") |
| 197 |
|
| 198 |
def getstate_u(self, space): |
| 199 |
if self.w_decoder and not space.is_w(self.w_decoder, space.w_None): |
| 200 |
w_state = space.call_method(self.w_decoder, "getstate") |
| 201 |
w_buffer, w_flag = space.unpackiterable(w_state, 2) |
| 202 |
flag = space.r_longlong_w(w_flag) |
| 203 |
else: |
| 204 |
w_buffer = space.newbytes("") |
| 205 |
flag = 0 |
| 206 |
flag <<= 1 |
| 207 |
if self.pendingcr: |
| 208 |
flag |= 1 |
| 209 |
return w_buffer, flag |
| 210 |
|
| 211 |
def getstate_w(self, space): |
| 212 |
w_buffer, flag = self.getstate_u(space) |
| 213 |
return space.newtuple2(w_buffer, space.newint(flag)) |
| 214 |
|
| 215 |
def setstate_w(self, space, w_state): |
| 216 |
w_buffer, w_flag = space.unpackiterable(w_state, 2) |
| 217 |
flag = space.r_longlong_w(w_flag) |
| 218 |
self.pendingcr = bool(flag & 1) |
| 219 |
flag >>= 1 |
| 220 |
|
| 221 |
if self.w_decoder and not space.is_w(self.w_decoder, space.w_None): |
| 222 |
w_state = space.newtuple2(w_buffer, space.newint(flag)) |
| 223 |
space.call_method(self.w_decoder, "setstate", w_state) |
| 224 |
|
| 225 |
W_IncrementalNewlineDecoder.typedef = TypeDef( |
| 226 |
'_io.IncrementalNewlineDecoder', |
| 227 |
__new__ = generic_new_descr(W_IncrementalNewlineDecoder), |
| 228 |
__init__ = interp2app(W_IncrementalNewlineDecoder.descr_init), |
| 229 |
|
| 230 |
decode = interp2app(W_IncrementalNewlineDecoder.decode_w), |
| 231 |
reset = interp2app(W_IncrementalNewlineDecoder.reset_w), |
| 232 |
getstate = interp2app(W_IncrementalNewlineDecoder.getstate_w), |
| 233 |
setstate = interp2app(W_IncrementalNewlineDecoder.setstate_w), |
| 234 |
|
| 235 |
newlines = GetSetProperty(W_IncrementalNewlineDecoder.newlines_get_w), |
| 236 |
) |
| 237 |
|
| 238 |
class W_TextIOBase(W_IOBase): |
| 239 |
w_encoding = None |
| 240 |
|
| 241 |
def __init__(self, space): |
| 242 |
W_IOBase.__init__(self, space) |
| 243 |
|
| 244 |
def read_w(self, space, w_size=None): |
| 245 |
self._unsupportedoperation(space, "read") |
| 246 |
|
| 247 |
def readline_w(self, space, w_limit=None): |
| 248 |
self._unsupportedoperation(space, "readline") |
| 249 |
|
| 250 |
def write_w(self, space, w_data): |
| 251 |
self._unsupportedoperation(space, "write") |
| 252 |
|
| 253 |
def detach_w(self, space): |
| 254 |
self._unsupportedoperation(space, "detach") |
| 255 |
|
| 256 |
def errors_get_w(self, space): |
| 257 |
return space.w_None |
| 258 |
|
| 259 |
def newlines_get_w(self, space): |
| 260 |
return space.w_None |
| 261 |
|
| 262 |
W_TextIOBase.typedef = TypeDef( |
| 263 |
'_io._TextIOBase', W_IOBase.typedef, |
| 264 |
__new__ = generic_new_descr(W_TextIOBase), |
| 265 |
|
| 266 |
read = interp2app(W_TextIOBase.read_w), |
| 267 |
readline = interp2app(W_TextIOBase.readline_w), |
| 268 |
write = interp2app(W_TextIOBase.write_w), |
| 269 |
detach = interp2app(W_TextIOBase.detach_w), |
| 270 |
encoding = interp_attrproperty_w("w_encoding", W_TextIOBase), |
| 271 |
newlines = GetSetProperty(W_TextIOBase.newlines_get_w), |
| 272 |
errors = GetSetProperty(W_TextIOBase.errors_get_w), |
| 273 |
) |
| 274 |
|
| 275 |
|
| 276 |
def _determine_encoding(space, encoding, w_buffer): |
| 277 |
if encoding == "locale": |
| 278 |
pass |
| 279 |
elif encoding is None: |
| 280 |
_maybe_warn_encoding(space) |
| 281 |
else: |
| 282 |
return space.newtext(encoding) |
| 283 |
|
| 284 |
|
| 285 |
try: |
| 286 |
w_fileno = space.call_method(w_buffer, 'fileno') |
| 287 |
except OperationError as e: |
| 288 |
from pypy.module._io.interp_io import Cache |
| 289 |
if not (e.match(space, space.w_AttributeError) or |
| 290 |
e.match(space, space.fromcache(Cache).w_unsupportedoperation)): |
| 291 |
raise |
| 292 |
else: |
| 293 |
try: |
| 294 |
w_encoding = device_encoding(space, space.int_w(w_fileno)) |
| 295 |
except OverflowError: |
| 296 |
raise oefmt(space.w_OverflowError, |
| 297 |
"Python int too large to convert to C int") |
| 298 |
else: |
| 299 |
if space.isinstance_w(w_encoding, space.w_unicode): |
| 300 |
return w_encoding |
| 301 |
if space.sys.get_flag('utf8_mode'): |
| 302 |
|
| 303 |
|
| 304 |
return space.newtext("utf-8") |
| 305 |
|
| 306 |
from pypy.module._locale.interp_locale import _getencoding |
| 307 |
return space.newtext(_getencoding()) |
| 308 |
|
| 309 |
@unwrap_spec(stacklevel=int) |
| 310 |
def text_encoding(space, w_encoding, stacklevel=2): |
| 311 |
""" |
| 312 |
A helper function to choose the text encoding. |
| 313 |
|
| 314 |
When encoding is not None, just return it. |
| 315 |
Otherwise, return the default text encoding (i.e. "locale"). |
| 316 |
|
| 317 |
This function emits an EncodingWarning if *encoding* is None and |
| 318 |
sys.flags.warn_default_encoding is non-zero. |
| 319 |
|
| 320 |
This can be used in APIs with an encoding=None parameter |
| 321 |
that pass it to TextIOWrapper or open. |
| 322 |
However, please consider using encoding="utf-8" for new APIs. |
| 323 |
""" |
| 324 |
if space.is_none(w_encoding): |
| 325 |
_maybe_warn_encoding(space, stacklevel + 1) |
| 326 |
if space.sys.get_flag('utf8_mode') == 0: |
| 327 |
return space.newtext("locale") |
| 328 |
else: |
| 329 |
return space.newtext("utf-8") |
| 330 |
return w_encoding |
| 331 |
|
| 332 |
def _maybe_warn_encoding(space, stacklevel=1): |
| 333 |
if space.sys.get_flag('warn_default_encoding') != 0: |
| 334 |
space.warn(space.newtext("'encoding' argument not specified."), |
| 335 |
space.w_EncodingWarning, stacklevel) |
| 336 |
|
| 337 |
class PositionCookie(object): |
| 338 |
def __init__(self, bigint): |
| 339 |
self.start_pos = bigint.ulonglongmask() |
| 340 |
bigint = bigint.rshift(r_ulonglong.BITS) |
| 341 |
x = intmask(bigint.uintmask()) |
| 342 |
assert x >= 0 |
| 343 |
self.dec_flags = x |
| 344 |
bigint = bigint.rshift(r_uint.BITS) |
| 345 |
x = intmask(bigint.uintmask()) |
| 346 |
assert x >= 0 |
| 347 |
self.bytes_to_feed = x |
| 348 |
bigint = bigint.rshift(r_uint.BITS) |
| 349 |
x = intmask(bigint.uintmask()) |
| 350 |
assert x >= 0 |
| 351 |
self.chars_to_skip = x |
| 352 |
bigint = bigint.rshift(r_uint.BITS) |
| 353 |
self.need_eof = bigint.tobool() |
| 354 |
|
| 355 |
def pack(self): |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
rb = rbigint.fromrarith_int |
| 362 |
|
| 363 |
res = rb(self.start_pos) |
| 364 |
bits = r_ulonglong.BITS |
| 365 |
res = res.or_(rb(r_uint(self.dec_flags)).lshift(bits)) |
| 366 |
bits += r_uint.BITS |
| 367 |
res = res.or_(rb(r_uint(self.bytes_to_feed)).lshift(bits)) |
| 368 |
bits += r_uint.BITS |
| 369 |
res = res.or_(rb(r_uint(self.chars_to_skip)).lshift(bits)) |
| 370 |
bits += r_uint.BITS |
| 371 |
return res.or_(rb(r_uint(self.need_eof)).lshift(bits)) |
| 372 |
|
| 373 |
def __repr__(self): |
| 374 |
return "coookie w/start_pos %d dec_flags %d bytes_to_feed %d chars_to_skip %d need_eof %d" %(self.start_pos, self.dec_flags, self.bytes_to_feed, self.chars_to_skip, self.need_eof) |
| 375 |
|
| 376 |
class PositionSnapshot: |
| 377 |
_immutable_fields_ = ["flags", "input"] |
| 378 |
def __init__(self, flags, input): |
| 379 |
self.flags = flags |
| 380 |
self.input = input |
| 381 |
|
| 382 |
|
| 383 |
class DecodeBuffer(object): |
| 384 |
def __init__(self, text=None, ulen=-1): |
| 385 |
|
| 386 |
if text is not None: |
| 387 |
assert ulen >= 0 |
| 388 |
self.text = text |
| 389 |
self.pos = 0 |
| 390 |
self.upos = 0 |
| 391 |
self.ulen = ulen |
| 392 |
|
| 393 |
def set(self, space, w_decoded): |
| 394 |
|
| 395 |
check_decoded(space, w_decoded) |
| 396 |
self.text, self.ulen = space.utf8_len_w(w_decoded) |
| 397 |
self.pos = 0 |
| 398 |
self.upos = 0 |
| 399 |
|
| 400 |
def reset(self): |
| 401 |
self.text = None |
| 402 |
self.pos = 0 |
| 403 |
self.upos = 0 |
| 404 |
self.ulen = -1 |
| 405 |
|
| 406 |
def get_chars(self, size): |
| 407 |
""" returns a tuple (utf8, lgt) """ |
| 408 |
|
| 409 |
if self.text is None or size == 0: |
| 410 |
return "", 0 |
| 411 |
|
| 412 |
lgt = self.ulen |
| 413 |
available = lgt - self.upos |
| 414 |
if size < 0 or size > available: |
| 415 |
size = available |
| 416 |
assert size >= 0 |
| 417 |
|
| 418 |
if self.pos > 0 or size < available: |
| 419 |
start = self.pos |
| 420 |
pos = start |
| 421 |
for i in range(size): |
| 422 |
pos = next_codepoint_pos(self.text, pos) |
| 423 |
self.upos += 1 |
| 424 |
assert start >= 0 |
| 425 |
assert pos >= 0 |
| 426 |
chars = self.text[start:pos] |
| 427 |
self.pos = pos |
| 428 |
else: |
| 429 |
chars = self.text |
| 430 |
self.pos = len(self.text) |
| 431 |
self.upos = lgt |
| 432 |
size = lgt |
| 433 |
|
| 434 |
return chars, size |
| 435 |
|
| 436 |
def has_data(self): |
| 437 |
return (self.text is not None and not self.exhausted()) |
| 438 |
|
| 439 |
def exhausted(self): |
| 440 |
return self.pos >= len(self.text) |
| 441 |
|
| 442 |
def find_newline_universal(self, limit): |
| 443 |
|
| 444 |
|
| 445 |
if limit < 0: |
| 446 |
limit = sys.maxint |
| 447 |
scanned = 0 |
| 448 |
while scanned < limit: |
| 449 |
if self.exhausted(): |
| 450 |
return False |
| 451 |
ch = self.text[self.pos] |
| 452 |
self._advance_codepoint() |
| 453 |
scanned += 1 |
| 454 |
if ch == '\n': |
| 455 |
return True |
| 456 |
if ch == '\r': |
| 457 |
if scanned >= limit: |
| 458 |
return False |
| 459 |
if self.exhausted(): |
| 460 |
return True |
| 461 |
ch = self.text[self.pos] |
| 462 |
if ch == '\n': |
| 463 |
self.pos += 1 |
| 464 |
self.upos += 1 |
| 465 |
return True |
| 466 |
else: |
| 467 |
return True |
| 468 |
return False |
| 469 |
|
| 470 |
def find_crlf(self, limit): |
| 471 |
if limit < 0: |
| 472 |
limit = sys.maxint |
| 473 |
scanned = 0 |
| 474 |
while scanned < limit: |
| 475 |
if self.exhausted(): |
| 476 |
return False |
| 477 |
ch = self.text[self.pos] |
| 478 |
scanned += 1 |
| 479 |
if ch == '\r': |
| 480 |
self.pos += 1 |
| 481 |
self.upos += 1 |
| 482 |
if scanned >= limit: |
| 483 |
return False |
| 484 |
if self.exhausted(): |
| 485 |
|
| 486 |
|
| 487 |
self.pos -= 1 |
| 488 |
self.upos -= 1 |
| 489 |
return False |
| 490 |
if self.text[self.pos] == '\n': |
| 491 |
self.pos += 1 |
| 492 |
self.upos += 1 |
| 493 |
return True |
| 494 |
else: |
| 495 |
self._advance_codepoint() |
| 496 |
return False |
| 497 |
|
| 498 |
def find_char(self, marker, limit): |
| 499 |
|
| 500 |
assert 0 <= ord(marker) < 128 |
| 501 |
|
| 502 |
if self.ulen == len(self.text): |
| 503 |
end = len(self.text) |
| 504 |
if limit >= 0: |
| 505 |
end = min(end, self.pos + limit) |
| 506 |
pos = self.pos |
| 507 |
assert pos >= 0 |
| 508 |
assert end >= 0 |
| 509 |
pos = self.text.find(marker, pos, end) |
| 510 |
if pos >= 0: |
| 511 |
self.pos = self.upos = pos + 1 |
| 512 |
return True |
| 513 |
else: |
| 514 |
self.pos = self.upos = end |
| 515 |
return False |
| 516 |
|
| 517 |
if limit < 0: |
| 518 |
limit = sys.maxint |
| 519 |
|
| 520 |
|
| 521 |
scanned = 0 |
| 522 |
while scanned < limit: |
| 523 |
|
| 524 |
if self.exhausted(): |
| 525 |
return False |
| 526 |
|
| 527 |
found = self.text[self.pos] == marker |
| 528 |
if found: |
| 529 |
self.pos += 1 |
| 530 |
self.upos += 1 |
| 531 |
return True |
| 532 |
self._advance_codepoint() |
| 533 |
scanned += 1 |
| 534 |
return False |
| 535 |
|
| 536 |
@always_inline |
| 537 |
def _advance_codepoint(self): |
| 538 |
|
| 539 |
self.pos = next_codepoint_pos(self.text, self.pos) |
| 540 |
self.upos += 1 |
| 541 |
|
| 542 |
|
| 543 |
def check_decoded(space, w_decoded): |
| 544 |
if not space.isinstance_w(w_decoded, space.w_unicode): |
| 545 |
msg = "decoder should return a string result, not '%T'" |
| 546 |
raise oefmt(space.w_TypeError, msg, w_decoded) |
| 547 |
return w_decoded |
| 548 |
|
| 549 |
def unwrap_newline(space, w_newline): |
| 550 |
if space.is_none(w_newline): |
| 551 |
newline = None |
| 552 |
else: |
| 553 |
newline = space.utf8_w(w_newline) |
| 554 |
if newline and newline not in ('\n', '\r\n', '\r'): |
| 555 |
raise oefmt(space.w_ValueError, |
| 556 |
"illegal newline value: %R", w_newline) |
| 557 |
return newline |
| 558 |
|
| 559 |
class W_TextIOWrapper(W_TextIOBase): |
| 560 |
def __init__(self, space): |
| 561 |
W_TextIOBase.__init__(self, space) |
| 562 |
self.state = STATE_ZERO |
| 563 |
self.w_encoder = None |
| 564 |
self.w_decoder = None |
| 565 |
|
| 566 |
self.decoded = DecodeBuffer() |
| 567 |
self.pending_bytes = None |
| 568 |
|
| 569 |
self.pending_bytes_count = 0 |
| 570 |
self.chunk_size = 8192 |
| 571 |
self.b2cratio = 0.0 |
| 572 |
|
| 573 |
self.readuniversal = False |
| 574 |
self.readtranslate = False |
| 575 |
self.readnl = None |
| 576 |
|
| 577 |
self.encodefunc = None |
| 578 |
self.encoding_start_of_stream = False |
| 579 |
|
| 580 |
self.snapshot = None |
| 581 |
|
| 582 |
@unwrap_spec(encoding="text0_or_none", line_buffering=int, write_through=int) |
| 583 |
def descr_init(self, space, w_buffer, encoding=None, |
| 584 |
w_errors=None, w_newline=None, line_buffering=0, |
| 585 |
write_through=0): |
| 586 |
self.state = STATE_ZERO |
| 587 |
self.w_buffer = w_buffer |
| 588 |
self.w_encoding = w_encoding = _determine_encoding(space, encoding, w_buffer) |
| 589 |
|
| 590 |
if space.is_none(w_errors) or w_errors is None: |
| 591 |
w_errors = space.newtext("strict") |
| 592 |
if not space.isinstance_w(w_errors, space.w_text): |
| 593 |
raise oefmt(space.w_TypeError, "TextIOWrapper() argument 'errors' " |
| 594 |
"must be str or None, not %N", w_errors) |
| 595 |
io_check_errors(space, w_errors) |
| 596 |
self.w_errors = w_errors |
| 597 |
|
| 598 |
newline = unwrap_newline(space, w_newline) |
| 599 |
|
| 600 |
self.line_buffering = bool(line_buffering) |
| 601 |
self.write_through = bool(write_through) |
| 602 |
|
| 603 |
self._set_newline(newline) |
| 604 |
|
| 605 |
self._set_encoder_decoder(w_encoding, w_errors) |
| 606 |
|
| 607 |
self.seekable = space.is_true(space.call_method(w_buffer, "seekable")) |
| 608 |
self.telling = self.seekable |
| 609 |
|
| 610 |
self.has_read1 = space.findattr(w_buffer, space.newtext("read1")) |
| 611 |
|
| 612 |
self._fix_encoder_state() |
| 613 |
|
| 614 |
self.state = STATE_OK |
| 615 |
|
| 616 |
def _set_newline(self, newline): |
| 617 |
self.readuniversal = not newline |
| 618 |
self.readtranslate = newline is None |
| 619 |
self.readnl = newline |
| 620 |
|
| 621 |
self.writetranslate = (newline != '') |
| 622 |
if not self.readuniversal: |
| 623 |
self.writenl = self.readnl |
| 624 |
if self.writenl == '\n': |
| 625 |
self.writenl = None |
| 626 |
elif _WINDOWS: |
| 627 |
self.writenl = "\r\n" |
| 628 |
else: |
| 629 |
self.writenl = None |
| 630 |
|
| 631 |
def _set_encoder_decoder(self, w_encoding, w_errors): |
| 632 |
space = self.space |
| 633 |
w_codec = interp_codecs.lookup_codec(space, |
| 634 |
space.text_w(w_encoding)) |
| 635 |
if not space.is_true(space.getattr(w_codec, |
| 636 |
space.newtext('_is_text_encoding'))): |
| 637 |
msg = ("%R is not a text encoding; " |
| 638 |
"use codecs.open() to handle arbitrary codecs") |
| 639 |
raise oefmt(space.w_LookupError, msg, w_encoding) |
| 640 |
|
| 641 |
|
| 642 |
if space.is_true(space.call_method(self.w_buffer, "readable")): |
| 643 |
self.w_decoder = space.call_method(w_codec, |
| 644 |
"incrementaldecoder", w_errors) |
| 645 |
if self.readuniversal: |
| 646 |
self.w_decoder = space.call_function( |
| 647 |
space.gettypeobject(W_IncrementalNewlineDecoder.typedef), |
| 648 |
self.w_decoder, space.newbool(self.readtranslate)) |
| 649 |
|
| 650 |
|
| 651 |
if space.is_true(space.call_method(self.w_buffer, "writable")): |
| 652 |
self.w_encoder = space.call_method(w_codec, |
| 653 |
"incrementalencoder", w_errors) |
| 654 |
|
| 655 |
def _fix_encoder_state(self): |
| 656 |
space = self.space |
| 657 |
self.encoding_start_of_stream = False |
| 658 |
if self.seekable and self.w_encoder: |
| 659 |
self.encoding_start_of_stream = True |
| 660 |
w_cookie = space.call_method(self.w_buffer, "tell") |
| 661 |
if not space.eq_w(w_cookie, space.newint(0)): |
| 662 |
self.encoding_start_of_stream = False |
| 663 |
space.call_method(self.w_encoder, "setstate", space.newint(0)) |
| 664 |
|
| 665 |
def reconfigure(self, space, __args__): |
| 666 |
""" |
| 667 |
Reconfigure the text stream with new parameters. |
| 668 |
|
| 669 |
This also does an implicit stream flush. |
| 670 |
""" |
| 671 |
|
| 672 |
|
| 673 |
args_w, kwargs_w = __args__.unpack() |
| 674 |
if args_w: |
| 675 |
raise oefmt(space.w_TypeError, "reconfigure() takes no positional arguments") |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
w_encoding = kwargs_w.pop("encoding", space.w_None) |
| 680 |
w_errors = kwargs_w.pop("errors", space.w_None) |
| 681 |
w_newline = kwargs_w.pop("newline", None) |
| 682 |
if (not space.is_none(w_encoding) and |
| 683 |
not space.isinstance_w(w_encoding, space.w_text)): |
| 684 |
raise oefmt(space.w_TypeError, |
| 685 |
"reconfigure argument 'encoding' must be str or None, not %T", |
| 686 |
w_encoding) |
| 687 |
if (not space.is_none(w_errors) and |
| 688 |
not space.isinstance_w(w_errors, space.w_text)): |
| 689 |
raise oefmt(space.w_TypeError, |
| 690 |
"reconfigure argument 'errors' must be str or None, not %T", |
| 691 |
w_errors) |
| 692 |
if (not space.is_none(w_newline) and |
| 693 |
not space.isinstance_w(w_newline, space.w_text)): |
| 694 |
raise oefmt(space.w_TypeError, |
| 695 |
"reconfigure argument 'newline' must be str or None, not %T", |
| 696 |
w_newline) |
| 697 |
w_line_buffering = kwargs_w.pop("line_buffering", space.w_None) |
| 698 |
w_write_through = kwargs_w.pop("write_through", space.w_None) |
| 699 |
if kwargs_w: |
| 700 |
key, w_value = kwargs_w.popitem() |
| 701 |
raise oefmt(space.w_TypeError, "%8 is an invalid keyword argument for reconfigure()", key) |
| 702 |
|
| 703 |
if self.decoded.text is not None: |
| 704 |
if (not space.is_none(w_encoding) or |
| 705 |
not space.is_none(w_errors) or |
| 706 |
w_newline is not None): |
| 707 |
self._unsupportedoperation( |
| 708 |
space, "It is not possible to set the encoding " |
| 709 |
"or newline of stream after the first read") |
| 710 |
|
| 711 |
|
| 712 |
newline = None |
| 713 |
if w_newline is not None: |
| 714 |
newline = unwrap_newline(space, w_newline) |
| 715 |
|
| 716 |
line_buffering = self.line_buffering |
| 717 |
if not space.is_none(w_line_buffering): |
| 718 |
line_buffering = bool(space.int_w(w_line_buffering)) |
| 719 |
write_through = self.write_through |
| 720 |
if not space.is_none(w_write_through): |
| 721 |
write_through = bool(space.int_w(w_write_through)) |
| 722 |
|
| 723 |
space.call_method(self, "flush") |
| 724 |
if w_newline is not None: |
| 725 |
self._set_newline(newline) |
| 726 |
if not space.is_none(w_errors): |
| 727 |
io_check_errors(space, w_errors) |
| 728 |
|
| 729 |
if not space.is_none(w_encoding): |
| 730 |
if space.is_none(w_errors): |
| 731 |
w_errors = space.newtext("strict") |
| 732 |
if not space.is_none(w_encoding) or not space.is_none(w_errors) or (w_newline is not None and self.readuniversal): |
| 733 |
if space.is_none(w_encoding): |
| 734 |
w_encoding = self.w_encoding |
| 735 |
elif space.eq_w(w_encoding, space.newtext("locale")): |
| 736 |
w_encoding = space.newtext("utf-8") |
| 737 |
if space.is_none(w_errors): |
| 738 |
w_errors = self.w_errors |
| 739 |
|
| 740 |
|
| 741 |
self._set_encoder_decoder(w_encoding, w_errors) |
| 742 |
self.w_encoding = w_encoding |
| 743 |
self.w_errors = w_errors |
| 744 |
|
| 745 |
self.line_buffering = line_buffering |
| 746 |
self.write_through = write_through |
| 747 |
|
| 748 |
self._fix_encoder_state() |
| 749 |
self.b2cratio = 0.0 |
| 750 |
|
| 751 |
def _check_init(self, space): |
| 752 |
if self.state == STATE_ZERO: |
| 753 |
raise oefmt(space.w_ValueError, |
| 754 |
"I/O operation on uninitialized object") |
| 755 |
|
| 756 |
def _check_attached(self, space): |
| 757 |
if jit.promote(self.state) == STATE_DETACHED: |
| 758 |
raise oefmt(space.w_ValueError, |
| 759 |
"underlying buffer has been detached") |
| 760 |
self._check_init(space) |
| 761 |
|
| 762 |
def _check_closed(self, space, message=None): |
| 763 |
self._check_init(space) |
| 764 |
W_TextIOBase._check_closed(self, space, message) |
| 765 |
|
| 766 |
def __w_attr_repr(self, space, name): |
| 767 |
w_attr = space.findattr(self, space.newtext(name)) |
| 768 |
if w_attr is None: |
| 769 |
return space.newtext("") |
| 770 |
return space.mod(space.newtext("%s=%%r " % name), w_attr) |
| 771 |
|
| 772 |
def descr_repr(self, space): |
| 773 |
self._check_init(space) |
| 774 |
w_args = space.newtuple([self.__w_attr_repr(space, 'name'), |
| 775 |
self.__w_attr_repr(space, 'mode'), |
| 776 |
self.w_encoding]) |
| 777 |
return space.mod( |
| 778 |
space.newtext("<_io.TextIOWrapper %s%sencoding=%r>"), w_args |
| 779 |
) |
| 780 |
|
| 781 |
def readable_w(self, space): |
| 782 |
self._check_attached(space) |
| 783 |
return space.call_method(self.w_buffer, "readable") |
| 784 |
|
| 785 |
def writable_w(self, space): |
| 786 |
self._check_attached(space) |
| 787 |
return space.call_method(self.w_buffer, "writable") |
| 788 |
|
| 789 |
def seekable_w(self, space): |
| 790 |
self._check_attached(space) |
| 791 |
return space.call_method(self.w_buffer, "seekable") |
| 792 |
|
| 793 |
def isatty_w(self, space): |
| 794 |
self._check_attached(space) |
| 795 |
return space.call_method(self.w_buffer, "isatty") |
| 796 |
|
| 797 |
def fileno_w(self, space): |
| 798 |
self._check_attached(space) |
| 799 |
return space.call_method(self.w_buffer, "fileno") |
| 800 |
|
| 801 |
def closed_get_w(self, space): |
| 802 |
self._check_attached(space) |
| 803 |
return space.getattr(self.w_buffer, space.newtext("closed")) |
| 804 |
|
| 805 |
def newlines_get_w(self, space): |
| 806 |
self._check_attached(space) |
| 807 |
if self.w_decoder is None: |
| 808 |
return space.w_None |
| 809 |
return space.findattr(self.w_decoder, space.newtext("newlines")) |
| 810 |
|
| 811 |
def name_get_w(self, space): |
| 812 |
self._check_attached(space) |
| 813 |
return space.getattr(self.w_buffer, space.newtext("name")) |
| 814 |
|
| 815 |
def flush_w(self, space): |
| 816 |
self._check_attached(space) |
| 817 |
self._check_closed(space) |
| 818 |
self.telling = self.seekable |
| 819 |
self._writeflush(space) |
| 820 |
space.call_method(self.w_buffer, "flush") |
| 821 |
|
| 822 |
@unwrap_spec(w_pos = WrappedDefault(None)) |
| 823 |
def truncate_w(self, space, w_pos=None): |
| 824 |
self._check_attached(space) |
| 825 |
|
| 826 |
space.call_method(self, "flush") |
| 827 |
return space.call_method(self.w_buffer, "truncate", w_pos) |
| 828 |
|
| 829 |
def close_w(self, space): |
| 830 |
self._check_attached(space) |
| 831 |
if space.is_true(space.getattr(self.w_buffer, |
| 832 |
space.newtext("closed"))): |
| 833 |
return |
| 834 |
try: |
| 835 |
space.call_method(self, "flush") |
| 836 |
except OperationError as e: |
| 837 |
try: |
| 838 |
ret = space.call_method(self.w_buffer, "close") |
| 839 |
except OperationError as e2: |
| 840 |
e2.chain_exceptions(space, e) |
| 841 |
raise |
| 842 |
else: |
| 843 |
ret = space.call_method(self.w_buffer, "close") |
| 844 |
self.maybe_unregister_rpython_finalizer_io(space) |
| 845 |
return ret |
| 846 |
|
| 847 |
def _dealloc_warn_w(self, space, w_source): |
| 848 |
space.call_method(self.w_buffer, "_dealloc_warn", w_source) |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
def _read_chunk(self, space, size_hint): |
| 854 |
"""Read and decode the next chunk of data from the BufferedReader. |
| 855 |
The return value is True unless EOF was reached. The decoded string |
| 856 |
is placed in self.decoded (replacing its previous value). |
| 857 |
The entire input chunk is sent to the decoder, though some of it may |
| 858 |
remain buffered in the decoder, yet to be converted.""" |
| 859 |
|
| 860 |
if not self.w_decoder: |
| 861 |
self._unsupportedoperation(space, "not readable") |
| 862 |
|
| 863 |
if self.telling: |
| 864 |
|
| 865 |
|
| 866 |
w_decoder = self.w_decoder |
| 867 |
|
| 868 |
|
| 869 |
if type(w_decoder) is W_IncrementalNewlineDecoder: |
| 870 |
w_dec_buffer, dec_flags = w_decoder.getstate_u(space) |
| 871 |
else: |
| 872 |
w_state = space.call_method(self.w_decoder, "getstate") |
| 873 |
if (not space.isinstance_w(w_state, space.w_tuple) |
| 874 |
or space.len_w(w_state) != 2): |
| 875 |
raise oefmt(space.w_TypeError, "illegal decoder state") |
| 876 |
|
| 877 |
|
| 878 |
w_dec_buffer, w_dec_flags = space.unpackiterable(w_state, 2) |
| 879 |
if not space.isinstance_w(w_dec_buffer, space.w_bytes): |
| 880 |
msg = ("illegal decoder state: the first value should be a " |
| 881 |
"bytes object not '%T'") |
| 882 |
raise oefmt(space.w_TypeError, msg, w_dec_buffer) |
| 883 |
dec_flags = space.int_w(w_dec_flags) |
| 884 |
dec_buffer = space.bytes_w(w_dec_buffer) |
| 885 |
else: |
| 886 |
dec_buffer = None |
| 887 |
dec_flags = 0 |
| 888 |
|
| 889 |
|
| 890 |
if size_hint > 0: |
| 891 |
size_hint = int(max(self.b2cratio, 1.0) * float(size_hint)) |
| 892 |
chunk_size = max(self.chunk_size, size_hint) |
| 893 |
func_name = "read1" if self.has_read1 else "read" |
| 894 |
w_input = space.call_method(self.w_buffer, func_name, |
| 895 |
space.newint(chunk_size)) |
| 896 |
|
| 897 |
try: |
| 898 |
input_buf = w_input.buffer_w(space, space.BUF_SIMPLE) |
| 899 |
except BufferInterfaceNotFound: |
| 900 |
msg = ("underlying %s() should have returned a bytes-like " |
| 901 |
"object, not '%T'") |
| 902 |
raise oefmt(space.w_TypeError, msg, func_name, w_input) |
| 903 |
|
| 904 |
nbytes = input_buf.getlength() |
| 905 |
eof = nbytes == 0 |
| 906 |
w_decoder = self.w_decoder |
| 907 |
if type(w_decoder) is W_IncrementalNewlineDecoder: |
| 908 |
w_decoded = w_decoder.decode_w(space, w_input, eof) |
| 909 |
else: |
| 910 |
w_decoded = space.call_method(w_decoder, "decode", |
| 911 |
w_input, space.newbool(eof)) |
| 912 |
self.decoded.set(space, w_decoded) |
| 913 |
nchars = space.len_w(w_decoded) |
| 914 |
if nchars > 0: |
| 915 |
eof = False |
| 916 |
self.b2cratio = float(nbytes) / float(nchars) |
| 917 |
else: |
| 918 |
self.b2cratio = 0.0 |
| 919 |
|
| 920 |
if self.telling: |
| 921 |
|
| 922 |
|
| 923 |
next_input = dec_buffer + input_buf.as_str() |
| 924 |
self.snapshot = PositionSnapshot(dec_flags, next_input) |
| 925 |
|
| 926 |
return not eof |
| 927 |
|
| 928 |
def _ensure_data(self, space, size_hint): |
| 929 |
while not self.decoded.has_data(): |
| 930 |
try: |
| 931 |
if not self._read_chunk(space, size_hint): |
| 932 |
self.decoded.reset() |
| 933 |
self.snapshot = None |
| 934 |
return False |
| 935 |
except OperationError as e: |
| 936 |
if trap_eintr(space, e): |
| 937 |
continue |
| 938 |
raise |
| 939 |
return True |
| 940 |
|
| 941 |
def next_w(self, space): |
| 942 |
self._check_attached(space) |
| 943 |
self.telling = False |
| 944 |
try: |
| 945 |
return W_TextIOBase.next_w(self, space) |
| 946 |
except OperationError as e: |
| 947 |
if e.match(space, space.w_StopIteration): |
| 948 |
self.telling = self.seekable |
| 949 |
raise |
| 950 |
|
| 951 |
def read_w(self, space, w_size=None): |
| 952 |
self._check_attached(space) |
| 953 |
self._check_closed(space) |
| 954 |
if not self.w_decoder: |
| 955 |
self._unsupportedoperation(space, "not readable") |
| 956 |
|
| 957 |
size = convert_size(space, w_size) |
| 958 |
self._writeflush(space) |
| 959 |
|
| 960 |
if size < 0: |
| 961 |
return self._read_all(space) |
| 962 |
else: |
| 963 |
return self._read(space, size) |
| 964 |
|
| 965 |
def _read_all(self, space): |
| 966 |
w_bytes = space.call_method(self.w_buffer, "read") |
| 967 |
w_decoded = space.call_method(self.w_decoder, "decode", w_bytes, space.w_True) |
| 968 |
check_decoded(space, w_decoded) |
| 969 |
w_result = space.newutf8(*self.decoded.get_chars(-1)) |
| 970 |
w_final = space.add(w_result, w_decoded) |
| 971 |
if self.snapshot: |
| 972 |
|
| 973 |
self.decoded.reset() |
| 974 |
self.snapshot = None |
| 975 |
return w_final |
| 976 |
|
| 977 |
def _read(self, space, size): |
| 978 |
remaining = size |
| 979 |
builder = Utf8StringBuilder(size) |
| 980 |
|
| 981 |
|
| 982 |
while remaining > 0: |
| 983 |
if not self._ensure_data(space, remaining): |
| 984 |
break |
| 985 |
data, size = self.decoded.get_chars(remaining) |
| 986 |
builder.append_utf8(data, size) |
| 987 |
remaining -= size |
| 988 |
|
| 989 |
return space.newutf8(builder.build(), builder.getlength()) |
| 990 |
|
| 991 |
def _scan_line_ending(self, limit): |
| 992 |
if self.readtranslate: |
| 993 |
|
| 994 |
return self.decoded.find_char('\n', limit) |
| 995 |
if self.readuniversal: |
| 996 |
return self.decoded.find_newline_universal(limit) |
| 997 |
else: |
| 998 |
|
| 999 |
newline = self.readnl |
| 1000 |
if newline == '\r\n': |
| 1001 |
return self.decoded.find_crlf(limit) |
| 1002 |
else: |
| 1003 |
return self.decoded.find_char(newline[0], limit) |
| 1004 |
|
| 1005 |
def readline_w(self, space, w_limit=None): |
| 1006 |
self._check_attached(space) |
| 1007 |
self._check_closed(space) |
| 1008 |
self._writeflush(space) |
| 1009 |
if space.is_w(w_limit, space.w_None): |
| 1010 |
raise oefmt(space.w_TypeError, |
| 1011 |
"'NoneType' object cannot be interpreted as an integer") |
| 1012 |
limit = convert_size(space, w_limit) |
| 1013 |
text, lgt = self._readline(space, limit) |
| 1014 |
return space.newutf8(text, lgt) |
| 1015 |
|
| 1016 |
def _readline(self, space, limit): |
| 1017 |
|
| 1018 |
remnant = None |
| 1019 |
remnant_ulen = -1 |
| 1020 |
builder = Utf8StringBuilder() |
| 1021 |
while True: |
| 1022 |
|
| 1023 |
has_data = self._ensure_data(space, 0) |
| 1024 |
if not has_data: |
| 1025 |
|
| 1026 |
if remnant: |
| 1027 |
builder.append_utf8(remnant, remnant_ulen) |
| 1028 |
break |
| 1029 |
|
| 1030 |
if remnant: |
| 1031 |
assert not self.readtranslate and self.readnl == '\r\n' |
| 1032 |
assert self.decoded.pos == 0 |
| 1033 |
if remnant == '\r' and self.decoded.text[0] == '\n': |
| 1034 |
builder.append_utf8('\r\n', 2) |
| 1035 |
self.decoded.pos = 1 |
| 1036 |
self.decoded.upos = 1 |
| 1037 |
remnant = None |
| 1038 |
remnant_ulen = -1 |
| 1039 |
break |
| 1040 |
else: |
| 1041 |
builder.append_utf8(remnant, remnant_ulen) |
| 1042 |
remnant = None |
| 1043 |
remnant_ulen = -1 |
| 1044 |
continue |
| 1045 |
|
| 1046 |
if limit >= 0: |
| 1047 |
remaining = limit - builder.getlength() |
| 1048 |
assert remaining >= 0 |
| 1049 |
else: |
| 1050 |
remaining = -1 |
| 1051 |
start = self.decoded.pos |
| 1052 |
ustart = self.decoded.upos |
| 1053 |
assert start >= 0 |
| 1054 |
found = self._scan_line_ending(remaining) |
| 1055 |
end_scan = self.decoded.pos |
| 1056 |
uend_scan = self.decoded.upos |
| 1057 |
if end_scan > start: |
| 1058 |
builder.append_utf8_slice(self.decoded.text, start, end_scan, uend_scan - ustart) |
| 1059 |
|
| 1060 |
if found or (limit >= 0 and builder.getlength() >= limit): |
| 1061 |
break |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
if not self.decoded.exhausted(): |
| 1066 |
remnant, remnant_ulen = self.decoded.get_chars(-1) |
| 1067 |
|
| 1068 |
self.decoded.reset() |
| 1069 |
|
| 1070 |
result = builder.build() |
| 1071 |
lgt = builder.getlength() |
| 1072 |
return (result, lgt) |
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
def write_w(self, space, w_text): |
| 1078 |
self._check_attached(space) |
| 1079 |
self._check_closed(space) |
| 1080 |
|
| 1081 |
if not self.w_encoder: |
| 1082 |
self._unsupportedoperation(space, "not writable") |
| 1083 |
|
| 1084 |
if not space.isinstance_w(w_text, space.w_unicode): |
| 1085 |
raise oefmt(space.w_TypeError, |
| 1086 |
"unicode argument expected, got '%T'", w_text) |
| 1087 |
|
| 1088 |
text, textlen = space.utf8_len_w(w_text) |
| 1089 |
|
| 1090 |
haslf = False |
| 1091 |
if (self.writetranslate and self.writenl) or self.line_buffering: |
| 1092 |
if text.find('\n') >= 0: |
| 1093 |
haslf = True |
| 1094 |
if haslf and self.writetranslate and self.writenl: |
| 1095 |
w_text = space.call_method(w_text, "replace", space.newutf8('\n', 1), |
| 1096 |
space.newutf8(self.writenl, codepoints_in_utf8(self.writenl))) |
| 1097 |
text = space.utf8_w(w_text) |
| 1098 |
|
| 1099 |
needflush = False |
| 1100 |
text_needflush = False |
| 1101 |
if self.write_through: |
| 1102 |
text_needflush = True |
| 1103 |
if self.line_buffering and (haslf or text.find('\r') >= 0): |
| 1104 |
needflush = True |
| 1105 |
|
| 1106 |
|
| 1107 |
if self.encodefunc: |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
w_bytes = self.encodefunc(space, w_text, self.errors) |
| 1114 |
self.encoding_start_of_stream = False |
| 1115 |
else: |
| 1116 |
w_bytes = space.call_method(self.w_encoder, "encode", w_text) |
| 1117 |
|
| 1118 |
b = space.bytes_w(w_bytes) |
| 1119 |
|
| 1120 |
if len(b) >= self.chunk_size and self.pending_bytes: |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
self._writeflush_loop(space) |
| 1128 |
|
| 1129 |
if not self.pending_bytes: |
| 1130 |
self.pending_bytes = [b] |
| 1131 |
else: |
| 1132 |
self.pending_bytes.append(b) |
| 1133 |
|
| 1134 |
self.pending_bytes_count += len(b) |
| 1135 |
|
| 1136 |
if (self.pending_bytes_count >= self.chunk_size or |
| 1137 |
needflush or text_needflush): |
| 1138 |
self._writeflush(space) |
| 1139 |
|
| 1140 |
if needflush: |
| 1141 |
space.call_method(self.w_buffer, "flush") |
| 1142 |
|
| 1143 |
if self.snapshot: |
| 1144 |
|
| 1145 |
self.decoded.reset() |
| 1146 |
self.snapshot = None |
| 1147 |
|
| 1148 |
if self.w_decoder: |
| 1149 |
space.call_method(self.w_decoder, "reset") |
| 1150 |
|
| 1151 |
return space.newint(textlen) |
| 1152 |
|
| 1153 |
def _writeflush_loop(self, space): |
| 1154 |
|
| 1155 |
while self.pending_bytes: |
| 1156 |
self._writeflush(space) |
| 1157 |
|
| 1158 |
def _writeflush(self, space): |
| 1159 |
|
| 1160 |
if not self.pending_bytes: |
| 1161 |
return |
| 1162 |
|
| 1163 |
self._really_flush(space) |
| 1164 |
|
| 1165 |
def _really_flush(self, space): |
| 1166 |
pending_bytes = ''.join(self.pending_bytes) |
| 1167 |
self.pending_bytes = None |
| 1168 |
self.pending_bytes_count = 0 |
| 1169 |
|
| 1170 |
while True: |
| 1171 |
try: |
| 1172 |
space.call_method(self.w_buffer, "write", |
| 1173 |
space.newbytes(pending_bytes)) |
| 1174 |
except OperationError as e: |
| 1175 |
if trap_eintr(space, e): |
| 1176 |
continue |
| 1177 |
raise |
| 1178 |
else: |
| 1179 |
break |
| 1180 |
|
| 1181 |
def detach_w(self, space): |
| 1182 |
self._check_attached(space) |
| 1183 |
space.call_method(self, "flush") |
| 1184 |
w_buffer = self.w_buffer |
| 1185 |
self.w_buffer = None |
| 1186 |
self.state = STATE_DETACHED |
| 1187 |
return w_buffer |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
def _decoder_setstate(self, space, cookie): |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
if cookie.start_pos == 0 and cookie.dec_flags == 0: |
| 1199 |
space.call_method(self.w_decoder, "reset") |
| 1200 |
else: |
| 1201 |
space.call_method(self.w_decoder, "setstate", |
| 1202 |
space.newtuple2(space.newbytes(""), |
| 1203 |
space.newint(cookie.dec_flags))) |
| 1204 |
|
| 1205 |
def _encoder_reset(self, space, start_of_stream): |
| 1206 |
if start_of_stream: |
| 1207 |
space.call_method(self.w_encoder, "reset") |
| 1208 |
self.encoding_start_of_stream = True |
| 1209 |
else: |
| 1210 |
space.call_method(self.w_encoder, "setstate", space.newint(0)) |
| 1211 |
self.encoding_start_of_stream = False |
| 1212 |
|
| 1213 |
def _encoder_setstate(self, space, cookie): |
| 1214 |
self._encoder_reset(space, |
| 1215 |
cookie.start_pos == 0 and cookie.dec_flags == 0) |
| 1216 |
|
| 1217 |
@unwrap_spec(whence=int) |
| 1218 |
def seek_w(self, space, w_pos, whence=0): |
| 1219 |
self._check_attached(space) |
| 1220 |
|
| 1221 |
if not self.seekable: |
| 1222 |
self._unsupportedoperation(space, |
| 1223 |
"underlying stream is not seekable") |
| 1224 |
|
| 1225 |
if whence == 1: |
| 1226 |
|
| 1227 |
if not space.eq_w(w_pos, space.newint(0)): |
| 1228 |
self._unsupportedoperation( |
| 1229 |
space, "can't do nonzero cur-relative seeks") |
| 1230 |
|
| 1231 |
|
| 1232 |
w_pos = space.call_method(self, "tell") |
| 1233 |
|
| 1234 |
elif whence == 2: |
| 1235 |
|
| 1236 |
if not space.eq_w(w_pos, space.newint(0)): |
| 1237 |
self._unsupportedoperation( |
| 1238 |
space, "can't do nonzero end-relative seeks") |
| 1239 |
space.call_method(self, "flush") |
| 1240 |
self.decoded.reset() |
| 1241 |
self.snapshot = None |
| 1242 |
if self.w_decoder: |
| 1243 |
space.call_method(self.w_decoder, "reset") |
| 1244 |
w_res = space.call_method(self.w_buffer, "seek", |
| 1245 |
w_pos, space.newint(whence)) |
| 1246 |
if self.w_encoder: |
| 1247 |
|
| 1248 |
start_of_stream = space.eq_w(w_res, space.newint(0)) |
| 1249 |
self._encoder_reset(space, start_of_stream) |
| 1250 |
return w_res |
| 1251 |
|
| 1252 |
elif whence != 0: |
| 1253 |
raise oefmt(space.w_ValueError, |
| 1254 |
"invalid whence (%d, should be 0, 1 or 2)", |
| 1255 |
whence) |
| 1256 |
|
| 1257 |
if space.is_true(space.lt(w_pos, space.newint(0))): |
| 1258 |
raise oefmt(space.w_ValueError, |
| 1259 |
"negative seek position %R", w_pos) |
| 1260 |
|
| 1261 |
space.call_method(self, "flush") |
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
cookie = PositionCookie(space.bigint_w(w_pos)) |
| 1266 |
|
| 1267 |
|
| 1268 |
space.call_method(self.w_buffer, "seek", space.newint(cookie.start_pos)) |
| 1269 |
|
| 1270 |
self.decoded.reset() |
| 1271 |
self.snapshot = None |
| 1272 |
|
| 1273 |
|
| 1274 |
if self.w_decoder: |
| 1275 |
self._decoder_setstate(space, cookie) |
| 1276 |
|
| 1277 |
if cookie.chars_to_skip: |
| 1278 |
|
| 1279 |
w_chunk = space.call_method(self.w_buffer, "read", |
| 1280 |
space.newint(cookie.bytes_to_feed)) |
| 1281 |
if not space.isinstance_w(w_chunk, space.w_bytes): |
| 1282 |
msg = "underlying read() should have returned " \ |
| 1283 |
"a bytes object, not '%T'" |
| 1284 |
raise oefmt(space.w_TypeError, msg, w_chunk) |
| 1285 |
|
| 1286 |
self.snapshot = PositionSnapshot(cookie.dec_flags, |
| 1287 |
space.bytes_w(w_chunk)) |
| 1288 |
|
| 1289 |
w_decoded = space.call_method(self.w_decoder, "decode", |
| 1290 |
w_chunk, space.newbool(bool(cookie.need_eof))) |
| 1291 |
w_decoded = check_decoded(space, w_decoded) |
| 1292 |
|
| 1293 |
|
| 1294 |
if space.len_w(w_decoded) < cookie.chars_to_skip: |
| 1295 |
raise oefmt(space.w_IOError, |
| 1296 |
"can't restore logical file position") |
| 1297 |
self.decoded.set(space, w_decoded) |
| 1298 |
self.decoded.pos = w_decoded._index_to_byte(cookie.chars_to_skip) |
| 1299 |
self.decoded.upos = cookie.chars_to_skip |
| 1300 |
else: |
| 1301 |
self.snapshot = PositionSnapshot(cookie.dec_flags, "") |
| 1302 |
|
| 1303 |
|
| 1304 |
if self.w_encoder: |
| 1305 |
self._encoder_setstate(space, cookie) |
| 1306 |
|
| 1307 |
return w_pos |
| 1308 |
|
| 1309 |
def tell_w(self, space): |
| 1310 |
self._check_closed(space) |
| 1311 |
if not self.seekable: |
| 1312 |
self._unsupportedoperation(space, |
| 1313 |
"underlying stream is not seekable") |
| 1314 |
if not self.telling: |
| 1315 |
raise oefmt(space.w_IOError, |
| 1316 |
"telling position disabled by next() call") |
| 1317 |
|
| 1318 |
self._writeflush(space) |
| 1319 |
space.call_method(self, "flush") |
| 1320 |
|
| 1321 |
w_pos = space.call_method(self.w_buffer, "tell") |
| 1322 |
|
| 1323 |
if self.w_decoder is None or self.snapshot is None: |
| 1324 |
assert not self.decoded.text |
| 1325 |
return w_pos |
| 1326 |
|
| 1327 |
cookie = PositionCookie(space.bigint_w(w_pos)) |
| 1328 |
|
| 1329 |
|
| 1330 |
cookie.dec_flags = self.snapshot.flags |
| 1331 |
input = self.snapshot.input |
| 1332 |
if len(input) > cookie.start_pos: |
| 1333 |
|
| 1334 |
|
| 1335 |
return space.newint(0) |
| 1336 |
cookie.start_pos -= len(input) |
| 1337 |
|
| 1338 |
|
| 1339 |
if self.decoded.pos == 0: |
| 1340 |
|
| 1341 |
return space.newlong_from_rbigint(cookie.pack()) |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
chars_to_skip = self.decoded.upos |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
w_saved_state = space.call_method(self.w_decoder, "getstate") |
| 1350 |
|
| 1351 |
skip_bytes = 0 |
| 1352 |
|
| 1353 |
|
| 1354 |
skip_bytes = int(self.b2cratio * chars_to_skip) |
| 1355 |
skip_back = 1; |
| 1356 |
|
| 1357 |
|
| 1358 |
while skip_bytes > 0: |
| 1359 |
|
| 1360 |
self._decoder_setstate(space, cookie) |
| 1361 |
|
| 1362 |
w_decoded = space.call_method(self.w_decoder, "decode", |
| 1363 |
space.newbytes(input[0:skip_bytes]) |
| 1364 |
) |
| 1365 |
check_decoded(space, w_decoded) |
| 1366 |
chars_decoded = space.len_w(w_decoded) |
| 1367 |
if chars_decoded <= chars_to_skip: |
| 1368 |
w_state = space.call_method(self.w_decoder, "getstate") |
| 1369 |
w_dec_buffer, w_flags = space.unpackiterable(w_state, 2) |
| 1370 |
dec_buffer_len = space.len_w(w_dec_buffer) |
| 1371 |
if (dec_buffer_len == 0): |
| 1372 |
|
| 1373 |
cookie.dec_flags = space.int_w(w_flags) |
| 1374 |
chars_to_skip -= chars_decoded |
| 1375 |
break |
| 1376 |
|
| 1377 |
skip_bytes -= dec_buffer_len; |
| 1378 |
skip_back = 1; |
| 1379 |
else: |
| 1380 |
|
| 1381 |
skip_bytes -= skip_back; |
| 1382 |
skip_back *= 2; |
| 1383 |
if skip_bytes <= 0: |
| 1384 |
skip_bytes = 0 |
| 1385 |
self._decoder_setstate(space, cookie) |
| 1386 |
cookie.start_pos += skip_bytes |
| 1387 |
cookie.chars_to_skip = chars_to_skip |
| 1388 |
if chars_to_skip == 0: |
| 1389 |
space.call_method(self.w_decoder, "setstate", w_saved_state) |
| 1390 |
return space.newlong_from_rbigint(cookie.pack()) |
| 1391 |
try: |
| 1392 |
|
| 1393 |
self._decoder_setstate(space, cookie) |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
chars_decoded = 0 |
| 1401 |
i = skip_bytes |
| 1402 |
while i < len(input): |
| 1403 |
w_decoded = space.call_method(self.w_decoder, "decode", |
| 1404 |
space.newbytes(input[i])) |
| 1405 |
check_decoded(space, w_decoded) |
| 1406 |
chars_decoded += space.len_w(w_decoded) |
| 1407 |
|
| 1408 |
cookie.bytes_to_feed += 1 |
| 1409 |
|
| 1410 |
w_state = space.call_method(self.w_decoder, "getstate") |
| 1411 |
w_dec_buffer, w_flags = space.unpackiterable(w_state, 2) |
| 1412 |
dec_buffer_len = space.len_w(w_dec_buffer) |
| 1413 |
|
| 1414 |
if dec_buffer_len == 0 and chars_decoded <= chars_to_skip: |
| 1415 |
|
| 1416 |
cookie.start_pos += cookie.bytes_to_feed |
| 1417 |
chars_to_skip -= chars_decoded |
| 1418 |
assert chars_to_skip >= 0 |
| 1419 |
cookie.dec_flags = space.int_w(w_flags) |
| 1420 |
cookie.bytes_to_feed = 0 |
| 1421 |
chars_decoded = 0 |
| 1422 |
if chars_decoded >= chars_to_skip: |
| 1423 |
break |
| 1424 |
i += 1 |
| 1425 |
else: |
| 1426 |
|
| 1427 |
w_decoded = space.call_method(self.w_decoder, "decode", |
| 1428 |
space.newbytes(""), |
| 1429 |
space.newint(1)) |
| 1430 |
check_decoded(space, w_decoded) |
| 1431 |
chars_decoded += space.len_w(w_decoded) |
| 1432 |
cookie.need_eof = 1 |
| 1433 |
|
| 1434 |
if chars_decoded < chars_to_skip: |
| 1435 |
raise oefmt(space.w_IOError, |
| 1436 |
"can't reconstruct logical file position") |
| 1437 |
finally: |
| 1438 |
space.call_method(self.w_decoder, "setstate", w_saved_state) |
| 1439 |
|
| 1440 |
|
| 1441 |
cookie.chars_to_skip = chars_to_skip |
| 1442 |
return space.newlong_from_rbigint(cookie.pack()) |
| 1443 |
|
| 1444 |
def chunk_size_get_w(self, space): |
| 1445 |
self._check_attached(space) |
| 1446 |
return space.newint(self.chunk_size) |
| 1447 |
|
| 1448 |
def chunk_size_set_w(self, space, w_size): |
| 1449 |
self._check_attached(space) |
| 1450 |
size = space.int_w(w_size) |
| 1451 |
if size <= 0: |
| 1452 |
raise oefmt(space.w_ValueError, |
| 1453 |
"a strictly positive integer is required") |
| 1454 |
self.chunk_size = size |
| 1455 |
|
| 1456 |
W_TextIOWrapper.typedef = TypeDef( |
| 1457 |
'_io.TextIOWrapper', W_TextIOBase.typedef, |
| 1458 |
__new__ = generic_new_descr(W_TextIOWrapper), |
| 1459 |
__init__ = interp2app(W_TextIOWrapper.descr_init), |
| 1460 |
__repr__ = interp2app(W_TextIOWrapper.descr_repr), |
| 1461 |
__next__ = interp2app(W_TextIOWrapper.next_w), |
| 1462 |
__getstate__ = interp2app(W_TextIOWrapper.getstate_w), |
| 1463 |
|
| 1464 |
read = interp2app(W_TextIOWrapper.read_w), |
| 1465 |
readline = interp2app(W_TextIOWrapper.readline_w), |
| 1466 |
write = interp2app(W_TextIOWrapper.write_w), |
| 1467 |
seek = interp2app(W_TextIOWrapper.seek_w), |
| 1468 |
tell = interp2app(W_TextIOWrapper.tell_w), |
| 1469 |
detach = interp2app(W_TextIOWrapper.detach_w), |
| 1470 |
flush = interp2app(W_TextIOWrapper.flush_w), |
| 1471 |
truncate = interp2app(W_TextIOWrapper.truncate_w), |
| 1472 |
close = interp2app(W_TextIOWrapper.close_w), |
| 1473 |
|
| 1474 |
reconfigure = interp2app(W_TextIOWrapper.reconfigure), |
| 1475 |
|
| 1476 |
line_buffering = interp_attrproperty("line_buffering", W_TextIOWrapper, |
| 1477 |
wrapfn="newbool"), |
| 1478 |
write_through = interp_attrproperty("write_through", W_TextIOWrapper, |
| 1479 |
wrapfn="newbool"), |
| 1480 |
readable = interp2app(W_TextIOWrapper.readable_w), |
| 1481 |
writable = interp2app(W_TextIOWrapper.writable_w), |
| 1482 |
seekable = interp2app(W_TextIOWrapper.seekable_w), |
| 1483 |
isatty = interp2app(W_TextIOWrapper.isatty_w), |
| 1484 |
fileno = interp2app(W_TextIOWrapper.fileno_w), |
| 1485 |
_dealloc_warn = interp2app(W_TextIOWrapper._dealloc_warn_w), |
| 1486 |
name = GetSetProperty(W_TextIOWrapper.name_get_w), |
| 1487 |
buffer = interp_attrproperty_w("w_buffer", cls=W_TextIOWrapper), |
| 1488 |
closed = GetSetProperty(W_TextIOWrapper.closed_get_w), |
| 1489 |
errors = interp_attrproperty_w("w_errors", cls=W_TextIOWrapper), |
| 1490 |
newlines = GetSetProperty(W_TextIOWrapper.newlines_get_w), |
| 1491 |
_CHUNK_SIZE = GetSetProperty( |
| 1492 |
W_TextIOWrapper.chunk_size_get_w, W_TextIOWrapper.chunk_size_set_w |
| 1493 |
), |
| 1494 |
) |