Mercurial > cpython-lru_cache-weakref
annotate Lib/posixfile.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 25301b9ea0cc |
| children | 39b413ac2cc9 |
| rev | line source |
|---|---|
| 1642 | 1 # |
| 2 # Start of posixfile.py | |
| 3 # | |
| 4 | |
| 5 # | |
| 6 # Extended file operations | |
| 7 # | |
|
2597
cd5a96f6239b
optional mode, bufsize for global function open()
Guido van Rossum <guido@python.org>
parents:
2566
diff
changeset
|
8 # f = posixfile.open(filename, [mode, [bufsize]]) |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
9 # will create a new posixfile object |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
10 # |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
11 # f = posixfile.fileopen(fileobject) |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
12 # will create a posixfile object from a builtin file object |
| 1642 | 13 # |
| 14 # f.file() | |
| 15 # will return the original builtin file object | |
| 16 # | |
| 17 # f.dup() | |
| 18 # will return a new file object based on a new filedescriptor | |
| 19 # | |
| 20 # f.dup2(fd) | |
| 21 # will return a new file object based on the given filedescriptor | |
| 22 # | |
| 23 # f.flags(mode) | |
| 24 # will turn on the associated flag (merge) | |
| 25 # mode can contain the following characters: | |
| 26 # | |
| 27 # (character representing a flag) | |
| 28 # a append only flag | |
| 29 # c close on exec flag | |
| 30 # n no delay flag | |
| 31 # s synchronization flag | |
| 32 # (modifiers) | |
| 33 # ! turn flags 'off' instead of default 'on' | |
| 34 # = copy flags 'as is' instead of default 'merge' | |
| 35 # ? return a string in which the characters represent the flags | |
| 36 # that are set | |
| 37 # | |
| 38 # note: - the '!' and '=' modifiers are mutually exclusive. | |
| 39 # - the '?' modifier will return the status of the flags after they | |
| 40 # have been changed by other characters in the mode string | |
| 41 # | |
| 42 # f.lock(mode [, len [, start [, whence]]]) | |
| 43 # will (un)lock a region | |
| 44 # mode can contain the following characters: | |
| 45 # | |
| 46 # (character representing type of lock) | |
| 47 # u unlock | |
| 48 # r read lock | |
| 49 # w write lock | |
| 50 # (modifiers) | |
| 51 # | wait until the lock can be granted | |
| 52 # ? return the first lock conflicting with the requested lock | |
| 53 # or 'None' if there is no conflict. The lock returned is in the | |
| 54 # format (mode, len, start, whence, pid) where mode is a | |
| 55 # character representing the type of lock ('r' or 'w') | |
| 56 # | |
| 57 # note: - the '?' modifier prevents a region from being locked; it is | |
| 58 # query only | |
| 59 # | |
| 60 | |
| 61 class _posixfile_: | |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
62 states = ['open', 'closed'] |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
63 |
| 1642 | 64 # |
| 65 # Internal routines | |
| 66 # | |
| 67 def __repr__(self): | |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
68 file = self._file_ |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
69 return "<%s posixfile '%s', mode '%s' at %s>" % \ |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
70 (self.states[file.closed], file.name, file.mode, \ |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
71 hex(id(self))[2:]) |
| 1642 | 72 |
| 73 def __del__(self): | |
| 74 self._file_.close() | |
| 75 | |
| 76 # | |
| 77 # Initialization routines | |
| 78 # | |
|
2566
919c53a35efa
default mode="r" and add optional bufsize
Guido van Rossum <guido@python.org>
parents:
1643
diff
changeset
|
79 def open(self, name, mode='r', bufsize=-1): |
| 1642 | 80 import __builtin__ |
|
2566
919c53a35efa
default mode="r" and add optional bufsize
Guido van Rossum <guido@python.org>
parents:
1643
diff
changeset
|
81 return self.fileopen(__builtin__.open(name, mode, bufsize)) |
| 1642 | 82 |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
83 def fileopen(self, file): |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
84 if `type(file)` != "<type 'file'>": |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
85 raise TypeError, 'posixfile.fileopen() arg must be file object' |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
86 self._file_ = file |
| 1642 | 87 # Copy basic file methods |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
88 for method in file.__methods__: |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
89 setattr(self, method, getattr(file, method)) |
| 1642 | 90 return self |
| 91 | |
| 92 # | |
| 93 # New methods | |
| 94 # | |
| 95 def file(self): | |
| 96 return self._file_ | |
| 97 | |
| 98 def dup(self): | |
| 99 import posix | |
| 100 | |
| 101 try: ignore = posix.fdopen | |
| 102 except: raise AttributeError, 'dup() method unavailable' | |
| 103 | |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
104 return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode) |
| 1642 | 105 |
| 106 def dup2(self, fd): | |
| 107 import posix | |
| 108 | |
| 109 try: ignore = posix.fdopen | |
| 110 except: raise AttributeError, 'dup() method unavailable' | |
| 111 | |
| 112 posix.dup2(self._file_.fileno(), fd) | |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
113 return posix.fdopen(fd, self._file_.mode) |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
114 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
115 def flags(self, *which): |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
116 import fcntl, FCNTL |
| 1642 | 117 |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
118 if which: |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
119 if len(which) > 1: |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
120 raise TypeError, 'Too many arguments' |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
121 which = which[0] |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
122 else: which = '?' |
| 1642 | 123 |
| 124 l_flags = 0 | |
| 125 if 'n' in which: l_flags = l_flags | FCNTL.O_NDELAY | |
| 126 if 'a' in which: l_flags = l_flags | FCNTL.O_APPEND | |
| 127 if 's' in which: l_flags = l_flags | FCNTL.O_SYNC | |
| 128 | |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
129 file = self._file_ |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
130 |
| 1642 | 131 if '=' not in which: |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
132 cur_fl = fcntl.fcntl(file.fileno(), FCNTL.F_GETFL, 0) |
| 1642 | 133 if '!' in which: l_flags = cur_fl & ~ l_flags |
| 134 else: l_flags = cur_fl | l_flags | |
| 135 | |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
136 l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_SETFL, l_flags) |
| 1642 | 137 |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
138 if 'c' in which: |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
139 arg = ('!' not in which) # 0 is don't, 1 is do close on exec |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
140 l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_SETFD, arg) |
| 1642 | 141 |
| 142 if '?' in which: | |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
143 which = '' # Return current flags |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
144 l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_GETFL, 0) |
| 1642 | 145 if FCNTL.O_APPEND & l_flags: which = which + 'a' |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
146 if fcntl.fcntl(file.fileno(), FCNTL.F_GETFD, 0) & 1: |
| 1642 | 147 which = which + 'c' |
| 148 if FCNTL.O_NDELAY & l_flags: which = which + 'n' | |
| 149 if FCNTL.O_SYNC & l_flags: which = which + 's' | |
| 150 return which | |
| 151 | |
| 152 def lock(self, how, *args): | |
| 153 import struct, fcntl, FCNTL | |
| 154 | |
| 155 if 'w' in how: l_type = FCNTL.F_WRLCK | |
|
2956
4d7fd9ff798d
fix stupid typo: r->RDLK
Guido van Rossum <guido@python.org>
parents:
2597
diff
changeset
|
156 elif 'r' in how: l_type = FCNTL.F_RDLCK |
| 1642 | 157 elif 'u' in how: l_type = FCNTL.F_UNLCK |
| 158 else: raise TypeError, 'no type of lock specified' | |
| 159 | |
| 160 if '|' in how: cmd = FCNTL.F_SETLKW | |
| 161 elif '?' in how: cmd = FCNTL.F_GETLK | |
| 162 else: cmd = FCNTL.F_SETLK | |
| 163 | |
| 164 l_whence = 0 | |
| 165 l_start = 0 | |
| 166 l_len = 0 | |
| 167 | |
| 168 if len(args) == 1: | |
| 169 l_len = args[0] | |
| 170 elif len(args) == 2: | |
| 171 l_len, l_start = args | |
| 172 elif len(args) == 3: | |
| 173 l_len, l_start, l_whence = args | |
| 174 elif len(args) > 3: | |
| 175 raise TypeError, 'too many arguments' | |
| 176 | |
|
4073
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
177 # Hack by davem@magnet.com to get locking to go on freebsd; |
|
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
178 # additions for AIX by Vladimir.Marangozov@imag.fr |
|
3823
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
179 import sys, os |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
180 if sys.platform == 'freebsd2': |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
181 flock = struct.pack('lxxxxlxxxxlhh', \ |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
182 l_start, l_len, os.getpid(), l_type, l_whence) |
|
4073
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
183 elif sys.platform in ['aix3', 'aix4']: |
|
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
184 flock = struct.pack('hhlllii', \ |
|
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
185 l_type, l_whence, l_start, l_len, 0, 0, 0) |
|
3823
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
186 else: |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
187 flock = struct.pack('hhllhh', \ |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
188 l_type, l_whence, l_start, l_len, 0, 0) |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
189 |
| 1642 | 190 flock = fcntl.fcntl(self._file_.fileno(), cmd, flock) |
| 191 | |
| 192 if '?' in how: | |
|
3823
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
193 if sys.platform == 'freebsd2': |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
194 l_start, l_len, l_pid, l_type, l_whence = \ |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
195 struct.unpack('lxxxxlxxxxlhh', flock) |
|
4073
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
196 elif sys.platform in ['aix3', 'aix4']: |
|
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
197 l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs = \ |
|
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
198 struct.unpack('hhlllii', flock) |
|
4264
25301b9ea0cc
Added linux2 specific code :-(
Guido van Rossum <guido@python.org>
parents:
4073
diff
changeset
|
199 elif sys.platform == "linux2": |
|
25301b9ea0cc
Added linux2 specific code :-(
Guido van Rossum <guido@python.org>
parents:
4073
diff
changeset
|
200 l_type, l_whence, l_start, l_len, l_pid, l_sysid = \ |
|
25301b9ea0cc
Added linux2 specific code :-(
Guido van Rossum <guido@python.org>
parents:
4073
diff
changeset
|
201 struct.unpack('hhllhh', flock) |
|
3823
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
202 else: |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
203 l_type, l_whence, l_start, l_len, l_sysid, l_pid = \ |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
204 struct.unpack('hhllhh', flock) |
|
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
205 |
| 1642 | 206 if l_type != FCNTL.F_UNLCK: |
| 207 if l_type == FCNTL.F_RDLCK: | |
| 208 return 'r', l_len, l_start, l_whence, l_pid | |
| 209 else: | |
| 210 return 'w', l_len, l_start, l_whence, l_pid | |
| 211 | |
| 212 # | |
| 213 # Public routine to obtain a posixfile object | |
| 214 # | |
|
2597
cd5a96f6239b
optional mode, bufsize for global function open()
Guido van Rossum <guido@python.org>
parents:
2566
diff
changeset
|
215 def open(name, mode='r', bufsize=-1): |
|
cd5a96f6239b
optional mode, bufsize for global function open()
Guido van Rossum <guido@python.org>
parents:
2566
diff
changeset
|
216 return _posixfile_().open(name, mode, bufsize) |
| 1642 | 217 |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
218 def fileopen(file): |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
219 return _posixfile_().fileopen(file) |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
220 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
221 # |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
222 # Constants |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
223 # |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
224 SEEK_SET = 0 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
225 SEEK_CUR = 1 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
226 SEEK_END = 2 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
227 |
| 1642 | 228 # |
| 229 # End of posixfile.py | |
| 230 # |
