Show More
@@ -123,11 +123,36 b' class mixedfilemodewrapper(object):' | |||||
123 | object.__setattr__(self, r'_lastop', self.OPREAD) |
|
123 | object.__setattr__(self, r'_lastop', self.OPREAD) | |
124 | return self._fp.readlines(*args, **kwargs) |
|
124 | return self._fp.readlines(*args, **kwargs) | |
125 |
|
125 | |||
|
126 | class fdproxy(object): | |||
|
127 | """Wraps osutil.posixfile() to override the name attribute to reflect the | |||
|
128 | underlying file name. | |||
|
129 | """ | |||
|
130 | def __init__(self, name, fp): | |||
|
131 | self.name = name | |||
|
132 | self._fp = fp | |||
|
133 | ||||
|
134 | def __enter__(self): | |||
|
135 | return self._fp.__enter__() | |||
|
136 | ||||
|
137 | def __exit__(self, exc_type, exc_value, traceback): | |||
|
138 | self._fp.__exit__(exc_type, exc_value, traceback) | |||
|
139 | ||||
|
140 | def __iter__(self): | |||
|
141 | return iter(self._fp) | |||
|
142 | ||||
|
143 | def __getattr__(self, name): | |||
|
144 | return getattr(self._fp, name) | |||
|
145 | ||||
126 | def posixfile(name, mode='r', buffering=-1): |
|
146 | def posixfile(name, mode='r', buffering=-1): | |
127 | '''Open a file with even more POSIX-like semantics''' |
|
147 | '''Open a file with even more POSIX-like semantics''' | |
128 | try: |
|
148 | try: | |
129 | fp = osutil.posixfile(name, mode, buffering) # may raise WindowsError |
|
149 | fp = osutil.posixfile(name, mode, buffering) # may raise WindowsError | |
130 |
|
150 | |||
|
151 | # PyFile_FromFd() ignores the name, and seems to report fp.name as the | |||
|
152 | # underlying file descriptor. | |||
|
153 | if pycompat.ispy3: | |||
|
154 | fp = fdproxy(name, fp) | |||
|
155 | ||||
131 | # The position when opening in append mode is implementation defined, so |
|
156 | # The position when opening in append mode is implementation defined, so | |
132 | # make it consistent with other platforms, which position at EOF. |
|
157 | # make it consistent with other platforms, which position at EOF. | |
133 | if 'a' in mode: |
|
158 | if 'a' in mode: |
General Comments 0
You need to be logged in to leave comments.
Login now