a
    V$c6r                     @   sJ  d dl mZmZmZmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZ d dlmZmZ d dlZd dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl Z d dl!Z!d dl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ d dl,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 G dd de7Z8d	d
 Z9d dl:m;Z;m<Z< G dd de=Z>dd Z?i Z@i ZAeB ZCdd ZDG dd dZEdaFdd ZGdd ZHdd ZIdd ZJdd ZKG dd dejLZMeM jNZOG d d! d!ejLZPeP jNZQG d"d# d#ejRZSeS ZTd$d% ZUeVd&ZWd'd( ZXeVd)ZYd*d+ ZZe j[j\e jj\B Z]e]e jj\B e jj\B e jj\B Z^e^e jj\B Z_esze_e j`j\O Z_eB Zai Zbi Zcd,d- Zdd.d/ ZedSd4d5Zfd6d7 Zgd8d9 Zhd:d; ZidTd=d>ZjdUd?d@ZkdVdAdBZldWdCdDZmdXdEdFZndGdH ZoG dIdJ dJe7ZpdKdL Zqer(dMdN Zresejter dOdP ZudQdR Zvesejweu dS )Y    )divisionabsolute_importwith_statementprint_functionunicode_literals)PY2
basestringbchrbordchropenpystrrangeroundstrtobytesunicode)OptionalAnyN)	CompressedList	DetRandomRevertableDictRevertableListRevertableObjectRevertableSetRollbackRandomrevertable_rangerevertable_sorted)
deletedStoreDeletedAlwaysRollback
NoRollbackSlottedNoRollbackrngreachedreached_varsRollbackRollbackLogc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )StoreModulezP
    This class represents one of the modules containing the store of data.
    c                 C   s   t | jffS N)get_store_module__name__self r.   renpy/python.py
__reduce__T   s    zStoreModule.__reduce__c                 C   s   t | d| d S )N__dict__)object__setattr__)r-   dr.   r.   r/   __init__W   s    zStoreModule.__init__c                 C   s   || j |< d S r)   r1   )r-   keyvaluer.   r.   r/   r3   Z   s    zStoreModule.__setattr__c                 C   s   | j |= d S r)   r6   )r-   r7   r.   r.   r/   __delattr__]   s    zStoreModule.__delattr__N)r+   
__module____qualname____doc__r0   r5   r3   r9   r.   r.   r.   r/   r(   L   s
   r(   c                 C   s
   t j|  S r)   )sysmodulesnamer.   r.   r/   r*   d   s    r*   )	DictItemsfind_changesc                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )	StoreDictz_
    This class represents the dictionary of a store module. It logs
    sets and deletes.
    c                 C   s   t dd S )Nz0Cannot pickle a reference to a store dictionary.)	Exceptionr,   r.   r.   r/   r0   q   s    zStoreDict.__reduce__c                 C   s   t | | _t | _d S r)   )rA   oldsetever_been_changedr,   r.   r.   r/   r5   t   s    
