From 9d319df8eb687503d133277549d25ab7b4c0d1e4 Mon Sep 17 00:00:00 2001 From: Aaron Voelker Date: Fri, 1 May 2020 17:06:48 -0400 Subject: [PATCH] Truncate semantic pointer names to 1 KB Fixes #244. --- CHANGES.rst | 6 ++++++ nengo_spa/semantic_pointer.py | 5 +++++ nengo_spa/tests/test_semantic_pointer.py | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index f3aa7c8b3..3a9c5af4e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -34,6 +34,12 @@ Release History (`#239 `__, `#240 `__) +**Fixed** + +- Fixed an issue where the names of iteratively generated semantic pointers + could grow exponentially in length, by truncating them at 1 KB. + (`#244 `__, + `#246 `__) 1.0.1 (December 14, 2019) diff --git a/nengo_spa/semantic_pointer.py b/nengo_spa/semantic_pointer.py index 0b548fce5..7b23390dc 100644 --- a/nengo_spa/semantic_pointer.py +++ b/nengo_spa/semantic_pointer.py @@ -42,6 +42,8 @@ class SemanticPointer(Fixed): Name of the Semantic Pointer. """ + MAX_NAME = 1024 + def __init__(self, data, vocab=None, algebra=None, name=None): super(SemanticPointer, self).__init__( TAnyVocab if vocab is None else TVocabulary(vocab) @@ -54,6 +56,9 @@ def __init__(self, data, vocab=None, algebra=None, name=None): self.v.setflags(write=False) self.vocab = vocab + + if name is not None and len(name) > self.MAX_NAME: + name = "%s..." % (name[: self.MAX_NAME - 3]) self.name = name def _get_algebra(cls, vocab, algebra): diff --git a/nengo_spa/tests/test_semantic_pointer.py b/nengo_spa/tests/test_semantic_pointer.py index 0b50b89cf..a32cb1e0b 100644 --- a/nengo_spa/tests/test_semantic_pointer.py +++ b/nengo_spa/tests/test_semantic_pointer.py @@ -323,3 +323,9 @@ def test_name(): assert (a + unnamed).name is None assert (a * unnamed).name is None + + # check that names that blow up exponentially in length are truncated + for i in range(10): + a += a * b + assert len(a.name) == a.MAX_NAME + assert a.name.endswith("...")