66# Extended file operations
77#
88# f = posixfile.open(filename, mode)
9+ # will create a new posixfile object
10+ #
11+ # f = posixfile.fileopen(fileobject)
12+ # will create a posixfile object from a builtin file object
913#
1014# f.file()
1115# will return the original builtin file object
5559#
5660
5761class _posixfile_ :
62+ states = ['open' , 'closed' ]
63+
5864 #
5965 # Internal routines
6066 #
6167 def __repr__ (self ):
62- return repr (self ._file_ )
68+ file = self ._file_
69+ return "<%s posixfile '%s', mode '%s' at %s>" % \
70+ (self .states [file .closed ], file .name , file .mode , \
71+ hex (id (self ))[2 :])
6372
6473 def __del__ (self ):
6574 self ._file_ .close ()
@@ -69,13 +78,15 @@ def __del__(self):
6978 #
7079 def open (self , name , mode ):
7180 import __builtin__
81+ return self .fileopen (__builtin__ .open (name , mode ))
7282
73- self ._name_ = name
74- self ._mode_ = mode
75- self ._file_ = __builtin__ .open (name , mode )
83+ def fileopen (self , file ):
84+ if `type(file)` != "<type 'file'>" :
85+ raise TypeError , 'posixfile.fileopen() arg must be file object'
86+ self ._file_ = file
7687 # Copy basic file methods
77- for method in self . _file_ .__methods__ :
78- setattr (self , method , getattr (self . _file_ , method ))
88+ for method in file .__methods__ :
89+ setattr (self , method , getattr (file , method ))
7990 return self
8091
8192 #
@@ -90,7 +101,7 @@ def dup(self):
90101 try : ignore = posix .fdopen
91102 except : raise AttributeError , 'dup() method unavailable'
92103
93- return posix .fdopen (posix .dup (self ._file_ .fileno ()), self ._mode_ )
104+ return posix .fdopen (posix .dup (self ._file_ .fileno ()), self ._file_ . mode )
94105
95106 def dup2 (self , fd ):
96107 import posix
@@ -99,32 +110,40 @@ def dup2(self, fd):
99110 except : raise AttributeError , 'dup() method unavailable'
100111
101112 posix .dup2 (self ._file_ .fileno (), fd )
102- return posix .fdopen (fd , self ._mode_ )
113+ return posix .fdopen (fd , self ._file_ . mode )
103114
104- def flags (self , which ):
115+ def flags (self , * which ):
105116 import fcntl , FCNTL
106117
118+ if which :
119+ if len (which ) > 1 :
120+ raise TypeError , 'Too many arguments'
121+ which = which [0 ]
122+ else : which = '?'
123+
107124 l_flags = 0
108125 if 'n' in which : l_flags = l_flags | FCNTL .O_NDELAY
109126 if 'a' in which : l_flags = l_flags | FCNTL .O_APPEND
110127 if 's' in which : l_flags = l_flags | FCNTL .O_SYNC
111128
129+ file = self ._file_
130+
112131 if '=' not in which :
113- cur_fl = fcntl .fcntl (self . _file_ .fileno (), FCNTL .F_GETFL , 0 )
132+ cur_fl = fcntl .fcntl (file .fileno (), FCNTL .F_GETFL , 0 )
114133 if '!' in which : l_flags = cur_fl & ~ l_flags
115134 else : l_flags = cur_fl | l_flags
116135
117- l_flags = fcntl .fcntl (self . _file_ .fileno (), FCNTL .F_SETFL , l_flags )
136+ l_flags = fcntl .fcntl (file .fileno (), FCNTL .F_SETFL , l_flags )
118137
119- if 'c' in which :
120- arg = ('!' not in which ) # 1 is close
121- l_flags = fcntl .fcntl (self . _file_ .fileno (), FCNTL .F_SETFD , arg )
138+ if 'c' in which :
139+ arg = ('!' not in which ) # 0 is don't, 1 is do close on exec
140+ l_flags = fcntl .fcntl (file .fileno (), FCNTL .F_SETFD , arg )
122141
123142 if '?' in which :
124- which = ''
125- l_flags = fcntl .fcntl (self . _file_ .fileno (), FCNTL .F_GETFL , 0 )
143+ which = '' # Return current flags
144+ l_flags = fcntl .fcntl (file .fileno (), FCNTL .F_GETFL , 0 )
126145 if FCNTL .O_APPEND & l_flags : which = which + 'a'
127- if fcntl .fcntl (self . _file_ .fileno (), FCNTL .F_GETFD , 0 ) & 1 :
146+ if fcntl .fcntl (file .fileno (), FCNTL .F_GETFD , 0 ) & 1 :
128147 which = which + 'c'
129148 if FCNTL .O_NDELAY & l_flags : which = which + 'n'
130149 if FCNTL .O_SYNC & l_flags : which = which + 's'
@@ -173,6 +192,16 @@ def lock(self, how, *args):
173192def open (name , mode ):
174193 return _posixfile_ ().open (name , mode )
175194
195+ def fileopen (file ):
196+ return _posixfile_ ().fileopen (file )
197+
198+ #
199+ # Constants
200+ #
201+ SEEK_SET = 0
202+ SEEK_CUR = 1
203+ SEEK_END = 2
204+
176205#
177206# End of posixfile.py
178207#
0 commit comments