Skip to content

Commit bcef793

Browse files
committed
Only remove parens if we are sure it's an exception
Don't do it if the module scope is tainted - it may not be the builtin. Only do it if the builtin is an exception in all supported python versions. When we support targeting python versions this may be expanded.
1 parent 13f5ddf commit bcef793

File tree

3 files changed

+47
-56
lines changed

3 files changed

+47
-56
lines changed

docs/source/transforms/remove_builtin_exception_brackets.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ This transform removes parentheses when raising builtin exceptions with no argum
66
The raise statement automatically instantiates exceptions with no arguments, so the parentheses are unnecessary.
77
This transform does nothing on Python 2.
88

9+
If the exception is not a builtin exception, or has arguments, the parentheses are not removed.
10+
911
This transform is enabled by default. Disable by passing the ``remove_builtin_exception_brackets=False`` argument to the :func:`python_minifier.minify` function,
1012
or passing ``--no-remove-builtin-exception-brackets`` to the pyminify command.
1113

src/python_minifier/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def minify(
153153
bind_names(module)
154154
resolve_names(module)
155155

156-
if remove_builtin_exception_brackets:
156+
if remove_builtin_exception_brackets and not module.tainted:
157157
remove_no_arg_exception_call(module)
158158

159159
if module.tainted:

src/python_minifier/transforms/remove_exception_brackets.py

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,26 @@
1313

1414
from python_minifier.rename.binding import BuiltinBinding
1515

16-
# This list may vary between python versions
16+
# These are always exceptions, in every version of python
1717
builtin_exceptions = [
18-
'BaseException',
19-
'BaseExceptionGroup',
20-
'GeneratorExit',
21-
'KeyboardInterrupt',
22-
'SystemExit',
23-
'Exception',
24-
'ArithmeticError',
25-
'FloatingPointError',
26-
'OverflowError',
27-
'ZeroDivisionError',
28-
'AssertionError',
29-
'AttributeError',
30-
'BufferError',
31-
'EOFError',
32-
'ExceptionGroup',
33-
'BaseExceptionGroup',
34-
'ImportError',
35-
'ModuleNotFoundError',
36-
'LookupError',
37-
'IndexError',
38-
'KeyError',
39-
'MemoryError',
40-
'NameError',
41-
'UnboundLocalError',
42-
'OSError',
43-
'BlockingIOError',
18+
'SyntaxError', 'Exception', 'ValueError', 'BaseException', 'MemoryError', 'RuntimeError', 'DeprecationWarning', 'UnicodeEncodeError', 'KeyError', 'LookupError', 'TypeError', 'BufferError',
19+
'ImportError', 'OSError', 'StopIteration', 'ArithmeticError', 'UserWarning', 'PendingDeprecationWarning', 'RuntimeWarning', 'IndentationError', 'UnicodeTranslateError', 'UnboundLocalError',
20+
'AttributeError', 'EOFError', 'UnicodeWarning', 'BytesWarning', 'NameError', 'IndexError', 'TabError', 'SystemError', 'OverflowError', 'FutureWarning', 'SystemExit', 'Warning',
21+
'FloatingPointError', 'ReferenceError', 'UnicodeError', 'AssertionError', 'SyntaxWarning', 'UnicodeDecodeError', 'GeneratorExit', 'ImportWarning', 'KeyboardInterrupt', 'ZeroDivisionError',
22+
'NotImplementedError'
23+
]
24+
25+
# These are exceptions only in python 2.7
26+
builtin_exceptions_2_7 = [
27+
'IOError',
28+
'StandardError',
29+
'EnvironmentError',
30+
'VMSError',
31+
'WindowsError'
32+
]
33+
34+
# These are exceptions in 3.3+
35+
builtin_exceptions_3_3 = [
4436
'ChildProcessError',
4537
'ConnectionError',
4638
'BrokenPipeError',
@@ -55,34 +47,30 @@
5547
'PermissionError',
5648
'ProcessLookupError',
5749
'TimeoutError',
58-
'ReferenceError',
59-
'RuntimeError',
60-
'NotImplementedError',
61-
'RecursionError',
62-
'StopAsyncIteration',
63-
'StopIteration',
64-
'SyntaxError',
65-
'IndentationError',
66-
'TabError',
67-
'SystemError',
68-
'TypeError',
69-
'ValueError',
70-
'UnicodeError',
71-
'UnicodeDecodeError',
72-
'UnicodeEncodeError',
73-
'UnicodeTranslateError',
74-
'Warning',
75-
'BytesWarning',
76-
'DeprecationWarning',
77-
'EncodingWarning',
78-
'FutureWarning',
79-
'ImportWarning',
80-
'PendingDeprecationWarning',
8150
'ResourceWarning',
82-
'RuntimeWarning',
83-
'SyntaxWarning',
84-
'UnicodeWarning',
85-
'UserWarning'
51+
]
52+
53+
# These are exceptions in 3.5+
54+
builtin_exceptions_3_5 = [
55+
'StopAsyncIteration',
56+
'RecursionError',
57+
]
58+
59+
# These are exceptions in 3.6+
60+
builtin_exceptions_3_6 = [
61+
'ModuleNotFoundError'
62+
]
63+
64+
# These are exceptions in 3.10+
65+
builtin_exceptions_3_10 = [
66+
'EncodingWarning'
67+
]
68+
69+
# These are exceptions in 3.11+
70+
builtin_exceptions_3_11 = [
71+
'BaseExceptionGroup',
72+
'ExceptionGroup',
73+
'BaseExceptionGroup',
8674
]
8775

8876
def _remove_empty_call(binding):
@@ -114,6 +102,7 @@ def _remove_empty_call(binding):
114102
raise_node.cause = name_node
115103
name_node.parent = raise_node
116104

105+
117106
def remove_no_arg_exception_call(module):
118107
assert isinstance(module, ast.Module)
119108

@@ -125,4 +114,4 @@ def remove_no_arg_exception_call(module):
125114
# We can remove any calls to builtin exceptions
126115
_remove_empty_call(binding)
127116

128-
return module
117+
return module

0 commit comments

Comments
 (0)