From ae7731af45263a5951c20265d704df69d603a6d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarek=20Ziad=C3=A9?= Date: Mon, 11 Jan 2010 23:41:32 +0000 Subject: [PATCH] module cleanup --- Lib/distutils/ccompiler.py | 217 ++++++++++++------------------------- 1 file changed, 69 insertions(+), 148 deletions(-) diff --git a/Lib/distutils/ccompiler.py b/Lib/distutils/ccompiler.py index 83ba83a02b6..97c61dd46f6 100644 --- a/Lib/distutils/ccompiler.py +++ b/Lib/distutils/ccompiler.py @@ -9,11 +9,12 @@ import sys import os import re -from distutils.errors import CompileError, LinkError, UnknownFileError +from distutils.errors import (CompileError, LinkError, UnknownFileError, + DistutilsPlatformError, DistutilsModuleError) from distutils.spawn import spawn from distutils.file_util import move_file from distutils.dir_util import mkpath -from distutils.dep_util import newer_pairwise, newer_group +from distutils.dep_util import newer_group from distutils.util import split_quoted, execute from distutils import log @@ -87,11 +88,7 @@ class CCompiler: } language_order = ["c++", "objc", "c"] - def __init__ (self, - verbose=0, - dry_run=0, - force=0): - + def __init__ (self, verbose=0, dry_run=0, force=0): self.dry_run = dry_run self.force = force self.verbose = verbose @@ -127,11 +124,7 @@ class CCompiler: for key in self.executables.keys(): self.set_executable(key, self.executables[key]) - # __init__ () - - - def set_executables (self, **args): - + def set_executables(self, **args): """Define the executables (and options for them) that will be run to perform the various stages of compilation. The exact set of executables that may be specified here depends on the compiler @@ -164,26 +157,21 @@ class CCompiler: (key, self.__class__.__name__) self.set_executable(key, args[key]) - # set_executables () - def set_executable(self, key, value): if isinstance(value, str): setattr(self, key, split_quoted(value)) else: setattr(self, key, value) - - def _find_macro (self, name): + def _find_macro(self, name): i = 0 for defn in self.macros: if defn[0] == name: return i i = i + 1 - return None - - def _check_macro_definitions (self, definitions): + def _check_macro_definitions(self, definitions): """Ensures that every element of 'definitions' is a valid macro definition, ie. either (name,value) 2-tuple or a (name,) tuple. Do nothing if all definitions are OK, raise TypeError otherwise. @@ -202,7 +190,7 @@ class CCompiler: # -- Bookkeeping methods ------------------------------------------- - def define_macro (self, name, value=None): + def define_macro(self, name, value=None): """Define a preprocessor macro for all compilations driven by this compiler object. The optional parameter 'value' should be a string; if it is not supplied, then the macro will be defined @@ -218,8 +206,7 @@ class CCompiler: defn = (name, value) self.macros.append (defn) - - def undefine_macro (self, name): + def undefine_macro(self, name): """Undefine a preprocessor macro for all compilations driven by this compiler object. If the same macro is defined by 'define_macro()' and undefined by 'undefine_macro()' the last call @@ -237,8 +224,7 @@ class CCompiler: undefn = (name,) self.macros.append (undefn) - - def add_include_dir (self, dir): + def add_include_dir(self, dir): """Add 'dir' to the list of directories that will be searched for header files. The compiler is instructed to search directories in the order in which they are supplied by successive calls to @@ -246,7 +232,7 @@ class CCompiler: """ self.include_dirs.append (dir) - def set_include_dirs (self, dirs): + def set_include_dirs(self, dirs): """Set the list of directories that will be searched to 'dirs' (a list of strings). Overrides any preceding calls to 'add_include_dir()'; subsequence calls to 'add_include_dir()' add @@ -256,8 +242,7 @@ class CCompiler: """ self.include_dirs = dirs[:] - - def add_library (self, libname): + def add_library(self, libname): """Add 'libname' to the list of libraries that will be included in all links driven by this compiler object. Note that 'libname' should *not* be the name of a file containing a library, but the @@ -273,7 +258,7 @@ class CCompiler: """ self.libraries.append (libname) - def set_libraries (self, libnames): + def set_libraries(self, libnames): """Set the list of libraries to be included in all links driven by this compiler object to 'libnames' (a list of strings). This does not affect any standard system libraries that the linker may @@ -282,29 +267,28 @@ class CCompiler: self.libraries = libnames[:] - def add_library_dir (self, dir): + def add_library_dir(self, dir): """Add 'dir' to the list of directories that will be searched for libraries specified to 'add_library()' and 'set_libraries()'. The linker will be instructed to search for libraries in the order they are supplied to 'add_library_dir()' and/or 'set_library_dirs()'. """ - self.library_dirs.append (dir) + self.library_dirs.append(dir) - def set_library_dirs (self, dirs): + def set_library_dirs(self, dirs): """Set the list of library search directories to 'dirs' (a list of strings). This does not affect any standard library search path that the linker may search by default. """ self.library_dirs = dirs[:] - - def add_runtime_library_dir (self, dir): + def add_runtime_library_dir(self, dir): """Add 'dir' to the list of directories that will be searched for shared libraries at runtime. """ - self.runtime_library_dirs.append (dir) + self.runtime_library_dirs.append(dir) - def set_runtime_library_dirs (self, dirs): + def set_runtime_library_dirs(self, dirs): """Set the list of directories to search for shared libraries at runtime to 'dirs' (a list of strings). This does not affect any standard search path that the runtime linker may search by @@ -312,16 +296,15 @@ class CCompiler: """ self.runtime_library_dirs = dirs[:] - - def add_link_object (self, object): + def add_link_object(self, object): """Add 'object' to the list of object files (or analogues, such as explicitly named library files or the output of "resource compilers") to be included in every link driven by this compiler object. """ - self.objects.append (object) + self.objects.append(object) - def set_link_objects (self, objects): + def set_link_objects(self, objects): """Set the list of object files (or analogues) to be included in every link to 'objects'. This does not affect any standard object files that the linker may include by default (such as system @@ -388,7 +371,7 @@ class CCompiler: cc_args[:0] = before return cc_args - def _fix_compile_args (self, output_dir, macros, include_dirs): + def _fix_compile_args(self, output_dir, macros, include_dirs): """Typecheck and fix-up some of the arguments to the 'compile()' method, and return fixed-up values. Specifically: if 'output_dir' is None, replaces it with 'self.output_dir'; ensures that 'macros' @@ -400,7 +383,7 @@ class CCompiler: """ if output_dir is None: output_dir = self.output_dir - elif type (output_dir) is not StringType: + elif not isinstance(output_dir, str): raise TypeError, "'output_dir' must be a string or None" if macros is None: @@ -420,9 +403,7 @@ class CCompiler: return output_dir, macros, include_dirs - # _fix_compile_args () - - def _fix_object_args (self, objects, output_dir): + def _fix_object_args(self, objects, output_dir): """Typecheck and fix up some arguments supplied to various methods. Specifically: ensure that 'objects' is a list; if output_dir is None, replace with self.output_dir. Return fixed versions of @@ -440,8 +421,7 @@ class CCompiler: return (objects, output_dir) - - def _fix_lib_args (self, libraries, library_dirs, runtime_library_dirs): + def _fix_lib_args(self, libraries, library_dirs, runtime_library_dirs): """Typecheck and fix up some of the arguments supplied to the 'link_*' methods. Specifically: ensure that all arguments are lists, and augment them with their permanent versions @@ -476,10 +456,7 @@ class CCompiler: return (libraries, library_dirs, runtime_library_dirs) - # _fix_lib_args () - - - def _need_link (self, objects, output_file): + def _need_link(self, objects, output_file): """Return true if we need to relink the files listed in 'objects' to recreate 'output_file'. """ @@ -492,9 +469,7 @@ class CCompiler: newer = newer_group (objects, output_file) return newer - # _need_link () - - def detect_language (self, sources): + def detect_language(self, sources): """Detect the language of a given file, or list of files. Uses language_map, and language_order to do the job. """ @@ -514,18 +489,11 @@ class CCompiler: pass return lang - # detect_language () - # -- Worker methods ------------------------------------------------ # (must be implemented by subclasses) - def preprocess (self, - source, - output_file=None, - macros=None, - include_dirs=None, - extra_preargs=None, - extra_postargs=None): + def preprocess(self, source, output_file=None, macros=None, + include_dirs=None, extra_preargs=None, extra_postargs=None): """Preprocess a single C/C++ source file, named in 'source'. Output will be written to file named 'output_file', or stdout if 'output_file' not supplied. 'macros' is a list of macro @@ -613,12 +581,8 @@ class CCompiler: # should implement _compile(). pass - def create_static_lib (self, - objects, - output_libname, - output_dir=None, - debug=0, - target_lang=None): + def create_static_lib(self, objects, output_libname, output_dir=None, + debug=0, target_lang=None): """Link a bunch of stuff together to create a static library file. The "bunch of stuff" consists of the list of object files supplied as 'objects', the extra object files supplied to @@ -643,26 +607,15 @@ class CCompiler: """ pass - # values for target_desc parameter in link() SHARED_OBJECT = "shared_object" SHARED_LIBRARY = "shared_library" EXECUTABLE = "executable" - def link (self, - target_desc, - objects, - output_filename, - output_dir=None, - libraries=None, - library_dirs=None, - runtime_library_dirs=None, - export_symbols=None, - debug=0, - extra_preargs=None, - extra_postargs=None, - build_temp=None, - target_lang=None): + def link(self, target_desc, objects, output_filename, output_dir=None, + libraries=None, library_dirs=None, runtime_library_dirs=None, + export_symbols=None, debug=0, extra_preargs=None, + extra_postargs=None, build_temp=None, target_lang=None): """Link a bunch of stuff together to create an executable or shared library file. @@ -711,19 +664,11 @@ class CCompiler: # Old 'link_*()' methods, rewritten to use the new 'link()' method. - def link_shared_lib (self, - objects, - output_libname, - output_dir=None, - libraries=None, - library_dirs=None, - runtime_library_dirs=None, - export_symbols=None, - debug=0, - extra_preargs=None, - extra_postargs=None, - build_temp=None, - target_lang=None): + def link_shared_lib(self, objects, output_libname, output_dir=None, + libraries=None, library_dirs=None, + runtime_library_dirs=None, export_symbols=None, + debug=0, extra_preargs=None, extra_postargs=None, + build_temp=None, target_lang=None): self.link(CCompiler.SHARED_LIBRARY, objects, self.library_filename(output_libname, lib_type='shared'), output_dir, @@ -732,37 +677,21 @@ class CCompiler: extra_preargs, extra_postargs, build_temp, target_lang) - def link_shared_object (self, - objects, - output_filename, - output_dir=None, - libraries=None, - library_dirs=None, - runtime_library_dirs=None, - export_symbols=None, - debug=0, - extra_preargs=None, - extra_postargs=None, - build_temp=None, - target_lang=None): + def link_shared_object(self, objects, output_filename, output_dir=None, + libraries=None, library_dirs=None, + runtime_library_dirs=None, export_symbols=None, + debug=0, extra_preargs=None, extra_postargs=None, + build_temp=None, target_lang=None): self.link(CCompiler.SHARED_OBJECT, objects, output_filename, output_dir, libraries, library_dirs, runtime_library_dirs, export_symbols, debug, extra_preargs, extra_postargs, build_temp, target_lang) - - def link_executable (self, - objects, - output_progname, - output_dir=None, - libraries=None, - library_dirs=None, - runtime_library_dirs=None, - debug=0, - extra_preargs=None, - extra_postargs=None, - target_lang=None): + def link_executable(self, objects, output_progname, output_dir=None, + libraries=None, library_dirs=None, + runtime_library_dirs=None, debug=0, extra_preargs=None, + extra_postargs=None, target_lang=None): self.link(CCompiler.EXECUTABLE, objects, self.executable_filename(output_progname), output_dir, libraries, library_dirs, runtime_library_dirs, None, @@ -774,29 +703,26 @@ class CCompiler: # no appropriate default implementation so subclasses should # implement all of these. - def library_dir_option (self, dir): + def library_dir_option(self, dir): """Return the compiler option to add 'dir' to the list of directories searched for libraries. """ raise NotImplementedError - def runtime_library_dir_option (self, dir): + def runtime_library_dir_option(self, dir): """Return the compiler option to add 'dir' to the list of directories searched for runtime libraries. """ raise NotImplementedError - def library_option (self, lib): + def library_option(self, lib): """Return the compiler option to add 'dir' to the list of libraries linked into the shared library or executable. """ raise NotImplementedError - def has_function(self, funcname, - includes=None, - include_dirs=None, - libraries=None, - library_dirs=None): + def has_function(self, funcname, includes=None, include_dirs=None, + libraries=None, library_dirs=None): """Return a boolean indicating whether funcname is supported on the current platform. The optional arguments can be used to augment the compilation environment. @@ -927,28 +853,28 @@ main (int argc, char **argv) { # -- Utility methods ----------------------------------------------- - def announce (self, msg, level=1): + def announce(self, msg, level=1): log.debug(msg) - def debug_print (self, msg): + def debug_print(self, msg): from distutils.debug import DEBUG if DEBUG: print msg - def warn (self, msg): - sys.stderr.write ("warning: %s\n" % msg) + def warn(self, msg): + sys.stderr.write("warning: %s\n" % msg) - def execute (self, func, args, msg=None, level=1): + def execute(self, func, args, msg=None, level=1): execute(func, args, msg, self.dry_run) - def spawn (self, cmd): - spawn (cmd, dry_run=self.dry_run) + def spawn(self, cmd): + spawn(cmd, dry_run=self.dry_run) - def move_file (self, src, dst): - return move_file (src, dst, dry_run=self.dry_run) + def move_file(self, src, dst): + return move_file(src, dst, dry_run=self.dry_run) - def mkpath (self, name, mode=0777): - mkpath (name, mode, dry_run=self.dry_run) + def mkpath(self, name, mode=0777): + mkpath(name, mode, dry_run=self.dry_run) # class CCompiler @@ -974,7 +900,6 @@ _default_compilers = ( ) def get_default_compiler(osname=None, platform=None): - """ Determine the default compiler to use for the given platform. osname should be one of the standard Python OS names (i.e. the @@ -1030,11 +955,7 @@ def show_compilers(): pretty_printer.print_help("List of available compilers:") -def new_compiler (plat=None, - compiler=None, - verbose=0, - dry_run=0, - force=0): +def new_compiler(plat=None, compiler=None, verbose=0, dry_run=0, force=0): """Generate an instance of some CCompiler subclass for the supplied platform/compiler combination. 'plat' defaults to 'os.name' (eg. 'posix', 'nt'), and 'compiler' defaults to the default compiler @@ -1076,10 +997,10 @@ def new_compiler (plat=None, # XXX The None is necessary to preserve backwards compatibility # with classes that expect verbose to be the first positional # argument. - return klass (None, dry_run, force) + return klass(None, dry_run, force) -def gen_preprocess_options (macros, include_dirs): +def gen_preprocess_options(macros, include_dirs): """Generate C pre-processor options (-D, -U, -I) as used by at least two types of compilers: the typical Unix compiler and Visual C++. 'macros' is the usual thing, a list of 1- or 2-tuples, where (name,)