
    dafpE                     X    d dl Z d dlZd dlmZ ddlmZmZmZmZm	Z	 d Z
 G d de	      Zy)    N)support   )BREAKPOINT_FNGDB_VERSIONrun_gdbsetup_moduleDebuggerTestsc                      t                y N)r        :/root/Python-3.12.4/Lib/test/test_gdb/test_pretty_print.pysetUpModuler   
   s    Nr   c                      e Zd Z	 	 d!dZd Zd"dZ ej                  d      d        Zd Z	d Z
d	 Z ej                  d      d
        Z ej                  d      d        Zd Z ej                  d      d        Z ej                  d      d        Zd Zd Zd Zd Zd"dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d  Z"y)#PrettyPrintTestsNc                     |xs dg}| j                  |t        ||      }t        j                  d|t        j                        }|s| j                  d|d|       |j                  d      |fS )Nzbacktrace 1)
breakpointcmds_after_breakpointimport_sitezS#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)?\)\s+at\s+\S*[A-Za-z]+/[A-Za-z0-9_-]+\.czUnexpected gdb output: 
r   )get_stack_tracer   researchDOTALLfailgroup)selfsourcer   r   
gdb_outputms         r   get_gdb_reprzPrettyPrintTests.get_gdb_repr   sz      !6 H-))&]@U6A * C
 II6 		# IIZPQwwqz:%%r   c                 T    | j                  d      }| j                  t        |v        y )Nid(42))r   
assertTruer   r   r   s     r   test_getting_backtracez'PrettyPrintTests.test_getting_backtrace3   s#    ))(3
34r   c           	          | j                  dt        |      z   dz         \  }}|st        |      }| j                  |||d|d|       y )Nzid()z did not equal expected z; full output was:
)r!   asciireprassertEqual)r   valexp_reprgdb_reprr   s        r   assertGdbReprzPrettyPrintTests.assertGdbRepr7   sQ      $00s1Cc1IJ*CyH8%x=	?r   cpuc                     | j                  d       | j                  d       | j                  d       | j                  d       | j                  d       y)z0Verify the pretty-printing of various int values*   r   il    J)l  I5 Nr/   r   s    r   test_intzPrettyPrintTests.test_intA   sK     	212=),-r   c                 j    | j                  d       | j                  d       | j                  d       y)z2Verify the pretty-printing of True, False and NoneTFNr3   r4   s    r   test_singletonsz PrettyPrintTests.test_singletonsJ   s,    4 5!4 r   c                 x    | j                  i        | j                  ddid       | j                  dddd       y)z*Verify the pretty-printing of dictionariesfoobarz{'foo': 'bar'}r2   )r9   douglasz{'foo': 'bar', 'douglas': 42}Nr3   r4   s    r   
test_dictszPrettyPrintTests.test_dictsP   s;    2E5>+;<5R8:YZr   c                 l    | j                  g        | j                  t        t        d                   y)z#Verify the pretty-printing of lists   N)r/   listranger4   s    r   
test_listszPrettyPrintTests.test_listsW   s&    24a>*r   c                    | j                  d       | j                  d       | j                  d       | j                  d       | j                  d       | j                  t        t        d      D cg c]  }| c}             yc c}w )z#Verify the pretty-printing of bytesr   s(   And now for something hopefully the sames7   string with embedded NUL here   and then some more texts7   this is a tab:	 this is a slash-N:
 this is a slash-R:s!   this is byte 255: and byte 128:   N)r/   bytesr@   )r   bs     r   
test_byteszPrettyPrintTests.test_bytes\   sy     	3FGVW 4 	
 	EF5U3Z!8!!89:!8s   3	Bc                     t        dd      \  }}||sst        d|d|       fd} j                  d        j                  d        j                  d        |d	        |d
        |t        d             y)z-Verify the pretty-printing of unicode stringsz--eval-commandz:python import locale; print(locale.getpreferredencoding())zZunable to determine the Python locale preferred encoding of embedded Python in GDB
