Process a .pth file within the site-packages directory: For each line in the file, either combine it with sitedir to a path and add that to known_paths, or execute it if it starts with 'import '.
(sitedir, name, known_paths)
| 164 | |
| 165 | |
| 166 | def addpackage(sitedir, name, known_paths): |
| 167 | """Process a .pth file within the site-packages directory: |
| 168 | For each line in the file, either combine it with sitedir to a path |
| 169 | and add that to known_paths, or execute it if it starts with 'import '. |
| 170 | """ |
| 171 | if known_paths is None: |
| 172 | known_paths = _init_pathinfo() |
| 173 | reset = True |
| 174 | else: |
| 175 | reset = False |
| 176 | fullname = os.path.join(sitedir, name) |
| 177 | try: |
| 178 | st = os.lstat(fullname) |
| 179 | except OSError: |
| 180 | return |
| 181 | if ((getattr(st, 'st_flags', 0) & stat.UF_HIDDEN) or |
| 182 | (getattr(st, 'st_file_attributes', 0) & stat.FILE_ATTRIBUTE_HIDDEN)): |
| 183 | _trace(f"Skipping hidden .pth file: {fullname!r}") |
| 184 | return |
| 185 | _trace(f"Processing .pth file: {fullname!r}") |
| 186 | try: |
| 187 | with io.open_code(fullname) as f: |
| 188 | pth_content = f.read() |
| 189 | except OSError: |
| 190 | return |
| 191 | |
| 192 | try: |
| 193 | # Accept BOM markers in .pth files as we do in source files |
| 194 | # (Windows PowerShell 5.1 makes it hard to emit UTF-8 files without a BOM) |
| 195 | pth_content = pth_content.decode("utf-8-sig") |
| 196 | except UnicodeDecodeError: |
| 197 | # Fallback to locale encoding for backward compatibility. |
| 198 | # We will deprecate this fallback in the future. |
| 199 | import locale |
| 200 | pth_content = pth_content.decode(locale.getencoding()) |
| 201 | _trace(f"Cannot read {fullname!r} as UTF-8. " |
| 202 | f"Using fallback encoding {locale.getencoding()!r}") |
| 203 | |
| 204 | for n, line in enumerate(pth_content.splitlines(), 1): |
| 205 | if line.startswith("#"): |
| 206 | continue |
| 207 | if line.strip() == "": |
| 208 | continue |
| 209 | try: |
| 210 | if line.startswith(("import ", "import\t")): |
| 211 | exec(line) |
| 212 | continue |
| 213 | line = line.rstrip() |
| 214 | dir, dircase = makepath(sitedir, line) |
| 215 | if dircase not in known_paths and os.path.exists(dir): |
| 216 | sys.path.append(dir) |
| 217 | known_paths.add(dircase) |
| 218 | except Exception as exc: |
| 219 | print(f"Error processing line {n:d} of {fullname}:\n", |
| 220 | file=sys.stderr) |
| 221 | import traceback |
| 222 | for record in traceback.format_exception(exc): |
| 223 | for line in record.splitlines(): |
no test coverage detected
searching dependent graphs…