
    7gI                       d Z ddl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	m
Z
mZ ddlmZ ddlmZ ddlmZmZmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZmZmZ ddl m!Z! dZ"dZ#er ejH                  dde%       ddZ&ddZ' G d dejP                        Z)ddZ*	 	 	 d	 	 	 	 	 	 	 	 	 ddZ+d dZ,d!dZ-d"d#dZ.d"d$dZ/	 d%	 	 	 	 	 	 	 d&dZ0y)'zThe AstroidBuilder makes astroid from living object and / or from _ast.

The builder is not thread safe and can't be used to parse different sources
at the same time.
    )annotationsN)IteratorSequence)TextIOWrapper)detect_encoding)basesmodutilsnodesraw_building	rebuilderutil)ParserModuleget_parser_module)
PY312_PLUS)AstroidBuildingErrorAstroidSyntaxErrorInferenceError)AstroidManager__z#@ignorezinvalid escape sequencec                    t        | d      5 }t        |j                        d   }d d d        t        | d       }|j                         }|||fS # 1 sw Y   ,xY w)Nrbr   )newlineencoding)openr   readlineread)filenamebyte_streamr   streamdatas        /lib/python3.12/site-packages/astroid/builder.pyopen_source_filer#   *   sW    	h	";#7#78; 
(D8<F;;=D8T!!	 
	s   AAc                    	 | j                         }|r||D ch c]  }|j                   c}vry| j                         dk7  S c c}w # t        $ r Y #w xY w)NFzbuiltins.object)slotsvalueNotImplementedErrorqname)nodeattrnamer%   slots       r"   _can_assign_attrr,   3   s`    

 XU%CUTdjjU%CC::<,,, &D  s   A	 A		AAc                       e Zd ZdZ	 d
	 	 	 	 	 d fdZ	 d	 	 	 	 	 ddZdddZ	 d	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	ddZ
dd	Z xZS )AstroidBuildera  Class for building an astroid tree from source code or from a live module.

    The param *manager* specifies the manager class which should be used.
    If no manager is given, then the default one will be used. The
    param *apply_transforms* determines if the transforms should be
    applied after the tree was built from source or from a live object,
    by default being True.
    c                    t         |   |       || _        t        j                  j
                  st        j                          y y N)super__init___apply_transformsr   InspectBuilderbootstrapped_astroid_bootstrapping)selfmanagerapply_transforms	__class__s      r"   r2   zAstroidBuilder.__init__H   s:     	!!1**77//1 8    c                J   d}t        |dd      }t        |dd      }|r7|xs |j                  }|j                  |      }|r| j                  |||      }|r|pt        j
                  j                  t        j                  |            \  }}|dv r7t        j
                  j                  |dz         r| j                  |dz   |      }|;| j                  |||      }| j                  r| j                  j                  |      }t        |t         j"                        sJ |S )z/Build an astroid from a living module instance.N__file__
__loader__)path>   .pyc.pyo.pyrB   modnamer?   )getattr__name__
get_sourcestring_buildosr?   splitextr	   _path_from_filenameexists
file_buildinspect_buildr3   _managervisit_transforms
isinstancer
   Module)	r7   modulerD   r)   r?   loadersourcepath_exts	            r"   module_buildzAstroidBuilder.module_buildP   s    vz40t4 0G&&w/F((t(D<D,))(*F*Ft*LMJE3--"''..2Ouu}g>< %%fgD%ID%% }}55d;$---r;   c                H   	 t        |      \  }}}|5  |%	 dj                  t        j                  |            }| j                  |||      \  }}| j!                  |||      cddd       S # t        $ r}t        d|||      |d}~wt        t        f$ r}t        d|||      |d}~wt        $ r}t        d|      |d}~ww xY w# t        $ rB t        j                  j                  t        j                  j                  |            d   }Y w xY w# 1 sw Y   yxY w)	zyBuild astroid from a source code file (i.e. from an ast).

        *path* is expected to be a python source file
        z#Unable to load file {path}:
{error})rD   r?   errorNzBPython 3 encoding specification error or unknown encoding:
{error}z.Wrong or no encoding specified for {filename}.)r   .r   )r#   OSErrorr   SyntaxErrorLookupErrorr   UnicodeErrorjoinr	   modpath_from_fileImportErrorrI   r?   rJ   basename_data_build_post_build)	r7   r?   rD   r    r   r!   excrS   builders	            r"   rM   zAstroidBuilder.file_buildm   s6   
	%5d%;"FHd* J!hhx'A'A$'GHG #..tWdCOFG##FGX> V)  	&6	
  [) 	$   	&@4	 # J gg..rww/?/?/EFqIGJ Vs]   A- D$C
