Get module source and parse inline mypy configurations.
(self)
| 3147 | # Methods for processing modules from source code. |
| 3148 | |
| 3149 | def get_source(self) -> str: |
| 3150 | """Get module source and parse inline mypy configurations.""" |
| 3151 | manager = self.manager |
| 3152 | t0 = time_ref() |
| 3153 | |
| 3154 | with self.wrap_context(): |
| 3155 | source = self.source |
| 3156 | if self.path and source is None: |
| 3157 | try: |
| 3158 | path = manager.maybe_swap_for_shadow_path(self.path) |
| 3159 | source = decode_python_encoding(manager.fscache.read(path)) |
| 3160 | self.source_hash = manager.fscache.hash_digest(path) |
| 3161 | except OSError as ioerr: |
| 3162 | # ioerr.strerror differs for os.stat failures between Windows and |
| 3163 | # other systems, but os.strerror(ioerr.errno) does not, so we use that. |
| 3164 | # (We want the error messages to be platform-independent so that the |
| 3165 | # tests have predictable output.) |
| 3166 | assert ioerr.errno is not None |
| 3167 | raise CompileError( |
| 3168 | [ |
| 3169 | "mypy: error: Cannot read file '{}': {}".format( |
| 3170 | self.path.replace(os.getcwd() + os.sep, ""), |
| 3171 | os.strerror(ioerr.errno), |
| 3172 | ) |
| 3173 | ], |
| 3174 | module_with_blocker=self.id, |
| 3175 | ) from ioerr |
| 3176 | except (UnicodeDecodeError, DecodeError) as decodeerr: |
| 3177 | if self.path.endswith(".pyd"): |
| 3178 | err = f"{self.path}: error: Stubgen does not support .pyd files" |
| 3179 | else: |
| 3180 | err = f"{self.path}: error: Cannot decode file: {str(decodeerr)}" |
| 3181 | raise CompileError([err], module_with_blocker=self.id) from decodeerr |
| 3182 | elif self.path and self.manager.fscache.isdir(self.path): |
| 3183 | source = "" |
| 3184 | self.source_hash = "" |
| 3185 | else: |
| 3186 | assert source is not None |
| 3187 | self.source_hash = compute_hash(source) |
| 3188 | |
| 3189 | self.parse_inline_configuration(source) |
| 3190 | |
| 3191 | self.size_hint = len(source) + MIN_SIZE_HINT |
| 3192 | self.time_spent_us += time_spent_us(t0) |
| 3193 | return source |
| 3194 | |
| 3195 | def parse_file_inner(self, source: str, raw_data: FileRawData | None = None) -> None: |
| 3196 | t0 = time_ref() |
no test coverage detected