
    <e                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZmZ d Z e            \  ZZZedk     r ej        dedede           ej                    s ej        d	          d
 ej                    v rej        dk    r ej        d           ej        d          pd ej        d          pdv r ej        d          e j                            e j                            ej                  d          ZdZd Z e            Zd Z ed          \  ZZes ej        d          dev r ej        d           edej                  \  ZZ de v r'dZ! ej        e!e "                                z             d Z# e#            Z$dZ% ej&        e	j'        d           G d  d!ej(                              Z) G d" d#e)          Z* ej&         e            d$           G d% d&e)                      Z+d'Z, G d( d)e)          Z- G d* d+e)          Z. G d, d-e)          Z/ G d. d/e)          Z0d0 Z1e2d1k    r ej3                     dS dS )2    N)support)findfilepython_is_optimizedc            
      \   	 g d} t          j        | t           j        t           j        d          }|5  |                                \  }}d d d            n# 1 swxY w Y   |j        r3t          dd                    |           d|j         d|d|          n"# t          $ r t          j	        d	          w xY wt          j        d
|          }|t          d|z            |t          |                    d                    t          |                    d                    fS )N)gdb-nxz	--versionT)stdoutstderruniversal_newlineszCommand  z failed with exit code z	: stdout=z stderr=zCouldn't find gdb on the pathz ^(?:GNU|HP) gdb.*?\b(\d+)\.(\d+)zunable to parse GDB version: %r      )
subprocessPopenPIPEcommunicate
returncode	ExceptionjoinOSErrorunittestSkipTestresearchintgroup)cmdprocversionr
   matchs        $/usr/lib/python3.11/test/test_gdb.pyget_gdb_versionr"      s   A)))'1'1379 9 9  	1 	1"..00OGV	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 ? 	E Dsxx}} D D.2oD D&-D D9?D D E E E	E  A A A  ?@@@A I97CCE}9GCDDDSQ((#ekk!nn*=*=>>s.   2B AB AB A =B B=   z=gdb versions before 7.0 didn't support python embedding. Saw .z:
z3test_gdb only works on source builds at the moment.ClangdarwinzDtest_gdb doesn't work correctly when python is built with LLVM clangPGO_PROF_USE_FLAGxxxPY_CORE_CFLAGS z&test_gdb is not reliable on PGO buildszpython-gdb.py123c                      t          j        d          } | sdS |                                 }d|v ot          d |D                       S )NCFLAGSFz-mcetc              3   l   K   | ]/}|                     d           o|                    d           V  0dS )z-fcf-protection)z=nonez=returnN)
startswithendswith).0flags     r!   	<genexpr>z!cet_protection.<locals>.<genexpr>Q   s\       ' ' __%677 A!]]+?@@@' ' ' ' ' '    )	sysconfigget_config_varsplitany)cflagsflagss     r!   cet_protectionr;   I   si    %h//F uLLNNE  ' ' ' %' ' ' ' '(r4   c                     |r4t           j                                        }|                    |           nd}d}t          t
          fdk    r|ddt          z   fz  }t          j        || z   t          j	        t          j	        t          j	        |          }|5  |
                                \  }}ddd           n# 1 swxY w Y   |                    dd          |                    dd          fS )	zRuns gdb in --batch mode with the additional arguments given by *args.

    Returns its (stdout, stderr) decoded from utf-8 using the replace handler.
    N)r   z--batchr   r#      z-iexzadd-auto-load-safe-path )stdinr	   r
   envzutf-8replace)osenvironcopyupdategdb_major_versiongdb_minor_versioncheckout_hook_pathr   r   r   r   decode)argsenv_varsr@   base_cmdr   outerrs          r!   run_gdbrO   Y   s/   
  joo

8 )H,-77V7:LLMMHtO #-/#-?#-? #% % %D 
 & &##%%S& & & & & & & & & & & & & & &::gy))3::gy+I+IIIs   B<<C C z9--eval-command=python import sys; print(sys.version_info)z*gdb not built with embedded python supportzmajor=2zgdb built with Python 2--argszauto-loading has been declinedz3gdb security settings prevent use of custom hooks: c                      t          d          \  } }t          j        d|           }|st          j        d          |                    d                              d          }d|v S )Nz+--eval-command=python print(dir(gdb.Frame))z.*\[(.*)\].*z1Unable to parse output from gdb.Frame.select testr   z, z'select')rO   r   r    r   r   r   r7   )r	   _mgdb_frame_dirs       r!   gdb_has_frame_selectrU      sh    EFFIFA
