Skip to content

Commit

Permalink
chore: set recursion limit to 2000 for all platforms (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan authored Oct 28, 2023
1 parent d8b4611 commit 574cec6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 0 additions & 4 deletions include/treespec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions optree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions optree/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 574cec6

Please sign in to comment.