Lichen

lib/__builtins__/file.py

351:c2bdc5b1a125
2016-12-08 Paul Boddie Ensure that base classes provide the attributes they use. Added tests demonstrating the use of attributes in base classes that are not necessarily defined in those classes or in some subclasses.
     1 #!/usr/bin/env python     2      3 """     4 File objects.     5      6 Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from __builtins__.types import check_int, check_string    23 import native    24     25 class filestream:    26     27     "Generic file-oriented stream functionality."    28     29     def __init__(self, bufsize=1024):    30     31         "Initialise the stream with the given 'bufsize'."    32     33         self.bufsize = bufsize    34         self.__data__ = None    35     36     def read(self, n=0):    37     38         "Read 'n' bytes from the stream."    39     40         check_int(n)    41     42         # Read any indicated number of bytes.    43     44         if n > 0:    45             return native._fread(self.__data__, n)    46     47         # Read all remaining bytes.    48     49         else:    50             l = []    51     52             # Read until end-of-file.    53     54             try:    55                 while True:    56                     l.append(native._fread(self.__data__, self.bufsize))    57     58             # Handle end-of-file reads.    59     60             except EOFError:    61                 pass    62     63             return "".join(l)    64     65     def write(self, s):    66     67         "Write string 's' to the stream."    68     69         check_string(s)    70         native._fwrite(self.__data__, s)    71     72     def close(self): pass    73     74 class file(filestream):    75     76     "A file abstraction."    77     78     def __init__(self, filename, mode="r", bufsize=1024):    79     80         "Open the file with the given 'filename' using the given access 'mode'."    81     82         get_using(filestream.__init__, self)(bufsize)    83         self.__data__ = native._fopen(filename, mode)    84     85     def readline(self, size=None): pass    86     def readlines(self, size=None): pass    87     88 # vim: tabstop=4 expandtab shiftwidth=4