~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/distutils/tests/test_build_ext.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
import os
 
3
import tempfile
 
4
import shutil
 
5
from io import StringIO
 
6
 
 
7
from distutils.core import Extension, Distribution
 
8
from distutils.command.build_ext import build_ext
 
9
from distutils import sysconfig
 
10
from distutils.tests.support import TempdirManager
 
11
 
 
12
import unittest
 
13
from test import support
 
14
 
 
15
# http://bugs.python.org/issue4373
 
16
# Don't load the xx module more than once.
 
17
ALREADY_TESTED = False
 
18
 
 
19
def _get_source_filename():
 
20
    srcdir = sysconfig.get_config_var('srcdir')
 
21
    return os.path.join(srcdir, 'Modules', 'xxmodule.c')
 
22
 
 
23
class BuildExtTestCase(TempdirManager, unittest.TestCase):
 
24
    def setUp(self):
 
25
        # Create a simple test environment
 
26
        # Note that we're making changes to sys.path
 
27
        super(BuildExtTestCase, self).setUp()
 
28
        self.tmp_dir = self.mkdtemp()
 
29
        self.sys_path = sys.path[:]
 
30
        sys.path.append(self.tmp_dir)
 
31
        shutil.copy(_get_source_filename(), self.tmp_dir)
 
32
        if sys.version > "2.6":
 
33
            import site
 
34
            self.old_user_base = site.USER_BASE
 
35
            site.USER_BASE = self.mkdtemp()
 
36
            from distutils.command import build_ext
 
37
            build_ext.USER_BASE = site.USER_BASE
 
38
 
 
39
    def test_build_ext(self):
 
40
        global ALREADY_TESTED
 
41
        xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
 
42
        xx_ext = Extension('xx', [xx_c])
 
43
        dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
 
44
        dist.package_dir = self.tmp_dir
 
45
        cmd = build_ext(dist)
 
46
        if os.name == "nt":
 
47
            # On Windows, we must build a debug version iff running
 
48
            # a debug build of Python
 
49
            cmd.debug = sys.executable.endswith("_d.exe")
 
50
        cmd.build_lib = self.tmp_dir
 
51
        cmd.build_temp = self.tmp_dir
 
52
 
 
53
        old_stdout = sys.stdout
 
54
        if not support.verbose:
 
55
            # silence compiler output
 
56
            sys.stdout = StringIO()
 
57
        try:
 
58
            cmd.ensure_finalized()
 
59
            cmd.run()
 
60
        finally:
 
61
            sys.stdout = old_stdout
 
62
 
 
63
        if ALREADY_TESTED:
 
64
            return
 
65
        else:
 
66
            ALREADY_TESTED = True
 
67
 
 
68
        import xx
 
69
 
 
70
        for attr in ('error', 'foo', 'new', 'roj'):
 
71
            self.assert_(hasattr(xx, attr))
 
72
 
 
73
        self.assertEquals(xx.foo(2, 5), 7)
 
74
        self.assertEquals(xx.foo(13,15), 28)
 
75
        self.assertEquals(xx.new().demo(), None)
 
76
        doc = 'This is a template module just for instruction.'
 
77
        self.assertEquals(xx.__doc__, doc)
 
78
        self.assert_(isinstance(xx.Null(), xx.Null))
 
79
        self.assert_(isinstance(xx.Str(), xx.Str))
 
80
 
 
81
    def tearDown(self):
 
82
        # Get everything back to normal
 
83
        support.unload('xx')
 
84
        sys.path = self.sys_path
 
85
        if sys.version > "2.6":
 
86
            import site
 
87
            site.USER_BASE = self.old_user_base
 
88
            from distutils.command import build_ext
 
89
            build_ext.USER_BASE = self.old_user_base
 
90
        super(BuildExtTestCase, self).tearDown()
 
91
 
 
92
    def test_solaris_enable_shared(self):
 
93
        dist = Distribution({'name': 'xx'})
 
94
        cmd = build_ext(dist)
 
95
        old = sys.platform
 
96
 
 
97
        sys.platform = 'sunos' # fooling finalize_options
 
98
        from distutils.sysconfig import  _config_vars
 
99
        old_var = _config_vars.get('Py_ENABLE_SHARED')
 
100
        _config_vars['Py_ENABLE_SHARED'] = 1
 
101
        try:
 
102
            cmd.ensure_finalized()
 
103
        finally:
 
104
            sys.platform = old
 
105
            if old_var is None:
 
106
                del _config_vars['Py_ENABLE_SHARED']
 
107
            else:
 
108
                _config_vars['Py_ENABLE_SHARED'] = old_var
 
109
 
 
110
        # make sur we get some lobrary dirs under solaris
 
111
        self.assert_(len(cmd.library_dirs) > 0)
 
112
 
 
113
    def test_user_site(self):
 
114
        # site.USER_SITE was introduced in 2.6
 
115
        if sys.version < '2.6':
 
116
            return
 
117
 
 
118
        import site
 
119
        dist = Distribution({'name': 'xx'})
 
120
        cmd = build_ext(dist)
 
121
 
 
122
        # making sure the suer option is there
 
123
        options = [name for name, short, lable in
 
124
                   cmd.user_options]
 
125
        self.assert_('user' in options)
 
126
 
 
127
        # setting a value
 
128
        cmd.user = 1
 
129
 
 
130
        # setting user based lib and include
 
131
        lib = os.path.join(site.USER_BASE, 'lib')
 
132
        incl = os.path.join(site.USER_BASE, 'include')
 
133
        os.mkdir(lib)
 
134
        os.mkdir(incl)
 
135
 
 
136
        # let's run finalize
 
137
        cmd.ensure_finalized()
 
138
 
 
139
        # see if include_dirs and library_dirs
 
140
        # were set
 
141
        self.assert_(lib in cmd.library_dirs)
 
142
        self.assert_(incl in cmd.include_dirs)
 
143
 
 
144
def test_suite():
 
145
    src = _get_source_filename()
 
146
    if not os.path.exists(src):
 
147
        if support.verbose:
 
148
            print('test_build_ext: Cannot find source code (test'
 
149
                  ' must run in python build dir)')
 
150
        return unittest.TestSuite()
 
151
    else: return unittest.makeSuite(BuildExtTestCase)
 
152
 
 
153
if __name__ == '__main__':
 
154
    support.run_unittest(test_suite())