
    }e=                        d Z ddlmZ ddlmZ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mZ ddlmZ ddlmZmZ dd	lmZ dad
 Z G d d      ZdZd Zd Zd Zd Zd Zd Zd Zd Zd Z G d de       Z! G d de       Z" G d de       Z# G d de       Z$ G d d       Z% G d! d"e       Z&d# Z'd$ Z(d% Z)d& Z*d' Z+dAd(Z,d) Z-d* Z.d+ Z/d, Z0d- Z1d. Z2d/ Z3d0 Z4ed1        Z5d2 Z6d3 Z7d4 Z8d5 Z9d6 Z:d7 Z;d8 Z<d9 Z=d: Z>d; Z?d< Z@d= ZAd> ZBd? ZCd@ ZDy)Bz/Tests for the object inspection functionality.
    )contextmanager)	signature	Signature	ParameterN   )oinspect)	decorator)AssertPrintsAssertNotPrints)compress_userc                  ,    t        j                         ay N)r   	Inspector	inspector     B/usr/lib/python3/dist-packages/IPython/core/tests/test_oinspect.pysetup_moduler      s    ""$Ir   c                       e Zd ZdZy)SourceModuleMainTest__main__N)__name__
__module____qualname__r   r   r   r   r   #   s    Jr   r   /   c                      t        j                  t              t        dz   k(  sJ t        j                  t              J t        j                  t
              J t        j                  t                     J y )N   )r   find_source_linestest_find_source_linesTHIS_LINE_NUMBERtyper   r   r   r   r   r   2   sm    %%&<=AQTUAUUUU%%d+333%%&:;CCC%%&:&<=EEEr   c                      t        j                  t              J t        j                  t              J t        j                  t                     J y r   )r   	getsourcer!   r   r   r   r   test_getsourcer$   9   sL    d#+++23;;;245===r   c                      t        j                  t              5  t        j                  t
               ddd       t        j                  t              5  t        j                  t               ddd       y# 1 sw Y   ExY w# 1 sw Y   yxY w)zACheck oinspect.find_file/getsource/find_source_lines expectationsN)pytestraises	TypeErrorinspectgetfiler!   OSErrorr   r   r   r   %test_inspect_getfile_raises_exceptionr,   ?   sb    	y	! 	w	 .,-. . . .s   A8B8BBc                 j    t         j                  j                  t        j                  dd|             S )Nz.py[co]$z.py)ospathnormcaseresub)fnames    r   pyfiler4   I   s$    77BFF:ue<==r   c                 6    t        |       t        |      k(  sJ y r   )r4   )f1f2s     r   match_pyfilesr8   M   s    ":###r   c                  6   t        t        j                  t              t        j
                  j                  t                     t        j                  t              J t        j                  t              J t        j                  t                     J y r   )
