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

« back to all changes in this revision

Viewing changes to Lib/weave/tests/test_scxx_object.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
 
""" Test refcounting and behavior of SCXX.
2
 
"""
3
 
import time
4
 
import os,sys
5
 
 
6
 
from numpy.testing import *
7
 
set_package_path()
8
 
from weave import inline_tools
9
 
restore_path()
10
 
 
11
 
class test_object_construct(ScipyTestCase):
12
 
    #------------------------------------------------------------------------
13
 
    # Check that construction from basic types is allowed and have correct
14
 
    # reference counts
15
 
    #------------------------------------------------------------------------
16
 
    def check_int(self,level=5):
17
 
        # strange int value used to try and make sure refcount is 2.
18
 
        code = """
19
 
               py::object val = 1001;
20
 
               return_val = val;
21
 
               """
22
 
        res = inline_tools.inline(code)
23
 
        assert sys.getrefcount(res) == 2
24
 
        assert res == 1001
25
 
    def check_float(self,level=5):
26
 
        code = """
27
 
               py::object val = (float)1.0;
28
 
               return_val = val;
29
 
               """
30
 
        res = inline_tools.inline(code)
31
 
        assert sys.getrefcount(res) == 2
32
 
        assert res == 1.0
33
 
    def check_double(self,level=5):
34
 
        code = """
35
 
               py::object val = 1.0;
36
 
               return_val = val;
37
 
               """
38
 
        res = inline_tools.inline(code)
39
 
        assert sys.getrefcount(res) == 2
40
 
        assert res == 1.0
41
 
    def check_complex(self,level=5):
42
 
        code = """
43
 
               std::complex<double> num = std::complex<double>(1.0,1.0);
44
 
               py::object val = num;
45
 
               return_val = val;
46
 
               """
47
 
        res = inline_tools.inline(code)
48
 
        assert sys.getrefcount(res) == 2
49
 
        assert res == 1.0+1.0j
50
 
    def check_string(self,level=5):
51
 
        code = """
52
 
               py::object val = "hello";
53
 
               return_val = val;
54
 
               """
55
 
        res = inline_tools.inline(code)
56
 
        assert sys.getrefcount(res) == 2
57
 
        assert res == "hello"
58
 
 
59
 
    def check_std_string(self,level=5):
60
 
        code = """
61
 
               std::string s = std::string("hello");
62
 
               py::object val = s;
63
 
               return_val = val;
64
 
               """
65
 
        res = inline_tools.inline(code)
66
 
        assert sys.getrefcount(res) == 2
67
 
        assert res == "hello"
68
 
 
69
 
class test_object_print(ScipyTestCase):
70
 
    #------------------------------------------------------------------------
71
 
    # Check the object print protocol.
72
 
    #------------------------------------------------------------------------
73
 
    def check_stdout(self,level=5):
74
 
        code = """
75
 
               py::object val = "how now brown cow";
76
 
               val.print(stdout);
77
 
               """
78
 
        res = inline_tools.inline(code)
79
 
        # visual check on this one.
80
 
    def check_stringio(self,level=5):
81
 
        import cStringIO
82
 
        file_imposter = cStringIO.StringIO()
83
 
        code = """
84
 
               py::object val = "how now brown cow";
85
 
               val.print(file_imposter);
86
 
               """
87
 
        res = inline_tools.inline(code,['file_imposter'])
88
 
        print file_imposter.getvalue()
89
 
        assert file_imposter.getvalue() == "'how now brown cow'"
90
 
 
91
 
##    def check_failure(self,level=5):
92
 
##        code = """
93
 
##               FILE* file = 0;
94
 
##               py::object val = "how now brown cow";
95
 
##               val.print(file);
96
 
##               """
97
 
##        try:
98
 
##            res = inline_tools.inline(code)
99
 
##        except:
100
 
##            # error was supposed to occur.
101
 
##            pass
102
 
 
103
 
 
104
 
class test_object_cast(ScipyTestCase):
105
 
    def check_int_cast(self,level=5):
106
 
        code = """
107
 
               py::object val = 1;
108
 
               int raw_val = val;
109
 
               """
110
 
        inline_tools.inline(code)
111
 
    def check_double_cast(self,level=5):
112
 
        code = """
113
 
               py::object val = 1.0;
114
 
               double raw_val = val;
115
 
               """
116
 
        inline_tools.inline(code)
117
 
    def check_float_cast(self,level=5):
118
 
        code = """
119
 
               py::object val = 1.0;
120
 
               float raw_val = val;
121
 
               """
122
 
        inline_tools.inline(code)
123
 
    def check_complex_cast(self,level=5):
124
 
        code = """
125
 
               std::complex<double> num = std::complex<double>(1.0,1.0);
126
 
               py::object val = num;
127
 
               std::complex<double> raw_val = val;
128
 
               """
129
 
        inline_tools.inline(code)
130
 
    def check_string_cast(self,level=5):
131
 
        code = """
132
 
               py::object val = "hello";
133
 
               std::string raw_val = val;
134
 
               """
135
 
        inline_tools.inline(code)
136
 
 
137
 
# test class used for testing python class access from C++.
138
 
class foo:
139
 
    def bar(self):
140
 
        return "bar results"
141
 
    def bar2(self,val1,val2):
142
 
        return val1, val2
143
 
    def bar3(self,val1,val2,val3=1):
144
 
        return val1, val2, val3
145
 
 
146
 
class str_obj:
147
 
    def __str__(self):
148
 
        return "b"
149
 
 
150
 
class test_object_hasattr(ScipyTestCase):
151
 
    def check_string(self,level=5):
152
 
        a = foo()
153
 
        a.b = 12345
154
 
        code = """
155
 
               return_val = a.hasattr("b");
156
 
               """
157
 
        res = inline_tools.inline(code,['a'])
158
 
        assert res
159
 
    def check_std_string(self,level=5):
160
 
        a = foo()
161
 
        a.b = 12345
162
 
        attr_name = "b"
163
 
        code = """
164
 
               return_val = a.hasattr(attr_name);
165
 
               """
166
 
        res = inline_tools.inline(code,['a','attr_name'])
167
 
        assert res
168
 
    def check_string_fail(self,level=5):
169
 
        a = foo()
170
 
        a.b = 12345
171
 
        code = """
172
 
               return_val = a.hasattr("c");
173
 
               """
174
 
        res = inline_tools.inline(code,['a'])
175
 
        assert not res
176
 
    def check_inline(self,level=5):
177
 
        """ THIS NEEDS TO MOVE TO THE INLINE TEST SUITE
178
 
        """
179
 
        a = foo()
180
 
        a.b = 12345
181
 
        code = """
182
 
               throw_error(PyExc_AttributeError,"bummer");
183
 
               """
184
 
        try:
185
 
            before = sys.getrefcount(a)
186
 
            res = inline_tools.inline(code,['a'])
187
 
        except AttributeError:
188
 
            after = sys.getrefcount(a)
189
 
            try:
190
 
                res = inline_tools.inline(code,['a'])
191
 
            except:
192
 
                after2 = sys.getrefcount(a)
193
 
            print "after and after2 should be equal in the following"
194
 
            print 'before, after, after2:', before, after, after2
195
 
            pass
196
 
 
197
 
    def check_func(self,level=5):
198
 
        a = foo()
199
 
        a.b = 12345
200
 
        code = """
201
 
               return_val = a.hasattr("bar");
202
 
               """
203
 
        res = inline_tools.inline(code,['a'])
204
 
        assert res
205
 
 
206
 
class test_object_attr(ScipyTestCase):
207
 
 
208
 
    def generic_attr(self,code,args=['a']):
209
 
        a = foo()
210
 
        a.b = 12345
211
 
 
212
 
        before = sys.getrefcount(a.b)
213
 
        res = inline_tools.inline(code,args)
214
 
        assert res == a.b
215
 
        del res
216
 
        after = sys.getrefcount(a.b)
217
 
        assert after == before
218
 
 
219
 
    def check_char(self,level=5):
220
 
        self.generic_attr('return_val = a.attr("b");')
221
 
 
222
 
    def check_char_fail(self,level=5):
223
 
        try:
224
 
            self.generic_attr('return_val = a.attr("c");')
225
 
        except AttributeError:
226
 
            pass
227
 
 
228
 
    def check_string(self,level=5):
