Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
distutils.tests.test_build_ext.BuildExtTestCase Class Reference

List of all members.

Public Member Functions

def setUp
def test_build_ext
def tearDown
def test_solaris_enable_shared
def test_user_site
def test_optional_extension
def test_finalize_options
def test_check_extensions_list
def test_get_source_files
def test_compiler_option
def test_get_outputs
def test_ext_fullpath
def test_deployment_target_default
def test_deployment_target_too_low
def test_deployment_target_higher_ok

Public Attributes

 tmp_dir
 sys_path
 old_user_base

Private Member Functions

def _try_compile_deployment_target

Detailed Description

Definition at line 26 of file test_build_ext.py.


Member Function Documentation

def distutils.tests.test_build_ext.BuildExtTestCase._try_compile_deployment_target (   self,
  operator,
  target 
) [private]

Definition at line 421 of file test_build_ext.py.

00421 
00422     def _try_compile_deployment_target(self, operator, target):
00423         orig_environ = os.environ
00424         os.environ = orig_environ.copy()
00425         self.addCleanup(setattr, os, 'environ', orig_environ)
00426 
00427         if target is None:
00428             if os.environ.get('MACOSX_DEPLOYMENT_TARGET'):
00429                 del os.environ['MACOSX_DEPLOYMENT_TARGET']
00430         else:
00431             os.environ['MACOSX_DEPLOYMENT_TARGET'] = target
00432 
00433         deptarget_c = os.path.join(self.tmp_dir, 'deptargetmodule.c')
00434 
00435         with open(deptarget_c, 'w') as fp:
00436             fp.write(textwrap.dedent('''\
00437                 #include <AvailabilityMacros.h>
00438 
00439                 int dummy;
00440 
00441                 #if TARGET %s MAC_OS_X_VERSION_MIN_REQUIRED
00442                 #else
00443                 #error "Unexpected target"
00444                 #endif
00445 
00446             ''' % operator))
00447 
00448         # get the deployment target that the interpreter was built with
00449         target = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
00450         target = tuple(map(int, target.split('.')))
00451         target = '%02d%01d0' % target
00452         deptarget_ext = Extension(
00453             'deptarget',
00454             [deptarget_c],
00455             extra_compile_args=['-DTARGET=%s'%(target,)],
00456         )
00457         dist = Distribution({
00458             'name': 'deptarget',
00459             'ext_modules': [deptarget_ext]
00460         })
00461         dist.package_dir = self.tmp_dir
00462         cmd = build_ext(dist)
00463         cmd.build_lib = self.tmp_dir
00464         cmd.build_temp = self.tmp_dir
00465 
00466         try:
00467             old_stdout = sys.stdout
00468             if not support.verbose:
00469                 # silence compiler output
00470                 sys.stdout = StringIO()
00471             try:
00472                 cmd.ensure_finalized()
00473                 cmd.run()
00474             finally:
00475                 sys.stdout = old_stdout
00476 
00477         except CompileError:
00478             self.fail("Wrong deployment target during compilation")
00479 

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 27 of file test_build_ext.py.

00027 
00028     def setUp(self):
00029         # Create a simple test environment
00030         # Note that we're making changes to sys.path
00031         super(BuildExtTestCase, self).setUp()
00032         self.tmp_dir = self.mkdtemp()
00033         self.sys_path = sys.path, sys.path[:]
00034         sys.path.append(self.tmp_dir)
00035         if sys.version > "2.6":
00036             import site
00037             self.old_user_base = site.USER_BASE
00038             site.USER_BASE = self.mkdtemp()
00039             from distutils.command import build_ext
00040             build_ext.USER_BASE = site.USER_BASE

Here is the caller graph for this function:

Definition at line 81 of file test_build_ext.py.

00081 
00082     def tearDown(self):
00083         # Get everything back to normal
00084         support.unload('xx')
00085         sys.path = self.sys_path[0]
00086         sys.path[:] = self.sys_path[1]
00087         if sys.version > "2.6":
00088             import site
00089             site.USER_BASE = self.old_user_base
00090             from distutils.command import build_ext
00091             build_ext.USER_BASE = self.old_user_base
00092         super(BuildExtTestCase, self).tearDown()

Here is the caller graph for this function:

Definition at line 41 of file test_build_ext.py.

