Prime the cache with a fake __init__.py file. This makes code that looks for path believe an empty file by that name exists. Should only be called after init_under_package_root() returns True.
(self, path: str)
| 137 | return ok |
| 138 | |
| 139 | def _fake_init(self, path: str) -> os.stat_result: |
| 140 | """Prime the cache with a fake __init__.py file. |
| 141 | |
| 142 | This makes code that looks for path believe an empty file by |
| 143 | that name exists. Should only be called after |
| 144 | init_under_package_root() returns True. |
| 145 | """ |
| 146 | dirname, basename = os.path.split(path) |
| 147 | assert basename == "__init__.py", path |
| 148 | assert not os.path.exists(path), path # Not cached! |
| 149 | dirname = os.path.normpath(dirname) |
| 150 | st = os.stat(dirname) # May raise OSError |
| 151 | # Get stat result as a list so we can modify it. |
| 152 | seq: list[float] = list(st) |
| 153 | seq[stat.ST_MODE] = stat.S_IFREG | 0o444 |
| 154 | seq[stat.ST_INO] = 1 |
| 155 | seq[stat.ST_NLINK] = 1 |
| 156 | seq[stat.ST_SIZE] = 0 |
| 157 | st = os.stat_result(seq) |
| 158 | # Make listdir() and read() also pretend this file exists. |
| 159 | self.fake_package_cache.add(dirname) |
| 160 | return st |
| 161 | |
| 162 | def listdir(self, path: str) -> list[str]: |
| 163 | path = os.path.normpath(path) |
no test coverage detected