Lichen

Annotated lib/posix/io.py

336:8c75cdf1a764
2016-12-06 Paul Boddie Introduced stream classes employing C-level FILE pointers, changing the sys stdin, stdout and stderr objects to be instances of these stream classes. Added fread and fwrite support to the native functions. Added support for raising EOFError.
paul@6 1
#!/usr/bin/env python
paul@6 2
paul@328 3
"""
paul@328 4
POSIX input/output functions.
paul@328 5
paul@328 6
Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
paul@328 7
paul@328 8
This program is free software; you can redistribute it and/or modify it under
paul@328 9
the terms of the GNU General Public License as published by the Free Software
paul@328 10
Foundation; either version 3 of the License, or (at your option) any later
paul@328 11
version.
paul@328 12
paul@328 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@328 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@328 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@328 16
details.
paul@328 17
paul@328 18
You should have received a copy of the GNU General Public License along with
paul@328 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@328 20
"""
paul@328 21
paul@328 22
import native
paul@328 23
paul@336 24
# Abstractions for system-level files and streams.
paul@336 25
paul@332 26
class sysfile:
paul@332 27
paul@332 28
    "A system-level file object."
paul@332 29
paul@332 30
    def __init__(self, fd):
paul@332 31
paul@332 32
        "Initialise the file with the given 'fd'."
paul@332 33
paul@332 34
        self.fd = fd
paul@332 35
paul@332 36
    def read(self, n):
paul@332 37
paul@336 38
        "Read 'n' bytes from the file, returning a string."
paul@332 39
paul@336 40
        _check_int(n)
paul@332 41
        return read(self.fd, n)
paul@332 42
paul@332 43
    def write(self, s):
paul@332 44
paul@336 45
        "Write string 's' to the file."
paul@336 46
paul@336 47
        _check_string(s)
paul@336 48
        write(self.fd, s)
paul@336 49
paul@336 50
class sysstream:
paul@336 51
paul@336 52
    "A system-level stream object."
paul@336 53
paul@336 54
    def __init__(self, fd, mode="r"):
paul@336 55
paul@336 56
        "Initialise the stream with the given 'fd' and 'mode'."
paul@336 57
paul@336 58
        self.__data__ = fdopen(fd, mode)
paul@332 59
paul@336 60
    def read(self, n):
paul@336 61
paul@336 62
        "Read 'n' bytes from the stream."
paul@336 63
paul@336 64
        _check_int(n)
paul@336 65
        return native._fread(self.__data__, n)
paul@336 66
paul@336 67
    def write(self, s):
paul@336 68
paul@336 69
        "Write string 's' to the stream."
paul@336 70
paul@336 71
        _check_string(s)
paul@336 72
        native._fwrite(self.__data__, s)
paul@336 73
paul@336 74
# Input/output functions.
paul@332 75
paul@6 76
def close(fd): pass
paul@6 77
def closerange(fd_low, fd_high): pass
paul@6 78
def dup(fd): pass
paul@6 79
def dup2(old_fd, new_fd): pass
paul@328 80
def fchdir(fd): pass
paul@6 81
def fchmod(fd, mode): pass
paul@6 82
def fchown(fd, uid, gid): pass
paul@328 83
def fdatasync(fd): pass
paul@328 84
paul@328 85
def fdopen(fd, mode="r"):
paul@328 86
paul@328 87
    """
paul@328 88
    Open a stream for the given file descriptor 'fd', operating in the given
paul@328 89
    'mode'.
paul@328 90
    """
paul@328 91
paul@328 92
    _check_fd(fd)
paul@328 93
    _check_string(mode)
paul@328 94
    return native._fdopen(fd, mode)
paul@328 95
paul@6 96
def fpathconf(fd, name): pass
paul@6 97
def fstat(fd): pass
paul@6 98
def fstatvfs(fd): pass
paul@328 99
def fsync(fd): pass
paul@6 100
def ftruncate(fd, length): pass
paul@6 101
def isatty(fd): pass
paul@328 102
paul@328 103
SEEK_CUR = 1
paul@328 104
SEEK_END = 2
paul@328 105
SEEK_SET = 0
paul@328 106
paul@6 107
def lseek(fd, pos, how): pass
paul@6 108
def open(filename, flag, mode=0777): pass
paul@6 109
def openpty(): pass
paul@6 110
def pipe(): pass
paul@6 111
def putenv(key, value): pass
paul@328 112
paul@328 113
def read(fd, n):
paul@328 114
paul@328 115
    """
paul@328 116
    Read using the low-level file descriptor 'fd' the given number of bytes 'n'.
paul@328 117
    """
paul@328 118
paul@328 119
    _check_fd(fd)
paul@328 120
    _check_int(n)
paul@328 121
    return native._read(fd, n)
paul@328 122
paul@6 123
def times(): pass
paul@6 124
def ttyname(fd): pass
paul@6 125
def umask(new_mask): pass
paul@6 126
def uname(): pass
paul@6 127
def unsetenv(key): pass
paul@328 128
paul@328 129
def write(fd, s):
paul@328 130
paul@328 131
    "Write using the low-level file descriptor 'fd' the given string 's'."
paul@328 132
paul@328 133
    _check_fd(fd)
paul@328 134
    _check_string(s)
paul@328 135
    native._write(fd, s)
paul@6 136
paul@336 137
# Constants.
paul@336 138
paul@6 139
O_APPEND = 1024
paul@6 140
O_ASYNC = 8192
paul@6 141
O_CREAT = 64
paul@6 142
O_DIRECT = 16384
paul@6 143
O_DIRECTORY = 65536
paul@6 144
O_DSYNC = 4096
paul@6 145
O_EXCL = 128
paul@6 146
O_LARGEFILE = 32768
paul@6 147
O_NDELAY = 2048
paul@6 148
O_NOATIME = 262144
paul@6 149
O_NOCTTY = 256
paul@6 150
O_NOFOLLOW = 131072
paul@6 151
O_NONBLOCK = 2048
paul@6 152
O_RDONLY = 0
paul@6 153
O_RDWR = 2
paul@6 154
O_RSYNC = 1052672
paul@6 155
O_SYNC = 1052672
paul@6 156
O_TRUNC = 512
paul@6 157
O_WRONLY = 1
paul@6 158
paul@336 159
# Type validation functions.
paul@336 160
paul@328 161
def _check_fd(fd):
paul@328 162
paul@328 163
    "Check the given low-level file descriptor 'fd'."
paul@328 164
paul@328 165
    if not native._isinstance(fd, int):
paul@328 166
        raise ValueError(fd)
paul@328 167
paul@328 168
def _check_int(i):
paul@328 169
paul@328 170
    "Check the given int 'i'."
paul@328 171
paul@328 172
    if not native._isinstance(i, int):
paul@328 173
        raise ValueError(i)
paul@328 174
paul@328 175
def _check_string(s):
paul@328 176
paul@328 177
    "Check the given string 's'."
paul@328 178
paul@328 179
    if not native._isinstance(s, string):
paul@328 180
        raise ValueError(s)
paul@6 181
paul@6 182
# vim: tabstop=4 expandtab shiftwidth=4