00041 
00042     def test_build_ext(self):
00043         global ALREADY_TESTED
00044         copy_xxmodule_c(self.tmp_dir)
00045         xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
00046         xx_ext = Extension('xx', [xx_c])
00047         dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
00048         dist.package_dir = self.tmp_dir
00049         cmd = build_ext(dist)
00050         fixup_build_ext(cmd)
00051         cmd.build_lib = self.tmp_dir
00052         cmd.build_temp = self.tmp_dir
00053 
00054         old_stdout = sys.stdout
00055         if not support.verbose:
00056             # silence compiler output
00057             sys.stdout = StringIO()
00058         try:
00059             cmd.ensure_finalized()
00060             cmd.run()
00061         finally:
00062             sys.stdout = old_stdout
00063 
00064         if ALREADY_TESTED:
00065             return
00066         else:
00067             ALREADY_TESTED = True
00068 
00069         import xx
00070 
00071         for attr in ('error', 'foo', 'new', 'roj'):
00072             self.assertTrue(hasattr(xx, attr))
00073 
00074         self.assertEqual(xx.foo(2, 5), 7)
00075         self.assertEqual(xx.foo(13,15), 28)
00076         self.assertEqual(xx.new().demo(), None)
00077         doc = 'This is a template module just for instruction.'
00078         self.assertEqual(xx.__doc__, doc)
00079         self.assertTrue(isinstance(xx.Null(), xx.Null))
00080         self.assertTrue(isinstance(xx.Str(), xx.Str))

Here is the call graph for this function:

Definition at line 226 of file test_build_ext.py.

00226 
00227     def test_check_extensions_list(self):
00228         dist = Distribution()
00229         cmd = build_ext(dist)
00230         cmd.finalize_options()
00231 
00232         #'extensions' option must be a list of Extension instances
00233         self.assertRaises(DistutilsSetupError,
00234                           cmd.check_extensions_list, 'foo')
00235 
00236         # each element of 'ext_modules' option must be an
00237         # Extension instance or 2-tuple
00238         exts = [('bar', 'foo', 'bar'), 'foo']
00239         self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
00240 
00241         # first element of each tuple in 'ext_modules'
00242         # must be the extension name (a string) and match
00243         # a python dotted-separated name
00244         exts = [('foo-bar', '')]
00245         self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
00246 
00247         # second element of each tuple in 'ext_modules'
00248         # must be a ary (build info)
00249         exts = [('foo.bar', '')]
00250         self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
00251 
00252         # ok this one should pass
00253         exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
00254                              'some': 'bar'})]
00255         cmd.check_extensions_list(exts)
00256         ext = exts[0]
00257         self.assertTrue(isinstance(ext, Extension))
00258 
00259         # check_extensions_list adds in ext the values passed
00260         # when they are in ('include_dirs', 'library_dirs', 'libraries'
00261         # 'extra_objects', 'extra_compile_args', 'extra_link_args')
00262         self.assertEqual(ext.libraries, 'foo')
00263         self.assertTrue(not hasattr(ext, 'some'))
00264 
00265         # 'macros' element of build info dict must be 1- or 2-tuple
00266         exts = [('foo.bar', {'sources': [''], 'libraries': 'foo',
00267                 'some': 'bar', 'macros': [('1', '2', '3'), 'foo']})]
00268         self.assertRaises(DistutilsSetupError, cmd.check_extensions_list, exts)
00269 
00270         exts[0][1]['macros'] = [('1', '2'), ('3',)]
00271         cmd.check_extensions_list(exts)
00272         self.assertEqual(exts[0].undef_macros, ['3'])
00273         self.assertEqual(exts[0].define_macros, [('1', '2')])

Here is the call graph for this function:

Definition at line 281 of file test_build_ext.py.

00281 
00282     def test_compiler_option(self):
00283         # cmd.compiler is an option and
00284         # should not be overriden by a compiler instance
00285         # when the command is run
00286         dist = Distribution()
00287         cmd = build_ext(dist)
00288         cmd.compiler = 'unix'
00289         cmd.ensure_finalized()
00290         cmd.run()
00291         self.assertEqual(cmd.compiler, 'unix')

Here is the call graph for this function:

Definition at line 395 of file test_build_ext.py.

