o
    HXhU                     @   sN   	 d dl Z d dlZd dlmZ G dd dejZdd Zedkr%e  dS dS )    N)supportc                   @   s*  e Zd Z		dDddZdd Zdd	 Zd
d Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zejd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!ejd>d? Z"ejd@dA Z#ejdBdC Z$dS )ESyntaxTestCase
<testcase>execNc
              
   C   s  	 zt ||| W np tyy }
 zd|r t|
|s | d|j  t|t|
}|d u r4| d|f  | |
j	| |d urF| |
j
| |d urQ| |
j| |d ur\| |
j| |	d urn| |
j|	 W Y d }
~
d S W Y d }
~
d S d }
~
ww | d d S )NzSyntaxError is not a %szSyntaxError did not contain %rz#compile() did not raise SyntaxError)compileSyntaxError
isinstancefail__name__researchstrassertEqualfilenamelinenooffset
end_lineno
end_offset)selfcodeZerrtextr   modesubclassr   r   r   r   errmo r   :/opt/python-3.10.19/usr/lib/python3.10/test/test_syntax.py_check_error'  s,   zSyntaxTestCase._check_errorc                 C      | j dddd d S )Nzprint(end1 + end2 = ' ')z=expression cannot contain assignment, perhaps you meant "=="?   )r   r   r   r   r   r   test_expression_with_assignmentE  
   
z.SyntaxTestCase.test_expression_with_assignmentc                 C   r   )Nzf{}invalid syntaxsingler   r   r    r   r   r   1test_curly_brace_after_primary_raises_immediatelyL  s   z@SyntaxTestCase.test_curly_brace_after_primary_raises_immediatelyc                 C      |  dd d S )Nzf() = 1Zassignr   r    r   r   r   test_assign_callO  s   zSyntaxTestCase.test_assign_callc                 C   sH  |  dd |  dd |  dd |  dd |  dd	 |  d
d |  dd	 |  dd	 |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  d d |  d!d |  d"d# |  d$d d S )%Nzdel (,)r#   zdel 1zcannot delete literalz
del (1, 2)zdel Nonezcannot delete Nonezdel *xzcannot delete starredzdel (*x)zcannot use starred expressionz	del (*x,)z	del [*x,]zdel f()zcannot delete function callzdel f(a, b)z	del o.f()z
del a[0]()z
del x, f()z
del f(), xzdel [a, b, ((c), (d,), e.f())]zdel (a if True else b)zcannot delete conditionalzdel +azcannot delete expressionz	del a, +bz	del a + bzdel (a + b, c)zdel (c[0], a + b)zdel a.b.c + 2zdel a.b.c[0] + 2zdel (a, b, (c, d.e.f + 2))zdel [a, b, (c, d.e.f[0] + 2)]zdel (a := 5)zcannot delete named expressionz
del a += br   r    r   r   r   test_assign_delR  s6   zSyntaxTestCase.test_assign_delc                 C      d}| j |ddd d S )Nzif 1:
            def error(a):
                global a  # SyntaxError
            def error2():
                b = 1
                global b  # SyntaxError
            zparameter and global   r   r   r   sourcer   r   r   test_global_param_err_firstq     z*SyntaxTestCase.test_global_param_err_firstc                 C   r*   )Nzif 1:
            def error(a):
                nonlocal a  # SyntaxError
            def error2():
                b = 1
                global b  # SyntaxError
            zparameter and nonlocalr+   r,   r   r-   r   r   r   test_nonlocal_param_err_first{  r0   z,SyntaxTestCase.test_nonlocal_param_err_firstc                 C   p   |  dd |  dd |  dd |  dd |  dd |  dd |  dd |  d	d |  d
d d S )Nzif 0: yieldoutside functionzif 0: yield
else:  x=1zif 1: pass
else: yieldzwhile 0: yieldzwhile 0: yield
else:  x=1zclass C:
  if 0: yieldz#class C:
  if 1: pass
  else: yieldzclass C:
  while 0: yieldz(class C:
  while 0: yield
  else:  x = 1r   r    r   r   r   test_yield_outside_function  s   z*SyntaxTestCase.test_yield_outside_functionc                 C   r2   )Nzif 0: returnr3   zif 0: return