r8   r   	find_filetest_find_filer.   r/   abspath__file__r!   r   r   r   r   r;   r;   Q   sn    ($$^4bggooh6OPd#+++23;;;245===r   c                      t         d        } | d        }t        t        j                  |      t        j
                  j                  t                     |j                  dk(  sJ y )Nc                       fd}|S )Nc                       | i |S r   r   )akwfs     r   wrapperz9test_find_file_decorated1.<locals>.noop1.<locals>.wrapper\   s    a;2;r   r   )rC   rD   s   ` r   noop1z(test_find_file_decorated1.<locals>.noop1Z   s    	r   c                      y)My docstringNr   xs    r   rC   z$test_find_file_decorated1.<locals>.f`       r   rG   	r	   r8   r   r:   r.   r/   r<   r=   __doc__)rE   rC   s     r   test_find_file_decorated1rM   X   s^     
   ($$Q')BC99&&&r   c                      t         d        } | | | d                      }t        t        j                  |      t        j
                  j                  t                     |j                  dk(  sJ y )Nc                      | |i |S r   r   )rC   rA   rB   s      r   noop2z(test_find_file_decorated2.<locals>.noop2j   s    !{r{r   c                      y)My docstring 2Nr   rH   s    r   rC   z$test_find_file_decorated2.<locals>.fn   rJ   r   rR   rK   )rP   rC   s     r   test_find_file_decorated2rS   h   so      

    ($$Q')BC99((((r   c                  \    t         j                  d      } t        j                  |       J y )Nrun)ipfind_line_magicr   r:   )rU   s    r   test_find_file_magicrX   x   s)    


U
#Cc"...r   c                   &    e Zd ZdZddZd ZddZy)CallThis is the class docstring.c                      y)z"This is the constructor docstring.Nr   )selfrI   ys      r   __init__zCall.__init__   rJ   r   c                      y)zThis is the call docstring.Nr   )r]   rA   rB   s      r   __call__zCall.__call__   rJ   r   c                      yzSome method's docstringNr   r]   rI   zs      r   methodzCall.method   rJ   r   N   r   )r   r   r   rL   r_   ra   rf   r   r   r   rZ   rZ      s    &1*&r   rZ   c                   J    e Zd ZdZ e edej                        g      Zd Zy)HasSignaturer[   testc                      y)zThis is the init docstringNr   )r]   argss     r   r_   zHasSignature.__init__   rJ   r   N)	r   r   r   rL   r   r   POSITIONAL_OR_KEYWORD__signature__r_   r   r   r   rk   rk      s$    &y1P1PQRSM)r   rk   c                       e Zd ZddZy)SimpleClassc                      yrc   r   rd   s      r   rf   zSimpleClass.method   rJ   r   Nri   )r   r   r   rf   r   r   r   rr   rr      s    &r   rr   c                       e Zd Zd Zy)Awkwardc                     t        |      r   )	Exception)r]   names     r   __getattr__zAwkward.__getattr__   s    or   N)r   r   r   ry   r   r   r   ru   ru      s    r   ru   c                       e Zd ZdZd Zd Zy)
NoBoolCallzH
    callable with `__bool__` raising should still be inspect-able.
    c                      y)zdoes nothingNr   r]   s    r   ra   zNoBoolCall.__call__   s    r   c                     t        d      )zjust raise NotImplementedzMust be implemented)NotImplementedErrorr}   s    r   __bool__zNoBoolCall.__bool__   s    !"788r   N)r   r   r   rL   ra   r   r   r   r   r{   r{      s    9r   r{   c                       e Zd ZdZddZd Zy)
SerialLiarzAttribute accesses always get another copy of the same class.

    unittest.mock.call does something similar, but it's not ideal for testing
    as the failure mode is to eat all your RAM. This gives up after 10k levels.
    c                 d    |dkD  rt        d      || _        || _        t        |d   |      |d<   y )Ni'  z)Nose too long, honesty is the best policyr   )RuntimeErrormax_fibbing_twig	lies_toldmax)r]   r   r   s      r   r_   zSerialLiar.__init__   s=    uJKK 0"!"21"5yAr   c                 H    t        | j                  | j                  dz         S )Nrh   )r   r   r   )r]   items     r   ry   zSerialLiar.__getattr__   s    $//!1CDDr   N)r   )r   r   r   rL   r_   ry   r   r   r   r   r      s    
