o
    Óó·iøY  ã                   @  s\  d Z 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	 ddlm
Z
 ddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ ededef d�ZdadZG dd„ deƒZd-dd„ZdZG dd„ deƒZdd„ Z G dd„ dƒZ!G dd„ dƒZ"G dd„ dƒZ#g Z$d.d!d"„Z%G d#d$„ d$e&ƒZ'd%d&„ Z(d'Z)dddd(e'j*fd)d*„Z+d+d,„ Z,dS )/z9
Asynchronous-friendly error mechanism.

See L{Failure}.
é    )ÚannotationsN)Úpartial)Úgetmro)ÚStringIO)ÚCallableÚNoReturnÚTypeVar)ÚVersion)Úreflect)ÚdeprecatedPropertyÚ_T_Callable.)Úboundé   c                   @  s   e Zd ZdS )ÚDefaultExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú\C:\Users\psant\Downloads\PAF backend\school\venv\Lib\site-packages\twisted/python/failure.pyr   &   s    r   Údefaultc              	   C  sx  |dvrt d|f ƒ‚|}|dkr*| D ]\}}}}}||› d|› d|› d�ƒ qdS |dkrT| D ]!\}}}}}|d|› d|› d	|› d�ƒ |d
t ||¡ ¡  ƒ q0dS |dkrq| D ]\}}}}}|d|||f ƒ qZ|dƒ dS |dkr¸| D ]B\}}}}}|d|||f ƒ |dƒ |D ]\}	}
|d|	› dt|
ƒ› d�ƒ q�|dƒ |D ]\}	}
|d|	› dt|
ƒ› d�ƒ q¥qwdS dS )a˜  
    Format and write frames.

    @param frames: is a list of frames as used by Failure.frames, with
        each frame being a list of
        (funcName, fileName, lineNumber, locals.items(), globals.items())
    @type frames: list
    @param write: this will be called with formatted strings.
    @type write: callable
    @param detail: Four detail levels are available:
        default, brief, verbose, and verbose-vars-not-captured.
        C{Failure.printDetailedTraceback} uses the latter when the caller asks
        for verbose, but no vars were captured, so that an explicit warning
        about the missing data is shown.
    @type detail: string
    )r   ÚbriefÚverboseúverbose-vars-not-capturedzNDetail must be default, brief, verbose, or verbose-vars-not-captured. (not %r)r   ú:Ú
r   z  File "z", line z, in z    %s
r   z%s:%d: %s(...)
zA [Capture of Locals and Globals disabled (use captureVars=True)]
r   z [ Locals ]
z  z : z ( Globals )
N)Ú
ValueErrorÚ	linecacheÚgetlineÚstripÚrepr)ÚframesÚwriteÚdetailÚwÚmethodÚfilenameÚlinenoZ	localVarsZ
globalVarsÚnameÚvalr   r   r   Úformat_frames*   s@   ÿÿÿþÿør)   z--- <exception caught here> ---c                   @  s   e Zd ZdZdS )ÚNoCurrentExceptionErrorzŠ
    Raised when trying to create a Failure from the current interpreter
    exception state and there is no current exception state.
    N)r   r   r   Ú__doc__r   r   r   r   r*   \   s    r*   c                 C  sb   t | ƒdks
J dƒ‚d}t| d |ƒ}t|ƒ }}| dd… D ]}t||ƒ}t|ƒ|_|j}q|S )zæ
    Construct a fake traceback object using a list of frames.

    It should have the same API as stdlib to allow interaction with
    other tools.

    @param tbFrames: [(methodname, filename, lineno, locals, globals), ...]
    r   zMust pass some framesNé   )ÚlenÚ_FrameÚ_TracebackFrameÚtb_next)ZtbFramesÚstackZfirstTbÚtbZsfr   r   r   Ú
_Tracebackc   s   	

r3   c                   @  ó   e Zd ZdZdd„ ZdS )r/   zq
    Fake traceback object which can be passed to functions in the standard
    library L{traceback} module.
    c                 C  s    || _ |j| _|j| _d| _dS )z-
        @param frame: _Frame object
        N)Útb_frameÚf_linenoÚ	tb_linenoÚf_lastiÚtb_lastir0   )ÚselfÚframer   r   r   Ú__init__Š   s   
