# HG changeset patch # User Paul Boddie # Date 1483802605 -3600 # Node ID 06c4fdc8da56050ebfaeef1d0ad3a746ca77b8e6 # Parent 224da90acd50be4f38dba04454a84b44f0ea440e Added support for readline on streams and the raw_input function. diff -r 224da90acd50 -r 06c4fdc8da56 lib/__builtins__/file.py --- a/lib/__builtins__/file.py Sat Jan 07 00:23:19 2017 +0100 +++ b/lib/__builtins__/file.py Sat Jan 07 16:23:25 2017 +0100 @@ -32,8 +32,20 @@ self.encoding = encoding self.bufsize = bufsize + + # Internal stream details. + self.__data__ = None + def _convert(self, bytes): + + "Convert 'bytes' to text if necessary." + + if self.encoding: + return unicode(bytes, self.encoding) + else: + return bytes + def read(self, n=0): "Read 'n' bytes from the stream." @@ -63,12 +75,51 @@ s = "".join(l) - # Convert bytes to text if necessary. + return self._convert(s) + + def readline(self, n=0): + + """ + Read until an end-of-line indicator is encountered or at most 'n' bytes, + if indicated. + """ + + check_int(n) + + # Read any indicated number of bytes. + + if n > 0: + s = fread(self.__data__, n) + + # Read until an end-of-line indicator. + + else: + l = [] - if self.encoding: - return unicode(s, self.encoding) - else: - return s + # Read until end-of-line or end-of-file. + # NOTE: Only POSIX newlines are supported currently. + + try: + while True: + s = fread(self.__data__, 1) + l.append(s) + + # Where a newline has been read, provide the preceding data + # plus the newline indicator. + + if s == "\n": + break + + # Handle end-of-file reads. + + except EOFError: + pass + + s = "".join(l) + + return self._convert(s) + + def readlines(self, n=None): pass def write(self, s): @@ -104,7 +155,4 @@ get_using(filestream.__init__, self)(encoding, bufsize) self.__data__ = fopen(filename, mode) - def readline(self, size=None): pass - def readlines(self, size=None): pass - # vim: tabstop=4 expandtab shiftwidth=4 diff -r 224da90acd50 -r 06c4fdc8da56 lib/__builtins__/io.py --- a/lib/__builtins__/io.py Sat Jan 07 00:23:19 2017 +0100 +++ b/lib/__builtins__/io.py Sat Jan 07 16:23:25 2017 +0100 @@ -19,9 +19,21 @@ this program. If not, see . """ +from sys import lstdin, stdout + open = file -def raw_input(prompt=None): pass +def raw_input(prompt=None): + + """ + Write any specified 'prompt' to standard output and read text from standard + input. + """ + + if prompt: + stdout.write(prompt) + + return lstdin.readline() def print_(dest, args, nl): @@ -30,12 +42,6 @@ 'nl' is given as a true value. """ - # If imported at the module level, the sys module must be set up first, - # which should be ensured by the module ordering activity, and a module - # attribute will be employed to hold a reference to sys.stdout. - - from sys import stdout - # Write to standard output if dest is not specified. dest = dest or stdout diff -r 224da90acd50 -r 06c4fdc8da56 lib/__builtins__/str.py --- a/lib/__builtins__/str.py Sat Jan 07 00:23:19 2017 +0100 +++ b/lib/__builtins__/str.py Sat Jan 07 16:23:25 2017 +0100 @@ -178,8 +178,17 @@ return str(b) def __bool__(self): + + "Return whether the string provides any data." + return str_nonempty(self.__data__) + def __contains__(self, value): + + "Return whether this string contains 'value'." + + return self.find(value) != -1 + def endswith(self, s): "Return whether this string ends with 's'." diff -r 224da90acd50 -r 06c4fdc8da56 tests/raw_input.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/raw_input.py Sat Jan 07 16:23:25 2017 +0100 @@ -0,0 +1,4 @@ +s = raw_input("Reading...") +print s +print s.__class__ +print s.encoding