Path: blob/main/Mac/IDLE/IDLE.app/Contents/Resources/idlemain.py
12 views
"""1Bootstrap script for IDLE as an application bundle.2"""3import sys, os45# Change the current directory the user's home directory, that way we'll get6# a more useful default location in the open/save dialogs.7os.chdir(os.path.expanduser('~/Documents'))8910# Make sure sys.executable points to the python interpreter inside the11# framework, instead of at the helper executable inside the application12# bundle (the latter works, but doesn't allow access to the window server)13#14# .../IDLE.app/15# Contents/16# MacOS/17# IDLE (a python script)18# Python{-32} (symlink)19# Resources/20# idlemain.py (this module)21# ...22#23# ../IDLE.app/Contents/MacOS/Python{-32} is symlinked to24# ..Library/Frameworks/Python.framework/Versions/m.n25# /Resources/Python.app/Contents/MacOS/Python{-32}26# which is the Python interpreter executable27#28# The flow of control is as follows:29# 1. IDLE.app is launched which starts python running the IDLE script30# 2. IDLE script exports31# PYTHONEXECUTABLE = .../IDLE.app/Contents/MacOS/Python{-32}32# (the symlink to the framework python)33# 3. IDLE script alters sys.argv and uses os.execve to replace itself with34# idlemain.py running under the symlinked python.35# This is the magic step.36# 4. During interpreter initialization, because PYTHONEXECUTABLE is defined,37# sys.executable may get set to an useless value.38#39# (Note that the IDLE script and the setting of PYTHONEXECUTABLE is40# generated automatically by bundlebuilder in the Python 2.x build.41# Also, IDLE invoked via command line, i.e. bin/idle, bypasses all of42# this.)43#44# Now fix up the execution environment before importing idlelib.4546# Reset sys.executable to its normal value, the actual path of47# the interpreter in the framework, by following the symlink48# exported in PYTHONEXECUTABLE.49pyex = os.environ['PYTHONEXECUTABLE']50sys.executable = os.path.join(sys.prefix, 'bin', 'python%d.%d'%(sys.version_info[:2]))5152# Remove any sys.path entries for the Resources dir in the IDLE.app bundle.53p = pyex.partition('.app')54if p[2].startswith('/Contents/MacOS/Python'):55sys.path = [value for value in sys.path if56value.partition('.app') != (p[0], p[1], '/Contents/Resources')]5758# Unexport PYTHONEXECUTABLE so that the other Python processes started59# by IDLE have a normal sys.executable.60del os.environ['PYTHONEXECUTABLE']6162# Look for the -psn argument that the launcher adds and remove it, it will63# only confuse the IDLE startup code.64for idx, value in enumerate(sys.argv):65if value.startswith('-psn_'):66del sys.argv[idx]67break6869# Now it is safe to import idlelib.70from idlelib.pyshell import main71if __name__ == '__main__':72main()737475