Skip to content

Commit

Permalink
core: Make gensym more robust by preventing stem collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
cpitclaudel committed Aug 27, 2021
1 parent 2105b12 commit 5d75901
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions alectryon/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from typing import Any, Iterator, List, Tuple, Union, NamedTuple
from typing import Any, Dict, Iterator, List, Tuple, Union, NamedTuple

from collections import namedtuple, defaultdict
from contextlib import contextmanager
Expand Down Expand Up @@ -89,9 +89,13 @@ def b16(i):
return hex(i)[len("0x"):]

class Gensym():
# Having a global table of counters ensures that creating multiple Gensym
# instances in the same session doesn't cause collisions
GENSYM_COUNTERS: Dict[str, int] = {}

def __init__(self, stem):
self.stem = stem
self.counters = defaultdict(lambda: -1)
self.counters = self.GENSYM_COUNTERS.setdefault(stem, defaultdict(lambda: -1))

def __call__(self, prefix):
self.counters[prefix] += 1
Expand Down

0 comments on commit 5d75901

Please sign in to comment.