stdout=z
stderr=c                     	 | j                         j                  |        y # t        $ r j                  | t        |              Y y w xY wr   )encoder/   UnicodeEncodeErrorr)   )textencodingr   s    r   
check_reprz1PrettyPrintTests.test_strings.<locals>.check_repr~   sI    )H% ""4( & 6""4t56s   & $AA z(And now for something hopefully the samez7string with embedded NUL here   and then some more textu   ☠u   文字化けi! N)r   RuntimeErrorr/   chr)r   stdoutstderrrM   rL   s   `   @r   test_stringszPrettyPrintTests.test_stringsk   s     !HJ   $ %& &	) 	2EFUV 	8
 	-. 	3w< r   c                 ~    | j                  t               d       | j                  dd       | j                  d       y)z$Verify the pretty-printing of tuplesz())r   z(1,))r9   r:   bazN)r/   tupler4   s    r   test_tupleszPrettyPrintTests.test_tuples   s3    57D)4(01r   c                    t         dk  r| j                  d       | j                  t               d       | j                  t        dg      d       t        j
                  j                  s:| j                  t        ddg      d       | j                  t        g d      d	       | j                  d
      \  }}| j                  |d       y)z"Verify the pretty-printing of sets      z.pretty-printing of sets needs gdb 7.3 or laterzset()az{'a'}rE   z
{'a', 'b'}   r>      z	{4, 5, 6}z&s = set(['a','b'])
s.remove('a')
id(s)z{'b'}N)	r   skipTestr/   setsysflagsignore_environmentr!   r+   r   r.   r   s      r   	test_setszPrettyPrintTests.test_sets   s     MMJK35'*3u:w/yy++sC:=s9~{;  $00 2	  
* 	7+r   c                 N   t         dk  r| j                  d       | j                  t               d       | j                  t        dg      d       t        j
                  j                  s;| j                  t        ddg      d       | j                  t        g d      d	       y
y
)z(Verify the pretty-printing of frozensetsrY   z4pretty-printing of frozensets needs gdb 7.3 or laterzfrozenset()r\   zfrozenset({'a'})rE   zfrozenset({'a', 'b'})r]   zfrozenset({4, 5, 6})N)r   r`   r/   	frozensetrb   rc   rd   r4   s    r   test_frozensetsz PrettyPrintTests.test_frozensets   s     MMPQ9;69cU+-?@yy++y#s46MNy35KL ,r   c                     | j                  d      \  }}| j                  |d       | j                  d      \  }}| j                  |d       y )NzR
try:
    raise RuntimeError("I am an error")
except RuntimeError as e:
    id(e)
zRuntimeError('I am an error',)z=
try:
    a = 1 / 0
except ZeroDivisionError as e:
    id(e)
z&ZeroDivisionError('division by zero',)r!   r+   re   s      r   test_exceptionsz PrettyPrintTests.test_exceptions   sc    #00 2  * 	9	;
  $00 2  * 	A	Cr   c                     | j                  d      \  }}t        j                  d|      }| j                  |d|z         y)z7Verify the pretty-printing of new-style class instancesz8
class Foo:
    pass
foo = Foo()
foo.an_int = 42
id(foo)*<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>'Unexpected new-style class rendering %rmsgNr!   r   matchr$   r   r.   r   r    s       r   test_modern_classz"PrettyPrintTests.test_modern_class   sK    #00 2  * HHBHMEP 	 	Rr   c                     | j                  d      \  }}t        j                  d|      }| j                  |d|z         y)z<Verify the pretty-printing of an instance of a list subclasszO
class Foo(list):
    pass
foo = Foo()
foo += [1, 2, 3]
foo.an_int = 42
id(foo)rn   ro   rp   Nrr   rt   s       r   test_subclassing_listz&PrettyPrintTests.test_subclassing_list   sK    #00 2  * HHBHMEP 	 	Rr   c                     | j                  d      \  }}t        j                  d|      }| j                  |d|z         y)z=Verify the pretty-printing of an instance of a tuple subclasszH
class Foo(tuple):
    pass
foo = Foo((1, 2, 3))
foo.an_int = 42
id(foo)rn   ro   rp   Nrr   rt   s       r   test_subclassing_tuplez'PrettyPrintTests.test_subclassing_tuple   sM      $00 2  * HHBHMEP 	 	Rr   c                     |r|dg}ndg}| j                  ||      \  }}|r||k(  ryd}t        j                  ||      }|s| j                  d|d|       yy)zRun Python under gdb, corrupting variables in the inferior process
        immediately before taking a backtrace.

        Verify that the variable's representation is the expected failsafe
        representation	backtracer   Nz<.* at remote 0x-?[0-9a-f]+>Unexpected gdb representation: r   )r!   r   rs   r   )	r   r   