229
 
        self.generic_attr('return_val = a.attr(std::string("b"));')
230
 
 
231
 
    def check_string_fail(self,level=5):
232
 
        try:
233
 
            self.generic_attr('return_val = a.attr(std::string("c"));')
234
 
        except AttributeError:
235
 
            pass
236
 
 
237
 
    def check_obj(self,level=5):
238
 
        code = """
239
 
               py::object name = "b";
240
 
               return_val = a.attr(name);
241
 
               """
242
 
        self.generic_attr(code,['a'])
243
 
 
244
 
    def check_obj_fail(self,level=5):
245
 
        try:
246
 
            code = """
247
 
                   py::object name = "c";
248
 
                   return_val = a.attr(name);
249
 
                   """
250
 
            self.generic_attr(code,['a'])
251
 
        except AttributeError:
252
 
            pass
253
 
 
254
 
    def check_attr_call(self,level=5):
255
 
        a = foo()
256
 
        res = inline_tools.inline('return_val = a.attr("bar").call();',['a'])
257
 
        first = sys.getrefcount(res)
258
 
        del res
259
 
        res = inline_tools.inline('return_val = a.attr("bar").call();',['a'])
260
 
        second = sys.getrefcount(res)
261
 
        assert res == "bar results"
262
 
        assert first == second
263
 
 
264
 
class test_object_set_attr(ScipyTestCase):
265
 
 
266
 
    def generic_existing(self, code, desired):
267
 
        args = ['a']
268
 
        a = foo()
269
 
        a.b = 12345
270
 
        res = inline_tools.inline(code,args)
271
 
        assert a.b == desired
272
 
 
273
 
    def generic_new(self, code, desired):
274
 
        args = ['a']
275
 
        a = foo()
276
 
        res = inline_tools.inline(code,args)
277
 
        assert a.b == desired
278
 
 
279
 
    def check_existing_char(self,level=5):
280
 
        self.generic_existing('a.set_attr("b","hello");',"hello")
281
 
    def check_new_char(self,level=5):
282
 
        self.generic_new('a.set_attr("b","hello");',"hello")
283
 
    def check_existing_string(self,level=5):
284
 
        self.generic_existing('a.set_attr("b",std::string("hello"));',"hello")
285
 
    def check_new_string(self,level=5):
286
 
        self.generic_new('a.set_attr("b",std::string("hello"));',"hello")
287
 
    def check_existing_object(self,level=5):
288
 
        code = """
289
 
               py::object obj = "hello";
290
 
               a.set_attr("b",obj);
291
 
               """
292
 
        self.generic_existing(code,"hello")
293
 
    def check_new_object(self,level=5):
294
 
        code = """
295
 
               py::object obj = "hello";
296
 
               a.set_attr("b",obj);
297
 
               """
298
 
        self.generic_new(code,"hello")
299
 
    def check_new_fail(self,level=5):
300
 
        try:
301
 
            code = """
302
 
                   py::object obj = 1;
303
 
                   a.set_attr(obj,"hello");
304
 
                   """
305
 
            self.generic_new(code,"hello")
306
 
        except:
307
 
            pass
308
 
 
309
 
    def check_existing_int(self,level=5):
310
 
        self.generic_existing('a.set_attr("b",1);',1)
311
 
    def check_existing_double(self,level=5):
312
 
        self.generic_existing('a.set_attr("b",1.0);',1.0)
313
 
    def check_existing_complex(self,level=5):
314
 
        code = """
315
 
               std::complex<double> obj = std::complex<double>(1,1);
316
 
               a.set_attr("b",obj);
317
 
               """
318
 
        self.generic_existing(code,1+1j)
319
 
    def check_existing_char1(self,level=5):
320
 
        self.generic_existing('a.set_attr("b","hello");',"hello")
321
 
    def check_existing_string1(self,level=5):
322
 
        code = """
323
 
               std::string obj = std::string("hello");
324
 
               a.set_attr("b",obj);
325
 
               """
326
 
        self.generic_existing(code,"hello")
327
 
 
328
 
class test_object_del(ScipyTestCase):
329
 
    def generic(self, code):
330
 
        args = ['a']
331
 
        a = foo()
332
 
        a.b = 12345
