o
    f                     @   s   d dl Z d dlZd dlZd dlZd dlZddddddZdd ZG d	d
 d
eZe j	dd Z
e j	dedfddZe j	dd ZdddZe j	dd Zdd ZdS )    N    linenooffsetc                C   s  ddl m} tjdd}tdt t|dd W d    n1 s#w   Y  | t|d| |\}| 	t
|jt|j |rJ| t|j| | |jd | |j |d urb| |j| tjdd}td	t || ||||d
 W d    n1 sw   Y  | |g  d S )Nr   )check_syntax_errorTrecordalwaysz
<testcase>execr   errorr   )test.supportr   warningscatch_warningssimplefilterSyntaxWarningcompileassertEquallen
assertTrue
issubclasscategoryassertRegexstrmessagefilenameassertIsNotNoner   )testcase	statementerrtextr   r   r   warnswarn r"   3/usr/lib/python3.10/test/support/warnings_helper.pycheck_syntax_warning   s.   r$   c                    s    fdd}|S )zDecorator to suppress warnings.

    Use of context managers to hide warnings make diffs
    more noisy and tools like 'git blame' less useful.
    c                    s   t   fdd}|S )Nc                    sP   t   t jd d | g|R i |W  d    S 1 s!w   Y  d S )Nignorer   )r   r   r   )selfargskwargs)r   testr"   r#   wrapper-   s   
$z3ignore_warnings.<locals>.decorator.<locals>.wrapper)	functoolswraps)r*   r+   r&   )r*   r#   	decorator,   s   z"ignore_warnings.<locals>.decoratorr"   )r   r.   r"   r&   r#   ignore_warnings&   s   r/   c                   @   s4   e Zd ZdZdd Zdd Zedd Zdd	 Zd
S )WarningsRecorderzyConvenience wrapper for the warnings list returned on
       entry to the warnings.catch_warnings() context manager.
    c                 C   s   || _ d| _d S )Nr   	_warnings_last)r'   warnings_listr"   r"   r#   __init__:   s   
zWarningsRecorder.__init__c                 C   s@   t | j| jkrt| jd |S |tjjv rd S td| |f )Nz%r has no attribute %r)r   r2   r3   getattrr   WarningMessage_WARNING_DETAILSAttributeError)r'   attrr"   r"   r#   __getattr__>   s
   zWarningsRecorder.__getattr__c                 C   s   | j | jd  S Nr1   r'   r"   r"   r#   r   E   s   zWarningsRecorder.warningsc                 C   s   t | j| _d S r=   )r   r2   r3   r>   r"   r"   r#   resetI   s   zWarningsRecorder.resetN)	__name__
__module____qualname____doc__r5   r<   propertyr   r?   r"   r"   r"   r#   r0   6   s    
r0   c                  O   s.   | d}| sdtff} |du rd}t| |S )a  Context manager to silence warnings.

    Accept 2-tuples as positional arguments:
        ("message regexp", WarningCategory)

    Optional argument:
     - if 'quiet' is True, it does not fail if a filter catches nothing
        (default True without argument,
         default False if some filters are defined)

    Without argument, it defaults to:
        check_warnings(("", Warning), quiet=True)
    quietr   NT)getWarning_filterwarnings)filtersr)   rE   r"   r"   r#   check_warningsM   s   


rJ   Fc                 c   sj    ddl m} tjdd}tjd||d dV  |r|  W d   n1 s(w   Y  | |g  dS )a  Context manager to check that no warnings are emitted.

    This context manager enables a given warning within its scope
    and checks that no warnings are emitted even with that warning
    enabled.

    If force_gc is True, a garbage collection is attempted before checking
    for warnings. This may help to catch warnings emitted when objects
    are deleted, such as ResourceWarning.

    Other keyword arguments are passed to warnings.filterwarnings().
    r   )
gc_collectTr   r
   )r   r   N)r   rK   r   r   filterwarningsr   )r   r   r   force_gcrK   r    r"   r"   r#   check_no_warningse   s   rN   c                 c   s<    t | tdd dV  W d   dS 1 sw   Y  dS )a"  Context manager to check that no ResourceWarning is emitted.

    Usage:

        with check_no_resource_warning(self):
            f = open(...)
            ...
            del f

    You must remove the object which may emit ResourceWarning before
    the end of the context manager.
    T)r   rM   N)rN   ResourceWarning)r   r"   r"   r#   check_no_resource_warning~   s   "rP   c                 c   s   t d}|jd}|r|  tjdd}t jd d t	|V  W d   n1 s0w   Y  t
|}g }| D ]5\}}d}	|dd D ]}|j}
t|t|
tjret|
j|red}	|| qI|	sr|sr|||jf q=|r}td	|d
  |rtd|d
  dS )zCatch the warnings, then check if all the expected
    warnings have been raised and re-raise unexpected warnings.
    If 'quiet' is True, only re-raise the unexpected warnings.
       __warningregistry__Tr   r   r
   NFzunhandled warning %sr   z)filter (%r, %s) did not catch any warning)sys	_getframe	f_globalsrF   clearr   r   modulesr   r0   listr   rematchr   Ir   	__class__removeappendr@   AssertionError)rI   rE   frameregistrywreraisemissingmsgcatseenwarningr"   r"   r#   rH      s>   


rH   c               
   c   s<    t jd d  } zd V  W | t jd d < d S | t jd d < w r=   )r   rI   )old_filtersr"   r"   r#   save_restore_warnings_filters   s
   "rj   c                   C   s   t jdtdd d S )NzThis is used in test_support test to ensure support.ignore_deprecations_from() works as expected. You should not be seeing this.r   )
stacklevel)r   r!   DeprecationWarningr"   r"   r"   r#   _warn_about_deprecation   s
   
rm   )r   )F)
contextlibr,   rY   rS   r   r$   r/   objectr0   contextmanagerrJ   rG   rN   rP   rH   rj   rm   r"   r"   r"   r#   <module>   s(    


'
