Lichen

lib/posix/io.py

503:644952535f7a
2017-01-23 Paul Boddie Added another, shorter test of get_using, commenting the existing test slightly.
     1 #!/usr/bin/env python     2      3 """     4 POSIX input/output functions.     5      6 Copyright (C) 2016 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__.file import filestream    23 from __builtins__.types import check_int, check_string    24     25 from native import (    26     close as _close,    27     fdopen as _fdopen,    28     read as _read,    29     write as _write    30     )    31     32 import locale    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         get_using(filestream.__init__, self)(encoding, bufsize)    76         self.__data__ = fdopen(fd, mode)    77     78 # Standard streams.    79     80 stdin = sysstream(0)    81 stdout = sysstream(1, "w")    82 stderr = sysstream(2, "w")    83     84 # Localised streams.    85 # Perform locale initialisation explicitly to ensure that the locale module    86 # and various function defaults have been initialised.    87     88 locale.initlocale()    89 lstdin = sysstream(0, "r", locale.getpreferredencoding())    90     91 # Input/output functions.    92     93 def close(fd):    94     95     "Close the file descriptor 'fd'."    96     97     _close(fd)    98     99 def closerange(fd_low, fd_high): pass   100 def dup(fd): pass   101 def dup2(old_fd, new_fd): pass   102 def fchdir(fd): pass   103 def fchmod(fd, mode): pass   104 def fchown(fd, uid, gid): pass   105 def fdatasync(fd): pass   106    107 def fdopen(fd, mode="r"):   108    109     """   110     Open a stream for the given file descriptor 'fd', operating in the given   111     'mode'.   112     """   113    114     check_int(fd)   115     check_string(mode)   116     return _fdopen(fd, mode)   117    118 def fpathconf(fd, name): pass   119 def fstat(fd): pass   120 def fstatvfs(fd): pass   121 def fsync(fd): pass   122 def ftruncate(fd, length): pass   123 def isatty(fd): pass   124    125 SEEK_CUR = 1   126 SEEK_END = 2   127 SEEK_SET = 0   128    129 def lseek(fd, pos, how): pass   130 def open(filename, flag, mode=0777): pass   131 def openpty(): pass   132 def pipe(): pass   133 def putenv(key, value): pass   134    135 def read(fd, n):   136    137     """   138     Read using the low-level file descriptor 'fd' the given number of bytes 'n'.   139     """   140    141     check_int(fd)   142     check_int(n)   143     return _read(fd, n)   144    145 def times(): pass   146 def ttyname(fd): pass   147 def umask(new_mask): pass   148 def uname(): pass   149 def unsetenv(key): pass   150    151 def write(fd, s):   152    153     "Write using the low-level file descriptor 'fd' the given string 's'."   154    155     check_int(fd)   156     check_string(s)   157     return _write(fd, s)   158    159 # Constants.   160    161 O_APPEND = 1024   162 O_ASYNC = 8192   163 O_CREAT = 64   164 O_DIRECT = 16384   165 O_DIRECTORY = 65536   166 O_DSYNC = 4096   167 O_EXCL = 128   168 O_LARGEFILE = 32768   169 O_NDELAY = 2048   170 O_NOATIME = 262144   171 O_NOCTTY = 256   172 O_NOFOLLOW = 131072   173 O_NONBLOCK = 2048   174 O_RDONLY = 0   175 O_RDWR = 2   176 O_RSYNC = 1052672   177 O_SYNC = 1052672   178 O_TRUNC = 512   179 O_WRONLY = 1   180    181 # vim: tabstop=4 expandtab shiftwidth=4