333
 
        res = inline_tools.inline(code,args)
334
 
        assert not hasattr(a,"b")
335
 
 
336
 
    def check_char(self,level=5):
337
 
        self.generic('a.del("b");')
338
 
    def check_string(self,level=5):
339
 
        code = """
340
 
               std::string name = std::string("b");
341
 
               a.del(name);
342
 
               """
343
 
        self.generic(code)
344
 
    def check_object(self,level=5):
345
 
        code = """
346
 
               py::object name = py::object("b");
347
 
               a.del(name);
348
 
               """
349
 
        self.generic(code)
350
 
 
351
 
class test_object_cmp(ScipyTestCase):
352
 
    def check_equal(self,level=5):
353
 
        a,b = 1,1
354
 
        res = inline_tools.inline('return_val = (a == b);',['a','b'])
355
 
        assert res == (a == b)
356
 
    def check_equal_objects(self,level=5):
357
 
        class foo:
358
 
            def __init__(self,x):
359
 
                self.x = x
360
 
            def __cmp__(self,other):
361
 
                return cmp(self.x,other.x)
362
 
        a,b = foo(1),foo(2)
363
 
        res = inline_tools.inline('return_val = (a == b);',['a','b'])
364
 
        assert res == (a == b)
365
 
    def check_lt(self,level=5):
366
 
        a,b = 1,2
367
 
        res = inline_tools.inline('return_val = (a < b);',['a','b'])
368
 
        assert res == (a < b)
369
 
    def check_gt(self,level=5):
370
 
        a,b = 1,2
371
 
        res = inline_tools.inline('return_val = (a > b);',['a','b'])
372
 
        assert res == (a > b)
373
 
    def check_gte(self,level=5):
374
 
        a,b = 1,2
375
 
        res = inline_tools.inline('return_val = (a >= b);',['a','b'])
376
 
        assert res == (a >= b)
377
 
    def check_lte(self,level=5):
378
 
        a,b = 1,2
379
 
        res = inline_tools.inline('return_val = (a <= b);',['a','b'])
380
 
        assert res == (a <= b)
381
 
    def check_not_equal(self,level=5):
382
 
        a,b = 1,2
383
 
        res = inline_tools.inline('return_val = (a != b);',['a','b'])
384
 
        assert res == (a != b)
385
 
    def check_int(self,level=5):
386
 
        a = 1
387
 
        res = inline_tools.inline('return_val = (a == 1);',['a'])
388
 
        assert res == (a == 1)
389
 
    def check_int2(self,level=5):
390
 
        a = 1
391
 
        res = inline_tools.inline('return_val = (1 == a);',['a'])
392
 
        assert res == (a == 1)
393
 
    def check_unsigned_long(self,level=5):
394
 
        a = 1
395
 
        res = inline_tools.inline('return_val = (a == (unsigned long)1);',['a'])
396
 
        assert res == (a == 1)
397
 
    def check_double(self,level=5):
398
 
        a = 1
399
 
        res = inline_tools.inline('return_val = (a == 1.0);',['a'])
400
 
        assert res == (a == 1.0)
401
 
    def check_char(self,level=5):
402
 
        a = "hello"
403
 
        res = inline_tools.inline('return_val = (a == "hello");',['a'])
404
 
        assert res == (a == "hello")
405
 
    def check_std_string(self,level=5):
406
 
        a = "hello"
407
 
        code = """
408
 
               std::string hello = std::string("hello");
409
 
               return_val = (a == hello);
410
 
               """
411
 
        res = inline_tools.inline(code,['a'])
412
 
        assert res == (a == "hello")
413
 
 
414
 
class test_object_repr(ScipyTestCase):
415
 
    def check_repr(self,level=5):
416
 
        class foo:
417
 
            def __str__(self):
418
 
                return "str return"
419
 
            def __repr__(self):
420
 
                return "repr return"
421
 
        a = foo()
422
 
        res = inline_tools.inline('return_val = a.repr();',['a'])
423
 
        first = sys.getrefcount(res)
424
 
        del res
425
 
        res = inline_tools.inline('return_val = a.repr();',['a'])
426
 
        second = sys.getrefcount(res)
427
 
        assert first == second
