1.1 --- a/lib/__builtins__/file.py Sat Jan 07 00:23:19 2017 +0100
1.2 +++ b/lib/__builtins__/file.py Sat Jan 07 16:23:25 2017 +0100
1.3 @@ -32,8 +32,20 @@
1.4
1.5 self.encoding = encoding
1.6 self.bufsize = bufsize
1.7 +
1.8 + # Internal stream details.
1.9 +
1.10 self.__data__ = None
1.11
1.12 + def _convert(self, bytes):
1.13 +
1.14 + "Convert 'bytes' to text if necessary."
1.15 +
1.16 + if self.encoding:
1.17 + return unicode(bytes, self.encoding)
1.18 + else:
1.19 + return bytes
1.20 +
1.21 def read(self, n=0):
1.22
1.23 "Read 'n' bytes from the stream."
1.24 @@ -63,12 +75,51 @@
1.25
1.26 s = "".join(l)
1.27
1.28 - # Convert bytes to text if necessary.
1.29 + return self._convert(s)
1.30 +
1.31 + def readline(self, n=0):
1.32 +
1.33 + """
1.34 + Read until an end-of-line indicator is encountered or at most 'n' bytes,
1.35 + if indicated.
1.36 + """
1.37 +
1.38 + check_int(n)
1.39 +
1.40 + # Read any indicated number of bytes.
1.41 +
1.42 + if n > 0:
1.43 + s = fread(self.__data__, n)
1.44 +
1.45 + # Read until an end-of-line indicator.
1.46 +
1.47 + else:
1.48 + l = []
1.49
1.50 - if self.encoding:
1.51 - return unicode(s, self.encoding)
1.52 - else:
1.53 - return s
1.54 + # Read until end-of-line or end-of-file.
1.55 + # NOTE: Only POSIX newlines are supported currently.
1.56 +
1.57 + try:
1.58 + while True:
1.59 + s = fread(self.__data__, 1)
1.60 + l.append(s)
1.61 +
1.62 + # Where a newline has been read, provide the preceding data
1.63 + # plus the newline indicator.
1.64 +
1.65 + if s == "\n":
1.66 + break
1.67 +
1.68 + # Handle end-of-file reads.
1.69 +
1.70 + except EOFError:
1.71 + pass
1.72 +
1.73 + s = "".join(l)
1.74 +
1.75 + return self._convert(s)
1.76 +
1.77 + def readlines(self, n=None): pass
1.78
1.79 def write(self, s):
1.80
1.81 @@ -104,7 +155,4 @@
1.82 get_using(filestream.__init__, self)(encoding, bufsize)
1.83 self.__data__ = fopen(filename, mode)
1.84
1.85 - def readline(self, size=None): pass
1.86 - def readlines(self, size=None): pass
1.87 -
1.88 # vim: tabstop=4 expandtab shiftwidth=4