ยปCore Development>Code coverage>Lib/posixfile.py

Python code coverage for Lib/posixfile.py

#countcontent
1n/a"""Extended file operations available in POSIX.
2n/a
3n/af = posixfile.open(filename, [mode, [bufsize]])
4n/a will create a new posixfile object
5n/a
6n/af = posixfile.fileopen(fileobject)
7n/a will create a posixfile object from a builtin file object
8n/a
9n/af.file()
10n/a will return the original builtin file object
11n/a
12n/af.dup()
13n/a will return a new file object based on a new filedescriptor
14n/a
15n/af.dup2(fd)
16n/a will return a new file object based on the given filedescriptor
17n/a
18n/af.flags(mode)
19n/a will turn on the associated flag (merge)
20n/a mode can contain the following characters:
21n/a
22n/a (character representing a flag)
23n/a a append only flag
24n/a c close on exec flag
25n/a n no delay flag
26n/a s synchronization flag
27n/a (modifiers)
28n/a ! turn flags 'off' instead of default 'on'
29n/a = copy flags 'as is' instead of default 'merge'
30n/a ? return a string in which the characters represent the flags
31n/a that are set
32n/a
33n/a note: - the '!' and '=' modifiers are mutually exclusive.
34n/a - the '?' modifier will return the status of the flags after they
35n/a have been changed by other characters in the mode string
36n/a
37n/af.lock(mode [, len [, start [, whence]]])
38n/a will (un)lock a region
39n/a mode can contain the following characters:
40n/a
41n/a (character representing type of lock)
42n/a u unlock
43n/a r read lock
44n/a w write lock
45n/a (modifiers)
46n/a | wait until the lock can be granted
47n/a ? return the first lock conflicting with the requested lock
48n/a or 'None' if there is no conflict. The lock returned is in the
49n/a format (mode, len, start, whence, pid) where mode is a
50n/a character representing the type of lock ('r' or 'w')
51n/a
52n/a note: - the '?' modifier prevents a region from being locked; it is
53n/a query only
541"""
551import warnings
561warnings.warn("The posixfile module is deprecated; "
571 "fcntl.lockf() provides better locking", DeprecationWarning, 2)
58n/a
592class _posixfile_:
601 """File wrapper class that provides extra POSIX file routines."""
61n/a
621 states = ['open', 'closed']
63n/a
64n/a #
65n/a # Internal routines
66n/a #
671 def __repr__(self):
680 file = self._file_
690 return "<%s posixfile '%s', mode '%s' at %s>" % \
700 (self.states[file.closed], file.name, file.mode, \
710 hex(id(self))[2:])
72n/a
73n/a #
74n/a # Initialization routines
75n/a #
761 def open(self, name, mode='r', bufsize=-1):
770 import __builtin__
780 return self.fileopen(__builtin__.open(name, mode, bufsize))
79n/a
801 def fileopen(self, file):
810 import types
820 if repr(type(file)) != "<type 'file'>":
830 raise TypeError, 'posixfile.fileopen() arg must be file object'
840 self._file_ = file
85n/a # Copy basic file methods
860 for maybemethod in dir(file):
870 if not maybemethod.startswith('_'):
880 attr = getattr(file, maybemethod)
890 if isinstance(attr, types.BuiltinMethodType):
900 setattr(self, maybemethod, attr)
910 return self
92n/a
93n/a #
94n/a # New methods
95n/a #
961 def file(self):
970 return self._file_
98n/a
991 def dup(self):
1000 import posix
101n/a
1020 if not hasattr(posix, 'fdopen'):
1030 raise AttributeError, 'dup() method unavailable'
104n/a
1050 return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
106n/a
1071 def dup2(self, fd):
1080 import posix
109n/a
1100 if not hasattr(posix, 'fdopen'):
1110 raise AttributeError, 'dup() method unavailable'
112n/a
1130 posix.dup2(self._file_.fileno(), fd)
1140 return posix.fdopen(fd, self._file_.mode)
115n/a
1161 def flags(self, *which):
1170 import fcntl, os
118n/a
1190 if which:
1200 if len(which) > 1:
1210 raise TypeError, 'Too many arguments'
1220 which = which[0]
1230 else: which = '?'
124n/a
1250 l_flags = 0
1260 if 'n' in which: l_flags = l_flags | os.O_NDELAY
1270 if 'a' in which: l_flags = l_flags | os.O_APPEND
1280 if 's' in which: l_flags = l_flags | os.O_SYNC
129n/a
1300 file = self._file_
131n/a
1320 if '=' not in which:
1330 cur_fl = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0)
1340 if '!' in which: l_flags = cur_fl & ~ l_flags
1350 else: l_flags = cur_fl | l_flags
136n/a
1370 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFL, l_flags)
138n/a
1390 if 'c' in which:
1400 arg = ('!' not in which) # 0 is don't, 1 is do close on exec
1410 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFD, arg)
142n/a
1430 if '?' in which:
1440 which = '' # Return current flags
1450 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0)
1460 if os.O_APPEND & l_flags: which = which + 'a'
1470 if fcntl.fcntl(file.fileno(), fcntl.F_GETFD, 0) & 1:
1480 which = which + 'c'
1490 if os.O_NDELAY & l_flags: which = which + 'n'
1500 if os.O_SYNC & l_flags: which = which + 's'
1510 return which
152n/a
1531 def lock(self, how, *args):
1540 import struct, fcntl
155n/a
1560 if 'w' in how: l_type = fcntl.F_WRLCK
1570 elif 'r' in how: l_type = fcntl.F_RDLCK
1580 elif 'u' in how: l_type = fcntl.F_UNLCK
1590 else: raise TypeError, 'no type of lock specified'
160n/a
1610 if '|' in how: cmd = fcntl.F_SETLKW
1620 elif '?' in how: cmd = fcntl.F_GETLK
1630 else: cmd = fcntl.F_SETLK
164n/a
1650 l_whence = 0
1660 l_start = 0
1670 l_len = 0
168n/a
1690 if len(args) == 1:
1700 l_len = args[0]
1710 elif len(args) == 2:
1720 l_len, l_start = args
1730 elif len(args) == 3:
1740 l_len, l_start, l_whence = args
1750 elif len(args) > 3:
1760 raise TypeError, 'too many arguments'
177n/a
178n/a # Hack by davem@magnet.com to get locking to go on freebsd;
179n/a # additions for AIX by Vladimir.Marangozov@imag.fr
1800 import sys, os
1810 if sys.platform in ('netbsd1',
1820 'openbsd2',
1830 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
1840 'freebsd6', 'freebsd7', 'freebsd8',
1850 'bsdos2', 'bsdos3', 'bsdos4'):
1860 flock = struct.pack('lxxxxlxxxxlhh', \
1870 l_start, l_len, os.getpid(), l_type, l_whence)
1880 elif sys.platform in ('aix3', 'aix4'):
1890 flock = struct.pack('hhlllii', \
1900 l_type, l_whence, l_start, l_len, 0, 0, 0)
191n/a else:
1920 flock = struct.pack('hhllhh', \
1930 l_type, l_whence, l_start, l_len, 0, 0)
194n/a
1950 flock = fcntl.fcntl(self._file_.fileno(), cmd, flock)
196n/a
1970 if '?' in how:
1980 if sys.platform in ('netbsd1',
1990 'openbsd2',
2000 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5',
2010 'bsdos2', 'bsdos3', 'bsdos4'):
202n/a l_start, l_len, l_pid, l_type, l_whence = \
2030 struct.unpack('lxxxxlxxxxlhh', flock)
2040 elif sys.platform in ('aix3', 'aix4'):
205n/a l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs = \
2060 struct.unpack('hhlllii', flock)
2070 elif sys.platform == "linux2":
208n/a l_type, l_whence, l_start, l_len, l_pid, l_sysid = \
2090 struct.unpack('hhllhh', flock)
210n/a else:
211n/a l_type, l_whence, l_start, l_len, l_sysid, l_pid = \
2120 struct.unpack('hhllhh', flock)
213n/a
2140 if l_type != fcntl.F_UNLCK:
2150 if l_type == fcntl.F_RDLCK:
2160 return 'r', l_len, l_start, l_whence, l_pid
217n/a else:
2180 return 'w', l_len, l_start, l_whence, l_pid
219n/a
2201def open(name, mode='r', bufsize=-1):
221n/a """Public routine to open a file as a posixfile object."""
2220 return _posixfile_().open(name, mode, bufsize)
223n/a
2241def fileopen(file):
225n/a """Public routine to get a posixfile object from a Python file object."""
2260 return _posixfile_().fileopen(file)
227n/a
228n/a#
229n/a# Constants
230n/a#
2311SEEK_SET = 0
2321SEEK_CUR = 1
2331SEEK_END = 2
234n/a
235n/a#
236n/a# End of posixfile.py
237n/a#