z_TracebackFrame.__init__N©r   r   r   r+   r<   r   r   r   r   r/   „   s    r/   c                   @  r4   )r.   aD  
    A fake frame object, used by L{_Traceback}.

    @ivar f_code: fake L{code<types.CodeType>} object
    @ivar f_lineno: line number
    @ivar f_globals: fake f_globals dictionary (usually empty)
    @ivar f_locals: fake f_locals dictionary (usually empty)
    @ivar f_back: previous stack frame (towards the caller)
    c                 C  s`   |\}}}}}t ||ƒ| _|| _t|pi ƒ| _t|pi ƒ| _|| _d| _tt	ƒ 
¡ | _d| _dS )z¢
        @param frameinfo: (methodname, filename, lineno, locals, globals)
        @param back: previous (older) stack frame
        @type back: C{frame}
        r   N)Ú_CodeÚf_coder6   ÚdictÚ	f_globalsÚf_localsÚf_backr8   ÚvarsÚbuiltinsÚcopyÚ
f_builtinsÚf_trace)r:   Ú	frameinfoÚbackr'   r%   r&   ÚlocalzÚglobalzr   r   r   r<   Ÿ   s   
z_Frame.__init__Nr=   r   r   r   r   r.   ”   s    
r.   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )r>   zÉ
    A fake code object, used by L{_Traceback} via L{_Frame}.

    It is intended to have the same API as the stdlib code type to allow
    interoperation with other tools based on that interface.
    c                 C  sd   || _ || _d| _d| _d| _g | _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _d| _d S )Nó    r   r   )Úco_nameÚco_filenameÚ	co_lnotabÚco_firstlinenoÚco_argcountÚco_varnamesÚco_codeÚco_cellvarsÚ	co_constsÚco_flagsÚco_freevarsÚco_posonlyargcountÚco_kwonlyargcountÚco_namesÚ
co_nlocalsÚco_stacksize)r:   r'   r%   r   r   r   r<   ¸   s    
z_Code.__init__c                 C  s   dS )N))NNNNr   ©r:   r   r   r   Úco_positionsÊ   s   z_Code.co_positionsN)r   r   r   r+   r<   r_   r   r   r   r   r>   °   s    r>   ÚfÚreturnc                 C  s   t  | j¡ | S )a  
    Mark the given callable as extraneous to inlineCallbacks exception
    reporting; don't show these functions.

    @param f: a function that you NEVER WANT TO SEE AGAIN in ANY TRACEBACK
        reported by Failure.

    @type f: function

    @return: f
    N)Ú_inlineCallbacksExtraneousÚappendÚ__code__)r`   r   r   r   Ú_extraneousÑ   s   re   c                   @  s4  e Zd ZdZdZdZdBdd„Zedd„ ƒZej	d	d„ ƒZe
ed
dddƒƒdd„ ƒZej	dd„ ƒZedd„ ƒZej	dd„ ƒZdd„ ZedCdd„ƒZdd„ Zdd„ ZdDdd „Zed!d"„ ƒZdEd$d%„ZdEd&d'„Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ ZdEd2d3„ZdEd4d5„ZdFdGd:d;„ZdHd<d=„Z dId>d?„Z!dId@dA„Z"dS )JÚFailureaÉ  
    A basic abstraction for an error that has occurred.

    This is necessary because Python's built-in error mechanisms are
    inconvenient for asynchronous communication.

    The C{frame} attribute contain the traceback frames.  Each frame is a tuple
    of (funcName, fileName, lineNumber, localsItems, globalsItems), where
    localsItems and globalsItems are the contents of
    C{locals().items()}/C{globals().items()} for that frame, or an empty tuple
    if those details were not captured.

    Local/global variables in C{frame} will only be captured if
    C{captureVars=True} when constructing the L{Failure}.

    @ivar value: The exception instance responsible for this failure.

    @ivar type: The exception's class.

    @ivar stack: Deprecated, always an empty list.  Equivalent information can
        be extracted from C{import traceback;
        traceback.extract_stack(your_failure.tb)}

    @ivar frames: list of frames, innermost first.
    r   NFc                 C  sØ   t d a t | _ d | _ | _}|| _|du r(t ¡ \| _| _}| jdu r'tƒ ‚n|du r?t|tƒr6|j	| _nt|ƒ| _|| _n|| _|| _t| jt
