| 162 | # If dironly is false, yields all file names inside a directory. |
| 163 | # If dironly is true, yields only directory names. |
| 164 | def _iterdir(dirname, dir_fd, dironly): |
| 165 | try: |
| 166 | fd = None |
| 167 | fsencode = None |
| 168 | if dir_fd is not None: |
| 169 | if dirname: |
| 170 | fd = arg = os.open(dirname, _dir_open_flags, dir_fd=dir_fd) |
| 171 | else: |
| 172 | arg = dir_fd |
| 173 | if isinstance(dirname, bytes): |
| 174 | fsencode = os.fsencode |
| 175 | elif dirname: |
| 176 | arg = dirname |
| 177 | elif isinstance(dirname, bytes): |
| 178 | arg = bytes(os.curdir, 'ASCII') |
| 179 | else: |
| 180 | arg = os.curdir |
| 181 | try: |
| 182 | with os.scandir(arg) as it: |
| 183 | for entry in it: |
| 184 | try: |
| 185 | if not dironly or entry.is_dir(): |
| 186 | if fsencode is not None: |
| 187 | yield fsencode(entry.name) |
| 188 | else: |
| 189 | yield entry.name |
| 190 | except OSError: |
| 191 | pass |
| 192 | finally: |
| 193 | if fd is not None: |
| 194 | os.close(fd) |
| 195 | except OSError: |
| 196 | return |
| 197 | |
| 198 | def _listdir(dirname, dir_fd, dironly): |
| 199 | with contextlib.closing(_iterdir(dirname, dir_fd, dironly)) as it: |