diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7ab95d6c..25ba3fef 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,7 +30,7 @@ repos: hooks: - id: clang-format - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.1.1 + rev: v0.1.3 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e4d4251..e82ca496 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Set recursion limit to 2000 for all platforms by [@XuehaiPan](https://github.com/XuehaiPan) in [#97](https://github.com/metaopt/optree/pull/97). - Make `PyTreeSpec.is_prefix` to be consistent with `PyTreeSpec.flatten_up_to` by [@XuehaiPan](https://github.com/XuehaiPan) in [#94](https://github.com/metaopt/optree/pull/94). - Decrease the `MAX_RECURSION_DEPTH` to 2000 on Windows by [@XuehaiPan](https://github.com/XuehaiPan) in [#85](https://github.com/metaopt/optree/pull/85). - Bump `abseil-cpp` version to 20230802.1 by [@XuehaiPan](https://github.com/XuehaiPan) in [#80](https://github.com/metaopt/optree/pull/80). diff --git a/include/treespec.h b/include/treespec.h index 5c7914be..579120a4 100644 --- a/include/treespec.h +++ b/include/treespec.h @@ -36,11 +36,7 @@ limitations under the License. namespace optree { // The maximum depth of a pytree. -#ifdef _WIN32 constexpr ssize_t MAX_RECURSION_DEPTH = 2000; -#else -constexpr ssize_t MAX_RECURSION_DEPTH = 5000; -#endif // A PyTreeSpec describes the tree structure of a PyTree. A PyTree is a tree of Python values, where // the interior nodes are tuples, lists, dictionaries, or user-defined containers, and the leaves diff --git a/optree/__init__.py b/optree/__init__.py index 836e7d8d..e0848339 100644 --- a/optree/__init__.py +++ b/optree/__init__.py @@ -152,8 +152,12 @@ 'structseq_fields', ] -MAX_RECURSION_DEPTH: int = MAX_RECURSION_DEPTH -"""Maximum recursion depth for pytree traversal. It is 5000 on Unix-like systems and 2500 on Windows.""" +MAX_RECURSION_DEPTH: int = MAX_RECURSION_DEPTH # 2000 +"""Maximum recursion depth for pytree traversal. It is 2000. + +This limit prevents infinite recursion from causing an overflow of the C stack +and crashing Python. +""" NONE_IS_NODE: bool = NONE_IS_NODE # literal constant """Literal constant that treats :data:`None` as a pytree non-leaf node.""" NONE_IS_LEAF: bool = NONE_IS_LEAF # literal constant diff --git a/optree/ops.py b/optree/ops.py index 7b3cb4c9..602cc1b0 100644 --- a/optree/ops.py +++ b/optree/ops.py @@ -96,8 +96,12 @@ 'prefix_errors', ] -MAX_RECURSION_DEPTH: int = _C.MAX_RECURSION_DEPTH -"""Maximum recursion depth for pytree traversal. It is 5000 on Unix-like systems and 2500 on Windows.""" +MAX_RECURSION_DEPTH: int = _C.MAX_RECURSION_DEPTH # 2000 +"""Maximum recursion depth for pytree traversal. It is 2000. + +This limit prevents infinite recursion from causing an overflow of the C stack +and crashing Python. +""" NONE_IS_NODE: bool = False # literal constant """Literal constant that treats :data:`None` as a pytree non-leaf node.""" NONE_IS_LEAF: bool = True # literal constant