|
3 | 3 | """ |
4 | 4 | import sys, os |
5 | 5 |
|
6 | | -from idlelib.PyShell import main |
7 | | - |
8 | 6 | # Change the current directory the user's home directory, that way we'll get |
9 | 7 | # a more useful default location in the open/save dialogs. |
10 | 8 | os.chdir(os.path.expanduser('~/Documents')) |
|
13 | 11 | # Make sure sys.executable points to the python interpreter inside the |
14 | 12 | # framework, instead of at the helper executable inside the application |
15 | 13 | # bundle (the latter works, but doesn't allow access to the window server) |
16 | | -if sys.executable.endswith('-32'): |
17 | | - sys.executable = os.path.join(sys.prefix, 'bin', 'python-32') |
18 | | -else: |
19 | | - sys.executable = os.path.join(sys.prefix, 'bin', 'python') |
| 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 unuseful 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(os.path.dirname(pyex), os.readlink(pyex)) |
| 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'] |
20 | 62 |
|
21 | 63 | # Look for the -psn argument that the launcher adds and remove it, it will |
22 | 64 | # only confuse the IDLE startup code. |
|
25 | 67 | del sys.argv[idx] |
26 | 68 | break |
27 | 69 |
|
28 | | -#argvemulator.ArgvCollector().mainloop() |
| 70 | +# Now it is safe to import idlelib. |
| 71 | +from idlelib.PyShell import main |
29 | 72 | if __name__ == '__main__': |
30 | 73 | main() |
0 commit comments