Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-134939: Add a Multiple Interpreters Howto Doc#136143

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ericsnowcurrently wants to merge23 commits intopython:main
base:main
Choose a base branch
Loading
fromericsnowcurrently:multiple-interpreters-howto
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
23 commits
Select commitHold shift + click to select a range
96e1e78
Add the multiple interpreters howto doc.
ericsnowcurrentlyJul 16, 2024
e5e980e
Drop extraneous changes.
ericsnowcurrentlyJun 19, 2025
2fcaedd
Finish howto doc, minus recipes.
ericsnowcurrentlyJun 20, 2025
aea8278
Add a TODO list.
ericsnowcurrentlyJun 26, 2025
4bb1e2f
Drop the execcomponents ref.
ericsnowcurrentlyJun 30, 2025
d1ab402
Fix a ref.
ericsnowcurrentlyJun 30, 2025
1c2d40f
Drop the examples section for now.
ericsnowcurrentlyJun 30, 2025
ade2ce3
Fix typos.
ericsnowcurrentlyJul 1, 2025
40f50e7
Add a misc section to the tutorial.
ericsnowcurrentlyJul 1, 2025
0c3105b
Clarify about prepare_main().
ericsnowcurrentlyJul 1, 2025
6028349
Fix a pseudo-ref.
ericsnowcurrentlyJul 1, 2025
d7a7cf0
Fix the examples.
ericsnowcurrentlyJul 1, 2025
fb13944
Tweak one example.
ericsnowcurrentlyJul 1, 2025
72e46fb
Add a ref.
ericsnowcurrentlyJul 2, 2025
675246d
Clarify about Interpreter.exec().
ericsnowcurrentlyJul 3, 2025
22e3ee0
Clarify about calling different kinds of function.
ericsnowcurrentlyJul 3, 2025
f7661da
Clarify a note.
ericsnowcurrentlyJul 3, 2025
28f0218
Add a caveat about -c and the REPL.
ericsnowcurrentlyJul 3, 2025
1b79755
Add a pro tip.
ericsnowcurrentlyJul 3, 2025
b75c380
Add run-examples.py and fix some of the examples.
ericsnowcurrentlySep 23, 2025
35101a0
Do not try reformatting run-examples.py.
ericsnowcurrentlySep 24, 2025
ddae34c
Fix some of the examples.
ericsnowcurrentlySep 24, 2025
652abd3
Do not lint run-examples.py.
ericsnowcurrentlySep 24, 2025
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
2 changes: 2 additions & 0 deletions.pre-commit-config.yaml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,6 +6,7 @@ repos:
name: Run Ruff (lint) on Doc/
args: [--exit-non-zero-on-fix]
files: ^Doc/
exclude: ^Doc/howto/multiple-interpreters-run-examples.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Let's remove these exclusions and do the fixes?

Suggested change
exclude: ^Doc/howto/multiple-interpreters-run-examples.py

- id: ruff
name: Run Ruff (lint) on Lib/test/
args: [--exit-non-zero-on-fix]
Expand All@@ -22,6 +23,7 @@ repos:
name: Run Ruff (format) on Doc/
args: [--check]
files: ^Doc/
exclude: ^Doc/howto/multiple-interpreters-run-examples.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
exclude: ^Doc/howto/multiple-interpreters-run-examples.py

- id: ruff-format
name: Run Ruff (format) on Tools/build/check_warnings.py
args: [--check, --config=Tools/build/.ruff.toml]
Expand Down
2 changes: 2 additions & 0 deletionsDoc/howto/index.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,6 +32,7 @@ Python Library Reference.
isolating-extensions.rst
timerfd.rst
mro.rst
multiple-interpreters.rst
free-threading-python.rst
free-threading-extensions.rst
remote_debugging.rst
Expand All@@ -57,6 +58,7 @@ Advanced development:
* :ref:`freethreading-python-howto`
* :ref:`freethreading-extensions-howto`
* :ref:`isolating-extensions-howto`
* :ref:`multiple-interpreters-howto`
* :ref:`python_2.3_mro`
* :ref:`socket-howto`
* :ref:`timerfd-howto`
Expand Down
177 changes: 177 additions & 0 deletionsDoc/howto/multiple-interpreters-run-examples.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
importos
importos.path
importshutil
importsubprocess
importsys
importtempfile
fromtextwrapimportdedent
importtraceback


HOWTO_DIR=os.path.dirname(__file__)
HOWTO_FILE=os.path.join(HOWTO_DIR,'multiple-interpreters.rst')

VERBOSITY=3