&))A U STTTGGAJJ$$T**M&&r4   
builtin_idznot useful for PGOc                   D    e Zd ZdZddedddfdZ	 	 d	dZd Zd Zd Z	dS )
DebuggerTestsz(Test that the debugger can debug Python.NFc                 8   dd|z  ddg}t           t          fdk    r|dgz  }|rt          r|dgz  }||z  }n|dgz  }d	 |D             }|d
t          j        gz  }|                    t          j                               |s|dgz  }|r|d|gz  }n|r||gz  }t          |dt          i\  }	}
|s2|

                                D ]}t          |t          j                   d|
v rt          j        d          dD ]}||	v rt          j        |d          |	S )z
        Run 'python -c SOURCE' under gdb with a breakpoint.

        Support injecting commands after the breakpoint is reached

        Returns the stdout from gdb

        cmds_after_breakpoint: if provided, a list of strings: gdb commands
        zset breakpoint pending yeszbreak %szset print address offrunr=   zset print entry-values nonext	backtracec                     g | ]}d |z  S )z--eval-command=%s )r1   r   s     r!   
<listcomp>z1DebuggerTests.get_stack_trace.<locals>.<listcomp>   s    >>>c#c)>>>r4   rP   z-Sz-cPYTHONHASHSEED)filezPC not savedzKgdb cannot walk the frame object because the Program Counter is not present)z!(frame information optimized out)z*Unable to read information on python framez found in gdb output)rF   rG   CET_PROTECTIONsys
executableextendr   _args_from_interpreter_flagsrO   r`   
splitlinesprintr
   r   r   )selfsourcescript
breakpointcmds_after_breakpointimport_siteignore_stderrcommandsrJ   rM   rN   linepatterns                r!   get_stack_tracezDebuggerTests.get_stack_trace   s   8 1+ ,* 01V;;455H  		& %
 VH$--HH%H
 ?>X>>>! 	!J;==>>> 	TFND 	T6N"DD 	VHD D@@@S 	-(( - -d,,,,, S  # %3 4 4 4
 	L 	LG #~~'7(J(J(JKKK  
r4   c                     |pdg}|                      |t          ||          }t          j        d|t          j                  }|s|                     d|d|           |                    d          |fS )Nzbacktrace 1)rl   rm   rn   zS#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   )rs   BREAKPOINT_FNr   r   DOTALLfailr   )ri   rj   rm   rn   
gdb_outputrS   s         r!   get_gdb_reprzDebuggerTests.get_gdb_repr   s      !6 H-))&]@U6A * C C
 I6 	# #  	RIIIZZPQQQwwqzz:%%r4   c                 d    |                      |                    |          |d|           dS )z9Ensure that the given "actual" string ends with "exp_end"z did not end with msgN)
assertTruer0   )ri   actualexp_ends      r!   assertEndsWithzDebuggerTests.assertEndsWith$  sH    008>H 	 	J 	J 	J 	J 	Jr4   c                     t          j        ||t           j                  }|s|                     |d|           d S d S )Nz did not match r|   )r   r    rw   rx   )ri   r   rr   rS   s       r!   assertMultilineMatchesz$DebuggerTests.assertMultilineMatches)  sQ    HWfbi00 	EII66677CIDDDDD	E 	Er4   c                      t          d          S )Nzgdb_sample.py)r   ri   s    r!   get_sample_scriptzDebuggerTests.get_sample_script.  s    (((r4   )NF)
__name__
__module____qualname____doc__rv   rs   rz   r   r   r   r^   r4   r!   rX   rX      s         32%)$#0.2$)&+	j j j jZ ,0!&"& "& "& "&HJ J J
E E E
) ) ) ) )r4   rX   c                       e 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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dS ) PrettyPrintTestsc                 h    |                      d          }|                     t          |v            d S )Nid(42))rs   r~   rv   ri   ry   s     r!   test_getting_backtracez'PrettyPrintTests.test_getting_backtrace2  s2    ))(33
344444r4   Nc           	          |                      dt          |          z   dz             \  }}|st          |          }|                     |||d|d|           d S )Nzid()z did not equal expected z; full output was:
)rz   asciireprassertEqual)ri   valexp_reprgdb_reprry   s        r!   assertGdbReprzPrettyPrintTests.assertGdbRepr6  s{      $00s1Cc1IJJ* 	!CyyH8%XXxxx=	? 	? 	? 	? 	?r4   c                     |                      d           |                      d           |                      d           |                      d           |                      d           dS )z0Verify the pretty-printing of various int values*   r   il    J)l  I5 Nr   r   s    r!   test_intzPrettyPrintTests.test_int@  sm    212=))),-----r4   c                     |                      d           |                      d           |                      d           dS )z2Verify the pretty-printing of True, False and NoneTFNr   r   s    r!   test_singletonsz PrettyPrintTests.test_singletonsH  sD    4   5!!!4     r4   c                     |                      i            |                      ddid           |                      dddd           dS )z*Verify the pretty-printing of dictionariesfoobarz{'foo': 'bar'}r   )r   douglasz{'foo': 'bar', 'douglas': 42}Nr   r   s    r!   
test_dictszPrettyPrintTests.test_dictsN  sV    2E5>+;<<<5R88:YZZZZZr4   c                     |                      g            |                      t          t          d                               dS )z#Verify the pretty-printing of lists   N)r   listranger   s    r!   
test_listszPrettyPrintTests.test_listsU  s<    24a>>*****r4   c                 J   |                      d           |                      d           |                      d           |                      d           |                      d           |                      t          d t          d          D                                  dS )	z#Verify the pretty-printing of bytesr4   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:c                     g | ]}|S r^   r^   )r1   bs     r!   r_   z/PrettyPrintTests.test_bytes.<locals>.<listcomp>f  s    !8!8!8!!8!8!8r4      N)r   bytesr   r   s    r!   
test_byteszPrettyPrintTests.test_bytesZ  s    3FGGGVWWW 4 	 	 	
 	EFFF5!8!8U3ZZ!8!8!899:::::r4   c                 j    t          dd          \  }}|                                |sst          d|            fd}                     d                                d                                d            |d            |d	            |t	          d
                     dS )z-Verify the pretty-printing of unicode stringsz--eval-commandz:python import locale; print(locale.getpreferredencoding())zFunable to determine the preferred encoding of embedded Python in GDB: c                     	 |                                                     |            d S # t          $ r'                     | t          |                      Y d S w xY wN)encoder   UnicodeEncodeErrorr   )textencodingri   s    r!   
check_reprz1PrettyPrintTests.test_strings.<locals>.check_reprx  sx    )H%%% ""4((((( & 6 6 6""4t5555556s   / -A A r*   z(And now for something hopefully the samez7string with embedded NUL here   and then some more textu   ☠u   文字化けi! N)rO   rstripRuntimeErrorr   chr)ri   rM   rN   r   r   s   `   @r!   test_stringszPrettyPrintTests.test_stringsh  s    HJ JS ::<< 	5h 	54.14 45 5 5	) 	) 	) 	) 	) 	) 	2EFFFUVVV 	
8
 	
