diff --git a/docs/classes/tree_sitter.Language.rst b/docs/classes/tree_sitter.Language.rst index e5e238b..7448bcd 100644 --- a/docs/classes/tree_sitter.Language.rst +++ b/docs/classes/tree_sitter.Language.rst @@ -29,6 +29,10 @@ Language .. versionadded:: 0.22.0 .. automethod:: __hash__ + .. important:: + + On 32-bit platforms, you must use ``hash(self) & 0xFFFFFFFF`` to get the actual hash. + .. versionadded:: 0.22.0 .. automethod:: __ne__ diff --git a/tests/test_language.py b/tests/test_language.py index 6bf8e03..747fa62 100644 --- a/tests/test_language.py +++ b/tests/test_language.py @@ -1,3 +1,4 @@ +from sys import maxsize from unittest import TestCase from tree_sitter import Language, Query @@ -79,4 +80,5 @@ def test_hash(self): with self.subTest(language=name): ptr = getattr(self, name) lang = Language(ptr) - self.assertEqual(hash(lang), ptr) + hash_ = hash(lang) & maxsize << 1 + self.assertEqual(hash_, ptr) diff --git a/tree_sitter/binding/lookahead_iterator.c b/tree_sitter/binding/lookahead_iterator.c index df1f856..e283d57 100644 --- a/tree_sitter/binding/lookahead_iterator.c +++ b/tree_sitter/binding/lookahead_iterator.c @@ -52,8 +52,8 @@ PyObject *lookahead_iterator_reset(LookaheadIterator *self, PyObject *args) { return NULL; } - Py_ssize_t language_id = PyLong_AsSsize_t(language_obj); - if (language_id < 1 || (language_id % sizeof(TSLanguage *)) != 0) { + Py_uintptr_t language_id = PyLong_AsSize_t(language_obj); + if (language_id == 0 || (language_id % sizeof(TSLanguage *)) != 0) { if (!PyErr_Occurred()) { PyErr_SetString(PyExc_ValueError, "invalid language ID"); }