+
    Biu                     X   ^ RI t ^ RIt^ RIt^ RIHt ^ RIHtHt ^ RIH	t	H
t
 ]P                  ! R4      t]P                  ! R4      t]! 4        ! R R]P                  4      4       t]P                   ! 4        ! R	 R
]4      4       t]P$                  ! ]P&                  ! 4       R4       ! R R]4      4       t]P$                  ! ]P*                  ! 4       R4       ! R R]4      4       t]P$                  ! ]P.                  R4       ! R R]4      4       t]R8X  d   ]P4                  ! 4        R# R# )    N)support)import_helperrequires_subprocess)assert_python_failureassert_python_ok	_testcapi_testinternalcapic                      a  ] tR t^t o RtRtR tR tR tR t	R t
R tR	 tR
 tR tR tR t]P$                  ! ]P(                  R4      R 4       tRtV tR# )PyMemDebugTestsdebugz(?:0x)?[0-9a-fA-F]+c           	         \         P                  ! 4       ;_uu_ 4        \        R VV P                  RR7      pRRR4       XP                  pVP                  RR4      #   + '       g   i     L.; i)-c
junk:falsePYTHONMALLOCMALLOC_CONFNasciireplace)r   SuppressCrashReportr   r   errdecode)selfcodeoutstderrs   &&  ./usr/lib/python3.14/test/test_capi/test_mem.pycheckPyMemDebugTests.check   sX    ((**'d!.. )C + }}Wi00 +*s   AA.	c                    V P                  R 4      pRpVP                  V P                  R7      p\        P                  ! V\        P
                  R7      pV P                  W4       R# )z3import _testcapi; _testcapi.pymem_buffer_overflow()a6  Debug memory block at address p={ptr}: API 'm'\n    16 bytes originally requested\n    The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n    The [0-9] pad bytes at tail={ptr} are not all FORBIDDENBYTE \(0x[0-9a-f]{{2}}\):\n        at tail\+0: 0x78 \*\*\* OUCH\n        at tail\+1: 0xfd\n        at tail\+2: 0xfd\n        .*\n(    The block was made by call #[0-9]+ to debug malloc/realloc.\n)?    Data at p: cd cd cd .*\n\nEnable tracemalloc to get the memory block allocation traceback\n\nFatal Python error: _PyMem_DebugRawFree: bad trailing pad byteptr)flagsN)r   format	PTR_REGEXrecompileDOTALLassertRegexr   r   regexs   &  r   test_buffer_overflow$PyMemDebugTests.test_buffer_overflow"   sQ    jjNOS 0

5		2$    c                    V P                  R 4      pRpVP                  V P                  R7      pV P                  W4       R# )z.import _testcapi; _testcapi.pymem_api_misuse()a  Debug memory block at address p={ptr}: API 'm'\n    16 bytes originally requested\n    The [0-9] pad bytes at p-[0-9] are FORBIDDENBYTE, as expected.\n    The [0-9] pad bytes at tail={ptr} are FORBIDDENBYTE, as expected.\n(    The block was made by call #[0-9]+ to debug malloc/realloc.\n)?    Data at p: cd cd cd .*\n\nEnable tracemalloc to get the memory block allocation traceback\n\nFatal Python error: _PyMem_DebugRawFree: bad ID: Allocated using API 'm', verified using API 'r'\nr    N)r   r#   r$   r(   r)   s   &  r   test_api_misusePyMemDebugTests.test_api_misuse6   s=    jjIJ	w 0$r-   c                    V P                  V4      p\        P                  '       g   R pMRpV P                  W24       R# )z^Fatal Python error: _PyMem_DebugMalloc: Python memory allocator called without holding the GILzFatal Python error: _PyMem_DebugMalloc: Python memory allocator called without an active thread state. Are you trying to call it inside of a Py_BEGIN_ALLOW_THREADS block?N)r   r   Py_GIL_DISABLEDassertIn)r   r   r   expecteds   &&  r   check_malloc_without_gil(PyMemDebugTests.check_malloc_without_gilE   s:    jj&&&QH^H 	h$r-   c                ,    R pV P                  V4       R# )z6import _testcapi; _testcapi.pymem_malloc_without_gil()Nr5   r   r   s   & r   test_pymem_malloc_without_gil-PyMemDebugTests.test_pymem_malloc_without_gilP   s     H%%d+r-   c                ,    R pV P                  V4       R# )z9import _testcapi; _testcapi.pyobject_malloc_without_gil()Nr8   r9   s   & r    test_pyobject_malloc_without_gil0PyMemDebugTests.test_pyobject_malloc_without_gilV   s     K%%d+r-   c                l    \         P                  ! R V R24      p\        RVV P                  RR7       R# )z
            import gc, os, sys, _testinternalcapi
            # Disable the GC to avoid crash on GC collection
            gc.disable()
            _testinternalcapi.z()
            # Exit immediately to avoid a crash while deallocating
            # the invalid object
            os._exit(0)
        r   r   r   N)textwrapdedentr   r   )r   	func_namer   s   && r   check_pyobject_is_freed'PyMemDebugTests.check_pyobject_is_freed\   sA     $  )k *		   	$**$	
