Source

pypy / pypy / module / _io / interp_fileio.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from pypy.interpreter.typedef import TypeDef, interp_attrproperty, GetSetProperty
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.error import OperationError, wrap_oserror, wrap_oserror2
from pypy.rlib.rarithmetic import r_longlong
from pypy.rlib.rstring import StringBuilder
from os import O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_TRUNC
import sys, os, stat, errno
from pypy.module._io.interp_iobase import W_RawIOBase, convert_size

def interp_member_w(name, cls, doc=None):
    "NOT_RPYTHON: initialization-time only"
    def fget(space, obj):
        w_value = getattr(obj, name)
        if w_value is None:
            raise OperationError(space.w_AttributeError,
                                 space.wrap(name))
        else:
            return w_value
    def fset(space, obj, w_value):
        setattr(obj, name, w_value)
    def fdel(space, obj):
        w_value = getattr(obj, name)
        if w_value is None:
            raise OperationError(space.w_AttributeError,
                                 space.wrap(name))
        setattr(obj, name, None)

    return GetSetProperty(fget, fset, fdel, cls=cls, doc=doc)


O_BINARY = getattr(os, "O_BINARY", 0)
O_APPEND = getattr(os, "O_APPEND", 0)

def _bad_mode(space):
    raise OperationError(space.w_ValueError, space.wrap(
        "Must have exactly one of read/write/append mode"))

def decode_mode(space, mode):
    flags = 0
    rwa = False
    readable = False
    writable = False
    append = False
    plus = False

    for s in mode:
        if s == 'r':
            if rwa:
                _bad_mode(space)
            rwa = True
            readable = True
        elif s == 'w':
            if rwa:
                _bad_mode(space)
            rwa = True
            writable = True
            flags |= O_CREAT | O_TRUNC
        elif s == 'a':
            if rwa:
                _bad_mode(space)
            rwa = True
            writable = True
            flags |= O_CREAT
            append = True
        elif s == 'b':
            pass
        elif s == '+':
            if plus:
                _bad_mode(space)
            readable = writable = True
            plus = True
        else:
            raise OperationError(space.w_ValueError, space.wrap(
                "invalid mode: %s" % (mode,)))

    if not rwa:
        _bad_mode(space)

    if readable and writable:
        flags |= O_RDWR
    elif readable:
        flags |= O_RDONLY
    else:
        flags |= O_WRONLY

    flags |= O_BINARY

    if append:
        flags |= O_APPEND

    return readable, writable, append, flags

SMALLCHUNK = 8 * 1024
BIGCHUNK = 512 * 1024

def new_buffersize(fd, currentsize):
    try:
        st = os.fstat(fd)
        end = st.st_size
        pos = os.lseek(fd, 0, 1)
    except OSError:
        pass
    else:
        # Files claiming a size smaller than SMALLCHUNK may
        # actually be streaming pseudo-files. In this case, we
        # apply the more aggressive algorithm below.
        if end >= SMALLCHUNK and end >= pos:
            # Add 1 so if the file were to grow we'd notice.
            return currentsize + end - pos + 1

    if currentsize > SMALLCHUNK:
        # Keep doubling until we reach BIGCHUNK;
        # then keep adding BIGCHUNK.
        if currentsize <= BIGCHUNK:
            return currentsize + currentsize
        else:
            return currentsize + BIGCHUNK
    return currentsize + SMALLCHUNK

def verify_fd(fd):
    return

