| 26 | |
| 27 | |
| 28 | class VerifyShadowedImport(object): |
| 29 | def __init__(self, import_name): |
| 30 | self.import_name = import_name |
| 31 | |
| 32 | def __enter__(self): |
| 33 | return self |
| 34 | |
| 35 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 36 | if exc_type is not None: |
| 37 | if exc_type == DebuggerInitializationError: |
| 38 | return False # It's already an error we generated. |
| 39 | |
| 40 | # We couldn't even import it... |
| 41 | found_at = find_in_pythonpath(self.import_name) |
| 42 | |
| 43 | if len(found_at) <= 1: |
| 44 | # It wasn't found anywhere or there was just 1 occurrence. |
| 45 | # Let's just return to show the original error. |
| 46 | return False |
| 47 | |
| 48 | # We found more than 1 occurrence of the same module in the PYTHONPATH |
| 49 | # (the user module and the standard library module). |
| 50 | # Let's notify the user as it seems that the module was shadowed. |
| 51 | msg = self._generate_shadowed_import_message(found_at) |
| 52 | raise DebuggerInitializationError(msg) |
| 53 | |
| 54 | def _generate_shadowed_import_message(self, found_at): |
| 55 | msg = """It was not possible to initialize the debugger due to a module name conflict. |
| 56 | |
| 57 | i.e.: the module "%(import_name)s" could not be imported because it is shadowed by: |
| 58 | %(found_at)s |
| 59 | Please rename this file/folder so that the original module from the standard library can be imported.""" % { |
| 60 | "import_name": self.import_name, |
| 61 | "found_at": found_at[0], |
| 62 | } |
| 63 | |
| 64 | return msg |
| 65 | |
| 66 | def check(self, module, expected_attributes): |
| 67 | msg = "" |
| 68 | for expected_attribute in expected_attributes: |
| 69 | try: |
| 70 | getattr(module, expected_attribute) |
| 71 | except: |
| 72 | msg = self._generate_shadowed_import_message([module.__file__]) |
| 73 | break |
| 74 | |
| 75 | if msg: |
| 76 | raise DebuggerInitializationError(msg) |
| 77 | |
| 78 | |
| 79 | with VerifyShadowedImport("threading") as verify_shadowed: |
no outgoing calls
no test coverage detected