corruptionexpreprr   r.   r   patternr    s	            r   
assertSanezPrettyPrintTests.assertSane   s     #-{";!#.-! f4I  K 	* 7"  1HHWh'II#Z1 2 r   c                 X    | j                  dddg      \  }}| j                  |d       y)z2Ensure that a NULL PyObject* is handled gracefullyr#   zset variable v=0r{   r|   0x0Nrk   re   s      r   test_NULL_ptrzPrettyPrintTests.test_NULL_ptr  sA     h5G5@5B  C 	* 	5)r   c                 (    | j                  dd       y)z?Ensure that a PyObject* with NULL ob_type is handled gracefullyr#   zset v->ob_type=0Nr   r4   s    r   test_NULL_ob_typez"PrettyPrintTests.test_NULL_ob_type%  s    *	,r   c                 ,    | j                  ddd       y)zDEnsure that a PyObject* with a corrupt ob_type is handled gracefullyr#   zset v->ob_type=0xDEADBEEF42r   Nr   r4   s    r   test_corrupt_ob_typez%PrettyPrintTests.test_corrupt_ob_type*  s    3 $ 	 	&r   c                 ,    | j                  ddd       y)zDEnsure that a PyObject* with a type with corrupt tp_flags is handledr#   zset v->ob_type->tp_flags=0x0r   r   Nr   r4   s    r   test_corrupt_tp_flagsz&PrettyPrintTests.test_corrupt_tp_flags0  s    6 $ 	 	&r   c                 ,    | j                  ddd       y)zCEnsure that a PyObject* with a type with corrupt tp_name is handledr#   z"set v->ob_type->tp_name=0xDEADBEEFr   r   Nr   r4   s    r   test_corrupt_tp_namez%PrettyPrintTests.test_corrupt_tp_name6  s    < $ 	 	&r   c                     t         j                  j                  r| j                  d       | j	                  dd      \  }}t        j                  d|      }| j                  |d|z         y)	zAEnsure that the new-style class _Helper in site.py can be handledz(need site module, but -S option was usedzid(__builtins__.help)T)r   z%<_Helper\(\) at remote 0x-?[0-9a-f]+>zUnexpected rendering %rrp   N)rb   rc   no_siter`   r!   r   rs   r$   rt   s       r   test_builtins_helpz#PrettyPrintTests.test_builtins_help<  si     99MMDE  $001HVZ0[*HH=xH5@ 	 	Br   c                     | j                  d      \  }}| j                  |d       | j                  d      \  }}| j                  |d       y)zbEnsure that a reference loop involving a list doesn't lead proxyval
        into an infinite loop:z#a = [3, 4, 5] ; a.append(a) ; id(a)z[3, 4, 5, [...]]z-a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)z[3, 4, 5, [[...]]]Nrk   re   s      r   test_selfreferential_listz*PrettyPrintTests.test_selfreferential_listJ  sX     CD 	*#56 MN 	*#78r   c                 P    | j                  d      \  }}| j                  |d       y)zbEnsure that a reference loop involving a dict doesn't lead proxyval
        into an infinite loop:z-a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)z{'foo': {'bar': {...}}}Nrk   re   s      r   test_selfreferential_dictz*PrettyPrintTests.test_selfreferential_dictU  s/     MN 	* 	#<=r   c                     | j                  d      \  }}| j                  t        j                  d|      d|d|       y )Nz:
class Foo:
    pass
foo = Foo()
foo.an_attr = foo
id(foo)1<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>r}   r   r!   r$   r   rs   re   s      r   'test_selfreferential_old_style_instancez8PrettyPrintTests.test_selfreferential_old_style_instance]  sI       	* 	!U!)+ &z3	4r   c                    | j                  d      \  }}| j                  t        j                  d|      d|d|       | j                  d      \  }}| j                  t        j                  d|      d|d|       y )NzB
class Foo(object):
    pass
foo = Foo()
foo.an_attr = foo
id(foo)r   r}   r   zR
class Foo(object):
    pass
a = Foo()
b = Foo()
a.an_attr = b
b.an_attr = a
id(a)zZ<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>\) at remote 0x-?[0-9a-f]+>r   re   s      r   'test_selfreferential_new_style_instancez8PrettyPrintTests.test_selfreferential_new_style_instancej  s       	* 	!U!)+ &z3	4  	 
 	* 	!~!)+ &z3	4r   c                     | j                  d      \  }}| j                  |d       | j                  t        |      dt        d      z          y)z)Verify that very long output is truncatedzid(list(range(1000)))a  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226...(truncated)i   z...(truncated)N)r!   r+   lenre   s      r   test_truncationz PrettyPrintTests.test_truncation  sQ    #001HI*7	8, 	X$4 55	7r   c                     | j                  d      \  }}| j                  t        j                  d|      d|d|       y )Nz$import sys; id(sys.stdout.readlines)zO<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>r}   r   r   re   s      r   test_builtin_methodz$PrettyPrintTests.test_builtin_method  sA    #001WX*!s!)+ &z3	4r   c                     | j                  dddg      }| j                  t        j                  d|t        j                        d|d|       y )NzQ
import sys
def foo(a, b, c):
    return sys._getframe(0)

f = foo(3, 4, 5)
id(f)
builtin_idzprint (PyFrameObject*)v)r   r   zK.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 4, in foo \(a=3.*r}   r   )r   r$   r   rs   r   r%   s     r   test_frameszPrettyPrintTests.test_frames  s[    )) +	 6BAZ@[ * 	,
 	!o!+!#, DNzZ	\r   )NFr   )#__name__
__module____qualname__r!   r&   r/   r   requires_resourcer5   r7   r<   rA   rF   rS   rW   rf   ri   rl   ru   rw   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s5   +/!&"&H5? Wu%. &.![+
 Wu%; &; Wu%,! &,!\2 Wu%, &,$ Wu%	M &	MC,
RRR2:*,
&&&B	9>446784\r   r   )r   rb   testr   utilr   r   r   r   r	   r   r   r   r   r   <module>r      s-    	 
 * *
h\} h\r   