-... 	
3w<<     r4   c                     |                      t                      d           |                      dd           |                      d           dS )z$Verify the pretty-printing of tuplesz())r   z(1,))r   r   bazN)r   tupler   s    r!   test_tupleszPrettyPrintTests.test_tuples  sM    577D)))4(((011111r4   c                    t           t          fdk     r|                     d           |                     t	                      d           |                     t	          dg          d           t
          j        j        sJ|                     t	          ddg          d           |                     t	          g d          d	           |                     d
          \  }}| 	                    |d           dS )z"Verify the pretty-printing of setsr#      z.pretty-printing of sets needs gdb 7.3 or laterzset()az{'a'}r   z
{'a', 'b'}r>   r      z	{4, 5, 6}z&s = set(['a','b'])
s.remove('a')
id(s)z{'b'}N)
rF   rG   skipTestr   setrc   r:   ignore_environmentrz   r   ri   r   ry   s      r!   	test_setszPrettyPrintTests.test_sets  s    01F::MMJKKK355'***3u::w///y+ 	<sC:===s999~~{;;;  $00 2	  
  
* 	7+++++r4   c                    t           t          fdk     r|                     d           |                     t	                      d           |                     t	          dg          d           t
          j        j        sL|                     t	          ddg          d           |                     t	          g d          d	           d
