1  """
       2  Bootstrap script for IDLE as an application bundle.
       3  """
       4  import sys, os
       5  
       6  # Change the current directory the user's home directory, that way we'll get
       7  # a more useful default location in the open/save dialogs.
       8  os.chdir(os.path.expanduser('~/Documents'))
       9  
      10  
      11  # Make sure sys.executable points to the python interpreter inside the
      12  # framework, instead of at the helper executable inside the application
      13  # bundle (the latter works, but doesn't allow access to the window server)
      14  #
      15  #  .../IDLE.app/
      16  #       Contents/
      17  #           MacOS/
      18  #               IDLE (a python script)
      19  #               Python{-32} (symlink)
      20  #           Resources/
      21  #               idlemain.py (this module)
      22  #               ...
      23  #
      24  # ../IDLE.app/Contents/MacOS/Python{-32} is symlinked to
      25  #       ..Library/Frameworks/Python.framework/Versions/m.n
      26  #                   /Resources/Python.app/Contents/MacOS/Python{-32}
      27  #       which is the Python interpreter executable
      28  #
      29  # The flow of control is as follows:
      30  # 1. IDLE.app is launched which starts python running the IDLE script
      31  # 2. IDLE script exports
      32  #       PYTHONEXECUTABLE = .../IDLE.app/Contents/MacOS/Python{-32}
      33  #           (the symlink to the framework python)
      34  # 3. IDLE script alters sys.argv and uses os.execve to replace itself with
      35  #       idlemain.py running under the symlinked python.
      36  #       This is the magic step.
      37  # 4. During interpreter initialization, because PYTHONEXECUTABLE is defined,
      38  #    sys.executable may get set to an useless value.
      39  #
      40  # (Note that the IDLE script and the setting of PYTHONEXECUTABLE is
      41  #  generated automatically by bundlebuilder in the Python 2.x build.
      42  #  Also, IDLE invoked via command line, i.e. bin/idle, bypasses all of
      43  #  this.)
      44  #
      45  # Now fix up the execution environment before importing idlelib.
      46  
      47  # Reset sys.executable to its normal value, the actual path of
      48  # the interpreter in the framework, by following the symlink
      49  # exported in PYTHONEXECUTABLE.
      50  pyex = os.environ['PYTHONEXECUTABLE']
      51  sys.executable = os.path.join(sys.prefix, 'bin', 'python%d.%d'%(sys.version_info[:2]))
      52  
      53  # Remove any sys.path entries for the Resources dir in the IDLE.app bundle.
      54  p = pyex.partition('.app')
      55  if p[2].startswith('/Contents/MacOS/Python'):
      56      sys.path = [value for value in sys.path if
      57              value.partition('.app') != (p[0], p[1], '/Contents/Resources')]
      58  
      59  # Unexport PYTHONEXECUTABLE so that the other Python processes started
      60  # by IDLE have a normal sys.executable.
      61  del os.environ['PYTHONEXECUTABLE']
      62  
      63  # Look for the -psn argument that the launcher adds and remove it, it will
      64  # only confuse the IDLE startup code.
      65  for idx, value in enumerate(sys.argv):
      66      if value.startswith('-psn_'):
      67          del sys.argv[idx]
      68          break
      69  
      70  # Now it is safe to import idlelib.
      71  from idlelib.pyshell import main
      72  if __name__ == '__main__':
      73      main()