annotate Lib/dos-8x3/arrayio.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents b41019557b6e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3784
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """File-like objects that read/write an array buffer.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 This implements (nearly) all stdio methods.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 f = ArrayIO() # ready for writing
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 f = ArrayIO(buf) # ready for reading
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 f.close() # explicitly release resources held
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 flag = f.isatty() # always false
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 pos = f.tell() # get current position
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 f.seek(pos) # set current position
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 f.seek(pos, mode) # mode 0: absolute; 1: relative; 2: relative to EOF
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 buf = f.read() # read until EOF
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 buf = f.read(n) # read up to n bytes
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 buf = f.readline() # read until end of line ('\n') or EOF
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 list = f.readlines()# list of f.readline() results until EOF
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 f.write(buf) # write at current position
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 f.writelines(list) # for line in list: f.write(line)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 f.getvalue() # return whole file's contents as a string
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 Notes:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 - This is very similar to StringIO. StringIO is faster for reading,
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 but ArrayIO is faster for writing.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 - ArrayIO uses an array object internally, but all its interfaces
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 accept and return strings.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 - Using a real file is often faster (but less convenient).
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 - fileno() is left unimplemented so that code which uses it triggers
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 an exception early.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 - Seeking far beyond EOF and then writing will insert real null
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 bytes that occupy space in the buffer.
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 - There's a simple test set (see end of this file).
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 """
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 import string
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 from array import array
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 class ArrayIO:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 def __init__(self, buf = ''):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 self.buf = array('c', buf)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 self.pos = 0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 self.closed = 0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 self.softspace = 0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 def close(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 if not self.closed:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 self.closed = 1
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 del self.buf, self.pos
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 def isatty(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 return 0
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 def seek(self, pos, mode = 0):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if mode == 1:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 pos = pos + self.pos
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 elif mode == 2:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 pos = pos + len(self.buf)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 self.pos = max(0, pos)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 def tell(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return self.pos
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 def read(self, n = -1):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 if n < 0:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 newpos = len(self.buf)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 newpos = min(self.pos+n, len(self.buf))
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 r = self.buf[self.pos:newpos].tostring()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 self.pos = newpos
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 return r
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 def readline(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 i = string.find(self.buf[self.pos:].tostring(), '\n')
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 if i < 0:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 newpos = len(self.buf)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 newpos = self.pos+i+1
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 r = self.buf[self.pos:newpos].tostring()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 self.pos = newpos
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 return r
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 def readlines(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 lines = string.splitfields(self.read(), '\n')
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 if not lines:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 return lines
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 for i in range(len(lines)-1):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 lines[i] = lines[i] + '\n'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 if not lines[-1]:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 del lines[-1]
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 return lines
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 def write(self, s):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 if not s: return
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 a = array('c', s)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 n = self.pos - len(self.buf)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 if n > 0:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 self.buf[len(self.buf):] = array('c', '\0')*n
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 newpos = self.pos + len(a)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 self.buf[self.pos:newpos] = a
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 self.pos = newpos
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 def writelines(self, list):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 self.write(string.joinfields(list, ''))
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 def flush(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 pass
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 def getvalue(self):
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 return self.buf.tostring()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 # A little test suite
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 def test():
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 import sys
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 if sys.argv[1:]:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 file = sys.argv[1]
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 else:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 file = '/etc/passwd'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 lines = open(file, 'r').readlines()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 text = open(file, 'r').read()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 f = ArrayIO()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 for line in lines[:-2]:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 f.write(line)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 f.writelines(lines[-2:])
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 if f.getvalue() != text:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 raise RuntimeError, 'write failed'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 length = f.tell()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 print 'File length =', length
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 f.seek(len(lines[0]))
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 f.write(lines[1])
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 f.seek(0)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 print 'First line =', `f.readline()`
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 here = f.tell()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 line = f.readline()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 print 'Second line =', `line`
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 f.seek(-len(line), 1)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 line2 = f.read(len(line))
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 if line != line2:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 raise RuntimeError, 'bad result after seek back'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 f.seek(len(line2), 1)
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 list = f.readlines()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 line = list[-1]
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 f.seek(f.tell() - len(line))
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 line2 = f.read()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 if line != line2:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 raise RuntimeError, 'bad result after seek back from EOF'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 print 'Read', len(list), 'more lines'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 print 'File length =', f.tell()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 if f.tell() != length:
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 raise RuntimeError, 'bad length'
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 f.close()
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 if __name__ == '__main__':
b41019557b6e Fuck. For PC support, this must be in the distribution.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 test()