Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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
base:main
Are you sure you want to change the base?
Changes fromall commits
96e1e78e5e980e2fcaeddaea82784bb1e2fd1ab4021c2d40fade2ce340f50e70c3105b6028349d7a7cf0fb1394472e46fb675246d22e3ee0f7661da28f02181b79755b75c38035101a0ddae34c652abd3File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -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 | ||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Let's remove these exclusions and do the fixes? Suggested change
| ||||
| - id: ruff | ||||
| name: Run Ruff (lint) on Lib/test/ | ||||
| args: [--exit-non-zero-on-fix] | ||||
| @@ -22,6 +23,7 @@ repos: | ||||
| name: Run Ruff (format) on Doc/ | ||||
| args: [--check] | ||||
| files: ^Doc/ | ||||
| exclude: ^Doc/howto/multiple-interpreters-run-examples.py | ||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||
| - id: ruff-format | ||||
| name: Run Ruff (format) on Tools/build/check_warnings.py | ||||
| args: [--check, --config=Tools/build/.ruff.toml] | ||||
| Original file line number | Diff line number | Diff 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 | ||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This line is unused: Suggested change
| ||||||||||
| returnexamples | ||||||||||
| defwrite_example(examplesdir,name,text): | ||||||||||
| filename=os.path.join(examplesdir,f'example-{name}.py') | ||||||||||
Comment on lines +66 to +67 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||
| 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 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||
| 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') | ||||||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggested change
| ||||||||||
| print(summary) | ||||||||||
| returnlen(failed) | ||||||||||
| finally: | ||||||||||
| shutil.rmtree(examplesdir) | ||||||||||
| if__name__=='__main__': | ||||||||||
| kwargs=parse_args() | ||||||||||
| ec=main(**kwargs) | ||||||||||
| sys.exit(ec) | ||||||||||
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.