"""蓝色天空网 mbsky.com: actual Python process and explicit completeness checks."""
from pathlib import Path
import sys,subprocess,codeop,types,platform
sys.path.insert(0,str(Path(__file__).resolve().parent))
from mbsky_pychecks import Checks
c=Checks()
def main():
c.eq('actual selected interpreter version',sys.version_info.major,3)
incomplete=codeop.compile_command('if True:\n','<owned-incomplete>','single');complete=codeop.compile_command('if True:\n print("learn")\n','<owned-complete>','single');c.eq('suite incomplete',incomplete,None);c.eq('suite complete code object',isinstance(complete,types.CodeType),True);c.raises('invalid syntax is different',SyntaxError,lambda:codeop.compile_command('if :\n'))
commands='print(brand)\nif True:\n print("one step")\n\nprint(6 * 7)\n'
args=[sys.executable,'-I','-B','-X','utf8','-q','-i','-c','brand="蓝色天空网 · mbsky.com"'];r=subprocess.run(args,input=commands,capture_output=True,text=True,encoding='utf8',timeout=20)
c.eq('actual interactive exit',r.returncode,0);c.eq('actual interactive stdout',r.stdout,'蓝色天空网 · mbsky.com\none step\n42\n');c.eq('primary prompt seen','>>>'in r.stderr,True);c.eq('continuation prompt seen','...'in r.stderr,True);c.eq('quiet removed banner','Type "help"'in r.stderr,False)
script=Path(__file__).with_name('interpreter-greeting.py');r2=subprocess.run([sys.executable,'-I','-B','-X','utf8',str(script),'学习'],capture_output=True,text=True,encoding='utf8',timeout=20)
c.eq('actual script exit',r2.returncode,0);c.eq('script stdout',r2.stdout,'蓝色天空网 · mbsky.com\n学习\n');c.eq('script no prompts',r2.stderr,'')
c.finish('python3-interpreter',version=platform.python_version(),interactive=dict(input=commands,stdout=r.stdout,stderr=r.stderr,arguments=args[1:]),script=dict(stdout=r2.stdout,stderr=r2.stderr),Unix_shebang_and_permissions='not tested on this Windows host',source_code_executed=False)
if __name__=='__main__':main()
本例需在 本地 Python 环境 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地 Python 环境
入口文件:projects/python-lab/python3-interpreter.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
python3-interpreter.py,或下载右侧课程工程包。 - 确认本地已准备 本地 Python 环境。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联82章) · 含当前章节或实例 · mbskycom202611a463.zip
课程关联工程包(登记关联82章) · 含当前章节或实例 · mbskycom202612a292.zip