
    Vi#                     ,   S SK r S SKrS SKrS SKJr  S/r " S S\5      rSS jr\" SS5      r	 " S S	\	5      r
 " S
 S\5      rSS jrSS jr " S S\\5      rS rSS jrS r\S:X  a	   \" 5         gg! \ a    S SKr\R,                  " 5         e f = f)    N)
namedtupleExceptionCauseMixinc                   ^   ^  \ rS rSrSrSrU 4S jrS rS rU 4S jr	SS jr
U 4S	 jrS
rU =r$ )r   0   a;  
A mixin class for wrapping an exception in another exception, or
otherwise indicating an exception was caused by another exception.

This is most useful in concurrent or failure-intolerant scenarios,
where just because one operation failed, doesn't mean the remainder
should be aborted, or that it's the appropriate time to raise
exceptions.

This is still a work in progress, but an example use case at the
bottom of this module.

NOTE: when inheriting, you will probably want to put the
ExceptionCauseMixin first. Builtin exceptions are not good about
calling super()
Nc                   > S nU(       a!  [        US   [        5      (       a	  US   USS  p[        [        U ]  " U /UQ70 UD6nX4l        Uc  U$ [        USS 5      nUc  X4l        OXTl        [        USS 5      nUbF  [        U5      Ul	        [        UR                  5      Ul
        [        UR                  5      Ul        U$  [        R                  " 5       u  pxn	Uc  Uc  UA	$ X8L d  XXL aK  [        U	5      Ul
        [        U	R                   5      Ul        UR                  S S UR                  -   Ul	        A	U$ ! A	f = f)Nr      
root_cause
full_trace)
isinstance	Exceptionsuperr   __new__causegetattrr	   listr
   _tb_stacksysexc_info_extract_from_tb_extract_from_frametb_frame)clsargskwr   retr	   r
   exc_type	exc_valueexc_tb	__class__s             c/home/james-whalen/.local/share/pipx/venvs/semgrep/lib/python3.13/site-packages/boltons/excutils.pyr   ExceptionCauseMixin.__new__D   s5   JtAw	22q'484'5cGDGBG	=JUL$7
"N'NUL$7
!!*-CN599oCGell+CJJ	*-,,.'HI$5  !Z%< +620A
!$CR377!:
 s   D: $AD: :D=c                     / nU R                  5       nU(       a  UR                  SU/5        UR                  U R                  5       5        SR	                  U5      $ )z
Get formatted the formatted traceback and exception
message. This function exists separately from __str__()
because __str__() is somewhat specialized for the built-in
traceback module's particular usage.
z#Traceback (most recent call last):
 )_get_trace_strextendappend_get_exc_strjoin)selfr   	trace_strs      r"   get_strExceptionCauseMixin.get_strh   sO     '')	JJ>	JK

4$$&'wws|    c                 v    [        U S/ 5      nU R                  (       a  USS  nU(       a  US   (       a  US   $ g)Nr   r   r   r%   )r   r   )r+   r   s     r"   _get_message ExceptionCauseMixin._get_messagev   s7    tVR(::8DDG7Nr/   c                    > U R                   (       d  [        [        U ]  5       $ U R                  (       a/  SR                  [        R                  " U R                  5      5      $ g)Nr%   )r   r   r   __repr__r
   r*   	tracebackformat_list)r+   r!   s    r"   r&   "ExceptionCauseMixin._get_trace_str~   sC    zz,d<>>??77900ABBr/   c                    [        U R                  5      nU R                  5       n/ nU(       a  U R                  R                  S/nU(       a  UR                  USUS/5        OUR                  SU/5        SR                  U5      $ )Nz: z (caused by )z caused by r%   )_format_excr	   r1   r!   __name__r'   r*   )r+   	incl_name	cause_strmessager   s        r"   r)    ExceptionCauseMixin._get_exc_str   sq    0	##%>>**D1CJJC@AJJy12wws|r/   c                 R  > U R                   (       d  [        [        U ]  5       $ U R	                  5       n/ nU(       a\  U R                  5       nU(       a  UR                  US/5        UR                  SUU R                  SS9/5        SR                  U5      $ U R                  SS9$ )Nz --- z+Wrapped traceback (most recent call last):
T)r<   r%   F)	r   r   r   __str__r&   r1   r'   r)   r*   )r+   r,   r   r>   r!   s       r"   rA   ExceptionCauseMixin.__str__   s    zz,d;=='')	'')G

GW-.JJF!))D)9; < 773<$$u$55r/    )T)r;   
__module____qualname____firstlineno____doc__r   r   r-   r1   r&   r)   rA   __static_attributes____classcell__r!   s   @r"   r   r   0   s3    " E"H
6 6r/   c                     Uc  U n[         R                  " U R                  R                  U5      nUR	                  5       $ N)r5   _format_final_exc_liner!   r;   rstrip)excr>   exc_strs      r"   r:   r:      s5    ..s}}/E/EwOG>>r/   _BaseTBItemzfilename, lineno, name, linec                   (   ^  \ rS rSrU 4S jrSrU =r$ )_TBItem   c                 P   > [         [        U ]  5       nUSU R                  -  -  nU$ )Nz <%r>)r   rS   r4   frame_id)r+   r   r!   s     r"   r4   _TBItem.__repr__   s*    GT+-w&&
r/   rC   )r;   rD   rE   rF   r4   rH   rI   rJ   s   @r"   rS   rS      s     r/   rS   c                   B    \ rS rSrSS jrS rS rS rS rS r	S	 r
S
rg)_DeferredLine   Nc                     Xl         X l        U=(       d    0 n[        UR                  5        VVs/ s H  u  pEUS;   d  M  XE4PM     snn5      U l        g s  snnf )N)r;   
__loader__)filenamelinenodictitemsmodule_globals)r+   r]   r^   ra   kvs         r"   __init___DeferredLine.__init__   s^     '-2"~7K7K7M $H7Mtq'(,F'F %+QF7M $H I $Hs   A
 A
c                 d    U R                   U R                  4UR                   UR                  4:H  $ rL   r^   r]   r+   others     r"   __eq___DeferredLine.__eq__   '    T]]+enn/MMMr/   c                 d    U R                   U R                  4UR                   UR                  4:g  $ rL   rg   rh   s     r"   __ne___DeferredLine.__ne__   rl   r/   c                 ,   [        U S5      (       a  U R                  $ [        R                  " U R                  5        [        R
                  " U R                  U R                  U R                  5      nU(       a  UR                  5       nOS nXl        U$ )N_line)	hasattrrq   	linecache
checkcacher]   getliner^   ra   strip)r+   lines     r"   rA   _DeferredLine.__str__   sp    4!!::T]]+  !%!%!4!46 ::<DD
r/   c                 *    [        [        U 5      5      $ rL   )reprstrr+   s    r"   r4   _DeferredLine.__repr__   s    CIr/   c                 *    [        [        U 5      5      $ rL   )lenr{   r|   s    r"   __len___DeferredLine.__len__   s    3t9~r/   c                 4    [        U 5      R                  5       $ rL   )r{   rv   r|   s    r"   rv   _DeferredLine.strip   s    4y  r/   )rq   r]   r^   ra   rL   )r;   rD   rE   rF   rd   rj   rn   rA   r4   r   rv   rH   rC   r/   r"   rY   rY      s(    INN!r/   rY   c                    / nU c  [         R                  " S5      n Uc  [        [         SS5      nSnU b  X1:  a  U R                  R                  nU R
                  nU R                  R                  n[        XEU R                  5      n[        XEXg5      n[        U 5      Ul        UR                  U5        U R                  n US-  nU b  X1:  a  M  UR                  5         U$ )Nr   tracebacklimit  r   )r   	_getframer   f_codeco_filenamef_linenoco_namerY   	f_globalsrS   idrV   r(   f_backreverse)	flimitr   nr]   r^   namerw   items	            r"   r   r      s    
CyMM!}-t4	A
-AI88''xxXq{{;x41

4HH	Q -AI KKMJr/   c                    / nUc  [        [        SS5      nSnU b  X1:  a  U R                  R                  R                  nU R
                  nU R                  R                  R                  n[        XEU R                  R                  5      n[        XEXg5      n[        U R                  5      Ul        UR                  U5        U R                  n US-  nU b  X1:  a  M  U$ )Nr   r   r   r   )r   r   r   r   r   	tb_linenor   rY   r   rS   r   rV   r(   tb_next)	tbr   r   r   r]   r^   r   rw   r   s	            r"   r   r      s    
C}-t4	A
.QY;;%%11{{!!))Xr{{/D/DEx42;;

4ZZ	Q .QY Jr/   c                       \ rS rSrSrg)	MathErrori  rC   N)r;   rD   rE   rF   rH   rC   r/   r"   r   r     s    r/   r   c                      SS-  $ )Nr   r   rC   rC   r/   r"   whoops_mathr     s    Q3Jr/   c                 |    U S:  a  [        U S-   S9$  [        5       $ ! [         a  n[        US5      nUeS nAff = f)N   r   )r   zya done messed up)math_lolr   ZeroDivisionErrorr   )r   zderO   s      r"   r   r     sF    1u!A#} 01	s   	 
;6;c                  Z     [        5         g ! [         a  n [        U S5      nUeS n A ff = f)Nhi)r   
ValueErrorr   )merO   s     r"   mainr     s-    
 D!	s   
 
*%*__main__rL   )NN)r   )r   r5   rs   collectionsr   __all__r   r   r:   rQ   rS   objectrY   r   r   r   r   r   r   r   r;   pdbpost_mortemrC   r/   r"   <module>r      s   D    " !
!o6) o6d (FGk #!F #!L**	#Z 	 z   3??$s   -A6 6B