
    j5                    N   d Z ddlmZ ddlZddlmc 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mZ ddlZ ee      j'                         j(                  d   Z ee      ej.                  vr"ej.                  j1                  d ee             edz  Zedz  Zd Zd	 Zd
 Zd Zd Zy)uD  test_load_otp_sysmodules_2553plus14.py — task-2553+14 Track B regression.

Locks in the `_load_otp()` sys.modules registration hardening (task-2553+12
post-hold-diagnosis root cause). Three guarantees:

  (1) post-hardening: the 3 F1 cases routed through `_load_otp()` PASS;
  (2) static (AST) + runtime assert: `_load_otp()` registers
      `sys.modules[spec.name] = mod` *before* `exec_module`;
  (3) smoke false-negative guard: an unregistered-loader replica of the
      pre-fix form deterministically raises the Python 3.12 `@dataclass`
      `dataclasses.py:749` AttributeError — proving a regression back to the
      pre-2553+14 form is *detected*, never silently false-negative.

Scope: test infra only. production code (anu_v2/owner_trigger_pat.py)
untouched; `_load_otp_streaming` and other PASS-path helpers unmodified.
    )annotationsN)Path   z:tests/regression/test_owner_trigger_2553_plus1_high_fix.pyzanu_v2/owner_trigger_pat.pyc                    t         j                  j                  dt              } g }d}| |u}|}|r| j                  }d}||u}|}|slt        j                  d|fd| |f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      dz  }dd|iz  }	|j                  |	       |rt        j                  dfd	f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      d
z  }
dd|
iz  }|j                  |       t        j                  |d      i z  }dd|iz  }t        t        j                  |            dx}x}x}x}x}x}}t         j                  j                  |       }|t         j"                  | j$                  <   	 | j                  j'                  |       t         j"                  j)                  | j$                  d       |S # t         j"                  j)                  | j$                  d       w xY w)z>Load the hardened high-fix test module (correctly registered).hf_under_test_2553p14Nis notz%(py2)s is not %(py5)sspecpy2py5%(py7)spy7z5%(py11)s
{%(py11)s = %(py9)s.loader
} is not %(py14)spy9py11py14%(py16)spy16r   assert %(py19)spy19)	importlibutilspec_from_file_location_HF_PATHloader
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprappend_format_boolopAssertionError_format_explanationmodule_from_specsysmodulesnameexec_modulepop)r   @py_assert1@py_assert4@py_assert3@py_assert0@py_assert10@py_assert13@py_assert12@py_format6@py_format8@py_format15@py_format17@py_format18@py_format20mods                  K/home/jay/workspace/tests/regression/test_load_otp_sysmodules_2553plus14.py_import_highfixr>   %   sh   >>11D 8t74t77474 777774t77777747774777t77777774777777777777777477777777777777
..
)
)$
/C CKK		)$		4(J 			4(s   ?I ,I2c            	        t               } | j                         }|j                  }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}}t        j                  t              5 }|j                  g d	d
dd       ddd       |j                  }j                   }t#        |      }	||	v }|sKt        j                  d|fd||	f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j                  t"              rt        j                  t"              nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |	      dz  }
dd|
iz  }t        t        j                  |            dx}x}x}}	t        j                  t              5 }|j                  g dd
dd       ddd       |j$                  }j                   }t#        |      }	||	v }|sKt        j                  d|fd||	f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j                  t"              rt        j                  t"              nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |	      dz  }
dd|
iz  }t        t        j                  |            dx}x}x}}	y# 1 sw Y   =xY w# 1 sw Y   xY w)z:(1) Post-hardening: the 3 F1 cases via `_load_otp()` PASS./gemini review==z<%(py2)s
{%(py2)s = %(py0)s.ALLOWED_COMMENT_BODY
} == %(py5)sotppy0r   r   assert %(py7)sr   N)api-XPOSTz/repos/o/r/issues/1/labels-fzbody=/gemini reviewor   in)zy%(py2)s
{%(py2)s = %(py0)s.ERR_ENDPOINT_NOT_ALLOWED
} in %(py9)s
{%(py9)s = %(py4)s(%(py7)s
{%(py7)s = %(py5)s.value
})
}strei_ep)rF   r   py4r   r   r   zassert %(py11)sr   )rH   rI   rJ   z/repos/o/r/issues/1/commentsrK   zbody=please merge)zu%(py2)s
{%(py2)s = %(py0)s.ERR_BODY_NOT_ALLOWED
} in %(py9)s
{%(py9)s = %(py4)s(%(py7)s
{%(py7)s = %(py5)s.value
})
}ei_body)r>   	_load_otpALLOWED_COMMENT_BODYr   r    r!   r"   r#   r$   r'   r(   pytestraises	Exception_assert_args_allowlistERR_ENDPOINT_NOT_ALLOWEDvaluerQ   ERR_BODY_NOT_ALLOWED)hfrD   r/   r0   r1   r6   r7   rR   @py_assert6@py_assert8@py_format10@py_format12rT   s                r=    test_f1_three_pass_posthardeningrc   4   s   		B
,,.C##7'77#'77777#'777777737773777#777'77777777	y	! 
U""*a	

 '';u{{;3{+;;'+;;;;;'+;;;;;;;3;;;3;;;';;;;;;3;;;3;;;;;;u;;;u;;;{;;;+;;;;;;;;	y	! 
W""(a	

 ##97==9s='99#'99999#'999999939993999#999999s999s99999979997999=999'99999999
 

 
s   <Q1QQQ%c                    t               } t        j                  t        j                  | j
                              }t        j                  |      }|j                  d   }t        j                  }t        ||      }|sddt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  |      rt        j                  |      nddt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      dz  }t!        t        j"                  |            dx}}d}d}t        j$                  |      D ]  }	t        |	t        j&                        r`|	j(                  D ]P  }
