Skip to content

Commit 8dc07c0

Browse files
author
Tarek Ziadé
committed
Merged revisions 72585 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r72585 | tarek.ziade | 2009-05-12 19:07:14 +0200 (Tue, 12 May 2009) | 1 line fixed #5977: distutils build_ext.get_outputs was not using the inplace option ........
1 parent 850ba43 commit 8dc07c0

4 files changed

Lines changed: 100 additions & 45 deletions

File tree

Lib/distutils/command/build_ext.py

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -303,38 +303,38 @@ def run(self):
303303

304304
# Setup the CCompiler object that we'll use to do all the
305305
# compiling and linking
306-
self.compiler = new_compiler(compiler=self.compiler,
306+
self._compiler = new_compiler(compiler=self.compiler,
307307
verbose=self.verbose,
308308
dry_run=self.dry_run,
309309
force=self.force)
310-
customize_compiler(self.compiler)
310+
customize_compiler(self._compiler)
311311
# If we are cross-compiling, init the compiler now (if we are not
312312
# cross-compiling, init would not hurt, but people may rely on
313313
# late initialization of compiler even if they shouldn't...)
314314
if os.name == 'nt' and self.plat_name != get_platform():
315-
self.compiler.initialize(self.plat_name)
315+
self._compiler.initialize(self.plat_name)
316316

317317
# And make sure that any compile/link-related options (which might
318318
# come from the command-line or from the setup script) are set in
319319
# that CCompiler object -- that way, they automatically apply to
320320
# all compiling and linking done here.
321321
if self.include_dirs is not None:
322-
self.compiler.set_include_dirs(self.include_dirs)
322+
self._compiler.set_include_dirs(self.include_dirs)
323323
if self.define is not None:
324324
# 'define' option is a list of (name,value) tuples
325325
for (name, value) in self.define:
326-
self.compiler.define_macro(name, value)
326+
self._compiler.define_macro(name, value)
327327
if self.undef is not None:
328328
for macro in self.undef:
329-
self.compiler.undefine_macro(macro)
329+
self._compiler.undefine_macro(macro)
330330
if self.libraries is not None:
331-
self.compiler.set_libraries(self.libraries)
331+
self._compiler.set_libraries(self.libraries)
332332
if self.library_dirs is not None:
333-
self.compiler.set_library_dirs(self.library_dirs)
333+
self._compiler.set_library_dirs(self.library_dirs)
334334
if self.rpath is not None:
335-
self.compiler.set_runtime_library_dirs(self.rpath)
335+
self._compiler.set_runtime_library_dirs(self.rpath)
336336
if self.link_objects is not None:
337-
self.compiler.set_link_objects(self.link_objects)
337+
self._compiler.set_link_objects(self.link_objects)
338338

339339
# Now actually compile and link everything.
340340
self.build_extensions()
@@ -438,9 +438,7 @@ def get_outputs(self):
438438
# "build" tree.
439439
outputs = []
440440
for ext in self.extensions:
441-
fullname = self.get_ext_fullname(ext.name)
442-
outputs.append(os.path.join(self.build_lib,
443-
self.get_ext_filename(fullname)))
441+
outputs.append(self.get_ext_fullpath(ext.name))
444442
return outputs
445443

446444
def build_extensions(self):
@@ -459,24 +457,9 @@ def build_extension(self, ext):
459457
"a list of source filenames") % ext.name
460458
sources = list(sources)
461459

462-
fullname = self.get_ext_fullname(ext.name)
463-
if self.inplace:
464-
# ignore build-lib -- put the compiled extension into
465-
# the source tree along with pure Python modules
466-
467-
modpath = string.split(fullname, '.')
468-
package = string.join(modpath[0:-1], '.')
469-
base = modpath[-1]
470-
471-
build_py = self.get_finalized_command('build_py')
472-
package_dir = build_py.get_package_dir(package)
473-
ext_filename = os.path.join(package_dir,
474-
self.get_ext_filename(base))
475-
else:
476-
ext_filename = os.path.join(self.build_lib,
477-
self.get_ext_filename(fullname))
460+
ext_path = self.get_ext_fullpath(ext.name)
478461
depends = sources + ext.depends
479-
if not (self.force or newer_group(depends, ext_filename, 'newer')):
462+
if not (self.force or newer_group(depends, ext_path, 'newer')):
480463
log.debug("skipping '%s' extension (up-to-date)", ext.name)
481464
return
482465
else:
@@ -507,13 +490,13 @@ def build_extension(self, ext):
507490
for undef in ext.undef_macros:
508491
macros.append((undef,))
509492

510-
objects = self.compiler.compile(sources,
511-
output_dir=self.build_temp,
512-
macros=macros,
513-
include_dirs=ext.include_dirs,
514-
debug=self.debug,
515-
extra_postargs=extra_args,
516-
depends=ext.depends)
493+
objects = self._compiler.compile(sources,
494+
output_dir=self.build_temp,
495+
macros=macros,
496+
include_dirs=ext.include_dirs,
497+
debug=self.debug,
498+
extra_postargs=extra_args,
499+
depends=ext.depends)
517500

518501
# XXX -- this is a Vile HACK!
519502
#
@@ -534,10 +517,10 @@ def build_extension(self, ext):
534517
extra_args = ext.extra_link_args or []
535518

536519
# Detect target language, if not provided
537-
language = ext.language or self.compiler.detect_language(sources)
520+
language = ext.language or self._compiler.detect_language(sources)
538521

