1.1 --- a/lib/__builtins__/file.py Thu Dec 08 19:29:12 2016 +0100
1.2 +++ b/lib/__builtins__/file.py Thu Dec 08 21:45:09 2016 +0100
1.3 @@ -19,14 +19,65 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -class file(object):
1.8 +from __builtins__.types import check_int, check_string
1.9 +import native
1.10 +
1.11 +class filestream:
1.12 +
1.13 + "Generic file-oriented stream functionality."
1.14 +
1.15 + def __init__(self, bufsize=1024):
1.16 + self.bufsize = bufsize
1.17 +
1.18 + def read(self, n=0):
1.19 +
1.20 + "Read 'n' bytes from the stream."
1.21 +
1.22 + check_int(n)
1.23 +
1.24 + # Read any indicated number of bytes.
1.25 +
1.26 + if n > 0:
1.27 + return native._fread(self.__data__, n)
1.28 +
1.29 + # Read all remaining bytes.
1.30 +
1.31 + else:
1.32 + l = []
1.33 +
1.34 + # Read until end-of-file.
1.35 +
1.36 + try:
1.37 + while True:
1.38 + l.append(native._fread(self.__data__, self.bufsize))
1.39 +
1.40 + # Handle end-of-file reads.
1.41 +
1.42 + except EOFError:
1.43 + pass
1.44 +
1.45 + return "".join(l)
1.46 +
1.47 + def write(self, s):
1.48 +
1.49 + "Write string 's' to the stream."
1.50 +
1.51 + check_string(s)
1.52 + native._fwrite(self.__data__, s)
1.53 +
1.54 + def close(self): pass
1.55 +
1.56 +class file(filestream):
1.57
1.58 "A file abstraction."
1.59
1.60 - def __init__(self, name, mode=None, buffering=None): pass
1.61 - def read(self, n=None): pass
1.62 - def write(self, s): pass
1.63 - def close(self): pass
1.64 + def __init__(self, filename, mode="r", bufsize=1024):
1.65 +
1.66 + "Open the file with the given 'filename' using the given access 'mode'."
1.67 +
1.68 + get_using(filestream.__init__, self)(bufsize)
1.69 + self.__data__ = native._fopen(filename, mode)
1.70 +
1.71 def readline(self, size=None): pass
1.72 def readlines(self, size=None): pass
1.73