ó
¬«Y]c           @   sÆ   d  Z  d d l Z d d l Z d d l m Z m Z m Z d „  Z d „  Z d e j	 f d „  ƒ  YZ
 d „  Z d	 e j	 f d
 „  ƒ  YZ d e j	 f d „  ƒ  YZ d d „ Z e d k rÂ e ƒ  n  d S(   sÃ  This module includes tests of the code object representation.

>>> def f(x):
...     def g(y):
...         return x + y
...     return g
...

>>> dump(f.func_code)
name: f
argcount: 1
names: ()
varnames: ('x', 'g')
cellvars: ('x',)
freevars: ()
nlocals: 2
flags: 3
consts: ('None', '<code object g>')

>>> dump(f(4).func_code)
name: g
argcount: 1
names: ()
varnames: ('y',)
cellvars: ()
freevars: ('x',)
nlocals: 1
flags: 19
consts: ('None',)

>>> def h(x, y):
...     a = x + y
...     b = x - y
...     c = a * b
...     return c
...
>>> dump(h.func_code)
name: h
argcount: 2
names: ()
varnames: ('x', 'y', 'a', 'b', 'c')
cellvars: ()
freevars: ()
nlocals: 5
flags: 67
consts: ('None',)

>>> def attrs(obj):
...     print obj.attr1
...     print obj.attr2
...     print obj.attr3

>>> dump(attrs.func_code)
name: attrs
argcount: 1
names: ('attr1', 'attr2', 'attr3')
varnames: ('obj',)
cellvars: ()
freevars: ()
nlocals: 1
flags: 67
consts: ('None',)

>>> def optimize_away():
...     'doc string'
...     'not a docstring'
...     53
...     53L

>>> dump(optimize_away.func_code)
name: optimize_away
argcount: 0
names: ()
varnames: ()
cellvars: ()
freevars: ()
nlocals: 0
flags: 67
consts: ("'doc string'", 'None')

iÿÿÿÿN(   t   run_doctestt   run_unittestt   cpython_onlyc         c   sD   x= |  D]5 } t  | ƒ } | j d ƒ r7 d | j Vq | Vq Wd S(   s.   Yield a doctest-safe sequence of object reprs.s   <code objects   <code object %s>N(   t   reprt
   startswitht   co_name(   t   tt   eltt   r(    (    s   lib/python2.7/test/test_code.pyt   constsX   s
    c         C   sa   xB d d d d d d d d g D]" } d	 | t  |  d
 | ƒ f GHq Wd Gt t |  j ƒ ƒ GHd S(   s1   Print out a text representation of a code object.t   namet   argcountt   namest   varnamest   cellvarst   freevarst   nlocalst   flagss   %s: %st   co_s   consts:N(   t   getattrt   tupleR	   t	   co_consts(   t   cot   attr(    (    s   lib/python2.7/test/test_code.pyt   dumpa   s     t   CodeTestc           B   s   e  Z e d  „  ƒ Z RS(   c         C   s^   d d  l  } | j d d d ƒ } |  j | j d ƒ |  j | j d ƒ |  j | j d ƒ d  S(   Niÿÿÿÿt   filenamet   funcnamei   (   t	   _testcapit   code_newemptyt   assertEqualt   co_filenameR   t   co_firstlineno(   t   selfR   R   (    (    s   lib/python2.7/test/test_code.pyt   test_newemptyk   s
    (   t   __name__t
   __module__R   R"   (    (    (    s   lib/python2.7/test/test_code.pyR   i   s   c         C   s   |  t  d |  d d d !ƒ k S(   Nt   _i   iÿÿÿÿ(   t   intern(   t   s(    (    s   lib/python2.7/test/test_code.pyt
   isinternedt   s    t   CodeConstsTestc           B   s_   e  Z d  „  Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 RS(   c         C   sB   x | D] } | | k r | Sq W|  j  | | ƒ |  j d ƒ d  S(   Ns   Should never be reached(   t   assertInt   fail(   R!   R	   t   valuet   v(    (    s   lib/python2.7/test/test_code.pyt
   find_consty   s
    c         C   s'   t  | ƒ s# |  j d | f ƒ n  d  S(   Ns   String %r is not interned(   R(   R+   (   R!   R'   (    (    s   lib/python2.7/test/test_code.pyt   assertIsInterned€   s    c         C   s'   t  | ƒ r# |  j d | f ƒ n  d  S(   Ns   String %r is interned(   R(   R+   (   R!   R'   (    (    s   lib/python2.7/test/test_code.pyt   assertIsNotInterned„   s    c         C   s8   t  d d d ƒ } |  j | j d ƒ } |  j | ƒ d  S(   Ns   res = "str_value"t   ?t   exect	   str_value(   t   compileR.   R   R/   (   R!   R   R-   (    (    s   lib/python2.7/test/test_code.pyt   test_interned_stringˆ   s    c         C   s<   t  d d d ƒ } |  j | j d ƒ } |  j | d ƒ d  S(   Ns   res = ("str_value",)R1   R2   R3   i    (   R3   (   R4   R.   R   R/   (   R!   R   R-   (    (    s   lib/python2.7/test/test_code.pyt   test_interned_string_in_tupleŽ   s    c         C   s    d d „ } |  j  | ƒ  ƒ d  S(   NR3   c         S   s   |  S(   N(    (   t   a(    (    s   lib/python2.7/test/test_code.pyt   f–   s    (   R/   (   R!   R8   (    (    s   lib/python2.7/test/test_code.pyt   test_interned_string_default”   s    c         C   s8   t  d d d ƒ } |  j | j d ƒ } |  j | ƒ d  S(   Ns   res = "str\0value!"R1   R2   s
   str value!(   R4   R.   R   R0   (   R!   R   R-   (    (    s   lib/python2.7/test/test_code.pyt   test_interned_string_with_nullš   s    (
   R#   R$   R.   R/   R0   R   R5   R6   R9   R:   (    (    (    s   lib/python2.7/test/test_code.pyR)   w   s   			t   CodeWeakRefTestc           B   s   e  Z d  „  Z RS(   c            s   i  } d e  ƒ  | U| d } ~ e ˆ  _ ‡  f d †  } e j | j | ƒ } ˆ  j e | ƒ  ƒ ƒ ~ ˆ  j e | ƒ  ƒ ƒ ˆ  j ˆ  j ƒ d  S(   Ns   def f(): passR8   c            s   t  ˆ  _ d  S(   N(   t   Truet   called(   t   code(   R!   (    s   lib/python2.7/test/test_code.pyt   callback¬   s    (	   t   globalst   FalseR=   t   weakreft   reft   __code__t
   assertTruet   boolt   assertFalse(   R!   t	   namespaceR8   R?   t   coderef(    (   R!   s   lib/python2.7/test/test_code.pyt
   test_basic£   s    
	(   R#   R$   RJ   (    (    (    s   lib/python2.7/test/test_code.pyR;   ¡   s   c         C   s1   d d l  m } t | |  ƒ t t t t ƒ d  S(   Niÿÿÿÿ(   t	   test_code(   t   testRK   R    R   R   R)   R;   (   t   verboseRK   (    (    s   lib/python2.7/test/test_code.pyt	   test_mainº   s    t   __main__(   t   __doc__t   unittestRB   t   test.test_supportR    R   R   R	   R   t   TestCaseR   R(   R)   R;   t   NoneRN   R#   (    (    (    s   lib/python2.7/test/test_code.pyt   <module>Q   s   				*