1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
|
import gc
import unittest
import sys
import weakref
import threading
import time
from gi.repository import GObject, GLib, Regress, Gio
from gi import _signalhelper as signalhelper
from gi.module import repository as repo
import testhelper
from .helper import capture_glib_warnings, capture_gi_deprecation_warnings
class C(GObject.GObject):
__gsignals__ = {
"my_signal": (GObject.SignalFlags.RUN_FIRST, None, (GObject.TYPE_INT,))
}
def do_my_signal(self, arg):
self.arg = arg
class D(C):
def do_my_signal(self, arg2):
self.arg2 = arg2
C.do_my_signal(self, arg2)
class TestSignalCreation(unittest.TestCase):
# Bug 540376.
def test_illegals(self):
self.assertRaises(
TypeError,
lambda: GObject.signal_new("test", None, 0, None, (GObject.TYPE_LONG,)),
)
class TestChaining(unittest.TestCase):
def setUp(self):
self.inst = C()
self.inst.connect("my_signal", self.my_signal_handler_cb, 1, 2, 3)
def my_signal_handler_cb(self, *args):
assert len(args) == 5
assert isinstance(args[0], C)
assert args[0] == self.inst
assert isinstance(args[1], int)
assert args[1] == 42
assert args[2:] == (1, 2, 3)
def test_chaining(self):
self.inst.emit("my_signal", 42)
assert self.inst.arg == 42
def test_chaining2(self):
inst2 = D()
inst2.emit("my_signal", 44)
assert inst2.arg == 44
assert inst2.arg2 == 44
# This is for bug 153718
class TestGSignalsError(unittest.TestCase):
def test_invalid_type(self, *args):
def foo():
class Foo(GObject.GObject):
__gsignals__ = None
self.assertRaises(TypeError, foo)
gc.collect()
def test_invalid_name(self, *args):
def foo():
class Foo(GObject.GObject):
__gsignals__ = {"not-exists": "override"}
with capture_glib_warnings(allow_warnings=True):
self.assertRaises(TypeError, foo)
gc.collect()
class TestGPropertyError(unittest.TestCase):
def test_invalid_type(self, *args):
def foo():
class Foo(GObject.GObject):
__gproperties__ = None
self.assertRaises(TypeError, foo)
gc.collect()
def test_invalid_name(self, *args):
def foo():
class Foo(GObject.GObject):
__gproperties__ = {None: None}
self.assertRaises(TypeError, foo)
gc.collect()
class TestList(unittest.TestCase):
def test_list_names(self):
self.assertEqual(GObject.signal_list_names(C), ("my-signal",))
def my_accumulator(ihint, return_accu, handler_return, user_data):
"""An accumulator that stops emission when the sum of handler
returned values reaches 3.
"""
assert user_data == "accum data"
if return_accu >= 3:
return False, return_accu
return True, return_accu + handler_return
class Foo(GObject.GObject):
my_acc_signal = GObject.Signal(
return_type=GObject.TYPE_INT,
flags=GObject.SignalFlags.RUN_LAST,
accumulator=my_accumulator,
accu_data="accum data",
)
my_other_acc_signal = GObject.Signal(
return_type=GObject.TYPE_BOOLEAN,
flags=GObject.SignalFlags.RUN_LAST,
accumulator=GObject.signal_accumulator_true_handled,
)
my_acc_first_wins = GObject.Signal(
return_type=GObject.TYPE_BOOLEAN,
flags=GObject.SignalFlags.RUN_LAST,
accumulator=GObject.signal_accumulator_first_wins,
)
class TestAccumulator(unittest.TestCase):
def test_accumulator(self):
inst = Foo()
inst.my_acc_signal.connect(lambda obj: 1)
inst.my_acc_signal.connect(lambda obj: 2)
# the value returned in the following handler will not be
# considered, because at this point the accumulator already
# reached its limit.
inst.my_acc_signal.connect(lambda obj: 3)
retval = inst.my_acc_signal.emit()
self.assertEqual(retval, 3)
def test_accumulator_true_handled(self):
inst = Foo()
inst.my_other_acc_signal.connect(self._true_handler1)
inst.my_other_acc_signal.connect(self._true_handler2)
# the following handler will not be called because handler2
# returns True, so it should stop the emission.
inst.my_other_acc_signal.connect(self._true_handler3)
self.__true_val = None
inst.my_other_acc_signal.emit()
self.assertEqual(self.__true_val, 2)
def test_accumulator_first_wins(self):
# First signal hit will always win
inst = Foo()
inst.my_acc_first_wins.connect(self._true_handler3)
inst.my_acc_first_wins.connect(self._true_handler1)
inst.my_acc_first_wins.connect(self._true_handler2)
self.__true_val = None
inst.my_acc_first_wins.emit()
self.assertEqual(self.__true_val, 3)
def _true_handler1(self, obj):
self.__true_val = 1
return False
def _true_handler2(self, obj):
self.__true_val = 2
return True
def _true_handler3(self, obj):
self.__true_val = 3
return False
class E(GObject.GObject):
__gsignals__ = {"signal": (GObject.SignalFlags.RUN_FIRST, None, ())}
# Property used to test detailed signal
prop = GObject.Property(type=int, default=0)
def __init__(self):
GObject.GObject.__init__(self)
self.status = 0
def do_signal(self):
assert self.status == 0
self.status = 1
class F(GObject.GObject):
__gsignals__ = {"signal": (GObject.SignalFlags.RUN_FIRST, None, ())}
def __init__(self):
GObject.GObject.__init__(self)
self.status = 0
def do_signal(self):
self.status += 1
class TestEmissionHook(unittest.TestCase):
def test_add(self):
self.hook = True
e = E()
e.connect("signal", self._callback)
GObject.add_emission_hook(E, "signal", self._emission_hook)
e.emit("signal")
self.assertEqual(e.status, 3)
def test_remove(self):
self.hook = False
e = E()
e.connect("signal", self._callback)
hook_id = GObject.add_emission_hook(E, "signal", self._emission_hook)
GObject.remove_emission_hook(E, "signal", hook_id)
e.emit("signal")
self.assertEqual(e.status, 3)
def _emission_hook(self, e):
self.assertEqual(e.status, 1)
e.status = 2
def _callback(self, e):
if self.hook:
self.assertEqual(e.status, 2)
else:
self.assertEqual(e.status, 1)
e.status = 3
def test_callback_return_false(self):
self.hook = False
obj = F()
def _emission_hook(obj):
obj.status += 1
return False
GObject.add_emission_hook(obj, "signal", _emission_hook)
obj.emit("signal")
obj.emit("signal")
self.assertEqual(obj.status, 3)
def test_callback_return_true(self):
self.hook = False
obj = F()
def _emission_hook(obj):
obj.status += 1
return True
hook_id = GObject.add_emission_hook(obj, "signal", _emission_hook)
obj.emit("signal")
obj.emit("signal")
GObject.remove_emission_hook(obj, "signal", hook_id)
self.assertEqual(obj.status, 4)
def test_callback_return_true_but_remove(self):
self.hook = False
obj = F()
def _emission_hook(obj):
obj.status += 1
return True
hook_id = GObject.add_emission_hook(obj, "signal", _emission_hook)
obj.emit("signal")
GObject.remove_emission_hook(obj, "signal", hook_id)
obj.emit("signal")
self.assertEqual(obj.status, 3)
class TestMatching(unittest.TestCase):
class Object(GObject.Object):
status = 0
prop = GObject.Property(type=int, default=0)
@GObject.Signal()
def my_signal(self):
pass
@unittest.expectedFailure # https://bugzilla.gnome.org/show_bug.cgi?id=692918
def test_signal_handler_block_matching(self):
def dummy(*args):
"""Hack to work around:"""
def foo(obj):
obj.status += 1
obj = self.Object()
handler_id = GObject.signal_connect_closure(obj, "my-signal", foo, after=False)
handler_id
self.assertEqual(obj.status, 0)
obj.emit("my-signal")
self.assertEqual(obj.status, 1)
# Blocking by match criteria disables the foo callback
signal_id, detail = GObject.signal_parse_name("my-signal", obj, True)
count = GObject.signal_handlers_block_matched(
obj,
GObject.SignalMatchType.ID | GObject.SignalMatchType.CLOSURE,
signal_id=signal_id,
detail=detail,
closure=foo,
func=dummy,
data=dummy,
)
self.assertEqual(count, 1)
obj.emit("my-signal")
self.assertEqual(obj.status, 1)
# Unblocking by the same match criteria allows callback to work again
count = GObject.signal_handlers_unblock_matched(
obj,
GObject.SignalMatchType.ID | GObject.SignalMatchType.CLOSURE,
signal_id=signal_id,
detail=detail,
closure=foo,
func=dummy,
data=dummy,
)
self.assertEqual(count, 1)
obj.emit("my-signal")
self.assertEqual(obj.status, 2)
# Disconnecting by match criteria completely removes the handler
count = GObject.signal_handlers_disconnect_matched(
obj,
GObject.SignalMatchType.ID | GObject.SignalMatchType.CLOSURE,
signal_id=signal_id,
detail=detail,
closure=foo,
func=dummy,
data=dummy,
)
self.assertEqual(count, 1)
obj.emit("my-signal")
self.assertEqual(obj.status, 2)
def test_signal_handler_find(self):
def foo(obj):
obj.status += 1
obj = self.Object()
handler_id = GObject.signal_connect_closure(obj, "my-signal", foo, after=False)
signal_id, detail = GObject.signal_parse_name("my-signal", obj, True)
found_id = GObject.signal_handler_find(
obj,
GObject.SignalMatchType.ID,
signal_id=signal_id,
detail=detail,
closure=None,
func=0,
data=0,
)
self.assertEqual(handler_id, found_id)
class TestClosures(unittest.TestCase):
def setUp(self):
self.count = 0
self.emission_stopped = False
self.emission_error = False
self.handler_pending = False
def _callback_handler_pending(self, e):
signal_id, detail = GObject.signal_parse_name("signal", e, True)
self.handler_pending = GObject.signal_has_handler_pending(
e, signal_id, detail, may_be_blocked=False
)
def _callback(self, e):
self.count += 1
def _callback_stop_emission(self, obj, prop, stop_it):
if stop_it:
obj.stop_emission_by_name("notify::prop")
self.emission_stopped = True
else:
self.count += 1
def _callback_invalid_stop_emission_name(self, obj, prop):
with capture_glib_warnings(allow_warnings=True, allow_criticals=True) as warn:
obj.stop_emission_by_name("notasignal::baddetail")
self.emission_error = True
self.assertTrue(warn)
def test_disconnect_by_func(self):
e = E()
e.connect("signal", self._callback)
e.disconnect_by_func(self._callback)
e.emit("signal")
self.assertEqual(self.count, 0)
def test_disconnect(self):
e = E()
handler_id = e.connect("signal", self._callback)
self.assertTrue(e.handler_is_connected(handler_id))
e.disconnect(handler_id)
e.emit("signal")
self.assertEqual(self.count, 0)
self.assertFalse(e.handler_is_connected(handler_id))
def test_stop_emission_by_name(self):
e = E()
# Sandwich a callback that stops emission in between a callback that increments
e.connect("notify::prop", self._callback_stop_emission, False)
e.connect("notify::prop", self._callback_stop_emission, True)
e.connect("notify::prop", self._callback_stop_emission, False)
e.set_property("prop", 1234)
self.assertEqual(e.get_property("prop"), 1234)
self.assertEqual(self.count, 1)
self.assertTrue(self.emission_stopped)
def test_stop_emission_by_name_error(self):
e = E()
e.connect("notify::prop", self._callback_invalid_stop_emission_name)
with capture_glib_warnings():
e.set_property("prop", 1234)
self.assertTrue(self.emission_error)
def test_handler_block(self):
e = E()
e.connect("signal", self._callback)
e.handler_block_by_func(self._callback)
e.emit("signal")
self.assertEqual(self.count, 0)
def test_handler_unblock(self):
e = E()
handler_id = e.connect("signal", self._callback)
e.handler_block(handler_id)
e.handler_unblock_by_func(self._callback)
e.emit("signal")
self.assertEqual(self.count, 1)
def test_handler_block_method(self):
# Filed as #375589
class A:
def __init__(self):
self.a = 0
def callback(self, o):
self.a = 1
o.handler_block_by_func(self.callback)
inst = A()
e = E()
e.connect("signal", inst.callback)
e.emit("signal")
self.assertEqual(inst.a, 1)
gc.collect()
def test_gstring(self):
class C(GObject.GObject):
__gsignals__ = {
"my_signal": (
GObject.SignalFlags.RUN_LAST,
GObject.TYPE_GSTRING,
(GObject.TYPE_GSTRING,),
)
}
def __init__(self, test):
GObject.GObject.__init__(self)
self.test = test
def do_my_signal(self, data):
self.data = data
self.test.assertEqual(len(data), 3)
return "".join([data[2], data[1], data[0]])
c = C(self)
data = c.emit("my_signal", "\01\00\02")
self.assertEqual(data, "\02\00\01")
def test_handler_pending(self):
obj = F()
obj.connect("signal", self._callback_handler_pending)
obj.connect("signal", self._callback)
self.assertEqual(self.count, 0)
self.assertEqual(self.handler_pending, False)
obj.emit("signal")
self.assertEqual(self.count, 1)
self.assertEqual(self.handler_pending, True)
def test_signal_handlers_destroy(self):
obj = F()
obj.connect("signal", self._callback)
obj.connect("signal", self._callback)
obj.connect("signal", self._callback)
obj.emit("signal")
self.assertEqual(self.count, 3)
# count should remain at 3 after all handlers are destroyed
GObject.signal_handlers_destroy(obj)
obj.emit("signal")
self.assertEqual(self.count, 3)
class SigPropClass(GObject.GObject):
__gsignals__ = {
"my_signal": (GObject.SignalFlags.RUN_FIRST, None, (GObject.TYPE_INT,))
}
__gproperties__ = {
"foo": (
str,
None,
None,
"",
GObject.ParamFlags.WRITABLE | GObject.ParamFlags.CONSTRUCT,
),
}
signal_emission_failed = False
def do_my_signal(self, arg):
self.arg = arg
def do_set_property(self, pspec, value):
if pspec.name == "foo":
self._foo = value
else:
raise AttributeError(f"unknown property {pspec.name}")
try:
self.emit("my-signal", 1)
except TypeError:
self.signal_emission_failed = True
class TestSigProp(unittest.TestCase):
def test_emit_in_property_setter(self):
obj = SigPropClass()
self.assertFalse(obj.signal_emission_failed)
class CM(GObject.GObject):
__gsignals__ = {
"test1": (GObject.SignalFlags.RUN_FIRST, None, ()),
"test2": (GObject.SignalFlags.RUN_LAST, None, (str,)),
"test3": (GObject.SignalFlags.RUN_LAST, int, (GObject.TYPE_DOUBLE,)),
"test4": (
GObject.SignalFlags.RUN_FIRST,
None,
(
bool,
int,
GObject.TYPE_FLOAT,
GObject.TYPE_DOUBLE,
int,
GObject.TYPE_UINT,
GObject.TYPE_ULONG,
),
),
"test_float": (
GObject.SignalFlags.RUN_LAST,
GObject.TYPE_FLOAT,
(GObject.TYPE_FLOAT,),
),
"test_double": (
GObject.SignalFlags.RUN_LAST,
GObject.TYPE_DOUBLE,
(GObject.TYPE_DOUBLE,),
),
"test_int64": (
GObject.SignalFlags.RUN_LAST,
GObject.TYPE_INT64,
(GObject.TYPE_INT64,),
),
"test_string": (GObject.SignalFlags.RUN_LAST, str, (str,)),
"test_object": (GObject.SignalFlags.RUN_LAST, object, (object,)),
"test_paramspec": (GObject.SignalFlags.RUN_LAST, GObject.ParamSpec, ()),
"test_paramspec_in": (
GObject.SignalFlags.RUN_LAST,
GObject.ParamSpec,
(GObject.ParamSpec,),
),
"test_gvalue": (GObject.SignalFlags.RUN_LAST, GObject.Value, (GObject.Value,)),
"test_gvalue_ret": (
GObject.SignalFlags.RUN_LAST,
GObject.Value,
(GObject.TYPE_GTYPE,),
),
}
testprop = GObject.Property(type=int)
class _TestCMarshaller:
def setUp(self):
self.obj = CM()
testhelper.connectcallbacks(self.obj)
def test_test1(self):
self.obj.emit("test1")
def test_test2(self):
self.obj.emit("test2", "string")
def test_test3(self):
rv = self.obj.emit("test3", 42.0)
self.assertEqual(rv, 20)
def test_test4(self):
self.obj.emit("test4", True, 10, 3.14, 1.78, 20, 30, 31)
def test_float(self):
rv = self.obj.emit("test-float", 1.234)
self.assertTrue(rv >= 1.233999 and rv <= 1.2400001, rv)
def test_double(self):
rv = self.obj.emit("test-double", 1.234)
self.assertEqual(rv, 1.234)
def test_int64(self):
rv = self.obj.emit("test-int64", 102030405)
self.assertEqual(rv, 102030405)
rv = self.obj.emit("test-int64", GLib.MAXINT64)
self.assertEqual(rv, GLib.MAXINT64 - 1)
rv = self.obj.emit("test-int64", GLib.MININT64)
self.assertEqual(rv, GLib.MININT64)
def test_string(self):
rv = self.obj.emit("test-string", "str")
self.assertEqual(rv, "str")
def test_object(self):
rv = self.obj.emit("test-object", self)
self.assertEqual(rv, self)
def test_paramspec(self):
rv = self.obj.emit("test-paramspec")
self.assertEqual(rv.name, "test-param")
self.assertEqual(rv.nick, "test")
def test_paramspec_in(self):
rv = GObject.param_spec_boolean(
"mybool", "test-bool", "do something", True, GObject.ParamFlags.READABLE
)
rv2 = self.obj.emit("test-paramspec-in", rv)
self.assertEqual(type(rv), type(rv2))
self.assertEqual(rv2.name, "mybool")
self.assertEqual(rv2.nick, "test-bool")
def test_C_paramspec(self):
self.notify_called = False
def cb_notify(obj, prop):
self.notify_called = True
self.assertEqual(obj, self.obj)
self.assertEqual(prop.name, "testprop")
self.obj.connect("notify", cb_notify)
self.obj.set_property("testprop", 42)
self.assertTrue(self.notify_called)
def test_gvalue(self):
# implicit int
rv = self.obj.emit("test-gvalue", 42)
self.assertEqual(rv, 42)
# explicit float
v = GObject.Value(GObject.TYPE_FLOAT, 1.234)
rv = self.obj.emit("test-gvalue", v)
self.assertAlmostEqual(rv, 1.234, places=4)
# implicit float
rv = self.obj.emit("test-gvalue", 1.234)
self.assertAlmostEqual(rv, 1.234, places=4)
# explicit int64
v = GObject.Value(GObject.TYPE_INT64, GLib.MAXINT64)
rv = self.obj.emit("test-gvalue", v)
self.assertEqual(rv, GLib.MAXINT64)
# explicit uint64
v = GObject.Value(GObject.TYPE_UINT64, GLib.MAXUINT64)
rv = self.obj.emit("test-gvalue", v)
self.assertEqual(rv, GLib.MAXUINT64)
@unittest.expectedFailure # https://bugzilla.gnome.org/show_bug.cgi?id=705291
def test_gvalue_implicit_int64(self):
# implicit int64
rv = self.obj.emit("test-gvalue", GLib.MAXINT64)
self.assertEqual(rv, GLib.MAXINT64)
# implicit uint64
rv = self.obj.emit("test-gvalue", GLib.MAXUINT64)
self.assertEqual(rv, GLib.MAXUINT64)
def test_gvalue_ret(self):
self.assertEqual(
self.obj.emit("test-gvalue-ret", GObject.TYPE_INT), GLib.MAXINT
)
self.assertEqual(
self.obj.emit("test-gvalue-ret", GObject.TYPE_UINT), GLib.MAXUINT
)
self.assertEqual(
self.obj.emit("test-gvalue-ret", GObject.TYPE_INT64), GLib.MAXINT64
)
self.assertEqual(
self.obj.emit("test-gvalue-ret", GObject.TYPE_UINT64), GLib.MAXUINT64
)
self.assertEqual(self.obj.emit("test-gvalue-ret", GObject.TYPE_STRING), "hello")
class TestCMarshaller(_TestCMarshaller, unittest.TestCase):
pass
# Test for 374653
class TestPyGValue(unittest.TestCase):
def test_none_null_boxed_conversion(self):
class C(GObject.GObject):
__gsignals__ = {
"my_boxed_signal": (GObject.SignalFlags.RUN_LAST, GObject.TYPE_STRV, ())
}
obj = C()
obj.connect("my-boxed-signal", lambda obj: None)
sys.last_type = None
obj.emit("my-boxed-signal")
assert not sys.last_type
class TestSignalDecorator(unittest.TestCase):
class Decorated(GObject.GObject):
value = 0
@GObject.Signal
def pushed(self):
"""This will push."""
self.value += 1
@GObject.Signal(flags=GObject.SignalFlags.RUN_LAST)
def pulled(self):
self.value -= 1
@GObject.Signal(flags=GObject.SignalFlags.DETAILED)
def detailed(self):
self.value -= 1
stomped = GObject.Signal("stomped", arg_types=(int,), doc="This will stomp.")
unnamed = GObject.Signal()
class DecoratedOverride(GObject.GObject):
overridden_closure_called = False
notify_called = False
value = GObject.Property(type=int, default=0)
@GObject.SignalOverride
def notify(self, *args, **kargs):
self.overridden_closure_called = True
def on_notify(self, obj, prop):
self.notify_called = True
def setUp(self):
self.unnamedCalled = False
def onUnnamed(self, obj):
self.unnamedCalled = True
def test_disconnect(self):
decorated = self.Decorated()
id_ = decorated.pushed.connect(lambda *args: None)
decorated.pushed.disconnect(id_)
def test_signal_repr(self):
decorated = self.Decorated()
assert repr(decorated.pushed) == 'BoundSignal("pushed")'
def test_signal_call(self):
decorated = self.Decorated()
assert decorated.value == 0
decorated.pushed()
assert decorated.value == 1
def test_connect_detailed(self):
decorated = self.Decorated()
id_ = decorated.detailed.connect_detailed(lambda *args: None, "foo")
decorated.pushed.disconnect(id_)
def test_get_signal_args(self):
self.assertEqual(
self.Decorated.pushed.get_signal_args(),
(GObject.SignalFlags.RUN_FIRST, None, (), None, None),
)
self.assertEqual(
self.Decorated.pulled.get_signal_args(),
(GObject.SignalFlags.RUN_LAST, None, (), None, None),
)
self.assertEqual(
self.Decorated.stomped.get_signal_args(),
(GObject.SignalFlags.RUN_FIRST, None, (int,), None, None),
)
def test_closures_called(self):
decorated = self.Decorated()
self.assertEqual(decorated.value, 0)
decorated.pushed.emit()
self.assertEqual(decorated.value, 1)
decorated.pulled.emit()
self.assertEqual(decorated.value, 0)
def test_signal_copy(self):
blah = self.Decorated.stomped.copy("blah")
self.assertEqual(str(blah), blah)
self.assertEqual(blah.func, self.Decorated.stomped.func)
self.assertEqual(blah.flags, self.Decorated.stomped.flags)
self.assertEqual(blah.return_type, self.Decorated.stomped.return_type)
self.assertEqual(blah.arg_types, self.Decorated.stomped.arg_types)
self.assertEqual(blah.__doc__, self.Decorated.stomped.__doc__)
def test_doc_string(self):
# Test the two techniques for setting doc strings on the signals
# class variables, through the "doc" keyword or as the getter doc string.
self.assertEqual(self.Decorated.stomped.__doc__, "This will stomp.")
self.assertEqual(self.Decorated.pushed.__doc__, "This will push.")
def test_unnamed_signal_gets_named(self):
self.assertEqual(str(self.Decorated.unnamed), "unnamed")
def test_unnamed_signal_gets_called(self):
obj = self.Decorated()
obj.connect("unnamed", self.onUnnamed)
self.assertEqual(self.unnamedCalled, False)
obj.emit("unnamed")
self.assertEqual(self.unnamedCalled, True)
def test_overridden_signal(self):
# Test that the pushed signal is called in with super and the override
# which should both increment the "value" to 3
obj = self.DecoratedOverride()
obj.connect("notify", obj.on_notify)
self.assertEqual(obj.value, 0)
obj.value = 1
self.assertEqual(obj.value, 1)
self.assertTrue(obj.overridden_closure_called)
self.assertTrue(obj.notify_called)
class TestSignalConnectors(unittest.TestCase):
class CustomButton(GObject.GObject):
on_notify_called = False
value = GObject.Property(type=int)
@GObject.Signal(arg_types=(int,))
def clicked(self, value):
self.value = value
def setUp(self):
self.obj = None
self.value = None
def on_clicked(self, obj, value):
self.obj = obj
self.value = value
def test_signal_notify(self):
def on_notify(obj, param):
obj.on_notify_called = True
obj = self.CustomButton()
obj.connect("notify", on_notify)
self.assertFalse(obj.on_notify_called)
obj.notify("value")
self.assertTrue(obj.on_notify_called)
def test_signal_emit(self):
# standard callback connection with different forms of emit.
obj = self.CustomButton()
obj.connect("clicked", self.on_clicked)
# vanilla
obj.emit("clicked", 1)
self.assertEqual(obj.value, 1)
self.assertEqual(obj, self.obj)
self.assertEqual(self.value, 1)
# using class signal as param
self.obj = None
self.value = None
obj.emit(self.CustomButton.clicked, 1)
self.assertEqual(obj, self.obj)
self.assertEqual(self.value, 1)
# using bound signal as param
self.obj = None
self.value = None
obj.emit(obj.clicked, 1)
self.assertEqual(obj, self.obj)
self.assertEqual(self.value, 1)
# using bound signal with emit
self.obj = None
self.value = None
obj.clicked.emit(1)
self.assertEqual(obj, self.obj)
self.assertEqual(self.value, 1)
def test_signal_class_connect(self):
obj = self.CustomButton()
obj.connect(self.CustomButton.clicked, self.on_clicked)
obj.emit("clicked", 2)
self.assertEqual(obj, self.obj)
self.assertEqual(self.value, 2)
def test_signal_bound_connect(self):
obj = self.CustomButton()
obj.clicked.connect(self.on_clicked)
obj.emit("clicked", 3)
self.assertEqual(obj, self.obj)
self.assertEqual(self.value, 3)
class _ConnectDataTestBase:
# Notes:
# - self.Object is overridden in sub-classes.
# - Numeric suffixes indicate the number of user data args passed in.
Object = None
def run_connect_test(self, emit_args, user_data, flags=0):
obj = self.Object()
callback_args = []
def callback(*args):
callback_args.append(args)
return 0
obj.connect_data(
"sig-with-int64-prop", callback, connect_flags=flags, *user_data
)
obj.emit("sig-with-int64-prop", *emit_args)
self.assertEqual(len(callback_args), 1)
return callback_args[0]
def test_0(self):
obj, value = self.run_connect_test([GLib.MAXINT64], user_data=[])
self.assertIsInstance(obj, self.Object)
self.assertEqual(value, GLib.MAXINT64)
def test_1(self):
obj, value, data = self.run_connect_test([GLib.MAXINT64], user_data=["mydata"])
self.assertIsInstance(obj, self.Object)
self.assertEqual(value, GLib.MAXINT64)
self.assertEqual(data, "mydata")
def test_after_0(self):
obj, value = self.run_connect_test(
[GLib.MAXINT64], user_data=[], flags=GObject.ConnectFlags.AFTER
)
self.assertIsInstance(obj, self.Object)
self.assertEqual(value, GLib.MAXINT64)
def test_after_1(self):
obj, value, data = self.run_connect_test(
[GLib.MAXINT64], user_data=["mydata"], flags=GObject.ConnectFlags.AFTER
)
self.assertIsInstance(obj, self.Object)
self.assertEqual(value, GLib.MAXINT64)
self.assertEqual(data, "mydata")
def test_swaped_0(self):
# Swapped only works with a single user data argument.
with self.assertRaises(ValueError):
self.run_connect_test(
[GLib.MAXINT64], user_data=[], flags=GObject.ConnectFlags.SWAPPED
)
def test_swaped_1(self):
# Notice obj and data are reversed in the return.
data, value, obj = self.run_connect_test(
[GLib.MAXINT64], user_data=["mydata"], flags=GObject.ConnectFlags.SWAPPED
)
self.assertIsInstance(obj, self.Object)
self.assertEqual(value, GLib.MAXINT64)
self.assertEqual(data, "mydata")
def test_swaped_2(self):
# Swapped only works with a single user data argument.
with self.assertRaises(ValueError):
self.run_connect_test(
[GLib.MAXINT64], user_data=[1, 2], flags=GObject.ConnectFlags.SWAPPED
)
def test_after_and_swapped_0(self):
# Swapped only works with a single user data argument.
with self.assertRaises(ValueError):
self.run_connect_test(
[GLib.MAXINT64],
user_data=[],
flags=GObject.ConnectFlags.AFTER | GObject.ConnectFlags.SWAPPED,
)
def test_after_and_swapped_1(self):
# Notice obj and data are reversed in the return.
data, value, obj = self.run_connect_test(
[GLib.MAXINT64],
user_data=["mydata"],
flags=GObject.ConnectFlags.AFTER | GObject.ConnectFlags.SWAPPED,
)
self.assertIsInstance(obj, self.Object)
self.assertEqual(value, GLib.MAXINT64)
self.assertEqual(data, "mydata")
def test_after_and_swapped_2(self):
# Swapped only works with a single user data argument.
with self.assertRaises(ValueError):
self.run_connect_test(
[GLib.MAXINT64],
user_data=[],
flags=GObject.ConnectFlags.AFTER | GObject.ConnectFlags.SWAPPED,
)
class TestConnectDataNonIntrospected(unittest.TestCase, _ConnectDataTestBase):
# This tests connect_data with non-introspected signals
# (created in Python in this case).
class Object(GObject.Object):
test = GObject.Signal()
sig_with_int64_prop = GObject.Signal(
return_type=GObject.TYPE_INT64,
arg_types=[GObject.TYPE_INT64],
flags=GObject.SignalFlags.RUN_LAST,
)
class TestConnectDataIntrospected(unittest.TestCase, _ConnectDataTestBase):
# This tests connect_data with introspected signals brought in from Regress.
Object = Regress.TestObj
class TestInstallSignals(unittest.TestCase):
# These tests only test how signalhelper.install_signals works
# with the __gsignals__ dict and therefore does not need to use
# GObject as a base class because that would automatically call
# install_signals within the meta-class.
class Base:
__gsignals__ = {"test": (0, None, ())}
class Sub1(Base):
pass
class Sub2(Base):
@GObject.Signal
def sub2test(self):
pass
def setUp(self):
self.assertEqual(len(self.Base.__gsignals__), 1)
signalhelper.install_signals(self.Base)
self.assertEqual(len(self.Base.__gsignals__), 1)
def test_subclass_gets_empty_gsignals_dict(self):
# Installing signals will add the __gsignals__ dict to a class
# if it doesn't already exists.
self.assertFalse("__gsignals__" in self.Sub1.__dict__)
signalhelper.install_signals(self.Sub1)
self.assertTrue("__gsignals__" in self.Sub1.__dict__)
# Sub1 should only contain an empty signals dict, this tests:
# https://bugzilla.gnome.org/show_bug.cgi?id=686496
self.assertEqual(self.Sub1.__dict__["__gsignals__"], {})
def test_subclass_with_decorator_gets_gsignals_dict(self):
self.assertFalse("__gsignals__" in self.Sub2.__dict__)
signalhelper.install_signals(self.Sub2)
self.assertTrue("__gsignals__" in self.Sub2.__dict__)
self.assertEqual(len(self.Base.__gsignals__), 1)
self.assertEqual(len(self.Sub2.__gsignals__), 1)
self.assertTrue("sub2test" in self.Sub2.__gsignals__)
# Make sure the vfunc was added
self.assertTrue(hasattr(self.Sub2, "do_sub2test"))
class TestPython3Signals(unittest.TestCase):
class AnnotatedClass(GObject.GObject):
@GObject.Signal
def sig1(self, a: int, b: float):
pass
@GObject.Signal(flags=GObject.SignalFlags.RUN_LAST)
def sig2_with_return(self, a: int, b: float) -> str:
return "test"
def test_annotations(self):
self.assertEqual(
signalhelper.get_signal_annotations(self.AnnotatedClass.sig1.func),
(None, (int, float)),
)
self.assertEqual(
signalhelper.get_signal_annotations(
self.AnnotatedClass.sig2_with_return.func
),
(str, (int, float)),
)
self.assertEqual(
self.AnnotatedClass.sig2_with_return.get_signal_args(),
(GObject.SignalFlags.RUN_LAST, str, (int, float), None, None),
)
self.assertEqual(self.AnnotatedClass.sig2_with_return.arg_types, (int, float))
self.assertEqual(self.AnnotatedClass.sig2_with_return.return_type, str)
def test_emit_return(self):
obj = self.AnnotatedClass()
self.assertEqual(obj.sig2_with_return.emit(1, 2.0), "test")
class TestSignalModuleLevelFunctions(unittest.TestCase):
def test_signal_list_ids_with_invalid_type(self):
with self.assertRaisesRegex(
TypeError, "type must be instantiable or an interface.*"
):
GObject.signal_list_ids(GObject.TYPE_INVALID)
def test_signal_list_ids(self):
with self.assertRaisesRegex(
TypeError, "type must be instantiable or an interface.*"
):
GObject.signal_list_ids(GObject.TYPE_INT)
ids = GObject.signal_list_ids(C)
self.assertEqual(len(ids), 1)
# Note canonicalized names
self.assertEqual(GObject.signal_name(ids[0]), "my-signal")
# There is no signal 0 in gobject
self.assertEqual(GObject.signal_name(0), None)
def test_signal_lookup_with_invalid_type(self):
with self.assertRaisesRegex(
TypeError, "type must be instantiable or an interface.*"
):
GObject.signal_lookup("NOT_A_SIGNAL_NAME", GObject.TYPE_INVALID)
def test_signal_lookup(self):
ids = GObject.signal_list_ids(C)
self.assertEqual(ids[0], GObject.signal_lookup("my_signal", C))
self.assertEqual(ids[0], GObject.signal_lookup("my-signal", C))
with self.assertRaisesRegex(
TypeError, "type must be instantiable or an interface.*"
):
GObject.signal_lookup("NOT_A_SIGNAL_NAME", GObject.TYPE_INT)
# Invalid signal names return 0 instead of raising
self.assertEqual(GObject.signal_lookup("NOT_A_SIGNAL_NAME", C), 0)
def test_signal_query(self):
(my_signal_id,) = GObject.signal_list_ids(C)
# Form is: (id, name, gtype, arg_count, return_type, (arg_type1, ...))
my_signal_expected_query_result = [
my_signal_id,
"my-signal",
C.__gtype__,
GObject.SignalFlags.RUN_FIRST,
GObject.TYPE_NONE,
(GObject.TYPE_INT,),
]
# signal_query(name, type)
self.assertEqual(
list(GObject.signal_query("my-signal", C)), my_signal_expected_query_result
)
# signal_query(signal_id)
self.assertEqual(
list(GObject.signal_query(my_signal_id)), my_signal_expected_query_result
)
# invalid query returns None instead of raising
self.assertEqual(GObject.signal_query(0), None)
self.assertEqual(GObject.signal_query("NOT_A_SIGNAL", C), None)
class TestIntrospectedSignals(unittest.TestCase):
def test_object_param_signal(self):
obj = Regress.TestObj()
def callback(obj, obj_param):
self.assertEqual(obj_param.props.int, 3)
self.assertGreater(obj_param.__grefcount__, 1)
obj.called = True
obj.called = False
obj.connect("sig-with-obj", callback)
obj.emit_sig_with_obj()
self.assertTrue(obj.called)
def test_connect_after(self):
obj = Regress.TestObj()
def callback(obj, obj_param):
obj.called = True
obj.called = False
obj.connect_after("sig-with-obj", callback)
obj.emit_sig_with_obj()
self.assertTrue(obj.called)
def test_int64_param_from_py(self):
obj = Regress.TestObj()
def callback(obj, i):
obj.callback_i = i
return i
obj.callback_i = None
obj.connect("sig-with-int64-prop", callback)
rv = obj.emit("sig-with-int64-prop", GLib.MAXINT64)
self.assertEqual(rv, GLib.MAXINT64)
self.assertEqual(obj.callback_i, GLib.MAXINT64)
def test_uint64_param_from_py(self):
obj = Regress.TestObj()
def callback(obj, i):
obj.callback_i = i
return i
obj.callback_i = None
obj.connect("sig-with-uint64-prop", callback)
rv = obj.emit("sig-with-uint64-prop", GLib.MAXUINT64)
self.assertEqual(rv, GLib.MAXUINT64)
self.assertEqual(obj.callback_i, GLib.MAXUINT64)
def test_int64_param_from_c(self):
obj = Regress.TestObj()
def callback(obj, i):
obj.callback_i = i
return i
obj.callback_i = None
obj.connect("sig-with-int64-prop", callback)
obj.emit_sig_with_int64()
self.assertEqual(obj.callback_i, GLib.MAXINT64)
def test_uint64_param_from_c(self):
obj = Regress.TestObj()
def callback(obj, i):
obj.callback_i = i
return i
obj.callback_i = None
obj.connect("sig-with-uint64-prop", callback)
obj.emit_sig_with_uint64()
self.assertEqual(obj.callback_i, GLib.MAXUINT64)
def test_intarray_ret(self):
obj = Regress.TestObj()
def callback(obj, i):
obj.callback_i = i
return [i, i + 1]
obj.callback_i = None
try:
obj.connect("sig-with-intarray-ret", callback)
except TypeError as e:
# compat with g-i 1.34.x
if "unknown signal" in str(e):
return
raise
rv = obj.emit("sig-with-intarray-ret", 42)
self.assertEqual(obj.callback_i, 42)
self.assertEqual(type(rv), GLib.Array)
self.assertEqual(rv.len, 2)
@unittest.skip("https://bugzilla.gnome.org/show_bug.cgi?id=669496")
def test_array_parm(self):
obj = Regress.TestObj()
def callback(obj, arr):
obj.callback_arr = arr
obj.connect("sig-with-array-prop", callback)
obj.callback_arr = None
self.assertEqual(obj.emit("sig-with-array-prop", [1, 2, GLib.MAXUINT]), None)
self.assertEqual(obj.callback_arr, [1, 2, GLib.MAXUINT])
def test_held_struct_ref(self):
held_structs = []
def callback(obj, struct):
# The struct held by Python will become a copy after this callback exits.
struct.some_int = 42
struct.some_int8 = 42
held_structs.append(struct)
struct = Regress.TestSimpleBoxedA()
obj = Regress.TestObj()
self.assertEqual(struct.some_int, 0)
self.assertEqual(struct.some_int8, 0)
obj.connect("test-with-static-scope-arg", callback)
obj.emit("test-with-static-scope-arg", struct)
# The held struct will be a copy of the modified struct.
self.assertEqual(len(held_structs), 1)
held_struct = held_structs[0]
self.assertEqual(held_struct.some_int, 42)
self.assertEqual(held_struct.some_int8, 42)
# Boxed equality checks pointers by default.
self.assertNotEqual(struct, held_struct)
def test_action(self):
obj = Regress.TestAction()
other_obj = obj.emit("action")
self.assertEqual(other_obj.__grefcount__, 1)
other_obj2 = obj.emit("action2")
self.assertIsNone(other_obj2)
class TestIntrospectedSignalsIssue158(unittest.TestCase):
"""The test for https://gitlab.gnome.org/GNOME/pygobject/issues/158."""
_obj_sig_names = [
sig.get_name() for sig in repo.find_by_name("Regress", "TestObj").get_signals()
]
def __init__(self, *args):
unittest.TestCase.__init__(self, *args)
self._gc_thread_stop = False
def _gc_thread(self):
while not self._gc_thread_stop:
gc.collect()
time.sleep(0.010)
def _callback(self, *args):
pass
def test_run(self):
"""Manually trigger GC from a different thread periodicaly
while the main thread keeps connecting/disconnecting to/from signals.
It takes a lot of time to reproduce the issue. It is possible to make it
fail reliably by changing the code of pygobject_unwatch_closure slightly from:
PyGObjectData *inst_data = data;
inst_data->closures = g_slist_remove (inst_data->closures, closure);
to
PyGObjectData *inst_data = data;
GSList *tmp = g_slist_remove (inst_data->closures, closure);
g_usleep(G_USEC_PER_SEC/10);
inst_data->closures = tmp;
"""
obj = Regress.TestObj()
gc_thread = threading.Thread(target=self._gc_thread)
gc_thread.start()
for _ in range(8):
handlers = [obj.connect(sig, self._callback) for sig in self._obj_sig_names]
time.sleep(0.010)
while len(handlers) > 0:
obj.disconnect(handlers.pop())
self._gc_thread_stop = True
gc_thread.join()
class _ConnectObjectTestBase:
# Notes:
# - self.Object is overridden in sub-classes.
# - Numeric suffixes indicate the number of user data args passed in.
Object = None
SwapObject = None
def run_connect_test(self, emit_args, user_data, flags=0):
obj = self.Object()
callback_args = []
swap_obj = self.SwapObject()
def callback(*args):
callback_args.append(args)
return 0
if flags & GObject.ConnectFlags.AFTER:
connect_func = obj.connect_object_after
else:
connect_func = obj.connect_object
with capture_gi_deprecation_warnings():
connect_func("sig-with-int64-prop", callback, swap_obj, *user_data)
obj.emit("sig-with-int64-prop", *emit_args)
self.assertEqual(len(callback_args), 1)
return callback_args[0]
def test_0(self):
obj, value = self.run_connect_test([GLib.MAXINT64], user_data=[])
self.assertIsInstance(obj, self.SwapObject)
self.assertEqual(value, GLib.MAXINT64)
def test_1(self):
obj, value, data = self.run_connect_test([GLib.MAXINT64], user_data=["mydata"])
self.assertIsInstance(obj, self.SwapObject)
self.assertEqual(value, GLib.MAXINT64)
self.assertEqual(data, "mydata")
def test_2(self):
obj, value, data1, data2 = self.run_connect_test(
[GLib.MAXINT64], user_data=["mydata1", "mydata2"]
)
self.assertIsInstance(obj, self.SwapObject)
self.assertEqual(value, GLib.MAXINT64)
self.assertEqual(data1, "mydata1")
self.assertEqual(data2, "mydata2")
def test_after_0(self):
obj, value = self.run_connect_test(
[GLib.MAXINT64], user_data=[], flags=GObject.ConnectFlags.AFTER
)
self.assertIsInstance(obj, self.SwapObject)
self.assertEqual(value, GLib.MAXINT64)
def test_after_1(self):
obj, value, data = self.run_connect_test(
[GLib.MAXINT64], user_data=["mydata"], flags=GObject.ConnectFlags.AFTER
)
self.assertIsInstance(obj, self.SwapObject)
self.assertEqual(value, GLib.MAXINT64)
self.assertEqual(data, "mydata")
def test_after_2(self):
obj, value, data1, data2 = self.run_connect_test(
[GLib.MAXINT64],
user_data=["mydata1", "mydata2"],
flags=GObject.ConnectFlags.AFTER,
)
self.assertIsInstance(obj, self.SwapObject)
self.assertEqual(value, GLib.MAXINT64)
self.assertEqual(data1, "mydata1")
self.assertEqual(data2, "mydata2")
class TestConnectGObjectNonIntrospected(unittest.TestCase, _ConnectObjectTestBase):
# This tests connect_object with non-introspected signals
# (created in Python in this case).
class Object(GObject.Object):
test = GObject.Signal()
sig_with_int64_prop = GObject.Signal(
return_type=GObject.TYPE_INT64,
arg_types=[GObject.TYPE_INT64],
flags=GObject.SignalFlags.RUN_LAST,
)
# Object passed for swapping is GObject based.
class SwapObject(GObject.Object):
pass
class TestConnectGObjectIntrospected(unittest.TestCase, _ConnectObjectTestBase):
# This tests connect_object with introspected signals brought in from Regress.
Object = Regress.TestObj
# Object passed for swapping is GObject based.
class SwapObject(GObject.Object):
pass
class TestConnectPyObjectNonIntrospected(unittest.TestCase, _ConnectObjectTestBase):
# This tests connect_object with non-introspected signals
# (created in Python in this case).
class Object(GObject.Object):
test = GObject.Signal()
sig_with_int64_prop = GObject.Signal(
return_type=GObject.TYPE_INT64,
arg_types=[GObject.TYPE_INT64],
flags=GObject.SignalFlags.RUN_LAST,
)
# Object passed for swapping is pure Python
SwapObject = object
class TestConnectPyObjectIntrospected(unittest.TestCase, _ConnectObjectTestBase):
# This tests connect_object with introspected signals brought in from Regress.
Object = Regress.TestObj
# Object passed for swapping is pure Python
SwapObject = object
class _RefCountTestBase:
# NOTE: ref counts are always one more than expected because the getrefcount()
# function adds a ref for the input argument.
# Sub-classes set this
Object = None
class PyData:
pass
@unittest.skipUnless(hasattr(sys, "getrefcount"), "no sys.getrefcount")
def test_callback_ref_count_del(self):
def callback(obj, value):
return value // 2
callback_ref = weakref.ref(callback)
self.assertEqual(
sys.getrefcount(callback), 2 if sys.version_info < (3, 14) else 1
)
obj = self.Object()
obj.connect("sig-with-int64-prop", callback)
self.assertEqual(
sys.getrefcount(callback), 3 if sys.version_info < (3, 14) else 2
)
del callback
self.assertEqual(sys.getrefcount(callback_ref()), 2)
res = obj.emit("sig-with-int64-prop", 42)
self.assertEqual(res, 21)
self.assertEqual(
sys.getrefcount(callback_ref), 2 if sys.version_info < (3, 14) else 1
)
del obj
self.assertIsNone(callback_ref())
@unittest.skipUnless(hasattr(sys, "getrefcount"), "no sys.getrefcount")
def test_callback_ref_count_disconnect(self):
def callback(obj, value):
return value // 2
callback_ref = weakref.ref(callback)
self.assertEqual(
sys.getrefcount(callback), 2 if sys.version_info < (3, 14) else 1
)
obj = self.Object()
handler_id = obj.connect("sig-with-int64-prop", callback)
self.assertEqual(
sys.getrefcount(callback), 3 if sys.version_info < (3, 14) else 2
)
del callback
self.assertEqual(sys.getrefcount(callback_ref()), 2)
res = obj.emit("sig-with-int64-prop", 42)
self.assertEqual(res, 21)
self.assertEqual(
sys.getrefcount(callback_ref), 2 if sys.version_info < (3, 14) else 1
)
obj.disconnect(handler_id)
self.assertIsNone(callback_ref())
@unittest.skipUnless(hasattr(sys, "getrefcount"), "no sys.getrefcount")
def test_callback_ref_count_disconnect_by_func(self):
def callback(obj, value):
return value // 2
callback_ref = weakref.ref(callback)
self.assertEqual(
sys.getrefcount(callback), 2 if sys.version_info < (3, 14) else 1
)
obj = self.Object()
obj.connect("sig-with-int64-prop", callback)
self.assertEqual(
sys.getrefcount(callback), 3 if sys.version_info < (3, 14) else 2
)
del callback
self.assertEqual(sys.getrefcount(callback_ref()), 2)
res = obj.emit("sig-with-int64-prop", 42)
self.assertEqual(res, 21)
self.assertEqual(
sys.getrefcount(callback_ref), 2 if sys.version_info < (3, 14) else 1
)
obj.disconnect_by_func(callback_ref())
self.assertIsNone(callback_ref())
@unittest.skipUnless(hasattr(sys, "getrefcount"), "no sys.getrefcount")
def test_user_data_ref_count(self):
def callback(obj, value, data):
return value // 2
data = self.PyData()
data_ref = weakref.ref(data)
self.assertEqual(sys.getrefcount(data), 2 if sys.version_info < (3, 14) else 1)
obj = self.Object()
obj.connect("sig-with-int64-prop", callback, data)
self.assertEqual(sys.getrefcount(data), 3 if sys.version_info < (3, 14) else 2)
del data
self.assertEqual(sys.getrefcount(data_ref()), 2)
res = obj.emit("sig-with-int64-prop", 42)
self.assertEqual(res, 21)
self.assertEqual(sys.getrefcount(data_ref()), 2)
del obj
self.assertIsNone(data_ref())
@unittest.skipUnless(hasattr(sys, "getrefcount"), "no sys.getrefcount")
@unittest.expectedFailure # https://bugzilla.gnome.org/show_bug.cgi?id=688064
def test_object_ref_count(self):
# connect_object() should only weakly reference the object passed in
# and auto-disconnect the signal when the object is destroyed.
def callback(data, value):
return value // 2
data = GObject.Object()
data_ref = weakref.ref(data)
self.assertEqual(sys.getrefcount(data), 2)
obj = self.Object()
handler_id = obj.connect_object("sig-with-int64-prop", callback, data)
self.assertEqual(sys.getrefcount(data), 2)
res = obj.emit("sig-with-int64-prop", 42)
self.assertEqual(res, 21)
self.assertEqual(sys.getrefcount(data), 2)
del data
self.assertIsNone(data_ref())
self.assertFalse(obj.handler_is_connected(handler_id))
class TestRefCountsNonIntrospected(unittest.TestCase, _RefCountTestBase):
class Object(GObject.Object):
sig_with_int64_prop = GObject.Signal(
return_type=GObject.TYPE_INT64,
arg_types=[GObject.TYPE_INT64],
flags=GObject.SignalFlags.RUN_LAST,
)
class TestRefCountsIntrospected(unittest.TestCase, _RefCountTestBase):
Object = Regress.TestObj
class TestClosureRefCycle(unittest.TestCase):
def test_closure_ref_cycle_unreachable(self):
# https://bugzilla.gnome.org/show_bug.cgi?id=731501
called = []
def on_add(store, *args):
called.append(store)
store = Gio.ListStore()
store.connect_object("items-changed", on_add, store)
# Remove all Python references to the object and keep it alive
# on the C level.
x = Gio.FileInfo()
x.set_attribute_object("store", store)
del store
gc.collect()
# get it back and trigger the signal
x.get_attribute_object("store").append(Gio.FileInfo())
self.assertEqual(len(called), 1)
self.assertTrue(called[0].__grefcount__ > 0)
|