| 140 | # If dironly is false, yields all file names inside a directory. |
| 141 | # If dironly is true, yields only directory names. |
| 142 | def _iterdir(dirname, dir_fd, dironly): |
| 143 | try: |
| 144 | fd = None |
| 145 | fsencode = None |
| 146 | if dir_fd is not None: |
| 147 | if dirname: |
| 148 | fd = arg = os.open(dirname, _dir_open_flags, dir_fd=dir_fd) |
| 149 | else: |
| 150 | arg = dir_fd |
| 151 | if isinstance(dirname, bytes): |
| 152 | fsencode = os.fsencode |
| 153 | elif dirname: |
| 154 | arg = dirname |
| 155 | elif isinstance(dirname, bytes): |
| 156 | arg = bytes(os.curdir, 'ASCII') |
| 157 | else: |
| 158 | arg = os.curdir |
| 159 | try: |
| 160 | with os.scandir(arg) as it: |
| 161 | for entry in it: |
| 162 | try: |
| 163 | if not dironly or entry.is_dir(): |
| 164 | if fsencode is not None: |
| 165 | yield fsencode(entry.name) |
| 166 | else: |
| 167 | yield entry.name |
| 168 | except OSError: |
| 169 | pass |
| 170 | finally: |
| 171 | if fd is not None: |
| 172 | os.close(fd) |
| 173 | except OSError: |
| 174 | return |
| 175 | |
| 176 | def _listdir(dirname, dir_fd, dironly): |
| 177 | with contextlib.closing(_iterdir(dirname, dir_fd, dironly)) as it: |