Compare the filenames associated with the given modules names. This tries to not actually load the modules.
(source_module, target_module)
| 89 | |
| 90 | |
| 91 | def _same_modules(source_module, target_module): |
| 92 | """Compare the filenames associated with the given modules names. |
| 93 | |
| 94 | This tries to not actually load the modules. |
| 95 | """ |
| 96 | if not (source_module or target_module): |
| 97 | return False |
| 98 | |
| 99 | if target_module is source_module: |
| 100 | return True |
| 101 | |
| 102 | def get_filename(module): |
| 103 | if inspect.ismodule(module): |
| 104 | return inspect.getfile(module) |
| 105 | elif ( |
| 106 | (spec := importlib.util.find_spec(module)) and |
| 107 | isinstance(spec.loader, importlib.machinery.SourceFileLoader)): |
| 108 | return spec.loader.get_filename() |
| 109 | |
| 110 | try: |
| 111 | return os.path.samefile( |
| 112 | get_filename(source_module), |
| 113 | get_filename(target_module)) |
| 114 | except (ValueError, TypeError, ImportError, FileNotFoundError): |
| 115 | return False |
| 116 | |
| 117 | |
| 118 | def derive_target_module(target_module, parent_frame): |
no test coverage detected