428
 
        assert res == "repr return"
429
 
 
430
 
class test_object_str(ScipyTestCase):
431
 
    def check_str(self,level=5):
432
 
        class foo:
433
 
            def __str__(self):
434
 
                return "str return"
435
 
            def __repr__(self):
436
 
                return "repr return"
437
 
        a = foo()
438
 
        res = inline_tools.inline('return_val = a.str();',['a'])
439
 
        first = sys.getrefcount(res)
440
 
        del res
441
 
        res = inline_tools.inline('return_val = a.str();',['a'])
442
 
        second = sys.getrefcount(res)
443
 
        assert first == second
444
 
        print res
445
 
        assert res == "str return"
446
 
 
447
 
class test_object_unicode(ScipyTestCase):
448
 
    # This ain't going to win awards for test of the year...
449
 
    def check_unicode(self,level=5):
450
 
        class foo:
451
 
            def __repr__(self):
452
 
                return "repr return"
453
 
            def __str__(self):
454
 
                return "unicode"
455
 
        a= foo()
456
 
        res = inline_tools.inline('return_val = a.unicode();',['a'])
457
 
        first = sys.getrefcount(res)
458
 
        del res
459
 
        res = inline_tools.inline('return_val = a.unicode();',['a'])
460
 
        second = sys.getrefcount(res)
461
 
        assert first == second
462
 
        assert res == "unicode"
463
 
 
464
 
class test_object_is_callable(ScipyTestCase):
465
 
    def check_true(self,level=5):
466
 
        class foo:
467
 
            def __call__(self):
468
 
                return 0
469
 
        a= foo()
470
 
        res = inline_tools.inline('return_val = a.is_callable();',['a'])
471
 
        assert res
472
 
    def check_false(self,level=5):
473
 
        class foo:
474
 
            pass
475
 
        a= foo()
476
 
        res = inline_tools.inline('return_val = a.is_callable();',['a'])
477
 
        assert not res
478
 
 
479
 
class test_object_call(ScipyTestCase):
480
 
    def check_noargs(self,level=5):
481
 
        def foo():
482
 
            return (1,2,3)
483
 
        res = inline_tools.inline('return_val = foo.call();',['foo'])
484
 
        assert res == (1,2,3)
485
 
        assert sys.getrefcount(res) == 2
486
 
    def check_args(self,level=5):
487
 
        def foo(val1,val2):
488
 
            return (val1,val2)
489
 
        code = """
490
 
               py::tuple args(2);
491
 
               args[0] = 1;
492
 
               args[1] = "hello";
493
 
               return_val = foo.call(args);
494
 
               """
495
 
        res = inline_tools.inline(code,['foo'])
496
 
        assert res == (1,"hello")
497
 
        assert sys.getrefcount(res) == 2
498
 
    def check_args_kw(self,level=5):
499
 
        def foo(val1,val2,val3=1):
500
 
            return (val1,val2,val3)
501
 
        code = """
502
 
               py::tuple args(2);
503
 
               args[0] = 1;
504
 
               args[1] = "hello";
505
 
               py::dict kw;
506
 
               kw["val3"] = 3;
507
 
               return_val = foo.call(args,kw);
508
 
               """
509
 
        res = inline_tools.inline(code,['foo'])
510
 
        assert res == (1,"hello",3)
511
 
        assert sys.getrefcount(res) == 2
512
 
    def check_noargs_with_args(self,level=5):
513
 
        # calling a function that does take args with args
514
 
        # should fail.
515
 
        def foo():
516
 
            return "blah"
517
 
        code = """
518
 
               py::tuple args(2);
519
 
               args[0] = 1;
520
 
               args[1] = "hello";
521
 
               return_val = foo.call(args);
522
 
               """
523
 
        try:
524
 
            first = sys.getrefcount(foo)
525
 
            res = inline_tools.inline(code,['foo'])
526
 
        except TypeError:
527
 
            second = sys.getrefcount(foo)
528
 
            try:
529
 
                res = inline_tools.inline(code,['foo'])
530
 
            except TypeError:
531
 
                third = sys.getrefcount(foo)
532
 
        # first should == second, but the weird refcount error
533
 
        assert second == third
534
 
 
535
 
