관리-도구
편집 파일: test_greenlet_trash.cpython-38.pyc
U �g � @ sF d Z ddlmZmZmZ ddlZG dd� dej�ZedkrBe� � dS )a� Tests for greenlets interacting with the CPython trash can API. The CPython trash can API is not designed to be re-entered from a single thread. But this can happen using greenlets, if something during the object deallocation process switches greenlets, and this second greenlet then causes the trash can to get entered again. Here, we do this very explicitly, but in other cases (like gevent) it could be arbitrarily more complicated: for example, a weakref callback might try to acquire a lock that's already held by another greenlet; that would allow a greenlet switch to occur. See https://github.com/gevent/gevent/issues/1909 This test is fragile and relies on details of the CPython implementation (like most of the rest of this package): - We enter the trashcan and deferred deallocation after ``_PyTrash_UNWIND_LEVEL`` calls. This constant, defined in CPython's object.c, is generally 50. That's basically how many objects are required to get us into the deferred deallocation situation. - The test fails by hitting an ``assert()`` in object.c; if the build didn't enable assert, then we don't catch this. - If the test fails in that way, the interpreter crashes. � )�print_function�absolute_import�divisionNc @ s e Zd Zdd� Zdd� ZdS )�TestTrashCanReEnterc C sh zddl m} W n< tk rL dd l}|jd d� dks>t�| �d� Y nX td�D ]}| �� qVd S )Nr ��get_tstate_trash_delete_nesting� )� � z1get_tstate_trash_delete_nesting is not available.� ) �greenlet._greenletr �ImportError�sys�version_info�AssertionErrorZskipTest�range�check_it)�selfr r �_� r �R/opt/hc_python/lib64/python3.8/site-packages/greenlet/tests/test_greenlet_trash.py�test_it# s zTestTrashCanReEnter.test_itc s dd l �ddlm} ��� �|� dks*t�d�d�G � ����fdd�d�� �fdd��� �fd d ���� }| �� j�� | �� jd� ~| �d|� � d � _| �d|� � | � � j d� | �� j�� | � � j� | �� j�j� | �� j� j� | �� j�d � dd l}|�� d S )Nr r �2 i� c sH e Zd ZdZdZdZdZdZdZdZ � fdd�Z � ����fdd�ZdS ) z-TestTrashCanReEnter.check_it.<locals>.DeallocaY An object with a ``__del__`` method. When it starts getting deallocated from a deferred trash can run, it switches greenlets, allocates more objects which then also go in the trash can. If we don't save state appropriately, nesting gets out of order and we can crash the interpreter. FNr c s || _ � jd7 _dS )a :param sequence_number: The ordinal of this object during one particular creation run. This is used to detect (guess, really) when we have entered the trash can's deferred deallocation. � N)�i�CREATED)r Zsequence_number)�Deallocr r �__init__` s z6TestTrashCanReEnter.check_it.<locals>.Dealloc.__init__c s� | j �krB| jsB��� � _���� }� _|�� }|dks>t�~n\| j dkr���� �k r�d� _z��d� W n0 �jk r� } zt |�� _~W 5 d }~X Y nX � j d7 _ ��� �k rƈ jd7 _d S )N�* �( Tr )r �SPAWNED� getcurrent�greenlet�BG_GLET�switchr �BG_RAN�GreenletExit�type� DESTROYED�DESTROYED_BG)r �other�x�ex�r �TRASH_UNWIND_LEVEL�background_greenletr"