(D-	C6BCB((C4CC
ADDDDD!c                    | j                  |||      \  }}|j                  d      |_        | j                  ||d      S )z&Build astroid from source code string.zutf-8)rd   encode
file_bytesre   )r7   r!   rD   r?   rS   rg   s         r"   rH   zAstroidBuilder.string_build   sB     **4$? KK099r;   c                   ||_         | j                  j                  |       |j                  D ]Q  }|j                  dk(  r/|j
                  D ]   \  }}|j                  j                  |       " | j                  |       S |j                  D ]  }| j                  |        | j                  r| j                  j                  |      }|S )zAHandles encoding and delayed nodes after a module has been built.
__future__)file_encodingrO   cache_module_import_from_nodesrD   namesfuture_importsaddadd_from_names_to_locals_delayed_assattrdelayed_assattrr3   rP   )r7   rS   rg   r   	from_nodesymbol_delayeds           r"   re   zAstroidBuilder._post_build   s      (""6* 33I  L0!*IFA))--f5 "1)))4	 4 //G  ) 0 !!]]33F;Fr;   c                   	 t        |d|      \  }}| t
        j                  j                  |      }nd}|j                  d      r|dd }d}nH|duxrB t
        j                  j                  t
        j                  j                  |            d	   d
k(  }t        j                  | j                  ||      }	|	j                  ||||      }
|
|	fS # t        t        t        f$ r}t	        d||||      |d}~ww xY w)z4Build tree node from data and add some informations.T)type_commentsrD   z#Parsing Python code failed:
{error})rU   rD   r?   rZ   Nz<?>z	.__init__ir   r2   )_parse_string	TypeError
ValueErrorr]   r   rI   r?   abspathendswithrJ   rc   r   TreeRebuilderrO   visit_module)r7   r!   rD   r?   r)   parser_modulerf   	node_filepackagerg   rS   s              r"   rd   zAstroidBuilder._data_build   s   	"/D'#D- -IIK(crlGG D  NGG$$RWW%5%5d%;<Q?:M  ))$--M%%dGYHw/ :{3 	$6 	s   C C;%C66C;c                   dddfd}|j                   sJ |j                  D ]  \  }}|dk(  rp	 |j                         }|j	                         D ]K  }|j                   j                  ||        ||j                   j                         j                  |          M {|j                   j                  |xs ||        ||j                   j                         j                  |xs |           y# t        $ r Y w xY w)zeStore imported names to the locals.

        Resort the locals if coming from a delayed node
        c                "    | j                   xs dS )Nr   )
fromlinenor)   s    r"   	_key_funcz:AstroidBuilder.add_from_names_to_locals.<locals>._key_func   s    ??'a'r;   c                *    | j                         y )N)key)sort)my_listr   s    r"   sort_localsz<AstroidBuilder.add_from_names_to_locals.<locals>.sort_locals   s    LLYL'r;   *N)r)   nodes.NodeNGreturnint)r   zlist[nodes.NodeNG]r   None)parentrp   do_import_moduler   public_names	set_localscopelocals)r7   r)   r   nameasnameimportedr   s         @r"   rs   z'AstroidBuilder.add_from_names_to_locals   s    	(	( {{{ JJLD&s{#446H %113DKK))$5 1 1 3 : :4 @A 4 %%fnd;DKK--/66v~FG ' , s   C55	D Dc                   ddl m} 	 |j                  j                         D ]  }t	        |t
        j                        r	 t        |      t        j                  |j                  hv r/|j                  }|j                  }t        ||j                        slvt	        |t        j                        rt	        |t        j                  t
        j                  f      r|j                   r|j                  }n|j"                  }|j'                  |j                  g       }||v r|j)                  |        y# t$        $ r Y $w xY w# t*        $ r Y yw xY w)z_Visit a AssAttr node.

        This adds name to locals and handle members definition.
        r   )objectsN)astroidr   exprinferrQ   r   UninferableBasetyper   InstanceExceptionInstance_proxiedinstance_attrsr,   r*   Proxyis_functionr   AttributeError
setdefaultappendr   )r7   r)   r   inferrediattrsvaluess         r"   ru   zAstroidBuilder.delayed_assattr   s   
 	$	 IIOO-h(<(<= H~%..':S:S)TT#+#4#4!)!8!8/$--H$#Henn= !#Hu{{D<P<P.QR !--!)!8!8!)  **4=="=6>d#7 .* &   		sZ   9E
 AD:E
 D:2E
 3*D:E
 %D:6E
 :	EE
 EE
 
	EE)NT)r8   zAstroidManager | Noner9   boolr   r   r0   )rS   ztypes.ModuleTyperD   
