
    IiV                         S r SSKrSSKrSSKrSSKrSSKrSSKJrJrJr  SSK	J
r
JrJrJrJrJrJrJrJrJrJr  / SQrSSS.rSSS.rS	 H  rS\\'   S
\\'   M      " S S5      rg)a  
Safe(ish) evaluation of minimal Python code using Python's ast module.

This module provides an Interpreter class that compiles a restricted set of
Python expressions and statements to Python's AST representation, and then
executes that representation using values held in a symbol table.

The symbol table is a simple dictionary, giving a flat namespace.  This comes
pre-loaded with many functions from Python's builtin and math module.  If numpy
is installed, many numpy functions are also included.  Additional symbols can
be added when an Interpreter is created, but the user of that interpreter will
not be able to import additional modules.

Expressions, including loops, conditionals, and function definitions can be
compiled into ast node and then evaluated later, using the current values
in the symbol table.

The result is a restricted, simplified version of Python meant for numerical
calculations that is somewhat safer than 'eval' because many unsafe operations
(such as 'eval') are simply not allowed, and others (such as 'import') are
disabled by default, but can be explicitly enabled.

Many parts of Python syntax are supported, including:
     for loops, while loops, if-then-elif-else conditionals, with,
     try-except-finally
     function definitions with def
     advanced slicing:    a[::-1], array[-3:, :, ::2]
     if-expressions:      out = one_thing if TEST else other
     list, dict, and set comprehension

The following Python syntax elements are not supported:
     Import, Exec, Lambda, Class, Global, Generators,
     Yield, Decorators

In addition, while many builtin functions are supported, several builtin
functions that are considered unsafe are missing ('eval', 'exec', and
'getattr' for example) are missing.
    N)exc_infostderrstdout   )	HAS_NUMPYExceptionHolderReturnedNoneEmptymake_symbol_tablenumpyop2funcsafe_getattrsafe_formatvalid_symbol_name	Procedure)2argassertassign	attribute	augassignbinopboolopbreakbytescallcompareconstantcontinuedeletedictdictcompellipsisexcepthandlerexprextsliceforfunctiondefififexpimport
importfromindex	interruptlistlistcompmodulenamenameconstantnumpassraisereprreturnsetsetcompslicestr	subscripttrytupleunaryopwhilewithformattedvalue	joinedstrF)r*   r+   )r   r   r   r(   r)   r&   rB   r'   printr5   r/   r!   r9   r=   r@   rA   Tc                      \ rS rSrSr     SES jrS rSFS jrS rS r	SGS	 jr
S
 rSHS jrS rS rSIS jr\S 5       rS rS rS rSFS jrS rS rS rS rS rS rS rS rS rS rS r S r!S  r"S! r#S" r$S# r%S$ r&S% r'S& r(S' r)S( r*S) r+S* r,S+ r-S, r.S- r/S. r0S/ r1S0 r2S1 r3S2 r4S3 r5S4 r6S5 r7S6 r8S7 r9S8 r:S9 r;S: r<S; r=S< r>S= r?S> r@S? rAS@ rBSA rCSB rDSC rESDrFg)JInterpreterG   a  create an asteval Interpreter: a restricted, simplified interpreter
of mathematical expressions using Python syntax.

Parameters
----------
symtable : dict or `None`
    dictionary or SymbolTable to use as symbol table (if `None`, one will be created).
nested_symtable : bool, optional
    whether to use a new-style nested symbol table instead of a plain dict [False]
user_symbols : dict or `None`
    dictionary of user-defined symbols to add to symbol table.
writer : file-like or `None`
    callable file-like object where standard output will be sent.
err_writer : file-like or `None`
    callable file-like object where standard error will be sent.
use_numpy : bool
    whether to use functions from numpy.
max_statement_length : int
    maximum length of expression allowed [50,000 characters]
readonly_symbols : iterable or `None`
    symbols that the user can not assign to
builtins_readonly : bool
    whether to blacklist all symbols that are in the initial symtable
minimal : bool
    create a minimal interpreter: disable many nodes (see Note 1).
config : dict
    dictionay listing which nodes to support (see note 2))

Notes
-----
1. setting `minimal=True` is equivalent to setting a config with the following
   nodes disabled: ('import', 'importfrom', 'if', 'for', 'while', 'try', 'with',
   'functiondef', 'ifexp', 'listcomp', 'dictcomp', 'setcomp', 'augassign',
   'assert', 'delete', 'raise', 'print')
