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
2.1 --- a/lib/__builtins__/io.py Sat Jan 07 00:23:19 2017 +0100
2.2 +++ b/lib/__builtins__/io.py Sat Jan 07 16:23:25 2017 +0100
2.3 @@ -19,9 +19,21 @@
2.4 this program. If not, see <http://www.gnu.org/licenses/>.
2.5 """
2.6
2.7 +from sys import lstdin, stdout
2.8 +
2.9 open = file
2.10
2.11 -def raw_input(prompt=None): pass
2.12 +def raw_input(prompt=None):
2.13 +
2.14 + """
2.15 + Write any specified 'prompt' to standard output and read text from standard
2.16 + input.
2.17 + """
2.18 +
2.19 + if prompt:
2.20 + stdout.write(prompt)
2.21 +
2.22 + return lstdin.readline()
2.23
2.24 def print_(dest, args, nl):
2.25
2.26 @@ -30,12 +42,6 @@
2.27 'nl' is given as a true value.
2.28 """
2.29
2.30 - # If imported at the module level, the sys module must be set up first,
2.31 - # which should be ensured by the module ordering activity, and a module
2.32 - # attribute will be employed to hold a reference to sys.stdout.
2.33 -
2.34 - from sys import stdout
2.35 -
2.36 # Write to standard output if dest is not specified.
2.37
2.38 dest = dest or stdout
3.1 --- a/lib/__builtins__/str.py Sat Jan 07 00:23:19 2017 +0100
3.2 +++ b/lib/__builtins__/str.py Sat Jan 07 16:23:25 2017 +0100
3.3 @@ -178,8 +178,17 @@
3.4 return str(b)
3.5
3.6 def __bool__(self):
3.7 +
3.8 + "Return whether the string provides any data."
3.9 +
3.10 return str_nonempty(self.__data__)
3.11
3.12 + def __contains__(self, value):
3.13 +
3.14 + "Return whether this string contains 'value'."
3.15 +
3.16 + return self.find(value) != -1
3.17 +
3.18 def endswith(self, s):
3.19
3.20 "Return whether this string ends with 's'."
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/raw_input.py Sat Jan 07 16:23:25 2017 +0100
4.3 @@ -0,0 +1,4 @@
4.4 +s = raw_input("Reading...")
4.5 +print s
4.6 +print s.__class__
4.7 +print s.encoding