str | Noner   nodes.Module)r?   strrD   r   r   r   ) N)r!   r   rD   r   r?   r   r   r   )rS   r   rg   zrebuilder.TreeRebuilderr   r   r   r   )r!   r   rD   r   r?   r   r   z,tuple[nodes.Module, rebuilder.TreeRebuilder])r)   znodes.ImportFromr   r   )r)   znodes.AssignAttrr   r   )rF   
__module____qualname____doc__r2   rX   rM   rH   re   rd   rs   ru   __classcell__)r:   s   @r"   r.   r.   >   s     OS2,2GK2	2 ?C&1;	:$?N @D::"%:2<:	:"-DPS	*"%-7	5BH4%r;   r.   c                \    t        j                  | |d      }|j                  g d        |S )NT)r?   r   )bodydoc_node)r
   rR   postinit)r   r?   rS   s      r"   build_namespace_package_moduler     s)    \\$T48F
OOdO+Mr;   c                ~    t        j                  |       } t        t               |      }|j	                  | ||      S )aw  Parses a source string in order to obtain an astroid AST from it.

    :param str code: The code for the module.
    :param str module_name: The name for the module, if any
    :param str path: The path for the module
    :param bool apply_transforms:
        Apply the transforms for the give code. Use it if you
        don't want the default transforms to be applied.
    )r8   r9   rC   )textwrapdedentr.   r   rH   )codemodule_namer?   r9   rg   s        r"   parser     s?     ??4 D 3CG kEEr;   c              #  r  K   t        | t        j                        rt        | j                  t        j                        r| j                  j
                  t        k(  r| j                  d   }| j                  sJ | j                  |_        | j                  j                  D ]b  }t        | j                  |      }t        |t              rt        |      D ]  \  }}|| u s|||<    G|| u sLt        | j                  ||       d | y| j                         D ]  }t        |      E d{     y7 w)a  Find expressions in a call to _TRANSIENT_FUNCTION and extract them.

    The function walks the AST recursively to search for expressions that
    are wrapped into a call to _TRANSIENT_FUNCTION. If it finds such an
    expression, it completely removes the function call node from the tree,
    replacing it by the wrapped expression inside the parent.

    :param node: An astroid node.
    :type node:  astroid.bases.NodeNG
    :yields: The sequence of wrapped expressions on the modified tree
    expression can be found.
    r   N)rQ   r
   CallfuncNamer   _TRANSIENT_FUNCTIONargsr   _astroid_fieldsrE   list	enumeratesetattrget_children_extract_expressions)r)   	real_exprr   childidxcompound_childs         r"   r   r   /  s      	4$tyy%**-IINN11IIaL	{{{;;	 KK//DDKK.E%&+4U+;'C%-%.c
 ,< $T95 0 &&(E+E222 )2s   CD7!D7.?D7-D5.D7c                   t        | t        j                  t        j                  t        j                  f      r| j
                  }n| j                  }||k(  r| S | j                         D ]  }t        ||      }|s|c S  y)a  Extracts the statement on a specific line from an AST.

    If the line number of node matches line, it will be returned;
    otherwise its children are iterated and the function is called
    recursively.

    :param node: An astroid node.
    :type node: astroid.bases.NodeNG
    :param line: The line number of the statement to extract.
    :type line: int
    :returns: The statement on the line, or None if no statement for the line
      can be found.
    :rtype:  astroid.bases.NodeNG or None
    N)	rQ   r
   ClassDefFunctionDef	MatchCaser   linenor   _find_statement_by_line)r)   line	node_liner   results        r"   r   r   W  sp     $):):EOOLM
 OO	KK	D""$(5M %
 r;   c                L   dd}g }t        | j                               D ]=  \  }}|j                         j                  t              s*|j                  |dz          ? t        | |      }|j                  st        d      g }|r|D cg c]  }t        ||       }}|j                  t        |             |s|j                  |j                  d          |D cg c]
  } ||       }}|D cg c]  }||	 }	}t        |	      dk(  r|	d   S |	S c c}w c c}w c c}w )ah  Parses some Python code as a module and extracts a designated AST node.

    Statements:
     To extract one or more statement nodes, append #@ to the end of the line

     Examples:
       >>> def x():
       >>>   def y():
       >>>     return 1 #@

       The return statement will be extracted.

       >>> class X(object):
       >>>   def meth(self): #@
       >>>     pass

      The function object 'meth' will be extracted.

    Expressions:
     To extract arbitrary expressions, surround them with the fake
     function call __(...). After parsing, the surrounded expression
     will be returned and the whole AST (accessible via the returned
     node's parent attribute) will look like the function call was
     never there in the first place.

     Examples:
       >>> a = __(1)

       The const node will be extracted.

       >>> def x(d=__(foo.bar)): pass

       The node containing the default argument will be extracted.

       >>> def foo(a, b):
       >>>   return 0 < __(len(a)) < b

       The node containing the function call 'len' will be extracted.

    If no statements or expressions are selected, the last toplevel
    statement will be returned.

    If the selected statement is a discard statement, (i.e. an expression
    turned into a statement), the wrapped expression is returned instead.

    For convenience, singleton lists are unpacked.

    :param str code: A piece of Python code that is parsed as
    a module. Will be passed through textwrap.dedent first.
    :param str module_name: The name of the module.
    :returns: The designated node from the parse tree, or a list of nodes.
    c                R    t        | t        j                        r| j                  S | S r0   )rQ   r
   Exprr&   r   s    r"   _extractzextract_node.<locals>._extract  s    dEJJ'::r;      )r   z"Empty tree, cannot extract from itr   )r)   nodes.NodeNG | Noner   r   )r   