00395 
00396     def test_deployment_target_default(self):
00397         # Issue 9516: Test that, in the absence of the environment variable,
00398         # an extension module is compiled with the same deployment target as
00399         #  the interpreter.
00400         self._try_compile_deployment_target('==', None)

Here is the call graph for this function:

Definition at line 409 of file test_build_ext.py.

00409 
00410     def test_deployment_target_higher_ok(self):
00411         # Issue 9516: Test that an extension module can be compiled with a
00412         # deployment target higher than that of the interpreter: the ext
00413         # module may depend on some newer OS feature.
00414         deptarget = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET')
00415         if deptarget:
00416             # increment the minor version number (i.e. 10.6 -> 10.7)
00417             deptarget = [int(x) for x in deptarget.split('.')]
00418             deptarget[-1] += 1
00419             deptarget = '.'.join(str(i) for i in deptarget)
00420             self._try_compile_deployment_target('<', deptarget)

Here is the call graph for this function:

Definition at line 402 of file test_build_ext.py.

00402 
00403     def test_deployment_target_too_low(self):
00404         # Issue 9516: Test that an extension module is not allowed to be
00405         # compiled with a deployment target less than that of the interpreter.
00406         self.assertRaises(DistutilsPlatformError,
00407             self._try_compile_deployment_target, '>', '10.1')

Here is the call graph for this function:

Definition at line 355 of file test_build_ext.py.

00355 
00356     def test_ext_fullpath(self):
00357         ext = sysconfig.get_config_vars()['SO']
00358         # building lxml.etree inplace
00359         #etree_c = os.path.join(self.tmp_dir, 'lxml.etree.c')
00360         #etree_ext = Extension('lxml.etree', [etree_c])
00361         #dist = Distribution({'name': 'lxml', 'ext_modules': [etree_ext]})
00362         dist = Distribution()
00363         cmd = build_ext(dist)
00364         cmd.inplace = 1
00365         cmd.distribution.package_dir = {'': 'src'}
00366         cmd.distribution.packages = ['lxml', 'lxml.html']
00367         curdir = os.getcwd()
00368         wanted = os.path.join(curdir, 'src', 'lxml', 'etree' + ext)
00369         path = cmd.get_ext_fullpath('lxml.etree')
00370         self.assertEqual(wanted, path)
00371 
00372         # building lxml.etree not inplace
00373         cmd.inplace = 0
00374         cmd.build_lib = os.path.join(curdir, 'tmpdir')
00375         wanted = os.path.join(curdir, 'tmpdir', 'lxml', 'etree' + ext)
00376         path = cmd.get_ext_fullpath('lxml.etree')
00377         self.assertEqual(wanted, path)
00378 
00379         # building twisted.runner.portmap not inplace
00380         build_py = cmd.get_finalized_command('build_py')
00381         build_py.package_dir = {}
00382         cmd.distribution.packages = ['twisted', 'twisted.runner.portmap']
00383         path = cmd.get_ext_fullpath('twisted.runner.portmap')
00384         wanted = os.path.join(curdir, 'tmpdir', 'twisted', 'runner',
00385                               'portmap' + ext)
00386         self.assertEqual(wanted, path)
00387 
00388         # building twisted.runner.portmap inplace
00389         cmd.inplace = 1
00390         path = cmd.get_ext_fullpath('twisted.runner.portmap')
00391         wanted = os.path.join(curdir, 'twisted', 'runner', 'portmap' + ext)
00392         self.assertEqual(wanted, path)
00393 

Here is the call graph for this function:

Definition at line 163 of file test_build_ext.py.