t        |
t        j*                        st        |
j,                  t        j.                        sD|
j,                  j0                  dk(  s^t        |
j,                  j,                  t        j2                        s|
j,                  j,                  j4                  d	k(  st        |
j6                  t        j.                        s|
j6                  j0                  d
k(  st        |
j6                  j,                  t        j2                        s |
j6                  j,                  j4                  dk(  sE|	j8                  }S t        |	t        j:                        st        |	j<                  t        j.                        s|	j<                  j0                  dk(  st        |	j<                  j,                  t        j.                        s|	j<                  j,                  j0                  dk(  s1t        |	j<                  j,                  j,                  t        j2                        sk|	j<                  j,                  j,                  j4                  dk(  s|	j8                  } d}||u}|st        j>                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }t        j@                  d      dz   d|iz  }t!        t        j"                  |            dx}}d}||u}|st        j>                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }t        j@                  d      dz   d|iz  }t!        t        j"                  |            dx}}||k  }|st        j>                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      nddt        j                         v st        j                  |      rt        j                  |      nddz  }t        j@                  d| d| d      dz   d|iz  }t!        t        j"                  |            d}y)zJ(2a) AST: `_load_otp` assigns `sys.modules[...] = mod` before exec_module.r   zVassert %(py6)s
{%(py6)s = %(py0)s(%(py1)s, %(py4)s
{%(py4)s = %(py2)s.FunctionDef
})
}
isinstancefnast)rF   py1r   rS   py6Nr+   r*   r,   r   r-   r   r   )z%(py0)s is not %(py3)s
reg_lineno)rF   py3zg_load_otp must register sys.modules[spec.name] = mod (regression to pre-task-2553+14 unregistered form)
>assert %(py5)sr   exec_linenoz0_load_otp must call spec.loader.exec_module(mod))<)z%(py0)s < %(py2)s)rF   r   z7sys.modules registration must precede exec_module (reg@z exec@)z
>assert %(py4)srS   )!r>   textwrapdedentinspect	getsourcerU   rg   parsebodyFunctionDefre   r!   r"   r   r#   r$   r'   r(   walkAssigntargets	Subscriptr\   	AttributeattrNameidslicelinenoCallfuncr    _format_assertmsg)r^   srctreerf   r1   @py_assert5@py_format7rj   rm   ntgt@py_assert2r/   @py_format4r6   @py_format3@py_format5s                    r=   ,test_load_otp_sysmodules_registration_staticr   I   s7   		B
//'++BLL9
:C99S>D	1B//*:b/********:***:******b***b******#***#***/**********JKXXb\  #a$yy * sCMM2"399cmm<		)3"399??CHH=		**e3"399cmm<		&0"399??CHH=		**f4!"J!*, q#((#1663==1},166<<7!!X-166<<--sxx8""%%/((KA #D " :T!  :T              "   	=     #V;d"VVV;dVVVVVV;VVV;VVVdVVV$VVVVVVV#  :                $    $   	|6+a	1        c                    t               } | j                         }|j                  }	 t        j                  }||v }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      nddt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      dz  }t        j                  d|d      dz   d	|iz  }t        t        j                  |            d
x}}t        j                  |   }||u }|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      nddz  }	t        j                  d      dz   d|	iz  }
t        t        j                  |
            d
x}}t        j                  j                  |d
       y
# t        j                  j                  |d
       w xY w)zG(2b) Runtime: after `_load_otp()` the module is registered & identical.rO   )z/%(py0)s in %(py4)s
{%(py4)s = %(py2)s.modules
}	spec_namer*   )rF   r   rS   z'_load_otp did not register sys.modules[]z
>assert %(py6)sri   N)is)z%(py1)s is %(py3)srD   )rh   rk   z=registered module object is not the one returned by _load_otprl   r   )r>   rU   __name__r*   r+   r   r    r!   r"   r#   r$   r   r'   r(   r.   )r^   rD   r   r1   r/   r   r   r2   r   r   r6   s              r=   -test_load_otp_sysmodules_registration_runtimer      s   		B
,,.C I)KK 	
yK' 	
 	
