Merged revisions 63955 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r63955 | ronald.oussoren | 2008-06-05 14:58:24 +0200 (Thu, 05 Jun 2008) | 20 lines

  MacOS X: Enable 4-way universal builds

  This patch adds a new configure argument on OSX:
          --with-universal-archs=[32-bit|64-bit|all]

  When used with the --enable-universalsdk option this controls which
  CPU architectures are includes in the framework. The default is 32-bit,
  meaning i386 and ppc. The most useful alternative is 'all', which includes
  all 4 CPU architectures supported by MacOS X (i386, ppc, x86_64 and ppc64).

  This includes limited support for the Carbon bindings in 64-bit mode as well,
  limited because (a) I haven't done extensive testing and (b) a large portion
  of the Carbon API's aren't available in 64-bit mode anyway.

  I've also duplicated a feature of Apple's build of python: setting the
  environment variable 'ARCHFLAGS' controls the '-arch' flags used for building
  extensions using distutils.
........
diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py
index d2b2c9a..3a120dd 100644
--- a/Lib/distutils/sysconfig.py
+++ b/Lib/distutils/sysconfig.py
@@ -516,6 +516,26 @@
                     flags = re.sub('-isysroot [^ \t]*', ' ', flags)
                     _config_vars[key] = flags
 
+            else:
+
+                # Allow the user to override the architecture flags using
+                # an environment variable.
+                # NOTE: This name was introduced by Apple in OSX 10.5 and
+                # is used by several scripting languages distributed with
+                # that OS release.
+
+                if 'ARCHFLAGS' in os.environ:
+                    arch = os.environ['ARCHFLAGS']
+                    for key in ('LDFLAGS', 'BASECFLAGS',
+                        # a number of derived variables. These need to be
+                        # patched up as well.
+                        'CFLAGS', 'PY_CFLAGS', 'BLDSHARED'):
+
+                        flags = _config_vars[key]
+                        flags = re.sub('-arch\s+\w+\s', ' ', flags)
+                        flags = flags + ' ' + arch
+                        _config_vars[key] = flags
+
     if args:
         vals = []
         for name in args:
diff --git a/Lib/distutils/unixccompiler.py b/Lib/distutils/unixccompiler.py
index 25a042d..87ce921 100644
--- a/Lib/distutils/unixccompiler.py
+++ b/Lib/distutils/unixccompiler.py
@@ -63,7 +63,7 @@
         stripArch = '-arch' in cc_args
         stripSysroot = '-isysroot' in cc_args
 
-    if stripArch:
+    if stripArch or 'ARCHFLAGS' in os.environ:
         while True:
             try:
                 index = compiler_so.index('-arch')
@@ -72,6 +72,12 @@
             except ValueError:
                 break
 
+    if 'ARCHFLAGS' in os.environ and not stripArch:
+        # User specified different -arch flags in the environ,
+        # see also distutils.sysconfig
+        compiler_so = compiler_so + ' ' + os.environ['ARCHFLAGS']
+
+
     if stripSysroot:
         try:
             index = compiler_so.index('-isysroot')
diff --git a/Lib/distutils/util.py b/Lib/distutils/util.py
index 72039a7..76798b9 100644
--- a/Lib/distutils/util.py
+++ b/Lib/distutils/util.py
@@ -124,12 +124,19 @@
             osname = "macosx"
 
 
-            if (release + '.') < '10.4.' and \
-                    get_config_vars().get('UNIVERSALSDK', '').strip():
+            if (release + '.') >= '10.4.' and \
+                    '-arch' in get_config_vars().get('CFLAGS', '').strip():
                 # The universal build will build fat binaries, but not on
                 # systems before 10.4
+                #
+                # Try to detect 4-way universal builds, those have machine-type
+                # 'universal' instead of 'fat'.
+
                 machine = 'fat'
 
+                if '-arch x86_64' in get_config_vars().get('CFLAGS'):
+                    machine = 'universal'
+
             elif machine in ('PowerPC', 'Power_Macintosh'):
                 # Pick a sane name for the PPC architecture.
                 machine = 'ppc'
diff --git a/Lib/test/test_macos.py b/Lib/test/test_macos.py
new file mode 100644
index 0000000..e65b174
--- /dev/null
+++ b/Lib/test/test_macos.py
@@ -0,0 +1,43 @@
+import unittest
+import MacOS
+import Carbon.File
+from test import test_support
+import os
+
+TESTFN2 = test_support.TESTFN + '2'
+
+class TestMacOS(unittest.TestCase):
+
+    def testOpenRF(self):
+        try:
+            fp = open(test_support.TESTFN, 'w')
+            fp.write('hello world\n')
+            fp.close()
+
+            rfp = MacOS.openrf(test_support.TESTFN, '*wb')
+            rfp.write('goodbye world\n')
+            rfp.close()
+
+
+            fp = open(test_support.TESTFN, 'r')
+            data = fp.read()
+            fp.close()
+            self.assertEquals(data, 'hello world\n')
+
+            rfp = MacOS.openrf(test_support.TESTFN, '*rb')
+            data = rfp.read(100)
+            data2 = rfp.read(100)
+            rfp.close()
+            self.assertEquals(data, 'goodbye world\n')
+            self.assertEquals(data2, '')
+
+
+        finally:
+            os.unlink(test_support.TESTFN)
+
+def test_main():
+    test_support.run_unittest(TestMacOS)
+
+
+if __name__ == '__main__':
+    test_main()