BEr   r   c                     t         j                  t        d      } | d   dk(  sJ t        t	        t                    }| d   |k(  sJ t        j                  d| d         sJ t        }|j                  d      r|d	d
 }| d   j                         t        |      j                         k(  sJ | d   d	k(  sJ | d   t        j                  k(  sJ | d   d	k(  sJ | d   du sJ | d   dk(  sJ | d   t        j                  j                  k(  sJ t         j                  t        d      } | d   J | d   d	k(  sJ t        d      }d|_        t         j                  |      } | d   dk(  sJ | d   dk(  sJ | d   t        j                  k(  sJ | d   t        j                  j                  k(  sJ | d   t        j                  j                  k(  sJ y	)z?Check that Inspector.info fills out various fields as expected.rZ   oname	type_namer!   
base_classzE<class 'IPython.core.tests.test_oinspect.Call'( at 0x[0-9a-f]{1,9})?>string_formz.pycNfile
definition	docstringsourceisclassTinit_definitionzCall(x, y=1)init_docstringrh   detail_levelzModified instance docstringclass_docstringcall_docstring)r   inforZ   strr!   r1   searchr=   endswithlowerr   rL   r_   ra   )iexpected_classr3   cs       r   	test_infor      s   t6*A[>V###d_N\?n,,,99O	-   E~~fcr
 V9??e 4 : : <<<<\?d"""[>T\\)))X;$Y<4>111$--"7"7777t!,AX;"""[>T!!!QA-AIqA[>V###[>::::4<<///$--"7"7777$--"7"7777r   c                      t         j                  t        d      } | d   dk(  sJ | d   t        j                  j                  k(  sJ y )Nrk   r   zHasSignature(test)r   )r   r   rk   r_   rL   )r   s    r   test_class_signaturer      sG    >>,7D!"&:::: !\%:%:%B%BBBBr   c                  >    t         j                  t                      y r   )r   r   ru   r   r   r   test_info_awkwardr      s    NN79r   c                  >    t         j                  t                      y r   )r   r   r{   r   r   r   test_bool_raiser      s    NN:< r   c                  Z    dg} t         j                  t        |              | d   dk  sJ y )Nr   i(#  )r   r   r   )fib_trackers    r   test_info_serialliarr      s.    #KNN:k*+ q>D   r   c                      y)zA simple function.Nr   )rI   r^   rA   rB   s       r   support_function_oner      rJ   r   c                      t         t               j                  t        t        j
                  fD ]  } t        j                  |       }|d   J  y )Ncall_def)r   rr   rf   anyr   upperr   r   )objr   s     r   test_calldef_noner      sD    $km&:&:CK %NN3}$$$%r   c                     y r   r   )poskwonlys     r   f_kwargr     s    r   c                  N    t         j                  t        d      } | d   dk(  sJ y )Nr   r   r   zf_kwarg(pos, *, kwonly))r   r   r   r   s    r   test_definition_kwonlyargsr     s'    wi0A\?7777r   c                  $    G d dt               }  G d dt               } G d dt               } |        } |       } |       }t        j                  |      dk(  sJ t        j                  |      dk(  sJ t        j                  |      dk(  sJ y )	Nc                       e Zd ZdZy)test_getdoc.<locals>.Astandard docstringN)r   r   r   rL   r   r   r   Ar     s     r   r   c                       e Zd ZdZd Zy)test_getdoc.<locals>.Br   c                      y)Ncustom docstringr   r}   s    r   getdocztest_getdoc.<locals>.B.getdoc  s    %r   Nr   r   r   rL   r   r   r   r   Br     s
     	&r   r   c                       e Zd ZdZd Zy)test_getdoc.<locals>.Cr   c                      y r   r   r}   s    r   r   ztest_getdoc.<locals>.C.getdoc  s    r   Nr   r   r   r   Cr     s
     	r   r   r   r   )objectr   r   )r   r   r   rA   br   s         r   test_getdocr   
  s    F &F &
F 
 	
A	A	A??1!5555??1!3333??1!5555r   c                  P    t         j                  t               d      } | d   J y )Nrh   r   r   )r   r   propertyr   s    r   !test_empty_property_has_no_sourcer   "  s&    xz2AX;r   c                  <   d  G fddt               } t        j                  | j                  d      }d|d   v sJ d|d   v sJ t        j                  | j                  d      }d	|d   v sJ t        j                  | j
                  d      }d