ƒrS|  | j¡ dS |du rg|r\|}nt| jddƒrg| jj}|| _dS )a-  
        Initialize me with an explanation of the error.

        By default, this will use the current C{exception}
        (L{sys.exc_info}()).  However, if you want to specify a
        particular kind of failure, you can pass an exception as an
        argument.

        If no C{exc_value} is passed, then an "original" C{Failure} will
        be searched for. If the current exception handler that this
        C{Failure} is being constructed in is handling an exception
        raised by L{raiseException}, then this C{Failure} will act like
        the original C{Failure}.

        For C{exc_tb} only L{traceback} instances or L{None} are allowed.
        If L{None} is supplied for C{exc_value}, the value of C{exc_tb} is
        ignored, otherwise if C{exc_tb} is L{None}, it will be found from
        execution context (ie, L{sys.exc_info}).

        @param captureVars: if set, capture locals and globals of stack
            frames.  This is pretty slow, and makes no difference unless you
            are going to use L{printDetailedTraceback}.
        r,   NÚ__traceback__)ÚcountÚtypeÚvalueÚcaptureVarsÚsysÚexc_infor*   Ú
isinstanceÚ	ExceptionÚ	__class__rf   Ú_extrapolateÚgetattrrg   r2   )r:   Ú	exc_valueÚexc_typeÚexc_tbrk   r2   r   r   r   r<   ÿ   s2   
ÿ