yK 	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
    	
 	
 		   	
 	
 		 ( 	
 	
  6i]!D	
 	
 	
 	
 	
 {{9% 	
%, 	
 	
% 	
 	
 		 & 	
 	
	6	
 	
  *- 	
 	
 		 *- 	
 	
  L	
 	
 	
 	
 	
 		4(	4(s   G"H+ +"Ic                    t         j                  dk  rt        j                  d       t        j
                  j                  dt              } g }d}| |u}|}|r| j                  }d}||u}|}|slt        j                  d|fd| |f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      dz  }d	d
|iz  }	|j                  |	       |rt        j                  dfdf      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }
dd|
iz  }|j                  |       t        j                   |d      i z  }dd|iz  }t#        t        j$                  |            dx}x}x}x}x}x}}t        j
                  j'                  |       }t         j(                  j+                  | j,                  d       	 t        j.                  t0              5 }| j                  j3                  |       ddd       d}j4                  }t7        |      }||v }|s-t        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  t6              rt        j                  t6              nddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        j8                  d|j4                        dz   d|iz  }t#        t        j$                  |            dx}x}x}}t         j(                  j+                  | j,                  d       t        j
                  j                  dt              }g }d}||u}|}|r|j                  }d}||u}|}|slt        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }d	d
|iz  }	|j                  |	       |rt        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }
dd|
iz  }|j                  |       t        j                   |d      i z  }dd|iz  }t#        t        j$                  |            dx}x}x}x}x}x}}t        j
                  j'                  |      }|t         j(                  |j,                  <   	 |j                  j3                  |       |j:                  }d}||k(  }|st        j                  d|fd||f      d t        j                         v st        j                  |      rt        j                  |      nd t        j                  |      t        j                  |      d!z  }d"d
|iz  }	t#        t        j$                  |	            dx}x}}t         j(                  j+                  |j,                  d       y# 1 sw Y   xY w# t         j(                  j+                  | j,                  d       w xY w# t         j(                  j+                  |j,                  d       w xY w)#z(3) Smoke guard: the pre-fix unregistered form is provably broken on
    py3.12 (raises AttributeError @ dataclasses.py:749), while the registered
    form loads cleanly. Detects any silent regression to the pre-fix loader.
    )      z5dataclasses.py:749 None-deref fault is Python >= 3.12otp_unreg_guard_2553p14Nr   r
   spec_badr   r   r   r   r   r   r   r   r   r   __dict__rO   )zK%(py1)s in %(py8)s
{%(py8)s = %(py3)s(%(py6)s
{%(py6)s = %(py4)s.value
})
}rQ   ei)rh   rk   rS   ri   py8z,expected py3.12 @dataclass None-deref, got: z
>assert %(py10)spy10otp_reg_guard_2553p14spec_okr@   rA   rC   mod_okrE   rG   )r*   version_inforW   skipr   r   r   	_OTP_PATHr   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r+   r.   r,   rX   AttributeErrorr-   r\   rQ   r   rV   )r   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   mod_badr   r   @py_assert7r   @py_format9@py_format11r   r   s                          r=   ,test_unregistered_loader_falsenegative_guardr      s   
 '!KL ~~55!9H @4?84?HOO?4?O4$?????84??????8???8???4???????O4??????H???H???O???4??????????????nn--h7GKKOOHMM4(-]]>* 	1bOO''0	1 	
 	
S] 	
z]* 	
 	
 	
z] 	
 	
 		  	
 	
	6	
 	
  ! 	
 	
 		 ! 	
 	
	6	
 	
  "$ 	
 	
 		 "$ 	
 	
 		 "* 	
 	
 		 + 	
 	
  ;288,G	
 	
 	
 	
 	
 	
 	t, nn44G >$=7$=7>>==>#=====7$======7===7===$=======>======7===7===>=================^^,,W5F &CKK,""6***>.>>*.>>>>>*.>>>>>>>v>>>v>>>*>>>.>>>>>>>>d+'	1 	1 	t, 	d+s2   4\	 [<)E\	 .C#\8 <\\	 	,\58,]$) __doc__
__future__r   builtinsr!   _pytest.assertion.rewrite	assertionrewriter   rg   importlib.utilr   rr   r*   rp   pathlibr   rW   __file__resolveparents_WSrQ   pathinsertr   r   r>   rc   r   r   r    r   r=   <module>r      s   " #   
   
   
8n&&q)s8388HHOOAs3x MM//	:*4n)&#,r   