Some error handling/reporting fixes.
[pygobject.git] / tests / test_gi.py
blob1fbc216f6eb39ecad203ed5267fa999f89eff86a
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # coding=utf-8
3 # vim: tabstop=4 shiftwidth=4 expandtab
5 import sys
7 import unittest
8 import tempfile
9 import shutil
10 import os
11 import locale
12 import subprocess
13 import gc
14 import weakref
15 import warnings
16 from io import StringIO, BytesIO
18 import gi
19 import gi.overrides
20 from gi import PyGIWarning
21 from gi import PyGIDeprecationWarning
22 from gi.repository import GObject, GLib, Gio
24 from gi.repository import GIMarshallingTests
26 from compathelper import _bytes, _unicode
27 from helper import capture_exceptions
29 if sys.version_info < (3, 0):
30 CONSTANT_UTF8 = "const \xe2\x99\xa5 utf8"
31 PY2_UNICODE_UTF8 = unicode(CONSTANT_UTF8, 'UTF-8')
32 CHAR_255 = '\xff'
33 else:
34 CONSTANT_UTF8 = "const ♥ utf8"
35 CHAR_255 = bytes([255])
37 CONSTANT_NUMBER = 42
40 class Number(object):
42 def __init__(self, value):
43 self.value = value
45 def __int__(self):
46 return int(self.value)
48 def __float__(self):
49 return float(self.value)
52 class Sequence(object):
54 def __init__(self, sequence):
55 self.sequence = sequence
57 def __len__(self):
58 return len(self.sequence)
60 def __getitem__(self, key):
61 return self.sequence[key]
64 class TestConstant(unittest.TestCase):
66 def test_constant_utf8(self):
67 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.CONSTANT_UTF8)
69 def test_constant_number(self):
70 self.assertEqual(CONSTANT_NUMBER, GIMarshallingTests.CONSTANT_NUMBER)
72 def test_min_max_int(self):
73 self.assertEqual(GLib.MAXINT32, 2 ** 31 - 1)
74 self.assertEqual(GLib.MININT32, -2 ** 31)
75 self.assertEqual(GLib.MAXUINT32, 2 ** 32 - 1)
77 self.assertEqual(GLib.MAXINT64, 2 ** 63 - 1)
78 self.assertEqual(GLib.MININT64, -2 ** 63)
79 self.assertEqual(GLib.MAXUINT64, 2 ** 64 - 1)
82 class TestBoolean(unittest.TestCase):
84 def test_boolean_return(self):
85 self.assertEqual(True, GIMarshallingTests.boolean_return_true())
86 self.assertEqual(False, GIMarshallingTests.boolean_return_false())
88 def test_boolean_in(self):
89 GIMarshallingTests.boolean_in_true(True)
90 GIMarshallingTests.boolean_in_false(False)
92 GIMarshallingTests.boolean_in_true(1)
93 GIMarshallingTests.boolean_in_false(0)
95 def test_boolean_out(self):
96 self.assertEqual(True, GIMarshallingTests.boolean_out_true())
97 self.assertEqual(False, GIMarshallingTests.boolean_out_false())
99 def test_boolean_inout(self):
100 self.assertEqual(False, GIMarshallingTests.boolean_inout_true_false(True))
101 self.assertEqual(True, GIMarshallingTests.boolean_inout_false_true(False))
104 class TestInt8(unittest.TestCase):
106 MAX = GLib.MAXINT8
107 MIN = GLib.MININT8
109 def test_int8_return(self):
110 self.assertEqual(self.MAX, GIMarshallingTests.int8_return_max())
111 self.assertEqual(self.MIN, GIMarshallingTests.int8_return_min())
113 def test_int8_in(self):
114 max = Number(self.MAX)
115 min = Number(self.MIN)
117 GIMarshallingTests.int8_in_max(max)
118 GIMarshallingTests.int8_in_min(min)
120 max.value += 1
121 min.value -= 1
123 self.assertRaises(OverflowError, GIMarshallingTests.int8_in_max, max)
124 self.assertRaises(OverflowError, GIMarshallingTests.int8_in_min, min)
126 self.assertRaises(TypeError, GIMarshallingTests.int8_in_max, "self.MAX")
128 def test_int8_out(self):
129 self.assertEqual(self.MAX, GIMarshallingTests.int8_out_max())
130 self.assertEqual(self.MIN, GIMarshallingTests.int8_out_min())
132 def test_int8_inout(self):
133 self.assertEqual(self.MIN, GIMarshallingTests.int8_inout_max_min(Number(self.MAX)))
134 self.assertEqual(self.MAX, GIMarshallingTests.int8_inout_min_max(Number(self.MIN)))
137 class TestUInt8(unittest.TestCase):
139 MAX = GLib.MAXUINT8
141 def test_uint8_return(self):
142 self.assertEqual(self.MAX, GIMarshallingTests.uint8_return())
144 def test_uint8_in(self):
145 number = Number(self.MAX)
147 GIMarshallingTests.uint8_in(number)
148 GIMarshallingTests.uint8_in(CHAR_255)
150 number.value += 1
151 self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, number)
152 self.assertRaises(OverflowError, GIMarshallingTests.uint8_in, Number(-1))
154 self.assertRaises(TypeError, GIMarshallingTests.uint8_in, "self.MAX")
156 def test_uint8_out(self):
157 self.assertEqual(self.MAX, GIMarshallingTests.uint8_out())
159 def test_uint8_inout(self):
160 self.assertEqual(0, GIMarshallingTests.uint8_inout(Number(self.MAX)))
163 class TestInt16(unittest.TestCase):
165 MAX = GLib.MAXINT16
166 MIN = GLib.MININT16
168 def test_int16_return(self):
169 self.assertEqual(self.MAX, GIMarshallingTests.int16_return_max())
170 self.assertEqual(self.MIN, GIMarshallingTests.int16_return_min())
172 def test_int16_in(self):
173 max = Number(self.MAX)
174 min = Number(self.MIN)
176 GIMarshallingTests.int16_in_max(max)
177 GIMarshallingTests.int16_in_min(min)
179 max.value += 1
180 min.value -= 1
182 self.assertRaises(OverflowError, GIMarshallingTests.int16_in_max, max)
183 self.assertRaises(OverflowError, GIMarshallingTests.int16_in_min, min)
185 self.assertRaises(TypeError, GIMarshallingTests.int16_in_max, "self.MAX")
187 def test_int16_out(self):
188 self.assertEqual(self.MAX, GIMarshallingTests.int16_out_max())
189 self.assertEqual(self.MIN, GIMarshallingTests.int16_out_min())
191 def test_int16_inout(self):
192 self.assertEqual(self.MIN, GIMarshallingTests.int16_inout_max_min(Number(self.MAX)))
193 self.assertEqual(self.MAX, GIMarshallingTests.int16_inout_min_max(Number(self.MIN)))
196 class TestUInt16(unittest.TestCase):
198 MAX = GLib.MAXUINT16
200 def test_uint16_return(self):
201 self.assertEqual(self.MAX, GIMarshallingTests.uint16_return())
203 def test_uint16_in(self):
204 number = Number(self.MAX)
206 GIMarshallingTests.uint16_in(number)
208 number.value += 1
210 self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, number)
211 self.assertRaises(OverflowError, GIMarshallingTests.uint16_in, Number(-1))
213 self.assertRaises(TypeError, GIMarshallingTests.uint16_in, "self.MAX")
215 def test_uint16_out(self):
216 self.assertEqual(self.MAX, GIMarshallingTests.uint16_out())
218 def test_uint16_inout(self):
219 self.assertEqual(0, GIMarshallingTests.uint16_inout(Number(self.MAX)))
222 class TestInt32(unittest.TestCase):
224 MAX = GLib.MAXINT32
225 MIN = GLib.MININT32
227 def test_int32_return(self):
228 self.assertEqual(self.MAX, GIMarshallingTests.int32_return_max())
229 self.assertEqual(self.MIN, GIMarshallingTests.int32_return_min())
231 def test_int32_in(self):
232 max = Number(self.MAX)
233 min = Number(self.MIN)
235 GIMarshallingTests.int32_in_max(max)
236 GIMarshallingTests.int32_in_min(min)
238 max.value += 1
239 min.value -= 1
241 self.assertRaises(OverflowError, GIMarshallingTests.int32_in_max, max)
242 self.assertRaises(OverflowError, GIMarshallingTests.int32_in_min, min)
244 self.assertRaises(TypeError, GIMarshallingTests.int32_in_max, "self.MAX")
246 def test_int32_out(self):
247 self.assertEqual(self.MAX, GIMarshallingTests.int32_out_max())
248 self.assertEqual(self.MIN, GIMarshallingTests.int32_out_min())
250 def test_int32_inout(self):
251 self.assertEqual(self.MIN, GIMarshallingTests.int32_inout_max_min(Number(self.MAX)))
252 self.assertEqual(self.MAX, GIMarshallingTests.int32_inout_min_max(Number(self.MIN)))
255 class TestUInt32(unittest.TestCase):
257 MAX = GLib.MAXUINT32
259 def test_uint32_return(self):
260 self.assertEqual(self.MAX, GIMarshallingTests.uint32_return())
262 def test_uint32_in(self):
263 number = Number(self.MAX)
265 GIMarshallingTests.uint32_in(number)
267 number.value += 1
269 self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, number)
270 self.assertRaises(OverflowError, GIMarshallingTests.uint32_in, Number(-1))
272 self.assertRaises(TypeError, GIMarshallingTests.uint32_in, "self.MAX")
274 def test_uint32_out(self):
275 self.assertEqual(self.MAX, GIMarshallingTests.uint32_out())
277 def test_uint32_inout(self):
278 self.assertEqual(0, GIMarshallingTests.uint32_inout(Number(self.MAX)))
281 class TestInt64(unittest.TestCase):
283 MAX = 2 ** 63 - 1
284 MIN = - (2 ** 63)
286 def test_int64_return(self):
287 self.assertEqual(self.MAX, GIMarshallingTests.int64_return_max())
288 self.assertEqual(self.MIN, GIMarshallingTests.int64_return_min())
290 def test_int64_in(self):
291 max = Number(self.MAX)
292 min = Number(self.MIN)
294 GIMarshallingTests.int64_in_max(max)
295 GIMarshallingTests.int64_in_min(min)
297 max.value += 1
298 min.value -= 1
300 self.assertRaises(OverflowError, GIMarshallingTests.int64_in_max, max)
301 self.assertRaises(OverflowError, GIMarshallingTests.int64_in_min, min)
303 self.assertRaises(TypeError, GIMarshallingTests.int64_in_max, "self.MAX")
305 def test_int64_out(self):
306 self.assertEqual(self.MAX, GIMarshallingTests.int64_out_max())
307 self.assertEqual(self.MIN, GIMarshallingTests.int64_out_min())
309 def test_int64_inout(self):
310 self.assertEqual(self.MIN, GIMarshallingTests.int64_inout_max_min(Number(self.MAX)))
311 self.assertEqual(self.MAX, GIMarshallingTests.int64_inout_min_max(Number(self.MIN)))
314 class TestUInt64(unittest.TestCase):
316 MAX = 2 ** 64 - 1
318 def test_uint64_return(self):
319 self.assertEqual(self.MAX, GIMarshallingTests.uint64_return())
321 def test_uint64_in(self):
322 number = Number(self.MAX)
324 GIMarshallingTests.uint64_in(number)
326 number.value += 1
328 self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, number)
329 self.assertRaises(OverflowError, GIMarshallingTests.uint64_in, Number(-1))
331 self.assertRaises(TypeError, GIMarshallingTests.uint64_in, "self.MAX")
333 def test_uint64_out(self):
334 self.assertEqual(self.MAX, GIMarshallingTests.uint64_out())
336 def test_uint64_inout(self):
337 self.assertEqual(0, GIMarshallingTests.uint64_inout(Number(self.MAX)))
340 class TestShort(unittest.TestCase):
342 MAX = GLib.MAXSHORT
343 MIN = GLib.MINSHORT
345 def test_short_return(self):
346 self.assertEqual(self.MAX, GIMarshallingTests.short_return_max())
347 self.assertEqual(self.MIN, GIMarshallingTests.short_return_min())
349 def test_short_in(self):
350 max = Number(self.MAX)
351 min = Number(self.MIN)
353 GIMarshallingTests.short_in_max(max)
354 GIMarshallingTests.short_in_min(min)
356 max.value += 1
357 min.value -= 1
359 self.assertRaises(OverflowError, GIMarshallingTests.short_in_max, max)
360 self.assertRaises(OverflowError, GIMarshallingTests.short_in_min, min)
362 self.assertRaises(TypeError, GIMarshallingTests.short_in_max, "self.MAX")
364 def test_short_out(self):
365 self.assertEqual(self.MAX, GIMarshallingTests.short_out_max())
366 self.assertEqual(self.MIN, GIMarshallingTests.short_out_min())
368 def test_short_inout(self):
369 self.assertEqual(self.MIN, GIMarshallingTests.short_inout_max_min(Number(self.MAX)))
370 self.assertEqual(self.MAX, GIMarshallingTests.short_inout_min_max(Number(self.MIN)))
373 class TestUShort(unittest.TestCase):
375 MAX = GLib.MAXUSHORT
377 def test_ushort_return(self):
378 self.assertEqual(self.MAX, GIMarshallingTests.ushort_return())
380 def test_ushort_in(self):
381 number = Number(self.MAX)
383 GIMarshallingTests.ushort_in(number)
385 number.value += 1
387 self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, number)
388 self.assertRaises(OverflowError, GIMarshallingTests.ushort_in, Number(-1))
390 self.assertRaises(TypeError, GIMarshallingTests.ushort_in, "self.MAX")
392 def test_ushort_out(self):
393 self.assertEqual(self.MAX, GIMarshallingTests.ushort_out())
395 def test_ushort_inout(self):
396 self.assertEqual(0, GIMarshallingTests.ushort_inout(Number(self.MAX)))
399 class TestInt(unittest.TestCase):
401 MAX = GLib.MAXINT
402 MIN = GLib.MININT
404 def test_int_return(self):
405 self.assertEqual(self.MAX, GIMarshallingTests.int_return_max())
406 self.assertEqual(self.MIN, GIMarshallingTests.int_return_min())
408 def test_int_in(self):
409 max = Number(self.MAX)
410 min = Number(self.MIN)
412 GIMarshallingTests.int_in_max(max)
413 GIMarshallingTests.int_in_min(min)
415 max.value += 1
416 min.value -= 1
418 self.assertRaises(OverflowError, GIMarshallingTests.int_in_max, max)
419 self.assertRaises(OverflowError, GIMarshallingTests.int_in_min, min)
421 self.assertRaises(TypeError, GIMarshallingTests.int_in_max, "self.MAX")
423 def test_int_out(self):
424 self.assertEqual(self.MAX, GIMarshallingTests.int_out_max())
425 self.assertEqual(self.MIN, GIMarshallingTests.int_out_min())
427 def test_int_inout(self):
428 self.assertEqual(self.MIN, GIMarshallingTests.int_inout_max_min(Number(self.MAX)))
429 self.assertEqual(self.MAX, GIMarshallingTests.int_inout_min_max(Number(self.MIN)))
430 self.assertRaises(TypeError, GIMarshallingTests.int_inout_min_max, Number(self.MIN), CONSTANT_NUMBER)
433 class TestUInt(unittest.TestCase):
435 MAX = GLib.MAXUINT
437 def test_uint_return(self):
438 self.assertEqual(self.MAX, GIMarshallingTests.uint_return())
440 def test_uint_in(self):
441 number = Number(self.MAX)
443 GIMarshallingTests.uint_in(number)
445 number.value += 1
447 self.assertRaises(OverflowError, GIMarshallingTests.uint_in, number)
448 self.assertRaises(OverflowError, GIMarshallingTests.uint_in, Number(-1))
450 self.assertRaises(TypeError, GIMarshallingTests.uint_in, "self.MAX")
452 def test_uint_out(self):
453 self.assertEqual(self.MAX, GIMarshallingTests.uint_out())
455 def test_uint_inout(self):
456 self.assertEqual(0, GIMarshallingTests.uint_inout(Number(self.MAX)))
459 class TestLong(unittest.TestCase):
461 MAX = GLib.MAXLONG
462 MIN = GLib.MINLONG
464 def test_long_return(self):
465 self.assertEqual(self.MAX, GIMarshallingTests.long_return_max())
466 self.assertEqual(self.MIN, GIMarshallingTests.long_return_min())
468 def test_long_in(self):
469 max = Number(self.MAX)
470 min = Number(self.MIN)
472 GIMarshallingTests.long_in_max(max)
473 GIMarshallingTests.long_in_min(min)
475 max.value += 1
476 min.value -= 1
478 self.assertRaises(OverflowError, GIMarshallingTests.long_in_max, max)
479 self.assertRaises(OverflowError, GIMarshallingTests.long_in_min, min)
481 self.assertRaises(TypeError, GIMarshallingTests.long_in_max, "self.MAX")
483 def test_long_out(self):
484 self.assertEqual(self.MAX, GIMarshallingTests.long_out_max())
485 self.assertEqual(self.MIN, GIMarshallingTests.long_out_min())
487 def test_long_inout(self):
488 self.assertEqual(self.MIN, GIMarshallingTests.long_inout_max_min(Number(self.MAX)))
489 self.assertEqual(self.MAX, GIMarshallingTests.long_inout_min_max(Number(self.MIN)))
492 class TestULong(unittest.TestCase):
494 MAX = GLib.MAXULONG
496 def test_ulong_return(self):
497 self.assertEqual(self.MAX, GIMarshallingTests.ulong_return())
499 def test_ulong_in(self):
500 number = Number(self.MAX)
502 GIMarshallingTests.ulong_in(number)
504 number.value += 1
506 self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, number)
507 self.assertRaises(OverflowError, GIMarshallingTests.ulong_in, Number(-1))
509 self.assertRaises(TypeError, GIMarshallingTests.ulong_in, "self.MAX")
511 def test_ulong_out(self):
512 self.assertEqual(self.MAX, GIMarshallingTests.ulong_out())
514 def test_ulong_inout(self):
515 self.assertEqual(0, GIMarshallingTests.ulong_inout(Number(self.MAX)))
518 class TestSSize(unittest.TestCase):
520 MAX = GLib.MAXLONG
521 MIN = GLib.MINLONG
523 def test_ssize_return(self):
524 self.assertEqual(self.MAX, GIMarshallingTests.ssize_return_max())
525 self.assertEqual(self.MIN, GIMarshallingTests.ssize_return_min())
527 def test_ssize_in(self):
528 max = Number(self.MAX)
529 min = Number(self.MIN)
531 GIMarshallingTests.ssize_in_max(max)
532 GIMarshallingTests.ssize_in_min(min)
534 max.value += 1
535 min.value -= 1
537 self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_max, max)
538 self.assertRaises(OverflowError, GIMarshallingTests.ssize_in_min, min)
540 self.assertRaises(TypeError, GIMarshallingTests.ssize_in_max, "self.MAX")
542 def test_ssize_out(self):
543 self.assertEqual(self.MAX, GIMarshallingTests.ssize_out_max())
544 self.assertEqual(self.MIN, GIMarshallingTests.ssize_out_min())
546 def test_ssize_inout(self):
547 self.assertEqual(self.MIN, GIMarshallingTests.ssize_inout_max_min(Number(self.MAX)))
548 self.assertEqual(self.MAX, GIMarshallingTests.ssize_inout_min_max(Number(self.MIN)))
551 class TestSize(unittest.TestCase):
553 MAX = GLib.MAXULONG
555 def test_size_return(self):
556 self.assertEqual(self.MAX, GIMarshallingTests.size_return())
558 def test_size_in(self):
559 number = Number(self.MAX)
561 GIMarshallingTests.size_in(number)
563 number.value += 1
565 self.assertRaises(OverflowError, GIMarshallingTests.size_in, number)
566 self.assertRaises(OverflowError, GIMarshallingTests.size_in, Number(-1))
568 self.assertRaises(TypeError, GIMarshallingTests.size_in, "self.MAX")
570 def test_size_out(self):
571 self.assertEqual(self.MAX, GIMarshallingTests.size_out())
573 def test_size_inout(self):
574 self.assertEqual(0, GIMarshallingTests.size_inout(Number(self.MAX)))
577 class TestTimet(unittest.TestCase):
579 def test_time_t_return(self):
580 self.assertEqual(1234567890, GIMarshallingTests.time_t_return())
582 def test_time_t_in(self):
583 GIMarshallingTests.time_t_in(1234567890)
584 self.assertRaises(TypeError, GIMarshallingTests.time_t_in, "hello")
586 def test_time_t_out(self):
587 self.assertEqual(1234567890, GIMarshallingTests.time_t_out())
589 def test_time_t_inout(self):
590 self.assertEqual(0, GIMarshallingTests.time_t_inout(1234567890))
593 class TestFloat(unittest.TestCase):
595 MAX = GLib.MAXFLOAT
596 MIN = GLib.MINFLOAT
598 def test_float_return(self):
599 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_return())
601 def test_float_in(self):
602 GIMarshallingTests.float_in(Number(self.MAX))
604 self.assertRaises(TypeError, GIMarshallingTests.float_in, "self.MAX")
606 def test_float_out(self):
607 self.assertAlmostEqual(self.MAX, GIMarshallingTests.float_out())
609 def test_float_inout(self):
610 self.assertAlmostEqual(self.MIN, GIMarshallingTests.float_inout(Number(self.MAX)))
613 class TestDouble(unittest.TestCase):
615 MAX = GLib.MAXDOUBLE
616 MIN = GLib.MINDOUBLE
618 def test_double_return(self):
619 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_return())
621 def test_double_in(self):
622 GIMarshallingTests.double_in(Number(self.MAX))
624 self.assertRaises(TypeError, GIMarshallingTests.double_in, "self.MAX")
626 def test_double_out(self):
627 self.assertAlmostEqual(self.MAX, GIMarshallingTests.double_out())
629 def test_double_inout(self):
630 self.assertAlmostEqual(self.MIN, GIMarshallingTests.double_inout(Number(self.MAX)))
633 class TestGType(unittest.TestCase):
635 def test_gtype_name(self):
636 self.assertEqual("void", GObject.TYPE_NONE.name)
637 self.assertEqual("gchararray", GObject.TYPE_STRING.name)
639 def check_readonly(gtype):
640 gtype.name = "foo"
642 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_NONE)
643 self.assertRaises(AttributeError, check_readonly, GObject.TYPE_STRING)
645 def test_gtype_return(self):
646 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_return())
647 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_return())
649 def test_gtype_in(self):
650 GIMarshallingTests.gtype_in(GObject.TYPE_NONE)
651 GIMarshallingTests.gtype_string_in(GObject.TYPE_STRING)
652 self.assertRaises(TypeError, GIMarshallingTests.gtype_in, "foo")
653 self.assertRaises(TypeError, GIMarshallingTests.gtype_string_in, "foo")
655 def test_gtype_out(self):
656 self.assertEqual(GObject.TYPE_NONE, GIMarshallingTests.gtype_out())
657 self.assertEqual(GObject.TYPE_STRING, GIMarshallingTests.gtype_string_out())
659 def test_gtype_inout(self):
660 self.assertEqual(GObject.TYPE_INT, GIMarshallingTests.gtype_inout(GObject.TYPE_NONE))
663 class TestUtf8(unittest.TestCase):
665 def test_utf8_none_return(self):
666 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_return())
668 def test_utf8_full_return(self):
669 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_return())
671 def test_utf8_none_in(self):
672 GIMarshallingTests.utf8_none_in(CONSTANT_UTF8)
673 if sys.version_info < (3, 0):
674 GIMarshallingTests.utf8_none_in(PY2_UNICODE_UTF8)
676 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, CONSTANT_NUMBER)
677 self.assertRaises(TypeError, GIMarshallingTests.utf8_none_in, None)
679 def test_utf8_none_out(self):
680 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_none_out())
682 def test_utf8_full_out(self):
683 self.assertEqual(CONSTANT_UTF8, GIMarshallingTests.utf8_full_out())
685 def test_utf8_dangling_out(self):
686 GIMarshallingTests.utf8_dangling_out()
688 def test_utf8_none_inout(self):
689 self.assertEqual("", GIMarshallingTests.utf8_none_inout(CONSTANT_UTF8))
691 def test_utf8_full_inout(self):
692 self.assertEqual("", GIMarshallingTests.utf8_full_inout(CONSTANT_UTF8))
695 class TestFilename(unittest.TestCase):
696 def setUp(self):
697 self.workdir = tempfile.mkdtemp()
699 def tearDown(self):
700 shutil.rmtree(self.workdir)
702 def test_filename_in(self):
703 fname = os.path.join(self.workdir, _unicode('testäø.txt'))
704 self.assertRaises(GLib.GError, GLib.file_get_contents, fname)
706 with open(fname.encode('UTF-8'), 'wb') as f:
707 f.write(b'hello world!\n\x01\x02')
709 (result, contents) = GLib.file_get_contents(fname)
710 self.assertEqual(result, True)
711 self.assertEqual(contents, b'hello world!\n\x01\x02')
713 def test_filename_out(self):
714 self.assertRaises(GLib.GError, GLib.Dir.make_tmp, 'test')
716 dirname = GLib.Dir.make_tmp('testäø.XXXXXX')
717 self.assertTrue('/testäø.' in dirname, dirname)
718 dirname = _bytes(dirname)
719 self.assertTrue(os.path.isdir(dirname))
720 os.rmdir(dirname)
722 def test_filename_type_error(self):
723 self.assertRaises(TypeError, GLib.file_get_contents, 23)
726 class TestArray(unittest.TestCase):
728 def test_array_fixed_int_return(self):
729 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_int_return())
731 def test_array_fixed_short_return(self):
732 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_short_return())
734 def test_array_fixed_int_in(self):
735 GIMarshallingTests.array_fixed_int_in(Sequence([-1, 0, 1, 2]))
737 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, Sequence([-1, '0', 1, 2]))
739 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, 42)
740 self.assertRaises(TypeError, GIMarshallingTests.array_fixed_int_in, None)
742 def test_array_fixed_short_in(self):
743 GIMarshallingTests.array_fixed_short_in(Sequence([-1, 0, 1, 2]))
745 def test_array_fixed_out(self):
746 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_fixed_out())
748 def test_array_fixed_inout(self):
749 self.assertEqual([2, 1, 0, -1], GIMarshallingTests.array_fixed_inout([-1, 0, 1, 2]))
751 def test_array_return(self):
752 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_return())
754 def test_array_return_etc(self):
755 self.assertEqual(([5, 0, 1, 9], 14), GIMarshallingTests.array_return_etc(5, 9))
757 def test_array_in(self):
758 GIMarshallingTests.array_in(Sequence([-1, 0, 1, 2]))
759 GIMarshallingTests.array_in_guint64_len(Sequence([-1, 0, 1, 2]))
760 GIMarshallingTests.array_in_guint8_len(Sequence([-1, 0, 1, 2]))
762 def test_array_in_len_before(self):
763 GIMarshallingTests.array_in_len_before(Sequence([-1, 0, 1, 2]))
765 def test_array_in_len_zero_terminated(self):
766 GIMarshallingTests.array_in_len_zero_terminated(Sequence([-1, 0, 1, 2]))
768 def test_array_uint8_in(self):
769 GIMarshallingTests.array_uint8_in(Sequence([97, 98, 99, 100]))
770 GIMarshallingTests.array_uint8_in(_bytes("abcd"))
772 def test_array_string_in(self):
773 GIMarshallingTests.array_string_in(['foo', 'bar'])
775 def test_array_out(self):
776 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.array_out())
778 def test_array_out_etc(self):
779 self.assertEqual(([-5, 0, 1, 9], 4), GIMarshallingTests.array_out_etc(-5, 9))
781 def test_array_inout(self):
782 self.assertEqual([-2, -1, 0, 1, 2], GIMarshallingTests.array_inout(Sequence([-1, 0, 1, 2])))
784 def test_array_inout_etc(self):
785 self.assertEqual(([-5, -1, 0, 1, 9], 4),
786 GIMarshallingTests.array_inout_etc(-5, Sequence([-1, 0, 1, 2]), 9))
788 def test_method_array_in(self):
789 object_ = GIMarshallingTests.Object()
790 object_.method_array_in(Sequence([-1, 0, 1, 2]))
792 def test_method_array_out(self):
793 object_ = GIMarshallingTests.Object()
794 self.assertEqual([-1, 0, 1, 2], object_.method_array_out())
796 def test_method_array_inout(self):
797 object_ = GIMarshallingTests.Object()
798 self.assertEqual([-2, -1, 0, 1, 2], object_.method_array_inout(Sequence([-1, 0, 1, 2])))
800 def test_method_array_return(self):
801 object_ = GIMarshallingTests.Object()
802 self.assertEqual([-1, 0, 1, 2], object_.method_array_return())
804 def test_array_enum_in(self):
805 GIMarshallingTests.array_enum_in([GIMarshallingTests.Enum.VALUE1,
806 GIMarshallingTests.Enum.VALUE2,
807 GIMarshallingTests.Enum.VALUE3])
809 def test_array_boxed_struct_in(self):
810 struct1 = GIMarshallingTests.BoxedStruct()
811 struct1.long_ = 1
812 struct2 = GIMarshallingTests.BoxedStruct()
813 struct2.long_ = 2
814 struct3 = GIMarshallingTests.BoxedStruct()
815 struct3.long_ = 3
817 GIMarshallingTests.array_struct_in([struct1, struct2, struct3])
819 def test_array_boxed_struct_in_item_marshal_failure(self):
820 struct1 = GIMarshallingTests.BoxedStruct()
821 struct1.long_ = 1
822 struct2 = GIMarshallingTests.BoxedStruct()
823 struct2.long_ = 2
825 self.assertRaises(TypeError, GIMarshallingTests.array_struct_in,
826 [struct1, struct2, 'not_a_struct'])
828 def test_array_boxed_struct_value_in(self):
829 struct1 = GIMarshallingTests.BoxedStruct()
830 struct1.long_ = 1
831 struct2 = GIMarshallingTests.BoxedStruct()
832 struct2.long_ = 2
833 struct3 = GIMarshallingTests.BoxedStruct()
834 struct3.long_ = 3
836 GIMarshallingTests.array_struct_value_in([struct1, struct2, struct3])
838 def test_array_boxed_struct_value_in_item_marshal_failure(self):
839 struct1 = GIMarshallingTests.BoxedStruct()
840 struct1.long_ = 1
841 struct2 = GIMarshallingTests.BoxedStruct()
842 struct2.long_ = 2
844 self.assertRaises(TypeError, GIMarshallingTests.array_struct_value_in,
845 [struct1, struct2, 'not_a_struct'])
847 def test_array_boxed_struct_take_in(self):
848 struct1 = GIMarshallingTests.BoxedStruct()
849 struct1.long_ = 1
850 struct2 = GIMarshallingTests.BoxedStruct()
851 struct2.long_ = 2
852 struct3 = GIMarshallingTests.BoxedStruct()
853 struct3.long_ = 3
855 GIMarshallingTests.array_struct_take_in([struct1, struct2, struct3])
857 self.assertEqual(1, struct1.long_)
859 def test_array_boxed_struct_return(self):
860 (struct1, struct2, struct3) = GIMarshallingTests.array_zero_terminated_return_struct()
861 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct1))
862 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct2))
863 self.assertEqual(GIMarshallingTests.BoxedStruct, type(struct3))
864 self.assertEqual(42, struct1.long_)
865 self.assertEqual(43, struct2.long_)
866 self.assertEqual(44, struct3.long_)
868 def test_array_simple_struct_in(self):
869 struct1 = GIMarshallingTests.SimpleStruct()
870 struct1.long_ = 1
871 struct2 = GIMarshallingTests.SimpleStruct()
872 struct2.long_ = 2
873 struct3 = GIMarshallingTests.SimpleStruct()
874 struct3.long_ = 3
876 GIMarshallingTests.array_simple_struct_in([struct1, struct2, struct3])
878 def test_array_simple_struct_in_item_marshal_failure(self):
879 struct1 = GIMarshallingTests.SimpleStruct()
880 struct1.long_ = 1
881 struct2 = GIMarshallingTests.SimpleStruct()
882 struct2.long_ = 2
884 self.assertRaises(TypeError, GIMarshallingTests.array_simple_struct_in,
885 [struct1, struct2, 'not_a_struct'])
887 def test_array_multi_array_key_value_in(self):
888 GIMarshallingTests.multi_array_key_value_in(["one", "two", "three"],
889 [1, 2, 3])
891 def test_array_in_nonzero_nonlen(self):
892 GIMarshallingTests.array_in_nonzero_nonlen(1, b'abcd')
894 def test_array_fixed_out_struct(self):
895 struct1, struct2 = GIMarshallingTests.array_fixed_out_struct()
897 self.assertEqual(7, struct1.long_)
898 self.assertEqual(6, struct1.int8)
899 self.assertEqual(6, struct2.long_)
900 self.assertEqual(7, struct2.int8)
902 def test_array_zero_terminated_return(self):
903 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_return())
905 def test_array_zero_terminated_return_null(self):
906 self.assertEqual([], GIMarshallingTests.array_zero_terminated_return_null())
908 def test_array_zero_terminated_in(self):
909 GIMarshallingTests.array_zero_terminated_in(Sequence(['0', '1', '2']))
911 def test_array_zero_terminated_out(self):
912 self.assertEqual(['0', '1', '2'], GIMarshallingTests.array_zero_terminated_out())
914 def test_array_zero_terminated_inout(self):
915 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.array_zero_terminated_inout(['0', '1', '2']))
917 def test_init_function(self):
918 self.assertEqual((True, []), GIMarshallingTests.init_function([]))
919 self.assertEqual((True, []), GIMarshallingTests.init_function(['hello']))
920 self.assertEqual((True, ['hello']),
921 GIMarshallingTests.init_function(['hello', 'world']))
924 class TestGStrv(unittest.TestCase):
926 def test_gstrv_return(self):
927 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_return())
929 def test_gstrv_in(self):
930 GIMarshallingTests.gstrv_in(Sequence(['0', '1', '2']))
932 def test_gstrv_out(self):
933 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gstrv_out())
935 def test_gstrv_inout(self):
936 self.assertEqual(['-1', '0', '1', '2'], GIMarshallingTests.gstrv_inout(['0', '1', '2']))
939 class TestArrayGVariant(unittest.TestCase):
941 def test_array_gvariant_none_in(self):
942 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
943 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_none_in(v)]
944 self.assertEqual([27, "Hello"], returned)
946 def test_array_gvariant_container_in(self):
947 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
948 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_container_in(v)]
949 self.assertEqual([27, "Hello"], returned)
951 def test_array_gvariant_full_in(self):
952 v = [GLib.Variant("i", 27), GLib.Variant("s", "Hello")]
953 returned = [GLib.Variant.unpack(r) for r in GIMarshallingTests.array_gvariant_full_in(v)]
954 self.assertEqual([27, "Hello"], returned)
956 def test_bytearray_gvariant(self):
957 v = GLib.Variant.new_bytestring(b"foo")
958 self.assertEqual(v.get_bytestring(), b"foo")
961 class TestGArray(unittest.TestCase):
963 def test_garray_int_none_return(self):
964 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.garray_int_none_return())
966 def test_garray_uint64_none_return(self):
967 self.assertEqual([0, GLib.MAXUINT64], GIMarshallingTests.garray_uint64_none_return())
969 def test_garray_utf8_none_return(self):
970 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_return())
972 def test_garray_utf8_container_return(self):
973 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_return())
975 def test_garray_utf8_full_return(self):
976 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_return())
978 def test_garray_int_none_in(self):
979 GIMarshallingTests.garray_int_none_in(Sequence([-1, 0, 1, 2]))
981 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, Sequence([-1, '0', 1, 2]))
983 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, 42)
984 self.assertRaises(TypeError, GIMarshallingTests.garray_int_none_in, None)
986 def test_garray_uint64_none_in(self):
987 GIMarshallingTests.garray_uint64_none_in(Sequence([0, GLib.MAXUINT64]))
989 def test_garray_utf8_none_in(self):
990 GIMarshallingTests.garray_utf8_none_in(Sequence(['0', '1', '2']))
992 def test_garray_utf8_none_out(self):
993 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_none_out())
995 def test_garray_utf8_container_out(self):
996 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_container_out())
998 def test_garray_utf8_full_out(self):
999 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out())
1001 def test_garray_utf8_full_out_caller_allocated(self):
1002 self.assertEqual(['0', '1', '2'], GIMarshallingTests.garray_utf8_full_out_caller_allocated())
1004 def test_garray_utf8_none_inout(self):
1005 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_none_inout(Sequence(('0', '1', '2'))))
1007 def test_garray_utf8_container_inout(self):
1008 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_container_inout(['0', '1', '2']))
1010 def test_garray_utf8_full_inout(self):
1011 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.garray_utf8_full_inout(['0', '1', '2']))
1014 class TestGPtrArray(unittest.TestCase):
1016 def test_gptrarray_utf8_none_return(self):
1017 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_return())
1019 def test_gptrarray_utf8_container_return(self):
1020 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_return())
1022 def test_gptrarray_utf8_full_return(self):
1023 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_return())
1025 def test_gptrarray_utf8_none_in(self):
1026 GIMarshallingTests.gptrarray_utf8_none_in(Sequence(['0', '1', '2']))
1028 def test_gptrarray_utf8_none_out(self):
1029 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_none_out())
1031 def test_gptrarray_utf8_container_out(self):
1032 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_container_out())
1034 def test_gptrarray_utf8_full_out(self):
1035 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gptrarray_utf8_full_out())
1037 def test_gptrarray_utf8_none_inout(self):
1038 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_none_inout(Sequence(('0', '1', '2'))))
1040 def test_gptrarray_utf8_container_inout(self):
1041 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_container_inout(['0', '1', '2']))
1043 def test_gptrarray_utf8_full_inout(self):
1044 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gptrarray_utf8_full_inout(['0', '1', '2']))
1047 class TestGBytes(unittest.TestCase):
1048 def test_gbytes_create(self):
1049 b = GLib.Bytes.new(b'\x00\x01\xFF')
1050 self.assertEqual(3, b.get_size())
1051 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1053 def test_gbytes_create_take(self):
1054 b = GLib.Bytes.new_take(b'\x00\x01\xFF')
1055 self.assertEqual(3, b.get_size())
1056 self.assertEqual(b'\x00\x01\xFF', b.get_data())
1058 def test_gbytes_full_return(self):
1059 b = GIMarshallingTests.gbytes_full_return()
1060 self.assertEqual(4, b.get_size())
1061 self.assertEqual(b'\x00\x31\xFF\x33', b.get_data())
1063 def test_gbytes_none_in(self):
1064 b = GIMarshallingTests.gbytes_full_return()
1065 GIMarshallingTests.gbytes_none_in(b)
1067 def test_compare(self):
1068 a1 = GLib.Bytes.new(b'\x00\x01\xFF')
1069 a2 = GLib.Bytes.new(b'\x00\x01\xFF')
1070 b = GLib.Bytes.new(b'\x00\x01\xFE')
1072 self.assertTrue(a1.equal(a2))
1073 self.assertTrue(a2.equal(a1))
1074 self.assertFalse(a1.equal(b))
1075 self.assertFalse(b.equal(a2))
1077 self.assertEqual(0, a1.compare(a2))
1078 self.assertLess(0, a1.compare(b))
1079 self.assertGreater(0, b.compare(a1))
1082 class TestGByteArray(unittest.TestCase):
1083 def test_new(self):
1084 ba = GLib.ByteArray.new()
1085 self.assertEqual(b'', ba)
1087 ba = GLib.ByteArray.new_take(b'\x01\x02\xFF')
1088 self.assertEqual(b'\x01\x02\xFF', ba)
1090 def test_bytearray_full_return(self):
1091 self.assertEqual(b'\x001\xFF3', GIMarshallingTests.bytearray_full_return())
1093 def test_bytearray_none_in(self):
1094 b = b'\x00\x31\xFF\x33'
1095 ba = GLib.ByteArray.new_take(b)
1097 # b should always have the same value even
1098 # though the generated GByteArray is being modified
1099 GIMarshallingTests.bytearray_none_in(b)
1100 GIMarshallingTests.bytearray_none_in(b)
1102 # The GByteArray is just a bytes
1103 # thus it will not reflect any changes
1104 GIMarshallingTests.bytearray_none_in(ba)
1105 GIMarshallingTests.bytearray_none_in(ba)
1108 class TestGList(unittest.TestCase):
1110 def test_glist_int_none_return(self):
1111 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.glist_int_none_return())
1113 def test_glist_uint32_none_return(self):
1114 self.assertEqual([0, GLib.MAXUINT32], GIMarshallingTests.glist_uint32_none_return())
1116 def test_glist_utf8_none_return(self):
1117 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_return())
1119 def test_glist_utf8_container_return(self):
1120 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_return())
1122 def test_glist_utf8_full_return(self):
1123 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_return())
1125 def test_glist_int_none_in(self):
1126 GIMarshallingTests.glist_int_none_in(Sequence((-1, 0, 1, 2)))
1128 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, Sequence((-1, '0', 1, 2)))
1130 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, 42)
1131 self.assertRaises(TypeError, GIMarshallingTests.glist_int_none_in, None)
1133 def test_glist_int_none_in_error_getitem(self):
1135 class FailingSequence(Sequence):
1136 def __getitem__(self, key):
1137 raise Exception
1139 self.assertRaises(Exception, GIMarshallingTests.glist_int_none_in, FailingSequence((-1, 0, 1, 2)))
1141 def test_glist_uint32_none_in(self):
1142 GIMarshallingTests.glist_uint32_none_in(Sequence((0, GLib.MAXUINT32)))
1144 def test_glist_utf8_none_in(self):
1145 GIMarshallingTests.glist_utf8_none_in(Sequence(('0', '1', '2')))
1147 def test_glist_utf8_none_out(self):
1148 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_none_out())
1150 def test_glist_utf8_container_out(self):
1151 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_container_out())
1153 def test_glist_utf8_full_out(self):
1154 self.assertEqual(['0', '1', '2'], GIMarshallingTests.glist_utf8_full_out())
1156 def test_glist_utf8_none_inout(self):
1157 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_none_inout(Sequence(('0', '1', '2'))))
1159 def test_glist_utf8_container_inout(self):
1160 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_container_inout(('0', '1', '2')))
1162 def test_glist_utf8_full_inout(self):
1163 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.glist_utf8_full_inout(('0', '1', '2')))
1166 class TestGSList(unittest.TestCase):
1168 def test_gslist_int_none_return(self):
1169 self.assertEqual([-1, 0, 1, 2], GIMarshallingTests.gslist_int_none_return())
1171 def test_gslist_utf8_none_return(self):
1172 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_return())
1174 def test_gslist_utf8_container_return(self):
1175 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_return())
1177 def test_gslist_utf8_full_return(self):
1178 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_return())
1180 def test_gslist_int_none_in(self):
1181 GIMarshallingTests.gslist_int_none_in(Sequence((-1, 0, 1, 2)))
1183 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, Sequence((-1, '0', 1, 2)))
1185 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, 42)
1186 self.assertRaises(TypeError, GIMarshallingTests.gslist_int_none_in, None)
1188 def test_gslist_int_none_in_error_getitem(self):
1190 class FailingSequence(Sequence):
1191 def __getitem__(self, key):
1192 raise Exception
1194 self.assertRaises(Exception, GIMarshallingTests.gslist_int_none_in, FailingSequence((-1, 0, 1, 2)))
1196 def test_gslist_utf8_none_in(self):
1197 GIMarshallingTests.gslist_utf8_none_in(Sequence(('0', '1', '2')))
1199 def test_gslist_utf8_none_out(self):
1200 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_none_out())
1202 def test_gslist_utf8_container_out(self):
1203 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_container_out())
1205 def test_gslist_utf8_full_out(self):
1206 self.assertEqual(['0', '1', '2'], GIMarshallingTests.gslist_utf8_full_out())
1208 def test_gslist_utf8_none_inout(self):
1209 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_none_inout(Sequence(('0', '1', '2'))))
1211 def test_gslist_utf8_container_inout(self):
1212 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_container_inout(('0', '1', '2')))
1214 def test_gslist_utf8_full_inout(self):
1215 self.assertEqual(['-2', '-1', '0', '1'], GIMarshallingTests.gslist_utf8_full_inout(('0', '1', '2')))
1218 class TestGHashTable(unittest.TestCase):
1220 def test_ghashtable_int_none_return(self):
1221 self.assertEqual({-1: 1, 0: 0, 1: -1, 2: -2}, GIMarshallingTests.ghashtable_int_none_return())
1223 def test_ghashtable_int_none_return2(self):
1224 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_return())
1226 def test_ghashtable_int_container_return(self):
1227 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_return())
1229 def test_ghashtable_int_full_return(self):
1230 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_return())
1232 def test_ghashtable_int_none_in(self):
1233 GIMarshallingTests.ghashtable_int_none_in({-1: 1, 0: 0, 1: -1, 2: -2})
1235 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, '0': 0, 1: -1, 2: -2})
1236 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, {-1: 1, 0: '0', 1: -1, 2: -2})
1238 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, '{-1: 1, 0: 0, 1: -1, 2: -2}')
1239 self.assertRaises(TypeError, GIMarshallingTests.ghashtable_int_none_in, None)
1241 def test_ghashtable_utf8_none_in(self):
1242 GIMarshallingTests.ghashtable_utf8_none_in({'-1': '1', '0': '0', '1': '-1', '2': '-2'})
1244 def test_ghashtable_utf8_none_out(self):
1245 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_none_out())
1247 def test_ghashtable_utf8_container_out(self):
1248 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_container_out())
1250 def test_ghashtable_utf8_full_out(self):
1251 self.assertEqual({'-1': '1', '0': '0', '1': '-1', '2': '-2'}, GIMarshallingTests.ghashtable_utf8_full_out())
1253 def test_ghashtable_utf8_none_inout(self):
1254 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1255 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1256 GIMarshallingTests.ghashtable_utf8_none_inout(i))
1258 def test_ghashtable_utf8_container_inout(self):
1259 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1260 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1261 GIMarshallingTests.ghashtable_utf8_container_inout(i))
1263 def test_ghashtable_utf8_full_inout(self):
1264 i = {'-1': '1', '0': '0', '1': '-1', '2': '-2'}
1265 self.assertEqual({'-1': '1', '0': '0', '1': '1'},
1266 GIMarshallingTests.ghashtable_utf8_full_inout(i))
1269 class TestGValue(unittest.TestCase):
1271 def test_gvalue_return(self):
1272 self.assertEqual(42, GIMarshallingTests.gvalue_return())
1274 def test_gvalue_in(self):
1275 GIMarshallingTests.gvalue_in(42)
1276 value = GObject.Value(GObject.TYPE_INT, 42)
1277 GIMarshallingTests.gvalue_in(value)
1279 def test_gvalue_in_with_modification(self):
1280 value = GObject.Value(GObject.TYPE_INT, 42)
1281 GIMarshallingTests.gvalue_in_with_modification(value)
1282 self.assertEqual(value.get_int(), 24)
1284 def test_gvalue_int64_in(self):
1285 value = GObject.Value(GObject.TYPE_INT64, GLib.MAXINT64)
1286 GIMarshallingTests.gvalue_int64_in(value)
1288 def test_gvalue_in_with_type(self):
1289 value = GObject.Value(GObject.TYPE_STRING, 'foo')
1290 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_STRING)
1292 value = GObject.Value(GIMarshallingTests.Flags.__gtype__,
1293 GIMarshallingTests.Flags.VALUE1)
1294 GIMarshallingTests.gvalue_in_with_type(value, GObject.TYPE_FLAGS)
1296 def test_gvalue_in_enum(self):
1297 value = GObject.Value(GIMarshallingTests.Enum.__gtype__,
1298 GIMarshallingTests.Enum.VALUE3)
1299 GIMarshallingTests.gvalue_in_enum(value)
1301 def test_gvalue_out(self):
1302 self.assertEqual(42, GIMarshallingTests.gvalue_out())
1304 def test_gvalue_int64_out(self):
1305 self.assertEqual(GLib.MAXINT64, GIMarshallingTests.gvalue_int64_out())
1307 def test_gvalue_out_caller_allocates(self):
1308 self.assertEqual(42, GIMarshallingTests.gvalue_out_caller_allocates())
1310 def test_gvalue_inout(self):
1311 self.assertEqual('42', GIMarshallingTests.gvalue_inout(42))
1312 value = GObject.Value(int, 42)
1313 self.assertEqual('42', GIMarshallingTests.gvalue_inout(value))
1315 def test_gvalue_flat_array_in(self):
1316 # the function already asserts the correct values
1317 GIMarshallingTests.gvalue_flat_array([42, "42", True])
1319 def test_gvalue_flat_array_in_item_marshal_failure(self):
1320 # Tests the failure to marshal 2^256 to a GValue mid-way through the array marshaling.
1321 self.assertRaises(RuntimeError, GIMarshallingTests.gvalue_flat_array,
1322 [42, 2 ** 256, True])
1324 def test_gvalue_flat_array_out(self):
1325 values = GIMarshallingTests.return_gvalue_flat_array()
1326 self.assertEqual(values, [42, '42', True])
1328 def test_gvalue_gobject_ref_counts(self):
1329 # Tests a GObject held by a GValue
1330 obj = GObject.Object()
1331 ref = weakref.ref(obj)
1332 grefcount = obj.__grefcount__
1334 value = GObject.Value()
1335 value.init(GObject.TYPE_OBJECT)
1337 # TYPE_OBJECT will inc ref count as it should
1338 value.set_object(obj)
1339 self.assertEqual(obj.__grefcount__, grefcount + 1)
1341 # multiple set_object should not inc ref count
1342 value.set_object(obj)
1343 self.assertEqual(obj.__grefcount__, grefcount + 1)
1345 # get_object will re-use the same wrapper as obj
1346 res = value.get_object()
1347 self.assertEqual(obj, res)
1348 self.assertEqual(obj.__grefcount__, grefcount + 1)
1350 # multiple get_object should not inc ref count
1351 res = value.get_object()
1352 self.assertEqual(obj.__grefcount__, grefcount + 1)
1354 # deletion of the result and value holder should bring the
1355 # refcount back to where we started
1356 del res
1357 del value
1358 gc.collect()
1359 self.assertEqual(obj.__grefcount__, grefcount)
1361 del obj
1362 gc.collect()
1363 self.assertEqual(ref(), None)
1365 def test_gvalue_boxed_ref_counts(self):
1366 # Tests a boxed type wrapping a python object pointer (TYPE_PYOBJECT)
1367 # held by a GValue
1368 class Obj(object):
1369 pass
1371 obj = Obj()
1372 ref = weakref.ref(obj)
1373 refcount = sys.getrefcount(obj)
1375 value = GObject.Value()
1376 value.init(GObject.TYPE_PYOBJECT)
1378 # boxed TYPE_PYOBJECT will inc ref count as it should
1379 value.set_boxed(obj)
1380 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1382 # multiple set_boxed should not inc ref count
1383 value.set_boxed(obj)
1384 self.assertEqual(sys.getrefcount(obj), refcount + 1)
1386 res = value.get_boxed()
1387 self.assertEqual(obj, res)
1388 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1390 # multiple get_boxed should not inc ref count
1391 res = value.get_boxed()
1392 self.assertEqual(sys.getrefcount(obj), refcount + 2)
1394 # deletion of the result and value holder should bring the
1395 # refcount back to where we started
1396 del res
1397 del value
1398 gc.collect()
1399 self.assertEqual(sys.getrefcount(obj), refcount)
1401 del obj
1402 gc.collect()
1403 self.assertEqual(ref(), None)
1405 # FIXME: crashes
1406 def disabled_test_gvalue_flat_array_round_trip(self):
1407 self.assertEqual([42, '42', True],
1408 GIMarshallingTests.gvalue_flat_array_round_trip(42, '42', True))
1411 class TestGClosure(unittest.TestCase):
1413 def test_in(self):
1414 GIMarshallingTests.gclosure_in(lambda: 42)
1416 def test_pass(self):
1417 # test passing a closure between two C calls
1418 closure = GIMarshallingTests.gclosure_return()
1419 GIMarshallingTests.gclosure_in(closure)
1421 def test_type_error(self):
1422 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, 42)
1423 self.assertRaises(TypeError, GIMarshallingTests.gclosure_in, None)
1426 class TestCallbacks(unittest.TestCase):
1427 def test_return_value_only(self):
1428 def cb():
1429 return 5
1430 self.assertEqual(GIMarshallingTests.callback_return_value_only(cb), 5)
1432 def test_one_out_arg(self):
1433 def cb():
1434 return 5.5
1435 self.assertAlmostEqual(GIMarshallingTests.callback_one_out_parameter(cb), 5.5)
1437 def test_multiple_out_args(self):
1438 def cb():
1439 return (5.5, 42.0)
1440 res = GIMarshallingTests.callback_multiple_out_parameters(cb)
1441 self.assertAlmostEqual(res[0], 5.5)
1442 self.assertAlmostEqual(res[1], 42.0)
1444 def test_return_and_one_out_arg(self):
1445 def cb():
1446 return (5, 42.0)
1447 res = GIMarshallingTests.callback_return_value_and_one_out_parameter(cb)
1448 self.assertEqual(res[0], 5)
1449 self.assertAlmostEqual(res[1], 42.0)
1451 def test_return_and_multiple_out_arg(self):
1452 def cb():
1453 return (5, 42, -1000)
1454 self.assertEqual(GIMarshallingTests.callback_return_value_and_multiple_out_parameters(cb),
1455 (5, 42, -1000))
1458 class TestPointer(unittest.TestCase):
1459 def test_pointer_in_return(self):
1460 self.assertEqual(GIMarshallingTests.pointer_in_return(42), 42)
1463 class TestEnum(unittest.TestCase):
1465 @classmethod
1466 def setUpClass(cls):
1467 '''Run tests under a test locale.
1469 Upper case conversion of member names should not be locale specific
1470 e. g. in Turkish, "i".upper() == "i", which gives results like "iNVALiD"
1472 Run test under a locale which defines toupper('a') == 'a'
1474 cls.locale_dir = tempfile.mkdtemp()
1475 src = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'te_ST@nouppera')
1476 dest = os.path.join(cls.locale_dir, 'te_ST.UTF-8@nouppera')
1477 subprocess.check_call(['localedef', '-i', src, '-c', '-f', 'UTF-8', dest])
1478 os.environ['LOCPATH'] = cls.locale_dir
1479 locale.setlocale(locale.LC_ALL, 'te_ST.UTF-8@nouppera')
1481 @classmethod
1482 def tearDownClass(cls):
1483 locale.setlocale(locale.LC_ALL, 'C')
1484 shutil.rmtree(cls.locale_dir)
1485 try:
1486 del os.environ['LOCPATH']
1487 except KeyError:
1488 pass
1490 def test_enum(self):
1491 self.assertTrue(issubclass(GIMarshallingTests.Enum, int))
1492 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE1, GIMarshallingTests.Enum))
1493 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE2, GIMarshallingTests.Enum))
1494 self.assertTrue(isinstance(GIMarshallingTests.Enum.VALUE3, GIMarshallingTests.Enum))
1495 self.assertEqual(42, GIMarshallingTests.Enum.VALUE3)
1497 def test_value_nick_and_name(self):
1498 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_nick, 'value1')
1499 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_nick, 'value2')
1500 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_nick, 'value3')
1502 self.assertEqual(GIMarshallingTests.Enum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE1')
1503 self.assertEqual(GIMarshallingTests.Enum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE2')
1504 self.assertEqual(GIMarshallingTests.Enum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_ENUM_VALUE3')
1506 def test_enum_in(self):
1507 GIMarshallingTests.enum_in(GIMarshallingTests.Enum.VALUE3)
1508 GIMarshallingTests.enum_in(42)
1510 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 43)
1511 self.assertRaises(TypeError, GIMarshallingTests.enum_in, 'GIMarshallingTests.Enum.VALUE3')
1513 def test_enum_return(self):
1514 enum = GIMarshallingTests.enum_returnv()
1515 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1516 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1518 def test_enum_out(self):
1519 enum = GIMarshallingTests.enum_out()
1520 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1521 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE3)
1523 def test_enum_inout(self):
1524 enum = GIMarshallingTests.enum_inout(GIMarshallingTests.Enum.VALUE3)
1525 self.assertTrue(isinstance(enum, GIMarshallingTests.Enum))
1526 self.assertEqual(enum, GIMarshallingTests.Enum.VALUE1)
1528 def test_enum_second(self):
1529 # check for the bug where different non-gtype enums share the same class
1530 self.assertNotEqual(GIMarshallingTests.Enum, GIMarshallingTests.SecondEnum)
1532 # check that values are not being shared between different enums
1533 self.assertTrue(hasattr(GIMarshallingTests.SecondEnum, "SECONDVALUE1"))
1534 self.assertRaises(AttributeError, getattr, GIMarshallingTests.Enum, "SECONDVALUE1")
1535 self.assertTrue(hasattr(GIMarshallingTests.Enum, "VALUE1"))
1536 self.assertRaises(AttributeError, getattr, GIMarshallingTests.SecondEnum, "VALUE1")
1538 def test_enum_gtype_name_is_namespaced(self):
1539 self.assertEqual(GIMarshallingTests.Enum.__gtype__.name,
1540 'PyGIMarshallingTestsEnum')
1542 def test_enum_add_type_error(self):
1543 self.assertRaises(TypeError,
1544 gi._gi.enum_add,
1545 GIMarshallingTests.NoTypeFlags.__gtype__)
1547 def test_type_module_name(self):
1548 self.assertEqual(GIMarshallingTests.Enum.__name__, "Enum")
1549 self.assertEqual(GIMarshallingTests.Enum.__module__,
1550 "gi.repository.GIMarshallingTests")
1552 def test_repr(self):
1553 self.assertEqual(repr(GIMarshallingTests.Enum.VALUE3),
1554 "<enum GI_MARSHALLING_TESTS_ENUM_VALUE3 of type "
1555 "GIMarshallingTests.Enum>")
1558 class TestEnumVFuncResults(unittest.TestCase):
1559 class EnumTester(GIMarshallingTests.Object):
1560 def do_vfunc_return_enum(self):
1561 return GIMarshallingTests.Enum.VALUE2
1563 def do_vfunc_out_enum(self):
1564 return GIMarshallingTests.Enum.VALUE3
1566 def test_vfunc_return_enum(self):
1567 tester = self.EnumTester()
1568 self.assertEqual(tester.vfunc_return_enum(), GIMarshallingTests.Enum.VALUE2)
1570 def test_vfunc_out_enum(self):
1571 tester = self.EnumTester()
1572 self.assertEqual(tester.vfunc_out_enum(), GIMarshallingTests.Enum.VALUE3)
1575 class TestGEnum(unittest.TestCase):
1577 def test_genum(self):
1578 self.assertTrue(issubclass(GIMarshallingTests.GEnum, GObject.GEnum))
1579 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE1, GIMarshallingTests.GEnum))
1580 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE2, GIMarshallingTests.GEnum))
1581 self.assertTrue(isinstance(GIMarshallingTests.GEnum.VALUE3, GIMarshallingTests.GEnum))
1582 self.assertEqual(42, GIMarshallingTests.GEnum.VALUE3)
1584 def test_value_nick_and_name(self):
1585 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_nick, 'value1')
1586 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_nick, 'value2')
1587 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_nick, 'value3')
1589 self.assertEqual(GIMarshallingTests.GEnum.VALUE1.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE1')
1590 self.assertEqual(GIMarshallingTests.GEnum.VALUE2.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE2')
1591 self.assertEqual(GIMarshallingTests.GEnum.VALUE3.value_name, 'GI_MARSHALLING_TESTS_GENUM_VALUE3')
1593 def test_genum_in(self):
1594 GIMarshallingTests.genum_in(GIMarshallingTests.GEnum.VALUE3)
1595 GIMarshallingTests.genum_in(42)
1596 GIMarshallingTests.GEnum.in_(42)
1598 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 43)
1599 self.assertRaises(TypeError, GIMarshallingTests.genum_in, 'GIMarshallingTests.GEnum.VALUE3')
1601 def test_genum_return(self):
1602 genum = GIMarshallingTests.genum_returnv()
1603 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1604 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1606 def test_genum_out(self):
1607 genum = GIMarshallingTests.genum_out()
1608 genum = GIMarshallingTests.GEnum.out()
1609 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1610 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE3)
1612 def test_genum_inout(self):
1613 genum = GIMarshallingTests.genum_inout(GIMarshallingTests.GEnum.VALUE3)
1614 self.assertTrue(isinstance(genum, GIMarshallingTests.GEnum))
1615 self.assertEqual(genum, GIMarshallingTests.GEnum.VALUE1)
1617 def test_type_module_name(self):
1618 self.assertEqual(GIMarshallingTests.GEnum.__name__, "GEnum")
1619 self.assertEqual(GIMarshallingTests.GEnum.__module__,
1620 "gi.repository.GIMarshallingTests")
1622 def test_repr(self):
1623 self.assertEqual(repr(GIMarshallingTests.GEnum.VALUE3),
1624 "<enum GI_MARSHALLING_TESTS_GENUM_VALUE3 of type "
1625 "GIMarshallingTests.GEnum>")
1628 class TestGFlags(unittest.TestCase):
1630 def test_flags(self):
1631 self.assertTrue(issubclass(GIMarshallingTests.Flags, GObject.GFlags))
1632 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1, GIMarshallingTests.Flags))
1633 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE2, GIMarshallingTests.Flags))
1634 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE3, GIMarshallingTests.Flags))
1635 # __or__() operation should still return an instance, not an int.
1636 self.assertTrue(isinstance(GIMarshallingTests.Flags.VALUE1 | GIMarshallingTests.Flags.VALUE2,
1637 GIMarshallingTests.Flags))
1638 self.assertEqual(1 << 1, GIMarshallingTests.Flags.VALUE2)
1640 def test_value_nick_and_name(self):
1641 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_nick, 'value1')
1642 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_nick, 'value2')
1643 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_nick, 'value3')
1645 self.assertEqual(GIMarshallingTests.Flags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE1')
1646 self.assertEqual(GIMarshallingTests.Flags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE2')
1647 self.assertEqual(GIMarshallingTests.Flags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_FLAGS_VALUE3')
1649 def test_flags_in(self):
1650 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2)
1651 GIMarshallingTests.Flags.in_(GIMarshallingTests.Flags.VALUE2)
1652 # result of __or__() operation should still be valid instance, not an int.
1653 GIMarshallingTests.flags_in(GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE2)
1654 GIMarshallingTests.flags_in_zero(Number(0))
1655 GIMarshallingTests.Flags.in_zero(Number(0))
1657 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 1 << 1)
1658 self.assertRaises(TypeError, GIMarshallingTests.flags_in, 'GIMarshallingTests.Flags.VALUE2')
1660 def test_flags_return(self):
1661 flags = GIMarshallingTests.flags_returnv()
1662 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1663 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1665 def test_flags_return_method(self):
1666 flags = GIMarshallingTests.Flags.returnv()
1667 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1668 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1670 def test_flags_out(self):
1671 flags = GIMarshallingTests.flags_out()
1672 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1673 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE2)
1675 def test_flags_inout(self):
1676 flags = GIMarshallingTests.flags_inout(GIMarshallingTests.Flags.VALUE2)
1677 self.assertTrue(isinstance(flags, GIMarshallingTests.Flags))
1678 self.assertEqual(flags, GIMarshallingTests.Flags.VALUE1)
1680 def test_type_module_name(self):
1681 self.assertEqual(GIMarshallingTests.Flags.__name__, "Flags")
1682 self.assertEqual(GIMarshallingTests.Flags.__module__,
1683 "gi.repository.GIMarshallingTests")
1685 def test_repr(self):
1686 self.assertEqual(repr(GIMarshallingTests.Flags.VALUE2),
1687 "<flags GI_MARSHALLING_TESTS_FLAGS_VALUE2 of type "
1688 "GIMarshallingTests.Flags>")
1691 class TestNoTypeFlags(unittest.TestCase):
1693 def test_flags(self):
1694 self.assertTrue(issubclass(GIMarshallingTests.NoTypeFlags, GObject.GFlags))
1695 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1, GIMarshallingTests.NoTypeFlags))
1696 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE2, GIMarshallingTests.NoTypeFlags))
1697 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE3, GIMarshallingTests.NoTypeFlags))
1698 # __or__() operation should still return an instance, not an int.
1699 self.assertTrue(isinstance(GIMarshallingTests.NoTypeFlags.VALUE1 | GIMarshallingTests.NoTypeFlags.VALUE2,
1700 GIMarshallingTests.NoTypeFlags))
1701 self.assertEqual(1 << 1, GIMarshallingTests.NoTypeFlags.VALUE2)
1703 def test_value_nick_and_name(self):
1704 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_nick, 'value1')
1705 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_nick, 'value2')
1706 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_nick, 'value3')
1708 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE1.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE1')
1709 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE2.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2')
1710 self.assertEqual(GIMarshallingTests.NoTypeFlags.VALUE3.first_value_name, 'GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE3')
1712 def test_flags_in(self):
1713 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2)
1714 GIMarshallingTests.no_type_flags_in(GIMarshallingTests.NoTypeFlags.VALUE2 | GIMarshallingTests.NoTypeFlags.VALUE2)
1715 GIMarshallingTests.no_type_flags_in_zero(Number(0))
1717 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 1 << 1)
1718 self.assertRaises(TypeError, GIMarshallingTests.no_type_flags_in, 'GIMarshallingTests.NoTypeFlags.VALUE2')
1720 def test_flags_return(self):
1721 flags = GIMarshallingTests.no_type_flags_returnv()
1722 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1723 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1725 def test_flags_out(self):
1726 flags = GIMarshallingTests.no_type_flags_out()
1727 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1728 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE2)
1730 def test_flags_inout(self):
1731 flags = GIMarshallingTests.no_type_flags_inout(GIMarshallingTests.NoTypeFlags.VALUE2)
1732 self.assertTrue(isinstance(flags, GIMarshallingTests.NoTypeFlags))
1733 self.assertEqual(flags, GIMarshallingTests.NoTypeFlags.VALUE1)
1735 def test_flags_gtype_name_is_namespaced(self):
1736 self.assertEqual(GIMarshallingTests.NoTypeFlags.__gtype__.name,
1737 'PyGIMarshallingTestsNoTypeFlags')
1739 def test_type_module_name(self):
1740 self.assertEqual(GIMarshallingTests.NoTypeFlags.__name__,
1741 "NoTypeFlags")
1742 self.assertEqual(GIMarshallingTests.NoTypeFlags.__module__,
1743 "gi.repository.GIMarshallingTests")
1745 def test_repr(self):
1746 self.assertEqual(repr(GIMarshallingTests.NoTypeFlags.VALUE2),
1747 "<flags GI_MARSHALLING_TESTS_NO_TYPE_FLAGS_VALUE2 of "
1748 "type GIMarshallingTests.NoTypeFlags>")
1751 class TestStructure(unittest.TestCase):
1753 def test_simple_struct(self):
1754 self.assertTrue(issubclass(GIMarshallingTests.SimpleStruct, GObject.GPointer))
1756 struct = GIMarshallingTests.SimpleStruct()
1757 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1759 self.assertEqual(0, struct.long_)
1760 self.assertEqual(0, struct.int8)
1762 struct.long_ = 6
1763 struct.int8 = 7
1765 self.assertEqual(6, struct.long_)
1766 self.assertEqual(7, struct.int8)
1768 del struct
1770 def test_nested_struct(self):
1771 struct = GIMarshallingTests.NestedStruct()
1773 self.assertTrue(isinstance(struct.simple_struct, GIMarshallingTests.SimpleStruct))
1775 struct.simple_struct.long_ = 42
1776 self.assertEqual(42, struct.simple_struct.long_)
1778 del struct
1780 def test_not_simple_struct(self):
1781 struct = GIMarshallingTests.NotSimpleStruct()
1782 self.assertEqual(None, struct.pointer)
1784 def test_simple_struct_return(self):
1785 struct = GIMarshallingTests.simple_struct_returnv()
1787 self.assertTrue(isinstance(struct, GIMarshallingTests.SimpleStruct))
1788 self.assertEqual(6, struct.long_)
1789 self.assertEqual(7, struct.int8)
1791 del struct
1793 def test_simple_struct_in(self):
1794 struct = GIMarshallingTests.SimpleStruct()
1795 struct.long_ = 6
1796 struct.int8 = 7
1798 GIMarshallingTests.SimpleStruct.inv(struct)
1800 del struct
1802 struct = GIMarshallingTests.NestedStruct()
1804 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, struct)
1806 del struct
1808 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.inv, None)
1810 def test_simple_struct_method(self):
1811 struct = GIMarshallingTests.SimpleStruct()
1812 struct.long_ = 6
1813 struct.int8 = 7
1815 struct.method()
1817 del struct
1819 self.assertRaises(TypeError, GIMarshallingTests.SimpleStruct.method)
1821 def test_pointer_struct(self):
1822 self.assertTrue(issubclass(GIMarshallingTests.PointerStruct, GObject.GPointer))
1824 struct = GIMarshallingTests.PointerStruct()
1825 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1827 del struct
1829 def test_pointer_struct_return(self):
1830 struct = GIMarshallingTests.pointer_struct_returnv()
1832 self.assertTrue(isinstance(struct, GIMarshallingTests.PointerStruct))
1833 self.assertEqual(42, struct.long_)
1835 del struct
1837 def test_pointer_struct_in(self):
1838 struct = GIMarshallingTests.PointerStruct()
1839 struct.long_ = 42
1841 struct.inv()
1843 del struct
1845 def test_boxed_struct(self):
1846 self.assertTrue(issubclass(GIMarshallingTests.BoxedStruct, GObject.GBoxed))
1848 struct = GIMarshallingTests.BoxedStruct()
1849 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1851 self.assertEqual(0, struct.long_)
1852 self.assertEqual(None, struct.string_)
1853 self.assertEqual([], struct.g_strv)
1855 del struct
1857 def test_boxed_struct_new(self):
1858 struct = GIMarshallingTests.BoxedStruct.new()
1859 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1860 self.assertEqual(struct.long_, 0)
1861 self.assertEqual(struct.string_, None)
1863 del struct
1865 def test_boxed_struct_copy(self):
1866 struct = GIMarshallingTests.BoxedStruct()
1867 struct.long_ = 42
1868 struct.string_ = 'hello'
1870 new_struct = struct.copy()
1871 self.assertTrue(isinstance(new_struct, GIMarshallingTests.BoxedStruct))
1872 self.assertEqual(new_struct.long_, 42)
1873 self.assertEqual(new_struct.string_, 'hello')
1875 del new_struct
1876 del struct
1878 def test_boxed_struct_return(self):
1879 struct = GIMarshallingTests.boxed_struct_returnv()
1881 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1882 self.assertEqual(42, struct.long_)
1883 self.assertEqual('hello', struct.string_)
1884 self.assertEqual(['0', '1', '2'], struct.g_strv)
1886 del struct
1888 def test_boxed_struct_in(self):
1889 struct = GIMarshallingTests.BoxedStruct()
1890 struct.long_ = 42
1892 struct.inv()
1894 del struct
1896 def test_boxed_struct_out(self):
1897 struct = GIMarshallingTests.boxed_struct_out()
1899 self.assertTrue(isinstance(struct, GIMarshallingTests.BoxedStruct))
1900 self.assertEqual(42, struct.long_)
1902 del struct
1904 def test_boxed_struct_inout(self):
1905 in_struct = GIMarshallingTests.BoxedStruct()
1906 in_struct.long_ = 42
1908 out_struct = GIMarshallingTests.boxed_struct_inout(in_struct)
1910 self.assertTrue(isinstance(out_struct, GIMarshallingTests.BoxedStruct))
1911 self.assertEqual(0, out_struct.long_)
1913 del in_struct
1914 del out_struct
1916 def test_struct_field_assignment(self):
1917 struct = GIMarshallingTests.BoxedStruct()
1919 struct.long_ = 42
1920 struct.string_ = 'hello'
1921 self.assertEqual(struct.long_, 42)
1922 self.assertEqual(struct.string_, 'hello')
1924 def test_union_init(self):
1925 with warnings.catch_warnings(record=True) as warn:
1926 warnings.simplefilter('always')
1927 GIMarshallingTests.Union(42)
1929 self.assertTrue(issubclass(warn[0].category, TypeError))
1931 with warnings.catch_warnings(record=True) as warn:
1932 warnings.simplefilter('always')
1933 GIMarshallingTests.Union(f=42)
1935 self.assertTrue(issubclass(warn[0].category, TypeError))
1937 def test_union(self):
1938 union = GIMarshallingTests.Union()
1940 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1942 new_union = union.copy()
1943 self.assertTrue(isinstance(new_union, GIMarshallingTests.Union))
1945 del union
1946 del new_union
1948 def test_union_return(self):
1949 union = GIMarshallingTests.union_returnv()
1951 self.assertTrue(isinstance(union, GIMarshallingTests.Union))
1952 self.assertEqual(42, union.long_)
1954 del union
1956 def test_union_in(self):
1957 union = GIMarshallingTests.Union()
1958 union.long_ = 42
1960 union.inv()
1962 del union
1964 def test_union_method(self):
1965 union = GIMarshallingTests.Union()
1966 union.long_ = 42
1968 union.method()
1970 del union
1972 self.assertRaises(TypeError, GIMarshallingTests.Union.method)
1974 def test_repr(self):
1975 self.assertRegexpMatches(
1976 repr(GIMarshallingTests.PointerStruct()),
1977 "<GIMarshallingTests.PointerStruct object at 0x[^\s]+ "
1978 "\(void at 0x[^\s]+\)>")
1980 self.assertRegexpMatches(
1981 repr(GIMarshallingTests.SimpleStruct()),
1982 "<GIMarshallingTests.SimpleStruct object at 0x[^\s]+ "
1983 "\(void at 0x[^\s]+\)>")
1985 self.assertRegexpMatches(
1986 repr(GIMarshallingTests.Union()),
1987 "<GIMarshallingTests.Union object at 0x[^\s]+ "
1988 "\(GIMarshallingTestsUnion at 0x[^\s]+\)>")
1990 self.assertRegexpMatches(
1991 repr(GIMarshallingTests.BoxedStruct()),
1992 "<GIMarshallingTests.BoxedStruct object at 0x[^\s]+ "
1993 "\(GIMarshallingTestsBoxedStruct at 0x[^\s]+\)>")
1996 class TestGObject(unittest.TestCase):
1998 def test_object(self):
1999 self.assertTrue(issubclass(GIMarshallingTests.Object, GObject.GObject))
2001 object_ = GIMarshallingTests.Object()
2002 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2003 self.assertEqual(object_.__grefcount__, 1)
2005 def test_object_new(self):
2006 object_ = GIMarshallingTests.Object.new(42)
2007 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2008 self.assertEqual(object_.__grefcount__, 1)
2010 def test_object_int(self):
2011 object_ = GIMarshallingTests.Object(int=42)
2012 self.assertEqual(object_.int_, 42)
2013 # FIXME: Don't work yet.
2014 # object_.int_ = 0
2015 # self.assertEqual(object_.int_, 0)
2017 def test_object_static_method(self):
2018 GIMarshallingTests.Object.static_method()
2020 def test_object_method(self):
2021 GIMarshallingTests.Object(int=42).method()
2022 self.assertRaises(TypeError, GIMarshallingTests.Object.method, GObject.GObject())
2023 self.assertRaises(TypeError, GIMarshallingTests.Object.method)
2025 def test_sub_object(self):
2026 self.assertTrue(issubclass(GIMarshallingTests.SubObject, GIMarshallingTests.Object))
2028 object_ = GIMarshallingTests.SubObject()
2029 self.assertTrue(isinstance(object_, GIMarshallingTests.SubObject))
2031 def test_sub_object_new(self):
2032 self.assertRaises(TypeError, GIMarshallingTests.SubObject.new, 42)
2034 def test_sub_object_static_method(self):
2035 object_ = GIMarshallingTests.SubObject()
2036 object_.static_method()
2038 def test_sub_object_method(self):
2039 object_ = GIMarshallingTests.SubObject(int=42)
2040 object_.method()
2042 def test_sub_object_sub_method(self):
2043 object_ = GIMarshallingTests.SubObject()
2044 object_.sub_method()
2046 def test_sub_object_overwritten_method(self):
2047 object_ = GIMarshallingTests.SubObject()
2048 object_.overwritten_method()
2050 self.assertRaises(TypeError, GIMarshallingTests.SubObject.overwritten_method, GIMarshallingTests.Object())
2052 def test_sub_object_int(self):
2053 object_ = GIMarshallingTests.SubObject()
2054 self.assertEqual(object_.int_, 0)
2055 # FIXME: Don't work yet.
2056 # object_.int_ = 42
2057 # self.assertEqual(object_.int_, 42)
2059 def test_object_none_return(self):
2060 object_ = GIMarshallingTests.Object.none_return()
2061 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2062 self.assertEqual(object_.__grefcount__, 2)
2064 def test_object_full_return(self):
2065 object_ = GIMarshallingTests.Object.full_return()
2066 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2067 self.assertEqual(object_.__grefcount__, 1)
2069 def test_object_none_in(self):
2070 object_ = GIMarshallingTests.Object(int=42)
2071 GIMarshallingTests.Object.none_in(object_)
2072 self.assertEqual(object_.__grefcount__, 1)
2074 object_ = GIMarshallingTests.SubObject(int=42)
2075 GIMarshallingTests.Object.none_in(object_)
2077 object_ = GObject.GObject()
2078 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, object_)
2080 self.assertRaises(TypeError, GIMarshallingTests.Object.none_in, None)
2082 def test_object_none_out(self):
2083 object_ = GIMarshallingTests.Object.none_out()
2084 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2085 self.assertEqual(object_.__grefcount__, 2)
2087 new_object = GIMarshallingTests.Object.none_out()
2088 self.assertTrue(new_object is object_)
2090 def test_object_full_out(self):
2091 object_ = GIMarshallingTests.Object.full_out()
2092 self.assertTrue(isinstance(object_, GIMarshallingTests.Object))
2093 self.assertEqual(object_.__grefcount__, 1)
2095 def test_object_none_inout(self):
2096 object_ = GIMarshallingTests.Object(int=42)
2097 new_object = GIMarshallingTests.Object.none_inout(object_)
2099 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
2101 self.assertFalse(object_ is new_object)
2103 self.assertEqual(object_.__grefcount__, 1)
2104 self.assertEqual(new_object.__grefcount__, 2)
2106 new_new_object = GIMarshallingTests.Object.none_inout(object_)
2107 self.assertTrue(new_new_object is new_object)
2109 GIMarshallingTests.Object.none_inout(GIMarshallingTests.SubObject(int=42))
2111 def test_object_full_inout(self):
2112 # Using gimarshallingtests.c from GI versions > 1.38.0 will show this
2113 # test as an "unexpected success" due to reference leak fixes in that file.
2114 # TODO: remove the expectedFailure once PyGI relies on GI > 1.38.0.
2115 object_ = GIMarshallingTests.Object(int=42)
2116 new_object = GIMarshallingTests.Object.full_inout(object_)
2118 self.assertTrue(isinstance(new_object, GIMarshallingTests.Object))
2120 self.assertFalse(object_ is new_object)
2122 self.assertEqual(object_.__grefcount__, 1)
2123 self.assertEqual(new_object.__grefcount__, 1)
2125 def test_repr(self):
2126 self.assertRegexpMatches(
2127 repr(GIMarshallingTests.Object(int=42)),
2128 "<GIMarshallingTests.Object object at 0x[^\s]+ "
2129 "\(GIMarshallingTestsObject at 0x[^\s]+\)>")
2131 def test_nongir_repr(self):
2132 self.assertRegexpMatches(
2133 repr(Gio.File.new_for_path("")),
2134 "<__gi__.GLocalFile object at 0x[^\s]+ "
2135 "\(GLocalFile at 0x[^\s]+\)>")
2137 # FIXME: Doesn't actually return the same object.
2138 # def test_object_inout_same(self):
2139 # object_ = GIMarshallingTests.Object()
2140 # new_object = GIMarshallingTests.object_full_inout(object_)
2141 # self.assertTrue(object_ is new_object)
2142 # self.assertEqual(object_.__grefcount__, 1)
2145 class TestPythonGObject(unittest.TestCase):
2147 class Object(GIMarshallingTests.Object):
2148 return_for_caller_allocated_out_parameter = 'test caller alloc return'
2150 def __init__(self, int):
2151 GIMarshallingTests.Object.__init__(self)
2152 self.val = None
2154 def method(self):
2155 # Don't call super, which asserts that self.int == 42.
2156 pass
2158 def do_method_int8_in(self, int8):
2159 self.val = int8
2161 def do_method_int8_out(self):
2162 return 42
2164 def do_method_int8_arg_and_out_caller(self, arg):
2165 return arg + 1
2167 def do_method_int8_arg_and_out_callee(self, arg):
2168 return arg + 1
2170 def do_method_str_arg_out_ret(self, arg):
2171 return (arg.upper(), len(arg))
2173 def do_method_with_default_implementation(self, int8):
2174 GIMarshallingTests.Object.do_method_with_default_implementation(self, int8)
2175 self.props.int += int8
2177 def do_vfunc_return_value_only(self):
2178 return 4242
2180 def do_vfunc_one_out_parameter(self):
2181 return 42.42
2183 def do_vfunc_multiple_out_parameters(self):
2184 return (42.42, 3.14)
2186 def do_vfunc_return_value_and_one_out_parameter(self):
2187 return (5, 42)
2189 def do_vfunc_return_value_and_multiple_out_parameters(self):
2190 return (5, 42, 99)
2192 def do_vfunc_caller_allocated_out_parameter(self):
2193 return self.return_for_caller_allocated_out_parameter
2195 class SubObject(GIMarshallingTests.SubObject):
2196 def __init__(self, int):
2197 GIMarshallingTests.SubObject.__init__(self)
2198 self.val = None
2200 def do_method_with_default_implementation(self, int8):
2201 self.val = int8
2203 def do_vfunc_return_value_only(self):
2204 return 2121
2206 class Interface3Impl(GObject.Object, GIMarshallingTests.Interface3):
2207 def __init__(self):
2208 GObject.Object.__init__(self)
2209 self.variants = None
2210 self.n_variants = None
2212 def do_test_variant_array_in(self, variants, n_variants):
2213 self.variants = variants
2214 self.n_variants = n_variants
2216 class ErrorObject(GIMarshallingTests.Object):
2217 def do_vfunc_return_value_only(self):
2218 raise ValueError('Return value should be 0')
2220 def test_object(self):
2221 self.assertTrue(issubclass(self.Object, GIMarshallingTests.Object))
2223 object_ = self.Object(int=42)
2224 self.assertTrue(isinstance(object_, self.Object))
2226 @unittest.skipUnless(hasattr(GIMarshallingTests.Object, 'new_fail'),
2227 'Requires newer version of GI')
2228 def test_object_fail(self):
2229 with self.assertRaises(GLib.Error):
2230 GIMarshallingTests.Object.new_fail(int_=42)
2232 def test_object_method(self):
2233 self.Object(int=0).method()
2235 def test_object_vfuncs(self):
2236 object_ = self.Object(int=42)
2237 object_.method_int8_in(84)
2238 self.assertEqual(object_.val, 84)
2239 self.assertEqual(object_.method_int8_out(), 42)
2241 # can be dropped when bumping g-i dependencies to >= 1.35.2
2242 if hasattr(object_, 'method_int8_arg_and_out_caller'):
2243 self.assertEqual(object_.method_int8_arg_and_out_caller(42), 43)
2244 self.assertEqual(object_.method_int8_arg_and_out_callee(42), 43)
2245 self.assertEqual(object_.method_str_arg_out_ret('hello'), ('HELLO', 5))
2247 object_.method_with_default_implementation(42)
2248 self.assertEqual(object_.props.int, 84)
2250 self.assertEqual(object_.vfunc_return_value_only(), 4242)
2251 self.assertAlmostEqual(object_.vfunc_one_out_parameter(), 42.42, places=5)
2253 (a, b) = object_.vfunc_multiple_out_parameters()
2254 self.assertAlmostEqual(a, 42.42, places=5)
2255 self.assertAlmostEqual(b, 3.14, places=5)
2257 self.assertEqual(object_.vfunc_return_value_and_one_out_parameter(), (5, 42))
2258 self.assertEqual(object_.vfunc_return_value_and_multiple_out_parameters(), (5, 42, 99))
2260 self.assertEqual(object_.vfunc_caller_allocated_out_parameter(),
2261 object_.return_for_caller_allocated_out_parameter)
2263 class ObjectWithoutVFunc(GIMarshallingTests.Object):
2264 def __init__(self, int):
2265 GIMarshallingTests.Object.__init__(self)
2267 object_ = ObjectWithoutVFunc(int=42)
2268 object_.method_with_default_implementation(84)
2269 self.assertEqual(object_.props.int, 84)
2271 def test_vfunc_return_ref_count(self):
2272 obj = self.Object(int=42)
2273 ref_count = sys.getrefcount(obj.return_for_caller_allocated_out_parameter)
2274 ret = obj.vfunc_caller_allocated_out_parameter()
2275 gc.collect()
2277 # Make sure the return and what the vfunc returned
2278 # are equal but not the same object.
2279 self.assertEqual(ret, obj.return_for_caller_allocated_out_parameter)
2280 self.assertFalse(ret is obj.return_for_caller_allocated_out_parameter)
2281 self.assertEqual(sys.getrefcount(obj.return_for_caller_allocated_out_parameter),
2282 ref_count)
2284 def test_subobject_parent_vfunc(self):
2285 object_ = self.SubObject(int=81)
2286 object_.method_with_default_implementation(87)
2287 self.assertEqual(object_.val, 87)
2289 def test_subobject_child_vfunc(self):
2290 object_ = self.SubObject(int=1)
2291 self.assertEqual(object_.vfunc_return_value_only(), 2121)
2293 def test_subobject_non_vfunc_do_method(self):
2294 class PythonObjectWithNonVFuncDoMethod(object):
2295 def do_not_a_vfunc(self):
2296 return 5
2298 class ObjectOverrideNonVFuncDoMethod(GIMarshallingTests.Object, PythonObjectWithNonVFuncDoMethod):
2299 def do_not_a_vfunc(self):
2300 value = super(ObjectOverrideNonVFuncDoMethod, self).do_not_a_vfunc()
2301 return 13 + value
2303 object_ = ObjectOverrideNonVFuncDoMethod()
2304 self.assertEqual(18, object_.do_not_a_vfunc())
2306 def test_native_function_not_set_in_subclass_dict(self):
2307 # Previously, GI was setting virtual functions on the class as well
2308 # as any *native* class that subclasses it. Here we check that it is only
2309 # set on the class that the method is originally from.
2310 self.assertTrue('do_method_with_default_implementation' in GIMarshallingTests.Object.__dict__)
2311 self.assertTrue('do_method_with_default_implementation' not in GIMarshallingTests.SubObject.__dict__)
2313 def test_subobject_with_interface_and_non_vfunc_do_method(self):
2314 # There was a bug for searching for vfuncs in interfaces. It was
2315 # triggered by having a do_* method that wasn't overriding
2316 # a native vfunc, as well as inheriting from an interface.
2317 class GObjectSubclassWithInterface(GObject.GObject, GIMarshallingTests.Interface):
2318 def do_method_not_a_vfunc(self):
2319 pass
2321 def test_subsubobject(self):
2322 class SubSubSubObject(GIMarshallingTests.SubSubObject):
2323 def do_method_deep_hierarchy(self, num):
2324 self.props.int = num * 2
2326 sub_sub_sub_object = SubSubSubObject()
2327 GIMarshallingTests.SubSubObject.do_method_deep_hierarchy(sub_sub_sub_object, 5)
2328 self.assertEqual(sub_sub_sub_object.props.int, 5)
2330 def test_interface3impl(self):
2331 iface3 = self.Interface3Impl()
2332 variants = [GLib.Variant('i', 27), GLib.Variant('s', 'Hello')]
2333 iface3.test_variant_array_in(variants)
2334 self.assertEqual(iface3.n_variants, 2)
2335 self.assertEqual(iface3.variants[0].unpack(), 27)
2336 self.assertEqual(iface3.variants[1].unpack(), 'Hello')
2338 def test_python_subsubobject_vfunc(self):
2339 class PySubObject(GIMarshallingTests.Object):
2340 def __init__(self):
2341 GIMarshallingTests.Object.__init__(self)
2342 self.sub_method_int8_called = 0
2344 def do_method_int8_in(self, int8):
2345 self.sub_method_int8_called += 1
2347 class PySubSubObject(PySubObject):
2348 def __init__(self):
2349 PySubObject.__init__(self)
2350 self.subsub_method_int8_called = 0
2352 def do_method_int8_in(self, int8):
2353 self.subsub_method_int8_called += 1
2355 so = PySubObject()
2356 so.method_int8_in(1)
2357 self.assertEqual(so.sub_method_int8_called, 1)
2359 # it should call the method on the SubSub object only
2360 sso = PySubSubObject()
2361 sso.method_int8_in(1)
2362 self.assertEqual(sso.subsub_method_int8_called, 1)
2363 self.assertEqual(sso.sub_method_int8_called, 0)
2365 def test_callback_in_vfunc(self):
2366 class SubObject(GIMarshallingTests.Object):
2367 def __init__(self):
2368 GObject.GObject.__init__(self)
2369 self.worked = False
2371 def do_vfunc_with_callback(self, callback):
2372 self.worked = callback(42) == 42
2374 _object = SubObject()
2375 _object.call_vfunc_with_callback()
2376 self.assertTrue(_object.worked)
2377 _object.worked = False
2378 _object.call_vfunc_with_callback()
2379 self.assertTrue(_object.worked)
2381 def test_exception_in_vfunc_return_value(self):
2382 obj = self.ErrorObject()
2383 with capture_exceptions() as exc:
2384 self.assertEqual(obj.vfunc_return_value_only(), 0)
2385 self.assertEqual(len(exc), 1)
2386 self.assertEqual(exc[0].type, ValueError)
2388 @unittest.skipUnless(hasattr(GIMarshallingTests, 'callback_owned_boxed'),
2389 'requires newer version of GI')
2390 def test_callback_owned_box(self):
2391 def callback(box, data):
2392 self.box = box
2394 def nop_callback(box, data):
2395 pass
2397 GIMarshallingTests.callback_owned_boxed(callback, None)
2398 GIMarshallingTests.callback_owned_boxed(nop_callback, None)
2399 self.assertEqual(self.box.long_, 1)
2402 class TestMultiOutputArgs(unittest.TestCase):
2404 def test_int_out_out(self):
2405 self.assertEqual((6, 7), GIMarshallingTests.int_out_out())
2407 def test_int_return_out(self):
2408 self.assertEqual((6, 7), GIMarshallingTests.int_return_out())
2411 # Interface
2413 class TestInterfaces(unittest.TestCase):
2415 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2416 def __init__(self):
2417 GObject.GObject.__init__(self)
2418 self.val = None
2420 def do_test_int8_in(self, int8):
2421 self.val = int8
2423 def setUp(self):
2424 self.instance = self.TestInterfaceImpl()
2426 def test_wrapper(self):
2427 self.assertTrue(issubclass(GIMarshallingTests.Interface, GObject.GInterface))
2428 self.assertEqual(GIMarshallingTests.Interface.__gtype__.name, 'GIMarshallingTestsInterface')
2429 self.assertRaises(NotImplementedError, GIMarshallingTests.Interface)
2431 def test_implementation(self):
2432 self.assertTrue(issubclass(self.TestInterfaceImpl, GIMarshallingTests.Interface))
2433 self.assertTrue(isinstance(self.instance, GIMarshallingTests.Interface))
2435 def test_int8_int(self):
2436 GIMarshallingTests.test_interface_test_int8_in(self.instance, 42)
2437 self.assertEqual(self.instance.val, 42)
2439 def test_subclass(self):
2440 class TestInterfaceImplA(self.TestInterfaceImpl):
2441 pass
2443 class TestInterfaceImplB(TestInterfaceImplA):
2444 pass
2446 instance = TestInterfaceImplA()
2447 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2448 self.assertEqual(instance.val, 42)
2450 def test_subclass_override(self):
2451 class TestInterfaceImplD(TestInterfaces.TestInterfaceImpl):
2452 val2 = None
2454 def do_test_int8_in(self, int8):
2455 self.val2 = int8
2457 instance = TestInterfaceImplD()
2458 self.assertEqual(instance.val, None)
2459 self.assertEqual(instance.val2, None)
2461 GIMarshallingTests.test_interface_test_int8_in(instance, 42)
2462 self.assertEqual(instance.val, None)
2463 self.assertEqual(instance.val2, 42)
2465 def test_type_mismatch(self):
2466 obj = GIMarshallingTests.Object()
2468 # wrong type for first argument: interface
2469 enum = Gio.File.new_for_path('.').enumerate_children(
2470 '', Gio.FileQueryInfoFlags.NONE, None)
2471 try:
2472 enum.next_file(obj)
2473 self.fail('call with wrong type argument unexpectedly succeeded')
2474 except TypeError as e:
2475 # should have argument name
2476 self.assertTrue('cancellable' in str(e), e)
2477 # should have expected type
2478 self.assertTrue('xpected Gio.Cancellable' in str(e), e)
2479 # should have actual type
2480 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2482 # wrong type for self argument: interface
2483 try:
2484 Gio.FileEnumerator.next_file(obj, None)
2485 self.fail('call with wrong type argument unexpectedly succeeded')
2486 except TypeError as e:
2487 # should have argument name
2488 self.assertTrue('self' in str(e), e)
2489 # should have expected type
2490 self.assertTrue('xpected Gio.FileEnumerator' in str(e), e)
2491 # should have actual type
2492 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2494 # wrong type for first argument: GObject
2495 var = GLib.Variant('s', 'mystring')
2496 action = Gio.SimpleAction.new('foo', var.get_type())
2497 try:
2498 action.activate(obj)
2499 self.fail('call with wrong type argument unexpectedly succeeded')
2500 except TypeError as e:
2501 # should have argument name
2502 self.assertTrue('parameter' in str(e), e)
2503 # should have expected type
2504 self.assertTrue('xpected GLib.Variant' in str(e), e)
2505 # should have actual type
2506 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2508 # wrong type for self argument: GObject
2509 try:
2510 Gio.SimpleAction.activate(obj, obj)
2511 self.fail('call with wrong type argument unexpectedly succeeded')
2512 except TypeError as e:
2513 # should have argument name
2514 self.assertTrue('self' in str(e), e)
2515 # should have expected type
2516 self.assertTrue('xpected Gio.Action' in str(e), e)
2517 # should have actual type
2518 self.assertTrue('GIMarshallingTests.Object' in str(e), e)
2521 class TestMRO(unittest.TestCase):
2522 def test_mro(self):
2523 # check that our own MRO calculation matches what we would expect
2524 # from Python's own C3 calculations
2525 class A(object):
2526 pass
2528 class B(A):
2529 pass
2531 class C(A):
2532 pass
2534 class D(B, C):
2535 pass
2537 class E(D, GIMarshallingTests.Object):
2538 pass
2540 expected = (E, D, B, C, A, GIMarshallingTests.Object,
2541 GObject.Object, GObject.Object.__base__, gi._gi._gobject.GObject,
2542 object)
2543 self.assertEqual(expected, E.__mro__)
2545 def test_interface_collision(self):
2546 # there was a problem with Python bailing out because of
2547 # http://en.wikipedia.org/wiki/Diamond_problem with interfaces,
2548 # which shouldn't really be a problem.
2550 class TestInterfaceImpl(GObject.GObject, GIMarshallingTests.Interface):
2551 pass
2553 class TestInterfaceImpl2(GIMarshallingTests.Interface,
2554 TestInterfaceImpl):
2555 pass
2557 class TestInterfaceImpl3(TestInterfaceImpl,
2558 GIMarshallingTests.Interface2):
2559 pass
2561 def test_old_style_mixin(self):
2562 # Note: Old style classes don't exist in Python 3
2563 class Mixin:
2564 pass
2566 with warnings.catch_warnings(record=True) as warn:
2567 warnings.simplefilter('always')
2569 # Dynamically create a new gi based class with an old
2570 # style mixin.
2571 type('GIWithOldStyleMixin', (GIMarshallingTests.Object, Mixin), {})
2573 if sys.version_info < (3, 0):
2574 self.assertTrue(issubclass(warn[0].category, RuntimeWarning))
2575 else:
2576 self.assertEqual(len(warn), 0)
2579 class TestInterfaceClash(unittest.TestCase):
2581 def test_clash(self):
2582 def create_clash():
2583 class TestClash(GObject.GObject, GIMarshallingTests.Interface, GIMarshallingTests.Interface2):
2584 def do_test_int8_in(self, int8):
2585 pass
2586 TestClash()
2588 self.assertRaises(TypeError, create_clash)
2591 class TestOverrides(unittest.TestCase):
2593 def test_constant(self):
2594 self.assertEqual(GIMarshallingTests.OVERRIDES_CONSTANT, 7)
2596 def test_struct(self):
2597 # Test that the constructor has been overridden.
2598 struct = GIMarshallingTests.OverridesStruct(42)
2600 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2602 # Test that the method has been overridden.
2603 self.assertEqual(6, struct.method())
2605 del struct
2607 # Test that the overrides wrapper has been registered.
2608 struct = GIMarshallingTests.overrides_struct_returnv()
2610 self.assertTrue(isinstance(struct, GIMarshallingTests.OverridesStruct))
2612 del struct
2614 def test_object(self):
2615 # Test that the constructor has been overridden.
2616 object_ = GIMarshallingTests.OverridesObject(42)
2618 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2620 # Test that the alternate constructor has been overridden.
2621 object_ = GIMarshallingTests.OverridesObject.new(42)
2623 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2625 # Test that the method has been overridden.
2626 self.assertEqual(6, object_.method())
2628 # Test that the overrides wrapper has been registered.
2629 object_ = GIMarshallingTests.OverridesObject.returnv()
2631 self.assertTrue(isinstance(object_, GIMarshallingTests.OverridesObject))
2633 def test_module_name(self):
2634 # overridden types
2635 self.assertEqual(GIMarshallingTests.OverridesStruct.__module__, 'gi.overrides.GIMarshallingTests')
2636 self.assertEqual(GIMarshallingTests.OverridesObject.__module__, 'gi.overrides.GIMarshallingTests')
2637 self.assertEqual(GObject.Object.__module__, 'gi.overrides.GObject')
2639 # not overridden
2640 self.assertEqual(GIMarshallingTests.SubObject.__module__, 'gi.repository.GIMarshallingTests')
2641 self.assertEqual(GObject.InitiallyUnowned.__module__, 'gi.repository.GObject')
2644 class TestDir(unittest.TestCase):
2645 def test_members_list(self):
2646 list = dir(GIMarshallingTests)
2647 self.assertTrue('OverridesStruct' in list)
2648 self.assertTrue('BoxedStruct' in list)
2649 self.assertTrue('OVERRIDES_CONSTANT' in list)
2650 self.assertTrue('GEnum' in list)
2651 self.assertTrue('int32_return_max' in list)
2653 def test_modules_list(self):
2654 import gi.repository
2655 list = dir(gi.repository)
2656 self.assertTrue('GIMarshallingTests' in list)
2658 # FIXME: test to see if a module which was not imported is in the list
2659 # we should be listing every typelib we find, not just the ones
2660 # which are imported
2662 # to test this I recommend we compile a fake module which
2663 # our tests would never import and check to see if it is
2664 # in the list:
2666 # self.assertTrue('DoNotImportDummyTests' in list)
2669 class TestParamSpec(unittest.TestCase):
2670 # https://bugzilla.gnome.org/show_bug.cgi?id=682355
2671 @unittest.expectedFailure
2672 def test_param_spec_in_bool(self):
2673 ps = GObject.param_spec_boolean('mybool', 'test-bool', 'boolblurb',
2674 True, GObject.ParamFlags.READABLE)
2675 GIMarshallingTests.param_spec_in_bool(ps)
2677 def test_param_spec_return(self):
2678 obj = GIMarshallingTests.param_spec_return()
2679 self.assertEqual(obj.name, 'test-param')
2680 self.assertEqual(obj.nick, 'test')
2681 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2683 def test_param_spec_out(self):
2684 obj = GIMarshallingTests.param_spec_out()
2685 self.assertEqual(obj.name, 'test-param')
2686 self.assertEqual(obj.nick, 'test')
2687 self.assertEqual(obj.value_type, GObject.TYPE_STRING)
2690 class TestKeywordArgs(unittest.TestCase):
2692 def test_calling(self):
2693 kw_func = GIMarshallingTests.int_three_in_three_out
2695 self.assertEqual(kw_func(1, 2, 3), (1, 2, 3))
2696 self.assertEqual(kw_func(**{'a': 4, 'b': 5, 'c': 6}), (4, 5, 6))
2697 self.assertEqual(kw_func(1, **{'b': 7, 'c': 8}), (1, 7, 8))
2698 self.assertEqual(kw_func(1, 7, **{'c': 8}), (1, 7, 8))
2699 self.assertEqual(kw_func(1, c=8, **{'b': 7}), (1, 7, 8))
2700 self.assertEqual(kw_func(2, c=4, b=3), (2, 3, 4))
2701 self.assertEqual(kw_func(a=2, c=4, b=3), (2, 3, 4))
2703 def assertRaisesMessage(self, exception, message, func, *args, **kwargs):
2704 try:
2705 func(*args, **kwargs)
2706 except exception:
2707 (e_type, e) = sys.exc_info()[:2]
2708 if message is not None:
2709 self.assertEqual(str(e), message)
2710 except:
2711 raise
2712 else:
2713 msg = "%s() did not raise %s" % (func.__name__, exception.__name__)
2714 raise AssertionError(msg)
2716 def test_type_errors(self):
2717 # test too few args
2718 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (0 given)",
2719 GIMarshallingTests.int_three_in_three_out)
2720 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (1 given)",
2721 GIMarshallingTests.int_three_in_three_out, 1)
2722 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (0 given)",
2723 GIMarshallingTests.int_three_in_three_out, *())
2724 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (0 given)",
2725 GIMarshallingTests.int_three_in_three_out, *(), **{})
2726 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 non-keyword arguments (0 given)",
2727 GIMarshallingTests.int_three_in_three_out, *(), **{'c': 4})
2729 # test too many args
2730 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 arguments (4 given)",
2731 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4))
2732 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() takes exactly 3 non-keyword arguments (4 given)",
2733 GIMarshallingTests.int_three_in_three_out, *(1, 2, 3, 4), c=6)
2735 # test too many keyword args
2736 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() got multiple values for keyword argument 'a'",
2737 GIMarshallingTests.int_three_in_three_out, 1, 2, 3, **{'a': 4, 'b': 5})
2738 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() got an unexpected keyword argument 'd'",
2739 GIMarshallingTests.int_three_in_three_out, d=4)
2740 self.assertRaisesMessage(TypeError, "GIMarshallingTests.int_three_in_three_out() got an unexpected keyword argument 'e'",
2741 GIMarshallingTests.int_three_in_three_out, **{'e': 2})
2743 def test_kwargs_are_not_modified(self):
2744 d = {'b': 2}
2745 d2 = d.copy()
2746 GIMarshallingTests.int_three_in_three_out(1, c=4, **d)
2747 self.assertEqual(d, d2)
2749 @unittest.skipUnless(hasattr(GIMarshallingTests, 'int_one_in_utf8_two_in_one_allows_none'),
2750 'Requires newer GIMarshallingTests')
2751 def test_allow_none_as_default(self):
2752 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, '3', '4')
2753 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, '3')
2754 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2)
2755 GIMarshallingTests.int_two_in_utf8_two_in_with_allow_none(1, 2, d='4')
2757 GIMarshallingTests.array_in_utf8_two_in_out_of_order('1', [-1, 0, 1, 2])
2758 GIMarshallingTests.array_in_utf8_two_in_out_of_order('1', [-1, 0, 1, 2], '2')
2759 self.assertRaises(TypeError,
2760 GIMarshallingTests.array_in_utf8_two_in_out_of_order,
2761 [-1, 0, 1, 2], a='1')
2762 self.assertRaises(TypeError,
2763 GIMarshallingTests.array_in_utf8_two_in_out_of_order,
2764 [-1, 0, 1, 2])
2766 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], '1', '2')
2767 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], '1')
2768 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2])
2769 GIMarshallingTests.array_in_utf8_two_in([-1, 0, 1, 2], b='2')
2771 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none(1, '2', '3')
2772 self.assertRaises(TypeError,
2773 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none,
2774 1, '3')
2775 self.assertRaises(TypeError,
2776 GIMarshallingTests.int_one_in_utf8_two_in_one_allows_none,
2777 1, c='3')
2780 class TestKeywords(unittest.TestCase):
2781 def test_method(self):
2782 # g_variant_print()
2783 v = GLib.Variant('i', 1)
2784 self.assertEqual(v.print_(False), '1')
2786 def test_function(self):
2787 # g_thread_yield()
2788 self.assertEqual(GLib.Thread.yield_(), None)
2790 def test_struct_method(self):
2791 # g_timer_continue()
2792 # we cannot currently instantiate GLib.Timer objects, so just ensure
2793 # the method exists
2794 self.assertTrue(callable(GLib.Timer.continue_))
2796 def test_uppercase(self):
2797 self.assertEqual(GLib.IOCondition.IN.value_nicks, ['in'])
2800 class TestModule(unittest.TestCase):
2801 def test_path(self):
2802 self.assertTrue(GIMarshallingTests.__path__.endswith('GIMarshallingTests-1.0.typelib'),
2803 GIMarshallingTests.__path__)
2805 def test_str(self):
2806 self.assertTrue("'GIMarshallingTests' from '" in str(GIMarshallingTests),
2807 str(GIMarshallingTests))
2809 def test_dir(self):
2810 _dir = dir(GIMarshallingTests)
2811 self.assertGreater(len(_dir), 10)
2813 self.assertTrue('SimpleStruct' in _dir)
2814 self.assertTrue('Interface2' in _dir)
2815 self.assertTrue('CONSTANT_GERROR_CODE' in _dir)
2816 self.assertTrue('array_zero_terminated_inout' in _dir)
2818 # assert that dir() does not contain garbage
2819 for item_name in _dir:
2820 item = getattr(GIMarshallingTests, item_name)
2821 self.assertTrue(hasattr(item, '__class__'))
2823 def test_help(self):
2824 orig_stdout = sys.stdout
2825 try:
2826 if sys.version_info < (3, 0):
2827 sys.stdout = BytesIO()
2828 else:
2829 sys.stdout = StringIO()
2830 help(GIMarshallingTests)
2831 output = sys.stdout.getvalue()
2832 finally:
2833 sys.stdout = orig_stdout
2835 self.assertTrue('SimpleStruct' in output, output)
2836 self.assertTrue('Interface2' in output, output)
2837 self.assertTrue('method_array_inout' in output, output)
2840 class TestProjectVersion(unittest.TestCase):
2841 def test_version_str(self):
2842 self.assertGreater(gi.__version__, "3.")
2844 def test_version_info(self):
2845 self.assertEqual(len(gi.version_info), 3)
2846 self.assertGreaterEqual(gi.version_info, (3, 3, 5))
2848 def test_check_version(self):
2849 self.assertRaises(ValueError, gi.check_version, (99, 0, 0))
2850 self.assertRaises(ValueError, gi.check_version, "99.0.0")
2851 gi.check_version((3, 3, 5))
2852 gi.check_version("3.3.5")
2855 class TestGIWarning(unittest.TestCase):
2857 def test_warning(self):
2858 ignored_by_default = (DeprecationWarning, PendingDeprecationWarning,
2859 ImportWarning)
2861 with warnings.catch_warnings(record=True) as warn:
2862 warnings.simplefilter('always')
2863 warnings.warn("test", PyGIWarning)
2864 self.assertTrue(issubclass(warn[0].category, Warning))
2865 # We don't want PyGIWarning get ignored by default
2866 self.assertFalse(issubclass(warn[0].category, ignored_by_default))
2869 class TestDeprecation(unittest.TestCase):
2870 def test_method(self):
2871 d = GLib.Date.new()
2872 with warnings.catch_warnings(record=True) as warn:
2873 warnings.simplefilter('always')
2874 d.set_time(1)
2875 self.assertTrue(issubclass(warn[0].category, DeprecationWarning))
2876 self.assertEqual(str(warn[0].message), "GLib.Date.set_time is deprecated")
2878 def test_function(self):
2879 with warnings.catch_warnings(record=True) as warn:
2880 warnings.simplefilter('always')
2881 GLib.strcasecmp("foo", "bar")
2882 self.assertTrue(issubclass(warn[0].category, DeprecationWarning))
2883 self.assertEqual(str(warn[0].message), "GLib.strcasecmp is deprecated")
2885 def test_deprecated_attribute_compat(self):
2886 # test if the deprecation descriptor behaves like an instance attribute
2888 # save the descriptor
2889 desc = type(GLib).__dict__["IO_STATUS_ERROR"]
2891 # the descriptor raises AttributeError for itself
2892 self.assertFalse(hasattr(type(GLib), "IO_STATUS_ERROR"))
2894 with warnings.catch_warnings():
2895 warnings.simplefilter('ignore', PyGIDeprecationWarning)
2896 self.assertTrue(hasattr(GLib, "IO_STATUS_ERROR"))
2898 try:
2899 # check if replacing works
2900 GLib.IO_STATUS_ERROR = "foo"
2901 self.assertEqual(GLib.IO_STATUS_ERROR, "foo")
2902 finally:
2903 # restore descriptor
2904 try:
2905 del GLib.IO_STATUS_ERROR
2906 except AttributeError:
2907 pass
2908 setattr(type(GLib), "IO_STATUS_ERROR", desc)
2910 try:
2911 # check if deleting works
2912 del GLib.IO_STATUS_ERROR
2913 self.assertFalse(hasattr(GLib, "IO_STATUS_ERROR"))
2914 finally:
2915 # restore descriptor
2916 try:
2917 del GLib.IO_STATUS_ERROR
2918 except AttributeError:
2919 pass
2920 setattr(type(GLib), "IO_STATUS_ERROR", desc)
2922 def test_deprecated_attribute_warning(self):
2923 with warnings.catch_warnings(record=True) as warn:
2924 warnings.simplefilter('always')
2925 self.assertEqual(GLib.IO_STATUS_ERROR, GLib.IOStatus.ERROR)
2926 GLib.IO_STATUS_ERROR
2927 GLib.IO_STATUS_ERROR
2928 self.assertEqual(len(warn), 3)
2929 self.assertTrue(
2930 issubclass(warn[0].category, PyGIDeprecationWarning))
2931 self.assertRegexpMatches(
2932 str(warn[0].message),
2933 ".*GLib.IO_STATUS_ERROR.*GLib.IOStatus.ERROR.*")
2935 def test_deprecated_attribute_warning_coverage(self):
2936 with warnings.catch_warnings(record=True) as warn:
2937 warnings.simplefilter('always')
2938 GObject.markup_escape_text
2939 GObject.PRIORITY_DEFAULT
2940 GObject.GError
2941 GObject.PARAM_CONSTRUCT
2942 GObject.SIGNAL_ACTION
2943 GObject.property
2944 GObject.IO_STATUS_ERROR
2945 GObject.G_MAXUINT64
2946 GLib.IO_STATUS_ERROR
2947 GLib.SPAWN_SEARCH_PATH
2948 GLib.OPTION_FLAG_HIDDEN
2949 GLib.IO_FLAG_IS_WRITEABLE
2950 GLib.IO_FLAG_NONBLOCK
2951 GLib.USER_DIRECTORY_DESKTOP
2952 GLib.OPTION_ERROR_BAD_VALUE
2953 GLib.glib_version
2954 GLib.pyglib_version
2955 self.assertEqual(len(warn), 17)
2957 def test_deprecated_init_no_keywords(self):
2958 def init(self, **kwargs):
2959 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
2961 fn = gi.overrides.deprecated_init(init, arg_names=('a', 'b', 'c'))
2962 with warnings.catch_warnings(record=True) as warn:
2963 warnings.simplefilter('always')
2964 fn(self, 1, 2, 3)
2965 self.assertEqual(len(warn), 1)
2966 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
2967 self.assertRegexpMatches(str(warn[0].message),
2968 '.*keyword.*a, b, c.*')
2970 def test_deprecated_init_no_keywords_out_of_order(self):
2971 def init(self, **kwargs):
2972 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
2974 fn = gi.overrides.deprecated_init(init, arg_names=('b', 'a', 'c'))
2975 with warnings.catch_warnings(record=True) as warn:
2976 warnings.simplefilter('always')
2977 fn(self, 2, 1, 3)
2978 self.assertEqual(len(warn), 1)
2979 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
2980 self.assertRegexpMatches(str(warn[0].message),
2981 '.*keyword.*b, a, c.*')
2983 def test_deprecated_init_ignored_keyword(self):
2984 def init(self, **kwargs):
2985 self.assertDictEqual(kwargs, {'a': 1, 'c': 3})
2987 fn = gi.overrides.deprecated_init(init,
2988 arg_names=('a', 'b', 'c'),
2989 ignore=('b',))
2990 with warnings.catch_warnings(record=True) as warn:
2991 warnings.simplefilter('always')
2992 fn(self, 1, 2, 3)
2993 self.assertEqual(len(warn), 1)
2994 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
2995 self.assertRegexpMatches(str(warn[0].message),
2996 '.*keyword.*a, b, c.*')
2998 def test_deprecated_init_with_aliases(self):
2999 def init(self, **kwargs):
3000 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3002 fn = gi.overrides.deprecated_init(init,
3003 arg_names=('a', 'b', 'c'),
3004 deprecated_aliases={'b': 'bb', 'c': 'cc'})
3005 with warnings.catch_warnings(record=True) as warn:
3006 warnings.simplefilter('always')
3008 fn(self, a=1, bb=2, cc=3)
3009 self.assertEqual(len(warn), 1)
3010 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3011 self.assertRegexpMatches(str(warn[0].message),
3012 '.*keyword.*"bb, cc".*deprecated.*"b, c" respectively')
3014 def test_deprecated_init_with_defaults(self):
3015 def init(self, **kwargs):
3016 self.assertDictEqual(kwargs, {'a': 1, 'b': 2, 'c': 3})
3018 fn = gi.overrides.deprecated_init(init,
3019 arg_names=('a', 'b', 'c'),
3020 deprecated_defaults={'b': 2, 'c': 3})
3021 with warnings.catch_warnings(record=True) as warn:
3022 warnings.simplefilter('always')
3023 fn(self, a=1)
3024 self.assertEqual(len(warn), 1)
3025 self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
3026 self.assertRegexpMatches(str(warn[0].message),
3027 '.*relying on deprecated non-standard defaults.*'
3028 'explicitly use: b=2, c=3')