zStoreDict.__init__c                 C   s   t  | _|   t| | _dS )zA
        Called to reset this to its initial conditions.
        N)rF   rG   clearrA   rE   r,   r.   r.   r/   reset~   s    zStoreDict.resetc                 C   s   t | | _dS )z@
        Called to mark the start of a rollback period.
        N)rA   rE   r,   r.   r.   r/   begin   s    zStoreDict.beginc                 C   sh   t | }t| j|t}|r || _|du r,dS t }|r`|D ]$}|| jvr:| j| || q:||fS )a  
        For every key that has changed since begin() was called, returns a
        dictionary mapping the key to its value when begin was called, or
        deleted if it did not exist when begin was called.

        As a side-effect, updates self.ever_been_changed, and returns the
        changes to ever_been_changed as well.

        `cycle`
            If true, this cycles the old changes to the new changes. If
            False, does not.
        N)rA   rB   rE   r   rF   rG   add)r-   cyclenewrv	delta_ebckr.   r.   r/   get_changes   s    
zStoreDict.get_changesN)	r+   r:   r;   r<   r0   r5   rI   rJ   rQ   r.   r.   r.   r/   rC   k   s   
	rC   c                  C   s   t  D ]} |   qdS )z+
    Calls .begin on every store dict.
    N)store_dictsvaluesrJ   )sdr.   r.   r/   begin_stores   s    rU   c                 C   s   |  d\}}}|rt| t| } | tv r0dS t|  t| t }|  t	| }||d< ||d< t
d| tjj D ] \}}||vr~|dkr~|||< q~| tv rt|  tj|< nt| t| < tj|< |rtj| t| |< dS )z(
    Creates the store with `name`.
    .Nr+   __package__1__all__)
rpartitioncreate_storer   initialized_store_dictsrK   rR   
setdefaultrC   rI   r   evalrenpyminstorer1   itemsstore_modulesr=   r>   r(   )r@   parent_varr4   pynamerP   vr.   r.   r/   r[      s*    


r[   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )StoreBackupzh
    This creates a copy of the current store, as it was at the start of
    the current statement.
    c                 C   s*   i | _ i | _i | _tD ]}| | qd S r)   )storerE   rG   rR   
backup_oner-   rP   r.   r.   r/   r5      s
    zStoreBackup.__init__c                 C   s:   t | }t|| j|< |j | j|< t|j| j|< d S r)   )rR   dictri   rE   as_dictrF   rG   )r-   r@   r4   r.   r.   r/   rj     s    zStoreBackup.backup_onec                 C   sP   t | }|  || j|  t| j| |_|j  |j| j|  d S r)   )rR   rH   updateri   rA   rE   rG   )r-   r@   rT   r.   r.   r/   restore_one  s    
zStoreBackup.restore_onec                 C   s   t D ]}| | qd S r)   )rR   ro   rk   r.   r.   r/   restore  s    zStoreBackup.restoreN)r+   r:   r;   r<   r5   rj   ro   rp   r.   r.   r.   r/   rh      s
   rh   c                  C   s.   t  D ]\} }|j  |  qt adS )z 
    Copy the clean stores.
    N)rR   ra   rG   rH   rJ   rh   clean_store_backup)_krg   r.   r.   r/   make_clean_stores"  s    

rs   c                   C   s   t   dS )z-
    Revert the store to the clean copy.
    N)rq   rp   r.   r.   r.   r/   clean_stores1  s    rt   c                 C   s    |  dsd|  } t|  dS )z4
    Reverts the named store to its clean copy.
    store.N)
startswithrq   ro   r?   r.   r.   r/   clean_store9  s    
rw   c                 C   s&   |  dsd|  } t|  }|  d S )Nru   )rv   rR   rJ   )r@   rT   r.   r.   r/   reset_store_changesD  s    
rx   c                 C   s   t r| dS | S d S )Nzutf-8)r   encodesr.   r.   r/   bO  s    
r|   c                   @   s,   e Zd ZdZdd Zes dd Zdd ZdS )	LoadedVariablesz:
    This is used to implement find_loaded_variables.
    c                 C   s`   t |jtjr| j|j n>t |jtjr<| j|j n t	r\t |jtj
r\| j|j d S r)   )
isinstancectxastLoadloadedrK   idStorestoredr   Paramr-   noder.   r.   r/   
visit_Name[  s    zLoadedVariables.visit_Namec                 C   s   | j |j d S r)   )r   rK   argr   r.   r.   r/   	visit_argg  s    zLoadedVariables.visit_argc                 C   s&   t  | _t  | _| | | j| j S r)   )rF   r   r   visitr   r.   r.   r/   findj  s    
zLoadedVariables.findN)r+   r:   r;   r<   r   r   r   r   r.   r.   r.   r/   r}   V  s
   
r}   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	StarredVariablesz
    Return a list of variables that are set using starred assignment, and hence
    need to be wrapped in RevertableList.
    c                 C   s2   t |jtjr.t |jjtjr.| j|jj d S r)   )	r~   r8   r   Namer   r   starredrK   r   r   r.   r.   r/   visit_Starred}  s    zStarredVariables.visit_Starredc                 C   s    t |jtjr| j|j d S r)   )r~   r   r   r   r   discardr   r   r.   r.   r/   r     s    zStarredVariables.visit_Namec                 C   s"   t  | _|D ]}| | q| jS r)   )rF   r   r   )r-   targetsir.   r.   r/   r     s    zStarredVariables.findN)r+   r:   r;   r<   r   r   r   r.   r.   r.   r/   r   w  s   r   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%S )&WrapNodec              	   C   s   |  |}ttt|}g }g }|D ]L}trH|tj|t d n|tj	|d |tj|t
 d q&trtjtjtj|ddg d|d|g dddS tjtjtjg |g g g d|d|g dS dS )	aw  
        This wraps generators in lambdas, such that:

            (i for i in l if i == b)

        becomes:

            (lambda l, b : (i for i in l if i == b))(l, b)

        Why do this? It's because if b is a local, it's not present inside
        the generator expression scope, and when compiled independently of
        a larger scope, no cell is generated.
        r   r   )r   N)argsvarargkwargdefaults)r   bodyfuncr   keywordsstarargskwargs)posonlyargsr   
