]> granicus.if.org Git - python/commitdiff
Issue #21923: Prevent AttributeError in distutils.sysconfig.customize_compiler
authorNed Deily <nad@acm.org>
Sun, 6 Jul 2014 23:11:44 +0000 (16:11 -0700)
committerNed Deily <nad@acm.org>
Sun, 6 Jul 2014 23:11:44 +0000 (16:11 -0700)
due to possible uninitialized _config_vars.  Original patch by Alex Gaynor.

Lib/distutils/sysconfig.py
Lib/distutils/tests/test_sysconfig.py
Misc/NEWS

index 4aa93346d5306f6b93b3d6948c984ead1e932612..de7da1d41394f0ba479eb7f6f60e95b89e130d63 100644 (file)
@@ -165,7 +165,8 @@ def customize_compiler(compiler):
             # version and build tools may not support the same set
             # of CPU architectures for universal builds.
             global _config_vars
-            if not _config_vars.get('CUSTOMIZED_OSX_COMPILER', ''):
+            # Use get_config_var() to ensure _config_vars is initialized.
+            if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
                 import _osx_support
                 _osx_support.customize_compiler(_config_vars)
                 _config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'
index ea8d7b5dd6eddaa7cc711d2e17055df78a77b65e..eb4d27c39efd0ec657be4c5002cde78f3e4b1858 100644 (file)
@@ -3,6 +3,9 @@ import os
 import test
 import unittest
 import shutil
+import subprocess
+import sys
+import textwrap
 
 from distutils import sysconfig
 from distutils.tests import support
@@ -99,6 +102,24 @@ class SysconfigTestCase(support.EnvironGuard,
         self.assertEqual(global_sysconfig.get_config_var('LDSHARED'), sysconfig.get_config_var('LDSHARED'))
         self.assertEqual(global_sysconfig.get_config_var('CC'), sysconfig.get_config_var('CC'))
 
+    def test_customize_compiler_before_get_config_vars(self):
+        # Issue #21923: test that a Distribution compiler
+        # instance can be called without an explicit call to
+        # get_config_vars().
+        with open(TESTFN, 'w') as f:
+            f.writelines(textwrap.dedent('''\
+                from distutils.core import Distribution
+                config = Distribution().get_command_obj('config')
+                # try_compile may pass or it may fail if no compiler
+                # is found but it should not raise an exception.
+                rc = config.try_compile('int x;')
+                '''))
+        p = subprocess.Popen([str(sys.executable), TESTFN],
+                stdout=subprocess.PIPE,
+                stderr=subprocess.STDOUT,
+                universal_newlines=True)
+        outs, errs = p.communicate()
+        self.assertEqual(0, p.returncode, "Subprocess failed: " + outs)
 
 
 def test_suite():
index 4384562db7b3ff171025939ffdc4c32753b13f90..97bea23f88e1d0ed68e07dcac9255ea513fc0e91 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@ Library
 - Issue #19870: BaseCookie now parses 'secure' and 'httponly' flags.
   Backport of issue #16611.
 
+- Issue #21923: Prevent AttributeError in distutils.sysconfig.customize_compiler
+  due to possible uninitialized _config_vars.
+
 
 What's New in Python 2.7.8?
 ===========================