class test_object_mcall(ScipyTestCase):
536
 
    def check_noargs(self,level=5):
537
 
        a = foo()
538
 
        res = inline_tools.inline('return_val = a.mcall("bar");',['a'])
539
 
        assert res == "bar results"
540
 
        first = sys.getrefcount(res)
541
 
        del res
542
 
        res = inline_tools.inline('return_val = a.mcall("bar");',['a'])
543
 
        assert res == "bar results"
544
 
        second = sys.getrefcount(res)
545
 
        assert first == second
546
 
    def check_args(self,level=5):
547
 
        a = foo()
548
 
        code = """
549
 
               py::tuple args(2);
550
 
               args[0] = 1;
551
 
               args[1] = "hello";
552
 
               return_val = a.mcall("bar2",args);
553
 
               """
554
 
        res = inline_tools.inline(code,['a'])
555
 
        assert res == (1,"hello")
556
 
        assert sys.getrefcount(res) == 2
557
 
    def check_args_kw(self,level=5):
558
 
        a = foo()
559
 
        code = """
560
 
               py::tuple args(2);
561
 
               args[0] = 1;
562
 
               args[1] = "hello";
563
 
               py::dict kw;
564
 
               kw["val3"] = 3;
565
 
               return_val = a.mcall("bar3",args,kw);
566
 
               """
567
 
        res = inline_tools.inline(code,['a'])
568
 
        assert res == (1,"hello",3)
569
 
        assert sys.getrefcount(res) == 2
570
 
    def check_std_noargs(self,level=5):
571
 
        a = foo()
572
 
        method = "bar"
573
 
        res = inline_tools.inline('return_val = a.mcall(method);',['a','method'])
574
 
        assert res == "bar results"
575
 
        first = sys.getrefcount(res)
576
 
        del res
577
 
        res = inline_tools.inline('return_val = a.mcall(method);',['a','method'])
578
 
        assert res == "bar results"
579
 
        second = sys.getrefcount(res)
580
 
        assert first == second
581
 
    def check_std_args(self,level=5):
582
 
        a = foo()
583
 
        method = "bar2"
584
 
        code = """
585
 
               py::tuple args(2);
586
 
               args[0] = 1;
587
 
               args[1] = "hello";
588
 
               return_val = a.mcall(method,args);
589
 
               """
590
 
        res = inline_tools.inline(code,['a','method'])
591
 
        assert res == (1,"hello")
592
 
        assert sys.getrefcount(res) == 2
593
 
    def check_std_args_kw(self,level=5):
594
 
        a = foo()
595
 
        method = "bar3"
596
 
        code = """
597
 
               py::tuple args(2);
598
 
               args[0] = 1;
599
 
               args[1] = "hello";
600
 
               py::dict kw;
601
 
               kw["val3"] = 3;
602
 
               return_val = a.mcall(method,args,kw);
603
 
               """
604
 
        res = inline_tools.inline(code,['a','method'])
605
 
        assert res == (1,"hello",3)
606
 
        assert sys.getrefcount(res) == 2
607
 
    def check_noargs_with_args(self,level=5):
608
 
        # calling a function that does take args with args
609
 
        # should fail.
610
 
        a = foo()
611
 
        code = """
612
 
               py::tuple args(2);
613
 
               args[0] = 1;
614
 
               args[1] = "hello";
615
 
               return_val = a.mcall("bar",args);
616
 
               """
617
 
        try:
618
 
            first = sys.getrefcount(a)
619
 
            res = inline_tools.inline(code,['a'])
620
 
        except TypeError:
621
 
            second = sys.getrefcount(a)
622
 
            try:
623
 
                res = inline_tools.inline(code,['a'])
624
 
            except TypeError:
625
 
                third = sys.getrefcount(a)
626
 
        # first should == second, but the weird refcount error
627
 
        assert second == third
628
 
 
629
 
class test_object_hash(ScipyTestCase):
630
 
    def check_hash(self,level=5):
631
 
        class foo:
632
 
            def __hash__(self):
633
 
                return 123
634
 
        a= foo()
635
 
        res = inline_tools.inline('return_val = a.hash(); ',['a'])
636
 
        print 'hash:', res
637
 
        assert res == 123
