From a03ce3ce1c6102163ded25a7dc3312533435417b Mon Sep 17 00:00:00 2001 From: ObserverOfTime Date: Sun, 25 Feb 2024 23:54:14 +0200 Subject: [PATCH] refactor: make binding module private --- setup.py | 2 +- tree_sitter/__init__.py | 8 ++++---- tree_sitter/{binding.pyi => _binding.pyi} | 0 tree_sitter/binding.c | 10 +++++++--- 4 files changed, 12 insertions(+), 8 deletions(-) rename tree_sitter/{binding.pyi => _binding.pyi} (100%) diff --git a/setup.py b/setup.py index bdfb63a..21bb2fd 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ }, ext_modules=[ Extension( - name="tree_sitter.binding", + name="tree_sitter._binding", sources=[ "tree_sitter/core/lib/src/lib.c", "tree_sitter/binding.c" diff --git a/tree_sitter/__init__.py b/tree_sitter/__init__.py index eaa7f77..17ae7fb 100644 --- a/tree_sitter/__init__.py +++ b/tree_sitter/__init__.py @@ -1,14 +1,14 @@ """Python bindings for tree-sitter.""" -import enum from ctypes import c_void_p, cdll +from enum import IntEnum from os import path from platform import system from tempfile import TemporaryDirectory from typing import Callable, List, Optional, Union from warnings import warn -from tree_sitter.binding import ( +from tree_sitter._binding import ( LookaheadIterator, LookaheadNamesIterator, Node, @@ -36,7 +36,7 @@ def _deprecate(old: str, new: str): warn("{} is deprecated. Use {} instead.".format(old, new), FutureWarning) -class SymbolType(enum.IntEnum): +class SymbolType(IntEnum): """An enumeration of the different types of symbols.""" REGULAR = 0 @@ -100,7 +100,7 @@ def build_library(output_path: str, repo_paths: List[str]) -> bool: else: flags = ["-fPIC"] if source_path.endswith(".c"): - flags.append("-std=c99") + flags.append("-std=c11") object_paths.append( compiler.compile( [source_path], diff --git a/tree_sitter/binding.pyi b/tree_sitter/_binding.pyi similarity index 100% rename from tree_sitter/binding.pyi rename to tree_sitter/_binding.pyi diff --git a/tree_sitter/binding.c b/tree_sitter/binding.c index 7e1e7e4..c5207cf 100644 --- a/tree_sitter/binding.c +++ b/tree_sitter/binding.c @@ -102,7 +102,7 @@ typedef struct { PyTypeObject *lookahead_names_iterator_type; } ModuleState; -#if PY_VERSION_HEX < 0x030900f0 +#if PY_MINOR_VERSION < 9 static ModuleState *global_state = NULL; static ModuleState *PyType_GetModuleState(PyTypeObject *obj) { return global_state; } static PyObject *PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases) { @@ -2918,6 +2918,9 @@ static struct PyModuleDef module_definition = { .m_methods = module_methods, }; +#if PY_MINOR_VERSION > 9 +#define AddObjectRef PyModule_AddObjectRef +#else // simulate PyModule_AddObjectRef for pre-Python 3.10 static int AddObjectRef(PyObject *module, const char *name, PyObject *value) { if (value == NULL) { @@ -2930,8 +2933,9 @@ static int AddObjectRef(PyObject *module, const char *name, PyObject *value) { } return ret; } +#endif -PyMODINIT_FUNC PyInit_binding(void) { +PyMODINIT_FUNC PyInit__binding(void) { PyObject *module = PyModule_Create(&module_definition); if (module == NULL) { return NULL; @@ -2992,7 +2996,7 @@ PyMODINIT_FUNC PyInit_binding(void) { goto cleanup; } -#if PY_VERSION_HEX < 0x030900f0 +#if PY_MINOR_VERSION < 9 global_state = state; #endif return module;