zFailure.__init__c                 C  sÈ   t | dƒr| jS g  }| _| j}|d urb|j}| jrH|j ¡ }|j|ju r(i }n|j ¡ }||fD ]	}d|v r:|d= q1t| 	¡ ƒ}t| 	¡ ƒ}nd }}| 
|jj|jj|j||f¡ |j}|d us|S )NÚ_framesÚ__builtins__r   )Úhasattrrv   r2   r5   rk   rB   rF   rA   ÚlistÚitemsrc   r?   rN   rO   r7   r0   )r:   r    r2   r`   rK   rL   Údr   r   r   r    8  s:   



€ûÿ	èzFailure.framesc                 C  ó
   || _ d S ©N)rv   )r:   r    r   r   r   r    [  ó   
ZTwistedé   é
   c                 C  s   g S r}   r   r^   r   r   r   r1   _  s   zFailure.stackc                 C  s   ~d S r}   r   )r:   r1   r   r   r   r1   c  s   c                 C  sZ   | j d ur| j S t | j¡r%t| jtƒr%t| jƒ}ttt	j
|ƒƒ| _ | j S | jg| _ | j S r}   )Ú_parentsÚinspectÚisclassri   Ú
issubclassro   r   ry   Úmapr
   Úqual)r:   ZparentCsr   r   r   Úparentsg  s   


ÿzFailure.parentsc                 C  r|   r}   )r�   )r:   r‡   r   r   r   r‡   s  r~   c                 C  s   t   |j¡| _dS )a,  
        Extrapolate from one failure into another, copying its stack frames.

        @param otherFailure: Another L{Failure}, whose traceback information,
            if any, should be preserved as part of the stack presented by this
            one.
        @type otherFailure: L{Failure}
        N)rF   Ú__dict__)r:   ZotherFailurer   r   r   rq   w  s   
zFailure._extrapolaterj   ÚBaseExceptionra   c                 C  s6   t  t ¡}td7 ad|_t|_| |_| j|_d|_|S )z¨
        Create a L{Failure} for an exception without a traceback.

        By restricting the inputs significantly, this constructor runs much
        faster.
        r,   FN)rf   Ú__new__rh   rk   rj   rp   ri   r2   )rj   Úresultr   r   r   Ú_withoutTracebackƒ  s   
zFailure._withoutTracebackc                 G  s   | j |Ž }|s|  ¡  |S )a€  
        Trap this failure if its type is in a predetermined list.

        This allows you to trap a Failure in an error callback.  It will be
        automatically re-raised if it is not a type that you expect.

        The reason for having this particular API is because it's very useful
        in Deferred errback chains::

            def _ebFoo(self, failure):
                r = failure.trap(Spam, Eggs)
                print('The Failure is due to either Spam or Eggs!')
                if r == Spam:
                    print('Spam did it!')
                elif r == Eggs:
                    print('Eggs did it!')

        If the failure is not a Spam or an Eggs, then the Failure will be
        'passed on' to the next errback. In Python 2 the Failure will be
        raised; in Python 3 the underlying exception will be re-raised.

        @type errorTypes: L{Exception}
        N)ÚcheckÚraiseException)r:   Ú
errorTypesÚerrorr   r   r   Útrap•  s   
zFailure.trapc                 G  sB   |D ]}|}t  |¡rt|tƒrt |¡}|| jv r|  S qdS )a  
        Check if this failure's type is in a predetermined list.

        @type errorTypes: list of L{Exception} classes or
                          fully-qualified class names.
        @returns: the matching L{Exception} type, or None if no match.
        N)r‚   rƒ   r„   ro   r
   r†   r‡   )r:   r�   r�   Úerrr   r   r   r�   ²  s   

ÿzFailure.checkr   c                 C  s   | j  | j¡‚)zf
        raise the original exception, preserving traceback
        information if available.
        N)rj   Úwith_tracebackr2   r^   r   r   r   rŽ   Â  s   zFailure.raiseExceptionc                 C  s   |  | j | j¡¡S )aJ  
        Throw the original exception into the given generator,
        preserving traceback information if available.

        @return: The next value yielded from the generator.
        @raise StopIteration: If there are no more values in the generator.
        @raise anything else: Anything that the generator raises.
        N)Úthrowrj   r“   r2   )r:   Úgr   r   r   ÚthrowExceptionIntoGeneratorÉ  s   
z#Failure.throwExceptionIntoGeneratorÚstrc                 C  s"   d  t | j¡t | j¡|  ¡ ¡S )Nz<{} {}: {}>)Úformatr
   r†   rp   ri   ÚgetErrorMessager^   r   r   r   Ú__repr__Õ  s
   

ýzFailure.__repr__c                 C  s   d|   ¡  S )Nz[Failure instance: %s])ÚgetBriefTracebackr^   r   r   r   Ú__str__Ü  s   zFailure.__str__c                 C  s>   d|v r	|  d¡ |  d¡|d< |  d¡|d< | j |¡ d S )Nr1   r‡   r�   r    rv   )Úpoprˆ   Úupdate©r:   Ústater   r   r   Ú__setstate__ß  s
   
zFailure.__setstate__c                 C  sr   | j }| j ¡ }| d¡|d< g |d< d|v r| d¡ | jr*dd„ | jD ƒ|d< n| j|d< d|d	< d
|d< |S )zë
        Avoid pickling objects in the traceback.

        This is not called direclty by pickle, since C{BaseException}
        implements reduce; instead, pickle calls C{Failure.__reduce__} which
        then calls this API.
        r�   r‡   r1   rv   c              	   S  s6   g | ]}|d  |d |d t |d ƒt |d ƒg‘qS )r   r,   é   é   r   )Ú_safeReprVars)Ú.0Úvr   r   r   Ú
<listcomp>û  s    ú

ûÿz(Failure.__getstate__.<locals>.<listcomp>r    Nr2   r,   Úpickled)r‡   rˆ   rF   r�   rk   r    )r:   Ú_Úcr   r   r   Ú__getstate__æ  s   	

ø
zFailure.__getstate__c                 C  s   t tjtƒd|  ¡ fS )Nr   )r   rf   rŠ   r«   r^   r   r   r   Ú
__reduce__  s   zFailure.__reduce__c                 C  s:   |   ¡ }| d¡|d< || _t| jddƒrd| j_dS dS )zÈ
        Remove references to other objects, replacing them with strings.

        On Python 3, this will also set the C{__traceback__} attribute of the
        exception instance to L{None}.
        r    rv   rg   N)r«   r�   rˆ   rr   rj   rg   rŸ   r   r   r   ÚcleanFailure  s   þzFailure.cleanFailurec                 C  s,   | j dur| j S t| jƒdkrt| jƒS dS )a‘  
        Get an object that represents this Failure's stack that can be passed
        to traceback.extract_tb.

        If the original traceback object is still present, return that. If this
        traceback object has been lost but we still have the information,
        return a fake traceback object (see L{_Traceback}). If there is no
        traceback information at all, return None.
        Nr   )r2   r-   r    r3   r^   r   r   r   ÚgetTracebackObject!  s
   


zFailure.getTracebackObjectc                 C  s"   t | jtƒr| j ¡ S t | j¡S )zJ
        Get a string of the exception which caused this Failure.
        N)rn   rj   rf   r™   r
   Úsafe_strr^   r   r   r   r™   2  s   
zFailure.getErrorMessagec                 C  s   t ƒ }| j|d� | ¡ S )N)Úfile)r   ÚprintBriefTracebackÚgetvalue)r:   Úior   r   r   r›   :  s   zFailure.getBriefTracebackr   ÚelideFrameworkCodeÚintr"   c                 C  s   t ƒ }| j|||d� | ¡ S )N)r°   r´   r"   )r   ÚprintTracebackr²   )r:   r´   r"   r³   r   r   r   ÚgetTraceback?  s
   ÿzFailure.getTracebackc                 C  s8  |du rddl m} |j}|j}|dkr| jsd}n|}|dkr/|d| j| jr)dp*df ƒ n"|d	krM| jr9d
}nd}|d|t 	| j
¡t 	| j¡f ƒ n|dƒ | jr\t| j||ƒ n|d	ksd|dƒ |d	ksz|t | j
¡› dt 	| j¡› d�ƒ t| jtƒr�| d¡ | j |||¡ |dkrš|d| j ƒ dS dS )a”  
        Emulate Python's standard error reporting mechanism.

        @param file: If specified, a file-like object to which to write the
            traceback.

        @param elideFrameworkCode: Deprecated, ignored.

        @param detail: A string indicating how much information to include
            in the traceback.  Must be one of C{'brief'}, C{'default'}, or
            C{'verbose'}.
        Nr   )Úlogr   r   z*--- Failure #%d%s---
z (pickled) ú r   Ú	Tracebackz"Traceback (failure with no frames)z%s: %s: %s
z#Traceback (most recent call last):
z	Failure: z: r   z (chained Failure)
z*--- End of Failure #%d ---
)Útwisted.pythonr¸   Zlogerrr!   rk   rh   r¨   r    r
   r¯   ri   rj   r)   r†   rn   rf   r¶   )r:   r°   r´   r"   r¸   r#   ZformatDetailZ	hasFramesr   r   r   r¶   F  sF   ÿÿÿÿ$
ÿzFailure.printTracebackc                 C  ó   | j ||dd� dS )z;
        Print a traceback as densely as possible.
        r   ©r"   N©r¶   ©r:   r°   r´   r   r   r   r±   ‡  ó   zFailure.printBriefTracebackc                 C  r¼   )zQ
        Print a traceback with detailed locals and globals information.
        r   r½   Nr¾   r¿   r   r   r   ÚprintDetailedTraceback�  rÀ   zFailure.printDetailedTraceback)NNNF)rj   r‰   ra   rf   )ra   r   )ra   r—   )r   r   )r´   rµ   r"   r—   ra   r—   )NFr   )Nr   )#r   r   r   r+   r¨   r�   r<   Úpropertyr    Úsetterr   r	   r1   r‡   rq   ÚstaticmethodrŒ   r‘   r�   rŽ   re   r–   rš   rœ   r¡   r«   r¬   r­   r®   r™   r›   r·   r¶   r±   rÁ   r   r   r   r   rf   á   sL    
9
"








(



Arf   c                 C  s   dd„ | D ƒS )aq  
    Convert a list of (name, object) pairs into (name, repr) pairs.

    L{twisted.python.reflect.safe_repr} is used to generate the repr, so no
    exceptions will be raised by faulty C{__repr__} methods.

    @param varsDictItems: a sequence of (name, value) pairs as returned by e.g.
        C{locals().items()}.
    @returns: a sequence of (name, repr) pairs.
    c                 S  s   g | ]\}}|t  |¡f‘qS r   )r
   Z	safe_repr)r¥   r'   Úobjr   r   r   r§   Ÿ  s    z!_safeReprVars.<locals>.<listcomp>Nr   )ZvarsDictItemsr   r   r   r¤   ”  s   r¤   TFc           	      C  sˆ   |||fdkr:t  ¡ }|d | jks:tr:zt|d ƒ}W n ty'   d}Y nw td |¡ƒ ddl}| 	|d ¡ || ||||ƒ dS )z;
    Initialize failure object, possibly spawning pdb.
    )NNNr   r,   z
broken strz8Jumping into debugger for post-mortem of exception '{}':Nr¢   )
rl   rm   rp   ÚDO_POST_MORTEMr—   r‰   Úprintr˜   ÚpdbZpost_mortem)	r:   rs   rt   ru   rk   ZFailure__init__ÚexcZstrreprrÈ   r   r   r   Ú
_debuginit§  s    ÿÿÿrÊ   c                   C  s
   t t_dS )z*
    Enable debug hooks for Failures.
    N)rÊ   rf   r<   r   r   r   r   ÚstartDebugModeÄ  s   
rË   )r   )r`   r   ra   r   )-r+   Ú
__future__r   rE   rF   r‚   r   rl   Ú	functoolsr   r   r³   r   Útypingr   r   r   Úincrementalr	   r»   r
   Ztwisted.python.deprecater   Úobjectr   rh   ZtraceupLengthro   r   r)   ZEXCEPTION_CAUGHT_HEREr*   r3   r/   r.   r>   rb   re   r‰   rf   r¤   rÆ   r<   rÊ   rË   r   r   r   r   Ú<module>   sP   
/!
   6
ú