Lichen

lib/libc/io.py

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