Skip to content

gh-136380: Fix import behavior for concurrent.futures.InterpreterPoolExecutor #136381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions Lib/concurrent/futures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
wait,
as_completed)

__all__ = (
__all__ = [
'FIRST_COMPLETED',
'FIRST_EXCEPTION',
'ALL_COMPLETED',
Expand All @@ -29,10 +29,18 @@
'Executor',
'wait',
'as_completed',
'InterpreterPoolExecutor',
'ProcessPoolExecutor',
'ThreadPoolExecutor',
)
]


try:
import _interpreters
except ImportError:
_interpreters = None

if _interpreters:
__all__.append('InterpreterPoolExecutor')


def __dir__():
Expand All @@ -43,22 +51,15 @@ def __getattr__(name):
global ProcessPoolExecutor, ThreadPoolExecutor, InterpreterPoolExecutor

if name == 'ProcessPoolExecutor':
from .process import ProcessPoolExecutor as pe
ProcessPoolExecutor = pe
return pe
from .process import ProcessPoolExecutor
return ProcessPoolExecutor

if name == 'ThreadPoolExecutor':
from .thread import ThreadPoolExecutor as te
ThreadPoolExecutor = te
return te
from .thread import ThreadPoolExecutor
return ThreadPoolExecutor

if name == 'InterpreterPoolExecutor':
try:
from .interpreter import InterpreterPoolExecutor as ie
except ModuleNotFoundError:
ie = InterpreterPoolExecutor = None
else:
InterpreterPoolExecutor = ie
return ie
if _interpreters and name == 'InterpreterPoolExecutor':
from .interpreter import InterpreterPoolExecutor
return InterpreterPoolExecutor

raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
41 changes: 41 additions & 0 deletions Lib/test/test_concurrent_futures/test_interpreter_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import contextlib
import io
import os
import subprocess
import sys
import textwrap
import time
import unittest
from concurrent.futures.interpreter import BrokenInterpreterPool
Expand Down Expand Up @@ -457,6 +459,45 @@ def test_free_reference(self):
# Weak references don't cross between interpreters.
raise unittest.SkipTest('not applicable')

@support.requires_subprocess()
def test_import_interpreter_pool_executor(self):
# Test the import behavior normally if _interpreters is unavailable.
code = textwrap.dedent("""
import sys
# Set it to None to emulate the case when _interpreter is unavailable.
sys.modules['_interpreters'] = None
from concurrent import futures

try:
futures.InterpreterPoolExecutor
except AttributeError:
pass
else:
print('AttributeError not raised!', file=sys.stderr)
sys.exit(1)

try:
from concurrent.futures import InterpreterPoolExecutor
except ImportError:
pass
else:
print('ImportError not raised!', file=sys.stderr)
sys.exit(1)

from concurrent.futures import *

if 'InterpreterPoolExecutor' in globals():
print('InterpreterPoolExecutor should not be imported!',
file=sys.stderr)
sys.exit(1)
""")

cmd = [sys.executable, '-c', code]
p = subprocess.run(cmd, capture_output=True)
self.assertEqual(p.returncode, 0, p.stderr.decode())
self.assertEqual(p.stdout.decode(), '')
self.assertEqual(p.stderr.decode(), '')


class AsyncioTest(InterpretersMixin, testasyncio_utils.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Raises :exc:`AttributeError` when accessing
:class:`concurrent.futures.InterpreterPoolExecutor` and subinterpreters are
not available.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy