/Mac/IDLE/idlemain.py

http://unladen-swallow.googlecode.com/ · Python · 73 lines · 16 code · 8 blank · 49 comment · 5 complexity · b6f084d424f9ec4b430a0fa349336b62 MD5 · raw file

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