r-   c                (    V P                  R 4       R# )check_pyobject_null_is_freedNrC   r   s   &r   test_pyobject_null_is_freed+PyMemDebugTests.test_pyobject_null_is_freedl   s    $$%CDr-   c                (    V P                  R 4       R# )%check_pyobject_uninitialized_is_freedNrG   rH   s   &r   $test_pyobject_uninitialized_is_freed4PyMemDebugTests.test_pyobject_uninitialized_is_freedo   s    $$%LMr-   c                (    V P                  R 4       R# )'check_pyobject_forbidden_bytes_is_freedNrG   rH   s   &r   &test_pyobject_forbidden_bytes_is_freed6PyMemDebugTests.test_pyobject_forbidden_bytes_is_freedr   s    $$%NOr-   c                (    V P                  R 4       R# )check_pyobject_freed_is_freedNrG   rH   s   &r   test_pyobject_freed_is_freed,PyMemDebugTests.test_pyobject_freed_is_freedu   s    $$%DEr-   zcannot test Py_TRACE_REFS buildc                @   R p\        RV4      w  r#pVP                  4       p\        V^4       Fl  w  rgV P                  RV4       VP	                  R4      Ev r\        V	4      p	V P                  W^
,          4       V P                  W^
,          ^,
          4       Kn  	  R# )a*  if 1:
            import _testcapi

            class C(): pass

            # The first loop tests both functions and that remove_mem_hooks()
            # can be called twice in a row. The second loop checks a call to
            # set_nomemory() after a call to remove_mem_hooks(). The third
            # loop checks the start and stop arguments of set_nomemory().
            for outer_cnt in range(1, 4):
                start = 10 * outer_cnt
                for j in range(100):
                    if j == 0:
                        if outer_cnt != 3:
                            _testcapi.set_nomemory(start)
                        else:
                            _testcapi.set_nomemory(start, start + 1)
                    try:
                        C()
                    except MemoryError as e:
                        if outer_cnt != 3:
                            _testcapi.remove_mem_hooks()
                        print('MemoryError', outer_cnt, j)
                        _testcapi.remove_mem_hooks()
                        break
        r   s   MemoryError    N)r   
splitlines	enumerater3   splitintassertLessEqualassertGreaterEqual)
r   r   rcr   r   linesiline_counts
   &         r   test_set_nomemory!PyMemDebugTests.test_set_nomemoryz   s    4 (d3  *GAMM.#.

4(IQJE  "-##ER462 +r-    N)__name__
__module____qualname____firstlineno__r   r$   r   r+   r/   r5   r:   r=   rC   rI   rM   rQ   rU   unittestskipIfr   Py_TRACE_REFSre   __static_attributes____classdictcell__)__classdict__s   @r   r   r      sq     L&I
1%(%	%,,
 ENPF
 __W**,MN"3 O"3r-   r   c                       ] tR t^tRtRtR# )PyMemMallocDebugTestsmalloc_debugrg   Nrh   ri   rj   rk   r   ro   rg   r-   r   rs   rs      s    !Lr-   rs   zneed pymallocc                       ] tR t^tRtRtR# )PyMemPymallocDebugTestspymalloc_debugrg   Nru   rg   r-   r   rw   rw          #Lr-   rw   zneed mimalocc                       ] tR t^tRtRtR# )PyMemMimallocDebugTestsmimalloc_debugrg   Nru   rg   r-   r   r{   r{      ry   r-   r{   zneed Py_DEBUGc                       ] tR t^tRtRtR# )PyMemDefaultTests rg   Nru   rg   r-   r   r~   r~      s	     Lr-   r~   __main__)r%   r@   rl   testr   test.supportr   r   test.support.script_helperr   r   import_moduler   r	   TestCaser   requires_gil_enabledrs   
skipUnlesswith_pymallocrw   with_mimallocr{   Py_DEBUGr~   rh   mainrg   r-   r   <module>r      s/   	    ; N
 ''4	!//0CD L3h'' L3 L3` 	"O "  " 
W**,o>$o $ ?$ 
W**,n=$o $ >$ 
W%%7  8
 zMMO r-   