Skip to content

Commit c0ab8a9

Browse files
committed
Fix a crash caused by EnumInstance.instantiate() calling itself.
PiperOrigin-RevId: 407145181
1 parent d97641f commit c0ab8a9

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

pytype/overlays/enum_overlay.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""
2424

2525
import collections
26+
import contextlib
2627
import logging
2728

2829
from pytype import overlay
@@ -199,6 +200,16 @@ def __init__(self, name, bases, members, cls, ctx):
199200
# These are set by EnumMetaInit.setup_interpreterclass.
200201
self.member_type = None
201202
self.member_attrs = {}
203+
self._instantiating = False
204+
205+
@contextlib.contextmanager
206+
def _is_instantiating(self):
207+
old_instantiating = self._instantiating
208+
self._instantiating = True
209+
try:
210+
yield
211+
finally:
212+
self._instantiating = old_instantiating
202213

203214
def instantiate(self, node, container=None):
204215
# Instantiate creates a canonical enum member. This intended for when no
@@ -216,7 +227,13 @@ def instantiate(self, node, container=None):
216227
value = self.ctx.new_unsolvable(node)
217228
instance.members["value"] = value
218229
for attr_name, attr_type in self.member_attrs.items():
219-
instance.members[attr_name] = attr_type.instantiate(node)
230+
# attr_type might refer back to self, so track whether we are
231+
# instantiating to avoid infinite recursion.
232+
if self._instantiating:
233+
instance.members[attr_name] = self.ctx.new_unsolvable(node)
234+
else:
235+
with self._is_instantiating():
236+
instance.members[attr_name] = attr_type.instantiate(node)
220237
return instance.to_variable(node)
221238

222239
def is_empty_enum(self):

0 commit comments

Comments
 (0)