diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7d02c819..7ab95d6c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ ci: default_stages: [commit, push, manual] repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-symlinks - id: destroyed-symlinks @@ -26,11 +26,11 @@ repos: - id: debug-statements - id: double-quote-string-fixer - repo: https://github.com/pre-commit/mirrors-clang-format - rev: v16.0.6 + rev: v17.0.3 hooks: - id: clang-format - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.0.292 + rev: v0.1.1 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] @@ -39,11 +39,11 @@ repos: hooks: - id: isort - repo: https://github.com/psf/black - rev: 23.9.1 + rev: 23.10.1 hooks: - id: black - repo: https://github.com/asottile/pyupgrade - rev: v3.14.0 + rev: v3.15.0 hooks: - id: pyupgrade args: [--py37-plus] diff --git a/CMakeLists.txt b/CMakeLists.txt index 42321e86..5d522412 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,16 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) # -fPIC set(CMAKE_CXX_VISIBILITY_PRESET hidden) # -fvisibility=hidden +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + if(NOT DEFINED _GLIBCXX_USE_CXX11_ABI AND NOT "$ENV{_GLIBCXX_USE_CXX11_ABI}" STREQUAL "") + set(_GLIBCXX_USE_CXX11_ABI "$ENV{_GLIBCXX_USE_CXX11_ABI}") + endif() + if(_GLIBCXX_USE_CXX11_ABI) + message(STATUS "Use _GLIBCXX_USE_CXX11_ABI: ${_GLIBCXX_USE_CXX11_ABI}") + add_definitions("-D_GLIBCXX_USE_CXX11_ABI=${_GLIBCXX_USE_CXX11_ABI}") # use C++11 ABI + endif() +endif() + if(MSVC) string( APPEND CMAKE_CXX_FLAGS diff --git a/Makefile b/Makefile index 13780b27..275fa75f 100644 --- a/Makefile +++ b/Makefile @@ -117,6 +117,7 @@ addlicense-install: go-install pytest: pytest-install cd tests && $(PYTHON) -c 'import $(PROJECT_PATH)' && \ + $(PYTHON) -c 'import $(PROJECT_PATH)._C; print(f"GLIBCXX_USE_CXX11_ABI={$(PROJECT_PATH)._C.GLIBCXX_USE_CXX11_ABI}")' && \ $(PYTHON) -m pytest --verbose --color=yes --durations=0 \ --cov="$(PROJECT_PATH)" --cov-config=.coveragerc --cov-report=xml --cov-report=term-missing \ $(PYTESTOPTS) . diff --git a/optree/_C.pyi b/optree/_C.pyi index bd9f19d5..83a973cb 100644 --- a/optree/_C.pyi +++ b/optree/_C.pyi @@ -28,6 +28,8 @@ MAX_RECURSION_DEPTH: int # Set if the type allows subclassing (see CPython's Include/object.h) Py_TPFLAGS_BASETYPE: int # (1UL << 10) +GLIBCXX_USE_CXX11_ABI: bool + def flatten( tree: PyTree[T], leaf_predicate: Callable[[T], bool] | None = None, diff --git a/src/optree.cpp b/src/optree.cpp index 972a82d9..fe5a8f3b 100644 --- a/src/optree.cpp +++ b/src/optree.cpp @@ -29,6 +29,12 @@ void BuildModule(py::module& mod) { // NOLINT[runtime/references] py::register_local_exception(mod, "InternalError", PyExc_SystemError); mod.attr("MAX_RECURSION_DEPTH") = py::ssize_t_cast(MAX_RECURSION_DEPTH); mod.attr("Py_TPFLAGS_BASETYPE") = py::ssize_t_cast(Py_TPFLAGS_BASETYPE); +#ifdef _GLIBCXX_USE_CXX11_ABI + // NOLINTNEXTLINE[modernize-use-bool-literals] + mod.attr("GLIBCXX_USE_CXX11_ABI") = static_cast(_GLIBCXX_USE_CXX11_ABI); +#else + mod.attr("GLIBCXX_USE_CXX11_ABI") = false; +#endif mod.def("register_node", &PyTreeTypeRegistry::Register, diff --git a/tests/conftest.py b/tests/conftest.py index 6f85b58c..3d3e622f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,6 +14,8 @@ # ============================================================================== import os +import random os.environ['PYTHONHASHSEED'] = '0' +random.seed(0)