|d   v sJ y )Nc                     | |z   S )zAdds two numbersr   )rA   r   s     r   
simple_addz)test_property_sources.<locals>.simple_add*  s    1ur   c                   x    e Zd Zed        Zej                  d       Z eej                        Z eW        Z	y) test_property_sources.<locals>.Ac                      yNbarr   r}   s    r   fooz$test_property_sources.<locals>.A.foo/  s    r   c                     t        | d|      S r   )setattr)r]   vs     r   <lambda>z)test_property_sources.<locals>.A.<lambda>3  s    ua)@ r   N)
r   r   r   r   r   setterr   r   dnameadder)r   s   r   r   r   .  s>    		 
	 jj@A)$r   r   rh   r   zdef foo(self):r   zlambda self, v:zdef getdoc(obj)zdef simple_add(a, b))r   r   r   r   r   r   )r   r   r   s     @r   test_property_sourcesr   '  s    %F % 	quu1-Aq{***(+++qwwQ/A(+++qwwQ/A!Qx[000r   c                       G d dt               }  |        t        j                  d<   dt        j                  dd      d   k(  sJ | t        j                  d	<   dt        j                  d
d      d   k(  sJ y )Nc                       e Zd Zed        Zy)@test_property_docstring_is_in_info_for_detail_level_0.<locals>.Ac                      y)This is `foobar` property.Nr   r}   s    r   foobarzGtest_property_docstring_is_in_info_for_detail_level_0.<locals>.A.foobarE  s     r   N)r   r   r   r   r   r   r   r   r   r   D  s    		 
	r   r   a_objr   za_obj.foobarr   r   r   a_clsza_cls.foobar)r   rV   user_nsobject_inspect)r   s    r   5test_property_docstring_is_in_info_for_detail_level_0r   C  s    F  #BJJw$^!<[I	J	J BJJw$^!<[I	J	Jr   c                  6    d } t         j                  | d       y )Nc                       y r   r   r   r   r   r   ztest_pdef.<locals>.fooY  rJ   r   r   )r   pdefr   s    r   	test_pdefr   W  s    NN3r   c               +     K   	 t        t        j                  j                               }t        j                  j	                  |        d t        t        j                  j                               |z
  }|D ]  }t        j                  |=  y# t        t        j                  j                               z
  }|D ]  }t        j                  |=  w xY ww)z
    On exit delete all the keys that were not in user_ns before entering.

    It does not restore old values !

    Parameters
    ----------

    **kwargs
        used to update ip.user_ns

    N)setrV   r   keysupdate)kwargsknownaddedks       r   cleanup_user_nsr   ]  s     BJJOO%&


&!BJJOO%&. 	A

1	 BJJOO%&. 	A

1	s   CA
B ACACCc                       G d d      }  |        }t        |      5  t        j                  ddd       ddd       y# 1 sw Y   yxY w)	z8
    Test that bool method is not called on parent.
    c                       e Zd ZdZd Zy)(test_pinfo_bool_raise.<locals>.RaiseBoolNc                     t        d      )Nz#pinfo should not access this method)
