Mercurial > cpython-lru_cache-weakref
annotate Lib/posixfile.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 365913d7af7d |
| children |
| rev | line source |
|---|---|
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
1 """Extended file operations available in POSIX. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
2 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
3 f = posixfile.open(filename, [mode, [bufsize]]) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
4 will create a new posixfile object |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
5 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
6 f = posixfile.fileopen(fileobject) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
7 will create a posixfile object from a builtin file object |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
8 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
9 f.file() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
10 will return the original builtin file object |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
11 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
12 f.dup() |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
13 will return a new file object based on a new filedescriptor |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
14 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
15 f.dup2(fd) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
16 will return a new file object based on the given filedescriptor |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
17 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
18 f.flags(mode) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
19 will turn on the associated flag (merge) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
20 mode can contain the following characters: |
| 1642 | 21 |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
22 (character representing a flag) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
23 a append only flag |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
24 c close on exec flag |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
25 n no delay flag |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
26 s synchronization flag |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
27 (modifiers) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
28 ! turn flags 'off' instead of default 'on' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
29 = copy flags 'as is' instead of default 'merge' |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
30 ? return a string in which the characters represent the flags |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
31 that are set |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
32 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
33 note: - the '!' and '=' modifiers are mutually exclusive. |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
34 - the '?' modifier will return the status of the flags after they |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
35 have been changed by other characters in the mode string |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
36 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
37 f.lock(mode [, len [, start [, whence]]]) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
38 will (un)lock a region |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
39 mode can contain the following characters: |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
40 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
41 (character representing type of lock) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
42 u unlock |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
43 r read lock |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
44 w write lock |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
45 (modifiers) |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
46 | wait until the lock can be granted |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
47 ? return the first lock conflicting with the requested lock |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
48 or 'None' if there is no conflict. The lock returned is in the |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
49 format (mode, len, start, whence, pid) where mode is a |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
50 character representing the type of lock ('r' or 'w') |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
51 |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
52 note: - the '?' modifier prevents a region from being locked; it is |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
53 query only |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
54 """ |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
35238
diff
changeset
|
55 import warnings |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
35238
diff
changeset
|
56 warnings.warn("The posixfile module is deprecated; " |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
35238
diff
changeset
|
57 "fcntl.lockf() provides better locking", DeprecationWarning, 2) |
|
20659
ce13aa31e189
Add a warning to the posixfile module stating that it will go away.
Fred Drake <fdrake@acm.org>
parents:
19958
diff
changeset
|
58 |
| 1642 | 59 class _posixfile_: |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
60 """File wrapper class that provides extra POSIX file routines.""" |
|
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
61 |
|
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): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
68 file = self._file_ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
69 return "<%s posixfile '%s', mode '%s' at %s>" % \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
70 (self.states[file.closed], file.name, file.mode, \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
71 hex(id(self))[2:]) |
| 1642 | 72 |
| 73 # | |
| 74 # Initialization routines | |
| 75 # | |
|
2566
919c53a35efa
default mode="r" and add optional bufsize
Guido van Rossum <guido@python.org>
parents:
1643
diff
changeset
|
76 def open(self, name, mode='r', bufsize=-1): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
77 import __builtin__ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
78 return self.fileopen(__builtin__.open(name, mode, bufsize)) |
| 1642 | 79 |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
80 def fileopen(self, file): |
|
19958
6081d736167b
This module didn't work at all anymore -- blew up with AttributeError
Tim Peters <tim.peters@gmail.com>
parents:
19214
diff
changeset
|
81 import types |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
20659
diff
changeset
|
82 if repr(type(file)) != "<type 'file'>": |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
83 raise TypeError, 'posixfile.fileopen() arg must be file object' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
84 self._file_ = file |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
85 # Copy basic file methods |
|
19958
6081d736167b
This module didn't work at all anymore -- blew up with AttributeError
Tim Peters <tim.peters@gmail.com>
parents:
19214
diff
changeset
|
86 for maybemethod in dir(file): |
|
6081d736167b
This module didn't work at all anymore -- blew up with AttributeError
Tim Peters <tim.peters@gmail.com>
parents:
19214
diff
changeset
|
87 if not maybemethod.startswith('_'): |
|
6081d736167b
This module didn't work at all anymore -- blew up with AttributeError
Tim Peters <tim.peters@gmail.com>
parents:
19214
diff
changeset
|
88 attr = getattr(file, maybemethod) |
|
6081d736167b
This module didn't work at all anymore -- blew up with AttributeError
Tim Peters <tim.peters@gmail.com>
parents:
19214
diff
changeset
|
89 if isinstance(attr, types.BuiltinMethodType): |
|
6081d736167b
This module didn't work at all anymore -- blew up with AttributeError
Tim Peters <tim.peters@gmail.com>
parents:
19214
diff
changeset
|
90 setattr(self, maybemethod, attr) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
91 return self |
| 1642 | 92 |
| 93 # | |
| 94 # New methods | |
| 95 # | |
| 96 def file(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
97 return self._file_ |
| 1642 | 98 |
| 99 def dup(self): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
100 import posix |
| 1642 | 101 |
|
17379
0f6d8fe4a3a9
Fix two unqualified except: clauses.
Guido van Rossum <guido@python.org>
parents:
16657
diff
changeset
|
102 if not hasattr(posix, 'fdopen'): |
|
0f6d8fe4a3a9
Fix two unqualified except: clauses.
Guido van Rossum <guido@python.org>
parents:
16657
diff
changeset
|
103 raise AttributeError, 'dup() method unavailable' |
| 1642 | 104 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
105 return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode) |
| 1642 | 106 |
| 107 def dup2(self, fd): | |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
108 import posix |
| 1642 | 109 |
|
17379
0f6d8fe4a3a9
Fix two unqualified except: clauses.
Guido van Rossum <guido@python.org>
parents:
16657
diff
changeset
|
110 if not hasattr(posix, 'fdopen'): |
|
0f6d8fe4a3a9
Fix two unqualified except: clauses.
Guido van Rossum <guido@python.org>
parents:
16657
diff
changeset
|
111 raise AttributeError, 'dup() method unavailable' |
| 1642 | 112 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
113 posix.dup2(self._file_.fileno(), fd) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
114 return posix.fdopen(fd, self._file_.mode) |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
115 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
116 def flags(self, *which): |
|
19214
d37e17658048
Add forgotten import (PyChecker)
Andrew M. Kuchling <amk@amk.ca>
parents:
17763
diff
changeset
|
117 import fcntl, os |
| 1642 | 118 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
119 if which: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
120 if len(which) > 1: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
121 raise TypeError, 'Too many arguments' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
122 which = which[0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
123 else: which = '?' |
| 1642 | 124 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
125 l_flags = 0 |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
126 if 'n' in which: l_flags = l_flags | os.O_NDELAY |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
127 if 'a' in which: l_flags = l_flags | os.O_APPEND |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
128 if 's' in which: l_flags = l_flags | os.O_SYNC |
| 1642 | 129 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
130 file = self._file_ |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
131 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
132 if '=' not in which: |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
133 cur_fl = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
134 if '!' in which: l_flags = cur_fl & ~ l_flags |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
135 else: l_flags = cur_fl | l_flags |
| 1642 | 136 |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
137 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFL, l_flags) |
| 1642 | 138 |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14494
diff
changeset
|
139 if 'c' in which: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
140 arg = ('!' not in which) # 0 is don't, 1 is do close on exec |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
141 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_SETFD, arg) |
| 1642 | 142 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
143 if '?' in which: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
144 which = '' # Return current flags |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
145 l_flags = fcntl.fcntl(file.fileno(), fcntl.F_GETFL, 0) |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
146 if os.O_APPEND & l_flags: which = which + 'a' |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
147 if fcntl.fcntl(file.fileno(), fcntl.F_GETFD, 0) & 1: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
148 which = which + 'c' |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
149 if os.O_NDELAY & l_flags: which = which + 'n' |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
150 if os.O_SYNC & l_flags: which = which + 's' |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
151 return which |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14494
diff
changeset
|
152 |
| 1642 | 153 def lock(self, how, *args): |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
154 import struct, fcntl |
| 1642 | 155 |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
156 if 'w' in how: l_type = fcntl.F_WRLCK |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
157 elif 'r' in how: l_type = fcntl.F_RDLCK |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
158 elif 'u' in how: l_type = fcntl.F_UNLCK |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
159 else: raise TypeError, 'no type of lock specified' |
| 1642 | 160 |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
161 if '|' in how: cmd = fcntl.F_SETLKW |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
162 elif '?' in how: cmd = fcntl.F_GETLK |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
163 else: cmd = fcntl.F_SETLK |
| 1642 | 164 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
165 l_whence = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
166 l_start = 0 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
167 l_len = 0 |
| 1642 | 168 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
169 if len(args) == 1: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
170 l_len = args[0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
171 elif len(args) == 2: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
172 l_len, l_start = args |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
173 elif len(args) == 3: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
174 l_len, l_start, l_whence = args |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
175 elif len(args) > 3: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
176 raise TypeError, 'too many arguments' |
| 1642 | 177 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
178 # Hack by davem@magnet.com to get locking to go on freebsd; |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
179 # 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
|
180 import sys, os |
|
10277
86035ae5306c
According to Jeffrey Honig, bsd/os 2.0 - 4.0 should be added to the
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
181 if sys.platform in ('netbsd1', |
|
11526
5c135ceab20c
According to Craig H Rowland, openbsd2 is yet another BSD variant that
Guido van Rossum <guido@python.org>
parents:
10277
diff
changeset
|
182 'openbsd2', |
|
14243
b2a8292c39a9
Add support for FreeBSD-[45].
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
183 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', |
|
43703
365913d7af7d
Enable platform-specific tweaks for FreeBSD 8 (exactly same to FreeBSD 7's yet)
Hye-Shik Chang <hyeshik@gmail.com>
parents:
41824
diff
changeset
|
184 'freebsd6', 'freebsd7', 'freebsd8', |
|
35238
526faaab00eb
Add support for FreeBSD 7.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
34545
diff
changeset
|
185 'bsdos2', 'bsdos3', 'bsdos4'): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
186 flock = struct.pack('lxxxxlxxxxlhh', \ |
|
15870
010d03853546
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
14494
diff
changeset
|
187 l_start, l_len, os.getpid(), l_type, l_whence) |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
34101
diff
changeset
|
188 elif sys.platform in ('aix3', 'aix4'): |
|
4073
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
189 flock = struct.pack('hhlllii', \ |
|
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
190 l_type, l_whence, l_start, l_len, 0, 0, 0) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
191 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
192 flock = struct.pack('hhllhh', \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
193 l_type, l_whence, l_start, l_len, 0, 0) |
|
3823
9aac37892e88
Changes to make it work on FreeBSD 2.x.
Guido van Rossum <guido@python.org>
parents:
2956
diff
changeset
|
194 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
195 flock = fcntl.fcntl(self._file_.fileno(), cmd, flock) |
| 1642 | 196 |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
197 if '?' in how: |
|
10277
86035ae5306c
According to Jeffrey Honig, bsd/os 2.0 - 4.0 should be added to the
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
198 if sys.platform in ('netbsd1', |
|
11526
5c135ceab20c
According to Craig H Rowland, openbsd2 is yet another BSD variant that
Guido van Rossum <guido@python.org>
parents:
10277
diff
changeset
|
199 'openbsd2', |
|
14243
b2a8292c39a9
Add support for FreeBSD-[45].
Guido van Rossum <guido@python.org>
parents:
11706
diff
changeset
|
200 'freebsd2', 'freebsd3', 'freebsd4', 'freebsd5', |
|
10277
86035ae5306c
According to Jeffrey Honig, bsd/os 2.0 - 4.0 should be added to the
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
201 'bsdos2', 'bsdos3', 'bsdos4'): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
202 l_start, l_len, l_pid, l_type, l_whence = \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
203 struct.unpack('lxxxxlxxxxlhh', flock) |
|
34545
9d66fba78444
Replace list of constants with tuples of constants.
Raymond Hettinger <python@rcn.com>
parents:
34101
diff
changeset
|
204 elif sys.platform in ('aix3', 'aix4'): |
|
4073
58504c9a37dc
Added locking hacks for AIX
Guido van Rossum <guido@python.org>
parents:
3823
diff
changeset
|
205 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
|
206 struct.unpack('hhlllii', flock) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
207 elif sys.platform == "linux2": |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
208 l_type, l_whence, l_start, l_len, l_pid, l_sysid = \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
209 struct.unpack('hhllhh', flock) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
210 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
211 l_type, l_whence, l_start, l_len, l_sysid, l_pid = \ |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
212 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
|
213 |
|
17763
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
214 if l_type != fcntl.F_UNLCK: |
|
65e26113b3fb
Remove all remaining uses of the FCNTL module from the standard library.
Fred Drake <fdrake@acm.org>
parents:
17379
diff
changeset
|
215 if l_type == fcntl.F_RDLCK: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
216 return 'r', l_len, l_start, l_whence, l_pid |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
217 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
6715
diff
changeset
|
218 return 'w', l_len, l_start, l_whence, l_pid |
| 1642 | 219 |
|
2597
cd5a96f6239b
optional mode, bufsize for global function open()
Guido van Rossum <guido@python.org>
parents:
2566
diff
changeset
|
220 def open(name, mode='r', bufsize=-1): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
221 """Public routine to open a file as a posixfile object.""" |
|
2597
cd5a96f6239b
optional mode, bufsize for global function open()
Guido van Rossum <guido@python.org>
parents:
2566
diff
changeset
|
222 return _posixfile_().open(name, mode, bufsize) |
| 1642 | 223 |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
224 def fileopen(file): |
|
11706
b6f52c1ef6da
More trivial comment -> docstring transformations by Ka-Ping Yee,
Guido van Rossum <guido@python.org>
parents:
11526
diff
changeset
|
225 """Public routine to get a posixfile object from a Python file object.""" |
|
1643
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
226 return _posixfile_().fileopen(file) |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
227 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
228 # |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
229 # Constants |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
230 # |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
231 SEEK_SET = 0 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
232 SEEK_CUR = 1 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
233 SEEK_END = 2 |
|
7a47fabb187c
Changes by Jaap -- added 'fileopen' method
Guido van Rossum <guido@python.org>
parents:
1642
diff
changeset
|
234 |
| 1642 | 235 # |
| 236 # End of posixfile.py | |
| 237 # |
