Skip to content

Commit 2fa905b

Browse files
committed
Automatically raise recursion limit
Have the `_pyinstaller_hooks_contrib/__init__.py` automatically raise the recursion limit to at least 5000, instead of doing this on per-hook basis. Remove the corresponding limit-raising code from hooks, since we do not need it there anymore.
1 parent 80efcce commit 2fa905b

File tree

7 files changed

+16
-64
lines changed

7 files changed

+16
-64
lines changed

_pyinstaller_hooks_contrib/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# SPDX-License-Identifier: GPL-2.0-or-later
1111
# ------------------------------------------------------------------
1212

13+
import sys
14+
1315
__version__ = '2025.6'
1416
__maintainer__ = 'Legorooj, bwoodsend'
1517
__uri__ = 'https://github.com/pyinstaller/pyinstaller-hooks-contrib'
@@ -24,3 +26,12 @@ def get_hook_dirs():
2426
# pre_* and run-time hooks
2527
hooks_dir,
2628
]
29+
30+
31+
# Several packages for which provide hooks are involved in deep dependency chains when various optional dependencies are
32+
# installed in the environment, and their analysis typically requires recursion limit that exceeds the default 1000.
33+
# Therefore, automatically raise the recursion limit to at least 5000. This alleviates the need to do so on per-hook
34+
# basis.
35+
new_recursion_limit = 5000
36+
if sys.getrecursionlimit() < new_recursion_limit:
37+
sys.setrecursionlimit(new_recursion_limit)

_pyinstaller_hooks_contrib/stdhooks/hook-langchain.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@
1010
# SPDX-License-Identifier: GPL-2.0-or-later
1111
# ------------------------------------------------------------------
1212

13-
import sys
14-
15-
from PyInstaller.utils.hooks import collect_data_files, logger
13+
from PyInstaller.utils.hooks import collect_data_files
1614

1715
datas = collect_data_files('langchain')
18-
19-
# Automatically raise recursion limit to ensure it is at least 5000; this attempts to mitigate recursion limit errors
20-
# caused by some import chains that involve langchain, but also depend on the build environment (i.e., other packages
21-
# installed in it).
22-
new_limit = 5000
23-
if sys.getrecursionlimit() < new_limit:
24-
logger.info("hook-langchain: raising recursion limit to %d", new_limit)
25-
sys.setrecursionlimit(new_limit)

_pyinstaller_hooks_contrib/stdhooks/hook-spacy.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,7 @@
1313
Spacy contains hidden imports and data files which are needed to import it
1414
"""
1515

16-
import sys
17-
18-
from PyInstaller.utils.hooks import collect_data_files, collect_submodules, logger
16+
from PyInstaller.utils.hooks import collect_data_files, collect_submodules
1917

2018
datas = collect_data_files("spacy")
2119
hiddenimports = collect_submodules("spacy")
22-
23-
# Automatically raise recursion limit to ensure it is at least 5000; this attempts to mitigate recursion limit errors
24-
# caused by some import chains that involve spacy, but also depend on the build environment (i.e., other packages
25-
# installed in it).
26-
new_limit = 5000
27-
if sys.getrecursionlimit() < new_limit:
28-
logger.info("hook-spacy: raising recursion limit to %d", new_limit)
29-
sys.setrecursionlimit(new_limit)

_pyinstaller_hooks_contrib/stdhooks/hook-sympy.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

_pyinstaller_hooks_contrib/stdhooks/hook-tensorflow.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
# SPDX-License-Identifier: GPL-2.0-or-later
1111
# ------------------------------------------------------------------
1212

13-
import sys
14-
1513
from _pyinstaller_hooks_contrib.compat import importlib_metadata
1614
from packaging.version import Version
1715

@@ -25,14 +23,6 @@
2523
logger,
2624
)
2725

28-
# Automatically raise recursion limit to ensure it is at least 5000; this attempts to mitigate recursion limit errors
29-
# caused by some import chains that involve tensorflow, but also depend on the build environment (i.e., other packages
30-
# installed in it).
31-
new_limit = 5000
32-
if sys.getrecursionlimit() < new_limit:
33-
logger.info("hook-tensorflow: raising recursion limit to %d", new_limit)
34-
sys.setrecursionlimit(new_limit)
35-
3626
# Determine the name of `tensorflow` dist; this is available under different names (releases vs. nightly, plus build
3727
# variants). We need to determine the dist that we are dealing with, so we can query its version and metadata.
3828
_CANDIDATE_DIST_NAMES = (

_pyinstaller_hooks_contrib/stdhooks/hook-torch.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,3 @@ def _collect_mkl_dlls():
149149
binaries += mkl_binaries
150150
else:
151151
datas = [(get_package_paths("torch")[1], "torch")]
152-
153-
# With torch 2.0.0, PyInstaller's modulegraph analysis hits the recursion limit.
154-
# So, unless the user has already done so, increase it automatically.
155-
if is_module_satisfies("torch >= 2.0.0"):
156-
import sys
157-
158-
new_limit = 5000
159-
if sys.getrecursionlimit() < new_limit:
160-
logger.info("hook-torch: raising recursion limit to %d", new_limit)
161-
sys.setrecursionlimit(new_limit)

news/925.update.1.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Automatically raise the recursion limit to at least 5000 when the
2+
``_pyinstaller_hooks_contrib`` module is loaded, rather than raising
3+
the recursion limit on per-hook basis.

0 commit comments

Comments
 (0)