1.1 --- a/lib/posix/io.py Thu Dec 08 19:29:12 2016 +0100
1.2 +++ b/lib/posix/io.py Thu Dec 08 21:45:09 2016 +0100
1.3 @@ -19,6 +19,8 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +from __builtins__.file import filestream
1.8 +from __builtins__.types import check_int, check_string
1.9 import native
1.10
1.11 # Abstractions for system-level files and streams.
1.12 @@ -37,17 +39,17 @@
1.13
1.14 "Read 'n' bytes from the file, returning a string."
1.15
1.16 - _check_int(n)
1.17 + check_int(n)
1.18 return read(self.fd, n)
1.19
1.20 def write(self, s):
1.21
1.22 "Write string 's' to the file."
1.23
1.24 - _check_string(s)
1.25 + check_string(s)
1.26 write(self.fd, s)
1.27
1.28 -class sysstream:
1.29 +class sysstream(filestream):
1.30
1.31 "A system-level stream object."
1.32
1.33 @@ -55,44 +57,8 @@
1.34
1.35 "Initialise the stream with the given 'fd' and 'mode'."
1.36
1.37 + get_using(filestream.__init__, self)(bufsize)
1.38 self.__data__ = fdopen(fd, mode)
1.39 - self.bufsize = bufsize
1.40 -
1.41 - def read(self, n=0):
1.42 -
1.43 - "Read 'n' bytes from the stream."
1.44 -
1.45 - _check_int(n)
1.46 -
1.47 - # Read any indicated number of bytes.
1.48 -
1.49 - if n > 0:
1.50 - return native._fread(self.__data__, n)
1.51 -
1.52 - # Read all remaining bytes.
1.53 -
1.54 - else:
1.55 - l = []
1.56 -
1.57 - # Read until end-of-file.
1.58 -
1.59 - try:
1.60 - while True:
1.61 - l.append(native._fread(self.__data__, self.bufsize))
1.62 -
1.63 - # Handle end-of-file reads.
1.64 -
1.65 - except EOFError:
1.66 - pass
1.67 -
1.68 - return "".join(l)
1.69 -
1.70 - def write(self, s):
1.71 -
1.72 - "Write string 's' to the stream."
1.73 -
1.74 - _check_string(s)
1.75 - native._fwrite(self.__data__, s)
1.76
1.77 # Standard streams.
1.78
1.79 @@ -118,8 +84,8 @@
1.80 'mode'.
1.81 """
1.82
1.83 - _check_fd(fd)
1.84 - _check_string(mode)
1.85 + check_int(fd)
1.86 + check_string(mode)
1.87 return native._fdopen(fd, mode)
1.88
1.89 def fpathconf(fd, name): pass
1.90 @@ -145,8 +111,8 @@
1.91 Read using the low-level file descriptor 'fd' the given number of bytes 'n'.
1.92 """
1.93
1.94 - _check_fd(fd)
1.95 - _check_int(n)
1.96 + check_int(fd)
1.97 + check_int(n)
1.98 return native._read(fd, n)
1.99
1.100 def times(): pass
1.101 @@ -159,8 +125,8 @@
1.102
1.103 "Write using the low-level file descriptor 'fd' the given string 's'."
1.104
1.105 - _check_fd(fd)
1.106 - _check_string(s)
1.107 + check_int(fd)
1.108 + check_string(s)
1.109 native._write(fd, s)
1.110
1.111 # Constants.
1.112 @@ -185,27 +151,4 @@
1.113 O_TRUNC = 512
1.114 O_WRONLY = 1
1.115
1.116 -# Type validation functions.
1.117 -
1.118 -def _check_fd(fd):
1.119 -
1.120 - "Check the given low-level file descriptor 'fd'."
1.121 -
1.122 - if not native._isinstance(fd, int):
1.123 - raise ValueError(fd)
1.124 -
1.125 -def _check_int(i):
1.126 -
1.127 - "Check the given int 'i'."
1.128 -
1.129 - if not native._isinstance(i, int):
1.130 - raise ValueError(i)
1.131 -
1.132 -def _check_string(s):
1.133 -
1.134 - "Check the given string 's'."
1.135 -
1.136 - if not native._isinstance(s, string):
1.137 - raise ValueError(s)
1.138 -
1.139 # vim: tabstop=4 expandtab shiftwidth=4