Skip to content

Commit

Permalink
fix unique name recursion overflow (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
szvsw committed Aug 11, 2022
1 parent f2171c2 commit 5d98e03
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
20 changes: 7 additions & 13 deletions archetypal/template/umi_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def comments(self):
class UniqueName(str):
"""Attribute unique user-defined names for :class:`UmiBase`."""

existing = set()
existing = {}

def __new__(cls, content):
"""Pick a name. Will increment the name if already used."""
Expand All @@ -519,17 +519,11 @@ def create_unique(cls, name):
if not name:
return None
if name not in cls.existing:
cls.existing.add(name)
cls.existing[name] = 0
return name
else:
match = re.match(r"^(.*?)(\D*)(\d+)$", name)
if match:
groups = list(match.groups())
pad = len(groups[-1])
groups[-1] = int(groups[-1])
groups[-1] += 1
groups[-1] = str(groups[-1]).zfill(pad)
name = "".join(map(str, groups))
return cls.create_unique(name)
else:
return cls.create_unique(name + "_1")
current_count = cls.existing[name]
new_count = current_count + 1
new_name = f"{name}_{str(new_count)}"
cls.existing[name] = new_count
return new_name
2 changes: 1 addition & 1 deletion archetypal/umi_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ def to_dict(self):
# create dict values
for group_name, group in self:
# reset unique names for group
UniqueName.existing = set()
UniqueName.existing = {}
obj: UmiBase
for obj in group:
try:
Expand Down

0 comments on commit 5d98e03

Please sign in to comment.