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
import tempfile
import shutil
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.command.build_ext import build_ext
import sysconfig
from distutils import sysconfig
from distutils.tests import support
from distutils.extension import Extension
from distutils.errors import (UnknownFileError, DistutilsSetupError,
CompileError)
from distutils.errors import DistutilsSetupError
import unittest
from test import test_support
......@@ -33,16 +28,10 @@ class BuildExtTestCase(support.TempdirManager,
# Create a simple test environment
# Note that we're making changes to sys.path
super(BuildExtTestCase, self).setUp()
self.tmp_dir = self.mkdtemp()
self.sys_path = sys.path, sys.path[:]
self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_")
self.sys_path = sys.path[:]
sys.path.append(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):
global ALREADY_TESTED
......@@ -76,27 +65,22 @@ class BuildExtTestCase(support.TempdirManager,
import xx
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(13,15), 28)
self.assertEquals(xx.new().demo(), None)
doc = 'This is a template module just for instruction.'
self.assertEquals(xx.__doc__, doc)
self.assertTrue(isinstance(xx.Null(), xx.Null))
self.assertTrue(isinstance(xx.Str(), xx.Str))
self.assert_(isinstance(xx.Null(), xx.Null))
self.assert_(isinstance(xx.Str(), xx.Str))
def tearDown(self):
# Get everything back to normal
test_support.unload('xx')
sys.path = self.sys_path[0]
sys.path[:] = self.sys_path[1]
if sys.version > "2.6":
import site
site.USER_BASE = self.old_user_base
from distutils.command import build_ext
build_ext.USER_BASE = self.old_user_base
sys.path = self.sys_path
# XXX on Windows the test leaves a directory with xx module in TEMP
shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin')
super(BuildExtTestCase, self).tearDown()
def test_solaris_enable_shared(self):
......@@ -105,83 +89,35 @@ class BuildExtTestCase(support.TempdirManager,
old = sys.platform
sys.platform = 'sunos' # fooling finalize_options
from sysconfig import _CONFIG_VARS
old_var = _CONFIG_VARS.get('Py_ENABLE_SHARED')
_CONFIG_VARS['Py_ENABLE_SHARED'] = 1
from distutils.sysconfig import _config_vars
old_var = _config_vars.get('Py_ENABLE_SHARED')
_config_vars['Py_ENABLE_SHARED'] = 1
try:
cmd.ensure_finalized()
finally:
sys.platform = old
if old_var is None:
del _CONFIG_VARS['Py_ENABLE_SHARED']
del _config_vars['Py_ENABLE_SHARED']
else:
_CONFIG_VARS['Py_ENABLE_SHARED'] = old_var
_config_vars['Py_ENABLE_SHARED'] = old_var
# make sure we get some library dirs under solaris
self.assertTrue(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
self.assert_(len(cmd.library_dirs) > 0)
def test_finalize_options(self):
# Make sure Python's include directories (for Python.h, pyconfig.h,
# etc.) are in the include search path.
modules = [Extension('foo', ['xxx'], optional=False)]
modules = [Extension('foo', ['xxx'])]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = build_ext(dist)
cmd.finalize_options()
py_include = sysconfig.get_path('include')
self.assertTrue(py_include in cmd.include_dirs)
from distutils import sysconfig
py_include = sysconfig.get_python_inc()
self.assert_(py_include in cmd.include_dirs)
plat_py_include = sysconfig.get_path('platinclude')
self.assertTrue(plat_py_include in cmd.include_dirs)
plat_py_include = sysconfig.get_python_inc(plat_specific=1)
self.assert_(plat_py_include in cmd.include_dirs)
# make sure cmd.libraries is turned into a list
# if it's a string
......@@ -195,7 +131,7 @@ class BuildExtTestCase(support.TempdirManager,
cmd = build_ext(dist)
cmd.library_dirs = 'my_lib_dir'
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
# if it's a list of os.pathsep's paths
......@@ -260,13 +196,13 @@ class BuildExtTestCase(support.TempdirManager,
'some': 'bar'})]
cmd.check_extensions_list(exts)
ext = exts[0]
self.assertTrue(isinstance(ext, Extension))
self.assert_(isinstance(ext, Extension))
# check_extensions_list adds in ext the values passed
# when they are in ('include_dirs', 'library_dirs', 'libraries'
# 'extra_objects', 'extra_compile_args', 'extra_link_args')
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
exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
......@@ -279,7 +215,7 @@ class BuildExtTestCase(support.TempdirManager,
self.assertEquals(exts[0].define_macros, [('1', '2')])
def test_get_source_files(self):
modules = [Extension('foo', ['xxx'], optional=False)]
modules = [Extension('foo', ['xxx'])]
dist = Distribution({'name': 'xx', 'ext_modules': modules})
cmd = build_ext(dist)
cmd.ensure_finalized()
......@@ -300,7 +236,7 @@ class BuildExtTestCase(support.TempdirManager,
tmp_dir = self.mkdtemp()
c_file = os.path.join(tmp_dir, 'foo.c')
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',
'ext_modules': [ext]})
cmd = build_ext(dist)
......@@ -324,16 +260,16 @@ class BuildExtTestCase(support.TempdirManager,
so_file = cmd.get_outputs()[0]
finally:
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],
sysconfig.get_config_var('SO'))
so_dir = os.path.dirname(so_file)
self.assertEquals(so_dir, other_tmp_dir)
cmd.compiler = None
cmd.inplace = 0
cmd.run()
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],
sysconfig.get_config_var('SO'))
so_dir = os.path.dirname(so_file)
......@@ -363,10 +299,6 @@ class BuildExtTestCase(support.TempdirManager,
def test_ext_fullpath(self):
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()
cmd = build_ext(dist)
cmd.inplace = 1
......@@ -399,25 +331,61 @@ class BuildExtTestCase(support.TempdirManager,
wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext)
self.assertEquals(wanted, path)
def test_compiler_deprecation_warning(self):
dist = Distribution()
def test_build_ext_inplace(self):
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.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):
def do_something(self):
pass
with check_warnings() as w:
warnings.simplefilter("always")
cmd.compiler = MyCompiler()
self.assertEquals(len(w.warnings), 1)
cmd.compile = 'unix'
self.assertEquals(len(w.warnings), 1)
cmd.compiler = MyCompiler()
cmd.compiler.do_something()
# two more warnings genereated by the get
# and the set
self.assertEquals(len(w.warnings), 3)
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 = setuptools_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_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():
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