blob: 4c5723255ad39d4d4fb0e9b6d93d58882675504c [file] [log] [blame]
Georg Brandlb533e262008-05-25 18:19:30 +00001import sys
2import os
3import tempfile
4import shutil
5from io import StringIO
6
7from distutils.core import Extension, Distribution
8from distutils.command.build_ext import build_ext
9from distutils import sysconfig
10
11import unittest
12from test import support
13
Christian Heimes3e7e0692008-11-25 21:21:32 +000014# http://bugs.python.org/issue4373
15# Don't load the xx module more than once.
16ALREADY_TESTED = False
17
Georg Brandlb533e262008-05-25 18:19:30 +000018class BuildExtTestCase(unittest.TestCase):
19 def setUp(self):
20 # Create a simple test environment
21 # Note that we're making changes to sys.path
22 self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_")
23 self.sys_path = sys.path[:]
24 sys.path.append(self.tmp_dir)
25
26 xx_c = os.path.join(sysconfig.project_base, 'Modules', 'xxmodule.c')
27 shutil.copy(xx_c, self.tmp_dir)
28
29 def test_build_ext(self):
Christian Heimes3e7e0692008-11-25 21:21:32 +000030 global ALREADY_TESTED
Georg Brandlb533e262008-05-25 18:19:30 +000031 xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
32 xx_ext = Extension('xx', [xx_c])
33 dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
34 dist.package_dir = self.tmp_dir
35 cmd = build_ext(dist)
Thomas Heller84b7f0c2008-05-26 11:51:44 +000036 if os.name == "nt":
37 # On Windows, we must build a debug version iff running
38 # a debug build of Python
39 cmd.debug = sys.executable.endswith("_d.exe")
Georg Brandlb533e262008-05-25 18:19:30 +000040 cmd.build_lib = self.tmp_dir
41 cmd.build_temp = self.tmp_dir
42
43 old_stdout = sys.stdout
44 if not support.verbose:
45 # silence compiler output
46 sys.stdout = StringIO()
47 try:
48 cmd.ensure_finalized()
49 cmd.run()
50 finally:
51 sys.stdout = old_stdout
52
Christian Heimes3e7e0692008-11-25 21:21:32 +000053 if ALREADY_TESTED:
54 return
55 else:
56 ALREADY_TESTED = True
57
Georg Brandlb533e262008-05-25 18:19:30 +000058 import xx
59
60 for attr in ('error', 'foo', 'new', 'roj'):
61 self.assert_(hasattr(xx, attr))
62
63 self.assertEquals(xx.foo(2, 5), 7)
64 self.assertEquals(xx.foo(13,15), 28)
65 self.assertEquals(xx.new().demo(), None)
66 doc = 'This is a template module just for instruction.'
67 self.assertEquals(xx.__doc__, doc)
68 self.assert_(isinstance(xx.Null(), xx.Null))
69 self.assert_(isinstance(xx.Str(), xx.Str))
70
71 def tearDown(self):
72 # Get everything back to normal
73 support.unload('xx')
74 sys.path = self.sys_path
Hirokazu Yamamoto956ffd72008-09-21 20:52:42 +000075 # XXX on Windows the test leaves a directory with xx module in TEMP
76 shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin')
Georg Brandlb533e262008-05-25 18:19:30 +000077
Tarek Ziadé5874ef12009-02-05 22:56:14 +000078 def test_solaris_enable_shared(self):
79 dist = Distribution({'name': 'xx'})
80 cmd = build_ext(dist)
81 old = sys.platform
82
83 sys.platform = 'sunos' # fooling finalize_options
84 from distutils.sysconfig import _config_vars
85 old_var = _config_vars.get('Py_ENABLE_SHARED')
86 _config_vars['Py_ENABLE_SHARED'] = 1
87 try:
88 cmd.ensure_finalized()
89 finally:
90 sys.platform = old
91 if old_var is None:
92 del _config_vars['Py_ENABLE_SHARED']
93 else:
94 _config_vars['Py_ENABLE_SHARED'] = old_var
95
96 # make sur we get some lobrary dirs under solaris
97 self.assert_(len(cmd.library_dirs) > 0)
98
Georg Brandlb533e262008-05-25 18:19:30 +000099def test_suite():
100 if not sysconfig.python_build:
101 if support.verbose:
102 print('test_build_ext: The test must be run in a python build dir')
103 return unittest.TestSuite()
104 else: return unittest.makeSuite(BuildExtTestCase)
105
106if __name__ == '__main__':
107 support.run_unittest(test_suite())