Lichen

Annotated lib/libc/io.py

878:c33b256b1270
2019-01-26 Paul Boddie Merged changes from the default branch. trailing-data
paul@529 1
#!/usr/bin/env python
paul@529 2
paul@529 3
"""
paul@529 4
C library input/output.
paul@529 5
paul@529 6
Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
paul@529 7
paul@529 8
This program is free software; you can redistribute it and/or modify it under
paul@529 9
the terms of the GNU General Public License as published by the Free Software
paul@529 10
Foundation; either version 3 of the License, or (at your option) any later
paul@529 11
version.
paul@529 12
paul@529 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@529 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@529 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@529 16
details.
paul@529 17
paul@529 18
You should have received a copy of the GNU General Public License along with
paul@529 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@529 20
"""
paul@529 21
paul@529 22
from __builtins__.stream import filestream
paul@529 23
from __builtins__.types import check_int, check_string
paul@529 24
paul@529 25
import locale
paul@529 26
paul@529 27
from native import (
paul@529 28
    close as _close,
paul@529 29
    fdopen as _fdopen,
paul@529 30
    read as _read,
paul@529 31
    write as _write
paul@529 32
    )
paul@529 33
paul@529 34
# Abstractions for system-level files and streams.
paul@529 35
paul@529 36
class sysfile:
paul@529 37
paul@529 38
    "A system-level file object."
paul@529 39
paul@529 40
    def __init__(self, fd):
paul@529 41
paul@529 42
        "Initialise the file with the given 'fd'."
paul@529 43
paul@529 44
        self.fd = fd
paul@529 45
paul@529 46
    def read(self, n):
paul@529 47
paul@529 48
        "Read 'n' bytes from the file, returning a string."
paul@529 49
paul@529 50
        return read(self.fd, n)
paul@529 51
paul@529 52
    def write(self, s):
paul@529 53
paul@529 54
        "Write string 's' to the file."
paul@529 55
paul@529 56
        return write(self.fd, s)
paul@529 57
paul@529 58
    def close(self):
paul@529 59
paul@529 60
        "Close the file."
paul@529 61
paul@529 62
        close(self.fd)
paul@529 63
paul@529 64
class sysstream(filestream):
paul@529 65
paul@529 66
    "A system-level stream object."
paul@529 67
paul@529 68
    def __init__(self, fd, mode="r", encoding=None, bufsize=1024):
paul@529 69
paul@529 70
        """
paul@529 71
        Initialise the stream with the given 'fd', 'mode', 'encoding' and
paul@529 72
        'bufsize'.
paul@529 73
        """
paul@529 74
paul@529 75
        check_int(fd)
paul@529 76
        check_string(mode)
paul@529 77
paul@529 78
        get_using(filestream.__init__, self)(encoding, bufsize)
paul@529 79
        self.__data__ = _fdopen(fd, mode)
paul@529 80
paul@529 81
# Standard streams.
paul@529 82
paul@529 83
stdin = sysstream(0)
paul@529 84
stdout = sysstream(1, "w")
paul@529 85
stderr = sysstream(2, "w")
paul@529 86
paul@529 87
# Localised streams.
paul@529 88
# Perform locale initialisation explicitly to ensure that the locale module
paul@529 89
# and various function defaults have been initialised.
paul@529 90
paul@529 91
locale.initlocale()
paul@529 92
lstdin = sysstream(0, "r", locale.getpreferredencoding())
paul@529 93
paul@529 94
# Input/output functions.
paul@529 95
paul@529 96
def close(fd):
paul@529 97
paul@529 98
    "Close the file descriptor 'fd'."
paul@529 99
paul@529 100
    _close(fd)
paul@529 101
paul@529 102
def fdopen(fd, mode="r"):
paul@529 103
paul@529 104
    """
paul@529 105
    Open a stream for the given file descriptor 'fd', operating in the given
paul@529 106
    'mode'.
paul@529 107
    """
paul@529 108
paul@529 109
    return sysstream(fd, mode)
paul@529 110
paul@529 111
def read(fd, n):
paul@529 112
paul@529 113
    """
paul@529 114
    Read using the low-level file descriptor 'fd' the given number of bytes 'n'.
paul@529 115
    """
paul@529 116
paul@529 117
    check_int(fd)
paul@529 118
    check_int(n)
paul@529 119
    return _read(fd, n)
paul@529 120
paul@529 121
def write(fd, s):
paul@529 122
paul@529 123
    "Write using the low-level file descriptor 'fd' the given string 's'."
paul@529 124
paul@529 125
    check_int(fd)
paul@529 126
    check_string(s)
paul@529 127
    return _write(fd, s)
paul@529 128
paul@529 129
# vim: tabstop=4 expandtab shiftwidth=4