Skip to content

Commit

Permalink
fix: more 32bit issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed May 18, 2024
1 parent 85e4948 commit d694939
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/classes/tree_sitter.Language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand Down
4 changes: 3 additions & 1 deletion tests/test_language.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from sys import maxsize
from unittest import TestCase

from tree_sitter import Language, Query
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions tree_sitter/binding/lookahead_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit d694939

Please sign in to comment.