Skip to content

Commit

Permalink
refactor: make binding module private
Browse files Browse the repository at this point in the history
  • Loading branch information
ObserverOfTime committed Feb 25, 2024
1 parent 9b8fb19 commit a03ce3c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions tree_sitter/__init__.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down
File renamed without changes.
10 changes: 7 additions & 3 deletions tree_sitter/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a03ce3c

Please sign in to comment.