
    4j.1                     L   S SK r S SKrS SKJr  S SKrS SKJrJrJr  S SKJrJ	r	J
r
  S SKJrJr   " S S5      r " S S	\5      r " S
 S\5      r\R"                  " \ R$                  S:  S5       " S S\5      5       r\R(                  " \" \ S5      S5       " S S\5      5       rg)    N)TestCase)encoderdecoderscanner)PY3	long_typeb)has_speedupsskip_if_speedups_missingc                       \ rS rSrS r\rSrg)BadBool   c                     SS-    g N   r    selfs    ]/root/lseg-credit/backend/venv/lib/python3.13/site-packages/simplejson/tests/test_speedups.py__bool__BadBool.__bool__   s	    	!    r   N__name__
__module____qualname____firstlineno__r   __nonzero____static_attributes__r   r   r   r   r      s    Kr   r   c                   4    \ rS rSr\S 5       r\S 5       rSrg)
TestDecode   c                 N    U R                  [        [        R                  S5        g )Nr   )assertRaisesAttributeErrorr   c_make_scannerr   s    r   test_make_scannerTestDecode.test_make_scanner   s    .'*@*@!Dr   c                     S nU R                  [        US5        U R                  [        US5        [        (       d/  U R                  [        US5        U R                  [        US5        g g )Nc                 Z    [         R                  " [        5       S9R                  U 5        g )Nstrict)r   JSONDecoderr   decode)values    r   test+TestDecode.test_bad_bool_args.<locals>.test   s    wy188?r   z""{})r$   ZeroDivisionErrorr   r   r0   s     r   test_bad_bool_argsTestDecode.test_bad_bool_args   s^    	@+T48+T48s/u=/u= r   r   N)r   r   r   r   r   r'   r5   r   r   r   r   r!   r!      s*    E E > >r   r!   c                   x    \ rS rSr\S 5       r\S 5       r\S 5       r\S 5       r\	(       a  \S 5       r
SrgSrg)	
TestEncode    c                 R    U R                  [        [        R                  S SS 5        g )Nu   Í}=NLùy×Rºò'J} Êu)r$   	TypeErrorr   c_make_encoderr   s    r   test_make_encoderTestEncode.test_make_encoder!   s'    ""8	
r   c                 |   SS K nS n[        R                  " S S US SSSSS0 SSSS S SSSUR                  S5      nU R	                  [
        USS	5        U R	                  [
        USS
0S	5        S n[        R                  " S S US SSSSS0 SSSS S SSSUR                  S5      nU R	                  [        USS	5        g )Nr   c                      g Nr   argss    r   bad_encoder15TestEncode.test_bad_str_encoder.<locals>.bad_encoder11   s    r   c                     [        U 5      $ rA   strobjs    r   <lambda>1TestEncode.test_bad_str_encoder.<locals>.<lambda>4       #c(r   z: z, Fzutf-8spam   *   c                      SS-    g r   r   rB   s    r   bad_encoder25TestEncode.test_bad_str_encoder.<locals>.bad_encoder2;   s	    aCr   c                     [        U 5      $ rA   rG   rI   s    r   rK   rL   >   rM   r   )decimalr   r<   Decimalr$   r;   r3   )r   rU   rD   encrR   s        r   test_bad_str_encoderTestEncode.test_bad_str_encoder,   s     		$$*dD$ueRudGUE7??E	K
 	)S&!4)S62,:	$$*dD$ueRudGUE7??E	K
 	+S&!<r   c                 2   S nU R                  [        US5        U R                  [        US5        U R                  [        US5        U R                  [        US5        U R                  [        US5        U R                  [        US5        U R                  [        US5        U R                  [        US	5        U R                  [        US
5        U R                  [        US5        U R                  [        US5        U R                  [        US5        g )Nc                 b    [         R                  " S0 U [        5       0D6R                  0 5        g )Nr   )r   JSONEncoderr   encode)names    r   r0   +TestEncode.test_bad_bool_args.<locals>.testF   s%    44"34;;B?r   skipkeysensure_asciicheck_circular	allow_nan	sort_keysuse_decimalnamedtuple_as_objecttuple_as_arraybigint_as_stringfor_json
ignore_naniterable_as_array)r$   r3   r4   s     r   r5   TestEncode.test_bad_bool_argsD   s    	@+T:>+T>B+T3CD+T;?+T;?+T=A+T3IJ+T3CD+T3EF+T:>+T<@+T3FGr   c                 l   ^ [        S5      S-  S-   mU4S jnU R                  [        [        4U5        g )N   r9      c                  L   > [         R                  " T S9R                  S5        g )Nint_as_string_bitcountr   )r   r\   r]   )
long_counts   r   r0   =TestEncode.test_int_as_string_bitcount_overflow.<locals>.testX   s    zBII!Lr   )r   r$   r;   OverflowError)r   r0   rs   s     @r   $test_int_as_string_bitcount_overflow/TestEncode.test_int_as_string_bitcount_overflowU   s1    q\2%b(
	M9m4d;r   c                     U R                  [        5         [        R                  " SS9R	                  [        S5      S05        S S S 5        g ! , (       d  f       g = f)Nu   )encodingkey{   )r$   UnicodeEncodeErrorr   r\   r]   r	   r   s    r   test_bad_encodingTestEncode.test_bad_encoding]   s>    ""#56##X6==qxoN 766s   /A
Ar   N)r   r   r   r   r   r=   rX   r5   rv   r   r}   r   r   r   r   r8   r8       sp    
 
 = =. H H  < < 	!	O 
"	O r   r8   )      zheap types require Python 3.13+c                   x    \ rS rSrSr\S 5       r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       rS	rg
)TestHeapTypesc   z?Verify that Scanner and Encoder are heap types on Python 3.13+.c                 N    SSK Jn  U R                  UR                  S-  S5        g )Nr   make_scanner   z&Scanner should be a heap type on 3.13+)simplejson._speedupsr   
assertTrue	__flags__)r   r   s     r   test_scanner_is_heap_type'TestHeapTypes.test_scanner_is_heap_typeh   s"    5..&9@	Br   c                 N    SSK Jn  U R                  UR                  S-  S5        g )Nr   make_encoderr   z&Encoder should be a heap type on 3.13+)r   r   r   r   )r   r   s     r   test_encoder_is_heap_type'TestHeapTypes.test_encoder_is_heap_typeo   s"    5..&9@	Br   c                 X    SSK nSSKJn  U R                  UR	                  U5      5        g)z7Heap types must be GC-tracked so they can be collected.r   Nr   )gcr   r   r   
is_tracked)r   r   r   s      r   test_scanner_type_is_gc_tracked-TestHeapTypes.test_scanner_type_is_gc_trackedu   s     	5l34r   c                 X    SS K nSSKJn  U R                  UR	                  U5      5        g )Nr   r   )r   r   r   r   r   )r   r   r   s      r   test_encoder_type_is_gc_tracked-TestHeapTypes.test_encoder_type_is_gc_tracked|   s    5l34r   c                 X    [         R                  " S5      nU R                  USS05        g)z4Verify Scanner heap type instances decode correctly.{"a": 1}ar   N)
simplejsonloadsassertEqualr   results     r   test_scanner_instances_work)TestHeapTypes.test_scanner_instances_work   s(     !!*-#q*r   c                 V    [         R                  " SS0SS9nU R                  US5        g)z4Verify Encoder heap type instances encode correctly.r   r   Trd   r   N)r   dumpsr   r   s     r   test_encoder_instances_work)TestHeapTypes.test_encoder_instances_work   s*     !!3(d;,r   r   N)r   r   r   r   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r   c   s     JB B B B
 5 5 5 5
 + +
 - -r   r   gettotalrefcountz+debug build required (sys.gettotalrefcount)c                      \ rS rSrSrSrSrS r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       rSrg)TestRefcountLeaks   zCatch refcount leaks in the C extension.

These tests only run on debug builds of CPython, which expose
sys.gettotalrefcount(). On release builds they skip silently.
i     c                 &   SSK n[        U R                  5       H  nU" 5         UR                  5         M     [        R
                  " 5       n[        U R                  5       H  nU" 5         UR                  5         M     [        R
                  " 5       n[        U R                  5       H  nU" 5         UR                  5         M     [        R
                  " 5       nXT-
  nXe-
  nSXxU R                  4-  n	U R                  [        U5      SU	5        g)a  Run `func` in two measurement phases and verify the second
phase's refcount delta stays near zero.

A real per-call leak (1 ref per call) grows linearly with the
iteration count, so both phase1 and phase2 would be ~ITER. But
front-loaded noise -- specializer inline caches, dict resize,
gc generation bumps, etc. -- shows up entirely in phase1 and
leaves phase2 near zero. Asserting on phase2 only is thus both
more sensitive (catches smaller linear leaks) and more robust
(no false positives from CPython internals).
r   Nzjphase1=%d, phase2=%d, iterations=%d. A real per-call leak would make phase2 grow linearly with iterations.d   )	r   rangeWARMUPcollectsysr   ITER
assertLessabs)
r   funcr   _startmidendphase1phase2msgs
             r   _assert_no_leak!TestRefcountLeaks._assert_no_leak   s     	t{{#AFJJL $ $$&tyy!AFJJL " ""$tyy!AFJJL " ""$G$)),- 	FS#.r   c                 B   ^ / SQSS SS.mU R                  U4S j5        g )N)r   rn   r   helloT)r   r	   cdc                  0   > [         R                  " T 5      $ rA   r   r   datas   r   rK   6TestRefcountLeaks.test_dumps_no_leak.<locals>.<lambda>       Z%5%5d%;r   r   r   r   s    @r   test_dumps_no_leak$TestRefcountLeaks.test_dumps_no_leak   s     W4dC;<r   c                 4   ^ SmU R                  U4S j5        g )Nz4{"a": [1, 2, 3], "b": "hello", "c": null, "d": true}c                  0   > [         R                  " T 5      $ rA   )r   r   )raws   r   rK   6TestRefcountLeaks.test_loads_no_leak.<locals>.<lambda>   s    Z%5%5c%:r   r   )r   r   s    @r   test_loads_no_leak$TestRefcountLeaks.test_loads_no_leak   s    D:;r   c                 (    U R                  S 5        g )Nc                  ,    [         R                  " 5       $ rA   )r   r-   r   r   r   rK   ETestRefcountLeaks.test_scanner_construction_no_leak.<locals>.<lambda>       Z%;%;%=r   r   r   s    r   !test_scanner_construction_no_leak3TestRefcountLeaks.test_scanner_construction_no_leak       =>r   c                 (    U R                  S 5        g )Nc                  ,    [         R                  " 5       $ rA   )r   r\   r   r   r   rK   ETestRefcountLeaks.test_encoder_construction_no_leak.<locals>.<lambda>   r   r   r   r   s    r   !test_encoder_construction_no_leak3TestRefcountLeaks.test_encoder_construction_no_leak   r   r   c                 v   ^  " S S5      mU4S jnU4S jnU R                  U5        U R                  U5        g)z>Error path in scanner_new/encoder_new must release module_ref.c                       \ rS rSrS r\rSrg)CTestRefcountLeaks.test_failed_construction_no_leak.<locals>.BadBool   c                     [        5       erA   )r3   r   s    r   r   LTestRefcountLeaks.test_failed_construction_no_leak.<locals>.BadBool.__bool__   s    '))r   r   Nr   r   r   r   r   r      s    *"Kr   r   c                  x   >  [         R                  " T " 5       S9R                  S5        g ! [         a     g f = f)Nr+   r2   )r   r-   r.   r3   r   s   r   try_bad_scannerKTestRefcountLeaks.test_failed_construction_no_leak.<locals>.try_bad_scanner   s5    ##795<<TB$    (, 
99c                  x   >  [         R                  " T " 5       S9R                  0 5        g ! [         a     g f = f)Nr`   )r   r\   r]   r3   r   s   r   try_bad_encoderKTestRefcountLeaks.test_failed_construction_no_leak.<locals>.try_bad_encoder   s5    ##WY7>>rB$ r   Nr   )r   r   r   r   s      @r    test_failed_construction_no_leak2TestRefcountLeaks.test_failed_construction_no_leak   s5    	# 	#
		 	_-_-r   c                 ,    S nU R                  U5        g)zeValueError mid-encode must not leak the partial accumulator,
markers dict entry, or the ident PyLong.c                  ^    0 n X S'    [         R                  " U 5        g ! [         a     g f = f)Nr   )r   r   
ValueError)r   s    r   circularCTestRefcountLeaks.test_circular_reference_no_leak.<locals>.circular   s3    AfI  # s    
,,Nr   )r   r   s     r   test_circular_reference_no_leak1TestRefcountLeaks.test_circular_reference_no_leak   s    	 	X&r   c                 H   ^  " S S5      mU4S jnU R                  U5        g)zqencoder_steal_encode's TypeError path on _asdict() returning
a non-dict must release the stolen newobj reference.c                       \ rS rSrS rSrg)GTestRefcountLeaks.test_asdict_returning_non_dict_no_leak.<locals>.BadNT   c                     g)Nz
not a dictr   r   s    r   _asdictOTestRefcountLeaks.test_asdict_returning_non_dict_no_leak.<locals>.BadNT._asdict   s    #r   r   N)r   r   r   r   r   r   r   r   r   BadNTr      s    $r   r  c                  \   >  [         R                  " T " 5       SS9  g ! [         a     g f = f)NT)rf   )r   r   r;   )r  s   r   
bad_asdictLTestRefcountLeaks.test_asdict_returning_non_dict_no_leak.<locals>.bad_asdict  s,      tD     
++Nr   )r   r  r  s     @r   &test_asdict_returning_non_dict_no_leak8TestRefcountLeaks.test_asdict_returning_non_dict_no_leak   s"    	$ 	$	
 	Z(r   c                 H   ^  " S S5      mU4S jnU R                  U5        g)zafor_json() raising inside its body must not leak the method
binding or partial accumulator state.c                       \ rS rSrS rSrg)ATestRefcountLeaks.test_for_json_raising_no_leak.<locals>.Explodesi  c                     [        S5      e)Nboom)RuntimeErrorr   s    r   ri   JTestRefcountLeaks.test_for_json_raising_no_leak.<locals>.Explodes.for_json  s    "6**r   r   N)r   r   r   r   ri   r   r   r   r   Explodesr
    s    +r   r  c                  \   >  [         R                  " T " 5       SS9  g ! [         a     g f = f)NT)ri   )r   r   r  )r  s   r   explode@TestRefcountLeaks.test_for_json_raising_no_leak.<locals>.explode  s,      d; r  Nr   )r   r  r  s     @r   test_for_json_raising_no_leak/TestRefcountLeaks.test_for_json_raising_no_leak  s"    	+ 	+	
 	W%r   c                 @   ^ SSSSSS.mU R                  U4S j5        g)	zDict keys that aren't already strings go through
encoder_stringify_key and the non-cached branch of the key_memo
logic. Both paths must release the transient stringified key.r   r	   r   xy)r   rn   r   TFc                  .   > [         R                  " T SS9$ NTr   r   r   s   r   rK   ETestRefcountLeaks.test_non_string_dict_keys_no_leak.<locals>.<lambda>  s    Z%5%5dd%Kr   Nr   r   s    @r   !test_non_string_dict_keys_no_leak3TestRefcountLeaks.test_non_string_dict_keys_no_leak  s$    
 33c#>KLr   c                 4   ^ SmU R                  U4S j5        g)zmaybe_quote_bigint's comparison path must release `encoded`
on the RichCompareBool error branch and on the quoted-return
path that replaces the unquoted string.l        c                  .   > [         R                  " T SS9$ )Nro   rq   r   )bigs   r   rK   ATestRefcountLeaks.test_bigint_as_string_no_leak.<locals>.<lambda>&  s    J$$SDr   Nr   )r   r  s    @r   test_bigint_as_string_no_leak/TestRefcountLeaks.test_bigint_as_string_no_leak  s    
 D	Fr   c                 B   ^ SSS/SSS.mU R                  U4S j5        g)zlThe PyDict_Next fast path (unsorted exact dict) must not leak
references compared to the iterator slow path.r   twor   NT)r   r	   r   r   ec                  0   > [         R                  " T 5      $ rA   r   r   s   r   rK   ?TestRefcountLeaks.test_dict_fast_path_no_leak.<locals>.<lambda>-  r   r   r   r   s    @r   test_dict_fast_path_no_leak-TestRefcountLeaks.test_dict_fast_path_no_leak(  s&     U!4dC;<r   c                 <   ^ SSSS.mU R                  U4S j5        g)z?The iterator slow path (sorted or dict subclass) must not leak.r   rn   r   )zr   mc                  .   > [         R                  " T SS9$ r  r   r   s   r   rK   ?TestRefcountLeaks.test_dict_slow_path_no_leak.<locals>.<lambda>4  s    J$$TT:r   Nr   r   s    @r   test_dict_slow_path_no_leak-TestRefcountLeaks.test_dict_slow_path_no_leak/  s"     QQ':	<r   c                 >   ^ SSSSS.mU R                  U4S j5        g)zAskipkeys on the PyDict_Next fast path must not leak skipped keys.r   rn   r   rO   )okrP   TNc                  .   > [         R                  " T SS9$ )NTr   r   r   s   r   rK   CTestRefcountLeaks.test_skipkeys_fast_path_no_leak.<locals>.<lambda>;  s    J$$TD9r   Nr   r   s    @r   test_skipkeys_fast_path_no_leak1TestRefcountLeaks.test_skipkeys_fast_path_no_leak6  s$     Qaq19	;r   c                 H   ^ SSSSSS/SS0/mU R                  U4S	 j5        g)
z4The indexed fast path for exact lists must not leak.r   r$        @TNrO   kvc                  0   > [         R                  " T 5      $ rA   r   r   s   r   rK   ?TestRefcountLeaks.test_list_fast_path_no_leak.<locals>.<lambda>A  r   r   r   r   s    @r   test_list_fast_path_no_leak-TestRefcountLeaks.test_list_fast_path_no_leak=  s.     5#tTA3c
;;<r   c                 4   ^ SmU R                  U4S j5        g)z5The indexed fast path for exact tuples must not leak.)r   r$  r8  TNc                  .   > [         R                  " T SS9$ )NT)rg   r   r   s   r   rK   @TestRefcountLeaks.test_tuple_fast_path_no_leak.<locals>.<lambda>H  s    J$$T$?r   Nr   r   s    @r   test_tuple_fast_path_no_leak.TestRefcountLeaks.test_tuple_fast_path_no_leakC  s     +?	Ar   r   N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r!  r(  r/  r5  r=  rB  r   r   r   r   r   r      sK    DF&/P = = < < ? ? ? ? . ., 
' 
' ) ) & & M M F F = = < < ; ; = =
 A Ar   r   )r   unittestr   r   r   r   r   simplejson.compatr   r   r	   simplejson.tests._helpersr
   r   r   r!   r8   skipIfversion_infor   
skipUnlesshasattrr   r   r   r   <module>rK     s    
    0 0 / / L > >@O @OF 
!!G+24'-H '-4'-T 
WS"45BDwA wADwAr   