1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110 |
- # Generated by cythonpp.py on 2017-06-05 11:35:20
- # Copyright (c) 2009-2012 Denis Bilenko. See LICENSE for details.
- # This directive, supported in Cython 0.24+, causes sources files to be
- # much smaller and thus cythonpp.py to be slightly faster. But it does make
- # debugging more difficult.
- # cython: emit_code_comments=False
- cimport cython
- cimport libev
- # Note this is not the standard cython 'cpython' (which has a backwards compat alias of 'python')
- # it's our custom def. If it's not on the include path, we get warned.
- from python cimport *
- # Work around lack of absolute_import in Cython
- # Note for PY3: not doing so will leave reference to locals() on import
- # (reproducible under Python 3.3, not under Python 3.4; see test__refcount_core.py)
- sys = __import__('sys', level=0)
- os = __import__('os', level=0)
- traceback = __import__('traceback', level=0)
- signalmodule = __import__('signal', level=0)
- __all__ = ['get_version',
- 'get_header_version',
- 'supported_backends',
- 'recommended_backends',
- 'embeddable_backends',
- 'time',
- 'loop']
- cdef tuple integer_types
- if sys.version_info[0] >= 3:
- integer_types = int,
- else:
- integer_types = (int, long)
- 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, integer_types):
- 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 as 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, integer_types):
- 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
- c_flags |= libev.EVFLAG_FORKCHECK
- 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):
- if sys.version_info[0] >= 3:
- message = message.decode()
- 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()
- def reset_sigchld(self):
- libev.gevent_reset_sigchld_handler()
- #endif
- def stat(self, str 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 str path
- cdef readonly bytes _paths
- def __init__(self, loop loop, str path, float interval=0.0, ref=True, priority=None):
- self.path = path
- cdef bytes paths
- if isinstance(path, unicode):
- # the famous Python3 filesystem encoding debacle hits us here. Can we do better?
- # We must keep a reference to the encoded string so that its bytes don't get freed
- # and overwritten, leading to strange errors from libev ("no such file or directory")
- paths = (<unicode>path).encode(sys.getfilesystemencoding())
- self._paths = paths
- else:
- paths = <bytes>path
- self._paths = paths
- libev.ev_stat_init(&self._watcher, <void *>gevent_callback_stat, <char*>paths, 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
|