kwonlyargskw_defaultsr   )r   r   r   )generic_visitlistsortedfind_loaded_variablesr   appendr   r   r   r   r   CallLambda	arguments)r-   r   	variableslambda_args	call_argsre   r.   r.   r/   wrap_generator  sN    
	
zWrapNode.wrap_generatorc                 C   s   t r|S t|}|s|S g }|D ]b}tjtjtdt dtj|t dgg d d d}tjtj|t dg|d}|	| q tj
|gg g |dS )N__renpy__list__r   r   r   r8   )r   handlersorelse	finalbody)r   find_starred_variablesr   r   r   r|   r   Assignr   r   Try)r-   nr   r   
list_stmtsre   callassignr.   r.   r/   wrap_starred_assign  s:    zWrapNode.wrap_starred_assignc                 C   s   t r|S t|jg}|s|S |D ]f}tjtjtdt dtj|t dgg d d d}tjtj|t	 dg|d}|j
d| q |S Nr   r   r   r   r   )r   r   targetr   r   r   r|   r   r   r   r   insert)r-   r   r   re   r   r   r.   r.   r/   wrap_starred_for  s.    zWrapNode.wrap_starred_forc                 C   s   t r|S g }|jD ]}|jd ur||j q|s6|S t|}|sF|S |D ]f}tjtjtdt	 dtj|t	 dgg d d d}tj
tj|t dg|d}|jd| qJ|S r   )r   ra   optional_varsr   r   r   r   r   r|   r   r   r   r   r   )r-   r   r   r   r   re   r   r   r.   r.   r/   wrap_starred_with  s:    

zWrapNode.wrap_starred_withc                 C   s   |  |}| ||jS r)   )r   r   r   r-   r   r.   r.   r/   visit_AssignI  s    
zWrapNode.visit_Assignc                 C   s   |  |}| ||jgS r)   )r   r   r   r   r.   r.   r/   visit_AnnAssignM  s    
zWrapNode.visit_AnnAssignc                 C   s   |  |}| |S r)   r   r   r   r.   r.   r/   	visit_ForQ  s    
zWrapNode.visit_Forc                 C   s   |  |}| |S r)   r   r   r.   r.   r/   visit_AsyncForU  s    
zWrapNode.visit_AsyncForc                 C   s   |  |}| |S r)   r   r   r   r.   r.   r/   
visit_WithY  s    
zWrapNode.visit_Withc                 C   s   |  |}| |S r)   r   r   r.   r.   r/   visit_AsyncWith]  s    
zWrapNode.visit_AsyncWithc                 C   s2   |  |}|js.|jtjtdt d |S )Nr2   r   )r   basesr   r   r   r|   r   r   r.   r.   r/   visit_ClassDefa  s    
zWrapNode.visit_ClassDefc                 C   s
   |  |S r)   )r   r   r.   r.   r/   visit_GeneratorExpi  s    zWrapNode.visit_GeneratorExpc                 C   s.   t jt jtdt  d| |gg d d dS N__renpy__set__r   r   r   r   r   r|   r   r   r   r.   r.   r/   visit_SetCompl  s    
zWrapNode.visit_SetCompc                 C   s.   t jt jtdt  d| |gg d d dS r   r   r   r   r|   r   r   r   r.   r.   r/   	visit_Setw  s    
zWrapNode.visit_Setc                 C   s.   t jt jtdt  d| |gg d d dS Nr   r   r   r   r   r.   r.   r/   visit_ListComp  s    
zWrapNode.visit_ListCompc                 C   sF   t |jtjs| |S tjtjtdt d| |gg d d dS r   )r~   r   r   r   r   r   r   r|   r   r.   r.   r/   
visit_List  s    