S d
S )z(Verify the pretty-printing of frozensetsr   z4pretty-printing of frozensets needs gdb 7.3 or laterzfrozenset()r   zfrozenset({'a'})r   zfrozenset({'a', 'b'})r   zfrozenset({4, 5, 6})N)rF   rG   r   r   	frozensetrc   r:   r   r   s    r!   test_frozensetsz PrettyPrintTests.test_frozensets  s    01F::MMPQQQ9;;6669cU++-?@@@y+ 	My#s446MNNNy335KLLLLL	M 	Mr4   c                     |                      d          \  }}|                     |d           |                      d          \  }}|                     |d           d S )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',)rz   r   r   s      r!   test_exceptionsz PrettyPrintTests.test_exceptions  s    #00 2    * 	9	; 	; 	;
  $00 2    * 	A	C 	C 	C 	C 	Cr4   c                     |                      d          \  }}t          j        d|          }|                     |d|z             dS )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 %rr|   Nrz   r   r    r~   ri   r   ry   rS   s       r!   test_modern_classz"PrettyPrintTests.test_modern_class  si    #00 2    * HBHMMEP 	 	R 	R 	R 	R 	Rr4   c                     |                      d          \  }}t          j        d|          }|                     |d|z             dS )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)r   r   r|   Nr   r   s       r!   test_subclassing_listz&PrettyPrintTests.test_subclassing_list  si    #00 2    * HBHMMEP 	 	R 	R 	R 	R 	Rr4   c                     |                      d          \  }}t          j        d|          }|                     |d|z             dS )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)r   r   r|   Nr   r   s       r!   test_subclassing_tuplez'PrettyPrintTests.test_subclassing_tuple  sk      $00 2    * HBHMMEP 	 	R 	R 	R 	R 	Rr4   c                     |r|dg}ndg}|                      ||          \  }}|r||k    rdS d}t          j        ||          }|s|                     d|d|           dS dS )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
        representationr\   rm   Nz<.* at remote 0x-?[0-9a-f]+>Unexpected gdb representation: ru   )rz   r   r    rx   )	ri   rj   
corruptionexpreprrm   r   ry   rr   rS   s	            r!   
assertSanezPrettyPrintTests.assertSane  s      	0#-{";!!#.-! f4I  K K 	*  	7""  1HWh'' 	2III#88ZZ1 2 2 2 2 2	2 	2r4   c                 j    |                      dddg          \  }}|                     |d           dS )z2Ensure that a NULL PyObject* is handled gracefullyr   zset variable v=0r\   r   0x0Nr   r   s      r!   test_NULL_ptrzPrettyPrintTests.test_NULL_ptr  sS     h5G5@5B  C C 	* 	5)))))r4   c                 2    |                      dd           dS )z?Ensure that a PyObject* with NULL ob_type is handled gracefullyr   zset v->ob_type=0Nr   r   s    r!   test_NULL_ob_typez"PrettyPrintTests.test_NULL_ob_type  s'    *	, 	, 	, 	, 	,r4   c                 6    |                      ddd           dS )zDEnsure that a PyObject* with a corrupt ob_type is handled gracefullyr   zset v->ob_type=0xDEADBEEF42r   Nr   r   s    r!   test_corrupt_ob_typez%PrettyPrintTests.test_corrupt_ob_type"  s/    3 $ 	 	& 	& 	& 	& 	&r4   c                 6    |                      ddd           dS )zDEnsure that a PyObject* with a type with corrupt tp_flags is handledr   zset v->ob_type->tp_flags=0x0r   r   Nr   r   s    r!   test_corrupt_tp_flagsz&PrettyPrintTests.test_corrupt_tp_flags(  s/    6 $ 	 	& 	& 	& 	& 	&r4   c                 6    |                      ddd           dS )zCEnsure that a PyObject* with a type with corrupt tp_name is handledr   z"set v->ob_type->tp_name=0xDEADBEEFr   r   Nr   r   s    r!   test_corrupt_tp_namez%PrettyPrintTests.test_corrupt_tp_name.  s/    < $ 	 	& 	& 	& 	& 	&r4   c                     t           j        j        r|                     d           |                     dd          \  }}t          j        d|          }|                     |d|z             dS )	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)rn   z%<_Helper\(\) at remote 0x-?[0-9a-f]+>zUnexpected rendering %rr|   N)rc   r:   no_siter   rz   r   r    r~   r   s       r!   test_builtins_helpz#PrettyPrintTests.test_builtins_help4  s     9 	FMMDEEE  $001HVZ0[[*H=xHH5@ 	 	B 	B 	B 	B 	Br4   c                     |                      d          \  }}|                     |d           |                      d          \  }}|                     |d           dS )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, [[...]]]Nr   r   s      r!   test_selfreferential_listz*PrettyPrintTests.test_selfreferential_listB  sp     CDD 	*#5666 MNN 	*#788888r4   c                 b    |                      d          \  }}|                     |d           dS )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': {...}}}Nr   r   s      r!   test_selfreferential_dictz*PrettyPrintTests.test_selfreferential_dictM  s>     MNN 	* 	#<=====r4   c                     |                      d          \  }}|                     t          j        d|          d|d|           d S )Nz:
class Foo:
    pass