539-
self.compiler.link_shared_object(
540-
objects, ext_filename,
522+
self._compiler.link_shared_object(
523+
objects, ext_path,
541524
libraries=self.get_libraries(ext),
542525
library_dirs=ext.library_dirs,
543526
runtime_library_dirs=ext.runtime_library_dirs,
@@ -639,8 +622,28 @@ def find_swig (self):
639622

640623
# -- Name generators -----------------------------------------------
641624
# (extension names, filenames, whatever)
625+
def get_ext_fullpath(self, ext_name):
626+
"""Returns the path of the filename for a given extension.
627+
628+
The file is located in `build_lib` or directly in the package
629+
(inplace option).
630+
"""
631+
if self.inplace:
632+
fullname = self.get_ext_fullname(ext_name)
633+
modpath = fullname.split('.')
634+
package = '.'.join(modpath[0:-1])
635+
base = modpath[-1]
636+
build_py = self.get_finalized_command('build_py')
637+
package_dir = os.path.abspath(build_py.get_package_dir(package))
638+
return os.path.join(package_dir, base)
639+
else:
640+
filename = self.get_ext_filename(ext_name)
641+
return os.path.join(self.build_lib, filename)
642+
643+
def get_ext_fullname(self, ext_name):
644+
"""Returns the fullname of a given extension name.
642645
643-
def get_ext_fullname (self, ext_name):
646+
Adds the `package.` prefix"""
644647
if self.package is None:
645648
return ext_name
646649
else:
@@ -687,7 +690,7 @@ def get_libraries (self, ext):
687690
# Append '_d' to the python import library on debug builds.
688691
if sys.platform == "win32":
689692
from distutils.msvccompiler import MSVCCompiler
690-
if not isinstance(self.compiler, MSVCCompiler):
693+
if not isinstance(self._compiler, MSVCCompiler):
691694
template = "python%d%d"
692695
if self.debug:
693696
template = template + '_d'

Lib/distutils/tests/support.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ def mkdtemp(self):
4242
self.tempdirs.append(d)
4343
return d
4444

45+
def write_file(self, path, content='xxx'):
46+
"""Writes a file in the given path.
47+
48+
path can be a string or a sequence.
49+
"""
50+
if isinstance(path, (list, tuple)):
51+
path = os.path.join(*path)
52+
f = open(path, 'w')
53+
try:
54+
f.write(content)
55+
finally:
56+
f.close()
4557

4658
class DummyCommand:
4759
"""Class to store options for retrieval via set_undefined_options()."""

Lib/distutils/tests/test_build_ext.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_solaris_enable_shared(self):
9999
else:
100100
_config_vars['Py_ENABLE_SHARED'] = old_var
101101

102-
# make sur we get some lobrary dirs under solaris
102+
# make sure we get some library dirs under solaris
103103
self.assert_(len(cmd.library_dirs) > 0)
104104

105105
def test_finalize_options(self):
@@ -219,13 +219,50 @@ def test_get_source_files(self):
219219
cmd.ensure_finalized()
220220
self.assertEquals(cmd.get_source_files(), ['xxx'])
221221

222+
def test_compiler_option(self):
223+
# cmd.compiler is an option and
224+
# should not be overriden by a compiler instance
225+
# when the command is run
226+
dist = Distribution()
227+
cmd = build_ext(dist)
228+
cmd.compiler = 'unix'
229+
cmd.ensure_finalized()
230+
cmd.run()
231+
self.assertEquals(cmd.compiler, 'unix')
232+
222233
def test_get_outputs(self):
223-
modules = [Extension('foo', ['xxx'])]
224-
dist = Distribution({'name': 'xx', 'ext_modules': modules})
234+
tmp_dir = self.mkdtemp()
235+
c_file = os.path.join(tmp_dir, 'foo.c')
236+
self.write_file(c_file, '')
237+
ext = Extension('foo', [c_file])
238+
dist = Distribution({'name': 'xx',
239+
'ext_modules': [ext]})
225240
cmd = build_ext(dist)
226241
cmd.ensure_finalized()
227242
self.assertEquals(len(cmd.get_outputs()), 1)
228243

244+
if os.name == "nt":
245+
cmd.debug = sys.executable.endswith("_d.exe")
246+
247+
cmd.build_lib = os.path.join(self.tmp_dir, 'build')
248+
cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
249+
250+
# issue #5977 : distutils build_ext.get_outputs
251+
# returns wrong result with --inplace
252+
cmd.inplace = 1
253+
cmd.run()
254+
so_file = cmd.get_outputs()[0]
255+
self.assert_(os.path.exists(so_file))
256+
so_dir = os.path.dirname(so_file)
257+
self.assertEquals(so_dir, os.getcwd())
258+
259+
cmd.inplace = 0
260+
cmd.run()
261+
so_file = cmd.get_outputs()[0]
262+
self.assert_(os.path.exists(so_file))
263+
so_dir = os.path.dirname(so_file)
264+
self.assertEquals(so_dir, cmd.build_lib)
265+
229266
def test_suite():
230267
if not sysconfig.python_build:
231268
if test_support.verbose:

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ Core and Builtins
192192
Library
193193
-------
194194

195+
- Issue #5977: distutils build_ext.get_outputs was not taking into account the
196+
inplace option. Initial patch by kxroberto.
197+
195198
- Issue #5984: distutils.command.build_ext.check_extensions_list checks were broken
196199
for old-style extensions.
197200

0 commit comments

Comments
 (0)