corecext.ppyx 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. # Copyright (c) 2009-2012 Denis Bilenko. See LICENSE for details.
  2. # This directive, supported in Cython 0.24+, causes sources files to be
  3. # much smaller and thus cythonpp.py to be slightly faster. But it does make
  4. # debugging more difficult.
  5. # cython: emit_code_comments=False
  6. cimport cython
  7. cimport libev
  8. # Note this is not the standard cython 'cpython' (which has a backwards compat alias of 'python')
  9. # it's our custom def. If it's not on the include path, we get warned.
  10. from python cimport *
  11. # Work around lack of absolute_import in Cython
  12. # Note for PY3: not doing so will leave reference to locals() on import
  13. # (reproducible under Python 3.3, not under Python 3.4; see test__refcount_core.py)
  14. sys = __import__('sys', level=0)
  15. os = __import__('os', level=0)
  16. traceback = __import__('traceback', level=0)
  17. signalmodule = __import__('signal', level=0)
  18. __all__ = ['get_version',
  19. 'get_header_version',
  20. 'supported_backends',
  21. 'recommended_backends',
  22. 'embeddable_backends',
  23. 'time',
  24. 'loop']
  25. cdef tuple integer_types
  26. if sys.version_info[0] >= 3:
  27. integer_types = int,
  28. else:
  29. integer_types = (int, long)
  30. cdef extern from "callbacks.h":
  31. void gevent_callback_io(libev.ev_loop, void*, int)
  32. void gevent_callback_timer(libev.ev_loop, void*, int)
  33. void gevent_callback_signal(libev.ev_loop, void*, int)
  34. void gevent_callback_idle(libev.ev_loop, void*, int)
  35. void gevent_callback_prepare(libev.ev_loop, void*, int)
  36. void gevent_callback_check(libev.ev_loop, void*, int)
  37. void gevent_callback_fork(libev.ev_loop, void*, int)
  38. void gevent_callback_async(libev.ev_loop, void*, int)
  39. void gevent_callback_child(libev.ev_loop, void*, int)
  40. void gevent_callback_stat(libev.ev_loop, void*, int)
  41. void gevent_run_callbacks(libev.ev_loop, void*, int)
  42. void gevent_periodic_signal_check(libev.ev_loop, void*, int)
  43. void gevent_call(loop, callback)
  44. void gevent_noop(libev.ev_loop, void*, int)
  45. cdef extern from *:
  46. int errno
  47. cdef extern from "stathelper.c":
  48. object _pystat_fromstructstat(void*)
  49. UNDEF = libev.EV_UNDEF
  50. NONE = libev.EV_NONE
  51. READ = libev.EV_READ
  52. WRITE = libev.EV_WRITE
  53. TIMER = libev.EV_TIMER
  54. PERIODIC = libev.EV_PERIODIC
  55. SIGNAL = libev.EV_SIGNAL
  56. CHILD = libev.EV_CHILD
  57. STAT = libev.EV_STAT
  58. IDLE = libev.EV_IDLE
  59. PREPARE = libev.EV_PREPARE
  60. CHECK = libev.EV_CHECK
  61. EMBED = libev.EV_EMBED
  62. FORK = libev.EV_FORK
  63. CLEANUP = libev.EV_CLEANUP
  64. ASYNC = libev.EV_ASYNC
  65. CUSTOM = libev.EV_CUSTOM
  66. ERROR = libev.EV_ERROR
  67. READWRITE = libev.EV_READ | libev.EV_WRITE
  68. MINPRI = libev.EV_MINPRI
  69. MAXPRI = libev.EV_MAXPRI
  70. BACKEND_PORT = libev.EVBACKEND_PORT
  71. BACKEND_KQUEUE = libev.EVBACKEND_KQUEUE
  72. BACKEND_EPOLL = libev.EVBACKEND_EPOLL
  73. BACKEND_POLL = libev.EVBACKEND_POLL
  74. BACKEND_SELECT = libev.EVBACKEND_SELECT
  75. FORKCHECK = libev.EVFLAG_FORKCHECK
  76. NOINOTIFY = libev.EVFLAG_NOINOTIFY
  77. SIGNALFD = libev.EVFLAG_SIGNALFD
  78. NOSIGMASK = libev.EVFLAG_NOSIGMASK
  79. @cython.internal
  80. cdef class _EVENTSType:
  81. def __repr__(self):
  82. return 'gevent.core.EVENTS'
  83. cdef public object GEVENT_CORE_EVENTS = _EVENTSType()
  84. EVENTS = GEVENT_CORE_EVENTS
  85. def get_version():
  86. return 'libev-%d.%02d' % (libev.ev_version_major(), libev.ev_version_minor())
  87. def get_header_version():
  88. return 'libev-%d.%02d' % (libev.EV_VERSION_MAJOR, libev.EV_VERSION_MINOR)
  89. # This list backends in the order they are actually tried by libev
  90. _flags = [(libev.EVBACKEND_PORT, 'port'),
  91. (libev.EVBACKEND_KQUEUE, 'kqueue'),
  92. (libev.EVBACKEND_EPOLL, 'epoll'),
  93. (libev.EVBACKEND_POLL, 'poll'),
  94. (libev.EVBACKEND_SELECT, 'select'),
  95. (libev.EVFLAG_NOENV, 'noenv'),
  96. (libev.EVFLAG_FORKCHECK, 'forkcheck'),
  97. (libev.EVFLAG_NOINOTIFY, 'noinotify'),
  98. (libev.EVFLAG_SIGNALFD, 'signalfd'),
  99. (libev.EVFLAG_NOSIGMASK, 'nosigmask')]
  100. _flags_str2int = dict((string, flag) for (flag, string) in _flags)
  101. _events = [(libev.EV_READ, 'READ'),
  102. (libev.EV_WRITE, 'WRITE'),
  103. (libev.EV__IOFDSET, '_IOFDSET'),
  104. (libev.EV_PERIODIC, 'PERIODIC'),
  105. (libev.EV_SIGNAL, 'SIGNAL'),
  106. (libev.EV_CHILD, 'CHILD'),
  107. (libev.EV_STAT, 'STAT'),
  108. (libev.EV_IDLE, 'IDLE'),
  109. (libev.EV_PREPARE, 'PREPARE'),
  110. (libev.EV_CHECK, 'CHECK'),
  111. (libev.EV_EMBED, 'EMBED'),
  112. (libev.EV_FORK, 'FORK'),
  113. (libev.EV_CLEANUP, 'CLEANUP'),
  114. (libev.EV_ASYNC, 'ASYNC'),
  115. (libev.EV_CUSTOM, 'CUSTOM'),
  116. (libev.EV_ERROR, 'ERROR')]
  117. cpdef _flags_to_list(unsigned int flags):
  118. cdef list result = []
  119. for code, value in _flags:
  120. if flags & code:
  121. result.append(value)
  122. flags &= ~code
  123. if not flags:
  124. break
  125. if flags:
  126. result.append(flags)
  127. return result
  128. if sys.version_info[0] >= 3:
  129. basestring = (bytes, str)
  130. else:
  131. basestring = __builtins__.basestring
  132. cpdef unsigned int _flags_to_int(object flags) except? -1:
  133. # Note, that order does not matter, libev has its own predefined order
  134. if not flags:
  135. return 0
  136. if isinstance(flags, integer_types):
  137. return flags
  138. cdef unsigned int result = 0
  139. try:
  140. if isinstance(flags, basestring):
  141. flags = flags.split(',')
  142. for value in flags:
  143. value = value.strip().lower()
  144. if value:
  145. result |= _flags_str2int[value]
  146. except KeyError as ex:
  147. raise ValueError('Invalid backend or flag: %s\nPossible values: %s' % (ex, ', '.join(sorted(_flags_str2int.keys()))))
  148. return result
  149. cdef str _str_hex(object flag):
  150. if isinstance(flag, integer_types):
  151. return hex(flag)
  152. return str(flag)
  153. cpdef _check_flags(unsigned int flags):
  154. cdef list as_list
  155. flags &= libev.EVBACKEND_MASK
  156. if not flags:
  157. return
  158. if not (flags & libev.EVBACKEND_ALL):
  159. raise ValueError('Invalid value for backend: 0x%x' % flags)
  160. if not (flags & libev.ev_supported_backends()):
  161. as_list = [_str_hex(x) for x in _flags_to_list(flags)]
  162. raise ValueError('Unsupported backend: %s' % '|'.join(as_list))
  163. cpdef _events_to_str(int events):
  164. cdef list result = []
  165. cdef int c_flag
  166. for (flag, string) in _events:
  167. c_flag = flag
  168. if events & c_flag:
  169. result.append(string)
  170. events = events & (~c_flag)
  171. if not events:
  172. break
  173. if events:
  174. result.append(hex(events))
  175. return '|'.join(result)
  176. def supported_backends():
  177. return _flags_to_list(libev.ev_supported_backends())
  178. def recommended_backends():
  179. return _flags_to_list(libev.ev_recommended_backends())
  180. def embeddable_backends():
  181. return _flags_to_list(libev.ev_embeddable_backends())
  182. def time():
  183. return libev.ev_time()
  184. #define LOOP_PROPERTY(NAME) property NAME: \
  185. \
  186. def __get__(self): \
  187. CHECK_LOOP3(self) \
  188. return self._ptr.NAME
  189. cdef bint _default_loop_destroyed = False
  190. #define CHECK_LOOP2(LOOP) \
  191. if not LOOP._ptr: \
  192. raise ValueError('operation on destroyed loop')
  193. #define CHECK_LOOP3(LOOP) \
  194. if not LOOP._ptr: \
  195. raise ValueError('operation on destroyed loop')
  196. cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
  197. cdef libev.ev_loop* _ptr
  198. cdef public object error_handler
  199. cdef libev.ev_prepare _prepare
  200. cdef public list _callbacks
  201. cdef libev.ev_timer _timer0
  202. #ifdef _WIN32
  203. cdef libev.ev_timer _periodic_signal_checker
  204. #endif
  205. def __init__(self, object flags=None, object default=None, size_t ptr=0):
  206. cdef unsigned int c_flags
  207. cdef object old_handler = None
  208. libev.ev_prepare_init(&self._prepare, <void*>gevent_run_callbacks)
  209. #ifdef _WIN32
  210. libev.ev_timer_init(&self._periodic_signal_checker, <void*>gevent_periodic_signal_check, 0.3, 0.3)
  211. #endif
  212. libev.ev_timer_init(&self._timer0, <void*>gevent_noop, 0.0, 0.0)
  213. if ptr:
  214. self._ptr = <libev.ev_loop*>ptr
  215. else:
  216. c_flags = _flags_to_int(flags)
  217. _check_flags(c_flags)
  218. c_flags |= libev.EVFLAG_NOENV
  219. c_flags |= libev.EVFLAG_FORKCHECK
  220. if default is None:
  221. default = True
  222. if _default_loop_destroyed:
  223. default = False
  224. if default:
  225. self._ptr = libev.gevent_ev_default_loop(c_flags)
  226. if not self._ptr:
  227. raise SystemError("ev_default_loop(%s) failed" % (c_flags, ))
  228. #ifdef _WIN32
  229. libev.ev_timer_start(self._ptr, &self._periodic_signal_checker)
  230. libev.ev_unref(self._ptr)
  231. #endif
  232. else:
  233. self._ptr = libev.ev_loop_new(c_flags)
  234. if not self._ptr:
  235. raise SystemError("ev_loop_new(%s) failed" % (c_flags, ))
  236. if default or __SYSERR_CALLBACK is None:
  237. set_syserr_cb(self._handle_syserr)
  238. libev.ev_prepare_start(self._ptr, &self._prepare)
  239. libev.ev_unref(self._ptr)
  240. self._callbacks = []
  241. cdef _run_callbacks(self):
  242. cdef callback cb
  243. cdef object callbacks
  244. cdef int count = 1000
  245. libev.ev_timer_stop(self._ptr, &self._timer0)
  246. while self._callbacks and count > 0:
  247. callbacks = self._callbacks
  248. self._callbacks = []
  249. for cb in callbacks:
  250. libev.ev_unref(self._ptr)
  251. gevent_call(self, cb)
  252. count -= 1
  253. if self._callbacks:
  254. libev.ev_timer_start(self._ptr, &self._timer0)
  255. def _stop_watchers(self):
  256. if libev.ev_is_active(&self._prepare):
  257. libev.ev_ref(self._ptr)
  258. libev.ev_prepare_stop(self._ptr, &self._prepare)
  259. #ifdef _WIN32
  260. if libev.ev_is_active(&self._periodic_signal_checker):
  261. libev.ev_ref(self._ptr)
  262. libev.ev_timer_stop(self._ptr, &self._periodic_signal_checker)
  263. #endif
  264. def destroy(self):
  265. global _default_loop_destroyed
  266. if self._ptr:
  267. self._stop_watchers()
  268. if __SYSERR_CALLBACK == self._handle_syserr:
  269. set_syserr_cb(None)
  270. if libev.ev_is_default_loop(self._ptr):
  271. _default_loop_destroyed = True
  272. libev.ev_loop_destroy(self._ptr)
  273. self._ptr = NULL
  274. def __dealloc__(self):
  275. if self._ptr:
  276. self._stop_watchers()
  277. if not libev.ev_is_default_loop(self._ptr):
  278. libev.ev_loop_destroy(self._ptr)
  279. self._ptr = NULL
  280. property ptr:
  281. def __get__(self):
  282. return <size_t>self._ptr
  283. property WatcherType:
  284. def __get__(self):
  285. return watcher
  286. property MAXPRI:
  287. def __get__(self):
  288. return libev.EV_MAXPRI
  289. property MINPRI:
  290. def __get__(self):
  291. return libev.EV_MINPRI
  292. def _handle_syserr(self, message, errno):
  293. if sys.version_info[0] >= 3:
  294. message = message.decode()
  295. self.handle_error(None, SystemError, SystemError(message + ': ' + os.strerror(errno)), None)
  296. cpdef handle_error(self, context, type, value, tb):
  297. cdef object handle_error
  298. cdef object error_handler = self.error_handler
  299. if error_handler is not None:
  300. # we do want to do getattr every time so that setting Hub.handle_error property just works
  301. handle_error = getattr(error_handler, 'handle_error', error_handler)
  302. handle_error(context, type, value, tb)
  303. else:
  304. self._default_handle_error(context, type, value, tb)
  305. cpdef _default_handle_error(self, context, type, value, tb):
  306. # note: Hub sets its own error handler so this is not used by gevent
  307. # this is here to make core.loop usable without the rest of gevent
  308. traceback.print_exception(type, value, tb)
  309. if self._ptr:
  310. libev.ev_break(self._ptr, libev.EVBREAK_ONE)
  311. def run(self, nowait=False, once=False):
  312. CHECK_LOOP2(self)
  313. cdef unsigned int flags = 0
  314. if nowait:
  315. flags |= libev.EVRUN_NOWAIT
  316. if once:
  317. flags |= libev.EVRUN_ONCE
  318. with nogil:
  319. libev.ev_run(self._ptr, flags)
  320. def reinit(self):
  321. if self._ptr:
  322. libev.ev_loop_fork(self._ptr)
  323. def ref(self):
  324. CHECK_LOOP2(self)
  325. libev.ev_ref(self._ptr)
  326. def unref(self):
  327. CHECK_LOOP2(self)
  328. libev.ev_unref(self._ptr)
  329. def break_(self, int how=libev.EVBREAK_ONE):
  330. CHECK_LOOP2(self)
  331. libev.ev_break(self._ptr, how)
  332. def verify(self):
  333. CHECK_LOOP2(self)
  334. libev.ev_verify(self._ptr)
  335. def now(self):
  336. CHECK_LOOP2(self)
  337. return libev.ev_now(self._ptr)
  338. def update(self):
  339. CHECK_LOOP2(self)
  340. libev.ev_now_update(self._ptr)
  341. def __repr__(self):
  342. return '<%s at 0x%x %s>' % (self.__class__.__name__, id(self), self._format())
  343. property default:
  344. def __get__(self):
  345. CHECK_LOOP3(self)
  346. return True if libev.ev_is_default_loop(self._ptr) else False
  347. property iteration:
  348. def __get__(self):
  349. CHECK_LOOP3(self)
  350. return libev.ev_iteration(self._ptr)
  351. property depth:
  352. def __get__(self):
  353. CHECK_LOOP3(self)
  354. return libev.ev_depth(self._ptr)
  355. property backend_int:
  356. def __get__(self):
  357. CHECK_LOOP3(self)
  358. return libev.ev_backend(self._ptr)
  359. property backend:
  360. def __get__(self):
  361. CHECK_LOOP3(self)
  362. cdef unsigned int backend = libev.ev_backend(self._ptr)
  363. for key, value in _flags:
  364. if key == backend:
  365. return value
  366. return backend
  367. property pendingcnt:
  368. def __get__(self):
  369. CHECK_LOOP3(self)
  370. return libev.ev_pending_count(self._ptr)
  371. #ifdef _WIN32
  372. def io(self, libev.vfd_socket_t fd, int events, ref=True, priority=None):
  373. return io(self, fd, events, ref, priority)
  374. #else
  375. def io(self, int fd, int events, ref=True, priority=None):
  376. return io(self, fd, events, ref, priority)
  377. #endif
  378. def timer(self, double after, double repeat=0.0, ref=True, priority=None):
  379. return timer(self, after, repeat, ref, priority)
  380. def signal(self, int signum, ref=True, priority=None):
  381. return signal(self, signum, ref, priority)
  382. def idle(self, ref=True, priority=None):
  383. return idle(self, ref, priority)
  384. def prepare(self, ref=True, priority=None):
  385. return prepare(self, ref, priority)
  386. def check(self, ref=True, priority=None):
  387. return check(self, ref, priority)
  388. def fork(self, ref=True, priority=None):
  389. return fork(self, ref, priority)
  390. def async(self, ref=True, priority=None):
  391. return async(self, ref, priority)
  392. #ifdef _WIN32
  393. #else
  394. def child(self, int pid, bint trace=0, ref=True):
  395. return child(self, pid, trace, ref)
  396. def install_sigchld(self):
  397. libev.gevent_install_sigchld_handler()
  398. def reset_sigchld(self):
  399. libev.gevent_reset_sigchld_handler()
  400. #endif
  401. def stat(self, str path, float interval=0.0, ref=True, priority=None):
  402. return stat(self, path, interval, ref, priority)
  403. def run_callback(self, func, *args):
  404. CHECK_LOOP2(self)
  405. cdef callback cb = callback(func, args)
  406. self._callbacks.append(cb)
  407. libev.ev_ref(self._ptr)
  408. return cb
  409. def _format(self):
  410. if not self._ptr:
  411. return 'destroyed'
  412. cdef object msg = self.backend
  413. if self.default:
  414. msg += ' default'
  415. msg += ' pending=%s' % self.pendingcnt
  416. #ifdef LIBEV_EMBED
  417. msg += self._format_details()
  418. #endif
  419. return msg
  420. #ifdef LIBEV_EMBED
  421. def _format_details(self):
  422. cdef str msg = ''
  423. cdef object fileno = self.fileno()
  424. cdef object sigfd = None
  425. cdef object activecnt = None
  426. try:
  427. sigfd = self.sigfd
  428. except AttributeError:
  429. sigfd = None
  430. try:
  431. activecnt = self.activecnt
  432. except AttributeError:
  433. pass
  434. if activecnt is not None:
  435. msg += ' ref=' + repr(activecnt)
  436. if fileno is not None:
  437. msg += ' fileno=' + repr(fileno)
  438. if sigfd is not None and sigfd != -1:
  439. msg += ' sigfd=' + repr(sigfd)
  440. return msg
  441. def fileno(self):
  442. cdef int fd
  443. if self._ptr:
  444. fd = self._ptr.backend_fd
  445. if fd >= 0:
  446. return fd
  447. LOOP_PROPERTY(activecnt)
  448. LOOP_PROPERTY(sig_pending)
  449. #if EV_USE_SIGNALFD
  450. LOOP_PROPERTY(sigfd)
  451. #endif
  452. property origflags:
  453. def __get__(self):
  454. CHECK_LOOP3(self)
  455. return _flags_to_list(self._ptr.origflags)
  456. property origflags_int:
  457. def __get__(self):
  458. CHECK_LOOP3(self)
  459. return self._ptr.origflags
  460. #endif
  461. cdef public class callback [object PyGeventCallbackObject, type PyGeventCallback_Type]:
  462. cdef public object callback
  463. cdef public tuple args
  464. def __init__(self, callback, args):
  465. self.callback = callback
  466. self.args = args
  467. def stop(self):
  468. self.callback = None
  469. self.args = None
  470. # Note, that __nonzero__ and pending are different
  471. # nonzero is used in contexts where we need to know whether to schedule another callback,
  472. # so it's true if it's pending or currently running
  473. # 'pending' has the same meaning as libev watchers: it is cleared before entering callback
  474. def __nonzero__(self):
  475. # it's nonzero if it's pending or currently executing
  476. return self.args is not None
  477. property pending:
  478. def __get__(self):
  479. return self.callback is not None
  480. def __repr__(self):
  481. if Py_ReprEnter(<PyObjectPtr>self) != 0:
  482. return "<...>"
  483. try:
  484. format = self._format()
  485. result = "<%s at 0x%x%s" % (self.__class__.__name__, id(self), format)
  486. if self.pending:
  487. result += " pending"
  488. if self.callback is not None:
  489. result += " callback=%r" % (self.callback, )
  490. if self.args is not None:
  491. result += " args=%r" % (self.args, )
  492. if self.callback is None and self.args is None:
  493. result += " stopped"
  494. return result + ">"
  495. finally:
  496. Py_ReprLeave(<PyObjectPtr>self)
  497. def _format(self):
  498. return ''
  499. #define PYTHON_INCREF if not self._flags & 1: \
  500. Py_INCREF(<PyObjectPtr>self) \
  501. self._flags |= 1
  502. #define LIBEV_UNREF if self._flags & 6 == 4: \
  503. libev.ev_unref(self.loop._ptr) \
  504. self._flags |= 2
  505. # about readonly _flags attribute:
  506. # bit #1 set if object owns Python reference to itself (Py_INCREF was called and we must call Py_DECREF later)
  507. # bit #2 set if ev_unref() was called and we must call ev_ref() later
  508. # bit #3 set if user wants to call ev_unref() before start()
  509. #define WATCHER_BASE(TYPE) \
  510. cdef public loop loop \
  511. cdef object _callback \
  512. cdef public tuple args \
  513. cdef readonly int _flags \
  514. cdef libev.ev_##TYPE _watcher \
  515. \
  516. property ref: \
  517. \
  518. def __get__(self): \
  519. return False if self._flags & 4 else True \
  520. \
  521. def __set__(self, object value): \
  522. CHECK_LOOP3(self.loop) \
  523. if value: \
  524. if not self._flags & 4: \
  525. return # ref is already True \
  526. if self._flags & 2: # ev_unref was called, undo \
  527. libev.ev_ref(self.loop._ptr) \
  528. self._flags &= ~6 # do not want unref, no outstanding unref \
  529. else: \
  530. if self._flags & 4: \
  531. return # ref is already False \
  532. self._flags |= 4 \
  533. if not self._flags & 2 and libev.ev_is_active(&self._watcher): \
  534. libev.ev_unref(self.loop._ptr) \
  535. self._flags |= 2 \
  536. \
  537. property callback: \
  538. \
  539. def __get__(self): \
  540. return self._callback \
  541. \
  542. def __set__(self, object callback): \
  543. if not PyCallable_Check(<PyObjectPtr>callback) and callback is not None: \
  544. raise TypeError("Expected callable, not %r" % (callback, )) \
  545. self._callback = callback \
  546. \
  547. def stop(self): \
  548. CHECK_LOOP2(self.loop) \
  549. if self._flags & 2: \
  550. libev.ev_ref(self.loop._ptr) \
  551. self._flags &= ~2 \
  552. libev.ev_##TYPE##_stop(self.loop._ptr, &self._watcher) \
  553. self._callback = None \
  554. self.args = None \
  555. if self._flags & 1: \
  556. Py_DECREF(<PyObjectPtr>self) \
  557. self._flags &= ~1 \
  558. \
  559. property priority: \
  560. \
  561. def __get__(self): \
  562. return libev.ev_priority(&self._watcher) \
  563. \
  564. def __set__(self, int priority): \
  565. if libev.ev_is_active(&self._watcher): \
  566. raise AttributeError("Cannot set priority of an active watcher") \
  567. libev.ev_set_priority(&self._watcher, priority) \
  568. \
  569. def feed(self, int revents, object callback, *args): \
  570. CHECK_LOOP2(self.loop) \
  571. self.callback = callback \
  572. self.args = args \
  573. LIBEV_UNREF \
  574. libev.ev_feed_event(self.loop._ptr, &self._watcher, revents) \
  575. PYTHON_INCREF
  576. #define ACTIVE property active: \
  577. \
  578. def __get__(self): \
  579. return True if libev.ev_is_active(&self._watcher) else False
  580. #define START(TYPE) def start(self, object callback, *args): \
  581. CHECK_LOOP2(self.loop) \
  582. if callback is None: \
  583. raise TypeError('callback must be callable, not None') \
  584. self.callback = callback \
  585. self.args = args \
  586. LIBEV_UNREF \
  587. libev.ev_##TYPE##_start(self.loop._ptr, &self._watcher) \
  588. PYTHON_INCREF
  589. #define PENDING \
  590. property pending: \
  591. \
  592. def __get__(self): \
  593. return True if libev.ev_is_pending(&self._watcher) else False
  594. #define WATCHER(TYPE) WATCHER_BASE(TYPE) \
  595. \
  596. START(TYPE) \
  597. \
  598. ACTIVE \
  599. \
  600. PENDING
  601. #define COMMA ,
  602. #define INIT(TYPE, ARGS_INITIALIZERS, ARGS) \
  603. def __init__(self, loop loop ARGS_INITIALIZERS, ref=True, priority=None): \
  604. libev.ev_##TYPE##_init(&self._watcher, <void *>gevent_callback_##TYPE ARGS) \
  605. self.loop = loop \
  606. if ref: \
  607. self._flags = 0 \
  608. else: \
  609. self._flags = 4 \
  610. if priority is not None: \
  611. libev.ev_set_priority(&self._watcher, priority)
  612. cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Type]:
  613. """Abstract base class for all the watchers"""
  614. def __repr__(self):
  615. if Py_ReprEnter(<PyObjectPtr>self) != 0:
  616. return "<...>"
  617. try:
  618. format = self._format()
  619. result = "<%s at 0x%x%s" % (self.__class__.__name__, id(self), format)
  620. if self.active:
  621. result += " active"
  622. if self.pending:
  623. result += " pending"
  624. if self.callback is not None:
  625. result += " callback=%r" % (self.callback, )
  626. if self.args is not None:
  627. result += " args=%r" % (self.args, )
  628. return result + ">"
  629. finally:
  630. Py_ReprLeave(<PyObjectPtr>self)
  631. def _format(self):
  632. return ''
  633. cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]:
  634. WATCHER_BASE(io)
  635. def start(self, object callback, *args, pass_events=False):
  636. CHECK_LOOP2(self.loop)
  637. if callback is None:
  638. raise TypeError('callback must be callable, not None')
  639. self.callback = callback
  640. if pass_events:
  641. self.args = (GEVENT_CORE_EVENTS, ) + args
  642. else:
  643. self.args = args
  644. LIBEV_UNREF
  645. libev.ev_io_start(self.loop._ptr, &self._watcher)
  646. PYTHON_INCREF
  647. ACTIVE
  648. PENDING
  649. #ifdef _WIN32
  650. def __init__(self, loop loop, libev.vfd_socket_t fd, int events, ref=True, priority=None):
  651. if events & ~(libev.EV__IOFDSET | libev.EV_READ | libev.EV_WRITE):
  652. raise ValueError('illegal event mask: %r' % events)
  653. cdef int vfd = libev.vfd_open(fd)
  654. libev.vfd_free(self._watcher.fd)
  655. libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, vfd, events)
  656. self.loop = loop
  657. if ref:
  658. self._flags = 0
  659. else:
  660. self._flags = 4
  661. if priority is not None:
  662. libev.ev_set_priority(&self._watcher, priority)
  663. #else
  664. def __init__(self, loop loop, int fd, int events, ref=True, priority=None):
  665. if fd < 0:
  666. raise ValueError('fd must be non-negative: %r' % fd)
  667. if events & ~(libev.EV__IOFDSET | libev.EV_READ | libev.EV_WRITE):
  668. raise ValueError('illegal event mask: %r' % events)
  669. libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, fd, events)
  670. self.loop = loop
  671. if ref:
  672. self._flags = 0
  673. else:
  674. self._flags = 4
  675. if priority is not None:
  676. libev.ev_set_priority(&self._watcher, priority)
  677. #endif
  678. property fd:
  679. def __get__(self):
  680. return libev.vfd_get(self._watcher.fd)
  681. def __set__(self, long fd):
  682. if libev.ev_is_active(&self._watcher):
  683. raise AttributeError("'io' watcher attribute 'fd' is read-only while watcher is active")
  684. cdef int vfd = libev.vfd_open(fd)
  685. libev.vfd_free(self._watcher.fd)
  686. libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, vfd, self._watcher.events)
  687. property events:
  688. def __get__(self):
  689. return self._watcher.events
  690. def __set__(self, int events):
  691. if libev.ev_is_active(&self._watcher):
  692. raise AttributeError("'io' watcher attribute 'events' is read-only while watcher is active")
  693. libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, self._watcher.fd, events)
  694. property events_str:
  695. def __get__(self):
  696. return _events_to_str(self._watcher.events)
  697. def _format(self):
  698. return ' fd=%s events=%s' % (self.fd, self.events_str)
  699. #ifdef _WIN32
  700. def __cinit__(self):
  701. self._watcher.fd = -1;
  702. def __dealloc__(self):
  703. libev.vfd_free(self._watcher.fd)
  704. #endif
  705. cdef public class timer(watcher) [object PyGeventTimerObject, type PyGeventTimer_Type]:
  706. WATCHER_BASE(timer)
  707. def start(self, object callback, *args, update=True):
  708. CHECK_LOOP2(self.loop)
  709. if callback is None:
  710. raise TypeError('callback must be callable, not None')
  711. self.callback = callback
  712. self.args = args
  713. LIBEV_UNREF
  714. if update:
  715. libev.ev_now_update(self.loop._ptr)
  716. libev.ev_timer_start(self.loop._ptr, &self._watcher)
  717. PYTHON_INCREF
  718. ACTIVE
  719. PENDING
  720. def __init__(self, loop loop, double after=0.0, double repeat=0.0, ref=True, priority=None):
  721. if repeat < 0.0:
  722. raise ValueError("repeat must be positive or zero: %r" % repeat)
  723. libev.ev_timer_init(&self._watcher, <void *>gevent_callback_timer, after, repeat)
  724. self.loop = loop
  725. if ref:
  726. self._flags = 0
  727. else:
  728. self._flags = 4
  729. if priority is not None:
  730. libev.ev_set_priority(&self._watcher, priority)
  731. property at:
  732. def __get__(self):
  733. return self._watcher.at
  734. # QQQ: add 'after' and 'repeat' properties?
  735. def again(self, object callback, *args, update=True):
  736. CHECK_LOOP2(self.loop)
  737. self.callback = callback
  738. self.args = args
  739. LIBEV_UNREF
  740. if update:
  741. libev.ev_now_update(self.loop._ptr)
  742. libev.ev_timer_again(self.loop._ptr, &self._watcher)
  743. PYTHON_INCREF
  744. cdef public class signal(watcher) [object PyGeventSignalObject, type PyGeventSignal_Type]:
  745. WATCHER(signal)
  746. def __init__(self, loop loop, int signalnum, ref=True, priority=None):
  747. if signalnum < 1 or signalnum >= signalmodule.NSIG:
  748. raise ValueError('illegal signal number: %r' % signalnum)
  749. # still possible to crash on one of libev's asserts:
  750. # 1) "libev: ev_signal_start called with illegal signal number"
  751. # EV_NSIG might be different from signal.NSIG on some platforms
  752. # 2) "libev: a signal must not be attached to two different loops"
  753. # we probably could check that in LIBEV_EMBED mode, but not in general
  754. libev.ev_signal_init(&self._watcher, <void *>gevent_callback_signal, signalnum)
  755. self.loop = loop
  756. if ref:
  757. self._flags = 0
  758. else:
  759. self._flags = 4
  760. if priority is not None:
  761. libev.ev_set_priority(&self._watcher, priority)
  762. cdef public class idle(watcher) [object PyGeventIdleObject, type PyGeventIdle_Type]:
  763. WATCHER(idle)
  764. INIT(idle,,)
  765. cdef public class prepare(watcher) [object PyGeventPrepareObject, type PyGeventPrepare_Type]:
  766. WATCHER(prepare)
  767. INIT(prepare,,)
  768. cdef public class check(watcher) [object PyGeventCheckObject, type PyGeventCheck_Type]:
  769. WATCHER(check)
  770. INIT(check,,)
  771. cdef public class fork(watcher) [object PyGeventForkObject, type PyGeventFork_Type]:
  772. WATCHER(fork)
  773. INIT(fork,,)
  774. cdef public class async(watcher) [object PyGeventAsyncObject, type PyGeventAsync_Type]:
  775. WATCHER_BASE(async)
  776. START(async)
  777. ACTIVE
  778. property pending:
  779. def __get__(self):
  780. return True if libev.ev_async_pending(&self._watcher) else False
  781. INIT(async,,)
  782. def send(self):
  783. CHECK_LOOP2(self.loop)
  784. libev.ev_async_send(self.loop._ptr, &self._watcher)
  785. #ifdef _WIN32
  786. #else
  787. cdef public class child(watcher) [object PyGeventChildObject, type PyGeventChild_Type]:
  788. WATCHER(child)
  789. def __init__(self, loop loop, int pid, bint trace=0, ref=True):
  790. if not loop.default:
  791. raise TypeError('child watchers are only available on the default loop')
  792. libev.gevent_install_sigchld_handler()
  793. libev.ev_child_init(&self._watcher, <void *>gevent_callback_child, pid, trace)
  794. self.loop = loop
  795. if ref:
  796. self._flags = 0
  797. else:
  798. self._flags = 4
  799. def _format(self):
  800. return ' pid=%r rstatus=%r' % (self.pid, self.rstatus)
  801. property pid:
  802. def __get__(self):
  803. return self._watcher.pid
  804. property rpid:
  805. def __get__(self):
  806. return self._watcher.rpid
  807. def __set__(self, int value):
  808. self._watcher.rpid = value
  809. property rstatus:
  810. def __get__(self):
  811. return self._watcher.rstatus
  812. def __set__(self, int value):
  813. self._watcher.rstatus = value
  814. #endif
  815. cdef public class stat(watcher) [object PyGeventStatObject, type PyGeventStat_Type]:
  816. WATCHER(stat)
  817. cdef readonly str path
  818. cdef readonly bytes _paths
  819. def __init__(self, loop loop, str path, float interval=0.0, ref=True, priority=None):
  820. self.path = path
  821. cdef bytes paths
  822. if isinstance(path, unicode):
  823. # the famous Python3 filesystem encoding debacle hits us here. Can we do better?
  824. # We must keep a reference to the encoded string so that its bytes don't get freed
  825. # and overwritten, leading to strange errors from libev ("no such file or directory")
  826. paths = (<unicode>path).encode(sys.getfilesystemencoding())
  827. self._paths = paths
  828. else:
  829. paths = <bytes>path
  830. self._paths = paths
  831. libev.ev_stat_init(&self._watcher, <void *>gevent_callback_stat, <char*>paths, interval)
  832. self.loop = loop
  833. if ref:
  834. self._flags = 0
  835. else:
  836. self._flags = 4
  837. if priority is not None:
  838. libev.ev_set_priority(&self._watcher, priority)
  839. property attr:
  840. def __get__(self):
  841. if not self._watcher.attr.st_nlink:
  842. return
  843. return _pystat_fromstructstat(&self._watcher.attr)
  844. property prev:
  845. def __get__(self):
  846. if not self._watcher.prev.st_nlink:
  847. return
  848. return _pystat_fromstructstat(&self._watcher.prev)
  849. property interval:
  850. def __get__(self):
  851. return self._watcher.interval
  852. __SYSERR_CALLBACK = None
  853. cdef void _syserr_cb(char* msg) with gil:
  854. try:
  855. __SYSERR_CALLBACK(msg, errno)
  856. except:
  857. set_syserr_cb(None)
  858. print_exc = getattr(traceback, 'print_exc', None)
  859. if print_exc is not None:
  860. print_exc()
  861. cpdef set_syserr_cb(callback):
  862. global __SYSERR_CALLBACK
  863. if callback is None:
  864. libev.ev_set_syserr_cb(NULL)
  865. __SYSERR_CALLBACK = None
  866. elif callable(callback):
  867. libev.ev_set_syserr_cb(<void *>_syserr_cb)
  868. __SYSERR_CALLBACK = callback
  869. else:
  870. raise TypeError('Expected callable or None, got %r' % (callback, ))
  871. #ifdef LIBEV_EMBED
  872. LIBEV_EMBED = True
  873. EV_USE_FLOOR = libev.EV_USE_FLOOR
  874. EV_USE_CLOCK_SYSCALL = libev.EV_USE_CLOCK_SYSCALL
  875. EV_USE_REALTIME = libev.EV_USE_REALTIME
  876. EV_USE_MONOTONIC = libev.EV_USE_MONOTONIC
  877. EV_USE_NANOSLEEP = libev.EV_USE_NANOSLEEP
  878. EV_USE_INOTIFY = libev.EV_USE_INOTIFY
  879. EV_USE_SIGNALFD = libev.EV_USE_SIGNALFD
  880. EV_USE_EVENTFD = libev.EV_USE_EVENTFD
  881. EV_USE_4HEAP = libev.EV_USE_4HEAP
  882. #else
  883. LIBEV_EMBED = False
  884. #endif