foo = Foo()
foo.an_attr = foo
id(foo)1<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>r   ru   rz   r~   r   r    r   s      r!   'test_selfreferential_old_style_instancez8PrettyPrintTests.test_selfreferential_old_style_instanceU  sp        	* 	!U!)+ + + &XXzz3	4 	4 	4 	4 	4r4   c                 "   |                      d          \  }}|                     t          j        d|          d|d|           |                      d          \  }}|                     t          j        d|          d|d|           d S )NzB
class Foo(object):
    pass
foo = Foo()
foo.an_attr = foo
id(foo)r   r   ru   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   r   s      r!   'test_selfreferential_new_style_instancez8PrettyPrintTests.test_selfreferential_new_style_instanceb  s        	* 	!U!)+ + + &XXzz3	4 	4 	4  	 
 
 	* 	!~!)+ + + &XXzz3	4 	4 	4 	4 	4r4   c                     |                      d          \  }}|                     |d           |                     t          |          dt          d          z              dS )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)rz   r   lenr   s      r!   test_truncationz PrettyPrintTests.test_truncation}  sw    #001HII*7	8 	8 	8, 	X$4 5 55	7 	7 	7 	7 	7r4   c                     |                      d          \  }}|                     t          j        d|          d|d|           d S )Nz$import sys; id(sys.stdout.readlines)zO<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>r   ru   r   r   s      r!   test_builtin_methodz$PrettyPrintTests.test_builtin_method  sf    #001WXX*!s!)+ + + &XXzz3	4 	4 	4 	4 	4r4   c                     |                      dddg          }|                     t          j        d|t          j                  d|d|           d S )NzQ
import sys
def foo(a, b, c):
    return sys._getframe(0)

f = foo(3, 4, 5)
id(f)rV   zprint (PyFrameObject*)v)rl   rm   zK.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 4, in foo \(a=3.*r   ru   )rs   r~   r   r    rw   r   s     r!   test_frameszPrettyPrintTests.test_frames  s    )) +	 6BAZ@[ * 	, 	,
 	!o!+!#, , , DN::zzZ	\ 	\ 	\ 	\ 	\r4   r   ) r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r^   r4   r!   r   r   1  s       5 5 5? ? ? ?. . .! ! ![ [ [+ + +
; ; ;*! *! *!X2 2 2, , ,$	M 	M 	MC C C,
R 
R 
RR R RR R R2 2 2 2:* * *, , ,
& & && & && & &B B B	9 	9 	9> > >4 4 44 4 467 7 784 4 4\ \ \ \ \r4   r   &Python was compiled with optimizationsc                   &    e Zd Zd Zd Zd Zd ZdS )PyListTestsc                 2    |                      ||           d S r   )r   )ri   expectedr   s      r!   assertListingzPyListTests.assertListing  s    FH-----r4   c                     |                      |                                 dg          }|                     d|           dS )z'Verify that the "py-list" command workszpy-listrk   rm   z   5    
   6    def bar(a, b, c):
   7        baz(a, b, c)
   8    
   9    def baz(*args):
 >10        id(42)
  11    
  12    foo(1, 2, 3)
Nrs   r   r  ri   bts     r!   test_basic_commandzPyListTests.test_basic_command  s_    !!)?)?)A)A9B " E E 	 4 	 	 	 	 	r4   c                     |                      |                                 dg          }|                     d|           dS )z7Verify the "py-list" command with one absolute argumentz	py-list 9r
  zI   9    def baz(*args):
 >10        id(42)
  11    
  12    foo(1, 2, 3)
Nr  r  s     r!   test_one_abs_argzPyListTests.test_one_abs_arg  s_    !!)?)?)A)A9D " G G 	 4 		 	 	 	 	r4   c                     |                      |                                 dg          }|                     d|           dS )z8Verify the "py-list" command with two absolute argumentszpy-list 1,3r
  zR   1    # Sample script for use by test_gdb.py
   2    
   3    def foo(a, b, c):