class W_FileIO(W_RawIOBase):
    def __init__(self, space):
        W_RawIOBase.__init__(self, space)
        self.fd = -1
        self.readable = False
        self.writable = False
        self.seekable = -1
        self.closefd = True
        self.w_name = None

    def descr_new(space, w_subtype, __args__):
        self = space.allocate_instance(W_FileIO, w_subtype)
        W_FileIO.__init__(self, space)
        return space.wrap(self)

    @unwrap_spec(mode=str, closefd=int)
    def descr_init(self, space, w_name, mode='r', closefd=True):
        if space.isinstance_w(w_name, space.w_float):
            raise OperationError(space.w_TypeError, space.wrap(
                "integer argument expected, got float"))

        fd = -1
        try:
            fd = space.int_w(w_name)
        except OperationError, e:
            pass
        else:
            if fd < 0:
                raise OperationError(space.w_ValueError, space.wrap(
                    "negative file descriptor"))

        self.readable, self.writable, append, flags = decode_mode(space, mode)

        if fd >= 0:
            verify_fd(fd)
            try:
                os.fstat(fd)
            except OSError, e:
                if e.errno == errno.EBADF:
                    raise wrap_oserror(space, e)
                # else: pass
            self.fd = fd
            self.closefd = bool(closefd)
        else:
            if not closefd:
                raise OperationError(space.w_ValueError, space.wrap(
                    "Cannot use closefd=False with file name"))
            self.closefd = True

            from pypy.module.posix.interp_posix import (
                dispatch_filename, rposix)
            try:
                self.fd = dispatch_filename(rposix.open)(
                    space, w_name, flags, 0666)
            except OSError, e:
                raise wrap_oserror2(space, e, w_name,
                                    exception_name='w_IOError')

            self._dircheck(space, w_name)
        self.w_name = w_name

        if append:
            # For consistent behaviour, we explicitly seek to the end of file
            # (otherwise, it might be done only on the first write()).
            try:
                os.lseek(self.fd, 0, os.SEEK_END)
            except OSError, e:
                raise wrap_oserror(space, e, exception_name='w_IOError')

    def _mode(self):
        if self.readable:
            if self.writable:
                return 'rb+'
            else:
                return 'rb'
        else:
            return 'wb'

    def descr_get_mode(self, space):
        return space.wrap(self._mode())

    def _closed(self, space):
        return self.fd < 0

    def _check_closed(self, space, message=None):
        if message is None:
            message = "I/O operation on closed file"
        if self.fd < 0:
            raise OperationError(space.w_ValueError, space.wrap(message))

    def _check_readable(self, space):
        if not self.readable:
            self._unsupportedoperation(space, "File not open for reading")

    def _check_writable(self, space):
        if not self.writable:
            self._unsupportedoperation(space, "File not open for writing")

    def _close(self, space):
        if self.fd < 0:
            return
        fd = self.fd
        self.fd = -1

        try:
            verify_fd(fd)
            os.close(fd)
        except OSError, e:
            raise wrap_oserror(space, e,
                               exception_name='w_IOError')

    def close_w(self, space):
        if not self.closefd:
            self.fd = -1
            return
        self._close(space)
        W_RawIOBase.close_w(self, space)

    def _dircheck(self, space, w_filename):
        # On Unix, fopen will succeed for directories.
        # In Python, there should be no file objects referring to
        # directories, so we need a check.
        if self.fd < 0:
            return
        try:
            st = os.fstat(self.fd)
        except OSError:
            return
        if stat.S_ISDIR(st.st_mode):
            self._close(space)
            raise wrap_oserror2(space, OSError(errno.EISDIR, "fstat"),
                                w_filename, exception_name='w_IOError')

    @unwrap_spec(pos=r_longlong, whence=int)
    def seek_w(self, space, pos, whence=0):
        self._check_closed(space)
        try:
            pos = os.lseek(self.fd, pos, whence)
        except OSError, e:
            raise wrap_oserror(space, e,
                               exception_name='w_IOError')
        return space.wrap(pos)

    def tell_w(self, space):
        self._check_closed(space)
        try:
            pos = os.lseek(self.fd, 0, 1)
        except OSError, e:
            raise wrap_oserror(space, e,
                               exception_name='w_IOError')
        return space.wrap(pos)

    def readable_w(self, space):
        self._check_closed(space)
        return space.wrap(self.readable)

    def writable_w(self, space):
        self._check_closed(space)
        return space.wrap(self.writable)

    def seekable_w(self, space):
        self._check_closed(space)
        if self.seekable < 0:
            try:
                pos = os.lseek(self.fd, 0, os.SEEK_CUR)
            except OSError:
                self.seekable = 0
            else:
                self.seekable = 1
        return space.newbool(self.seekable == 1)

    # ______________________________________________

    def fileno_w(self, space):
        self._check_closed(space)
        return space.wrap(self.fd)

    def isatty_w(self, space):
        self._check_closed(space)
        try:
            res = os.isatty(self.fd)
        except OSError, e:
            raise wrap_oserror(space, e, exception_name='w_IOError')
        return space.wrap(res)

    def repr_w(self, space):
        if self.fd < 0:
            return space.wrap("<_io.FileIO [closed]>")

        if self.w_name is None:
            return space.wrap(
                "<_io.FileIO fd=%d mode='%s'>" % (
                    self.fd, self._mode()))
        else:
            w_repr = space.repr(self.w_name)
            return space.wrap(
                "<_io.FileIO name=%s mode='%s'>" % (
                    space.str_w(w_repr), self._mode()))

    # ______________________________________________

    def write_w(self, space, w_data):
        self._check_closed(space)
        self._check_writable(space)
        data = space.bufferstr_w(w_data)

        try:
            n = os.write(self.fd, data)
        except OSError, e:
            if e.errno == errno.EAGAIN:
                return space.w_None
            raise wrap_oserror(space, e,
                               exception_name='w_IOError')

        return space.wrap(n)

    def read_w(self, space, w_size=None):
        self._check_closed(space)
        self._check_readable(space)
        size = convert_size(space, w_size)

        if size < 0:
            return self.readall_w(space)

        try:
            s = os.read(self.fd, size)
        except OSError, e:
            if e.errno == errno.EAGAIN:
                return space.w_None
            raise wrap_oserror(space, e,
                               exception_name='w_IOError')

        return space.wrapbytes(s)

    def readinto_w(self, space, w_buffer):
        self._check_closed(space)
        self._check_readable(space)
        rwbuffer = space.rwbuffer_w(w_buffer)
        length = rwbuffer.getlength()
        try:
            buf = os.read(self.fd, length)
        except OSError, e:
            if e.errno == errno.EAGAIN:
                return space.w_None
            raise wrap_oserror(space, e,
                               exception_name='w_IOError')
        rwbuffer.setslice(0, buf)
        return space.wrap(len(buf))

    def readall_w(self, space):
        self._check_closed(space)
        self._check_readable(space)
        total = 0

        builder = StringBuilder()
        while True:
            newsize = int(new_buffersize(self.fd, total))

            try:
                chunk = os.read(self.fd, newsize - total)
            except OSError, e:
                if e.errno == errno.EAGAIN:
                    if total > 0:
                        # return what we've got so far
                        break
                    return space.w_None
                raise wrap_oserror(space, e,
                                   exception_name='w_IOError')

            if not chunk:
                break
            builder.append(chunk)
            total += len(chunk)
        return space.wrapbytes(builder.build())

    if sys.platform == "win32":
        def _truncate(self, size):
            from pypy.rlib.streamio import ftruncate_win32
            ftruncate_win32(self.fd, size)
    else:
        def _truncate(self, size):
            os.ftruncate(self.fd, size)

    def truncate_w(self, space, w_size=None):
        self._check_closed(space)
        self._check_writable(space)
        if space.is_none(w_size):
            w_size = self.tell_w(space)

        try:
            self._truncate(space.r_longlong_w(w_size))
        except OSError, e:
            raise wrap_oserror(space, e, exception_name='w_IOError')

        return w_size