00163 
00164     def test_finalize_options(self):
00165         # Make sure Python's include directories (for Python.h, pyconfig.h,
00166         # etc.) are in the include search path.
00167         modules = [Extension('foo', ['xxx'], optional=False)]
00168         dist = Distribution({'name': 'xx', 'ext_modules': modules})
00169         cmd = build_ext(dist)
00170         cmd.finalize_options()
00171 
00172         from distutils import sysconfig
00173         py_include = sysconfig.get_python_inc()
00174         self.assertTrue(py_include in cmd.include_dirs)
00175 
00176         plat_py_include = sysconfig.get_python_inc(plat_specific=1)
00177         self.assertTrue(plat_py_include in cmd.include_dirs)
00178 
00179         # make sure cmd.libraries is turned into a list
00180         # if it's a string
00181         cmd = build_ext(dist)
00182         cmd.libraries = 'my_lib'
00183         cmd.finalize_options()
00184         self.assertEqual(cmd.libraries, ['my_lib'])
00185 
00186         # make sure cmd.library_dirs is turned into a list
00187         # if it's a string
00188         cmd = build_ext(dist)
00189         cmd.library_dirs = 'my_lib_dir'
00190         cmd.finalize_options()
00191         self.assertTrue('my_lib_dir' in cmd.library_dirs)
00192 
00193         # make sure rpath is turned into a list
00194         # if it's a list of os.pathsep's paths
00195         cmd = build_ext(dist)
00196         cmd.rpath = os.pathsep.join(['one', 'two'])
00197         cmd.finalize_options()
00198         self.assertEqual(cmd.rpath, ['one', 'two'])
00199 
00200         # XXX more tests to perform for win32
00201 
00202         # make sure define is turned into 2-tuples
00203         # strings if they are ','-separated strings
00204         cmd = build_ext(dist)
00205         cmd.define = 'one,two'
00206         cmd.finalize_options()
00207         self.assertEqual(cmd.define, [('one', '1'), ('two', '1')])
00208 
00209         # make sure undef is turned into a list of
00210         # strings if they are ','-separated strings
00211         cmd = build_ext(dist)
00212         cmd.undef = 'one,two'
00213         cmd.finalize_options()
00214         self.assertEqual(cmd.undef, ['one', 'two'])
00215 
00216         # make sure swig_opts is turned into a list
00217         cmd = build_ext(dist)
00218         cmd.swig_opts = None
00219         cmd.finalize_options()
00220         self.assertEqual(cmd.swig_opts, [])
00221 
00222         cmd = build_ext(dist)
00223         cmd.swig_opts = '1 2'
00224         cmd.finalize_options()
00225         self.assertEqual(cmd.swig_opts, ['1', '2'])

Here is the call graph for this function:

Definition at line 292 of file test_build_ext.py.

00292 
00293     def test_get_outputs(self):
00294         tmp_dir = self.mkdtemp()
00295         c_file = os.path.join(tmp_dir, 'foo.c')
00296         self.write_file(c_file, 'void PyInit_foo(void) {}\n')
00297         ext = Extension('foo', [c_file], optional=False)
00298         dist = Distribution({'name': 'xx',
00299                              'ext_modules': [ext]})
00300         cmd = build_ext(dist)
00301         fixup_build_ext(cmd)
00302         cmd.ensure_finalized()
00303         self.assertEqual(len(cmd.get_outputs()), 1)
00304 
00305         cmd.build_lib = os.path.join(self.tmp_dir, 'build')
00306         cmd.build_temp = os.path.join(self.tmp_dir, 'tempt')
00307 
00308         # issue #5977 : distutils build_ext.get_outputs
00309         # returns wrong result with --inplace
00310         other_tmp_dir = os.path.realpath(self.mkdtemp())
00311         old_wd = os.getcwd()
00312         os.chdir(other_tmp_dir)
00313         try:
00314             cmd.inplace = 1
00315             cmd.run()
00316             so_file = cmd.get_outputs()[0]
00317         finally:
00318             os.chdir(old_wd)
00319         self.assertTrue(os.path.exists(so_file))
00320         so_ext = sysconfig.get_config_var('SO')
00321         self.assertTrue(so_file.endswith(so_ext))
00322         so_dir = os.path.dirname(so_file)
00323         self.assertEqual(so_dir, other_tmp_dir)
00324 
00325         cmd.inplace = 0
00326         cmd.compiler = None
00327         cmd.run()
00328         so_file = cmd.get_outputs()[0]
00329         self.assertTrue(os.path.exists(so_file))
00330         self.assertTrue(so_file.endswith(so_ext))
00331         so_dir = os.path.dirname(so_file)
00332         self.assertEqual(so_dir, cmd.build_lib)
00333 
00334         # inplace = 0, cmd.package = 'bar'
00335         build_py = cmd.get_finalized_command('build_py')
00336         build_py.package_dir = {'': 'bar'}
00337         path = cmd.get_ext_fullpath('foo')
00338         # checking that the last directory is the build_dir
00339         path = os.path.split(path)[0]
00340         self.assertEqual(path, cmd.build_lib)
00341 
00342         # inplace = 1, cmd.package = 'bar'
00343         cmd.inplace = 1
00344         other_tmp_dir = os.path.realpath(self.mkdtemp())
00345         old_wd = os.getcwd()
00346         os.chdir(other_tmp_dir)
00347         try:
00348             path = cmd.get_ext_fullpath('foo')
00349         finally:
00350             os.chdir(old_wd)
00351         # checking that the last directory is bar
00352         path = os.path.split(path)[0]
00353         lastdir = os.path.split(path)[-1]
00354         self.assertEqual(lastdir, 'bar')

