1.1 --- a/lib/posix/io.py Tue Dec 06 22:30:20 2016 +0100
1.2 +++ b/lib/posix/io.py Tue Dec 06 22:39:23 2016 +0100
1.3 @@ -21,6 +21,8 @@
1.4
1.5 import native
1.6
1.7 +# Abstractions for system-level files and streams.
1.8 +
1.9 class sysfile:
1.10
1.11 "A system-level file object."
1.12 @@ -33,15 +35,43 @@
1.13
1.14 def read(self, n):
1.15
1.16 - "Read 'n' bytes from the file."
1.17 + "Read 'n' bytes from the file, returning a string."
1.18
1.19 + _check_int(n)
1.20 return read(self.fd, n)
1.21
1.22 def write(self, s):
1.23
1.24 - "Write 's' to the file."
1.25 + "Write string 's' to the file."
1.26 +
1.27 + _check_string(s)
1.28 + write(self.fd, s)
1.29 +
1.30 +class sysstream:
1.31 +
1.32 + "A system-level stream object."
1.33 +
1.34 + def __init__(self, fd, mode="r"):
1.35 +
1.36 + "Initialise the stream with the given 'fd' and 'mode'."
1.37 +
1.38 + self.__data__ = fdopen(fd, mode)
1.39
1.40 - write(self.fd, str(s))
1.41 + def read(self, n):
1.42 +
1.43 + "Read 'n' bytes from the stream."
1.44 +
1.45 + _check_int(n)
1.46 + return native._fread(self.__data__, n)
1.47 +
1.48 + def write(self, s):
1.49 +
1.50 + "Write string 's' to the stream."
1.51 +
1.52 + _check_string(s)
1.53 + native._fwrite(self.__data__, s)
1.54 +
1.55 +# Input/output functions.
1.56
1.57 def close(fd): pass
1.58 def closerange(fd_low, fd_high): pass
1.59 @@ -104,6 +134,8 @@
1.60 _check_string(s)
1.61 native._write(fd, s)
1.62
1.63 +# Constants.
1.64 +
1.65 O_APPEND = 1024
1.66 O_ASYNC = 8192
1.67 O_CREAT = 64
1.68 @@ -124,6 +156,8 @@
1.69 O_TRUNC = 512
1.70 O_WRONLY = 1
1.71
1.72 +# Type validation functions.
1.73 +
1.74 def _check_fd(fd):
1.75
1.76 "Check the given low-level file descriptor 'fd'."