W_FileIO.typedef = TypeDef(
    'FileIO', W_RawIOBase.typedef,
    __new__  = interp2app(W_FileIO.descr_new.im_func),
    __init__  = interp2app(W_FileIO.descr_init),
    __repr__ = interp2app(W_FileIO.repr_w),

    seek = interp2app(W_FileIO.seek_w),
    tell = interp2app(W_FileIO.tell_w),
    write = interp2app(W_FileIO.write_w),
    read = interp2app(W_FileIO.read_w),
    readinto = interp2app(W_FileIO.readinto_w),
    readall = interp2app(W_FileIO.readall_w),
    truncate = interp2app(W_FileIO.truncate_w),
    close = interp2app(W_FileIO.close_w),

    readable = interp2app(W_FileIO.readable_w),
    writable = interp2app(W_FileIO.writable_w),
    seekable = interp2app(W_FileIO.seekable_w),
    fileno = interp2app(W_FileIO.fileno_w),
    isatty = interp2app(W_FileIO.isatty_w),
    name = interp_member_w('w_name', cls=W_FileIO),
    closefd = interp_attrproperty(
        'closefd', cls=W_FileIO,
        doc="True if the file descriptor will be closed"),
    mode = GetSetProperty(W_FileIO.descr_get_mode,
                          doc="String giving the file mode"),
    )
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.