zWrapNode.visit_Listc                 C   s.   t jt jtdt  d| |gg d d dS N__renpy__dict__r   r   r   r   r.   r.   r/   visit_DictComp  s    
zWrapNode.visit_DictCompc                 C   s.   t jt jtdt  d| |gg d d dS r   r   r   r.   r.   r/   
visit_Dict  s    
zWrapNode.visit_DictN)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     s$   A("*r   c                 C   sF   t d}t |D ]}t ||jd  q| j|jd _|j| _dS )z
    Wraps code inside a python hide or python early hide block inside a
    function, so it gets its own scope that works the way Python expects
    it to.
    z9def _execute_python_hide(): pass;
_execute_python_hide()
r   N)r   parsewalkcopy_locationr   )treehider   r.   r.   r/   	wrap_hide  s
    
r   z[\u0080-\uffff]c                 C   sb   |  d}t|s|S |  d}|  d}|  d}d|vrNd|vrNd| }|| | | }|S )zZ
    If the string s contains a unicode character, make it into a
    unicode string.
    r            uU)group
unicode_research)mr{   prefixsepr   rN   r.   r.   r/   unicode_sub  s    




r   z*([uU]?[rR]?)("""|"|\'\'\'|\')((\\.|.)*?)\2c                 C   s   t | rtt| } | S r)   )r   r   	string_resubr   rz   r.   r.   r/   escape_unicode  s    
r   c                 C   sd   d| j v r"t| ds|| _n| j}d| j v rDt| ds>|| _n| j}t| D ]}t||| qNd S )Nlineno
col_offset)_attributeshasattrr   r   r   iter_child_nodesfix_missing_locations)r   r   r   childr.   r.   r/   r     s    



r   c           	      C   s  d| vr| S g }| d7 } t | }d}d}d}||k r| | }||vrv|}|d7 }| | |v rJqbqJ|| ||  q,|dkr|| |d7 }q,|dkr|| ||d	   |d	7 }q,|d
v rP|du st |dkr| |d  |kr| |d	  |kr|| | }n|}|dur*||kr*d}n|du r8|}|| |t |7 }q,|dkr|du rn|d |d |d7 }q,td|q,d|dd S )z
    Quotes a string for `eval`. This is necessary when it's in certain places,
    like as part of an argument string. We need to stick a single backslash
    at the end of lines that don't have it already, and that aren't in triple-quoted strings.
    
z  r   z \'"
Nr    \r   z'"r   z#Unknown character {} (can't happen) )lenr   rD   formatjoin)	r{   rN   len_sr   specialstringcstartdelimr.   r.   r/   
quote_eval  sX    


:





r  <none>r   FTc                 C   sj  |rd}t | tjr t| ||S t | tjjrH| j}| j}|du rH| j}|du r^t	rZd}nd}|r||t
| |tjjf}t|d}|dur|S t|d}|dur|t|< |S tjjj|d}	|	dur|	tjjj|< t|	}|t|< |S nd}t
| } | dd} |dkrt| } |d }
z|d	kr6d
}n|}t	rJ|tv rt}zt| ||tj|B d}W nh ty } zNz(tjj| }t|||tj|B d}W n ty   |Y n0 W Y d}~n
d}~0 0 nVzt}t| ||tj|B d}W n6 ty(   t }t!| } t| ||tj|B d}Y n0 t"#|}|d	krFt$| t%|dd t&||d  d}
|rt|j'W S zt||||d}W nf ty } zLz.tjj(|}t%|dd t||||d}W n   |Y n0 W Y d}~n
d}~0 0 |r|t|< t)|tjjj|< dtjj_*|W S  tyd } z*|j+durL| j+|
7  _+|W Y d}~n
d}~0 0 dS )a  
    Compiles the given source code using the supplied codegenerator.
    Lists, List Comprehensions, and Dictionaries are wrapped when
    appropriate.

    `source`
        The source code, as a either a string, pyexpr, or ast module
        node.

    `mode`
        One of "exec" or "eval".

    `filename`
        The filename the source comes from. If a pyexpr is given, the
        filename embedded in the pyexpr is used.

    `lineno`
        The line number of the first line of source code. If a pyexpr is
        given, the filename embedded in the pyexpr is used.

    `ast_node`
        Rather than returning compiled bytecode, returns the AST object
        that would be used.
    FNr   r   r   r^   r   r   execr   T),r~   r   Modulecompiler_   PyExprfilename
linenumberpyr   r   scriptMAGICpy_compile_cachegetold_py_compile_cachegamebytecode_oldcachebytecode_newcachemarshalloadsreplacer  	py3_filespy3_compile_flagsPyCF_ONLY_ASTSyntaxErrorcompatfixes
fix_tokensrD   new_compile_flagsold_compile_flagsr   	wrap_noder   r   r   increment_linenor   fix_astdumpsbytecode_dirtyr   )sourcemoder  r   ast_nodecacher  r7   rN   bytecodeline_offsetpy_modeflagsr   orig_efixed_sourceer.   r.   r/   
py_compilet  s    


"

 
r4  c                 K   s    t | dfddi|}t|S )Nr	  r,  Fr4  r  r'  r)  r   coder.   r.   r/   py_compile_exec_bytecode  s    r8  c                 K   s    t | dfddi|}t|S )Nr   r,  Fr5  r6  r.   r.   r/   py_compile_hide_bytecode  s    r9  c                 K   s(   |   } t| dfddi|}t|S )Nr^   r,  F)stripr4  r  r'  r6  r.   r.   r/   py_compile_eval_bytecode  s    r;  ri   c                 C   s4   |ri }|d u rt | }|d u r$|}t| || d S r)   )rR   r	  )r-  r   globalslocalsri   r.   r.   r/   py_exec_bytecode  s    r>  c                 C   s4   |d u rt d }|ri }n|}tt| d|| d S )Nri   r	  )rR   r	  r4  )r)  r   ri   r=  r.   r.   r/   py_exec(  s    r?  c                 C   s(   |d u rt d }|d u r|}t| ||S )Nri   )rR   r^   )r-  r<  r=  r.   r.   r/   py_eval_bytecode5  s
    r@  c                 C   s    t | trt| d} t| ||S )Nr^   )r~   r   r4  r@  r7  r<  r=  r.   r.   r/   py_eval@  s    

rB  c                 C   s    |d u rt dj}t| ||S )Nr   )r=   	_getframe	f_globalsrB  rA  r.   r.   r/   
store_evalG  s    rE  c                 C   sB   |\}}t d|}t ||d  t||d}t|d| i dS )z
    Raises `e` (which must be an Exception object) at location `loc`.

    `loc`
        A location, which should be a (filename, line_number) tuple.
    zraise er   r	  r3  N)r   r   r%  r  r	  )r3  locr  liner   r7  r.   r.   r/   raise_at_locationO  s
    rH  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )
StoreProxyc                 C   s   t tj|S r)   )getattrr_   ri   rk   r.   r.   r/   __getattr__e  s    zStoreProxy.__getattr__c                 C   s   t tj|| d S r)   )setattrr_   ri   )r-   rP   rg   r.   r.   r/   r3   h  s    zStoreProxy.__setattr__c                 C   s   t tj| d S r)   )delattrr_   ri   rk   r.   r.   r/   r9   k  s    zStoreProxy.__delattr__N)r+   r:   r;   rK  r3   r9   r.   r.   r.   r/   rI  c  s   rI  c                 C   s
   t | |S r)   )rJ  )objr@   r.   r.   r/   method_unpickleo  s    rO  c                 C   s(   | j j}| j}|d u r| j}t||ffS r)   )Zim_funcr+   Zim_selfZim_classrO  )methodr@   rN  r.   r.   r/   method_pickleu  s
    rQ  c                 C   s"   t jjrtd| t| jffS )NzCould not pickle {!r}.)r_   config	developerrD   r   module_unpickler+   )moduler.   r.   r/   module_pickle  s    rV  c                 C   s   t | S r)   )
__import__r?   r.   r.   r/   rT    s    rT  )r  r   FTN)FNNri   )FN)NN)NN)NN)x
__future__r   r   r   r   r   renpy.compatr   r   r	   r
   r   r   r   r   r   r   r   r   typingr   r   r   r  randomweakrefrer=   timeiotypescopyreg	functoolsr_   renpy.revertabler   r   r   r   r   r   r   r   r   renpy.rollbackr   r   r    r!   r"   r#   r$   r%   r&   r'   r2   r(   r*   renpy.pydictrA   rB   rl   rC   rU   rR   rb   rF   r\   r[   rh   rq   rs   rt   rw   rx   r|   NodeVisitorr}   r   r   r   r   NodeTransformerr   r$  r   r  r   r   r   r   nested_scopescompiler_flagr#  r"  r  annotationsr  r  r  r   r  r4  r8  r9  r;  r>  r?  r@  rB  rE  rH  rI  rO  rQ  pickle
MethodTyperV  rT  
ModuleTyper.   r.   r.   r/   <module>   s   8,0F,-  &

Y
 






