o
    b^                     @   s   d dl mZ d dlZd dlZd dlZd dlZd dlmZmZ ddl	m
Z
 dd Zda	 ed	d
 Ze
jZeejeju rCeejZndZdd Zedd Zdd Zg adaedd ZdS )    )print_functionN)closingcontextmanager   )_gic                 C   sl   t | dr| d dS zddl}W n ty   tdw |  }|||j}|||j||jB  dS )zEnsures that the socket is not inherited by child processes

    Raises:
        EnvironmentError
        NotImplementedError: With Python <3.4 on Windows
    set_inheritableFr   Nz+Not implemented for older Python on Windows)	hasattrr   fcntlImportErrorNotImplementedErrorfilenoF_GETFDF_SETFD
FD_CLOEXEC)sockr	   fdflags r   1/usr/lib/python3/dist-packages/gi/_ossighelper.pyensure_socket_not_inheritable   s   
r   Fc                  #   s   t rdV  dS ddlm  t \} t t|  | fD ]}|d t| q"z	t	| 
 }W n tyP   dV  Y W d   W d   dS w da  fdd}zRtjdkri j
 }n j
 } | j jj jjB  jjB  jjB |}z
dV  W  | n | w W t	|}|| 
 krt	| da nt	|}|| 
 krt	| da w W d   n1 sw   Y  W d   dS W d   dS 1 sw   Y  dS )	a  A decorator for functions which create a glib event loop to keep
    Python signal handlers working while the event loop is idling.

    In case an OS signal is received will wake the default event loop up
    shortly so that any registered Python signal handlers registered through
    signal.signal() can run.

    In case the wrapped function is not called from the main thread it will be
    called as is and it will not wake up the default loop for signals.
    Nr   )GLibFTc              
      sN   | j @ r%ztdW S  ty$ } zt| W Y d }~dS d }~ww dS )Nr   F)IO_INboolrecvEnvironmentErrorprint)source	conditioner   read_socketr   r   signal_notify\   s   
z'wakeup_on_signal.<locals>.signal_notifynt)_wakeup_fd_is_activegi.repositoryr   socket
socketpairr   setblockingr   signalset_wakeup_fdr   
ValueErrorosname	IOChannelwin32_new_socketunix_newio_add_watchPRIORITY_DEFAULTIOConditionINHUPNVALERRsource_remove)write_socketr   orig_fdr!   channel	source_idwrite_fdr   r   r   wakeup_on_signal7   s^   






Pr=   c                   C   s    t t jt ju ott jtkS )z?Returns if on SIGINT the default Python handler would be called)r(   	getsignalSIGINTdefault_int_handlerPyOS_getsigstartup_sigint_ptrr   r   r   r   sigint_handler_is_default   s   rD   c              
   c   s    t  sJ ttj|  ttj}z!dV  W ttj| u r0ttj|kr2ttjtj dS dS dS ttj| u rLttj|krMttjtj w w w )zContext manager for saving/restoring the SIGINT handler default state.

    Will only restore the default handler again if the handler is not changed
    while the context is active.
    N)rD   r(   r@   rB   r?   rA   )handlersig_ptrr   r   r   &sigint_handler_set_and_restore_default   s   

rG   c                   C   s   t  jdkS )z@Returns True in case the function is called from the main thread
MainThread)	threadingcurrent_threadr,   r   r   r   r   is_main_thread   s   rK   c              
   c   s    t  s	dV  dS t s3tr.t|  zdV  W t }tr!|  dS t }tr-|  w w dV  dS dadd }t|  z)t| dV  W d   n1 sQw   Y  W trbttj	d dS t  dS trrttj	d w t  w )a  Installs a SIGINT signal handler in case the default Python one is
    active which calls 'callback' in case the signal occurs.

    Only does something if called from the main thread.

    In case of nested context managers the signal handler will be only
    installed once and the callbacks will be called in the reverse order
    of their registration.

    The old signal handler will be restored in case no signal handler is
    registered while the context is active.
    NFc                 S   s   t rd S da t   d S )NT)_sigint_called_callback_stackpop)sig_numframer   r   r   sigint_handler   s   z0register_sigint_fallback.<locals>.sigint_handler)
rK   rD   rM   appendrN   rL   rG   r(   rA   r@   )callbackcbrQ   r   r   r   register_sigint_fallback   sB   



rU   )
__future__r   r+   r%   r(   rI   
contextlibr   r    r   r   r#   r=   pyos_getsigrB   r?   r@   rA   rC   rD   rG   rK   rM   rL   rU   r   r   r   r   <module>   s0   
I
