Skip to content

Commit 4d031e5

Browse files
author
cvs2svn
committed
This commit was manufactured by cvs2svn to create branch
'release24-maint'.
1 parent 68909fd commit 4d031e5

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

Mac/OSX/fixapplepython23.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""fixapplepython23 - Fix Apple-installed Python 2.3 (on Mac OS X 10.3)
2+
3+
Python 2.3 (and 2.3.X for X<5) have the problem that building an extension
4+
for a framework installation may accidentally pick up the framework
5+
of a newer Python, in stead of the one that was used to build the extension.
6+
7+
This script modifies the Makefile (in .../lib/python2.3/config) to use
8+
the newer method of linking extensions with "-undefined dynamic_lookup"
9+
which fixes this problem.
10+
11+
The script will first check all prerequisites, and return a zero exit
12+
status also when nothing needs to be fixed.
13+
"""
14+
import sys
15+
import os
16+
import gestalt
17+
18+
MAKEFILE='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/Makefile'
19+
CHANGES=((
20+
'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n',
21+
'LDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
22+
),(
23+
'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n',
24+
'BLDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
25+
),(
26+
'CC=\t\tgcc\n',
27+
'CC=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc\n'
28+
),(
29+
'CXX=\t\tc++\n',
30+
'CXX=\t\t/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++\n'
31+
))
32+
33+
GCC_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-gcc'
34+
GXX_SCRIPT='/System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/config/PantherPythonFix/run-g++'
35+
SCRIPT="""#!/bin/sh
36+
export MACOSX_DEPLOYMENT_TARGET=10.3
37+
exec %s "${@}"
38+
"""
39+
40+
def findline(lines, start):
41+
"""return line starting with given string or -1"""
42+
for i in range(len(lines)):
43+
if lines[i][:len(start)] == start:
44+
return i
45+
return -1
46+
47+
def fix(makefile, do_apply):
48+
"""Fix the Makefile, if required."""
49+
fixed = False
50+
lines = open(makefile).readlines()
51+
52+
for old, new in CHANGES:
53+
i = findline(lines, new)
54+
if i >= 0:
55+
# Already fixed
56+
continue
57+
i = findline(lines, old)
58+
if i < 0:
59+
print 'fixapplepython23: Python installation not fixed (appears broken)'
60+
print 'fixapplepython23: missing line:', old
61+
return 2
62+
lines[i] = new
63+
fixed = True
64+
65+
if fixed:
66+
if do_apply:
67+
print 'fixapplepython23: Fix to Apple-installed Python 2.3 applied'
68+
os.rename(makefile, makefile + '~')
69+
open(makefile, 'w').writelines(lines)
70+
return 0
71+
else:
72+
print 'fixapplepython23: Fix to Apple-installed Python 2.3 should be applied'
73+
return 1
74+
else:
75+
print 'fixapplepython23: No fix needed, appears to have been applied before'
76+
return 0
77+
78+
def makescript(filename, compiler):
79+
"""Create a wrapper script for a compiler"""
80+
dirname = os.path.split(filename)[0]
81+
if not os.access(dirname, os.X_OK):
82+
os.mkdir(dirname, 0755)
83+
fp = open(filename, 'w')
84+
fp.write(SCRIPT % compiler)
85+
fp.close()
86+
os.chmod(filename, 0755)
87+
print 'fixapplepython23: Created', filename
88+
89+
def main():
90+
# Check for -n option
91+
if len(sys.argv) > 1 and sys.argv[1] == '-n':
92+
do_apply = False
93+
else:
94+
do_apply = True
95+
# First check OS version
96+
if gestalt.gestalt('sysv') < 0x1030:
97+
print 'fixapplepython23: no fix needed on MacOSX < 10.3'
98+
sys.exit(0)
99+
# Test that a framework Python is indeed installed
100+
if not os.path.exists(MAKEFILE):
101+
print 'fixapplepython23: Python framework does not appear to be installed (?), nothing fixed'
102+
sys.exit(0)
103+
# Check that we can actually write the file
104+
if do_apply and not os.access(MAKEFILE, os.W_OK):
105+
print 'fixapplepython23: No write permission, please run with "sudo"'
106+
sys.exit(2)
107+
# Create the shell scripts
108+
if do_apply:
109+
if not os.access(GCC_SCRIPT, os.X_OK):
110+
makescript(GCC_SCRIPT, "gcc")
111+
if not os.access(GXX_SCRIPT, os.X_OK):
112+
makescript(GXX_SCRIPT, "g++")
113+
# Finally fix the makefile
114+
rv = fix(MAKEFILE, do_apply)
115+
sys.exit(rv)
116+
117+
if __name__ == '__main__':
118+
main()
119+

0 commit comments

Comments
 (0)