|
| 1 | +""" |
| 2 | +Remove Call nodes that are only used to raise exceptions with no arguments |
| 3 | +
|
| 4 | +If a Raise statement is used on a Name and the name refers to an exception, it is automatically instantiated with no arguments |
| 5 | +We can remove any Call nodes that are only used to raise exceptions with no arguments and let the Raise statement do the instantiation. |
| 6 | +When printed, this essentially removes the brackets from the exception name. |
| 7 | +
|
| 8 | +We can't generally know if a name refers to an exception, so we only do this for builtin exceptions |
| 9 | +""" |
| 10 | + |
| 11 | +import ast |
| 12 | + |
| 13 | +from python_minifier.rename.binding import BuiltinBinding |
| 14 | + |
| 15 | +# This list may vary between python versions |
| 16 | +builtin_exceptions = [ |
| 17 | + 'BaseException', |
| 18 | + 'BaseExceptionGroup', |
| 19 | + 'GeneratorExit', |
| 20 | + 'KeyboardInterrupt', |
| 21 | + 'SystemExit', |
| 22 | + 'Exception', |
| 23 | + 'ArithmeticError', |
| 24 | + 'FloatingPointError', |
| 25 | + 'OverflowError', |
| 26 | + 'ZeroDivisionError', |
| 27 | + 'AssertionError', |
| 28 | + 'AttributeError', |
| 29 | + 'BufferError', |
| 30 | + 'EOFError', |
| 31 | + 'ExceptionGroup', |
| 32 | + 'BaseExceptionGroup', |
| 33 | + 'ImportError', |
| 34 | + 'ModuleNotFoundError', |
| 35 | + 'LookupError', |
| 36 | + 'IndexError', |
| 37 | + 'KeyError', |
| 38 | + 'MemoryError', |
| 39 | + 'NameError', |
| 40 | + 'UnboundLocalError', |
| 41 | + 'OSError', |
| 42 | + 'BlockingIOError', |
| 43 | + 'ChildProcessError', |
| 44 | + 'ConnectionError', |
| 45 | + 'BrokenPipeError', |
| 46 | + 'ConnectionAbortedError', |
| 47 | + 'ConnectionRefusedError', |
| 48 | + 'ConnectionResetError', |
| 49 | + 'FileExistsError', |
| 50 | + 'FileNotFoundError', |
| 51 | + 'InterruptedError', |
| 52 | + 'IsADirectoryError', |
| 53 | + 'NotADirectoryError', |
| 54 | + 'PermissionError', |
| 55 | + 'ProcessLookupError', |
| 56 | + 'TimeoutError', |
| 57 | + 'ReferenceError', |
| 58 | + 'RuntimeError', |
| 59 | + 'NotImplementedError', |
| 60 | + 'RecursionError', |
| 61 | + 'StopAsyncIteration', |
| 62 | + 'StopIteration', |
| 63 | + 'SyntaxError', |
| 64 | + 'IndentationError', |
| 65 | + 'TabError', |
| 66 | + 'SystemError', |
| 67 | + 'TypeError', |
| 68 | + 'ValueError', |
| 69 | + 'UnicodeError', |
| 70 | + 'UnicodeDecodeError', |
| 71 | + 'UnicodeEncodeError', |
| 72 | + 'UnicodeTranslateError', |
| 73 | + 'Warning', |
| 74 | + 'BytesWarning', |
| 75 | + 'DeprecationWarning', |
| 76 | + 'EncodingWarning', |
| 77 | + 'FutureWarning', |
| 78 | + 'ImportWarning', |
| 79 | + 'PendingDeprecationWarning', |
| 80 | + 'ResourceWarning', |
| 81 | + 'RuntimeWarning', |
| 82 | + 'SyntaxWarning', |
| 83 | + 'UnicodeWarning', |
| 84 | + 'UserWarning' |
| 85 | +] |
| 86 | + |
| 87 | +def _remove_empty_call(binding): |
| 88 | + assert isinstance(binding, BuiltinBinding) |
| 89 | + |
| 90 | + for name_node in binding.references: |
| 91 | + assert isinstance(name_node, ast.Name) # For this to be a builtin, all references must be name nodes as it is not defined anywhere |
| 92 | + |
| 93 | + if not isinstance(name_node.parent, ast.Call): |
| 94 | + # This is not a call |
| 95 | + continue |
| 96 | + call_node = name_node.parent |
| 97 | + |
| 98 | + if not isinstance(call_node.parent, ast.Raise): |
| 99 | + # This is not a raise statement |
| 100 | + continue |
| 101 | + raise_node = call_node.parent |
| 102 | + |
| 103 | + if len(call_node.args) > 0 or len(call_node.keywords) > 0: |
| 104 | + # This is a call with arguments |
| 105 | + continue |
| 106 | + |
| 107 | + # This is an instance of the exception being called with no arguments |
| 108 | + # let's replace it with just the name, cutting out the Call node |
| 109 | + |
| 110 | + if raise_node.exc is call_node: |
| 111 | + raise_node.exc = name_node |
| 112 | + elif raise_node.cause is call_node: |
| 113 | + raise_node.cause = name_node |
| 114 | + name_node.parent = raise_node |
| 115 | + |
| 116 | +def remove_no_arg_exception_call(module): |
| 117 | + assert isinstance(module, ast.Module) |
| 118 | + |
| 119 | + for binding in module.bindings: |
| 120 | + if isinstance(binding, BuiltinBinding) and binding.name in builtin_exceptions: |
| 121 | + # We can remove any calls to builtin exceptions |
| 122 | + _remove_empty_call(binding) |
0 commit comments