638
 
 
639
 
class test_object_is_true(ScipyTestCase):
640
 
    def check_true(self,level=5):
641
 
        class foo:
642
 
            pass
643
 
        a= foo()
644
 
        res = inline_tools.inline('return_val = a.is_true();',['a'])
645
 
        assert res == 1
646
 
    def check_false(self,level=5):
647
 
        a= None
648
 
        res = inline_tools.inline('return_val = a.is_true();',['a'])
649
 
        assert res == 0
650
 
 
651
 
class test_object_is_true(ScipyTestCase):
652
 
    def check_false(self,level=5):
653
 
        class foo:
654
 
            pass
655
 
        a= foo()
656
 
        res = inline_tools.inline('return_val = a.not();',['a'])
657
 
        assert res == 0
658
 
    def check_true(self,level=5):
659
 
        a= None
660
 
        res = inline_tools.inline('return_val = a.not();',['a'])
661
 
        assert res == 1
662
 
 
663
 
class test_object_type(ScipyTestCase):
664
 
    def check_type(self,level=5):
665
 
        class foo:
666
 
            pass
667
 
        a= foo()
668
 
        res = inline_tools.inline('return_val = a.type();',['a'])
669
 
        assert res == type(a)
670
 
 
671
 
class test_object_size(ScipyTestCase):
672
 
    def check_size(self,level=5):
673
 
        class foo:
674
 
            def __len__(self):
675
 
                return 10
676
 
        a= foo()
677
 
        res = inline_tools.inline('return_val = a.size();',['a'])
678
 
        assert res == len(a)
679
 
    def check_len(self,level=5):
680
 
        class foo:
681
 
            def __len__(self):
682
 
                return 10
683
 
        a= foo()
684
 
        res = inline_tools.inline('return_val = a.len();',['a'])
685
 
        assert res == len(a)
686
 
    def check_length(self,level=5):
687
 
        class foo:
688
 
            def __len__(self):
689
 
                return 10
690
 
        a= foo()
691
 
        res = inline_tools.inline('return_val = a.length();',['a'])
692
 
        assert res == len(a)
693
 
 
694
 
from UserList import UserList
695
 
class test_object_set_item_op_index(ScipyTestCase):
696
 
    def check_list_refcount(self,level=5):
697
 
        a = UserList([1,2,3])
698
 
        # temporary refcount fix until I understand why it incs by one.
699
 
        inline_tools.inline("a[1] = 1234;",['a'])
700
 
        before1 = sys.getrefcount(a)
701
 
        after1 = sys.getrefcount(a)
702
 
        assert after1 == before1
703
 
    def check_set_int(self,level=5):
704
 
        a = UserList([1,2,3])
705
 
        inline_tools.inline("a[1] = 1234;",['a'])
706
 
        assert sys.getrefcount(a[1]) == 2
707
 
        assert a[1] == 1234
708
 
    def check_set_double(self,level=5):
709
 
        a = UserList([1,2,3])
710
 
        inline_tools.inline("a[1] = 123.0;",['a'])
711
 
        assert sys.getrefcount(a[1]) == 2
712
 
        assert a[1] == 123.0
713
 
    def check_set_char(self,level=5):
714
 
        a = UserList([1,2,3])
715
 
        inline_tools.inline('a[1] = "bubba";',['a'])
716
 
        assert sys.getrefcount(a[1]) == 2
717
 
        assert a[1] == 'bubba'
718
 
    def check_set_string(self,level=5):
719
 
        a = UserList([1,2,3])
720
 
        inline_tools.inline('a[1] = std::string("sissy");',['a'])
721
 
        assert sys.getrefcount(a[1]) == 2
722
 
        assert a[1] == 'sissy'
723
 
    def check_set_string(self,level=5):
724
 
        a = UserList([1,2,3])
725
 
        inline_tools.inline('a[1] = std::complex<double>(1,1);',['a'])
726
 
        assert sys.getrefcount(a[1]) == 2
727
 
        assert a[1] == 1+1j
728
 
 
729
 
from UserDict import UserDict
730
 
class test_object_set_item_op_key(ScipyTestCase):
731
 
    def check_key_refcount(self,level=5):
732
 
        a = UserDict()
