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 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077
|
# Generated by cythonpp.py on 2014-04-30 16:45:07
# Copyright (c) 2009-2012 Denis Bilenko. See LICENSE for details.
cimport cython
cimport libev
from python cimport *
import sys
# Work around lack of absolute_import in Cython
os = __import__('os', level=0)
import traceback
import signal as signalmodule
__all__ = ['get_version',
'get_header_version',
'supported_backends',
'recommended_backends',
'embeddable_backends',
'time',
'loop']
cdef extern from "callbacks.h":
void gevent_callback_io(libev.ev_loop, void*, int)
void gevent_callback_timer(libev.ev_loop, void*, int)
void gevent_callback_signal(libev.ev_loop, void*, int)
void gevent_callback_idle(libev.ev_loop, void*, int)
void gevent_callback_prepare(libev.ev_loop, void*, int)
void gevent_callback_check(libev.ev_loop, void*, int)
void gevent_callback_fork(libev.ev_loop, void*, int)
void gevent_callback_async(libev.ev_loop, void*, int)
void gevent_callback_child(libev.ev_loop, void*, int)
void gevent_callback_stat(libev.ev_loop, void*, int)
void gevent_run_callbacks(libev.ev_loop, void*, int)
void gevent_periodic_signal_check(libev.ev_loop, void*, int)
void gevent_call(loop, callback)
void gevent_noop(libev.ev_loop, void*, int)
cdef extern from *:
int errno
cdef extern from "stathelper.c":
object _pystat_fromstructstat(void*)
UNDEF = libev.EV_UNDEF
NONE = libev.EV_NONE
READ = libev.EV_READ
WRITE = libev.EV_WRITE
TIMER = libev.EV_TIMER
PERIODIC = libev.EV_PERIODIC
SIGNAL = libev.EV_SIGNAL
CHILD = libev.EV_CHILD
STAT = libev.EV_STAT
IDLE = libev.EV_IDLE
PREPARE = libev.EV_PREPARE
CHECK = libev.EV_CHECK
EMBED = libev.EV_EMBED
FORK = libev.EV_FORK
CLEANUP = libev.EV_CLEANUP
ASYNC = libev.EV_ASYNC
CUSTOM = libev.EV_CUSTOM
ERROR = libev.EV_ERROR
READWRITE = libev.EV_READ | libev.EV_WRITE
MINPRI = libev.EV_MINPRI
MAXPRI = libev.EV_MAXPRI
BACKEND_PORT = libev.EVBACKEND_PORT
BACKEND_KQUEUE = libev.EVBACKEND_KQUEUE
BACKEND_EPOLL = libev.EVBACKEND_EPOLL
BACKEND_POLL = libev.EVBACKEND_POLL
BACKEND_SELECT = libev.EVBACKEND_SELECT
FORKCHECK = libev.EVFLAG_FORKCHECK
NOINOTIFY = libev.EVFLAG_NOINOTIFY
SIGNALFD = libev.EVFLAG_SIGNALFD
NOSIGMASK = libev.EVFLAG_NOSIGMASK
@cython.internal
cdef class _EVENTSType:
def __repr__(self):
return 'gevent.core.EVENTS'
cdef public object GEVENT_CORE_EVENTS = _EVENTSType()
EVENTS = GEVENT_CORE_EVENTS
def get_version():
return 'libev-%d.%02d' % (libev.ev_version_major(), libev.ev_version_minor())
def get_header_version():
return 'libev-%d.%02d' % (libev.EV_VERSION_MAJOR, libev.EV_VERSION_MINOR)
# This list backends in the order they are actually tried by libev
_flags = [(libev.EVBACKEND_PORT, 'port'),
(libev.EVBACKEND_KQUEUE, 'kqueue'),
(libev.EVBACKEND_EPOLL, 'epoll'),
(libev.EVBACKEND_POLL, 'poll'),
(libev.EVBACKEND_SELECT, 'select'),
(libev.EVFLAG_NOENV, 'noenv'),
(libev.EVFLAG_FORKCHECK, 'forkcheck'),
(libev.EVFLAG_NOINOTIFY, 'noinotify'),
(libev.EVFLAG_SIGNALFD, 'signalfd'),
(libev.EVFLAG_NOSIGMASK, 'nosigmask')]
_flags_str2int = dict((string, flag) for (flag, string) in _flags)
_events = [(libev.EV_READ, 'READ'),
(libev.EV_WRITE, 'WRITE'),
(libev.EV__IOFDSET, '_IOFDSET'),
(libev.EV_PERIODIC, 'PERIODIC'),
(libev.EV_SIGNAL, 'SIGNAL'),
(libev.EV_CHILD, 'CHILD'),
(libev.EV_STAT, 'STAT'),
(libev.EV_IDLE, 'IDLE'),
(libev.EV_PREPARE, 'PREPARE'),
(libev.EV_CHECK, 'CHECK'),
(libev.EV_EMBED, 'EMBED'),
(libev.EV_FORK, 'FORK'),
(libev.EV_CLEANUP, 'CLEANUP'),
(libev.EV_ASYNC, 'ASYNC'),
(libev.EV_CUSTOM, 'CUSTOM'),
(libev.EV_ERROR, 'ERROR')]
cpdef _flags_to_list(unsigned int flags):
cdef list result = []
for code, value in _flags:
if flags & code:
result.append(value)
flags &= ~code
if not flags:
break
if flags:
result.append(flags)
return result
if sys.version_info[0] >= 3:
basestring = (bytes, str)
else:
basestring = __builtins__.basestring
cpdef unsigned int _flags_to_int(object flags) except? -1:
# Note, that order does not matter, libev has its own predefined order
if not flags:
return 0
if isinstance(flags, (int, long)):
return flags
cdef unsigned int result = 0
try:
if isinstance(flags, basestring):
flags = flags.split(',')
for value in flags:
value = value.strip().lower()
if value:
result |= _flags_str2int[value]
except KeyError, ex:
raise ValueError('Invalid backend or flag: %s\nPossible values: %s' % (ex, ', '.join(sorted(_flags_str2int.keys()))))
return result
cdef str _str_hex(object flag):
if isinstance(flag, (int, long)):
return hex(flag)
return str(flag)
cpdef _check_flags(unsigned int flags):
cdef list as_list
flags &= libev.EVBACKEND_MASK
if not flags:
return
if not (flags & libev.EVBACKEND_ALL):
raise ValueError('Invalid value for backend: 0x%x' % flags)
if not (flags & libev.ev_supported_backends()):
as_list = [_str_hex(x) for x in _flags_to_list(flags)]
raise ValueError('Unsupported backend: %s' % '|'.join(as_list))
cpdef _events_to_str(int events):
cdef list result = []
cdef int c_flag
for (flag, string) in _events:
c_flag = flag
if events & c_flag:
result.append(string)
events = events & (~c_flag)
if not events:
break
if events:
result.append(hex(events))
return '|'.join(result)
def supported_backends():
return _flags_to_list(libev.ev_supported_backends())
def recommended_backends():
return _flags_to_list(libev.ev_recommended_backends())
def embeddable_backends():
return _flags_to_list(libev.ev_embeddable_backends())
def time():
return libev.ev_time()
cdef bint _default_loop_destroyed = False
cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
cdef libev.ev_loop* _ptr
cdef public object error_handler
cdef libev.ev_prepare _prepare
cdef public list _callbacks
cdef libev.ev_timer _timer0
#ifdef _WIN32
cdef libev.ev_timer _periodic_signal_checker
#endif
def __init__(self, object flags=None, object default=None, size_t ptr=0):
cdef unsigned int c_flags
cdef object old_handler = None
libev.ev_prepare_init(&self._prepare, <void*>gevent_run_callbacks)
#ifdef _WIN32
libev.ev_timer_init(&self._periodic_signal_checker, <void*>gevent_periodic_signal_check, 0.3, 0.3)
#endif
libev.ev_timer_init(&self._timer0, <void*>gevent_noop, 0.0, 0.0)
if ptr:
self._ptr = <libev.ev_loop*>ptr
else:
c_flags = _flags_to_int(flags)
_check_flags(c_flags)
c_flags |= libev.EVFLAG_NOENV
if default is None:
default = True
if _default_loop_destroyed:
default = False
if default:
self._ptr = libev.gevent_ev_default_loop(c_flags)
if not self._ptr:
raise SystemError("ev_default_loop(%s) failed" % (c_flags, ))
#ifdef _WIN32
libev.ev_timer_start(self._ptr, &self._periodic_signal_checker)
libev.ev_unref(self._ptr)
#endif
else:
self._ptr = libev.ev_loop_new(c_flags)
if not self._ptr:
raise SystemError("ev_loop_new(%s) failed" % (c_flags, ))
if default or __SYSERR_CALLBACK is None:
set_syserr_cb(self._handle_syserr)
libev.ev_prepare_start(self._ptr, &self._prepare)
libev.ev_unref(self._ptr)
self._callbacks = []
cdef _run_callbacks(self):
cdef callback cb
cdef object callbacks
cdef int count = 1000
libev.ev_timer_stop(self._ptr, &self._timer0)
while self._callbacks and count > 0:
callbacks = self._callbacks
self._callbacks = []
for cb in callbacks:
libev.ev_unref(self._ptr)
gevent_call(self, cb)
count -= 1
if self._callbacks:
libev.ev_timer_start(self._ptr, &self._timer0)
def _stop_watchers(self):
if libev.ev_is_active(&self._prepare):
libev.ev_ref(self._ptr)
libev.ev_prepare_stop(self._ptr, &self._prepare)
#ifdef _WIN32
if libev.ev_is_active(&self._periodic_signal_checker):
libev.ev_ref(self._ptr)
libev.ev_timer_stop(self._ptr, &self._periodic_signal_checker)
#endif
def destroy(self):
global _default_loop_destroyed
if self._ptr:
self._stop_watchers()
if __SYSERR_CALLBACK == self._handle_syserr:
set_syserr_cb(None)
if libev.ev_is_default_loop(self._ptr):
_default_loop_destroyed = True
libev.ev_loop_destroy(self._ptr)
self._ptr = NULL
def __dealloc__(self):
if self._ptr:
self._stop_watchers()
if not libev.ev_is_default_loop(self._ptr):
libev.ev_loop_destroy(self._ptr)
self._ptr = NULL
property ptr:
def __get__(self):
return <size_t>self._ptr
property WatcherType:
def __get__(self):
return watcher
property MAXPRI:
def __get__(self):
return libev.EV_MAXPRI
property MINPRI:
def __get__(self):
return libev.EV_MINPRI
def _handle_syserr(self, message, errno):
self.handle_error(None, SystemError, SystemError(message + ': ' + os.strerror(errno)), None)
cpdef handle_error(self, context, type, value, tb):
cdef object handle_error
cdef object error_handler = self.error_handler
if error_handler is not None:
# we do want to do getattr every time so that setting Hub.handle_error property just works
handle_error = getattr(error_handler, 'handle_error', error_handler)
handle_error(context, type, value, tb)
else:
self._default_handle_error(context, type, value, tb)
cpdef _default_handle_error(self, context, type, value, tb):
# note: Hub sets its own error handler so this is not used by gevent
# this is here to make core.loop usable without the rest of gevent
traceback.print_exception(type, value, tb)
if self._ptr:
libev.ev_break(self._ptr, libev.EVBREAK_ONE)
def run(self, nowait=False, once=False):
if not self._ptr:
raise ValueError('operation on destroyed loop')
cdef unsigned int flags = 0
if nowait:
flags |= libev.EVRUN_NOWAIT
if once:
flags |= libev.EVRUN_ONCE
with nogil:
libev.ev_run(self._ptr, flags)
def reinit(self):
if self._ptr:
libev.ev_loop_fork(self._ptr)
def ref(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
libev.ev_ref(self._ptr)
def unref(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
libev.ev_unref(self._ptr)
def break_(self, int how=libev.EVBREAK_ONE):
if not self._ptr:
raise ValueError('operation on destroyed loop')
libev.ev_break(self._ptr, how)
def verify(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
libev.ev_verify(self._ptr)
def now(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return libev.ev_now(self._ptr)
def update(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
libev.ev_now_update(self._ptr)
def __repr__(self):
return '<%s at 0x%x %s>' % (self.__class__.__name__, id(self), self._format())
property default:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return True if libev.ev_is_default_loop(self._ptr) else False
property iteration:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return libev.ev_iteration(self._ptr)
property depth:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return libev.ev_depth(self._ptr)
property backend_int:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return libev.ev_backend(self._ptr)
property backend:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
cdef unsigned int backend = libev.ev_backend(self._ptr)
for key, value in _flags:
if key == backend:
return value
return backend
property pendingcnt:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return libev.ev_pending_count(self._ptr)
#ifdef _WIN32
def io(self, libev.vfd_socket_t fd, int events, ref=True, priority=None):
return io(self, fd, events, ref, priority)
#else
def io(self, int fd, int events, ref=True, priority=None):
return io(self, fd, events, ref, priority)
#endif
def timer(self, double after, double repeat=0.0, ref=True, priority=None):
return timer(self, after, repeat, ref, priority)
def signal(self, int signum, ref=True, priority=None):
return signal(self, signum, ref, priority)
def idle(self, ref=True, priority=None):
return idle(self, ref, priority)
def prepare(self, ref=True, priority=None):
return prepare(self, ref, priority)
def check(self, ref=True, priority=None):
return check(self, ref, priority)
def fork(self, ref=True, priority=None):
return fork(self, ref, priority)
def async(self, ref=True, priority=None):
return async(self, ref, priority)
#ifdef _WIN32
#else
def child(self, int pid, bint trace=0, ref=True):
return child(self, pid, trace, ref)
def install_sigchld(self):
libev.gevent_install_sigchld_handler()
#endif
def stat(self, bytes path, float interval=0.0, ref=True, priority=None):
return stat(self, path, interval, ref, priority)
def run_callback(self, func, *args):
if not self._ptr:
raise ValueError('operation on destroyed loop')
cdef callback cb = callback(func, args)
self._callbacks.append(cb)
libev.ev_ref(self._ptr)
return cb
def _format(self):
if not self._ptr:
return 'destroyed'
cdef object msg = self.backend
if self.default:
msg += ' default'
msg += ' pending=%s' % self.pendingcnt
#ifdef LIBEV_EMBED
msg += self._format_details()
#endif
return msg
#ifdef LIBEV_EMBED
def _format_details(self):
cdef str msg = ''
cdef object fileno = self.fileno()
cdef object sigfd = None
cdef object activecnt = None
try:
sigfd = self.sigfd
except AttributeError:
sigfd = None
try:
activecnt = self.activecnt
except AttributeError:
pass
if activecnt is not None:
msg += ' ref=' + repr(activecnt)
if fileno is not None:
msg += ' fileno=' + repr(fileno)
if sigfd is not None and sigfd != -1:
msg += ' sigfd=' + repr(sigfd)
return msg
def fileno(self):
cdef int fd
if self._ptr:
fd = self._ptr.backend_fd
if fd >= 0:
return fd
property activecnt:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return self._ptr.activecnt
property sig_pending:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return self._ptr.sig_pending
#if EV_USE_SIGNALFD
property sigfd:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return self._ptr.sigfd
#endif
property origflags:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return _flags_to_list(self._ptr.origflags)
property origflags_int:
def __get__(self):
if not self._ptr:
raise ValueError('operation on destroyed loop')
return self._ptr.origflags
#endif
cdef public class callback [object PyGeventCallbackObject, type PyGeventCallback_Type]:
cdef public object callback
cdef public tuple args
def __init__(self, callback, args):
self.callback = callback
self.args = args
def stop(self):
self.callback = None
self.args = None
# Note, that __nonzero__ and pending are different
# nonzero is used in contexts where we need to know whether to schedule another callback,
# so it's true if it's pending or currently running
# 'pending' has the same meaning as libev watchers: it is cleared before entering callback
def __nonzero__(self):
# it's nonzero if it's pending or currently executing
return self.args is not None
property pending:
def __get__(self):
return self.callback is not None
def __repr__(self):
if Py_ReprEnter(<PyObjectPtr>self) != 0:
return "<...>"
try:
format = self._format()
result = "<%s at 0x%x%s" % (self.__class__.__name__, id(self), format)
if self.pending:
result += " pending"
if self.callback is not None:
result += " callback=%r" % (self.callback, )
if self.args is not None:
result += " args=%r" % (self.args, )
if self.callback is None and self.args is None:
result += " stopped"
return result + ">"
finally:
Py_ReprLeave(<PyObjectPtr>self)
def _format(self):
return ''
# about readonly _flags attribute:
# bit #1 set if object owns Python reference to itself (Py_INCREF was called and we must call Py_DECREF later)
# bit #2 set if ev_unref() was called and we must call ev_ref() later
# bit #3 set if user wants to call ev_unref() before start()
cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Type]:
"""Abstract base class for all the watchers"""
def __repr__(self):
if Py_ReprEnter(<PyObjectPtr>self) != 0:
return "<...>"
try:
format = self._format()
result = "<%s at 0x%x%s" % (self.__class__.__name__, id(self), format)
if self.active:
result += " active"
if self.pending:
result += " pending"
if self.callback is not None:
result += " callback=%r" % (self.callback, )
if self.args is not None:
result += " args=%r" % (self.args, )
return result + ">"
finally:
Py_ReprLeave(<PyObjectPtr>self)
def _format(self):
return ''
cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_io _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_io_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args, pass_events=False):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
if pass_events:
self.args = (GEVENT_CORE_EVENTS, ) + args
else:
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_io_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
#ifdef _WIN32
def __init__(self, loop loop, libev.vfd_socket_t fd, int events, ref=True, priority=None):
if events & ~(libev.EV__IOFDSET | libev.EV_READ | libev.EV_WRITE):
raise ValueError('illegal event mask: %r' % events)
cdef int vfd = libev.vfd_open(fd)
libev.vfd_free(self._watcher.fd)
libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, vfd, events)
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
#else
def __init__(self, loop loop, int fd, int events, ref=True, priority=None):
if fd < 0:
raise ValueError('fd must be non-negative: %r' % fd)
if events & ~(libev.EV__IOFDSET | libev.EV_READ | libev.EV_WRITE):
raise ValueError('illegal event mask: %r' % events)
libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, fd, events)
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
#endif
property fd:
def __get__(self):
return libev.vfd_get(self._watcher.fd)
def __set__(self, long fd):
if libev.ev_is_active(&self._watcher):
raise AttributeError("'io' watcher attribute 'fd' is read-only while watcher is active")
cdef int vfd = libev.vfd_open(fd)
libev.vfd_free(self._watcher.fd)
libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, vfd, self._watcher.events)
property events:
def __get__(self):
return self._watcher.events
def __set__(self, int events):
if libev.ev_is_active(&self._watcher):
raise AttributeError("'io' watcher attribute 'events' is read-only while watcher is active")
libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, self._watcher.fd, events)
property events_str:
def __get__(self):
return _events_to_str(self._watcher.events)
def _format(self):
return ' fd=%s events=%s' % (self.fd, self.events_str)
#ifdef _WIN32
def __cinit__(self):
self._watcher.fd = -1;
def __dealloc__(self):
libev.vfd_free(self._watcher.fd)
#endif
cdef public class timer(watcher) [object PyGeventTimerObject, type PyGeventTimer_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_timer _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_timer_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args, update=True):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
if update:
libev.ev_now_update(self.loop._ptr)
libev.ev_timer_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
def __init__(self, loop loop, double after=0.0, double repeat=0.0, ref=True, priority=None):
if repeat < 0.0:
raise ValueError("repeat must be positive or zero: %r" % repeat)
libev.ev_timer_init(&self._watcher, <void *>gevent_callback_timer, after, repeat)
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
property at:
def __get__(self):
return self._watcher.at
# QQQ: add 'after' and 'repeat' properties?
def again(self, object callback, *args, update=True):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
if update:
libev.ev_now_update(self.loop._ptr)
libev.ev_timer_again(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
cdef public class signal(watcher) [object PyGeventSignalObject, type PyGeventSignal_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_signal _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_signal_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_signal_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
def __init__(self, loop loop, int signalnum, ref=True, priority=None):
if signalnum < 1 or signalnum >= signalmodule.NSIG:
raise ValueError('illegal signal number: %r' % signalnum)
# still possible to crash on one of libev's asserts:
# 1) "libev: ev_signal_start called with illegal signal number"
# EV_NSIG might be different from signal.NSIG on some platforms
# 2) "libev: a signal must not be attached to two different loops"
# we probably could check that in LIBEV_EMBED mode, but not in general
libev.ev_signal_init(&self._watcher, <void *>gevent_callback_signal, signalnum)
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
cdef public class idle(watcher) [object PyGeventIdleObject, type PyGeventIdle_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_idle _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_idle_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_idle_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
def __init__(self, loop loop , ref=True, priority=None):
libev.ev_idle_init(&self._watcher, <void *>gevent_callback_idle )
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
cdef public class prepare(watcher) [object PyGeventPrepareObject, type PyGeventPrepare_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_prepare _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_prepare_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_prepare_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
def __init__(self, loop loop , ref=True, priority=None):
libev.ev_prepare_init(&self._watcher, <void *>gevent_callback_prepare )
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
cdef public class check(watcher) [object PyGeventCheckObject, type PyGeventCheck_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_check _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_check_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_check_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
def __init__(self, loop loop , ref=True, priority=None):
libev.ev_check_init(&self._watcher, <void *>gevent_callback_check )
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
cdef public class fork(watcher) [object PyGeventForkObject, type PyGeventFork_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_fork _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_fork_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_fork_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
def __init__(self, loop loop , ref=True, priority=None):
libev.ev_fork_init(&self._watcher, <void *>gevent_callback_fork )
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
cdef public class async(watcher) [object PyGeventAsyncObject, type PyGeventAsync_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_async _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_async_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_async_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_async_pending(&self._watcher) else False
def __init__(self, loop loop , ref=True, priority=None):
libev.ev_async_init(&self._watcher, <void *>gevent_callback_async )
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
def send(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
libev.ev_async_send(self.loop._ptr, &self._watcher)
#ifdef _WIN32
#else
cdef public class child(watcher) [object PyGeventChildObject, type PyGeventChild_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_child _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_child_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_child_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
def __init__(self, loop loop, int pid, bint trace=0, ref=True):
if not loop.default:
raise TypeError('child watchers are only available on the default loop')
libev.gevent_install_sigchld_handler()
libev.ev_child_init(&self._watcher, <void *>gevent_callback_child, pid, trace)
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
def _format(self):
return ' pid=%r rstatus=%r' % (self.pid, self.rstatus)
property pid:
def __get__(self):
return self._watcher.pid
property rpid:
def __get__(self):
return self._watcher.rpid
def __set__(self, int value):
self._watcher.rpid = value
property rstatus:
def __get__(self):
return self._watcher.rstatus
def __set__(self, int value):
self._watcher.rstatus = value
#endif
cdef public class stat(watcher) [object PyGeventStatObject, type PyGeventStat_Type]:
cdef public loop loop
cdef object _callback
cdef public tuple args
cdef readonly int _flags
cdef libev.ev_stat _watcher
property ref:
def __get__(self):
return False if self._flags & 4 else True
def __set__(self, object value):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if value:
if not self._flags & 4:
return # ref is already True
if self._flags & 2: # ev_unref was called, undo
libev.ev_ref(self.loop._ptr)
self._flags &= ~6 # do not want unref, no outstanding unref
else:
if self._flags & 4:
return # ref is already False
self._flags |= 4
if not self._flags & 2 and libev.ev_is_active(&self._watcher):
libev.ev_unref(self.loop._ptr)
self._flags |= 2
property callback:
def __get__(self):
return self._callback
def __set__(self, object callback):
if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None:
raise TypeError("Expected callable, not %r" % (callback, ))
self._callback = callback
def stop(self):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if self._flags & 2:
libev.ev_ref(self.loop._ptr)
self._flags &= ~2
libev.ev_stat_stop(self.loop._ptr, &self._watcher)
self._callback = None
self.args = None
if self._flags & 1:
Py_DECREF(<PyObjectPtr>self)
self._flags &= ~1
property priority:
def __get__(self):
return libev.ev_priority(&self._watcher)
def __set__(self, int priority):
if libev.ev_is_active(&self._watcher):
raise AttributeError("Cannot set priority of an active watcher")
libev.ev_set_priority(&self._watcher, priority)
def feed(self, int revents, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_feed_event(self.loop._ptr, &self._watcher, revents)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
def start(self, object callback, *args):
if not self.loop._ptr:
raise ValueError('operation on destroyed loop')
if callback is None:
raise TypeError('callback must be callable, not None')
self.callback = callback
self.args = args
if self._flags & 6 == 4:
libev.ev_unref(self.loop._ptr)
self._flags |= 2
libev.ev_stat_start(self.loop._ptr, &self._watcher)
if not self._flags & 1:
Py_INCREF(<PyObjectPtr>self)
self._flags |= 1
property active:
def __get__(self):
return True if libev.ev_is_active(&self._watcher) else False
property pending:
def __get__(self):
return True if libev.ev_is_pending(&self._watcher) else False
cdef readonly bytes path
def __init__(self, loop loop, bytes path, float interval=0.0, ref=True, priority=None):
self.path = path
libev.ev_stat_init(&self._watcher, <void *>gevent_callback_stat, <char*>self.path, interval)
self.loop = loop
if ref:
self._flags = 0
else:
self._flags = 4
if priority is not None:
libev.ev_set_priority(&self._watcher, priority)
property attr:
def __get__(self):
if not self._watcher.attr.st_nlink:
return
return _pystat_fromstructstat(&self._watcher.attr)
property prev:
def __get__(self):
if not self._watcher.prev.st_nlink:
return
return _pystat_fromstructstat(&self._watcher.prev)
property interval:
def __get__(self):
return self._watcher.interval
__SYSERR_CALLBACK = None
cdef void _syserr_cb(char* msg) with gil:
try:
__SYSERR_CALLBACK(msg, errno)
except:
set_syserr_cb(None)
print_exc = getattr(traceback, 'print_exc', None)
if print_exc is not None:
print_exc()
cpdef set_syserr_cb(callback):
global __SYSERR_CALLBACK
if callback is None:
libev.ev_set_syserr_cb(NULL)
__SYSERR_CALLBACK = None
elif callable(callback):
libev.ev_set_syserr_cb(<void *>_syserr_cb)
__SYSERR_CALLBACK = callback
else:
raise TypeError('Expected callable or None, got %r' % (callback, ))
#ifdef LIBEV_EMBED
LIBEV_EMBED = True
EV_USE_FLOOR = libev.EV_USE_FLOOR
EV_USE_CLOCK_SYSCALL = libev.EV_USE_CLOCK_SYSCALL
EV_USE_REALTIME = libev.EV_USE_REALTIME
EV_USE_MONOTONIC = libev.EV_USE_MONOTONIC
EV_USE_NANOSLEEP = libev.EV_USE_NANOSLEEP
EV_USE_INOTIFY = libev.EV_USE_INOTIFY
EV_USE_SIGNALFD = libev.EV_USE_SIGNALFD
EV_USE_EVENTFD = libev.EV_USE_EVENTFD
EV_USE_4HEAP = libev.EV_USE_4HEAP
#else
LIBEV_EMBED = False
#endif
|