ó
Š]c           @   s%  d  Z  d d l m Z d d l m Z 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 Z d d l Z d d l Z d d l m Z d d l m Z d d	 l m Z d d
 l m Z d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ  d e f d  „  ƒ  YZ! d! e f d" „  ƒ  YZ" d# e j f d$ „  ƒ  YZ# d% e f d& „  ƒ  YZ$ d' „  Z% d S((   sF  Test suite for 2to3's parser and grammar files.

This is the place to add tests for changes to 2to3's grammar, such as those
merging the grammars for Python 2 and 3. In addition to specific tests for
parts of the grammar we've changed, we also make sure we can parse the
test_grammar.py files from both Python 2 and Python 3.
i   (   t   support(   t   drivert   test_diriÿÿÿÿN(   R   (   t   tokenizei   (   t
   ParseError(   t   python_symbolst
   TestDriverc           B   s   e  Z d  „  Z RS(   c         C   sa   d } t  j | ƒ } |  j | j d j d j t j ƒ |  j | j d j d j t j ƒ d  S(   Ns   print 1
print 2
i    i   (   R   t   parse_stringt   assertEqualt   childrent   typet   symst
   print_stmt(   t   selft   st   t(    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_formfeed!   s    $(   t   __name__t
   __module__R   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR      s   t   TestPgen2Cachingc           B   sG   e  Z d  „  Z d „  Z e j e j d k d ƒ d „  ƒ Z	 d „  Z
 RS(   c         C   s    t  j t j d t d t ƒd  S(   Nt   savet   force(   t   pgen2_drivert   load_grammarR    t   grammar_patht   Falset   True(   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_load_grammar_from_txt_file)   s    c         C   sÃ   t  j ƒ  } z¢ t j j | t j j t j ƒ ƒ } t j	 t j | ƒ t
 j | ƒ } t
 j | d t d t ƒ|  j t j j | ƒ ƒ t j | ƒ t
 j | d t d t ƒWd  t j | ƒ Xd  S(   NR   R   (   t   tempfilet   mkdtempt   ost   patht   joint   basenameR    R   t   shutilt   copyR   t   _generate_pickle_nameR   R   t
   assertTruet   existst   unlinkR   t   rmtree(   R   t   tmpdirt   grammar_copyt   pickle_name(    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_load_grammar_from_pickle,   s    	s   sys.executable requiredc         C   s¶  t  j ƒ  } t j j | d ƒ } z€t j | ƒ t j j t j ƒ } t j j | | ƒ } t j j | | ƒ } t	 j
 t j | ƒ t	 j
 t j | ƒ t j | ƒ } t j | ƒ } |  j | | ƒ t j | d t d t ƒ|  j t j j | ƒ ƒ t t j ƒ } d | d <t j t j d d | f g d | ƒ|  j t j j | ƒ ƒ t | d	 ƒ A }	 t | d	 ƒ ) }
 |  j |	 j ƒ  |
 j ƒ  d
 d ƒWd  QXWd  QXWd  t	 j | ƒ Xd  S(   Nt   subdirR   R   t   randomt   PYTHONHASHSEEDs   -cs{   
from lib2to3.pgen2 import driver as pgen2_driver
pgen2_driver.load_grammar(%r, save=True, force=True)
                    t   envt   rbt   msgsG   Grammar caches generated using different hash seeds were not identical.(   R   R   R   R   R    t   mkdirR!   R    R   R"   R#   R   R$   t   assertNotEqualR   R   R%   R&   t   dictt   environt
   subprocesst
   check_callt   syst
   executablet   openR   t   readR(   (   R   R)   t	   tmpsubdirt   grammar_baseR*   t   grammar_sub_copyR+   t   pickle_sub_namet   sub_envt
   pickle_f_1t
   pickle_f_2(    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt!   test_load_grammar_from_subprocess>   s8    	
	c            s‘   t  d } d d d „  ƒ  Y‰  d t j f ‡  f d †  ƒ  Y} | | ƒ t j | <|  j t j t j | ƒ t j	 | d ƒ } |  j
 | j d ƒ d  S(	   Ns
   .load_testt   MyLoaderc           B   s   e  Z d  „  Z RS(   c         S   s   t  j i d d 6ƒ S(   Ni   t   elephant(   t   picklet   dumps(   R   t   where(    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   get_dataj   s    (   R   R   RJ   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRE   i   s   t   MyModulec              s   e  Z d  Z ˆ  ƒ  Z RS(   t   parsertestmodule(   R   R   t   __file__t
   __loader__(    (   RE   (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRK   l   s   s   Grammar.txti   (    (   R   t   typest
   ModuleTypeR9   t   modulest
   addCleanupt   operatort   delitemR   t   load_packaged_grammarR   RF   (   R   t   modnameRK   t   g(    (   RE   s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_load_packaged_grammarg   s    
N(   R   R   R   R,   t   unittestt   skipIfR9   R:   t   NoneRD   RX   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR   (   s   		$)t   GrammarTestc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   t  j | ƒ d  S(   N(   R    R   (   R   t   code(    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   validatev   s    c         C   s5   y |  j  | ƒ Wn t k
 r$ n Xt d ƒ ‚ d  S(   Ns    Syntax shouldn't have been valid(   R^   R   t   AssertionError(   R   R]   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   invalid_syntaxy   s
    (   R   R   R^   R`   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR\   u   s   	t   TestMatrixMultiplicationc           B   s   e  Z d  „  Z RS(   c         C   s   |  j  d ƒ |  j  d ƒ d  S(   Ns   a @ bs   a @= b(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt#   test_matrix_multiplication_operatorƒ   s    (   R   R   Rb   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRa   ‚   s   t   TestYieldFromc           B   s   e  Z d  „  Z RS(   c         C   s+   |  j  d ƒ |  j  d ƒ |  j d ƒ d  S(   Ns   yield from xs   (yield from x) + ys
   yield from(   R^   R`   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRb   ‰   s    (   R   R   Rb   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRc   ˆ   s   t   TestRaiseChangesc           B   sY   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(	   c         C   s   |  j  d ƒ d  S(   Nt   raise(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_2x_style_1   s    c         C   s   |  j  d ƒ d  S(   Ns
   raise E, V(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_2x_style_2“   s    c         C   s   |  j  d ƒ d  S(   Ns   raise E, V, T(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_2x_style_3–   s    c         C   s   |  j  d ƒ d  S(   Ns   raise E, V, T, Z(   R`   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_2x_style_invalid_1™   s    c         C   s   |  j  d ƒ d  S(   Ns   raise E1 from E2(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_3x_styleœ   s    c         C   s   |  j  d ƒ d  S(   Ns   raise E, V from E1(   R`   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_3x_style_invalid_1Ÿ   s    c         C   s   |  j  d ƒ d  S(   Ns   raise E from E1, E2(   R`   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_3x_style_invalid_2¢   s    c         C   s   |  j  d ƒ d  S(   Ns   raise from E1, E2(   R`   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_3x_style_invalid_3¥   s    c         C   s   |  j  d ƒ d  S(   Ns   raise E from(   R`   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_3x_style_invalid_4¨   s    (   R   R   Rf   Rg   Rh   Ri   Rj   Rk   Rl   Rm   Rn   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRd      s   								t   TestUnpackingGeneralizationsc           B   sb   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   c         C   s   |  j  d ƒ d  S(   Ns   func(1, *(2, 3), 4)(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_mid_positional_star°   s    c         C   s   |  j  d ƒ d  S(   Ns,   func(**{'eggs':'scrambled', 'spam':'fried'})(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_double_star_dict_literal³   s    c         C   s   |  j  d ƒ d  S(   Ns*   func(spam='fried', **{'eggs':'scrambled'})(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt,   test_double_star_dict_literal_after_keywords¶   s    c         C   s   |  j  d ƒ d  S(   Ns   [*{2}, 3, *[4]](   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_list_display¹   s    c         C   s   |  j  d ƒ d  S(   Ns   {*{2}, 3, *[4]}(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_set_display¼   s    c         C   s   |  j  d ƒ d  S(   Ns   {**{}}(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_dict_display_1¿   s    c         C   s   |  j  d ƒ d  S(   Ns   {**{}, 3:4, **{5:6, 7:8}}(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_dict_display_2Â   s    c         C   s   |  j  d ƒ d  S(   Ns   f(a, *b, *c, d)(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_argument_unpacking_1Å   s    c         C   s   |  j  d ƒ d  S(   Ns   f(**a, **b)(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_argument_unpacking_2È   s    c         C   s   |  j  d ƒ d  S(   Ns   f(2, *a, *b, **b, **c, **d)(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_argument_unpacking_3Ë   s    (   R   R   Rp   Rq   Rr   Rs   Rt   Ru   Rv   Rw   Rx   Ry   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRo   ¯   s   									t   TestFunctionAnnotationsc           B   sP   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s   |  j  d ƒ d  S(   Ns   def f(x) -> list: pass(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_1Ñ   s    c         C   s   |  j  d ƒ d  S(   Ns   def f(x:int): pass(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_2Ô   s    c         C   s   |  j  d ƒ d  S(   Ns   def f(*x:str): pass(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_3×   s    c         C   s   |  j  d ƒ d  S(   Ns   def f(**x:float): pass(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_4Ú   s    c         C   s   |  j  d ƒ d  S(   Ns   def f(x, y:1+2): pass(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_5Ý   s    c         C   s   |  j  d ƒ d  S(   Ns   def f(a, (b:1, c:2, d)): pass(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_6à   s    c         C   s   |  j  d ƒ d  S(   Ns/   def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_7ã   s    c         C   s   d } |  j  | ƒ d  S(   Nsg   def f(a, (b:1, c:2, d), e:3=4, f=5,
                        *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass(   R^   (   R   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_8æ   s    (
   R   R   R{   R|   R}   R~   R   R€   R   R‚   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRz   Ð   s   							t
   TestExceptc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   d } |  j  | ƒ d  S(   NsP   
            try:
                x
            except E as N:
                y(   R^   (   R   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_newí   s    c         C   s   d } |  j  | ƒ d  S(   NsN   
            try:
                x
            except E, N:
                y(   R^   (   R   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_oldõ   s    (   R   R   R„   R…   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRƒ   ì   s   	t   TestSetLiteralc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   |  j  d ƒ d  S(   Ns   x = {'one'}(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR{      s    c         C   s   |  j  d ƒ d  S(   Ns   x = {'one', 1,}(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR|     s    c         C   s   |  j  d ƒ d  S(   Ns   x = {'one', 'two', 'three'}(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR}     s    c         C   s   |  j  d ƒ d  S(   Ns   x = {2, 3, 4,}(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR~   	  s    (   R   R   R{   R|   R}   R~   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR†   ÿ   s   			t   TestNumericLiteralsc           B   s   e  Z d  „  Z d „  Z RS(   c         C   s   |  j  d ƒ |  j d ƒ d  S(   Nt   0o7777777777777t   0o7324528887(   R^   R`   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_new_octal_notation  s    c         C   s   |  j  d ƒ |  j d ƒ d  S(   Nt   0b101010t	   0b0101021(   R^   R`   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_new_binary_notation  s    (   R   R   RŠ   R   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR‡     s   	t   TestClassDefc           B   s   e  Z d  „  Z RS(   c         C   sE   |  j  d ƒ |  j  d ƒ |  j  d ƒ |  j  d ƒ |  j  d ƒ d  S(   Ns   class B(t=7): passs   class B(t, *args): passs   class B(t, **kwargs): passs!   class B(t, *args, **kwargs): passs&   class B(t, y=9, *args, **kwargs): pass(   R^   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_new_syntax  s
    (   R   R   R   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyRŽ     s   t   TestParserIdempotencyc           B   s    e  Z d  Z d „  Z d „  Z RS(   s,   A cut-down version of pytree_idempotency.py.c         C   sç   t  j j d ƒ r d  SxÊ t j ƒ  D]¼ } t | d ƒ  } t j | j ƒ d } Wd  QX|  j	 | d | ƒ t | d ƒ " } | j
 ƒ  } | j | ƒ } Wd  QXt j | ƒ } t | ƒ } t | | | ƒ r# |  j d | ƒ q# q# Wd  S(   Nt   winR1   i    s   can't detect encoding for %st   rs   Idempotency failed: %s(   R9   t   platformt
   startswithR    t   all_project_filesR;   R   t   detect_encodingt   readlinet   assertIsNotNoneR<   t   decodeR   R   t   unicodet   difft   fail(   R   t   filepatht   fpt   encodingt   sourcet   treet   new(    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_all_project_files$  s    	c         C   s8   t  j d ƒ t  j d ƒ t  j d ƒ t  j d ƒ d  S(   Ns   a, *b, c = x
s   [*a, b] = x
s   (z, *y, w) = m
s   for *z, m in d: pass
(   R   R   (   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_extended_unpacking5  s    (   R   R   t   __doc__R£   R¤   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR      s   	t   TestLiteralsc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s   t  j t j | ƒ d ƒ d  S(   Ns   

(   R   R   R    t   dedent(   R   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR^   =  s    c         C   s   d } |  j  | ƒ d  S(   Nsá   
            md5test(b"ª" * 80,
                    (b"Test Using Larger Than Block-Size Key "
                     b"and Larger Than One Block-Size Data"),
                    "6f630fad67cda0ee1fb1f562db3aa53e")
            (   R^   (   R   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_multiline_bytes_literals@  s    c         C   s   d } |  j  | ƒ d  S(   Ns¬   
            b"""
            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN">
            """
            (   R^   (   R   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt'   test_multiline_bytes_tripquote_literalsI  s    c         C   s   d } |  j  | ƒ d  S(   NsÞ   
            md5test("ª" * 80,
                    ("Test Using Larger Than Block-Size Key "
                     "and Larger Than One Block-Size Data"),
                    "6f630fad67cda0ee1fb1f562db3aa53e")
            (   R^   (   R   R   (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   test_multiline_str_literalsR  s    (   R   R   R^   R¨   R©   Rª   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR¦   ;  s   					c         C   ss   t  d d ƒ } z | j | j | ƒ ƒ Wd  | j ƒ  Xz' |  j d d ƒ }  t j d |  ƒ SWd  t j d ƒ Xd  S(   Nt   @t   wt   "s   \"s   diff -u "%s" @(   R;   t   writet   encodet   closet   replaceR   t   systemt   remove(   t   fnt   resultRŸ   t   f(    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyR›   \  s    (&   R¥   t    R    R   R   RS   R   RG   R"   R7   R9   R   RO   RY   t   lib2to3.pgen2R   R   t   pgen2.parseR   t   lib2to3.pygramR   R   t   TestCaseR   R   R\   Ra   Rc   Rd   Ro   Rz   Rƒ   R†   R‡   RŽ   R   R¦   R›   (    (    (    s*   lib/python2.7/lib2to3/tests/test_parser.pyt   <module>   s<   	M !
	!