Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7bc5fb6

Browse files
committed
Issue #21923: Prevent AttributeError in distutils.sysconfig.customize_compiler
due to possible uninitialized _config_vars. Original patch by Alex Gaynor.
1 parent 898eb82 commit 7bc5fb6

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

Lib/distutils/sysconfig.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ def customize_compiler(compiler):
179179
# version and build tools may not support the same set
180180
# of CPU architectures for universal builds.
181181
global _config_vars
182-
if not _config_vars.get('CUSTOMIZED_OSX_COMPILER', ''):
182+
# Use get_config_var() to ensure _config_vars is initialized.
183+
if not get_config_var('CUSTOMIZED_OSX_COMPILER'):
183184
import _osx_support
184185
_osx_support.customize_compiler(_config_vars)
185186
_config_vars['CUSTOMIZED_OSX_COMPILER'] = 'True'

Lib/distutils/tests/test_sysconfig.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Tests for distutils.sysconfig."""
22
import os
33
import shutil
4+
import subprocess
5+
import sys
6+
import textwrap
47
import unittest
58

69
from distutils import sysconfig
@@ -174,6 +177,25 @@ def test_SO_in_vars(self):
174177
self.assertIsNotNone(vars['SO'])
175178
self.assertEqual(vars['SO'], vars['EXT_SUFFIX'])
176179

180+
def test_customize_compiler_before_get_config_vars(self):
181+
# Issue #21923: test that a Distribution compiler
182+
# instance can be called without an explicit call to
183+
# get_config_vars().
184+
with open(TESTFN, 'w') as f:
185+
f.writelines(textwrap.dedent('''\
186+
from distutils.core import Distribution
187+
config = Distribution().get_command_obj('config')
188+
# try_compile may pass or it may fail if no compiler
189+
# is found but it should not raise an exception.
190+
rc = config.try_compile('int x;')
191+
'''))
192+
p = subprocess.Popen([str(sys.executable), TESTFN],
193+
stdout=subprocess.PIPE,
194+
stderr=subprocess.STDOUT,
195+
universal_newlines=True)
196+
outs, errs = p.communicate()
197+
self.assertEqual(0, p.returncode, "Subprocess failed: " + outs)
198+
177199

178200
def test_suite():
179201
suite = unittest.TestSuite()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ Library
136136

137137
- Issue #21801: Validate that __signature__ is None or an instance of Signature.
138138

139+
- Issue #21923: Prevent AttributeError in distutils.sysconfig.customize_compiler
140+
due to possible uninitialized _config_vars.
141+
139142
Build
140143
-----
141144

0 commit comments

Comments
 (0)