B
    |bto                 @   s   d dl mZmZm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 ddlmZ ddlmZ d(d
dZdd Zdd ZG dd deZG dd deZdd Zdd Zd)ddZdd Zd*ddZG dd dZd d! Zd"d# Zd$d% ZG d&d' d'eZdS )+    )absolute_importdivisionunicode_literalsN   )compaterrorshandlerstagsutil)json)numeric_typesFTignorec	       
      C   s`   t |tr| }nt|s(td |p.t}|pDt|||||d}|	| }	|j
|	||dS )a  Convert a JSON string into a Python object.

    The keyword argument 'keys' defaults to False.
    If set to True then jsonpickle will decode non-string dictionary keys
    into python objects via the jsonpickle protocol.

    The keyword argument 'classes' defaults to None.
    If set to a single class, or a sequence (list, set, tuple) of classes,
    then the classes will be made available when constructing objects.  This
    can be used to give jsonpickle access to local classes that are not
    available through the global module import scope.

    The keyword argument 'safe' defaults to False.
    If set to True, eval() is avoided, but backwards-compatible
    (pre-0.7.0) deserialization of repr-serialized objects is disabled.

    The keyword argument 'backend' defaults to None.
    If set to an instance of jsonpickle.backend.JSONBackend, jsonpickle
    will use that backend for deserialization.

    The keyword argument 'v1_decode' defaults to False.
    If set to True it enables you to decode objects serialized in jsonpickle v1.
    Please do not attempt to re-encode the objects in the v1 format! Version 2's
    format fixes issue #255, and allows dictionary identity to be preserved
    through an encode/decode cycle.

    The keyword argument 'on_missing' defaults to 'ignore'.
    If set to 'error', it will raise an error if the class it's decoding is not
    found. If set to 'warn', it will warn you in said case. If set to a
    non-awaitable function, it will call said callback function with the class
    name (a string) as the only parameter. Strings passed to on_missing are
    lowercased automatically.


    >>> decode('"my string"') == 'my string'
    True
    >>> decode('36')
    36
    zHUnpickler.on_missing must be a string or a function! It will be ignored!)keysbackendsafe	v1_decode
on_missing)resetclasses)
isinstancestrlowerr
   is_functionwarningswarnr   	Unpicklerdecoderestore)
stringr   contextr   r   r   r   r   r   data r!   3lib/python3.7/site-packages/jsonpickle/unpickler.pyr      s    3



r   c             C   s*   yt | | dS  tk
r$   dS X dS )zBWorkaround unreliable hasattr() availability on sqlalchemy objectsTFN)object__getattribute__AttributeError)objattrr!   r!   r"   _safe_hasattrX   s
    r(   c             C   s   t | tjo| tjS )z<Has this key a special object that has been encoded to JSON?)r   r   Zstring_types
startswithr	   JSON_KEY)keyr!   r!   r"   _is_json_keya   s    r,   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_Proxya  Proxies are dummy objects that are later replaced by real instances

    The `restore()` function has to solve a tricky problem when pickling
    objects with cyclical references -- the parent instance does not yet
    exist.

    The problem is that `__getnewargs__()`, `__getstate__()`, custom handlers,
    and cyclical objects graphs are allowed to reference the yet-to-be-created
    object via the referencing machinery.

    In other words, objects are allowed to depend on themselves for
    construction!

    We solve this problem by placing dummy Proxy objects into the referencing
    machinery so that we can construct the child objects before constructing
    the parent.  Objects are initially created with Proxy attribute values
    instead of real references.

    We collect all objects that contain references to proxies and run
    a final sweep over them to swap in the real instance.  This is done
    at the very end of the top-level `restore()`.

    The `instance` attribute below is replaced with the real instance
    after `__new__()` has been used to construct the object and is used
    when swapping proxies with real instances.

    c             C   s
   d | _ d S )N)instance)selfr!   r!   r"   __init__   s    z_Proxy.__init__c             C   s   | j S )N)r.   )r/   r!   r!   r"   get   s    z
_Proxy.getc             C   s
   || _ d S )N)r.   )r/   r.   r!   r!   r"   r      s    z_Proxy.resetN)__name__
__module____qualname____doc__r0   r1   r   r!   r!   r!   r"   r-   f   s   r-   c               @   s   e Zd Zdd Zdd ZdS )_IDProxyc             C   s   || _ || _d S )N)_index_objs)r/   Zobjsindexr!   r!   r"   r0      s    z_IDProxy.__init__c             C   s   | j | j S )N)r8   r7   )r/   r!   r!   r"   r1      s    z_IDProxy.getN)r2   r3   r4   r0   r1   r!   r!   r!   r"   r6      s   r6   c             C   s   t | ||  d S )N)setattrr1   )r&   r'   proxyr!   r!   r"   _obj_setattr   s    r<   c             C   s   |  | |< d S )N)r1   )r&   idxr;   r!   r!   r"   _obj_setvalue   s    r>   c             C   s   |r"y||  S  t k
r    Y nX | d}xtt|d ddD ]}td|d| }yRt| tj	| }x:||d D ]*}yt
||}W q tk
r   wY qX qW |S  tttfk
r   wBY qBX qBW dS )zLoads the module and returns the class.

    >>> cls = loadclass('datetime.datetime')
    >>> cls.__name__
    'datetime'

    >>> loadclass('does.not.exist')

    >>> loadclass('builtins.int')()
    0

    .r   r   N)KeyErrorsplitrangelenr
   Zuntranslate_module_namejoin
__import__sysmodulesgetattrr%   ImportError
ValueError)Zmodule_and_namer   namesZup_tomoduler&   
class_namer!   r!   r"   	loadclass   s(    

rO   c             C   s   t | tko|| kS )zHelper class that tests to see if the obj is a dictionary
    and contains a particular key/tag.

    >>> obj = {'test': 1}
    >>> has_tag(obj, 'test')
    True
    >>> has_tag(obj, 'fail')
    False

    >>> has_tag(42, 'fail')
    False

    )typedict)r&   tagr!   r!   r"   has_tag   s    rS   c             C   s   t | tjrtdt | tjr*| tj S t | tjr@| tj S y| tj }| tj }W n tk
rl   g S X t	||d}|sg S t
|drt|jt|kr|S g S )z'Return arguments suitable for __new__()z+__newargs_ex__ returns both args and kwargs)r   _fields)rS   r	   	NEWARGSEXrK   ZNEWARGSZINITARGSSEQOBJECTrA   rO   hasattrrD   rT   )r&   r   Zseq_listZobj_dicttyperefr!   r!   r"   getargs   s$    



rZ   c               @   s   e Zd ZdZdS )_trivialclassicz?
    A trivial class that can be instantiated with no args
    N)r2   r3   r4   r5   r!   r!   r!   r"   r[      s   r[   c             C   s   t  }| |_|S )z
    Implement the mandated strategy for dealing with classic classes
    which cannot be instantiated without __getinitargs__ because they
    take parameters
    )r[   	__class__)clsr.   r!   r!   r"   make_blank_classic   s    r^   c             C   sD   |  d\}}t }|}d|kr0| ddd }t|||< t|S )zReturns an instance of the object from the object's repr() string.
    It involves the dynamic specification of code.

    >>> obj = loadrepr('datetime/datetime.datetime.now()')
    >>> obj.__class__.__name__
    'datetime'

    /r?   r   r   )rB   localsrF   eval)ZreprstrrM   ZevalstrZmylocalsZ	localnamer!   r!   r"   loadrepr  s    	rb   c             C   s   || kS )zHelper class that tests to see if the obj is a dictionary
    and contains a particular key/tag.

    >>> obj = {'test': 1}
    >>> has_tag(obj, 'test')
    True
    >>> has_tag(obj, 'fail')
    False

    >>> has_tag(42, 'fail')
    False

    r!   )r&   rR   r!   r!   r"   has_tag_dict  s    rc   c               @   s  e Zd ZdHddZdd Zdd	 Zd
d ZdId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/d0 Zd1d2 ZdJd3d4Zd5d6 Zd7d8 ZdKd:d;Zd<d= Zd>d? Zd@dA Z dBdC Z!dDdE Z"dFdG Z#dS )Lr   NFr   c             C   s.   |pt | _|| _|| _|| _|| _|   d S )N)r   r   r   r   r   r   r   )r/   r   r   r   r   r   r!   r!   r"   r0   &  s    
zUnpickler.__init__c             C   s(   i | _ g | _i | _g | _g | _i | _dS )z#Resets the object's internal state.N)	_namedict
_namestack_obj_to_idxr8   _proxies_classes)r/   r!   r!   r"   r   1  s    zUnpickler.resetc             C   s.   x"| j D ]\}}}}|||| qW g | _ dS )z2Replace proxies with their corresponding instancesN)rg   )r/   r&   r'   r;   methodr!   r!   r"   _swap_proxiesA  s    zUnpickler._swap_proxiesc             C   s0   t |tttttfsdd }n
| |}||S )Nc             S   s   | S )Nr!   )xr!   r!   r"   r   L  s    z#Unpickler._restore.<locals>.restore)r   r   listrQ   settuple_restore_tags)r/   r&   r   r!   r!   r"   _restoreG  s    

zUnpickler._restoreTc             C   s4   |r|    |r| | | |}|r0|   |S )a#  Restores a flattened object to its original python state.

        Simply returns any of the basic builtin types

        >>> u = Unpickler()
        >>> u.restore('hello world') == 'hello world'
        True
        >>> u.restore({'key': 'value'}) == {'key': 'value'}
        True

        )r   register_classesrp   rj   )r/   r&   r   r   valuer!   r!   r"   r   S  s    

zUnpickler.restorec             C   s>   t |tttfr*x(|D ]}| | qW n|| jt|< dS )zqRegister one or more classes

        :param classes: sequence of classes or a single class to register

        N)r   rl   rn   rm   rq   rh   r
   Zimportable_name)r/   r   r]   r!   r!   r"   rq   h  s    
zUnpickler.register_classesc             C   s   t |tj dS )Nzutf-8)r
   Z	b64decoder	   B64encode)r/   r&   r!   r!   r"   _restore_base64t  s    zUnpickler._restore_base64c             C   s   t |tj dS )Nzutf-8)r
   Z	b85decoder	   B85rt   )r/   r&   r!   r!   r"   _restore_base85w  s    zUnpickler._restore_base85c             C   s   t |tj dS )Nzutf-8)quopriZdecodestringr	   BYTESrt   )r/   r&   r!   r!   r"   _restore_quopri{  s    zUnpickler._restore_quopric             C   s   dd | j S )a  Calculates the name of the current location in the JSON stack.

        This is called as jsonpickle traverses the object structure to
        create references to previously-traversed objects.  This allows
        cyclical data structures such as doubly-linked lists.
        jsonpickle ensures that duplicate python references to the same
        object results in only a single JSON object definition and
        special reference tags to represent each reference.

        >>> u = Unpickler()
        >>> u._namestack = []
        >>> u._refname() == '/'
        True
        >>> u._namestack = ['a']
        >>> u._refname() == '/a'
        True
        >>> u._namestack = ['a', 'b']
        >>> u._refname() == '/a/b'
        True

        r_   )rE   re   )r/   r!   r!   r"   _refname~  s    zUnpickler._refnamec             C   sZ   t |}y| j|  W n> tk
rT   t| j| j|< | j| || j|  < Y nX |S )N)idrf   rA   rD   r8   appendrd   r{   )r/   r&   Zobj_idr!   r!   r"   _mkref  s    zUnpickler._mkrefc                sV   g   fdd|D }| t  fddtD }j| S )Nc                s   g | ]}  |qS r!   )rp   ).0v)r/   r!   r"   
<listcomp>  s    z+Unpickler._restore_list.<locals>.<listcomp>c                s&   g | ]\}}t |tr|| fqS r!   )r   r-   )r   r=   rr   )ri   parentr!   r"   r     s   )r~   extendr>   	enumeraterg   )r/   r&   ZchildrenZproxiesr!   )ri   r   r/   r"   _restore_list  s    

zUnpickler._restore_listc             C   s   t | |tj S )N)iterr   r	   ITERATOR)r/   r&   r!   r!   r"   _restore_iterator  s    zUnpickler._restore_iteratorc             C   sH   t |}t |}| j| }|| j|< | j|= || j|< || j|  < d S )N)r|   rf   r8   rd   r{   )r/   r;   r.   Zproxy_idZinstance_idZinstance_indexr!   r!   r"   _swapref  s    


zUnpickler._swaprefc             C   s,  t  }| | tt| j|tj }t|dk rJ|dgdt|   |\}}}}}|tj	ksrt
|dddkr|d }	t|	ts| |	}	|	j|	f|dd  }
n|| }
|ry|
| W n tk
r   y.x"|
j D ]\}}||| qW ||
_W n tk
r   y(x"| D ]\}}t|
|| q"W W nZ tk
r   |\}}|rj|
j| |rx"| D ]\}}t|
|| qzW Y nX Y nX Y nX |ry|
| W n0 tk
r   x|D ]}|
| qW Y nX |rx|D ]\}}|
|| qW ||
 | ||
 |
S )z
        Supports restoring with all elements of __reduce__ as per pep 307.
        Assumes that iterator items (the last two) are represented as lists
        as per pickler implementation.
           Nr2    
__newobj__r   r   )r-   r~   rl   maprp   r	   REDUCErD   r   ZNEWOBJrI   r   rP   __new____setstate__r%   __dict__items
setdefaultr:   	Exceptionupdater}   __setitem__r   r   )r/   r&   r;   Z
reduce_valfargsstateZ	listitemsZ	dictitemsr]   Zstage1kr   Z
dict_stateZslots_staterk   r!   r!   r"   _restore_reduce  sV    



$

zUnpickler._restore_reducec             C   s6   y|t j }| j| S  tk
r0   t| j|S X d S )N)r	   IDr8   
IndexErrorr6   )r/   r&   r=   r!   r!   r"   _restore_id  s
    

zUnpickler._restore_idc             C   s   | j |tj S )N)rd   r1   r	   REF)r/   r&   r!   r!   r"   _restore_ref  s    zUnpickler._restore_refc             C   s$   t |tj | jd}|d kr |S |S )N)r   )rO   r	   TYPErh   )r/   r&   rY   r!   r!   r"   _restore_type  s    zUnpickler._restore_typec             C   s"   | j r
d S t|tj }| |S )N)r   rb   r	   REPRr~   )r/   r&   r!   r!   r"   _restore_repr  s    zUnpickler._restore_reprc             C   s2   y|d }W n t k
r    d S X |d= | |S )Ndefault_factory)rA   rp   )r/   r&   r   r!   r!   r"   _loadfactory  s    zUnpickler._loadfactoryc             C   sZ   | j dkrnJ| j dkr&td|  n0| j dkr@td| nt| j rV|  | d S )Nr   r   z,Unpickler._restore_object could not find %s!errorz+Unpickler.restore_object could not find %s!)r   r   r   r   ZClassNotFoundErrorr
   r   )r/   rN   r!   r!   r"   _process_missing  s    


zUnpickler._process_missingc             C   s.   t |r*t|ttjd | j| ddd}|S )zRestore a possibly pickled keyNTF)r   r   r   r   )r,   r   rD   r	   r*   r   )r/   r+   r!   r!   r"   _restore_pickled_key)  s    zUnpickler._restore_pickled_keyc             C   s   | j r| j}ndd }|S )zReturn a callable that restores keys

        This function is responsible for restoring non-string keys
        when we are decoding with `keys=True`.

        c             S   s   | S )Nr!   )r+   r!   r!   r"   restore_keyE  s    z.Unpickler._restore_key_fn.<locals>.restore_key)r   r   )r/   r   r!   r!   r"   _restore_key_fn5  s    zUnpickler._restore_key_fnc          	   C   sJ  |   }t}i }x"t|D ]\}}|r8|tjkr8qt|trL| }	n|}	| j	
|	 ||}| |}
t|st|ry"|dkrt|||
 n|
||< W n0 tk
r   |dkr|
||< | j	  wY nX n0|dst|||
 nt|d|jj | |
 t|
tr(| j
|||
|f | j	  qW |rF||}|S )Nr   ___)r   r<   r
   r   r	   RESERVEDr   r   __str__re   r}   rp   Zis_noncomplexZis_dictionary_subclassr:   	TypeErrorpopr)   r\   r2   r-   rg   )r/   r&   r.   ignorereservedr   ri   Zdeferredr   r   str_krr   r!   r!   r"   _restore_from_dictJ  s>    






zUnpickler._restore_from_dictc             C   s   |  |tj }t|to2t|dko2t|d t}|oDt|d t}t|dr\|| nft|trx| j	||dd}nJ|r| j	|d |dd}|r| j	|d |dd}nt|dst|ds|}|S )	N   r   r   r   F)r   __getnewargs____getnewargs_ex__)
rp   r	   STATEr   rn   rD   rQ   rX   r   r   )r/   r&   r.   r   Z	has_slotsZhas_slots_and_dictr!   r!   r"   _restore_state|  s     $

zUnpickler._restore_statec             C   s   |  ||}t|tjrvt|drHxR|tj D ]}|| | q.W n.t|drvx"|tj D ]}|| | q^W t|tjr| 	||}|S )Nr}   add)
r   rS   r	   rV   rX   r}   rp   r   r   r   )r/   r&   r.   r   r!   r!   r"   "_restore_object_instance_variables  s    

z,Unpickler._restore_object_instance_variablesr   c       
      C   s  t  }| | | |}t|tjr6|tj \}}nt|| jd}i }|rV| |}|rd| |}t	|t
pxt|dd  }yP|st|dr|r|j||f||}	||	_q|j|f||}	n
t|}	W n tk
r   d}Y nX |rDy|| }	W nJ tk
rB   yt|}	W n& tk
r<   | | | |S X Y nX ||	 | ||	 t	|	trj|	S | ||	}	t|	drt	|	jt r|	j |	_|	S )N)r   Z__meta__r   Tr   )r-   r~   r   rS   r	   rU   rZ   rh   rp   r   rP   rI   rX   r   r   r#   r   r^   r   r   r   r   rn   r   r(   r1   )
r/   r&   r]   rN   r;   factoryr   kwargsZis_oldstyler.   r!   r!   r"   _restore_object_instance  sN    






z"Unpickler._restore_object_instancec             C   s   |t j }t|| jd}t|t|}|d k	rjt }| | || |}|	| | 
|| |S |d kr|| |S | |||S )N)r   )r	   rW   rO   rh   r   r1   r-   r~   r   r   r   r   )r/   r&   rN   r]   Zhandlerr;   r.   r!   r!   r"   _restore_object  s    



zUnpickler._restore_objectc             C   s   t |tj | jdS )N)r   )rO   r	   FUNCTIONrh   )r/   r&   r!   r!   r"   _restore_function  s    zUnpickler._restore_functionc                s    fdd|t j D S )Nc                s   h | ]}  |qS r!   )rp   )r   r   )r/   r!   r"   	<setcomp>  s    z)Unpickler._restore_set.<locals>.<setcomp>)r	   SET)r/   r&   r!   )r/   r"   _restore_set  s    zUnpickler._restore_setc             C   sF  i }| j s| | | jrx\t|D ]N\}}t|r8q&t|trL| }n|}| j	
| | |||< | j	  q&W xt|D ]b\}}t|sq| j	
| | |}| | ||< }t|tr| j
|||tf | j	  qW nVxTt|D ]F\}}t|tr| }n|}| j	
| | |||< | j	  qW |S )N)r   r~   r   r
   r   r,   r   r   r   re   r}   rp   r   r   r-   rg   r>   )r/   r&   r    r   r   r   resultr!   r!   r"   _restore_dict  s<    





zUnpickler._restore_dictc             C   s  y.t jt|ks,t|ttfkr,dd }|S W n tk
rB   Y nX t|tkrjt|t jrh| j	}qt|t j
r|| j}nt|t jr| j}nt|t jr| j}nt|t jr| j}nt|t jr| j}nt|t jr| j}nt|t jr| j}ntt|t jr
| j}n^t|t jr | j}nHt|t jr6| j}n2t|t jrL| j}nt|t j rb| j!}n| j"}nt#$|r~| j%}ndd }|S )Nc             S   s   | S )Nr!   )rk   r!   r!   r"   r   0  s    z(Unpickler._restore_tags.<locals>.restorec             S   s   | S )Nr!   )rk   r!   r!   r"   r   W  s    )&r	   r   rm   rP   rl   rQ   r   rc   TUPLE_restore_tupler   r   rs   ru   rv   rw   r   r   r   r   rW   r   r   r   r   r   r   r   ry   rz   r   r   r   r   r   r
   Zis_listr   )r/   r&   r   r!   r!   r"   ro   ,  sL    
zUnpickler._restore_tagsc                s   t  fdd|tj D S )Nc                s   g | ]}  |qS r!   )rp   )r   r   )r/   r!   r"   r   ]  s    z,Unpickler._restore_tuple.<locals>.<listcomp>)rn   r	   r   )r/   r&   r!   )r/   r"   r   \  s    zUnpickler._restore_tuple)NFFFr   )TN)T)r   )$r2   r3   r4   r0   r   rj   rp   r   rq   ru   rw   rz   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ro   r   r!   r!   r!   r"   r   %  sB   


?
2
;00r   )NNFTFNFr   )N)N) Z
__future__r   r   r   rx   rG   r   r   r   r   r   r	   r
   r   r   r   r   r(   r,   r#   r-   r6   r<   r>   rO   rS   rZ   r[   r^   rb   rc   r   r!   r!   r!   r"   <module>   s8          
=	'	
(
