Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/util/walk.py @ 199:b2aba84ca426
Implement a "close()" method for cutils.util.walk.ScanDir.
Also check for closed iterators when __next__ is called.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 17 Jan 2025 15:24:08 +0100 |
| parents | c1e875ba4bdc |
| children | 58d93453c307 |
comparison
equal
deleted
inserted
replaced
| 198:c1e875ba4bdc | 199:b2aba84ca426 |
|---|---|
| 169 | 169 |
| 170 def __iter__(self): | 170 def __iter__(self): |
| 171 return self | 171 return self |
| 172 | 172 |
| 173 def __next__(self): | 173 def __next__(self): |
| 174 if self._scandir_it is None: | |
| 175 raise StopIteration("closed") | |
| 174 return WalkDirEntry.from_direntry(next(self._scandir_it)) | 176 return WalkDirEntry.from_direntry(next(self._scandir_it)) |
| 175 | 177 |
| 176 if PY2: | 178 if PY2: |
| 177 next = __next__ | 179 next = __next__ |
| 178 | 180 |
| 179 def __enter__(self): | 181 def __enter__(self): |
| 180 return self | 182 return self |
| 181 | 183 |
| 182 def __exit__(self, *args, **kwds): | 184 def __exit__(self, *args, **kwds): |
| 183 if hasattr(self._scandir_it, "close"): | 185 self.close() |
| 184 self._scandir_it.close() | 186 |
| 187 def close(self): | |
| 188 if self._scandir_it is not None: | |
| 189 if hasattr(self._scandir_it, "close"): | |
| 190 self._scandir_it.close() | |
| 191 self._scandir_it = None | |
| 185 | 192 |
| 186 else: | 193 else: |
| 187 | 194 |
| 188 class ScanDir(object): | 195 class ScanDir(object): |
| 189 | 196 |
| 201 | 208 |
| 202 def __iter__(self): | 209 def __iter__(self): |
| 203 return self | 210 return self |
| 204 | 211 |
| 205 def __next__(self): | 212 def __next__(self): |
| 213 if self._listdir_it is None: | |
| 214 raise StopIteration("closed") | |
| 206 return WalkDirEntry.from_path_name(self._path, | 215 return WalkDirEntry.from_path_name(self._path, |
| 207 next(self._listdir_it)) | 216 next(self._listdir_it)) |
| 208 | 217 |
| 209 if PY2: | 218 if PY2: |
| 210 next = __next__ | 219 next = __next__ |
| 211 | 220 |
| 212 def __enter__(self): | 221 def __enter__(self): |
| 213 return self | 222 return self |
| 214 | 223 |
| 215 def __exit__(self, *args, **kwds): | 224 def __exit__(self, *args, **kwds): |
| 225 pass | |
| 226 | |
| 227 def close(self): | |
| 216 pass | 228 pass |
| 217 | 229 |
| 218 | 230 |
| 219 def getfsencoding(): | 231 def getfsencoding(): |
| 220 """Return the stored _FSENCODING of this module""" | 232 """Return the stored _FSENCODING of this module""" |
