
    5i                         d Z ddlZddlmZ ddlm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mZmZ defdZd Zd	d
dZy)z
Private utilities for testing
    N)NativeResource)join_all_native_threads)ClientBootstrapDefaultHostResolverEventLoopGroupreturnc                  *    t        j                         S )a  
    Returns number of bytes currently allocated by awscrt's native code.

    `AWS_CRT_MEMORY_TRACING `environment variable must be set before module
    is loaded, or 0 will always be returned. Legal values are:

    *   `AWS_CRT_MEMORY_TRACING=0`: No tracing

    *   `AWS_CRT_MEMORY_TRACING=1`: Only track allocation sizes and total allocated

    *   `AWS_CRT_MEMORY_TRACING=2`: Capture callstacks for each allocation
    )_awscrtnative_memory_usage     n/home/marpiech/ifpan-abm-pgxpred/analysis/marpiech-gwas-test/venv/lib/python3.12/site-packages/awscrt/_test.pyr   r      s     &&((r   c                  *    t        j                         S )a4  
    If there are outstanding allocations from awscrt's native code, dump them
    to log, along with any information gathered based on the tracing level.

    In order to see the dump, logging must initialized at `LogLevel.Trace`
    and the `AWS_CRT_MEMORY_TRACING` environment variable must be non-zero
    when module is loaded. Legal values are:

    *   `AWS_CRT_MEMORY_TRACING=0`: No tracing

    *   `AWS_CRT_MEMORY_TRACING=1`: Only track allocation sizes and total allocated

    *   `AWS_CRT_MEMORY_TRACING=2`: Capture callstacks for each allocation
    )r
   native_memory_dumpr   r   r   dump_native_memoryr   !   s     %%''r   g      $@timeout_secc                    t        j                          t        j                          t        j                          t	        j
                  d      dk7  rt        d      t        j                  st        d      t        j                         | z   }t        j                         |k  rkt        j                  st               dkD  syt        j                          t        d       t        j                  d       t        j                         |k  rkt!        t        j                        }|rmt	        j
                  d	      d
k(  rt#        d       nt#        d       fd}t        j                  D ]#  } |d|       t%        j&                  |      dz
  }g }t        j(                  |      D ]}  }t+        |t,        j.                        rPt1        j2                  |      }|j4                  j7                  d      xs |j4                  j7                  d      }	|	rm|j9                  |        t#        d|       t#        dt!        |             |D ]B  }t+        |t,        j.                        r |dt1        j2                  |             : |d|       D & t               }
|
dkD  r$t#        dj;                  |
             t=                t        dj;                  ||
            )a  
    Checks that all awscrt resources have been freed after a test.

    If any resources still exist, debugging info is printed and an exception is raised.

    Requirements:
        * `awscrt.NativeResource._track_lifetime = True`: must be set before test begins
            to ensure accurate tracking.

        * `AWS_CRT_MEMORY_TRACING=2`: environment variable that must be set before
            any awscrt modules are imported, to ensure accurate native leak checks.

        * `AWS_CRT_MEMORY_PRINT_SECRETS_OK=1`: optional environment variable that
            will print the full contents of leaked python objects. DO NOT SET THIS
            if the test results will be made public as it may result in secrets
            being leaked.
    AWS_CRT_MEMORY_TRACING2zRenvironment variable AWS_CRT_MEMORY_TRACING=2 must be set for accurate leak checkszOawscrt.NativeResource._track_lifetime=True must be set for accurate leak checksr   Ng?r   AWS_CRT_MEMORY_PRINT_SECRETS_OK1zLeaking NativeResources:zaLeaking NativeResources (set AWS_CRT_MEMORY_PRINT_SECRETS_OK=1 env var for more detailed report):c                     r.t        |      }t        |      dkD  r|d d dz   }t        | |       y t        | t        |             y )Ni  z...TRUNCATED PRINT)strlenprinttype)prefixobjsleak_secrets_oks      r   	_printobjz"check_for_leaks.<locals>._printobjc   sC    Hq6D=%4#77Afa fd3i(r   -   z_weakrefset.pyzawscrt/_test.pyz  sys.getrefcount():z  gc.referrers():z  -zALeaking {} bytes native memory (enable Trace logging to see more)zKawscrt leak check failed. {} NativeResource objects. {} bytes native memory)r   release_static_defaultr   r   osgetenvRuntimeErrorr   _track_lifetimetime_livingr   gccollectr   sleepr   r   sysgetrefcountget_referrers
isinstancetypes	FrameTypeinspectgetframeinfofilenameendswithappendformatr   )r   
wait_untilnum_living_resourcesr"   irefcount	referrersr	frameinfo	our_fault	mem_bytesr!   s              @r   check_for_leaksrD   3   sf   $ **,))+..0	yy)*c1opp))lmm {*J
))+

"%%.A.Ca.G


C0

3 ))+

" ~556))$EF#M,-uv	)  '' 	(Ac1 q)A-H I%%a( $a1 ' 4 4Q 7I!*!3!3!<!<=M!N "P!*!3!3!<!<=N!O     #$ ((3%s9~6 (a1eW%9%9!%<=eQ'	(5	(@ $%I1}QXXYbcd
dkki) * *r   )__doc__r
   awscrtr   awscrt.commonr   r,   r5   r&   r/   r*   r3   	awscrt.ior   r   r   intr   r   rD   r   r   r   <module>rJ      sI     ! 1 	  	 
   J J)S ) ($ $( `*r   