else:  x=1zif 1: pass
else: returnzwhile 0: returnzclass C:
  if 0: returnzclass C:
  while 0: returnz'class C:
  while 0: return
  else:  x=1z$class C:
  if 0: return
  else: x= 1z$class C:
  if 1: pass
  else: returnr   r    r   r   r   test_return_outside_function  s   z+SyntaxTestCase.test_return_outside_functionc                 C   sx   d}| j d|dd | j d|dd | j d|dd | j d|dd | j d	|dd | j d
|dd | j d|dd d S )Nzoutside loopbreak   r,   zif 0: breakzif 0: break
else:  x=1zif 1: pass
else: break   zclass C:
  if 0: breakz#class C:
  if 1: pass
  else: breakr+   zwith object() as obj:
 breakr   r   msgr   r   r   test_break_outside_loop  s   
z&SyntaxTestCase.test_break_outside_loopc                 C   sh   d}| j d|dd | j d|dd | j d|dd | j d|dd | j d	|d
d | j d|dd d S )Nznot properly in loopzif 0: continuer7   r,   zif 0: continue
else:  x=1zif 1: pass
else: continuer8   zclass C:
  if 0: continuez&class C:
  if 1: pass
  else: continuer+   z"with object() as obj:
    continuer   r9   r   r   r   test_continue_outside_loop  s   
z)SyntaxTestCase.test_continue_outside_loopc                 C      | j ddtd d S )Nzfoo()
 bar()
zunexpected indentr   r   IndentationErrorr    r   r   r   test_unexpected_indent     
z%SyntaxTestCase.test_unexpected_indentc                 C   r=   )Nzif 1:
foo()zexpected an indented blockr>   r?   r    r   r   r   test_no_indent  rB   zSyntaxTestCase.test_no_indentc                 C   r=   )Nzif 1:
  foo()
 bar()z unindent does not match .* levelr>   r?   r    r   r   r   test_bad_outdent  s   
zSyntaxTestCase.test_bad_outdentc                 C   r'   )Nzint(base=10, '2')z,positional argument follows keyword argumentr   r    r   r   r   test_kwargs_last     zSyntaxTestCase.test_kwargs_lastc                 C   r'   )Nzint(**{'base': 10}, '2')z6positional argument follows keyword argument unpackingr   r    r   r   r   test_kwargs_last2  rF   z SyntaxTestCase.test_kwargs_last2c                 C   r'   )Nzint(**{'base': 10}, *['2'])z>iterable argument unpacking follows keyword argument unpackingr   r    r   r   r   test_kwargs_last3  rF   z SyntaxTestCase.test_kwargs_last3c                 C   s   | j ddddddd d S )Nz<foo(x,    y for y in range(3) for z in range(2) if z    , p)z*Generator expression must be parenthesizedr7      5   )r   r   r   r   r   r    r   r   r   test_generator_in_function_call  s   
z.SyntaxTestCase.test_generator_in_function_callc                 C   s|   d}zt |dd W n ty   | d Y nw d}d}z| t |ddt |dd W d S  ty=   | d Y d S w )Nz\
pass
        \

pass
<string>r   z8Empty line after a line continuation character is valid.zN\
def fib(n):
    \
'''Print a Fibonacci series up to n.'''
    \
a, b = 0, 1
zJ\
def fib(n):
    '''Print a Fibonacci series up to n.'''
    a, b = 0, 1
z/Indented statement over multiple lines is valid)r   r   r	   r   )r   ss1s2r   r   r   test_empty_line_after_linecont  s   "z-SyntaxTestCase.test_empty_line_after_linecontc                 C   s   d}|  tt| d S )Nz(\
if x:
    y = 1
  \
  foo = 1
        )assertRaisesr@   r   r   r   r   r   r   !test_continuation_bad_indentation  s   z0SyntaxTestCase.test_continuation_bad_indentationc                 C   sj   d}t dD ]}|d|  d7 }|d|d   d7 }|d|  d7 }q|d d	7 }| |d
 d S )N    z    ztry:
r7   zraise Exception
zexcept Exception as e:
z0                                                pass!too many statically nested blocks)ranger   )r   r   ir   r   r   test_nested_named_except_blocks  s   z.SyntaxTestCase.test_nested_named_except_blocksc                 C      d}|  |d d S )Nzw
def func1():
    if a != b:
        raise ValueError

def func2():
    try
        return 1
    finally:
        pass