defget_examples(lines):
examples= {}
current=None
expected=None
start=None
forlno,lineinenumerate(lines,1):
ifcurrentisNone:
ifline.endswith('::'):
# It *might* be an example.
current= []
else:
ifnotcurrent:
ifline.strip():
ifline==' from concurrent import interpreters':
# It *is* an example.
current.append(line)
expected= []
start=lno
else:
# It wasn't actually an example.
current=None
elifnotline.strip()orline.startswith(' '):
# The example continues.
current.append(line)
before,sep,after=line.partition('# prints: ')
ifsep:
assertnotbefore.strip(),before
expected.append(after)
else:
# We've reached the end of the example.
assertnotcurrent[-1].strip(),current
example=dedent(os.linesep.join(current))
expected=''.join(f'{l}{os.linesep}'forlinexpected)
examples[start]= (example,expected)
current=expected=start=None

ifline.endswith('::'):
# It *might* be an example.
current= []
ifcurrent:
ifcurrent[-1].strip():
current.append('')
example=dedent(os.linesep.join(current))
expected=''.join(f'{l}{os.linesep}'forlinexpected)
examples[start]= (example,expected)
current=expected=start=None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This line is unused:

Suggested change
current = expected = start = None

returnexamples


defwrite_example(examplesdir,name,text):
filename=os.path.join(examplesdir,f'example-{name}.py')
Comment on lines +66 to +67
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
defwrite_example(examplesdir,name,text):
filename=os.path.join(examplesdir,f'example-{name}.py')
defwrite_example(examples_dir,name,text):
filename=os.path.join(examples_dir,f'example-{name}.py')

withopen(filename,'w')asoutfile:
outfile.write(text)
returnfilename,name,text


defrun_example(filename,expected):
ifisinstance(expected,str):
rc=0
stdout=expected
stderr=''
else:
rc,stdout,stderr=expected

proc=subprocess.run(
[sys.executable,filename],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
Comment on lines +83 to +84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
capture_output=True,

text=True,
cwd=os.path.dirname(filename),
)
ifproc.returncode!=rc:
ifproc.returncode!=0andproc.stderr:
print(proc.stderr)
raiseAssertionError((proc.returncode,rc))
assertproc.stdout==stdout, (proc.stdout,stdout)
assertproc.stderr==stderr, (proc.stderr,stderr)


#######################################
# the script

defparse_args(argv=sys.argv[1:],prog=sys.argv[0]):
importargparse
parser=argparse.ArgumentParser(prog=prog)

parser.add_argument('--dry-run',dest='dryrun',action='store_true')

parser.add_argument('-v','--verbose',action='count',default=0)
parser.add_argument('-q','--quiet',action='count',default=0)

parser.add_argument('requested',nargs='*')

args=parser.parse_args(argv)
ns=vars(args)

args.verbosity=max(0,VERBOSITY+ns.pop('verbose')-ns.pop('quiet'))

requested= []
forreqinargs.requested:
foriinreq.replace(',',' ').split():
requested.append(int(i))
args.requested=requestedorNone

returnns


defmain(requested=None,*,verbosity=VERBOSITY,dryrun=False):
withopen(HOWTO_FILE)asinfile:
examples=get_examples(l.rstrip(os.linesep)forlininfile)
examplesdir=tempfile.mkdtemp(prefix='multi-interp-howto-')
try:
ifrequested:
requested=set(requested)
_req=f',{len(requested)} requested'
else:
_req=''
summary=f'({len(examples)} found{_req})'
print('#',summary)

failed= []
fori, (lno, (text,expected))inenumerate(examples.items(),1):
ifrequestedandinotinrequested:
continue
name='multiinterp-{i}'
print()
print('#'*60)
print(f'# example{i} ({os.path.relpath(HOWTO_FILE)}:{lno})')
print('#'*60)
print()
ifverbosity>VERBOSITYor (verbosity==VERBOSITYanddryrun):
print(text)
print('----')
ifexpected.rstrip():
print(expected.rstrip(os.linesep))
print('----')
ifdryrun:
continue
filename,_,_=write_example(examplesdir,name,text)
try:
run_example(filename,expected)
exceptException:
traceback.print_exc()
failed.append(str(i))

print()
iffailed:
print(f'{len(failed)} failed:{",".join(failed)}')
else:
print(f'all succeeded')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
print(f'all succeeded')
print('all succeeded')

print(summary)

returnlen(failed)
finally:
shutil.rmtree(examplesdir)


if__name__=='__main__':
kwargs=parse_args()
ec=main(**kwargs)
sys.exit(ec)
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp