| 1 |
import os |
| 2 |
import sys |
| 3 |
|
| 4 |
from pypy.interpreter.error import OperationError, oefmt |
| 5 |
from pypy.interpreter.gateway import interp2app, unwrap_spec |
| 6 |
from pypy.interpreter.typedef import ( |
| 7 |
TypeDef, interp_attrproperty, generic_new_descr) |
| 8 |
from pypy.module._io.interp_fileio import W_FileIO |
| 9 |
from pypy.module._io.interp_textio import W_TextIOWrapper |
| 10 |
from pypy.module.posix import interp_posix |
| 11 |
from rpython.rlib import jit |
| 12 |
|
| 13 |
_WIN32 = sys.platform == 'win32' |
| 14 |
|
| 15 |
class Cache: |
| 16 |
def __init__(self, space): |
| 17 |
self.w_unsupportedoperation = space.new_exception_class( |
| 18 |
"io.UnsupportedOperation", |
| 19 |
space.newtuple2(space.w_ValueError, space.w_IOError)) |
| 20 |
|
| 21 |
@unwrap_spec(mode='text', buffering=int, |
| 22 |
encoding="text_or_none", errors="text_or_none", |
| 23 |
newline="text_or_none", closefd=int) |
| 24 |
def open(space, w_file, mode="r", buffering=-1, encoding=None, errors=None, |
| 25 |
newline=None, closefd=True, w_opener=None): |
| 26 |
return _open(space, w_file, mode, buffering, encoding, errors, newline, |
| 27 |
closefd, w_opener) |
| 28 |
|
| 29 |
@jit.look_inside_iff(lambda space, w_file, mode, buffering, encoding, errors, |
| 30 |
newlines, closefd, w_opener: jit.isconstant(mode)) |
| 31 |
def _open(space, w_file, mode, buffering, encoding, errors, newline, closefd, |
| 32 |
w_opener): |
| 33 |
from pypy.module._io.interp_bufferedio import (W_BufferedRandom, |
| 34 |
W_BufferedWriter, W_BufferedReader) |
| 35 |
|
| 36 |
if not (space.isinstance_w(w_file, space.w_unicode) or |
| 37 |
space.isinstance_w(w_file, space.w_bytes) or |
| 38 |
space.isinstance_w(w_file, space.w_int)): |
| 39 |
w_file = interp_posix.fspath(space, w_file) |
| 40 |
|
| 41 |
reading = writing = creating = appending = updating = text = binary = False |
| 42 |
|
| 43 |
for i in range(1, len(mode)): |
| 44 |
flag = mode[i] |
| 45 |
if mode.find(flag, 0, i) != -1: |
| 46 |
raise oefmt(space.w_ValueError, "invalid mode: %s", mode) |
| 47 |
|
| 48 |
for flag in mode: |
| 49 |
if flag == "r": |
| 50 |
reading = True |
| 51 |
elif flag == "w": |
| 52 |
writing = True |
| 53 |
elif flag == "x": |
| 54 |
creating = True |
| 55 |
elif flag == "a": |
| 56 |
appending = True |
| 57 |
elif flag == "+": |
| 58 |
updating = True |
| 59 |
elif flag == "t": |
| 60 |
text = True |
| 61 |
elif flag == "b": |
| 62 |
binary = True |
| 63 |
else: |
| 64 |
raise oefmt(space.w_ValueError, "invalid mode: %s", mode) |
| 65 |
|
| 66 |
|
| 67 |
if text and binary: |
| 68 |
raise oefmt(space.w_ValueError, |
| 69 |
"can't have text and binary mode at once") |
| 70 |
if creating + reading + writing + appending > 1: |
| 71 |
raise oefmt(space.w_ValueError, |
| 72 |
"must have exactly one of create/read/write/append mode") |
| 73 |
if binary and encoding is not None: |
| 74 |
raise oefmt(space.w_ValueError, |
| 75 |
"binary mode doesn't take an encoding argument") |
| 76 |
if binary and newline is not None: |
| 77 |
raise oefmt(space.w_ValueError, |
| 78 |
"binary mode doesn't take a newline argument") |
| 79 |
if binary and buffering == 1: |
| 80 |
space.warn( |
| 81 |
space.newtext( |
| 82 |
"line buffering (buffering=1) isn't supported in " |
| 83 |
"binary mode, the default buffer size will be used" |
| 84 |
), space.w_RuntimeWarning |
| 85 |
) |
| 86 |
|
| 87 |
rawmode = "" |
| 88 |
if reading: |
| 89 |
rawmode = "r" |
| 90 |
if updating: |
| 91 |
rawmode = "r+" |
| 92 |
elif writing: |
| 93 |
rawmode = "w" |
| 94 |
if updating: |
| 95 |
rawmode = "w+" |
| 96 |
elif creating: |
| 97 |
rawmode = "x" |
| 98 |
if updating: |
| 99 |
rawmode = "x+" |
| 100 |
elif appending: |
| 101 |
rawmode = "a" |
| 102 |
if updating: |
| 103 |
rawmode = "a+" |
| 104 |
else: |
| 105 |
|
| 106 |
if updating: |
| 107 |
rawmode = "+" |
| 108 |
|
| 109 |
w_result = None |
| 110 |
try: |
| 111 |
rawclass = W_FileIO |
| 112 |
if _WIN32: |
| 113 |
from pypy.module._io.interp_win32consoleio import W_WinConsoleIO, _pyio_get_console_type |
| 114 |
typ = _pyio_get_console_type(space, w_file) |
| 115 |
if typ != '\0': |
| 116 |
rawclass = W_WinConsoleIO |
| 117 |
encoding = "utf-8" |
| 118 |
w_raw = space.call_function( |
| 119 |
space.gettypefor(rawclass), w_file, space.newtext(rawmode), |
| 120 |
space.newbool(bool(closefd)), w_opener) |
| 121 |
else: |
| 122 |
w_raw = W_FileIO(space) |
| 123 |
w_raw.descr_init(space, w_file, rawmode, bool(closefd), w_opener) |
| 124 |
|
| 125 |
w_result = w_raw |
| 126 |
|
| 127 |
isatty = space.is_true(space.call_method(w_raw, "isatty")) |
| 128 |
line_buffering = buffering == 1 or (buffering < 0 and isatty) |
| 129 |
if line_buffering: |
| 130 |
buffering = -1 |
| 131 |
|
| 132 |
if buffering < 0: |
| 133 |
buffering = space.c_int_w(space.getattr( |
| 134 |
w_raw, space.newtext("_blksize"))) |
| 135 |
|
| 136 |
if buffering < 0: |
| 137 |
raise oefmt(space.w_ValueError, "invalid buffering size") |
| 138 |
|
| 139 |
if buffering == 0: |
| 140 |
if not binary: |
| 141 |
raise oefmt(space.w_ValueError, |
| 142 |
"can't have unbuffered text I/O") |
| 143 |
return w_result |
| 144 |
|
| 145 |
if updating: |
| 146 |
w_buffer = W_BufferedRandom(space) |
| 147 |
w_buffer.descr_init(space, w_raw, buffering) |
| 148 |
elif writing or creating or appending: |
| 149 |
w_buffer = W_BufferedWriter(space) |
| 150 |
w_buffer.descr_init(space, w_raw, buffering) |
| 151 |
elif reading: |
| 152 |
w_buffer = W_BufferedReader(space) |
| 153 |
w_buffer.descr_init(space, w_raw, buffering) |
| 154 |
else: |
| 155 |
raise oefmt(space.w_ValueError, "unknown mode: '%s'", mode) |
| 156 |
w_result = w_buffer |
| 157 |
if binary: |
| 158 |
return w_result |
| 159 |
|
| 160 |
w_wrapper = W_TextIOWrapper(space) |
| 161 |
w_wrapper.descr_init(space, w_buffer, encoding, |
| 162 |
space.newtext_or_none(errors), |
| 163 |
space.newtext_or_none(newline), |
| 164 |
line_buffering) |
| 165 |
w_result = w_wrapper |
| 166 |
space.setattr(w_wrapper, space.newtext("mode"), space.newtext(mode)) |
| 167 |
return w_result |
| 168 |
except OperationError as e: |
| 169 |
if w_result: |
| 170 |
try: |
| 171 |
space.call_method(w_result, "close") |
| 172 |
except OperationError as e2: |
| 173 |
e.chain_exceptions(space, e2) |
| 174 |
raise |
| 175 |
|
| 176 |
def open_code(space, w_file): |
| 177 |
return open(space, w_file, "rb") |