ValueErrorr}   s    r   r   z1test_pinfo_bool_raise.<locals>.RaiseBool.__bool__}  s    BCCr   )r   r   r   attrr   r   r   r   	RaiseBoolr   z  s    	Dr   r  )
raise_boolpinfozraise_bool.attrr   r   N)r   rV   _inspect)r  r  s     r   test_pinfo_bool_raiser  u  sK    
D D J	J	/ @
G.Q?@ @ @s   A  A	c                     d } | g}t        |      5  t        d      5  t        j                  ddd       d d d        d d d        dt        j                  j                         vsJ y # 1 sw Y   4xY w# 1 sw Y   8xY w)	Nc                       y)z
        MARKER
        Nr   r   r   r   dummyz"test_pinfo_getindex.<locals>.dummy  rJ   r   	containerMARKERr  zcontainer[0]r   r   r  )r   r
   rV   r  r   r   r
  r  s     r   test_pinfo_getindexr    s    
 I	9	- A(# 	AKKaK@	AA bjjoo////	A 	AA As!   A7A+A7+A4	0A77B c                      d } | g}t        |      5  t        d      5  t        j                  d       d d d        d d d        dt        j                  j                         vsJ y # 1 sw Y   4xY w# 1 sw Y   8xY w)Nc                       y)z
        MARKER 2
        Nr   r   r   r   r
  z"test_qmark_getindex.<locals>.dummy  rJ   r   r  zMARKER 2zcontainer[0]?r  r   r
   rV   run_cellr   r   r  s     r   test_qmark_getindexr    sq    
 I	9	- )*% 	)KK(	)) bjjoo////	) 	)) )!   A4A(A4(A1	-A44A=c                      d } | g}t        |      5  t        d      5  t        j                  d       d d d        d d d        dt        j                  j                         vsJ y # 1 sw Y   4xY w# 1 sw Y   8xY w)Nc                       y)z
        MARKER 3
        Nr   r   r   r   r
  z*test_qmark_getindex_negatif.<locals>.dummy  rJ   r   r  zMARKER 3zcontainer[-1]?r  r  r  s     r   test_qmark_getindex_negatifr    sr    
 I	9	- **% 	*KK()	** bjjoo////	* 	** *r  c                  f    ddl m}  | t        j                  d<   t        j	                  ddd       y )Nrh   	nonascii2r  r  r   ) r  rV   r   r  r  s    r   test_pinfo_nonasciir    s%    'BJJ{KK1K5r   c                  0    t         j                  dd       y)zO
    type can fail in various edge case, for example `type.__subclass__()`
    r  r!   N)rV   r  r   r   r   test_pinfo_typer    s     KK r   c                      t        d      5  t        j                  ddd       ddd       t        d      5  t        j                  ddd       ddd       y# 1 sw Y   6xY w# 1 sw Y   yxY w)zFDocstring should be included with detail_level=1 if there is no source
Docstring:r  z
str.formatr   r   Nrh   r
   rV   r  r   r   r   test_pinfo_docstring_no_sourcer#    se    	l	# ;
G\:;	l	# ;
G\:; ;; ;; ;   AA&A#&A/c                     d } | t         j                  d<   t        d      5  t         j                  ddd       ddd       t        d      5  t         j                  ddd	       ddd       t	        d      5  t         j                  ddd	       ddd       y# 1 sw Y   bxY w# 1 sw Y   BxY w# 1 sw Y   yxY w)
zGDocstring should not be included with detail_level=1 if source is foundc                       y)zfoo has a docstringNr   r   r   r   r   z.test_pinfo_no_docstring_if_source.<locals>.foo  rJ   r   r   r!  r  r   r   NSource:rh   )rV   r   r
   r  r   r   s    r   !test_pinfo_no_docstring_if_sourcer(    s    " BJJu	l	# 4
GU34	i	  4
GU34		& 4
GU34 4	4 44 44 4s#   BB(:B4B%(B14B=c                     d} t         j                  |        t         j                  d       t        d      5  t        d      5  t         j	                  ddd       d	d	d	       t        d      5  t         j	                  ddd
       d	d	d	       t        d      5  t         j	                  ddd       d	d	d	       d	d	d	       t        d      5  t        d      5  t         j	                  ddd
       d	d	d	       d	d	d	       y	# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   jxY w# 1 sw Y   nxY w# 1 sw Y   BxY w# 1 sw Y   y	xY w)z< Docstring should be displayed if source info not available zclass Foo(object):
                  """ This is a docstring for Foo """
                  def bar(self):
                      """ This is a docstring for Foo.bar """
                      pass
              zfoo = Foo()r'  r!  r  r   r   r   Nrh   zfoo.bar)rV   r  r   r
   r  obj_defs    r   ,test_pinfo_docstring_if_detail_and_no_sourcer,    s    G KKKK		# <,' 	8KKQK7	8,' 	8KKQK7	8,' 	<KKK;	<< 
	& <)$ 	<KKK;	<< <	8 	8	8 	8	< 	<< <	< 	<< <sw   D*DD*0D	D*D5D*ED65ED	D*D	D*D'	#D**D36D?	;EEc                  r   d} t         j                  |        t         j                  d       t        d      5  t         j                  dd       d d d        t        d      5  t         j                  dd       d d d        t        d      5  t         j                  d       d d d        t        d      5  t         j                  d	       d d d        t        d
      5  t         j                  d       d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   txY w# 1 sw Y   WxY w# 1 sw Y   y xY w)Na2  class Bar:
    __custom_documentations__ = {
     "prop" : "cdoc for prop",
     "non_exist" : "cdoc for non_exist",
    }
    @property
    def prop(self):
        '''
        Docstring for prop
        '''
        return self._prop
    
    @prop.setter
    def prop(self, v):
        self._prop = v
    z	b = Bar()zDocstring:   cdoc for propr  zb.propzDocstring:   cdoc for non_existzb.non_existzb.prop?zb.non_exist?zDocstring:   <no docstring>zb.undefined?)rV   r  r
   run_line_magicr*  s    r   test_pinfo_docstring_dynamicr/    s
   G  KKKK	2	3 -