splitlinesstripr   _STATEMENT_SELECTORr   r   r   r~   r   extendr   len)
r   r   r   requested_linesr   r   tree	extractedr)   extracted_without_nones
             r"   extract_noder   z  s#   l "$Ot01	T::<  !45""37+ 2 ;/D99=>>+-IETU_T,T48_	U )$/02',56ID$II6/8MytD<LdyM
!"a'%a((!! V 7Ms   DD4D!<D!c                H    t        | |      }t        |t              r|d   S |S )zDCall extract_node while making sure that only one value is returned.r   )r   rQ   r   )r   r   rets      r"   _extract_single_noder     s&    
t[
)C#t1vJr;   c                   t        |      }	 |j                  | dz   ||      }||fS # t        $ rX}t        j                  d|j
                  xs d      }|r|s t        d      }|j                  | dz   d      }Y d }~||fS d }~ww xY w)N)r{   
)r{   r   z	#\s+type:r   F)r   r   r]   researchtext)r!   r{   rD   r   parsedrf   type_annot_relateds          r"   r|   r|     s     &MBMG$$4K}w % 
 =    
G
  YY|SXX^D"})>$$TD[$F=  
Gs   ) 	B
ABB
)r   r   r   ztuple[TextIOWrapper, str, str])r)   znodes.ClassDefr*   r   r   r   )r   r   r?   zSequence[str]r   r   )r   NT)
r   r   r   r   r?   r   r9   r   r   r   )r)   r   r   zIterator[nodes.NodeNG])r)   r   r   r   r   r   )r   )r   r   r   r   r   z!nodes.NodeNG | list[nodes.NodeNG])r   r   r   r   r   r   )TN)r!   r   r{   r   rD   r   r   ztuple[ast.Module, ParserModule])1r   rl   r   astrI   r   r   typeswarningscollections.abcr   r   ior   tokenizer   r   r   r	   r
   r   r   r   astroid._astr   r   astroid.constr   astroid.exceptionsr   r   r   astroid.managerr   r   r   filterwarningsSyntaxWarningr#   r,   r4   r.   r   r   r   r   r   r   r|    r;   r"   <module>r     s  
 # 
 	 	    .  $ I I 8 $ W W *
    HH&?O"-R\00 Rj !	F
FF F 	F
 F,%3P FS"l BF!
!"!4>!$!r;   