o
    Ni                     @   sF   d Z ddlZddlmZ ddlmZ ddlmZ G dd dejZdS )	z
Test libpython.py. This is already partly tested by test_libcython_in_gdb and
Lib/test/test_gdb.py in the Python source. These tests are run in gdb and
called from test_libcython_in_gdb.main()
    N)	libcython)	libpython   )test_libcython_in_gdbc                       sn   e Zd ZdZ fddZdd ZdddZd	d
 ZdddZdddZ	dd Z
dd Zdd Zdd Z  ZS )TestPrettyPrintersa  
    Test whether types of Python objects are correctly inferred and that
    the right libpython.PySomeTypeObjectPtr classes are instantiated.

    Also test whether values are appropriately formatted (don't be too
    laborious as Lib/test/test_gdb.py already covers this extensively).

    Don't take care of decreffing newly allocated objects as a new
    interpreter is started for every test anyway.
    c                    s   t    | d d S )Nzb = c = d = 0)supersetUpbreak_and_run)self	__class__ _/home/ubuntu/.local/lib/python3.10/site-packages/Cython/Debugger/Tests/test_libpython_in_gdb.pyr      s   
zTestPrettyPrinters.setUpc                 C   s    t |}t|dksJ |S )Nr   )gdbparse_and_evalr   pointervalue)r
   codevaluer   r   r   get_pyobject   s   
zTestPrettyPrinters.get_pyobjectNc                 C   s:   |d ur||d}t d|  d| }tj| |S )N)varnamer   zset $%(varname)s = %(code)s$)r   executer   PyObjectPtrfrom_pyobject_ptrr   )r
   r   gdbvardr   r   r   pyobject_fromcode$   s
   
z$TestPrettyPrinters.pyobject_fromcodec                 C   s   | tjS N)get_truncated_reprr   MAX_OUTPUT_LEN)r
   pyobjectr   r   r   get_repr,   s   zTestPrettyPrinters.get_reprc                 C   s0   d|vsJ d| dt|f }| j||dS )N   "z0(PyObject *) PyBytes_FromStringAndSize("%s", %d)	iso8859-1r   )decodelenr   )r
   stringr   r   r   r   r   alloc_bytestring/   s   z#TestPrettyPrinters.alloc_bytestringc                 C   sN   t  }d|f }|dd}| jd||ddddt|f |d	S )
NzPyUnicode%s_DecodeUnicodeEscapeunicode_escaper#   z#(PyObject *) %s("%s", %d, "strict")"z\"\z\\r$   )r   get_inferior_unicode_postfixencoder%   r   replacer&   )r
   r'   r   postfixfuncnamedatar   r   r   alloc_unicodestring6   s   
z&TestPrettyPrinters.alloc_unicodestringc                 C   s:   |  d}tj}d}| t|| | | || d S )Ns   spamzb'spam')r(   r   PyBytesObjectPtrassertEqualtyper!   )r
   
bytestringbytestring_classexpectedr   r   r   test_bytestring@   s
   
z"TestPrettyPrinters.test_bytestringc                 C   s2   |  d}| t|tj | | |d d S )Nu   spam ἄλφαu   'spam ἄλφα')r2   r4   r5   r   PyUnicodeObjectPtrr!   )r
   unicode_stringr   r   r   test_unicodeI   s   
zTestPrettyPrinters.test_unicodec                 C   sD   | j ddd}tdsJ | t|tj | | |d d S )NzPyLong_FromLong(200)longvalr$   z!$longval->ob_type == &PyLong_Type200)r   r   r   r4   r5   r   PyLongObjectPtrr!   )r
   r=   r   r   r   	test_longO   s   zTestPrettyPrinters.test_longc                 C   s    |  d}| t|tj d S )NzPyEval_GetFrame())r   r4   r5   r   PyFrameObjectPtr)r
   framer   r   r   test_frame_typeW   s   
z"TestPrettyPrinters.test_frame_typer   )__name__
__module____qualname____doc__r   r   r   r!   r(   r2   r9   r<   r@   rC   __classcell__r   r   r   r   r      s    



	r   )	rG   r   Cython.Debuggerr   r    r   DebugTestCaser   r   r   r   r   <module>   s    