Edit: As of November 21, 2011, this is fixed. See https://trac.macports.org/ticket/30031 for more information.
I use MacPorts for my Python installations. With the latest Xcode 4.2, MacPorts uses clang as the default compiler. This causes all sorts of fun with decimal arithmetics, such as:
$ python
Python 2.6.7 (r267:88850, Oct 28 2011, 17:33:57)
[GCC 4.2.1 Compatible Apple Clang 3.0] on darwin
>>> from decimal import Decimal
>>> Decimal(10)/Decimal(5)
Decimal('8.955976040786690048E-10')
Note that Python is compiled with Clang 3.0.
The fix is to compile Python with GCC. The easiest way to accomplish this is to edit the Portfile and for MacPorts to use a specific compiler.
MacPorts keeps a local version of available Python ports at:
/opt/local/var/macports/sources/rsync.macports.org/release/tarballs/ports/lang/
For example, Python 2.6 is at:
/opt/local/var/macports/sources/rsync.macports.org/release/tarballs/ports/lang/python26
Edit Portfile and add the following line:
configure.compiler gcc-4.2
I add it in the # ensure that correct compiler is used section, before configure.cc is used, like so:
# ensure that correct compiler is used
configure.compiler gcc-4.2
build.args-append MAKE="${build.cmd} CC=${configure.cc}"
destroot.args-append MAKE="${destroot.cmd} CC=${configure.cc}"
Now recompile python:
sudo port -n upgrade --force python26
After waiting for a while everything will be OK again:
$ python
Python 2.6.7 (r267:88850, Oct 29 2011, 21:09:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>>> from decimal import Decimal
>>> Decimal(10)/Decimal(5)
Decimal('2')
Note that Python is now compiled with GCC 4.2.1.
The steps I described is a hack and the next time you perform port selfupdate your changes will be wiped out. But I imagine this bug will be fixed before you have to update Python again. If you want to do things properly by setting up your own local repository, see the MacPorts guide.
You should obviously recompile all versions of MacPorts Python you use, as this bug isn’t isolated to 2.6.
For more detailed discussions on the bug, see:
For information on selecting the right compiler for MacPorts, see the wiki page.
-
mintyfresh posted this