Nr  r  s     r!   test_two_abs_argszPyListTests.test_two_abs_args  s_    !!)?)?)A)A9F " I I 	 9 	 	 	 	 	r4   N)r   r   r   r  r  r  r  r^   r4   r!   r  r    sP        . . .  	 	 	    r4   r  z

from _testcapi import pyobject_fastcall

def foo(a, b, c):
    bar(a, b, c)

def bar(a, b, c):
    pyobject_fastcall(baz, (a, b, c))

def baz(*args):
    id(42)

foo(1, 2, 3)

c                   Z   e Zd Z ej        ed           ej         e            d          d                         Z ej        ed          d             Z	 ej        ed          d             Z
 ej        ed           ej         e            d          d                         ZdS )StackNavigationTests$test requires py-up/py-down commandsr  c                 n    |                      t          ddg          }|                     |d           dS )z%Verify that the "py-up" command workspy-uprj   rm   z^.*
#[0-9]+ Frame 0x-?[0-9a-f]+, for file <string>, line 12, in baz \(args=\(1, 2, 3\)\)
#[0-9]+ <built-in method pyobject_fastcall of module object at remote 0x[0-9a-f]+>
$Nrs   SAMPLE_WITH_C_CALLr   r  s     r!   test_pyup_commandz&StackNavigationTests.test_pyup_command  sT    
 !!);9@'8J " L L##B%	 	 	 	 	r4   c                     |                      |                                 dg          }|                     |d           dS )z7Verify handling of "py-down" at the bottom of the stackpy-downr
  z$Unable to find a newer python frame
Nrs   r   r   r  s     r!   test_down_at_bottomz(StackNavigationTests.test_down_at_bottom  s_     !!)?)?)A)A9B " E EBC	E 	E 	E 	E 	Er4   c                     |                      |                                 dgdz            }|                     |d           dS )z2Verify handling of "py-up" at the top of the stackr  r   r
  z%Unable to find an older python frame
Nr  r  s     r!   test_up_at_topz#StackNavigationTests.test_up_at_top  sc     !!)?)?)A)A9@	A " G GBD	F 	F 	F 	F 	Fr4   c                 n    |                      t          g d          }|                     |d           dS )z$Verify "py-up" followed by "py-down")r  r  r  r  a  ^.*
#[0-9]+ Frame 0x-?[0-9a-f]+, for file <string>, line 12, in baz \(args=\(1, 2, 3\)\)
#[0-9]+ <built-in method pyobject_fastcall of module object at remote 0x[0-9a-f]+>
#[0-9]+ Frame 0x-?[0-9a-f]+, for file <string>, line 12, in baz \(args=\(1, 2, 3\)\)
$Nr  r  s     r!   test_up_then_downz&StackNavigationTests.test_up_then_down
  sU    
 !!);8U8U8U " W W##B%	 	 	 	 	r4   N)r   r   r   r   
skipUnlessHAS_PYUP_PYDOWNskipIfr   r  r  r!  r#  r^   r4   r!   r  r    s        X*PQQX_((**=? ? ? ? RQ X*PQQE E RQE X*PQQF F RQF X*PQQX_((**=? ?	 	? ? RQ	 	 	r4   r  c                      e Zd Z ej         e            d          d             Z ej         e            d          d             Z ej         e            d          d             Z ej         e            d          d             Z	 ej         e            d          d             Z
 ej         e            d          d             ZdS )		PyBtTestsr  c                     |                      |                                 dg          }|                     |d           dS )z%Verify that the "py-bt" command workspy-btr
  aL  ^.*
Traceback \(most recent call first\):
  <built-in method id of module object .*>
  File ".*gdb_sample.py", line 10, in baz
    id\(42\)
  File ".*gdb_sample.py", line 7, in bar
    baz\(a, b, c\)
  File ".*gdb_sample.py", line 4, in foo
    bar\(a=a, b=b, c=c\)
  File ".*gdb_sample.py", line 12, in <module>
    foo\(1, 2, 3\)
Nrs   r   r   r  s     r!   test_btzPyBtTests.test_bt  s\     !!)?)?)A)A9@	 " C C##B%	 	 	 	 	r4   c                     |                      |                                 dg          }|                     |d           dS )z*Verify that the "py-bt-full" command works
py-bt-fullr
  aD  ^.*