zexpected ':'r   rR   r   r   r   &test_barry_as_flufl_with_syntax_errors  s   z5SyntaxTestCase.test_barry_as_flufl_with_syntax_errorsc                 C   s:   | j ddddd | j ddddd | j ddd	dd d S )
Nz	a = 3 \ 46unexpected character after line continuation characterr7      )r   r   z1,\#
2   z
fgdfgf
1,\#
2
r+   r   r    r   r   r   -test_invalid_line_continuation_error_position  s   
z<SyntaxTestCase.test_invalid_line_continuation_error_positionc                 C   s   |  dd |  dd d S )Nu   A.Ɗ\ r]   u   A.μ\
zunexpected EOF while parsingr   r    r   r   r   -test_invalid_line_continuation_left_recursive(  s   z<SyntaxTestCase.test_invalid_line_continuation_left_recursivec                 C   s~   dD ]}|  |d d| d qdD ]}|  d| dd| d qdD ]}|  |d d| d	 q&d
}|  |d d S )Nz([{z1 + 2\z' was never closedza = z 1, 2, 3
b=3z)]}zunmatched '\'zFfunc(
    a=["unclosed], # Need a quote in this comment: "
    b=2,
)
z8parenthesis '\)' does not match opening parenthesis '\['r   )r   Zparenr   r   r   r   test_error_parenthesis0  s   z%SyntaxTestCase.test_error_parenthesisc                 C   s4   |  dd |  dd |  dd |  dd d S )Nz'blechzunterminated string literalz"blechz'''blechz)unterminated triple-quoted string literalz"""blechr   r    r   r   r   test_error_string_literalC  s   z(SyntaxTestCase.test_error_string_literalc                 C      d}t |dd d S )Nz)
def match(x):
    return 1+1

match(34)
rL   r   r   rR   r   r   r   +test_match_call_does_not_raise_syntax_errorJ     z:SyntaxTestCase.test_match_call_does_not_raise_syntax_errorc                 C   rf   )Nz'
def case(x):
    return 1+1

case(34)
rL   r   rg   rR   r   r   r   *test_case_call_does_not_raise_syntax_errorS  ri   z9SyntaxTestCase.test_case_call_does_not_raise_syntax_errorc                 C   r   )Nzcall(
a=1,
a=1
)zkeyword argument repeatedr+   r,   r   r    r   r   r   /test_multiline_compiler_error_points_to_the_end\  r"   z>SyntaxTestCase.test_multiline_compiler_error_points_to_the_endc                 C   r[   )Na  
while 1:
 while 2:
  while 3:
   while 4:
    while 5:
     while 6:
      while 8:
       while 9:
        while 10:
         while 11:
          while 12:
           while 13:
            while 14:
             while 15:
              while 16:
               while 17:
                while 18:
                 while 19:
                  while 20:
                   while 21:
                    while 22:
                     break
rW   r   r-   r   r   r   )test_syntax_error_on_deeply_nested_blocksc  s   	z8SyntaxTestCase.test_syntax_error_on_deeply_nested_blocksc              
   C   s|   dd d }dD ]3}| j |d# | t t|d| W d    n1 s'w   Y  W d    n1 s6w   Y  qd S )N-i 4)r   evalr$   r%   rL   )ZsubTestrQ   MemoryErrorr   )r   r.   r   r   r   r   #test_error_on_parser_stack_overflow  s   z2SyntaxTestCase.test_error_on_parser_stack_overflowc                 C   s@   d}|  t t|dd W d    d S 1 sw   Y  d S )Nz,d{{{{{{{{{{{{{{{{{{{{{{{{{```{{{{{{{ef f():yrL   r   )rQ   r   r   r-   r   r   r   test_deep_invalid_rule  s   "z%SyntaxTestCase.test_deep_invalid_rule)r   r   NNNNN)%r
   
__module____qualname__r   r!   r&   r(   r)   r/   r1   r4   r5   r;   r<   rA   rC   rD   rE   rG   rH   rK   rP   rS   r   Zcpython_onlyrZ   r\   r`   ra   rd   re   rh   rj   rk   rl   rq   rr   r   r   r   r   r   %  sN    



			
"
r   c                  C   s(   t t ddlm}  t j| dd d S )Nr   test_syntaxT)	verbosity)r   Zrun_unittestr   testrv   Zrun_doctestru   r   r   r   	test_main  s   
ry   __main__)r   Zunittestrx   r   ZTestCaser   ry   r
   r   r   r   r   <module>   s*              )  t
