Commit 95ca0fbb authored by Tarek Ziadé's avatar Tarek Ziadé

copied back the build_ext tests from 2.6

parent 17e4614f
...@@ -3,17 +3,12 @@ import os ...@@ -3,17 +3,12 @@ import os
import tempfile import tempfile
import shutil import shutil
from StringIO import StringIO from StringIO import StringIO
import warnings
from test.test_support import check_warnings
from test.test_support import captured_stdout
from distutils.core import Extension, Distribution from distutils.core import Extension, Distribution
from distutils.command.build_ext import build_ext from distutils.command.build_ext import build_ext
import sysconfig from distutils import sysconfig
from distutils.tests import support from distutils.tests import support
from distutils.extension import Extension from distutils.errors import DistutilsSetupError
from distutils.errors import (UnknownFileError, DistutilsSetupError,
CompileError)
import unittest import unittest
from test import test_support from test import test_support
...@@ -33,16 +28,10 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -33,16 +28,10 @@ class BuildExtTestCase(support.TempdirManager,
# Create a simple test environment # Create a simple test environment
# Note that we're making changes to sys.path # Note that we're making changes to sys.path
super(BuildExtTestCase, self).setUp() super(BuildExtTestCase, self).setUp()
self.tmp_dir = self.mkdtemp() self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_")
self.sys_path = sys.path, sys.path[:] self.sys_path = sys.path[:]
sys.path.append(self.tmp_dir) sys.path.append(self.tmp_dir)
shutil.copy(_get_source_filename(), self.tmp_dir) shutil.copy(_get_source_filename(), self.tmp_dir)
if sys.version > "2.6":
import site
self.old_user_base = site.USER_BASE
site.USER_BASE = self.mkdtemp()
from distutils.command import build_ext
build_ext.USER_BASE = site.USER_BASE
def test_build_ext(self): def test_build_ext(self):
global ALREADY_TESTED global ALREADY_TESTED
...@@ -76,27 +65,22 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -76,27 +65,22 @@ class BuildExtTestCase(support.TempdirManager,
import xx import xx
for attr in ('error', 'foo', 'new', 'roj'): for attr in ('error', 'foo', 'new', 'roj'):
self.assertTrue(hasattr(xx, attr)) self.assert_(hasattr(xx, attr))
self.assertEquals(xx.foo(2, 5), 7) self.assertEquals(xx.foo(2, 5), 7)
self.assertEquals(xx.foo(13,15), 28) self.assertEquals(xx.foo(13,15), 28)
self.assertEquals(xx.new().demo(), None) self.assertEquals(xx.new().demo(), None)
doc = 'This is a template module just for instruction.' doc = 'This is a template module just for instruction.'
self.assertEquals(xx.__doc__, doc) self.assertEquals(xx.__doc__, doc)
self.assertTrue(isinstance(xx.Null(), xx.Null)) self.assert_(isinstance(xx.Null(), xx.Null))
self.assertTrue(isinstance(xx.Str(), xx.Str)) self.assert_(isinstance(xx.Str(), xx.Str))
def tearDown(self): def tearDown(self):
# Get everything back to normal # Get everything back to normal
test_support.unload('xx') test_support.unload('xx')
sys.path = self.sys_path[0] sys.path = self.sys_path
sys.path[:] = self.sys_path[1] # XXX on Windows the test leaves a directory with xx module in TEMP
if sys.version > "2.6": shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin')
import site
site.USER_BASE = self.old_user_base
from distutils.command import build_ext
build_ext.USER_BASE = self.old_user_base
super(BuildExtTestCase, self).tearDown() super(BuildExtTestCase, self).tearDown()
def test_solaris_enable_shared(self): def test_solaris_enable_shared(self):
...@@ -105,83 +89,35 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -105,83 +89,35 @@ class BuildExtTestCase(support.TempdirManager,
old = sys.platform old = sys.platform
sys.platform = 'sunos' # fooling finalize_options sys.platform = 'sunos' # fooling finalize_options
from sysconfig import _CONFIG_VARS from distutils.sysconfig import _config_vars
old_var = _CONFIG_VARS.get('Py_ENABLE_SHARED') old_var = _config_vars.get('Py_ENABLE_SHARED')
_CONFIG_VARS['Py_ENABLE_SHARED'] = 1 _config_vars['Py_ENABLE_SHARED'] = 1
try: try:
cmd.ensure_finalized() cmd.ensure_finalized()
finally: finally:
sys.platform = old sys.platform = old
if old_var is None: if old_var is None:
del _CONFIG_VARS['Py_ENABLE_SHARED'] del _config_vars['Py_ENABLE_SHARED']
else: else:
_CONFIG_VARS['Py_ENABLE_SHARED'] = old_var _config_vars['Py_ENABLE_SHARED'] = old_var
# make sure we get some library dirs under solaris # make sure we get some library dirs under solaris
self.assertTrue(len(cmd.library_dirs) > 0) self.assert_(len(cmd.library_dirs) > 0)
def test_user_site(self):
# site.USER_SITE was introduced in 2.6
if sys.version < '2.6':
return
import site
dist = Distribution({'name': 'xx'})
cmd = build_ext(dist)
# making sure the user option is there
options = [name for name, short, lable in
cmd.user_options]
self.assertTrue('user' in options)
# setting a value
cmd.user = 1
# setting user based lib and include
lib = os.path.join(site.USER_BASE, 'lib')
incl = os.path.join(site.USER_BASE, 'include')
os.mkdir(lib)
os.mkdir(incl)
# let's run finalize
cmd.ensure_finalized()
# see if include_dirs and library_dirs
# were set
self.assertTrue(lib in cmd.library_dirs)
self.assertTrue(lib in cmd.rpath)
self.assertTrue(incl in cmd.include_dirs)
def test_optional_extension(self):
# this extension will fail, but let's ignore this failure
# with the optional argument.
modules = [Extension('foo', ['xxx'], optional=False)]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = build_ext(dist)
cmd.ensure_finalized()
self.assertRaises((UnknownFileError, CompileError),
cmd.run) # should raise an error
modules = [Extension('foo', ['xxx'], optional=True)]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = build_ext(dist)
cmd.ensure_finalized()
cmd.run() # should pass
def test_finalize_options(self): def test_finalize_options(self):
# Make sure Python's include directories (for Python.h, pyconfig.h, # Make sure Python's include directories (for Python.h, pyconfig.h,
# etc.) are in the include search path. # etc.) are in the include search path.
modules = [Extension('foo', ['xxx'], optional=False)] modules = [Extension('foo', ['xxx'])]
dist = Distribution({'name': 'xx', 'ext_modules': modules}) dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = build_ext(dist) cmd = build_ext(dist)
cmd.finalize_options() cmd.finalize_options()
py_include = sysconfig.get_path('include') from distutils import sysconfig
self.assertTrue(py_include in cmd.include_dirs) py_include = sysconfig.get_python_inc()
self.assert_(py_include in cmd.include_dirs)
plat_py_include = sysconfig.get_path('platinclude') plat_py_include = sysconfig.get_python_inc(plat_specific=1)
self.assertTrue(plat_py_include in cmd.include_dirs) self.assert_(plat_py_include in cmd.include_dirs)
# make sure cmd.libraries is turned into a list # make sure cmd.libraries is turned into a list
# if it's a string # if it's a string
...@@ -195,7 +131,7 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -195,7 +131,7 @@ class BuildExtTestCase(support.TempdirManager,
cmd = build_ext(dist) cmd = build_ext(dist)
cmd.library_dirs = 'my_lib_dir' cmd.library_dirs = 'my_lib_dir'
cmd.finalize_options() cmd.finalize_options()
self.assertTrue('my_lib_dir' in cmd.library_dirs) self.assert_('my_lib_dir' in cmd.library_dirs)
# make sure rpath is turned into a list # make sure rpath is turned into a list
# if it's a list of os.pathsep's paths # if it's a list of os.pathsep's paths
...@@ -260,13 +196,13 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -260,13 +196,13 @@ class BuildExtTestCase(support.TempdirManager,
'some': 'bar'})] 'some': 'bar'})]
cmd.check_extensions_list(exts) cmd.check_extensions_list(exts)
ext = exts[0] ext = exts[0]
self.assertTrue(isinstance(ext, Extension)) self.assert_(isinstance(ext, Extension))
# check_extensions_list adds in ext the values passed # check_extensions_list adds in ext the values passed
# when they are in ('include_dirs', 'library_dirs', 'libraries' # when they are in ('include_dirs', 'library_dirs', 'libraries'
# 'extra_objects', 'extra_compile_args', 'extra_link_args') # 'extra_objects', 'extra_compile_args', 'extra_link_args')
self.assertEquals(ext.libraries, 'foo') self.assertEquals(ext.libraries, 'foo')
self.assertTrue(not hasattr(ext, 'some')) self.assert_(not hasattr(ext, 'some'))
# 'macros' element of build info dict must be 1- or 2-tuple # 'macros' element of build info dict must be 1- or 2-tuple
exts = [('foo.bar', {'sources': [''], 'libraries': 'foo', exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
...@@ -279,7 +215,7 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -279,7 +215,7 @@ class BuildExtTestCase(support.TempdirManager,
self.assertEquals(exts[0].define_macros, [('1', '2')]) self.assertEquals(exts[0].define_macros, [('1', '2')])
def test_get_source_files(self): def test_get_source_files(self):
modules = [Extension('foo', ['xxx'], optional=False)] modules = [Extension('foo', ['xxx'])]
dist = Distribution({'name': 'xx', 'ext_modules': modules}) dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = build_ext(dist) cmd = build_ext(dist)
cmd.ensure_finalized() cmd.ensure_finalized()
...@@ -300,7 +236,7 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -300,7 +236,7 @@ class BuildExtTestCase(support.TempdirManager,
tmp_dir = self.mkdtemp() tmp_dir = self.mkdtemp()
c_file = os.path.join(tmp_dir, 'foo.c') c_file = os.path.join(tmp_dir, 'foo.c')
self.write_file(c_file, 'void initfoo(void) {};\n') self.write_file(c_file, 'void initfoo(void) {};\n')
ext = Extension('foo', [c_file], optional=False) ext = Extension('foo', [c_file])
dist = Distribution({'name': 'xx', dist = Distribution({'name': 'xx',
'ext_modules': [ext]}) 'ext_modules': [ext]})
cmd = build_ext(dist) cmd = build_ext(dist)
...@@ -324,16 +260,16 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -324,16 +260,16 @@ class BuildExtTestCase(support.TempdirManager,
so_file = cmd.get_outputs()[0] so_file = cmd.get_outputs()[0]
finally: finally:
os.chdir(old_wd) os.chdir(old_wd)
self.assertTrue(os.path.exists(so_file)) self.assert_(os.path.exists(so_file))
self.assertEquals(os.path.splitext(so_file)[-1], self.assertEquals(os.path.splitext(so_file)[-1],
sysconfig.get_config_var('SO')) sysconfig.get_config_var('SO'))
so_dir = os.path.dirname(so_file) so_dir = os.path.dirname(so_file)
self.assertEquals(so_dir, other_tmp_dir) self.assertEquals(so_dir, other_tmp_dir)
cmd.compiler = None
cmd.inplace = 0 cmd.inplace = 0
cmd.run() cmd.run()
so_file = cmd.get_outputs()[0] so_file = cmd.get_outputs()[0]
self.assertTrue(os.path.exists(so_file)) self.assert_(os.path.exists(so_file))
self.assertEquals(os.path.splitext(so_file)[-1], self.assertEquals(os.path.splitext(so_file)[-1],
sysconfig.get_config_var('SO')) sysconfig.get_config_var('SO'))
so_dir = os.path.dirname(so_file) so_dir = os.path.dirname(so_file)
...@@ -363,10 +299,6 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -363,10 +299,6 @@ class BuildExtTestCase(support.TempdirManager,
def test_ext_fullpath(self): def test_ext_fullpath(self):
ext = sysconfig.get_config_vars()['SO'] ext = sysconfig.get_config_vars()['SO']
# building lxml.etree inplace
#etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
#etree_ext = Extension('lxml.etree', [etree_c])
#dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
dist = Distribution() dist = Distribution()
cmd = build_ext(dist) cmd = build_ext(dist)
cmd.inplace = 1 cmd.inplace = 1
...@@ -399,25 +331,61 @@ class BuildExtTestCase(support.TempdirManager, ...@@ -399,25 +331,61 @@ class BuildExtTestCase(support.TempdirManager,
wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext) wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext)
self.assertEquals(wanted, path) self.assertEquals(wanted, path)
def test_compiler_deprecation_warning(self): def test_build_ext_inplace(self):
dist = Distribution() etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
etree_ext = Extension('lxml.etree', [etree_c])
dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
cmd = build_ext(dist) cmd = build_ext(dist)
cmd.ensure_finalized()
cmd.inplace = 1
cmd.distribution.package_dir = {'': 'src'}
cmd.distribution.packages = ['lxml', 'lxml.html']
curdir = os.getcwd()
ext = sysconfig.get_config_var("SO")
wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext)
path = cmd.get_ext_fullpath('lxml.etree')
self.assertEquals(wanted, path)
def test_setuptools_compat(self):
from setuptools_build_ext import build_ext as setuptools_build_ext
from setuptools_extension import Extension
class MyCompiler(object): etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
def do_something(self): etree_ext = Extension('lxml.etree', [etree_c])
pass dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
cmd = setuptools_build_ext(dist)
with check_warnings() as w: cmd.ensure_finalized()
warnings.simplefilter("always") cmd.inplace = 1
cmd.compiler = MyCompiler() cmd.distribution.package_dir = {'': 'src'}
self.assertEquals(len(w.warnings), 1) cmd.distribution.packages = ['lxml', 'lxml.html']
cmd.compile = 'unix' curdir = os.getcwd()
self.assertEquals(len(w.warnings), 1) ext = sysconfig.get_config_var("SO")
cmd.compiler = MyCompiler() wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext)
cmd.compiler.do_something() path = cmd.get_ext_fullpath('lxml.etree')
# two more warnings genereated by the get self.assertEquals(wanted, path)
# and the set
self.assertEquals(len(w.warnings), 3) def test_build_ext_path_with_os_sep(self):
dist = Distribution({'name': 'UpdateManager'})
cmd = build_ext(dist)
cmd.ensure_finalized()
ext = sysconfig.get_config_var("SO")
ext_name = os.path.join('UpdateManager', 'fdsend')
ext_path = cmd.get_ext_fullpath(ext_name)
wanted = os.path.join(cmd.build_lib, 'UpdateManager', 'fdsend' + ext)
self.assertEquals(ext_path, wanted)
def test_build_ext_path_cross_platform(self):
if sys.platform != 'win32':
return
dist = Distribution({'name': 'UpdateManager'})
cmd = build_ext(dist)
cmd.ensure_finalized()
ext = sysconfig.get_config_var("SO")
# this needs to work even under win32
ext_name = 'UpdateManager/fdsend'
ext_path = cmd.get_ext_fullpath(ext_name)
wanted = os.path.join(cmd.build_lib, 'UpdateManager', 'fdsend' + ext)
self.assertEquals(ext_path, wanted)
def test_suite(): def test_suite():
src = _get_source_filename() src = _get_source_filename()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment