�
*�KgH����ddlZddlZddlZddlZddlZddlZddlZddlmZd�Z d�Z
d�Zejdd���Z
dd�Zejdd���ZGd �d
��Zdefd�Ze��Zd
d�d�ZdS)�N)�supportc�d�tj��tj���fS�N)�_thread�_count� threading� _dangling�copy���4/usr/lib/python3.11/test/support/threading_helper.py�threading_setuprs$���>���Y�0�5�5�7�7�7�7rc��d}t|��D]�}tj��tjf}||krdS|srdt
_|d}tjd|d|dz
�d|d�dt|���d���|D]}tjd |�����d}d}tj
d
��tj����dS)N�dT�z&threading_cleanup() failed to cleanup rz threads (count: z, dangling: �)zDangling thread: �{�G�z�?)�rangerrrr r�environment_altered�
print_warning�len�time�sleep�
gc_collect)�original_values�
_MAX_COUNT�count�values�dangling_threads�threads r
�threading_cleanupr!s6���J��z�"�"������!�!�9�#6�6���_�$�$��E�E�� $�*.�G�'�%�a�y���!�#H�%+�A�Y���1C�%C�#H�#H�-3�A�Y�#H�#H�03�3C�/D�/D�#H�#H�#H�
I�
I�
I�+�
F�
F���%�&D�&�&D�&D�E�E�E�E� $�����
�4����������+�rc�F��tj����fd���}|S)z�Use this function when threads are being used. This will
ensure that the threads are cleaned up even when the test fails.
c�Z��t��} �|�t|�S#t|�wxYwr)rr!)�args�key�funcs �r
� decoratorzreap_threads.<locals>.decorator;s<������� $��4��;��s�#�#�#���s�#�#�#���s��*)� functools�wraps)r&r's` r
�reap_threadsr*7s;����_�T���$�$�$�$���$��rc#�K�|�tj}tj��} dV�t j��}||z} tj��}||krdSt j��|kr8t j��|z
}d||z
�d|d�d|�d|�d� }t
|���t jd ��tj����#t j��}||z} tj��}||krnwt j��|kr8t j��|z
}d||z
�d|d�d|�d|�d� }t
|���t jd ��tj����wxYw)
aH
bpo-31234: Context manager to wait until all threads created in the with
statement exit.
Use _thread.count() to check if threads exited. Indirectly, wait until
threads exit the internal t_bootstrap() C function of the _thread module.
threading_setup() and threading_cleanup() are designed to emit a warning
if a test leaves running threads in the background. This context manager
is designed to cleanup threads started by the _thread.start_new_thread()
which doesn't allow to wait for thread exit, whereas thread.Thread has a
join() method.
NTz!wait_threads() failed to cleanup z threads after �.1fz seconds (count: z
, old count: rr) r�
SHORT_TIMEOUTrrr� monotonic�AssertionErrorrr)�timeout� old_count�
start_time�deadliner�dt�msgs r
�wait_threads_exitr6Es�������'���� � �I�!�
�����^�%�%�
���'�� !��N�$�$�E�� �!�!����~���(�*�*��^�%�%�
�2��C�5�9�;L�C�C�(*�9�C�C�"'�C�C�6?�C�C�C��%�S�)�)�)��J�u������ � � � !���^�%�%�
���'�� !��N�$�$�E�� �!�!���~���(�*�*��^�%�%�
�2��C�5�9�;L�C�C�(*�9�C�C�"'�C�C�6?�C�C�C��%�S�)�)�)��J�u������ � � � !� !� !� !� !s�C�B,Fc��|�tj}|�|��|���rd|d�d�}t |���dS)zcJoin a thread. Raise an AssertionError if the thread is still alive
after timeout seconds.
Nzfailed to join the thread in r,z seconds)rr-�join�is_aliver/)r r0r5s r
�join_threadr:js`�����'��
�K�K�����
�����"�C�g�C�C�C�C���S�!�!�!�"�"rc#�bK�ddl}t|��}g} |D]+}|���|�|���,n@#tjr.t
dt|��t|��fz���xYwdV� |r
|��tj ��}tdd��D]�}|dz
}|D]9}|�t|tj ��z
d�����:d�|D��}|sn.tjr!t
dt|��|fz����d �|D��}|r>|�
tj��t!d
t|��z���dS#d �|D��}|r>|�
tj��t!d
t|��z���wxYw# |r
|��tj ��}tdd��D]�}|dz
}|D]9}|�t|tj ��z
d�����:d�|D��}|sn.tjr!t
dt|��|fz����d �|D��}|r>|�
tj��t!d
t|��z���nQ#d �|D��}|r>|�
tj��t!d
t|��z���wxYwwxYw)Nrz/Can't start %d threads, only %d threads startedr��<rc�:�g|]}|����|��Sr�r9��.0�ts r
�
<listcomp>z!start_threads.<locals>.<listcomp>�s%��>�>�>�������>�1�>�>�>rz7Unable to join %d threads during a period of %d minutesc�:�g|]}|����|��Srr?r@s r
rCz!start_threads.<locals>.<listcomp>�s%��:�:�:�Q�Q�Z�Z�\�\�:�q�:�:�:rzUnable to join %d threads)�faulthandler�list�start�appendr�verbose�printrrr.rr8�max�dump_traceback�sys�stdoutr/)�threads�unlockrE�startedrB�endtimer0s r
�
start_threadsrSvs����������7�m�m�G��G�Q� ��
"�
"����� � � ����q�!�!�!�!�
"�� ���
4��G��7�|�|�S��\�\�2�3�4�4�4�����
���� Q��
�������n�&�&�G� ��B�<�<�
B�
B���2�
�� �B�B�A��F�F�3�w���)9�)9�9�4�@�@�A�A�A�A�>�>�g�>�>�>�����E��?�B��'�*-�g�,�,��)@�A�B�B�B��;�:�'�:�:�:�G��
Q��+�+�C�J�7�7�7�$�%@�3�w�<�<�%O�P�P�P�
Q�
Q��;�:�'�:�:�:�G��
Q��+�+�C�J�7�7�7�$�%@�3�w�<�<�%O�P�P�P�P�P�P�P��% Q��
�������n�&�&�G� ��B�<�<�
B�
B���2�
�� �B�B�A��F�F�3�w���)9�)9�9�4�@�@�A�A�A�A�>�>�g�>�>�>�����E��?�B��'�*-�g�,�,��)@�A�B�B�B��;�:�'�:�:�:�G��
Q��+�+�C�J�7�7�7�$�%@�3�w�<�<�%O�P�P�P�
Q��;�:�'�:�:�:�G��
Q��+�+�C�J�7�7�7�$�%@�3�w�<�<�%O�P�P�P�P�P�P�P�P�P�P�PsF�.A �G� =B�G�B0F�AG�L.�B0K�A
L.�AL*�*L.c�*�eZdZdZd�Zd�Zd�Zd�ZdS)�catch_threading_exceptiona
Context manager catching threading.Thread exception using
threading.excepthook.
Attributes set when an exception is caught:
* exc_type
* exc_value
* exc_traceback
* thread
See threading.excepthook() documentation for these attributes.
These attributes are deleted at the context manager exit.
Usage:
with threading_helper.catch_threading_exception() as cm:
# code spawning a thread which raises an exception
...
# check the thread exception, use cm attributes:
# exc_type, exc_value, exc_traceback, thread
...
# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
# exists at this point
# (to avoid reference cycles)
c�L�d|_d|_d|_d|_d|_dSr)�exc_type� exc_value�
exc_tracebackr � _old_hook��selfs r
�__init__z"catch_threading_exception.__init__�s*����
����!����������rc�f�|j|_|j|_|j|_|j|_dSr)rWrXrYr )r\r$s r
�_hookzcatch_threading_exception._hook�s,���
��
�����!�/����k����rc�J�tj|_|jt_|Sr)r�
excepthookrZr_r[s r
� __enter__z#catch_threading_exception.__enter__�s��"�-���#�z� ���rc�8�|jt_|`|`|`|`dSr)rZrrarWrXrYr )r\�exc_infos r
�__exit__z"catch_threading_exception.__exit__�s%��#�~� ���M��N����K�K�KrN)�__name__�
__module__�__qualname__�__doc__r]r_rbrerrr
rUrU�sZ��������<���"�"�"����
����rrU�returnc�l�tjdkrtjjStjdkrdSdS)ayDetect whether Python can start new threads.
Some WebAssembly platforms do not provide a working pthread
implementation. Thread support is stubbed and any attempt
to create a new thread fails.
- wasm32-wasi does not have threading.
- wasm32-emscripten can be compiled with or without pthread
support (-s USE_PTHREADS / __EMSCRIPTEN_PTHREADS__).
�
emscripten�wasiFT)rM�platform�_emscripten_info�pthreadsrrr
�_can_start_threadrq�s6���|�|�#�#��#�,�,� ��� � ��u��trF)�modulec�x�d}|rtstj|���dStjt|��S)z�Skip tests or modules that require working threading.
Can be used as a function/class decorator or to skip an entire module.
zrequires threading supportN)�can_start_thread�unittest�SkipTest�
skipUnless)rrr5s r
�requires_working_threadingrx�sJ��
'�C�
�:�� )��#�C�(�(�(� )� )��"�#3�S�9�9�9rr)r�
contextlibr(rMrrru�testrrr!r*�contextmanagerr6r:rSrU�boolrqrtrxrrr
�<module>r}si��������������
�
�
�
�������������������8�8�8����6�����!!�!!�!!���!!�H "� "� "� "���"Q�"Q�"Q���"Q�J6�6�6�6�6�6�6�6�r�4�����&%�$�&�&��).�
:�
:�
:�
:�
:�
:�
:r |