2. by default 'import' and 'importfrom' are disabled, though they can be enabled.
Nc           
         [         R                   " U(       a  [        O[        5      U l        Ub  U R                  R	                  U5        X R                  S'   Uc  0 nSU;   a  UR                  S5      n[        U5      S:  a  UR                  5        Hy  u  pUR                  S5      (       a'  USS  nU[        ;   a  U(       + U R                  U'   M@  MB  UR                  S5      (       d  MZ  USS  nU[        ;   d  Mk  XR                  U'   M{     U=(       d    [        U l        U=(       d    [        U l        [        S[        S	U5      5      U l        ["        =(       a    UU l        Uc  ['        SUU R$                  S
.UD6nU R(                  US'   Xl        S U l        / U l        S U l        S U l        S U l        SU l        SU l        / U l        [<        R<                  " 5       U l        0 U l         [         HY  nU RB                  nU R                  RE                  US5      (       a  [G        U SU 3U RB                  5      nUU R@                  U'   M[     SU R@                  ;   a8  U R@                  S   U R@                  S'   U R@                  S   U R@                  S'   U	c  [I        5       U l%        O[I        U	5      U l%        U
(       a(  U =RJ                  [I        U R*                  5      -  sl%        UR                  5        VVs/ s HL  u  p[M        U5      (       d5  [N        RP                  " U5      (       d  S[S        [U        U5      5      ;   d  MJ  UPMN     snnU l+        g s  snnf )Nnested_symtableusersymsr   no_   with_   r   g    חA)nested	use_numpyrD   Ton_r=   	tryexcept
tryfinallyznumpy.lib.index_tricks ),copyMINIMAL_CONFIGDEFAULT_CONFIGconfigupdatepoplenitems
startswith	ALL_NODESr   writerr   
err_writermaxminmax_statement_lengthr   rP   r   _printersymtable
_interrupterror	error_msgr$   retval
_calldepthlineno	code_texttime
start_timenode_handlersunimplementedgetgetattrr8   readonly_symbolscallableinspectismoduler6   typeno_deepcopy)selfre   rI   user_symbolsr_   r`   rP   rc   minimalrs   builtins_readonlyrX   kwskeyvalnodehandlers                    5/venv/lib/python3.13/site-packages/asteval/asteval.py__init__Interpreter.__init__l   s    ii'~NKKv&)8%&LS "wwz2s8a<IIK>>%((qr7Dy(03GD) )^^G,,qr7Dy(,/D) ( &$.$'3t5I+J$K!"0y( S37>>SEQSH !MM 
	))+D((G{{tT**!$#dVd6H6HI'.Dt$	  D&&&.2.@.@.GD{+/3/A/A%/HD|,#$'ED!$'(8$9D!!!S%77!080@ Q0@HC (#*#3#3C#8#8#;tDI#N  0@ Q Qs   A	MMc                 ^    SnXR                   ;   a  U R                   R                  U5      nU$ )zlremove support for a node
returns current node handler, so that it
might be re-added with add_nodehandler()
N)ro   rZ   )ry   r   outs      r   remove_nodehandlerInterpreter.remove_nodehandler   s0    
 %%%$$((.C
    c                 \    Uc  [        U SU 3U R                  5      nX R                  U'   U$ )z0set node handler or use current built-in defaultrQ   )rr   rp   ro   )ry   r   r   s      r   set_nodehandlerInterpreter.set_nodehandler   s4    ?dc$L$2D2DEG#*4 r   c                     [        U R                  R                  5       5      n[        U R                  5      nUR	                  U5      nU$ )zReturn a set of symbols that have been added to symtable after
construction.

I.e., the symbols from self.symtable that are not in
self.no_deepcopy.

Returns
-------
unique_symbols : set
    symbols in symtable that are not in self.no_deepcopy

)r8   re   keysrx   
difference)ry   sym_in_currentsym_from_constructionunique_symbolss       r   user_defined_symbols Interpreter.user_defined_symbols   sC     T]]//12 #D$4$4 5'223HIr   c                 `    UR                   R                   S3nU R                  U[        US9  g)zUnimplemented nodes.z not supportedexcmsgN)	__class____name__raise_exceptionNotImplementedErrorry   r   r   s      r   rp   Interpreter.unimplemented   s/    (()8T':Dr   c           	      *   Ub  X@l         [        U5      nU R                   n[        U R                  5      S:  a  U R                  S   n[	        XX0R                   XeS9n[
        R                  " 5       U l        U R                  R                  U5        U R                  c  X0l
        O[        U5      S:  a   Uc  U R                  S   R                  nUcy  [        U R                  5      S:  a`  Uc]  [        U R                  5      S:  aD  U R                  R                  5       nUR                  nUc  [        U R                  5      S:  a  MD  Uc  [        n[        UR                  5      S:X  ah  [        U R                  5      S:X  aO  [        U R                  5      S:  a6  U R                  R                  S5      nUR                  UR                  5      e[        UR                  5      S:X  a  U R                  Ul        U" U R                  5      e)zAdd an exception.r   )r   r   r$   textrk   r   )r$   r;   r[   rl   r   astRaiserf   rg   appendrh   r   rZ   	Exceptionr   )ry   r   r   r   r$   rk   r   errs           r   r   Interpreter.raise_exception   s   I#hyyt~~">>"%Dd99"&7))+

#>>! NX\ ;**R.$$C{s4::2kc$**o&9**..*C''C kc$**o&9 ;Csww<1T^^!4!9c$**oPQ>Q**..$C''#''""377|q ..dnn%%r   c                 b   [        U5      U R                  :  a&  SU R                  S S3nU R                  S[        US9  Xl         [
        R                  " U5      n[
        R                  " W5      nU$ ! [         a    U R                  S[        US9   N9  U R                  S[        US9   NQ= f)z1Parse statement/expression to Ast representation.zlength of text exceeds dz charactersNr   r$   )	r[   rc   r   RuntimeErrorr$   r   parseSyntaxErrorfix_missing_locations)ry   r   r   r   s       r   r   Interpreter.parse  s    t9t000+D,E,Ea+HTC  <c B		D))D/C
 '',
  	C  ;T B	D  <d Cs   A5 5B.B.c                 x   [        U[        5      (       a  U R                  XS9$ Sn[        U R                  5      S:  a  U$ U R
                  b  U R
                  $ [        U R                  [        R                  [        R                  45      (       a  U R                  $ Uc  U$ Ub  X0l
        Ub!  X l        U R                  R                  U5         U R                  UR                  R                   R#                  5          n W" U5      n[        U[*        5      (       a  [-        U5      nU$ ! [$         a"    U R'                  S[(        U R                  S9   NVf = f!   U(       a&  U R                  b  U R'                  XR                  S9   O= f[        U R                  5      S:  a  U R/                  5         g)z4Execute parsed Ast representation for an expression.)raise_errorsNr   r   )r$      )
isinstancer;   evalr[   rg   ri   rf   r   BreakContinuerk   r$   rl   r   ro   r   r   lowerKeyErrorr   r   	enumerater.   _remove_duplicate_errors)ry   r   r$   rk   
with_raiser   r   rets           r   runInterpreter.run  sq    dC  99T9;;tzz?QJ;;";;doo		3<<'@AA??"<J KINN!!$'	P(()@)@)F)F)HIG	;$-C#y))3iJ  	P  +>TYY O	P	;dii3$$T		$: tzz?Q))+s   1D/ )E /)EE/Fc                    U R                   S   /nU R                   SS  H  nUS   nUR                  UR                  :w  d6  UR                  UR                  :w  d  UR                  UR                  :w  d  MX  [	        UR                  [
        5      (       d  My  [        UR                  5      S:  d  M  UR                  U5        M     Xl         g)zremove duplicate exceptionsr   r   Nr   )rg   r   r$   r   r   r;   r[   r   )ry   rg   r   lerrs       r   r   $Interpreter._remove_duplicate_errorsE  s    A::ab>C9Dww$(("chh$))&;sww488?Scggs++CGGq0@LL%	 "
 
r   c                 (    U R                   " U40 UD6$ )z Call class instance as function.)r   )ry   r$   kws      r   __call__Interpreter.__call__O  s    yy$$$r   c                    X l         / U l        SU l        [        R                  " 5       U l        [        U[        5      (       a   U R                  U5      nOUn U R                  XQX$S9$ ! [         a}    [        5       S   n[        U R                  5      S:  a:  U R                  S   nUR                  5       S   nU(       a  UR                  U5      eU(       a  [        X`R                  S9   gf = f! [         aj    U(       a`  U(       dY  [        5       S   n[        U R                  5      S:  a   U R                  S   R                  5       S   n[        X`R                  S9   Of = fU(       aZ  [        U R                  5      S:  aA  U R!                  5         U R                  S   nUR                  UR                  5       S   5      eg)zEvaluate a single statement.Nr   r   r   )file)r$   rk   r   )rk   rg   rh   rm   rn   r   r;   r   r   r   r[   	get_errorr   rD   r`   r   r   )	ry   r$   rk   show_errorsr   r   errmsgr   r   s	            r   r   Interpreter.evalS  sq   
))+dC  zz$' D	488DF8TT  	!Atzz?Q&::b>D!^^-a0F#"hhv..&7	  	4<!Atzz?Q&!ZZ^557:Ff??3	4 C

Oa/))+**R.C''#--/!,--s&   A* C4 *BC10C14A1E('E(c                 0    [         R                  " U 40 UD6$ )zSimple ast dumper.)r   dump)r   r   s     r   r   Interpreter.dumpv  s     xx###r   c                 8    U R                  UR                  5      $ )zExpression.r   valuery   r   s     r   on_exprInterpreter.on_expr|      xx

##r   c                 v    UR                    H)  nU R                  UR                  UR                  5        M+     g)zsimple importN)namesimport_moduler1   asnamery   r   tnodes      r   	on_importInterpreter.on_import  s(    ZZEuzz5<<8  r   c                     / / p2UR                    H9  nUR                  UR                  5        UR                  UR                  5        M;     U R	                  UR
                  X2S9  g)zimport/from)fromlistN)r   r   r1   r   r   r0   )ry   r   r   r   r   s        r   on_importfromInterpreter.on_importfrom  sP    r&ZZEOOEJJ'MM%,,'   	4;;Br   c                    U[         R                  ;   a  [         R                  U   nO [        U5        [         R                  U   nUc  Ub!  [         R                  U   U R
                  U'   g/ nUR                  S5      n[        U5      S:  av  UR                  UR                  S5      5        SR                  U5      n[        U5      S:X  a  UOUn[         R                  U   U R
                  U'   [        U5      S:  a  Mu  ggUc  S/[        U5      -  n[        X25       H#  u  pU
c  U	n
[        WU	5      U R
                  U
'   M%     g!   U R                  S[        SS9   GN= f)a  import a python module, installing it into the symbol table.
options:
  name       name of module to import 'foo' in 'import foo'
  asname     alias for imported name(s)
                  'bar' in 'import foo as bar'
               or
                  ['s','t'] in 'from foo import x as s, y as t'
  fromlist   list of symbols to import with 'from-import'
                 ['x','y'] in 'from foo import x, y'
NzImport Errorr   .r   )sysmodules
__import__r   ImportErrorre   splitr[   r   rZ   joinziprr   )ry   r1   r   r   thismodmpartspartsmodnameinnamesymaliass              r   r   Interpreter.import_module  s7    3;;kk$'GP4 ++d+ !(+D(9f%

3%j1nMM%))A,/!hhv.G&)%jAoTGF,/KK,@DMM&)	 %j1n ~H-!(3
=E'.w'<e$ 4#P$$T{$Os   E E'c                 8    U R                  UR                  5      $ )zIndex.r   r   s     r   on_indexInterpreter.on_index  r   r   c                     U R                   S:X  a  [        S5      eU R                  UR                  5      U l        U R                  c  [
        U l        gg)z9Return statement: look for None, return special sentinel.r   zcannot return at top levelN)rj   r   r   r   ri   r	   r   s     r   	on_returnInterpreter.on_return  sD    ??a:;;hhtzz*;;&DK r   c                 J    [        U R                  UR                  5      5      $ )zRepr.)r6   r   r   r   s     r   on_reprInterpreter.on_repr  s    DHHTZZ())r   c                 R    SnUR                    H  nU R                  U5      nM     U$ )zModule def.N)bodyr   )ry   r   r   r   s       r   	on_moduleInterpreter.on_module  s'    YYE((5/C 
r   c                 $    U R                  U5      $ )zbasic expression)r   r   s     r   on_expressionInterpreter.on_expression  s    ~~d##r   c                     g)zPass statement.NrT   r   s     r   on_passInterpreter.on_pass  s    r   c                     Xl         U$ )zInterrupt handler.)rf   r   s     r   on_interruptInterpreter.on_interrupt  s    r   c                 $    U R                  U5      $ )zBreak.r  r   s     r   on_breakInterpreter.on_break        &&r   c                 $    U R                  U5      $ )z	Continue.r
  r   s     r   on_continueInterpreter.on_continue  r  r   c                     U R                  UR                  5      (       d>  UR                  (       a  UR                  R                  OSnU R	                  U[
        US9  g)zAssert statement. r   T)r   testr   r   r   AssertionErrorr   s      r   	on_assertInterpreter.on_assert  sB    xx		""$(HH$((.."C  >s Cr   c                 b    UR                    Vs/ s H  o R                  U5      PM     sn$ s  snf )zList.)eltsr   )ry   r   es      r   on_listInterpreter.on_list  s$    %)YY/YY///s   ,c                 6    [        U R                  U5      5      $ )zTuple.)r>   r  r   s     r   on_tupleInterpreter.on_tuple  s    T\\$'((r   c                 t    [        UR                   Vs/ s H  o R                  U5      PM     sn5      $ s  snf )zSet.)r8   r  r   ry   r   ks      r   on_setInterpreter.on_set  s)    3AHHQK3443   5c                     [        UR                  UR                  5       VVs0 s H&  u  p#U R                  U5      U R                  U5      _M(     snn$ s  snnf )zDictionary.)r   r   valuesr   )ry   r   r!  vs       r   on_dictInterpreter.on_dict  sO     DIIt{{+-+ .2QTXXa[(+- 	- -s   -Ac                     UR                   $ )zReturn constant value.)r   r   s     r   on_constantInterpreter.on_constant  s    zzr   c                     SR                  UR                   Vs/ s H  o R                  U5      PM     sn5      $ s  snf )zjoin strings, used in f-stringsr  )r   r&  r   r   s      r   on_joinedstrInterpreter.on_joinedstr   s-    wwT[[9[[9::9s   ;c                 (   U R                  UR                  5      n[        [        [        S.nUR
                  U;   a  X1R
                     " U5      nSnUR                  b  SU R                  UR                  5       S3n[        X@R                  XS9$ )zformatting used in f-strings)s   r   a   z{__fstring__}z{__fstring__:})__fstring__)	r   r   r;   r6   ascii
conversionformat_specr   r   )ry   r   r   fstring_convertersfmts        r   on_formattedvalueInterpreter.on_formattedvalue  s    hhtzz"#&Tu=??00$__5c:C'"488D,<,<#=">bAC3 4 4dLLr   c                     U R                   R                  UR                  [        5      n[	        U[
        5      (       a%  SUR                   S3nU R                  U[        US9  U$ )Nzname 'z' is not definedr   )re   rq   idr	   r   r
   r   	NameError)ry   r   r   r   s       r   _getsymInterpreter._getsym  sV    mm6c5!!477)#34C  9# >
r   c                     UR                   R                  nU[        R                  [        R                  4;   a  [        UR                  5      $ U R                  U5      $ )z
Name node.)ctxr   r   ParamDelr;   r>  r@  )ry   r   rC  s      r   on_nameInterpreter.on_name  sC    hh  399cgg&&tww<||D!!r   c                    UR                   [        R                  :X  a  [        UR                  5      (       a  UR                  U R
                  ;   a$  SUR                   3nU R                  U[        US9  X R                  UR                  '   UR                  U R                  ;   a&  U R                  R                  UR                  5        ggUR                   [        R                  :X  a}  UR                  R                   [        R                  :X  a$  SUR                   3nU R                  U[        US9  [!        U R#                  UR$                  5      UR                  U5        gUR                   [        R&                  :X  a7  X R#                  UR$                  5      U R#                  UR(                  5      '   gUR                   [        R*                  [        R,                  4;   a^  [/        U5      [/        UR0                  5      :X  a1  [3        UR0                  U5       H  u  pVU R5                  XV5        M     g[7        S5      eg)z}Assign a value (not the node.value object) to a node.

This is used by on_assign, but also by for, list comprehension,
etc.

%invalid symbol name (reserved word?) r   zcannot assign to attribute ztoo many values to unpackN)r   r   Namer   r>  rs   r   r?  re   rx   remove	AttributerC  LoadattrAttributeErrorsetattrr   r   	Subscriptr:   TupleListr[   r  r   node_assign
ValueError)ry   r   r   r   r   telemtvals          r   rT  Interpreter.node_assign  s    >>SXX%%dgg..GGt444@	J$$Tyf$E%(MM$''"ww$***  ''0 + ^^s}},xx!!SXX-3DII;?$$T~3$GDHHTZZ($))S9^^s}},9<HHTZZ $**!56^^		388443x3tyy>)#&tyy##6KE$$U1 $7 !!<== 5r   c                 N   UR                   R                  nU[        R                  :X  a  SnU R	                  U[
        US9  U R                  UR                  5      nU[        R                  :X  a  [        XAR                  5      $ [        XAR                  U R                  U5      $ )zExtract attribute.z)attribute for storage: shouldn't be here!r   )rC  r   r   Storer   r   r   r   rE  delattrrN  r   )ry   r   rC  r   r   s        r   on_attributeInterpreter.on_attribute>  s~     hh  #))=C  <S Ahhtzz"#''>3		**CD,@,@$GGr   c                     U R                  UR                  5      nUR                   H  nU R                  X25        M     g)zSimple assignment.N)r   r   targetsrT  ry   r   r   r   s       r   	on_assignInterpreter.on_assignM  s0    hhtzz"\\EU( "r   c                     U R                  [        R                  " UR                  /[        R                  " UR                  UR
                  UR                  S9S95      $ )zAugmented assign.)leftopright)r_  r   )ra  r   AssigntargetBinOpre  r   r   s     r   on_augassignInterpreter.on_augassignS  sJ    ~~cjj$++/2yydkk<@GG?Czz0KL M 	Mr   c                     [        U R                  UR                  5      U R                  UR                  5      U R                  UR                  5      5      $ )zSimple slice.)r:   r   r   upperstepr   s     r   on_sliceInterpreter.on_sliceZ  s>    TXXdjj)XXdjj)XXdii(* 	*r   c                 t    [        UR                   Vs/ s H  o R                  U5      PM     sn5      $ s  snf )zExtended slice.)r>   dimsr   r   s      r   on_extsliceInterpreter.on_extslice`  s)    499=9%hhuo9=>>=r$  c                 p    U R                  UR                  5      U R                  UR                  5         $ )zSubscript handling)r   r   r:   r   s     r   on_subscriptInterpreter.on_subscriptd  s'    xx

#DHHTZZ$899r   c                    UR                    GH  nUR                  R                  [        R                  :w  a    g/ nUR                  [        R
                  :X  aG  UR                  UR                  5        UR                  nUR                  [        R
                  :X  a  MG  UR                  [        R                  :X  ar  UR                  U R                  ;  aX  UR                  UR                  5        UR                  5         U R                  R                  SR                  U5      5        GM%  UR                  [        R                   :X  d  GMF  U R#                  UR$                  5      n/ nUR                  nUR                  [        R
                  :X  aG  UR                  UR                  5        UR                  nUR                  [        R
                  :X  a  MG  UR                  [        R                  :X  d  GM  UR                  U R                  ;  d  GM  UR                  UR                  5        UR                  5         SR                  U5      nU R#                  U5      nXd	 ['        U5      S:X  a  X`R                  U'   GM  U R                  US      nUSS  H  nXx   nM	     [)        XsS   U5        GM     g)zDelete statement.r   r   r   r   N)r_  rC  r   r   rE  rL  r   rN  r   rJ  r>  rs   reversere   rZ   r   rQ  r   r:   r[   rP  )	ry   r   r   childrennslicesnamer   childcnames	            r   	on_deleteInterpreter.on_deletei  s   \\Eyy""cgg-H//S]]2

+ //S]]2 388+HHD$9$99)  "!!#((8"45CMM1%++.oo6OOEJJ/!KKE oo6 OOsxx/HH 5 55OOEHH-$$&HHX.E((5/C8})/2e, $hqk :%-a^E$)LE &4|S9A "r   c                 j    [        UR                  5      " U R                  UR                  5      5      $ )zUnary operator.)r   re  r   operandr   s     r   
on_unaryopInterpreter.on_unaryop  s#    tww 677r   c                     [        UR                  5      " U R                  UR                  5      U R                  UR                  5      5      $ )zBinary operator.)r   re  r   rd  rf  r   s     r   on_binopInterpreter.on_binop  s7    tww 3 $ 46 	6r   c                    U R                  UR                  S   5      n[        R                  UR                  R
                  :H  nU(       a  U(       d  U(       dh  U(       da  UR                  SS  HN  n[        UR                  5      " X R                  U5      5      nU(       a  U(       a  U(       a  MD  U(       d  MM    U$    U$ )zBoolean operator.r   r   N)r   r&  r   Andre  r   r   )ry   r   r   is_andnodevals        r   	on_boolopInterpreter.on_boolop  s    hht{{1~&DGG---sF3;;qr?dgg&sHHW,=>333
	 + 
r   c                    U R                  UR                  5      n/ n[        UR                  5      S:  n[	        UR                  UR
                  5       HU  u  pVU R                  U5      n[        U5      " X'5      nU(       d  M0  UR                  U5        [        U5      (       d    gUnMW     U(       a  [        U5      nW$ )z;comparison operators, including chained comparisons (a<b<c)r   F)	r   rd  r[   opsr   comparatorsr   r   all)	ry   r   lvalresultsmultioperrnodervalr   s	            r   
on_compareInterpreter.on_compare  s    xx		"DHH!txx)9)9:KD88E?D$-+Cus#7||  ; g,C
r   c                 2   U R                   R                  SS5      (       av  UR                  SS5      nUR                  SU R                  5      nUR                  SS5      nUR                  SS5      n[	        XXVS.6  U(       a  UR                  5         g	g	g	)
zGeneric print function.rD   Tflushr   sep 
)r   r  endN)rX   rq   rZ   r_   rD   r  )ry   r   r}   r  filehr  r  s          r   rd   Interpreter._printer  s{    ;;??7D))GGGT*EGGFDKK0E''%%C''%&C35  *r   c                     UR                   nU R                  UR                  5      (       d  UR                  nU H  nU R                  U5        M     g)zRegular if-then-else statement.N)r   r   r  orelse)ry   r   blockr   s       r   on_ifInterpreter.on_if  s<    		xx		""KKEEHHUO r   c                     UR                   nU R                  UR                  5      (       a  UR                  nU R                  U5      $ )zIf expressions.)r  r   r  r   )ry   r   r$   s      r   on_ifexpInterpreter.on_ifexp  s5    {{88DII99Dxx~r   c                    U R                  UR                  5      (       a  SU l        UR                   H#  nU R                  U5        U R                  c  M#    O   [	        U R                  [
        R                  5      (       a  OFU R                  UR                  5      (       a  M  UR                   H  nU R                  U5        M     SU l        g)zWhile blocks.N)r   r  rf   r   r   r   r   r  r   s      r   on_whileInterpreter.on_while  s    hhtyy!!"DO??. # $//39955 hhtyy!!  %r   c                    U R                  UR                  5       H  nU R                  UR                  U5        SU l        UR
                   H#  nU R                  U5        U R                  c  M#    O   [        U R                  [        R                  5      (       d  M    O&   UR                   H  nU R                  U5        M     SU l        g)zFor blocks.N)
r   iterrT  rh  rf   r   r   r   r   r  r`  s       r   on_forInterpreter.on_for  s    88DII&CT[[#."DO??. # $//39955 '  %r   c                    / nUR                    H  nU R                  UR                  5      nUR                  U5        [	        US5      (       a=  UR                  5       nUR                  b  U R                  UR                  U5        M{  M}  Sn[        S[        U5       SU 35      e   UR                   H#  nU R                  U5        U R                  c  M#    O   U H&  n[	        US5      (       d  M  UR                  5         M(     g)zwith blocks.	__enter__Nz4object does not support the context manager protocol'z' __exit__)r\   r   context_exprr   hasattrr  optional_varsrT  	TypeErrorrw   r   rf   r  )ry   r   contextsitemrC  resultr   bnodes           r   on_withInterpreter.on_with  s    JJD((4,,-COOC sK((%%1$$T%7%7@ 2 M!DI;b 677  YYEHHUO* 
 CsJ'' r   c                    0 nUR                    GH  nUR                  R                  [        R                  :X  a  [        UR                  R                  5      (       a$  UR                  R                  U R                  ;   a8  SUR                  R                   3nU R                  UR                  [        US9  UR                  R                  U R                  ;   aH  [        R                  " U R                  UR                  5      5      X#R                  R                  '   GM  GM  UR                  R                  [        R                  :X  d  GMF  UR                  R                   HP  nUR                  U R                  ;   d  M  [        R                  " U R                  U5      5      X%R                  '   MR     GM     U$ )z:find and save symbols that will be used in a comprehensionrI  r   )
generatorsrh  r   r   rJ  r   r>  rs   r   r?  re   rU   deepcopyr@  rR  r  )ry   r   
saved_symsr   r   rW  s         r   _comp_save_symsInterpreter._comp_save_syms  s#   
__E||%%1)%,,//::LLOOt'<'<<DU\\__DUVF((9&(Q<<??dmm326--U\\@Z2[J||/ 4 ''3994!LL--Dww$--/.2mmDLL<N.O
77+ . % r   c                    US   nSnSnUR                   R                  [        R                  :X  a  [	        UR                   R
                  5      (       a$  UR                   R
                  U R                  ;   a8  SUR                   R
                   3nU R                  UR                   [        US9  UR                   R
                  nOcUR                   R                  [        R                  :X  a;  Sn[        UR                   R                   Vs/ s H  oR
                  PM     sn5      nU R                  UR                  5       GH4  n	U(       a  Ub  XR                  U'   O"[        Xi5       H  u  pXR                  U
'   M     SnUR                    H&  nU=(       a    U R                  U5      nU(       a  M&    O   U(       d  M  [#        U5      S:  a  U R%                  USS X#5        M  ['        U[(        5      (       a,  UR+                  U R                  UR,                  5      5        M  ['        U[.        5      (       d  M  U R                  UR0                  5      X0R                  UR2                  5      '   GM7     gs  snf )zgeneral purpose generator r   TNrI  r   Fr   )rh  r   r   rJ  r   r>  rs   r   r?  rR  r>   r  r   r  re   r   ifsr[   do_generatorr   r.   r   eltr    r   r~   )ry   gnodesr   r   gnodenametyperh  r   gvalr   rV  rW  addconds                 r   r  Interpreter.do_generator  s   q	<<!!SXX-%elloo664#8#88@@QR$$U\\yf$M\\__F\\##syy0H0A0AB0AGG0ABCF88EJJ'CF.(+f%#&v#3KE+/MM%( $4C		,dhhtns " sv;?%%fQRj$<T**JJtxx12T**.2hhtzz.BC*+# ( Cs   Jc                     U R                  U5      n/ nU R                  UR                  X5        UR                  5        H  u  pEXPR                  U'   M     U$ )zList comprehension v2r  r  r  r\   re   ry   r   r  r   r1   r   s         r   on_listcompInterpreter.on_listcomp6  Q    ))$/
$//45#))+ID"%MM$ ,
r   c                 6    [        U R                  U5      5      $ )zSet comprehension)r8   r  r   s     r   
on_setcompInterpreter.on_setcomp@  s    4##D)**r   c                     U R                  U5      n0 nU R                  UR                  X5        UR                  5        H  u  pEXPR                  U'   M     U$ )zDict comprehension v2r  r  s         r   on_dictcompInterpreter.on_dictcompD  r  r   c                 f    U R                  UR                  5      UR                  UR                  4$ )zException handler...)r   rw   r1   r   r   s     r   on_excepthandlerInterpreter.on_excepthandlerN  s$    #TYY		::r   c                 R   SnUR                    GH"  nU R                  USS9  U=(       a    [        U R                  5      S:H  n[        U R                  5      S:  d  MP  U R                  S   R                  u  pEnUR
                   H  nSnUR                  b*  [        R                  UR                  R                  S5      nUb  [        U" 5       U5      (       d  MW  / U l        UR                  b  XPR                  UR                  '   UR                    H  n	U R                  U	5        M       O     O   U(       a5  [        US5      (       a$  UR                   H  nU R                  U5        M     [        US5      (       a%  UR                   H  nU R                  U5        M     gg)	zTry/except/else/finally blocks.TF)r   r   r   Nr  	finalbody)r   r   r[   rg   r   handlersrw   __builtins__rq   r>  r   r1   re   r  r  r  )
ry   r   	no_errorsr   e_typee_value_hndhtypetlines
             r   on_tryInterpreter.on_tryR  sG   	YYEHHUuH-!:c$**o&:I4::"%)ZZ^%<%<"==C Exx+ , 0 0d C}
68U(C(C%'
88/6=MM#((3%(XXE HHUO &. ) ! " x00 % 4%% ( &r   c                    UR                   nUR                  nU R                  U5      nSR                  UR                  5      nU R                  U5      nUS;  a	  US SUS 3nU R                  SUR                  USS9  g)z4Raise statement: note difference for python 2 and 3.r  )NNones: Nr  )r   r   r$   )r   causer   r   argsr   r   )ry   r   excnodemsgnoder   r   msg2s          r   on_raiseInterpreter.on_raisen  sy    ((**hhwhhsxx xx ~%G2d1X&CTs}}#BGr   c                 j   U R                  UR                  5      n[        US5      (       d0  [        U[        5      (       d  SU S3nU R                  U[        US9  UR                   Vs/ s H  o@R                  U5      PM     nn[        USS5      nUb  XPR                  U5      -   n0 nU[        :X  a  U R                  US'   UR                   H  n[        U[        R                  5      (       d  SU S3nU R                  XS	9  UR                  c,  UR                  U R                  UR                   5      5        Mp  UR                  U;   a$  U R                  U["        S
UR                   3S9  M  U R                  UR                   5      XxR                  '   M     [        USS5      n	U	b   UR                  U R                  U	5      5        [        U[$        5      (       a  U =R&                  S-  sl         U" U0 UD6n
[        U[$        5      (       a  U =R&                  S-  sl        U
$ s  snf ! [(         aD  nSn
[        US[+        U5      5      nSU SU S3nU SU SU 3nU R                  XS	9   SnANzSnAff = f! [        U[$        5      (       a  U =R&                  S-  sl        f f = f)zFunction execution.r   r  z' is not callable!!r   starargsNr   z keyword error in function call ')r   zkeyword argument repeated: kwargsr   r   zError running function 'z' with args 'z and kwargs r  )r   funcr  r   rw   r   r  r  rr   rD   r_   keywordsr   keywordr   rY   r   r   r   rj   r   r;   )ry   r   r  r   targr  r  r  r~   r  r   ex	func_names                r   on_callInterpreter.on_cally  sd   xx		"tZ((D$1G1GdV./C  9# >+/99594954T2((8,,D5=#{{HV==Cc3;;//8a@$$T$3ww 34H$$$T{+Fswwi)P % R %)HHSYY$7! ! x.OODHHV,-dI&&OOq O
	%))C $	**1$
K 68  	0Cj#d)<I,YK}TF!LCEhZr"6C   /	0 $	**1$ +s0   +H/;H4 4
J>:I=8J =JJ -J2c                     UR                   $ )zArg for function definitions.)r   r   s     r   on_argInterpreter.on_arg  s    xxr   c                    UR                   (       a  [        S5      e/ n[        UR                  5      (       a  UR                  U R                  ;   a$  SUR                   3nU R                  U[        US9  [        UR                  R                  5      [        UR                  R                  5      -
  n[        UR                  R                  5       HR  u  pVU R                  U5      nU R                  UR                  R                  XT-      5      nUR                  X45        MT     UR                  R                  SU  V	s/ s H  oR                  PM     n
n	SnUR                  S   n[        U[         R"                  5      (       a5  [        UR$                  [         R&                  5      (       a  UR$                  nUR                  R(                  nUR                  R*                  n[        U[         R                  5      (       a  UR                  n[        U[         R                  5      (       a  UR                  n[-        UR                  XU R.                  UR                  [         R0                  " U5      XXS9
U R2                  UR                  '   UR                  U R4                  ;   a&  U R4                  R7                  UR                  5        ggs  sn	f )zDefine procedures.z#decorated procedures not supported!z'invalid function name (reserved word?) r   Nr   )docrk   r   r   r  r  varargvarkws)decorator_listWarningr   r1   rs   r   r?  r[   r  defaultsr   r   r   r   r   r   r   Exprr   Constantkwargr  r   rk   unparsere   rx   rK  )ry   r   r  r   offsetidefdefnodedefvalkeyvalr   r  r  nb0r  r  s                  r   on_functiondefInterpreter.on_functiondef  s    ?@@!$)),,		T222>tyykJF  9& ATYY^^$s499+=+='>>&tyy'9'9:MDXXg&FXXdiinnT[9:FMM6*+ ;
 (,yy~~gv'>?'>e		'>?iilc388$$CIIs||)L)L))C!!fcgg&&ZZFfcgg&&ZZF#,TYY48KK26))25++d2C264:$Kdii  99(((##DII. )# @s   K)rj   rf   rl   rX   r`   rg   rh   r$   rk   rc   rx   ro   rs   ri   rn   re   rP   r_   )NFNNNTiP  FNFN)N)Nr  NN)NNT)r   TF)Gr   
__module____qualname____firstlineno____doc__r   r   r   r   rp   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r"  r(  r+  r.  r;  r@  rF  rT  r\  ra  rj  ro  rs  rv  r  r  r  r  r  rd   r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  __static_attributes__rT   r   r   rF   rF   G   sz   #H 7<<@6;1515	GQR$E
#&R.`%!F $ $
$
9
C&=P$'*$

''0)5-
;	M">BH)M*?:
":H86
	"	 ,& CD+; 8	H+Z$/r   rF   )r  r   r   rU   ru   rm   r   r   r   astutilsr   r   r	   r
   r   r   r   r   r   r   r   r^   rV   rW   _tnoderF   rT   r   r   <module>r      s   %L  
    ( (` ` ` `,	 "7!7<F #N6!N6	<G/ G/r   