Skip to content

Commit

Permalink
unparser: support TryStar (Python 3.11+)
Browse files Browse the repository at this point in the history
  • Loading branch information
Technologicat committed Sep 27, 2024
1 parent a40fd4c commit b77536c
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions mcpyrate/unparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def __init__(self, tree, *, file=sys.stdout,
global quotes
from . import quotes

self._except_handler_mode_stack = [] # Python 3.11+: TryStar is like Try, but ExceptHandler nodes inside it are interpreted differently.

self.debug = debug
self.color = color
self._color_override = False # for syntax highlighting of decorators
Expand Down Expand Up @@ -418,13 +420,17 @@ def _Raise(self, t):
self.write(self.maybe_colorize_python_keyword(" from "))
self.dispatch(t.cause)

def _Try(self, t):
def __Try_helper(self, t, mode):
self.fill(self.maybe_colorize_python_keyword("try"), lineno_node=t)
self.enter()
self.dispatch(t.body)
self.leave()
for ex in t.handlers:
self.dispatch(ex)
self._except_handler_mode_stack.append(mode)
try:
for ex in t.handlers:
self.dispatch(ex)
finally:
self._except_handler_mode_stack.pop()
if t.orelse:
self.fill(self.maybe_colorize_python_keyword("else"), lineno_node=t)
self.enter()
Expand All @@ -436,8 +442,20 @@ def _Try(self, t):
self.dispatch(t.finalbody)
self.leave()

def _Try(self, t):
self.__Try_helper(t, mode="Try")

def _TryStar(self, t): # Python 3.11+
self.__Try_helper(t, mode="TryStar")

def _ExceptHandler(self, t):
self.fill(self.maybe_colorize_python_keyword("except"), lineno_node=t)
mode = self._except_handler_mode_stack[-1]
if mode == "Try":
self.fill(self.maybe_colorize_python_keyword("except"), lineno_node=t)
elif mode == "TryStar": # Python 3.11+
self.fill(self.maybe_colorize_python_keyword("except*"), lineno_node=t)
else:
assert False # unknown mode, should not happen (it's managed by `__Try_helper`)
if t.type:
self.write(" ")
self.dispatch(t.type)
Expand Down

0 comments on commit b77536c

Please sign in to comment.