Here is the call graph for this function:

Definition at line 274 of file test_build_ext.py.

00274 
00275     def test_get_source_files(self):
00276         modules = [Extension('foo', ['xxx'], optional=False)]
00277         dist = Distribution({'name': 'xx', 'ext_modules': modules})
00278         cmd = build_ext(dist)
00279         cmd.ensure_finalized()
00280         self.assertEqual(cmd.get_source_files(), ['xxx'])

Here is the call graph for this function:

Definition at line 146 of file test_build_ext.py.

00146 
00147     def test_optional_extension(self):
00148 
00149         # this extension will fail, but let's ignore this failure
00150         # with the optional argument.
00151         modules = [Extension('foo', ['xxx'], optional=False)]
00152         dist = Distribution({'name': 'xx', 'ext_modules': modules})
00153         cmd = build_ext(dist)
00154         cmd.ensure_finalized()
00155         self.assertRaises((UnknownFileError, CompileError),
00156                           cmd.run)  # should raise an error
00157 
00158         modules = [Extension('foo', ['xxx'], optional=True)]
00159         dist = Distribution({'name': 'xx', 'ext_modules': modules})
00160         cmd = build_ext(dist)
00161         cmd.ensure_finalized()
00162         cmd.run()  # should pass

Here is the call graph for this function:

Definition at line 93 of file test_build_ext.py.

00093 
00094     def test_solaris_enable_shared(self):
00095         dist = Distribution({'name': 'xx'})
00096         cmd = build_ext(dist)
00097         old = sys.platform
00098 
00099         sys.platform = 'sunos' # fooling finalize_options
00100         from distutils.sysconfig import  _config_vars
00101         old_var = _config_vars.get('Py_ENABLE_SHARED')
00102         _config_vars['Py_ENABLE_SHARED'] = 1
00103         try:
00104             cmd.ensure_finalized()
00105         finally:
00106             sys.platform = old
00107             if old_var is None:
00108                 del _config_vars['Py_ENABLE_SHARED']
00109             else:
00110                 _config_vars['Py_ENABLE_SHARED'] = old_var
00111 
00112         # make sure we get some library dirs under solaris
00113         self.assertTrue(len(cmd.library_dirs) > 0)

Here is the call graph for this function:

Definition at line 114 of file test_build_ext.py.

00114 
00115     def test_user_site(self):
00116         # site.USER_SITE was introduced in 2.6
00117         if sys.version < '2.6':
00118             return
00119 
00120         import site
00121         dist = Distribution({'name': 'xx'})
00122         cmd = build_ext(dist)
00123 
00124         # making sure the user option is there
00125         options = [name for name, short, lable in
00126                    cmd.user_options]
00127         self.assertTrue('user' in options)
00128 
00129         # setting a value
00130         cmd.user = 1
00131 
00132         # setting user based lib and include
00133         lib = os.path.join(site.USER_BASE, 'lib')
00134         incl = os.path.join(site.USER_BASE, 'include')
00135         os.mkdir(lib)
00136         os.mkdir(incl)
00137 
00138         # let's run finalize
00139         cmd.ensure_finalized()
00140 
00141         # see if include_dirs and library_dirs
00142         # were set
00143         self.assertIn(lib, cmd.library_dirs)
00144         self.assertIn(lib, cmd.rpath)
00145         self.assertIn(incl, cmd.include_dirs)

Here is the call graph for this function:


Member Data Documentation

Definition at line 36 of file test_build_ext.py.

Definition at line 32 of file test_build_ext.py.

Definition at line 31 of file test_build_ext.py.


The documentation for this class was generated from the following file: