~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/weave/tests/test_catalog.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import sys, os
2
 
 
3
 
 
4
 
from numpy.testing import *
5
 
set_package_path()
6
 
from weave import catalog
7
 
restore_path()
8
 
 
9
 
set_local_path()
10
 
from weave_test_utils import *
11
 
restore_path()
12
 
 
13
 
 
14
 
class test_default_dir(ScipyTestCase):
15
 
    def check_is_writable(self):
16
 
        path = catalog.default_dir()
17
 
        name = os.path.join(path,'dummy_catalog')
18
 
        test_file = open(name,'w')
19
 
        try:
20
 
            test_file.write('making sure default location is writable\n')
21
 
        finally:
22
 
            test_file.close()
23
 
            os.remove(name)
24
 
 
25
 
class test_os_dependent_catalog_name(ScipyTestCase):
26
 
    pass
27
 
 
28
 
class test_catalog_path(ScipyTestCase):
29
 
    def check_default(self):
30
 
        in_path = catalog.default_dir()
31
 
        path = catalog.catalog_path(in_path)
32
 
        d,f = os.path.split(path)
33
 
        assert(d == in_path)
34
 
        assert(f == catalog.os_dependent_catalog_name())
35
 
    def check_current(self):
36
 
        in_path = '.'
37
 
        path = catalog.catalog_path(in_path)
38
 
        d,f = os.path.split(path)
39
 
        assert(d == os.path.abspath(in_path))
40
 
        assert(f == catalog.os_dependent_catalog_name())
41
 
    def check_user(path):
42
 
        if sys.platform != 'win32':
43
 
            in_path = '~'
44
 
            path = catalog.catalog_path(in_path)
45
 
            d,f = os.path.split(path)
46
 
            assert(d == os.path.expanduser(in_path))
47
 
            assert(f == catalog.os_dependent_catalog_name())
48
 
    def check_module(self):
49
 
        # hand it a module and see if it uses the parent directory
50
 
        # of the module.
51
 
        path = catalog.catalog_path(os.__file__)
52
 
        d,f = os.path.split(os.__file__)
53
 
        d2,f = os.path.split(path)
54
 
        assert (d2 == d)
55
 
    def check_path(self):
56
 
        # use os.__file__ to get a usable directory.
57
 
        in_path,f = os.path.split(os.__file__)
58
 
        path = catalog.catalog_path(in_path)
59
 
        d,f = os.path.split(path)
60
 
        assert (d == in_path)
61
 
    def check_bad_path(self):
62
 
        # stupid_path_name
63
 
        in_path = 'stupid_path_name'
64
 
        path = catalog.catalog_path(in_path)
65
 
        assert (path is None)
66
 
 
67
 
class test_get_catalog(ScipyTestCase):
68
 
    """ This only tests whether new catalogs are created correctly.
69
 
        And whether non-existent return None correctly with read mode.
70
 
        Putting catalogs in the right place is all tested with
71
 
        catalog_dir tests.
72
 
    """
73
 
    def get_test_dir(self,erase = 0):
74
 
        # make sure tempdir catalog doesn't exist
75
 
        import tempfile, glob
76
 
        #temp = tempfile.gettempdir()
77
 
        pardir = tempfile.mktemp(suffix='cat_test')
78
 
        if not os.path.exists(pardir):
79
 
            os.mkdir(pardir)
80
 
        cat_glob = os.path.join(pardir,catalog.os_dependent_catalog_name()+'.*')
81
 
        cat_files = glob.glob(cat_glob)
82
 
        if erase:
83
 
            for cat_file in cat_files:
84
 
                os.remove(cat_file)
85
 
        return pardir
86
 
    def remove_dir(self,d):
87
 
        import distutils.dir_util
88
 
        distutils.dir_util.remove_tree(d)
89
 
 
90
 
    def check_nonexistent_catalog_is_none(self):
91
 
        pardir = self.get_test_dir(erase=1)
92
 
        cat = catalog.get_catalog(pardir,'r')
93
 
        self.remove_dir(pardir)
94
 
        assert(cat is None)
95
 
    def check_create_catalog(self):
96
 
        pardir = self.get_test_dir(erase=1)
97
 
        cat = catalog.get_catalog(pardir,'c')
98
 
        self.remove_dir(pardir)
99
 
        assert(cat is not None)
100
 
 
101
 
class test_catalog(ScipyTestCase):
102
 
 
103
 
    def clear_environ(self):
104
 
        if os.environ.has_key('PYTHONCOMPILED'):
105
 
            self.old_PYTHONCOMPILED = os.environ['PYTHONCOMPILED']
106
 
            del os.environ['PYTHONCOMPILED']
107
 
        else:
108
 
            self.old_PYTHONCOMPILED = None
109
 
    def reset_environ(self):
110
 
        if self.old_PYTHONCOMPILED:
111
 
            os.environ['PYTHONCOMPILED'] = self.old_PYTHONCOMPILED
112
 
            self.old_PYTHONCOMPILED = None
113
 
    def setUp(self):
114
 
        self.clear_environ()
115
 
    def tearDown(self):
116
 
        self.reset_environ()
117
 
 
118
 
    def check_set_module_directory(self):
119
 
        q = catalog.catalog()
120
 
        q.set_module_directory('bob')
121
 
        r = q.get_module_directory()
122
 
        assert (r == 'bob')
123
 
    def check_clear_module_directory(self):
124
 
        q = catalog.catalog()
125
 
        r = q.get_module_directory()
126
 
        assert (r is None)
127
 
        q.set_module_directory('bob')
128
 
        r = q.clear_module_directory()
129
 
        assert (r is None)
130
 
    def check_get_environ_path(self):
131
 
        if sys.platform == 'win32': sep = ';'
132
 
        else: sep = ':'
133
 
        os.environ['PYTHONCOMPILED'] = sep.join(('path1','path2','path3'))
134
 
        q = catalog.catalog()
135
 
        path = q.get_environ_path()
136
 
        assert(path == ['path1','path2','path3'])
137
 
    def check_build_search_order1(self):
138
 
        """ MODULE in search path should be replaced by module_dir.
139
 
        """
140
 
        q = catalog.catalog(['first','MODULE','third'])
141
 
        q.set_module_directory('second')
142
 
        order = q.build_search_order()
143
 
        assert(order == ['first','second','third',catalog.default_dir()])
144
 
    def check_build_search_order2(self):
145
 
        """ MODULE in search path should be removed if module_dir==None.
146
 
        """
147
 
        q = catalog.catalog(['first','MODULE','third'])
148
 
        order = q.build_search_order()
149
 
        assert(order == ['first','third',catalog.default_dir()])
150
 
    def check_build_search_order3(self):
151
 
        """ If MODULE is absent, module_dir shouldn't be in search path.
152
 
        """
153
 
        q = catalog.catalog(['first','second'])
154
 
        q.set_module_directory('third')
155
 
        order = q.build_search_order()
156
 
        assert(order == ['first','second',catalog.default_dir()])
157
 
    def check_build_search_order4(self):
158
 
        """ Make sure environment variable is getting used.
159
 
        """
160
 
        q = catalog.catalog(['first','second'])
161
 
        if sys.platform == 'win32': sep = ';'
162
 
        else: sep = ':'
163
 
        os.environ['PYTHONCOMPILED'] = sep.join(('MODULE','fourth','fifth'))
164
 
        q.set_module_directory('third')
165
 
        order = q.build_search_order()
166
 
        assert(order == ['first','second','third','fourth','fifth',catalog.default_dir()])
167
 
 
168
 
    def check_catalog_files1(self):
169
 
        """ Be sure we get at least one file even without specifying the path.
170
 
        """
171
 
        q = catalog.catalog()
172
 
        files = q.get_catalog_files()
173
 
        assert(len(files) == 1)
174
 
 
175
 
    def check_catalog_files2(self):
176
 
        """ Ignore bad paths in the path.
177
 
        """
178
 
        q = catalog.catalog()
179
 
        os.environ['PYTHONCOMPILED'] = '_some_bad_path_'
180
 
        files = q.get_catalog_files()
181
 
        assert(len(files) == 1)
182
 
 
183
 
    def check_get_existing_files1(self):
184
 
        """ Shouldn't get any files when temp doesn't exist and no path set.
185
 
        """
186
 
        clear_temp_catalog()
187
 
        q = catalog.catalog()
188
 
        files = q.get_existing_files()
189
 
        restore_temp_catalog()
190
 
        assert(len(files) == 0)
191
 
    def check_get_existing_files2(self):
192
 
        """ Shouldn't get a single file from the temp dir.
193
 
        """
194
 
        clear_temp_catalog()
195
 
        q = catalog.catalog()
196
 
        # create a dummy file
197
 
        import os
198
 
        q.add_function('code', os.getpid)
199
 
        del q
200
 
        q = catalog.catalog()
201
 
        files = q.get_existing_files()
202
 
        restore_temp_catalog()
203
 
        assert(len(files) == 1)
204
 
 
205
 
    def check_access_writable_file(self):
206
 
        """ There should always be a writable file -- even if it is in temp
207
 
        """
208
 
        q = catalog.catalog()
209
 
        file = q.get_writable_file()
210
 
        try:
211
 
            f = open(file,'w')
212
 
            f.write('bob')
213
 
        finally:
214
 
            f.close()
215
 
            os.remove(file)
216
 
    def check_writable_with_bad_path(self):
217
 
        """ There should always be a writable file -- even if search paths contain
218
 
            bad values.
219
 
        """
220
 
        if sys.platform == 'win32': sep = ';'
221
 
        else: sep = ':'
222
 
        os.environ['PYTHONCOMPILED'] = sep.join(('_bad_path_name_'))
223
 
        q = catalog.catalog()
224
 
        file = q.get_writable_file()
225
 
        try:
226
 
            f = open(file,'w')
227
 
            f.write('bob')
228
 
        finally:
229
 
            f.close()
230
 
        os.remove(file)
231
 
    def check_writable_dir(self):
232
 
        """ Check that we can create a file in the writable directory
233
 
        """
234
 
        q = catalog.catalog()
235
 
        d = q.get_writable_dir()
236
 
        file = os.path.join(d,'some_silly_file')
237
 
        try:
238
 
            f = open(file,'w')
239
 
            f.write('bob')
240
 
        finally:
241
 
            f.close()
242
 
            os.remove(file)
243
 
    def check_unique_module_name(self):
244
 
        """ Check that we can create a file in the writable directory
245
 
        """
246
 
        q = catalog.catalog()
247
 
        file = q.unique_module_name('bob')
248
 
        cfile1 = file+'.cpp'
249
 
        assert(not os.path.exists(cfile1))
250
 
        #make sure it is writable
251
 
        try:
252
 
            f = open(cfile1,'w')
253
 
            f.write('bob')
254
 
        finally:
255
 
            f.close()
256
 
        # try again with same code fragment -- should get unique name
257
 
        file = q.unique_module_name('bob')
258
 
        cfile2 = file+'.cpp'
259
 
        assert(not os.path.exists(cfile2+'.cpp'))
260
 
        os.remove(cfile1)
261
 
    def check_add_function_persistent1(self):
262
 
        """ Test persisting a function in the default catalog
263
 
        """
264
 
        clear_temp_catalog()
265
 
        q = catalog.catalog()
266
 
        # just use some already available functions
267
 
        import string
268
 
        funcs = [string.upper, string.lower, string.find,string.replace]
269
 
        for i in funcs:
270
 
            q.add_function_persistent('code',i)
271
 
        pfuncs = q.get_cataloged_functions('code')
272
 
        # any way to clean modules???
273
 
        restore_temp_catalog()
274
 
        for i in funcs:
275
 
            assert(i in pfuncs)
276
 
 
277
 
    def check_add_function_ordered(self):
278
 
        clear_temp_catalog()
279
 
        q = catalog.catalog()
280
 
        import string
281
 
 
282
 
        q.add_function('f',string.upper)
283
 
        q.add_function('f',string.lower)
284
 
        q.add_function('ff',string.find)
285
 
        q.add_function('ff',string.replace)
286
 
        q.add_function('fff',string.atof)
287
 
        q.add_function('fff',string.atoi)
288
 
        del q
289
 
 
290
 
        # now we're gonna make a new catalog with same code
291
 
        # but different functions in a specified module directory
292
 
        env_dir = empty_temp_dir()
293
 
        r = catalog.catalog(env_dir)
294
 
        r.add_function('ff',os.abort)
295
 
        r.add_function('ff',os.chdir)
296
 
        r.add_function('fff',os.access)
297
 
        r.add_function('fff',os.open)
298
 
        del r
299
 
        # now we're gonna make a new catalog with same code
300
 
        # but different functions in a user specified directory
301
 
        user_dir = empty_temp_dir()
302
 
        s = catalog.catalog(user_dir)
303
 
        import re
304
 
        s.add_function('fff',re.match)
305
 
        s.add_function('fff',re.purge)
306
 
        del s
307
 
 
308
 
        # open new catalog and make sure it retreives the functions
309
 
        # from d catalog instead of the temp catalog (made by q)
310
 
        os.environ['PYTHONCOMPILED'] = env_dir
311
 
        t = catalog.catalog(user_dir)
312
 
        funcs1 = t.get_functions('f')
313
 
        funcs2 = t.get_functions('ff')
314
 
        funcs3 = t.get_functions('fff')
315
 
        restore_temp_catalog()
316
 
        # make sure everything is read back in the correct order
317
 
        # a little cheating... I'm ignoring any functions that might have
318
 
        # been read in from a prior catalog file (such as the defualt one).
319
 
        # the test should really be made so that these aren't read in, but
320
 
        # until I get this figured out...
321
 
        #assert(funcs1 == [string.lower,string.upper])
322
 
        #assert(funcs2 == [os.chdir,os.abort,string.replace,string.find])
323
 
        #assert(funcs3 == [re.purge,re.match,os.open,
324
 
        #                  os.access,string.atoi,string.atof])
325
 
        assert(funcs1[:2] == [string.lower,string.upper]),`funcs1`
326
 
        assert(funcs2[:4] == [os.chdir,os.abort,string.replace,string.find])
327
 
        assert(funcs3[:6] == [re.purge,re.match,os.open,
328
 
                          os.access,string.atoi,string.atof])
329
 
        cleanup_temp_dir(user_dir)
330
 
        cleanup_temp_dir(env_dir)
331
 
 
332
 
 
333
 
if __name__ == '__main__':
334
 
    ScipyTest('weave.catalog').run()