Skip to content

Commit

Permalink
Truncate semantic pointer names to 1 KB
Browse files Browse the repository at this point in the history
Fixes #244.
  • Loading branch information
arvoelke committed May 1, 2020
1 parent 840f8eb commit 9d319df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Release History
(`#239 <https://github.com/nengo/nengo_spa/issues/239>`__,
`#240 <https://github.com/nengo/nengo_spa/pull/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 <https://github.com/nengo/nengo_spa/issues/244>`__,
`#246 <https://github.com/nengo/nengo_spa/pull/246>`__)


1.0.1 (December 14, 2019)
Expand Down
5 changes: 5 additions & 0 deletions nengo_spa/semantic_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions nengo_spa/tests/test_semantic_pointer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("...")

0 comments on commit 9d319df

Please sign in to comment.