Skip to content

Commit

Permalink
Merge pull request #31 from dlitz/for-upstream
Browse files Browse the repository at this point in the history
Fix "TypeError: unhashable type: 'Symbol'" (and 'String'); Fix imports for Python >= 3.10
  • Loading branch information
jd-boyd authored Oct 20, 2022
2 parents 48e481e + ecb95df commit 0572eb2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion sexpdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@
]

import re
from collections import namedtuple, Iterable, Mapping, Sequence
from collections import namedtuple
try:
from collections.abc import Iterable, Mapping, Sequence
except ImportError:
# Python < 3.3
from collections import Iterable, Mapping, Sequence
from itertools import chain
from string import whitespace

Expand Down Expand Up @@ -446,6 +451,14 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

def __hash__(self):
"""
>>> D = {'a': 1, String('a'): 2, Symbol('a'): 3}
>>> len(D)
3
"""
return unicode.__hash__(self)

_lisp_quoted_specials = [ # from Pymacs
('\\', '\\\\'), # must come first to avoid doubly quoting "\"
('"', '\\"'), ('\b', '\\b'), ('\f', '\\f'),
Expand Down
11 changes: 11 additions & 0 deletions test_sexpdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ def test_parse_special_symbols(self):
r'\ ', r'\.', r'\,', r'\?', r'\;', r'\#']:
self.assert_parse(s, Symbol(Symbol.unquote(s)))

def test_hashable_and_distinct(self):
d = {
String("A"): "StrA",
Symbol("A"): "SymA",
"A": "strA",
}
self.assertEqual(3, len(d))
self.assertEqual("StrA", d[String("A")])
self.assertEqual("SymA", d[Symbol("A")])
self.assertEqual("strA", d["A"])


class TestParseFluctuation(BaseTestCase):

Expand Down

0 comments on commit 0572eb2

Please sign in to comment.