#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
    baz\(a, b, c\)
#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
    bar\(a=a, b=b, c=c\)
#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
    foo\(1, 2, 3\)
Nr+  r  s     r!   test_bt_fullzPyBtTests.test_bt_full-  s\     !!)?)?)A)A9E " H H##B%	 	 	 	 	r4   c                     d}|                      |dg          }|                     d|           |                      |dg          }|                     d|           dS )zBVerify that "py-bt" indicates threads that are waiting for the GILaN  
from threading import Thread

class TestThread(Thread):
    # These threads would run forever, but we'll interrupt things with the
    # debugger
    def run(self):
        i = 0
        while 1:
             i += 1

t = {}
for i in range(4):
   t[i] = TestThread()
   t[i].start()

# Trigger a breakpoint on the main thread
id(42)

zthread apply all py-btr   zWaiting for the GILzthread apply all py-bt-fullNrs   assertInri   r   ry   s      r!   test_threadszPyBtTests.test_threads=  s    * ))#AY@Z * \ \
+Z888 ))#A^@_ * a a
+Z88888r4   c                     d}|                      |g d          }|                     d|           |                      |g d          }|                     d|           dS )z?Verify that "py-bt" indicates if a thread is garbage-collectingzRfrom gc import collect
id(42)
def foo():
    collect()
def bar():
    foo()