'8,- 
7	8 2
'=12 
2	3 
I 
7	8 $
N#$ 
3	4 $
N#$ $- -2 2 $ $$ $s;   C="D	D5D!D-=D	DD!D*-D6c                      t        d      5  t        j                  ddd       d d d        t        d      5  t        j                  ddd       d d d        y # 1 sw Y   6xY w# 1 sw Y   y xY w)Nr!  r  lsmagicr   r   r'  rh   r"  r   r   r   test_pinfo_magicr2    sg    	l	# 8
GYQ78 
i	  8
GYQ78 88 88 8r$  c                  L    t         j                  t              } | d   }d|vsJ y )Nr   z[0m)r   r   rk   r   init_defs     r   test_init_colorsr6    s)    >>,'D%&H   r   c                  H    t         j                  t              } | d   }|J y )Nr   )r   r   listr4  s     r   test_builtin_initr9    s'    >>$D%&Hr   c                  l    dd} t        j                  t        |       | j                        }|dk(  sJ y )Nc                      y r   r   )rA   s    r   	short_funz.test_render_signature_short.<locals>.short_fun&  rJ   r   zshort_fun(a=1)rg   )r   _render_signaturer   r   )r<  sigs     r   test_render_signature_shortr?  %  s7    

$
$)C """"r   c            	          ddl m}  	 	 d	dt        dt        d| t           dt        fd}t        j                  t        |      |j                        }d}||k(  sJ y )
Nr   )Optionala_really_long_parameterand_another_long_onelet_us_make_sure_this_is_looongreturnc                      y r   r   )rB  rC  rD  s      r   long_functionz1test_render_signature_long.<locals>.long_function1  s     r   zlong_function(
    a_really_long_parameter: int,
    and_another_long_one: bool = False,
    let_us_make_sure_this_is_looong: Optional[str] = None,
) -> bool)FN)	typingrA  intboolr   r   r=  r   r   )rA  rG  r>  expecteds       r   test_render_signature_longrL  .  sr     &+9=!$" *2# 
	 
$
$- CH (??r   ri   )ErL   
contextlibr   r)   r   r   r   r.   r&   r1   sysr  r   r	   IPython.testing.toolsr
   r   IPython.utils.pathr   r   r   r   r    r   r$   r,   r4   r8   r;   rM   rS   rX   r   rZ   rk   rr   ru   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r#  r(  r,  r/  r2  r6  r9  r?  rL  r   r   r   <module>rQ     sm   & 3 3  	  	 
   ? , 	%
   F>.>$>' ) /
&6 
&)6 )&& &
f 9 9E E("8JC!!%	8
60
18(  .@"
0
0
06!;4<2"$J8! #r   