[Python-checkins] distutils2: now compilers are loaded using fully qualified names

tarek.ziade python-checkins at python.org
Thu Nov 11 22:18:10 CET 2010


tarek.ziade pushed b4ccfe426524 to distutils2:

http://hg.python.org/distutils2/rev/b4ccfe426524
changeset:   809:b4ccfe426524
user:        Tarek Ziade <tarek at ziade.org>
date:        Thu Nov 11 21:51:02 2010 +0100
summary:     now compilers are loaded using fully qualified names
files:       distutils2/command/build.py, distutils2/command/build_ext.py, distutils2/compiler/__init__.py, distutils2/compiler/ccompiler.py

diff --git a/distutils2/command/build.py b/distutils2/command/build.py
--- a/distutils2/command/build.py
+++ b/distutils2/command/build.py
@@ -1,17 +1,15 @@
 """distutils.command.build
 
-Implements the Distutils 'build' command."""
-
-
-import sys, os
+Implements the Distutils 'build' command.
+"""
+import sys
+import os
 
 from distutils2.util import get_platform
 from distutils2.command.cmd import Command
 from distutils2.errors import DistutilsOptionError
+from distutils2.compiler import show_compilers
 
-def show_compilers():
-    from distutils2.compiler.ccompiler import show_compilers
-    show_compilers()
 
 class build(Command):
 
diff --git a/distutils2/command/build_ext.py b/distutils2/command/build_ext.py
--- a/distutils2/command/build_ext.py
+++ b/distutils2/command/build_ext.py
@@ -12,7 +12,7 @@
 from distutils2.command.cmd import Command
 from distutils2.errors import (CCompilerError, CompileError, DistutilsError,
                                DistutilsPlatformError, DistutilsSetupError)
-from distutils2.compiler import customize_compiler
+from distutils2.compiler import customize_compiler, show_compilers
 from distutils2.util import newer_group
 from distutils2.extension import Extension
 from distutils2 import logger
@@ -39,11 +39,6 @@
     (r'^[a-zA-Z_][a-zA-Z_0-9]*(\.[a-zA-Z_][a-zA-Z_0-9]*)*$')
 
 
-def show_compilers ():
-    from distutils2.compiler.ccompiler import show_compilers
-    show_compilers()
-
-
 class build_ext(Command):
 
     description = "build C/C++ extensions (compile/link to build directory)"
diff --git a/distutils2/compiler/__init__.py b/distutils2/compiler/__init__.py
--- a/distutils2/compiler/__init__.py
+++ b/distutils2/compiler/__init__.py
@@ -1,7 +1,10 @@
 import os
 import sys
 import re
+
 from distutils2._backport import sysconfig
+from distutils2.util import resolve_name
+from distutils2.errors import DistutilsModuleError, DistutilsPlatformError
 
 
 def customize_compiler(compiler):
@@ -101,35 +104,24 @@
     # Default to Unix compiler
     return 'unix'
 
-# Map compiler types to (module_name, class_name) pairs -- ie. where to
-# find the code that implements an interface to this compiler.  (The module
-# is assumed to be in the 'distutils' package.)
-compiler_class = { 'unix':    ('unixccompiler', 'UnixCCompiler',
-                               "standard UNIX-style compiler"),
-                   'msvc':    ('msvccompiler', 'MSVCCompiler',
-                               "Microsoft Visual C++"),
-                   'cygwin':  ('cygwinccompiler', 'CygwinCCompiler',
-                               "Cygwin port of GNU C Compiler for Win32"),
-                   'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
-                               "Mingw32 port of GNU C Compiler for Win32"),
-                   'bcpp':    ('bcppcompiler', 'BCPPCompiler',
-                               "Borland C++ Compiler"),
-                   'emx':     ('emxccompiler', 'EMXCCompiler',
-                               "EMX port of GNU C Compiler for OS/2"),
-                 }
+
+_COMPILERS = {'unix': 'distutils2.compiler.unixccompiler.UnixCCompiler',
+              'msvc': 'distutils2.compiler.msvccompiler.MSVCCompiler',
+              'cygwin': 'distutils2.compiler.cygwinccompiler.CygWinCCompiler',
+              'mingw32': 'distutils2.compiler.cygwinccompiler.Mingw32CCompiler',
+              'bcpp': 'distutils2.compilers.bcppcompiler.BCPPCompiler'}
 
 def show_compilers():
     """Print list of available compilers (used by the "--help-compiler"
     options to "build", "build_ext", "build_clib").
     """
-    # XXX this "knows" that the compiler option it's describing is
-    # "--compiler", which just happens to be the case for the three
-    # commands that use it.
     from distutils2.fancy_getopt import FancyGetopt
     compilers = []
-    for compiler in compiler_class.keys():
-        compilers.append(("compiler="+compiler, None,
-                          compiler_class[compiler][2]))
+
+    for compiler, location in _COMPILERS.items():
+        klass = resolve_name(location)
+        compilers.append(("compiler=" + compiler, None, klass.description))
+
     compilers.sort()
     pretty_printer = FancyGetopt(compilers)
     pretty_printer.print_help("List of available compilers:")
@@ -153,26 +145,19 @@
         if compiler is None:
             compiler = get_default_compiler(plat)
 
-        (module_name, class_name, long_description) = compiler_class[compiler]
+        location = _COMPILERS[compiler]
     except KeyError:
         msg = "don't know how to compile C/C++ code on platform '%s'" % plat
         if compiler is not None:
             msg = msg + " with '%s' compiler" % compiler
-        raise DistutilsPlatformError, msg
+        raise DistutilsPlatformError(msg)
 
     try:
-        module_name = "distutils2.compiler." + module_name
-        __import__ (module_name)
-        module = sys.modules[module_name]
-        cls = vars(module)[class_name]
+        cls = resolve_name(location)
     except ImportError:
-        raise DistutilsModuleError, \
-              "can't compile C/C++ code: unable to load module '%s'" % \
-              module_name
-    except KeyError:
-        raise DistutilsModuleError, \
-              ("can't compile C/C++ code: unable to find class '%s' " +
-               "in module '%s'") % (class_name, module_name)
+        raise DistutilsModuleError(
+              "can't compile C/C++ code: unable to load '%s'" % \
+              location)
 
     # XXX The None is necessary to preserve backwards compatibility
     # with classes that expect verbose to be the first positional
diff --git a/distutils2/compiler/ccompiler.py b/distutils2/compiler/ccompiler.py
--- a/distutils2/compiler/ccompiler.py
+++ b/distutils2/compiler/ccompiler.py
@@ -32,11 +32,7 @@
     # 'compiler_type' is a class attribute that identifies this class.  It
     # keeps code that wants to know what kind of compiler it's dealing with
     # from having to import all possible compiler classes just to do an
-    # 'isinstance'.  In concrete CCompiler subclasses, 'compiler_type'
-    # should really, really be one of the keys of the 'compiler_class'
-    # dictionary (see below -- used by the 'new_compiler()' factory
-    # function) -- authors of new compiler interface classes are
-    # responsible for updating 'compiler_class'!
+    # 'isinstance'.
     compiler_type = None
     description = None
 

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list