bar()
)break update_refscontinuer*  r   zGarbage-collecting)r6  r7  r.  Nr1  r3  s      r!   test_gczPyBtTests.test_gc_  s     ))#@j@j@j * , ,
 	*J777 ))#@o@o@o * , ,
 	*J77777r4   c                    d}dD ]\  }}dD ]}|                      | d|           5  t          j        d| d| d| d          }|                     ||dd	gd
          }|                     d| |           |                     ||dgd
          }|}|t          j        d|           z  }|                     ||           ddd           n# 1 swxY w Y   ӌdS )zAVerify that "py-bt" displays invocations of PyCFunction instancesz#(1|2)))meth_varargsr*   )meth_varargs_keywordsr*   )meth_oz[])meth_noargsr*   )meth_fastcallr*   )meth_fastcall_keywordsr*   )	_testcapiz_testcapi.MethClassz_testcapi.MethClass()z_testcapi.MethStatic()r$   zi
                        import _testcapi
                        def foo():
                            (zy)
                        def bar():
                            foo()
                        bar()
                    r  r*  T)rl   rm   ro   z<built-in method r.  z <built-in method N)subTesttextwrapdedentrs   r2  r   escapeassertRegex)ri   expected_frame	func_namerJ   objr   ry   regexs           r!   test_pycfunctionzPyBtTests.test_pycfunctionz  s     # 
 0	8 0	8OIt (8 (8 \\S"6"69"6"677 8 8"/ + !+ + $-+ + 04+ + +  C "&!5!5#,/3Wo '+ "6 " "J MM"Ai"A"A:NNN "&!5!5#,/;n '+ "6 " "J +ERY'GI'G'GHHHE$$Z777?8 8 8 8 8 8 8 8 8 8 8 8 8 8 8(80	8 0	8s   BCCCc                     t          j        d          }ddg}t          r|                    d           |                    d           |                     ||          }|                     |d           d S )Nz
            class MyList(list):
                def __init__(self):
                    super().__init__()   # wrapper_call()

            id("first break point")
            l = MyList()
        zbreak wrapper_callr7  r[   r*  r   z1<method-wrapper u?'__init__' of MyList object at )rC  rD  rb   appendrs   rF  )ri   r   rm   ry   s       r!   test_wrapper_callzPyBtTests.test_wrapper_call  s     o    "6z B 	1 "((000$$W--- ))#@U * W W
M	O 	O 	O 	O 	Or4   N)r   r   r   r   r&  r   r,  r/  r4  r8  rK  rN  r^   r4   r!   r(  r(    s{       X_((**=? ? ? ?$ X_((**=? ? ? ? X_((**=? ?9 9? ?9@ X_((**=? ?
8 8? ?
8, X_((**=? ?88 88? ?88t X_((**=? ?O O? ?O O Or4   r(  c                   L   e Zd Z ej         e            d          d             Z ej         e            d           ej        ed          d                         Z	 ej         e            d          d             Z
 ej         e            d          d             ZdS )PyPrintTestsr  c                 n    |                      t          ddg          }|                     |d           dS )z(Verify that the "py-print" command worksr  zpy-print argsr  z".*\nlocal 'args' = \(1, 2, 3\)\n.*Nr  r  s     r!   r  zPyPrintTests.test_basic_command  sW     !!);9@/8R " T T##B$I	K 	K 	K 	K 	Kr4   r  c                 n    |                      t          g d          }|                     |d           d S )N)r  r  z
py-print cz
py-print bz
py-print ar  z3.*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*r  r  s     r!   test_print_after_upz PyPrintTests.test_print_after_up  sX     !!);8t8t8t " v v##B$Z	\ 	\ 	\ 	\ 	\r4   c                     |                      |                                 ddg          }|                     |d           d S )Nr  zpy-print __name__r
  z&.*\nglobal '__name__' = '__main__'\n.*r+  r  s     r!   test_printing_globalz!PyPrintTests.test_printing_global  sc     !!)?)?)A)A9@BU8V " X X##B$M	O 	O 	O 	O 	Or4   c                     |                      |                                 ddg          }|                     |d           d S )Nr  zpy-print lenr
  zV.*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*r+  r  s     r!   test_printing_builtinz"PyPrintTests.test_printing_builtin  sb     !!)?)?)A)A9@.8Q " S S##B$}	 	 	 	 	r4   N)r   r   r   r   r&  r   r  r$  r%  rS  rU  rW  r^   r4   r!   rP  rP    s(       X_((**=? ?K K? ?K X_((**=? ?X*PQQ\ \ RQ? ?\ X_((**=? ?O O? ?O X_((**=? ? ? ?  r4   rP  c                       e Zd Z ej         e            d          d             Z ej        ed           ej         e            d          d                         Z	dS )PyLocalsTestsr  c                     |                      |                                 ddg          }|                     |d           d S )Nr  	py-localsr
  z.*\nargs = \(1, 2, 3\)\n.*r+  r  s     r!   r  z PyLocalsTests.test_basic_command  sb     !!)?)?)A)A9@+8N " P P##B$A	C 	C 	C 	C 	Cr4   r  c                     |                      |                                 g d          }|                     |d           d S )N)r  r  r[  r
  z<^.*
Locals for foo
a = 1
b = 2
c = 3
Locals for <module>
.*$r+  r  s     r!   test_locals_after_upz"PyLocalsTests.test_locals_after_up  s`     !!)?)?)A)A8W8W8W " Y Y##B%	 	 	 	 	r4   N)
r   r   r   r   r&  r   r  r$  r%  r]  r^   r4   r!   rY  rY    s        X_((**=? ?C C? ?C X*PQQX_((**=? ?
 
? ? RQ
 
 
r4   rY  c                      t           j        rNt          dt          dt          d           t
                                          D ]} t          d| z              d S d S )NzGDB version r$   :z    )r   verboserh   rF   rG   gdb_versionrg   )rq   s    r!   setUpModulerb    ss     "&7&7&79J9J9JKLLL**,, 	" 	"D'D.!!!!" "	" 	"r4   __main__)4rB   platformr   r   rc   r5   rC  r   testr   test.supportr   r   r"   ra  rF   rG   r   is_python_buildpython_compilerr6   pathr   dirnamerd   rH   r`   r;   rb   rO   gdbpy_versionrR   gdbpy_errorsr}   r   rU   r%  rv   r&  PGOTestCaserX   r   r  r  r  r(  rP  rY  rb  r   mainr^   r4   r!   <module>rp     s   
			  				     



             6 6 6 6 6 6 6 6? ? ?< 5DO4E4E 1 1q
(

0002C2C2C*{, - - -
 !y "" S
(
Q
R
RR
&h&((((S\X-E-E
(
 5 6 6 6 Y122;eY.//527 7
(
D
E
EE W\\"'//#."A"A"13 3  
( 
( 
(  !!J J J6 7VWW q J
(
H
I
II
(
5
6
66 '(CN33<#|33
?C
(
C,"5"5"7"77
8
88' ' ' '&((233_) _) _) _) _)H% _) _) 43_)B}\ }\ }\ }\ }\} }\ }\ }\~ $$&&9; ;& & & & &- & &; ;&P $* * * * *= * * *X~O ~O ~O ~O ~O ~O ~O ~O@! ! ! ! != ! ! !F    M   2" " " zHMOOOOO r4   