Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6116f07

Browse files
committed
A script to fix Apple-installed Python 2.3 (and a test whether the user
needs to run it in the Makefile). After installing a newer framework Python the apple-installed Python can no longer build extension modules, because they will inadvertantly be linked against the newer framework. This script modifies lib/config/Makefile so it will link extensions with "-undefined dynamic_lookup", which forestalls this problem. Will backport to 2.4 and 2.3.
1 parent 9935e7f commit 6116f07

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

Mac/OSX/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ compileall=$(srcdir)/Lib/compileall.py
6060
bundlebuilder=$(srcdir)/Lib/plat-mac/bundlebuilder.py
6161

6262
installapps: install_PythonLauncher install_Python install_BuildApplet install_IDE \
63-
install_IDLE install_PackageManager
63+
install_IDLE install_PackageManager checkapplepython
6464

6565
install_PythonLauncher:
6666
cd $(srcdir)/Mac/OSX/PythonLauncher/PythonLauncher.pbproj ; \
@@ -257,3 +257,10 @@ installextras:
257257
$(DESTDIR)$(PYTHONAPPSDIR)/Extras/Demo
258258
$(BUILDPYTHON) $(srcdir)/Mac/OSX/Extras.install.py $(srcdir)/Tools \
259259
$(DESTDIR)$(PYTHONAPPSDIR)/Extras/Tools
260+
261+
checkapplepython:
262+
@if ! $(BUILDPYTHON) $(srcdir)/Mac/OSX/fixapplepython23.py -n; then \
263+
echo "* WARNING: Apple-installed Python 2.3 will have trouble building extensions from now on."; \
264+
echo "* WARNING: Run $(srcdir)/Mac/OSX/fixapplepython23.py with \"sudo\" to fix this."; \
265+
fi
266+

Mac/OSX/fixapplepython23.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
OLD_LDSHARED='LDSHARED=\t$(CC) $(LDFLAGS) -bundle -framework $(PYTHONFRAMEWORK)\n'
20+
OLD_BLDSHARED='B' + OLD_LDSHARED
21+
NEW_LDSHARED='LDSHARED=\t$(CC) $(LDFLAGS) -bundle -undefined dynamic_lookup\n'
22+
NEW_BLDSHARED='B' + NEW_LDSHARED
23+
24+
def findline(lines, start):
25+
"""return line starting with given string or -1"""
26+
for i in range(len(lines)):
27+
if lines[i][:len(start)] == start:
28+
return i
29+
return -1
30+
31+
def fix(makefile, do_apply):
32+
"""Fix the Makefile, if required."""
33+
fixed = False
34+
lines = open(makefile).readlines()
35+
36+
i = findline(lines, 'LDSHARED=')
37+
if i < 0:
38+
print 'fixapplepython23: Python installation not fixed (appears broken, no LDSHARED)'
39+
return 2
40+
if lines[i] == OLD_LDSHARED:
41+
lines[i] = NEW_LDSHARED
42+
fixed = True
43+
elif lines[i] != NEW_LDSHARED:
44+
print 'fixapplepython23: Python installation not fixed (appears modified, unexpected LDSHARED)'
45+
return 2
46+
47+
i = findline(lines, 'BLDSHARED=')
48+
if i < 0:
49+
print 'fixapplepython23: Python installation not fixed (appears broken, no BLDSHARED)'
50+
return 2
51+
if lines[i] == OLD_BLDSHARED:
52+
lines[i] = NEW_BLDSHARED
53+
fixed = True
54+
elif lines[i] != NEW_BLDSHARED:
55+
print 'fixapplepython23: Python installation not fixed (appears modified, unexpected BLDSHARED)'
56+
return 2
57+
58+
if fixed:
59+
if do_apply:
60+
print 'fixapplepython23: Fix to Apple-installed Python 2.3 applied'
61+
os.rename(makefile, makefile + '~')
62+
open(makefile, 'w').writelines(lines)
63+
return 0
64+
else:
65+
print 'fixapplepython23: Fix to Apple-installed Python 2.3 should be applied'
66+
return 1
67+
else:
68+
print 'fixapplepython23: No fix needed, appears to have been applied before'
69+
return 0
70+
71+
def main():
72+
# Check for -n option
73+
if len(sys.argv) > 1 and sys.argv[1] == '-n':
74+
do_apply = False
75+
else:
76+
do_apply = True
77+
# First check OS version
78+
if gestalt.gestalt('sysv') < 0x1030:
79+
print 'fixapplepython23: no fix needed on MacOSX < 10.3'
80+
sys.exit(0)
81+
# Test that a framework Python is indeed installed
82+
if not os.path.exists(MAKEFILE):
83+
print 'fixapplepython23: Python framework does not appear to be installed (?), nothing fixed'
84+
sys.exit(0)
85+
# Check that we can actually write the file
86+
if do_apply and not os.access(MAKEFILE, os.W_OK):
87+
print 'fixapplepython23: No write permission, please run with "sudo"'
88+
sys.exit(2)
89+
# And finally fix it
90+
rv = fix(MAKEFILE, do_apply)
91+
sys.exit(rv)
92+
93+
if __name__ == '__main__':
94+
main()
95+

0 commit comments

Comments
 (0)