î j fÛÉã@sdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlmZddlmZddlmZddlmZddlmZdd lmZdd lmZd gZd Zd ZdZdd„Zdd„Z dZ!e"e dƒrŽe!e j#OZ!ne"e dƒr­e!e j$OZ!nej%ddƒdd„ƒZ&dd„Z'dd„Z(Gdd„dej)ƒZ*Gdd „d ej+ƒZ,dS) a™Base implementation of event loop. The event loop can be broken up into a multiplexer (the part responsible for notifying us of I/O events) and the event loop proper, which wraps a multiplexer with functionality for scheduling callbacks, immediately or at a given time in the future. Whenever a public API takes a callback, subsequent positional arguments will be passed to the callback if/when it is called. This avoids the proliferation of trivial lambdas implementing closures. Keyword arguments for the callback are not supported; this is a conscious design decision, leaving the door open for keyword arguments to modify the meaning of the API call itself. éNé)Úcompat)Ú coroutines)Úevents)Úfutures)Útasks)Ú coroutine)ÚloggerÚ BaseEventLoopéédgà?cCsH|j}tj|ƒr:t|jtjƒr:t|jƒSt|ƒSdS)N) Ú _callbackÚinspectZismethodÚ isinstanceÚ__self__rÚTaskÚreprÚstr)ÚhandleÚcb©rú8/opt/alt/python34/lib64/python3.4/asyncio/base_events.pyÚ_format_handle9s $ rcCs4|tjkrdS|tjkr&dSt|ƒSdS)Nzz)Ú subprocessÚPIPEÚSTDOUTr)ÚfdrrrÚ _format_pipeBs rÚ SOCK_NONBLOCKÚ SOCK_CLOEXECÚmaxsizeicCsæ|dtjtjhks'|dkr+dS|tM}|tjkrQtj}n|tjkrltj}ndSttdƒr2|tjkr£tjtj g}n |g}x|D]w}y\|tj krëtj ||j dƒdƒntj ||ƒ|||d||ffSWq³t k r)Yq³Xq³WdSyt j|ƒ}WnKtk r’y t j|j dƒdƒ}Wntk r�dSYnXYnX|jdkr«tjntj }|tj|fkrÍdS|||d||ffS)NrÚ inet_ptonú%Úé)ÚsocketZ IPPROTO_TCPZ IPPROTO_UDPÚ_SOCKET_TYPE_MASKÚ SOCK_STREAMÚ SOCK_DGRAMÚhasattrÚ AF_UNSPECZAF_INETÚAF_INET6r!Ú partitionÚOSErrorÚ ipaddressZ IPv4AddressÚ ValueErrorZ IPv6AddressÚversion)ÚhostÚportÚfamilyÚtypeÚprotoZafsÚafÚaddrrrrÚ _ipaddr_infoSs@'           !r8cCsyttdƒr%|jtjkr%dS|dd…\}}t|||j|j|jƒdkrutd|ƒ‚ndS)NÚAF_UNIXéz2address must be resolved (IP address), got host %r)r)r%r3r9r8r4r5r/)ÚsockÚaddressr1r2rrrÚ_check_resolved_address�s !'r=cCs=|j}t|tƒr,t|tƒ r,dS|jjƒdS)N)Z _exceptionrÚ BaseExceptionÚ ExceptionÚ_loopÚstop)ÚfutÚexcrrrÚ_run_until_complete_cbšs  rDc@sjeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Ze d d„ƒZ dS)ÚServercCs(||_||_d|_g|_dS)Nr)r@ÚsocketsÚ _active_countÚ_waiters)ÚselfÚlooprFrrrÚ__init__¦s   zServer.__init__cCsd|jj|jfS)Nz<%s sockets=%r>)Ú __class__Ú__name__rF)rIrrrÚ__repr__¬szServer.__repr__cCs(|jdk st‚|jd7_dS)Nr)rFÚAssertionErrorrG)rIrrrÚ_attach¯szServer._attachcCsS|jdkst‚|jd8_|jdkrO|jdkrO|jƒndS)Nrr)rGrOrFÚ_wakeup)rIrrrÚ_detach³szServer._detachcCsc|j}|dkrdSd|_x|D]}|jj|ƒq)W|jdkr_|jƒndS)Nr)rFr@Z _stop_servingrGrQ)rIrFr;rrrÚclose¹s    z Server.closecCsC|j}d|_x*|D]"}|jƒs|j|ƒqqWdS)N)rHÚdoneÚ set_result)rIÚwaitersÚwaiterrrrrQÃs     zServer._wakeupccsT|jdks|jdkr"dStjd|jƒ}|jj|ƒ|DdHdS)NrJ)rFrHrÚFuturer@Úappend)rIrWrrrÚ wait_closedÊs zServer.wait_closedN) rMÚ __module__Ú __qualname__rKrNrPrRrSrQrrZrrrrrE¤s      rEc@s!eZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d d d d dd„Zd dddd d d d d dd„Z d d d dd„Z d d dd„Z d d dd„Z e d dd„ƒZdd„Zdd „Zd!d"„Zd#d$„Zd%d&„Zd'd(„Zd)d*„Zd+d,„ZejrPd-d.„Znd/d0„Zd1d2„Zd3d4„Zd5d6„Zd7d8„Zd9d:„Zd;d<„Z d=d>„Z!d?d@„Z"dAdB„Z#dCdD„Z$dEdFdGdFdHdFdIdFdJdK„Z%dFdLdM„Z&e d d dNd dEdFdHdFdIdFdOd dPd dd dQdR„ƒZ'e dSdT„ƒZ(e d d dEdFdHdFdIdFdUd dVd dWd dOd dXdY„ƒZ)e dZd[„ƒZ*e d d dEe+j,dIe+j-dOd d\d]dNd dUd dVd d^d_„ƒZ.e d`da„ƒZ/e dbdc„ƒZ0ddde„Z1e dfe2j3dge2j3dhe2j3diddjdkdldFdmdn„ƒZ4e dfe2j3dge2j3dhe2j3diddjddldFdodp„ƒZ5dqdr„Z6dsdt„Z7dudv„Z8dwdx„Z9dydz„Z:d{d|„Z;d}d~„Z<dd€„Z=d�d‚„Z>dƒd„„Z?d S)…r cCsÀd|_d|_d|_tjƒ|_g|_d|_d|_d|_ t j dƒj |_ d|_|jtjj o”ttjjdƒƒƒd|_d|_d|_d|_dS)NrFÚ monotonicZPYTHONASYNCIODEBUGgš™™™™™¹?)Ú_timer_cancelled_countÚ_closedÚ _stoppingÚ collectionsÚdequeÚ_readyÚ _scheduledÚ_default_executorZ _internal_fdsÚ _thread_idÚtimeZget_clock_infoZ resolutionÚ_clock_resolutionÚ_exception_handlerÚ set_debugÚsysÚflagsÚignore_environmentÚboolÚosÚenvironÚgetÚslow_callback_durationÚ_current_handleÚ _task_factoryÚ_coroutine_wrapper_set)rIrrrrKÕs            zBaseEventLoop.__init__cCs,d|jj|jƒ|jƒ|jƒfS)Nz"<%s running=%s closed=%s debug=%s>)rLrMÚ is_runningÚ is_closedÚ get_debug)rIrrrrNëszBaseEventLoop.__repr__cCs]|jƒ|jdkrGtj|d|ƒ}|jrY|jd=qYn|j||ƒ}|S)zDSchedule a coroutine object. Return a task object. NrJréÿÿÿÿ)Ú _check_closedrtrrÚ_source_traceback)rIZcoroZtaskrrrÚ create_taskðs  zBaseEventLoop.create_taskcCs5|dk r(t|ƒ r(tdƒ‚n||_dS)awSet a task factory that will be used by loop.create_task(). If factory is None the default task factory will be set. If factory is a callable, it should have a signature matching '(loop, coro)', where 'loop' will be a reference to the active event loop, 'coro' will be a coroutine object. The callable must return a Future. Nz'task factory must be a callable or None)ÚcallableÚ TypeErrorrt)rIZfactoryrrrÚset_task_factoryþs zBaseEventLoop.set_task_factorycCs|jS)zs zBaseEventLoop._check_closedc Cs�|jƒ|jƒr%tdƒ‚n|j|jƒtjƒ|_z"x|jƒ|j rJPqJqJWWdd|_ d|_|jdƒXdS)zRun until stop() is called.zEvent loop is running.NF) rzrvr™Ú_set_coroutine_wrapperÚ_debugÚ threadingÚ get_identrfÚ _run_oncer`)rIrrrÚ run_foreverBs       zBaseEventLoop.run_foreverc CsË|jƒt|tjƒ }tj|d|ƒ}|rDd|_n|jtƒy|j ƒWn7|r‘|j ƒr‘|j ƒ r‘|j ƒn‚YnX|j tƒ|j ƒsÁtdƒ‚n|jƒS)a\Run until the Future is done. If the argument is a coroutine, it is wrapped in a Task. WARNING: It would be disastrous to call run_until_complete() with the same coroutine twice -- it would wrap it in two different Tasks and that can't be good. Return the Future's result, or raise its exception. rJFz+Event loop stopped before Future completed.)rzrrrXrZ ensure_futureZ_log_destroy_pendingZadd_done_callbackrDrŸrTZ cancelledÚ exceptionZremove_done_callbackr™Úresult)rIZfutureZnew_taskrrrÚrun_until_completeSs      z BaseEventLoop.run_until_completecCs d|_dS)z´Stop running the event loop. Every callback already scheduled will still run. This simply informs run_forever to stop looping after a complete iteration. TN)r`)rIrrrrAwszBaseEventLoop.stopcCsœ|jƒrtdƒ‚n|jr(dS|jrDtjd|ƒnd|_|jjƒ|jjƒ|j }|dk r˜d|_ |j ddƒndS)z¾Close the event loop. This clears the queues and shuts down the executor, but does not wait for the executor to finish. The event loop must not be running. z!Cannot close a running event loopNzClose %rTÚwaitF) rvr™r_r›r ÚdebugrcÚclearrdreZshutdown)rIÚexecutorrrrrSs         zBaseEventLoop.closecCs|jS)z*Returns True if the event loop was closed.)r_)rIrrrrw•szBaseEventLoop.is_closedcCs@|jƒs<tjd|tƒ|jƒs<|jƒq<ndS)Nzunclosed event loop %r)rwÚwarningsÚwarnÚResourceWarningrvrS)rIrrrÚ__del__�s  zBaseEventLoop.__del__cCs |jdk S)z*Returns True if the event loop is running.N)rf)rIrrrrv£szBaseEventLoop.is_runningcCs tjƒS)zçReturn the time according to the event loop's clock. This is a float expressed in seconds since an epoch, but the epoch, precision, accuracy and drift are unspecified and may differ per event loop. )rgr])rIrrrrg§szBaseEventLoop.timecGs9|j|jƒ|||Œ}|jr5|jd=n|S)a8Arrange for a callback to be called at a given time. Return a Handle: an opaque object with a cancel() method that can be used to cancel the call. The delay can be an int or float, expressed in seconds. It is always relative to the current time. Each callback will be called exactly once. If two callbacks are scheduled for exactly the same time, it undefined which will be called first. Any positional arguments after the callback will be passed to the callback when it is called. rry)Úcall_atrgr{)rIZdelayÚcallbackrŽÚtimerrrrÚ call_later°s  zBaseEventLoop.call_latercGs›tj|ƒstj|ƒr-tdƒ‚n|jƒ|jrM|jƒntj||||ƒ}|j r{|j d=nt j |j |ƒd|_ |S)z|Like call_later(), but uses an absolute time. Absolute time corresponds to the event loop's time() method. z(coroutines cannot be used with call_at()rTry) rÚ iscoroutineÚiscoroutinefunctionr~rzr›Ú _check_threadrÚ TimerHandler{ÚheapqÚheappushrd)rIÚwhenr¬rŽr­rrrr«Ås      zBaseEventLoop.call_atcGsB|jr|jƒn|j||ƒ}|jr>|jd=n|S)aTArrange for a callback to be called as soon as possible. This operates as a FIFO queue: callbacks are called in the order in which they are registered. Each callback will be called exactly once. Any positional arguments after the callback will be passed to the callback when it is called. rry)r›r±Ú _call_soonr{)rIr¬rŽrrrrÚ call_soon×s    zBaseEventLoop.call_sooncCsvtj|ƒstj|ƒr-tdƒ‚n|jƒtj|||ƒ}|jrb|jd=n|jj |ƒ|S)Nz*coroutines cannot be used with call_soon()rry) rr¯r°r~rzrÚHandler{rcrY)rIr¬rŽrrrrr¶ès   zBaseEventLoop._call_sooncCsA|jdkrdStjƒ}||jkr=tdƒ‚ndS)aoCheck that the current thread is the thread running the event loop. Non-thread-safe methods of this class make this assumption and will likely behave incorrectly when the assumption is violated. Should only be called when (self._debug == True). The caller is responsible for checking this condition for performance reasons. NzMNon-thread-safe operation invoked on an event loop other than the current one)rfrœr�r™)rIZ thread_idrrrr±ós  zBaseEventLoop._check_threadcGs6|j||ƒ}|jr(|jd=n|jƒ|S)z"Like call_soon(), but thread-safe.rry)r¶r{r–)rIr¬rŽrrrrÚcall_soon_threadsafes    z"BaseEventLoop.call_soon_threadsafecGstj|ƒstj|ƒr-tdƒ‚n|jƒt|tjƒr±| sVt‚t|tj ƒ sot‚|j r›t j d|ƒ}|j dƒ|S|j|j}}n|dkró|j}|dkrótj jtƒ}||_qónt j|j||Œd|ƒS)Nz0coroutines cannot be used with run_in_executor()rJ)rr¯r°r~rzrrr¸rOr²Ú _cancelledrrXrUr Ú_argsreÚ concurrentZThreadPoolExecutorÚ _MAX_WORKERSZ wrap_futureZsubmit)rIr¦ÚfuncrŽÚfrrrÚrun_in_executor s$       zBaseEventLoop.run_in_executorcCs ||_dS)N)re)rIr¦rrrÚset_default_executor sz"BaseEventLoop.set_default_executorc Csd||fg}|r-|jd|ƒn|rG|jd|ƒn|ra|jd|ƒn|r{|jd|ƒndj|ƒ}tjd|ƒ|jƒ}tj||||||ƒ} |jƒ|} d|| d | f}| |jkr tj|ƒn tj|ƒ| S) Nz%s:%rz family=%rztype=%rzproto=%rzflags=%rz, zGet address info %sz(Getting address info %s took %.3f ms: %rg@�@) rYÚjoinr r¤rgr%Ú getaddrinforrÚinfo) rIr1r2r3r4r5rlÚmsgÚt0ZaddrinfoÚdtrrrÚ_getaddrinfo_debug#s(  z BaseEventLoop._getaddrinfo_debugr3rr4r5rlc Cs¡t|||||ƒ}|dk rJtjd|ƒ}|j|gƒ|S|jrx|jd|j||||||ƒS|jdtj||||||ƒSdS)NrJ) r8rrXrUr›rÀrÈr%rÃ) rIr1r2r3r4r5rlrÄrBrrrrÃ<s  zBaseEventLoop.getaddrinfocCs|jdtj||ƒS)N)rÀr%Ú getnameinfo)rIZsockaddrrlrrrrÉJszBaseEventLoop.getnameinfoÚsslr;Ú local_addrc#s0| dk r"| r"tdƒ‚n| dkrR|rR|sItdƒ‚n|} n|dk sj|dk r¤|dk r…tdƒ‚n|j||d|dtjd|d|ƒ} | g} | dk r|jd|dtjd|d|| Œ} | j| ƒnd} tj| d |ƒDdH| jƒ}|s@td ƒ‚n| dk rp| jƒ}|sptd ƒ‚qpng}xF|D]š\}}}}}ytjd|d|d|ƒ}|j d ƒ| dk rpx¡|D]ƒ\}}}}}y|j |ƒPWqÓtk rU}z9t|j d j ||j jƒƒƒ}|j|ƒWYdd}~XqÓXqÓW|jƒd}w}n|jr�tjd ||ƒn|j||ƒDdHWnntk rò}z+|dk rÓ|jƒn|j|ƒWYdd}~Xq}|dk r|jƒn‚Yq}XPq}Wt|ƒdkr:|d‚q¿t|dƒ‰t‡fdd†|Dƒƒrv|d‚ntdj djdd„|Dƒƒƒƒ‚n|dkr¿tdƒ‚n|j d ƒ|j|||| ƒDdH\}}|jr&|jdƒ}tjd|||||ƒn||fS)aöConnect to a TCP server. Create a streaming transport connection to a given Internet host and port: socket family AF_INET or socket.AF_INET6 depending on host (or family if specified), socket type SOCK_STREAM. protocol_factory must be a callable returning a protocol instance. This method is a coroutine which will try to establish the connection in the background. When successful, the coroutine returns a (transport, protocol) pair. Nz+server_hostname is only meaningful with sslz:You must set server_hostname when using ssl without a hostz8host/port and sock can not be specified at the same timer3r4r5rlrJz!getaddrinfo() returned empty listFz2error while attempting to bind on address {!r}: {}zconnect %r to %rrrc3s!|]}t|ƒˆkVqdS)N)r)Ú.0rC)Úmodelrrú ²sz2BaseEventLoop.create_connection..zMultiple exceptions: {}z, css|]}t|ƒVqdS)N)r)rÌrCrrrrηsz5host and port was not specified and no sock specifiedr%z%r connected to %s:%r: (%r, %r))r/rÃr%r'rYrr£r¡r-Ú setblockingÚbindÚerrnoÚformatÚstrerrorÚlowerrSr›r r¤Ú sock_connectÚlenrÚallrÂÚ_create_connection_transportZget_extra_info)rIÚprotocol_factoryr1r2rÊr3r5rlr;rËr‡Úf1ÚfsÚf2ÚinfosZ laddr_infosÚ exceptionsr4Zcnamer<Ú_ZladdrrCÚ transportr„r)rÍrÚcreate_connectionMs˜              $         %     zBaseEventLoop.create_connectionc cs§|ƒ}tjd|ƒ}|rct|tƒr6dn|}|j||||ddd|ƒ}n|j|||ƒ}y |DdHWn|jƒ‚YnX||fS)NrJr†Fr‡)rrXrrnr‰r…rS) rIr;rÙrÊr‡r„rWrˆràrrrrØÉs   z*BaseEventLoop._create_connection_transportÚ reuse_addressÚ reuse_portÚallow_broadcastc#s½| dk rňs<ˆs<|s<|s<|s<|s<|s<| r¯tdˆdˆd|d|d|d|d|d | ƒ} d jd d „| jƒDƒƒ} td j| ƒƒ‚n| jdƒd} nRˆpΈs|dkrìtdƒ‚n||fd ff}nEtjƒ}xÿdˆfdˆffD]å\}}|dk r)t|t ƒrbt |ƒdksnt dƒ‚|j d|dt jd|d||ŒDdH}|s²tdƒ‚nxY|D]N\}}}}}||f}||krùddg||.zNsocket modifier keyword arguments can not be used when sock is specified. ({})Frzunexpected address familyrr:z2-tuple is expectedr4z!getaddrinfo() returned empty listcsNg|]D\}}ˆr(|ddkp;ˆo;|ddks||f‘qS)rNrr)rÌÚkeyZ addr_pair)rËrårrú s z:BaseEventLoop.create_datagram_endpoint..zcan not get address informationÚposixÚcygwinÚ SO_REUSEPORTz)reuse_port not supported by socket modulerJz@Datagram endpoint local_addr=%r remote_addr=%r created: (%r, %r)z2Datagram endpoint remote_addr=%r created: (%r, %r))NN)%ÚdictrÂÚitemsr/rÒrÏraÚ OrderedDictrÚtuplerÖrOrÃr%r(r-roÚnamerkÚplatformÚ setsockoptÚ SOL_SOCKETÚ SO_REUSEADDRr)rìZ SO_BROADCASTrÐrÕrSrYrrXrŠr›r rÄr¤)rIrÙrËrår3r5rlrârãrär;ZoptsZproblemsZr_addrZaddr_pairs_infoZ addr_infosÚidxr7rÝZfamrßZpror<rèrÞZ local_addressZremote_addressrCr„rWràr)rËrårÚcreate_datagram_endpointÞs¶         % $    " !               z&BaseEventLoop.create_datagram_endpointc csN|j||d|dtjd|ƒDdH}|sJtdj|ƒƒ‚n|S)Nr3r4rlz%getaddrinfo({!r}) returned empty list)rÃr%r'r-rÒ)rIr1r2r3rlrÝrrrÚ_create_server_getaddrinfoZs  z(BaseEventLoop._create_server_getaddrinfoÚbacklogr c #s…t|tƒrtdƒ‚n|dk s6ˆdk rñ|dk rQtdƒ‚nttddƒ} | dkr�tjdkoŠtj dk} ng} |dkr®dg} n4t|t ƒsÐt|t j ƒ rÜ|g} n|} ‡‡‡‡fd d †| Dƒ}t jd ˆ|ŒDdH}tjj|ƒ}d }z�xƒ|D]{}|\}}}}}ytj|||ƒ}Wn@tjk r¶ˆjr¯tjd |||ddƒnw@YnX| j|ƒ| ræ|jtjtjdƒn| r&ttdƒs tdƒ‚q&|jtjtjdƒn|| kr]ttdƒr]|jtjtjdƒny|j|ƒWq@tk rº}z*t|j d||j!j"ƒfƒ‚WYdd}~Xq@Xq@Wd}Wd|síx| D]}|j#ƒqÖWnXn$|dkr tdƒ‚n|g} t$ˆ| ƒ}x>| D]6}|j%|ƒ|j&d ƒˆj'||||ƒq+Wˆjr�tj(d|ƒn|S)a‚Create a TCP server. The host parameter can be a string, in that case the TCP server is bound to host and port. The host parameter can also be a sequence of strings and in that case the TCP server is bound to all hosts of the sequence. Return a Server object which can be used to stop the service. This method is a coroutine. z*ssl argument must be an SSLContext or NoneNz8host/port and sock can not be specified at the same timer+rrêrër#c s.g|]$}ˆj|ˆdˆdˆƒ‘qS)r3rl)rø)rÌr1)r3rlr2rIrrréŒs z/BaseEventLoop.create_server..rJFz:create_server() failed to create socket.socket(%r, %r, %r)Úexc_infoTrìz)reuse_port not supported by socket moduleÚ IPPROTO_IPV6z0error while attempting to bind on address %r: %sz)Neither host/port nor sock were specifiedz %r is serving))rrnr~r/Úgetattrr%rorñrkròrraÚIterablerZgatherÚ itertoolsÚchainÚ from_iterableÚerrorr›r ÚwarningrYrórôrõr)rìrûZ IPV6_V6ONLYrÐr-rÑrÓrÔrSrEZlistenrÏZ_start_servingrÄ)rIrÙr1r2r3rlr;rùrÊrârãr+rFZhostsrÛrÝZ completedÚresr6Zsocktyper5Z canonnameZsaÚerrr‚r)r3rlr2rIrÚ create_servercs‚   !            0        zBaseEventLoop.create_serverc cs‡|ƒ}tjd|ƒ}|j|||ƒ}y |DdHWn|jƒ‚YnX|jr}tjd|jƒ||ƒn||fS)NrJz Read pipe %r connected: (%r, %r))rrXrŒrSr›r r¤Úfileno)rIrÙr‹r„rWràrrrÚconnect_read_pipeÊs     zBaseEventLoop.connect_read_pipec cs‡|ƒ}tjd|ƒ}|j|||ƒ}y |DdHWn|jƒ‚YnX|jr}tjd|jƒ||ƒn||fS)NrJz!Write pipe %r connected: (%r, %r))rrXr�rSr›r r¤r)rIrÙr‹r„rWràrrrÚconnect_write_pipeÛs     z BaseEventLoop.connect_write_pipecCsÊ|g}|dk r/|jdt|ƒƒn|dk rd|tjkrd|jdt|ƒƒnL|dk rŠ|jdt|ƒƒn|dk r°|jdt|ƒƒntjdj|ƒƒdS)Nzstdin=%szstdout=stderr=%sz stdout=%sz stderr=%sú )rYrrrr r¤rÂ)rIrÅr�r‘r’rÄrrrÚ_log_subprocessìs    zBaseEventLoop._log_subprocessr�r‘r’Úuniversal_newlinesr�Tr“c ksôt|ttfƒs$tdƒ‚n|r9tdƒ‚n|sNtdƒ‚n|dkritdƒ‚n|ƒ} |jržd|} |j| |||ƒn|j| |d||||| �DdH} |jrêtjd| | fƒn| | fS) Nzcmd must be a stringz universal_newlines must be Falsezshell must be Truerzbufsize must be 0zrun shell command %rTz%s: %r) rÚbytesrr/r›r r•r rÄ) rIrÙÚcmdr�r‘r’r r�r“r”r„Ú debug_logràrrrÚsubprocess_shellùs"    # zBaseEventLoop.subprocess_shellc os|rtdƒ‚n|r*tdƒ‚n|dkrEtdƒ‚n|f| } x?| D]7} t| ttfƒsYtdt| ƒjƒ‚qYqYW|ƒ} |jrÉd|}|j||||ƒn|j | | d||||| �DdH}|jrt j d||fƒn|| fS) Nz universal_newlines must be Falsezshell must be Falserzbufsize must be 0z8program arguments must be a bytes or text string, not %szexecute program %rFz%s: %r) r/rrr r~r4rMr›r r•r rÄ)rIrÙZprogramr�r‘r’r r�r“rŽr”Z popen_argsÚargr„rràrrrÚsubprocess_execs*       zBaseEventLoop.subprocess_execcCs>|dk r1t|ƒ r1tdj|ƒƒ‚n||_dS)a³Set handler as the new event loop exception handler. If handler is None, the default exception handler will be set. If handler is a callable object, it should have a signature matching '(loop, context)', where 'loop' will be a reference to the active event loop, 'context' will be a dict object (see `call_exception_handler()` documentation for details about context). Nz/A callable object or None is expected, got {!r})r}r~rÒri)rIZhandlerrrrÚset_exception_handler0s  z#BaseEventLoop.set_exception_handlerc Cs�|jdƒ}|sd}n|jdƒ}|dk rTt|ƒ||jf}nd}d|kr”|jdk r”|jjr”|jj|dú exceptionúmessage)rqr4Ú __traceback__rsr{ÚsortedrÂÚ tracebackÚ format_listÚrstriprrYrÒr r) rIÚcontextrr rúZ log_linesrèÚvalueÚtbrrrÚdefault_exception_handlerAs6           z'BaseEventLoop.default_exception_handlercCsÙ|jdkrKy|j|ƒWqÕtk rGtjdddƒYqÕXnŠy|j||ƒWnstk rÔ}zSy&|jidd6|d6|d6ƒWn%tk rÁtjd ddƒYnXWYdd}~XnXdS) a¤Call the current event loop's exception handler. The context argument is a dict containing the following keys: - 'message': Error message; - 'exception' (optional): Exception object; - 'future' (optional): Future instance; - 'handle' (optional): Handle instance; - 'protocol' (optional): Protocol instance; - 'transport' (optional): Transport instance; - 'socket' (optional): Socket instance. New keys maybe introduced in the future. Note: do not overload this method in an event loop subclass. For custom exception handling, use the `set_exception_handler()` method. Nz&Exception in default exception handlerrúTz$Unhandled error in exception handlerrr rzeException in default exception handler while handling an unexpected error in custom exception handler)rirr?r r)rIrrCrrrÚcall_exception_handlerms"     z$BaseEventLoop.call_exception_handlercCsXt|tjƒstdƒ‚|jr+dSt|tjƒ sDt‚|jj|ƒdS)z3Add a Handle to _scheduled (TimerHandle) or _ready.zA Handle is required hereN)rrr¸rOrºr²rcrY)rIrrrrÚ _add_callback�s  zBaseEventLoop._add_callbackcCs|j|ƒ|jƒdS)z6Like _add_callback() but called from a signal handler.N)r!r–)rIrrrrÚ_add_callback_signalsafe¥s z&BaseEventLoop._add_callback_signalsafecCs|jr|jd7_ndS)z3Notification that a TimerHandle has been cancelled.rN)rdr^)rIrrrrÚ_timer_handle_cancelledªs z%BaseEventLoop._timer_handle_cancelledc Cs{t|jƒ}|tkrŒ|j|tkrŒg}x3|jD](}|jrYd|_q>|j|ƒq>Wtj|ƒ||_d|_nJxG|jrÕ|jdjrÕ|jd8_tj |jƒ}d|_q�Wd}|j sî|j r÷d}n5|jr,|jdj }t d||jƒƒ}n|jr|dkr|jƒ}|jj|ƒ}|jƒ|}|dkr‡tj} n tj} t|ƒ} |dkrÅtj| d|d| ƒq1| rïtj| d|d|d| ƒq1|dkr1tj| d |d|dƒq1n|jj|ƒ}|j|ƒ|jƒ|j} xX|jr«|jd}|j | kr}Pntj |jƒ}d|_|j j|ƒqTWt|j ƒ} x³t| ƒD]¥} |j jƒ}|jrìqÈn|jrcz^||_|jƒ}|jƒ|jƒ|}||jkrRtjd t|ƒ|ƒnWdd|_XqÈ|jƒqÈWd}dS) zÕRun one full iteration of the event loop. This calls all currently ready callbacks, polls for I/O, schedules the resulting callbacks, and finally schedules 'call_later' callbacks. FrrNgð?zpoll took %.3f ms: %s eventsg@�@z$poll %.3f ms took %.3f ms: %s eventsz"poll %.3f ms took %.3f ms: timeoutzExecuting %s took %.3f seconds) rÖrdÚ_MIN_SCHEDULED_TIMER_HANDLESr^Ú%_MIN_CANCELLED_TIMER_HANDLES_FRACTIONrºrYr³ÚheapifyÚheappoprcr`Z_whenÚmaxrgr›Z _selectorZselectÚloggingÚINFOÚDEBUGr Úlogr˜rhÚrangeÚpopleftrsZ_runrrrr)rIZ sched_countZ new_scheduledrZtimeoutrµrÆr—rÇÚlevelZneventZend_timeZntodoÚirrrrž¯sˆ                                zBaseEventLoop._run_oncec Csåytj}tj}Wntk r.dSYnXt|ƒ}|j|krNdStj}|ƒ}|r¥|d|fkr�tj d|t ƒqá||ƒd|_n<|d|fkrÎtj d|t ƒn|dƒd|_dS)Nz[loop.set_debug(True): cannot set debug coroutine wrapper; another wrapper is already set %rTzWloop.set_debug(False): cannot unset debug coroutine wrapper; another wrapper was set %rF) rkZset_coroutine_wrapperZget_coroutine_wrapperÚAttributeErrorrnrurZ debug_wrapperr§r¨ÚRuntimeWarning)rIÚenabledZ set_wrapperZ get_wrapperÚwrapperZcurrent_wrapperrrrršs.          z$BaseEventLoop._set_coroutine_wrappercCs|jS)N)r›)rIrrrrx3szBaseEventLoop.get_debugcCs)||_|jƒr%|j|ƒndS)N)r›rvrš)rIr3rrrrj6s  zBaseEventLoop.set_debug)@rMr[r\rKrNr|rr€r…r‰rŠrŒr�rr•r–r˜rzrŸr¢rArSrwrZPY34rªrvrgr®r«r·r¶r±r¹rÀrÁrÈrÃrÉrárØr÷rør%r*Z AI_PASSIVErrrr rrrrrrr r!r"r#ržršrxrjrrrrr Ós¤           $             !  y  w    ^    , 0    c ! )-Ú__doc__raZconcurrent.futuresr¼Ú functoolsr³rr.rþr)ror%rrœrgrrkr§r#rrrrrrr,r Ú__all__r½r$r%rrr&r)rrÚ lru_cacher8r=rDZAbstractServerrEZAbstractEventLoopr rrrrÚsN                  : /