733
 
        code =  """
734
 
                py::object one = 1;
735
 
                py::object two = 2;
736
 
                py::tuple ref_counts(3);
737
 
                py::tuple obj_counts(3);
738
 
                py::tuple val_counts(3);
739
 
                py::tuple key_counts(3);
740
 
                obj_counts[0] = a.refcount();
741
 
                key_counts[0] = one.refcount();
742
 
                val_counts[0] = two.refcount();
743
 
                a[1] = 2;
744
 
                obj_counts[1] = a.refcount();
745
 
                key_counts[1] = one.refcount();
746
 
                val_counts[1] = two.refcount();
747
 
                a[1] = 2;
748
 
                obj_counts[2] = a.refcount();
749
 
                key_counts[2] = one.refcount();
750
 
                val_counts[2] = two.refcount();
751
 
 
752
 
                ref_counts[0] = obj_counts;
753
 
                ref_counts[1] = key_counts;
754
 
                ref_counts[2] = val_counts;
755
 
                return_val = ref_counts;
756
 
                """
757
 
        obj,key,val = inline_tools.inline(code,['a'])
758
 
        assert obj[0] == obj[1] and obj[1] == obj[2]
759
 
        assert key[0] + 1 == key[1] and key[1] == key[2]
760
 
        assert val[0] + 1 == val[1] and val[1] == val[2]
761
 
 
762
 
    def check_set_double_exists(self,level=5):
763
 
        a = UserDict()
764
 
        key = 10.0
765
 
        a[key] = 100.0
766
 
        inline_tools.inline('a[key] = 123.0;',['a','key'])
767
 
        first = sys.getrefcount(key)
768
 
        inline_tools.inline('a[key] = 123.0;',['a','key'])
769
 
        second = sys.getrefcount(key)
770
 
        assert first == second
771
 
        # !! I think the following should be 3
772
 
        assert sys.getrefcount(key) ==  5
773
 
        assert sys.getrefcount(a[key]) == 2
774
 
        assert a[key] == 123.0
775
 
    def check_set_double_new(self,level=5):
776
 
        a = UserDict()
777
 
        key = 1.0
778
 
        inline_tools.inline('a[key] = 123.0;',['a','key'])
779
 
        assert sys.getrefcount(key) == 4 # should be 3
780
 
        assert sys.getrefcount(a[key]) == 2
781
 
        assert a[key] == 123.0
782
 
    def check_set_complex(self,level=5):
783
 
        a = UserDict()
784
 
        key = 1+1j
785
 
        inline_tools.inline("a[key] = 1234;",['a','key'])
786
 
        assert sys.getrefcount(key) == 3
787
 
        assert sys.getrefcount(a[key]) == 2
788
 
        assert a[key] == 1234
789
 
    def check_set_char(self,level=5):
790
 
        a = UserDict()
791
 
        inline_tools.inline('a["hello"] = 123.0;',['a'])
792
 
        assert sys.getrefcount(a["hello"]) == 2
793
 
        assert a["hello"] == 123.0
794
 
 
795
 
    def check_set_class(self,level=5):
796
 
        a = UserDict()
797
 
        class foo:
798
 
            def __init__(self,val):
799
 
                self.val = val
800
 
            def __hash__(self):
801
 
                return self.val
802
 
        key = foo(4)
803
 
        inline_tools.inline('a[key] = "bubba";',['a','key'])
804
 
        first = sys.getrefcount(key)
805
 
        inline_tools.inline('a[key] = "bubba";',['a','key'])
806
 
        second = sys.getrefcount(key)
807
 
        # I don't think we're leaking if this is true
808
 
        assert first == second
809
 
        # !! BUT -- I think this should be 3
810
 
        assert sys.getrefcount(key) == 4
811
 
        assert sys.getrefcount(a[key]) == 2
812
 
        assert a[key] == 'bubba'
813
 
    def check_set_from_member(self,level=5):
814
 
        a = UserDict()
815
 
        a['first'] = 1
816
 
        a['second'] = 2
817
 
        inline_tools.inline('a["first"] = a["second"];',['a'])
818
 
        assert a['first'] == a['second']
819
 
 
820
 
if __name__ == "__main__":
821
 
    ScipyTest().run()