U
    	^                     @   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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kre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   sp   t | dr| d nVzddl}W n tk
r>   tdY n.X |  }|||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filenoZF_GETFDZF_SETFDZ
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jdkp4tjdd dk} ttd}|rH| sRdV  dS t	 \}t
4 t
| |fD ]}|d t| q~zt| }W n0 tk
r   dV  Y W 5 Q R  W 5 Q R  dS X d	a  fd
d}zxtjdkr j }n j } | j jj jjB  jjB  jjB |}z
dV  W 5  | X W 5 t|}|| krt| da X W 5 Q R X W 5 Q R X 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.

    Works on Windows but needs Python 3.5+.

    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   )GLibnt   )      
socketpairFTc              
      sX   | j @ rPztdW S  tk
rJ } zt| W Y dS d }~X Y nX dS dS d S )Nr   FT)ZIO_INboolZrecvEnvironmentErrorprint)sourceZ	conditioner   Zread_socketr   r   signal_notifyj   s    
z'wakeup_on_signal.<locals>.signal_notify)_wakeup_fd_is_activeZgi.repositoryr   osnamesysversion_infor   socketr   r   Zsetblockingr   signalset_wakeup_fdr   
ValueErrorZ	IOChannelZwin32_new_socketZunix_newZio_add_watchZPRIORITY_DEFAULTZIOConditionINZHUPZNVALZERRZsource_remove)	Zset_wakeup_fd_supports_socketZhas_socketpairZwrite_socketr   Zorig_fdr   Zwrite_fdZchannelZ	source_idr   r   r   wakeup_on_signal9   sZ    





r*   c                   C   s    t t jt jk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    r1   c              
   c   sb   t  s
tttj|  ttj}z
dV  W 5 ttj| kr\ttj|kr\ttjtj X dS )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)r1   AssertionErrorr&   r-   r/   r,   r.   )ZhandlerZsig_ptrr   r   r   &sigint_handler_set_and_restore_default   s    


r3   c                   C   s   t  jdkS )z@Returns True in case the function is called from the main threadZ
MainThread)	threadingZcurrent_threadr"   r   r   r   r   is_main_thread   s    r5   c              
   c   s   t  sdV  dS t sPtrFt|  z
dV  W 5 t }trB|  X ndV  dS dadd }t|  zt	| dV  W 5 Q R X W 5 trttjd nt  X dS )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)Zsig_numframer   r   r   sigint_handler   s    z0register_sigint_fallback.<locals>.sigint_handler)
r5   r1   r7   appendr8   r6   r&   r.   r-   r3   )callbackcbr:   r   r   r   register_sigint_fallback   s,    




r>   )Z
__future__r   r!   r#   r%   r&   r4   
contextlibr   r    r   r   r    r*   Zpyos_getsigr/   r,   r-   r.   r0   r1   r3   r5   r7   r6   r>   r   r   r   r   <module>   s.   
U
