U
    P|j1  ã                   @   sP  U d Z ddlZddlZddlZddlZddlZddlmZ	 ddl
mZ ddlmZmZ ddlmZ i Zeeejf ed< ejZdd	„ Zd
d„ Zdd„ Ze eje¡ dd„ Zdd„ Ze eje¡ dd„ Zdd„ Ze eje¡ dd„ Z dd„ Z!dd„ Z"dd„ Z#G dd„ dƒZ$i a%ee&d f ed!< i a'd"d#„ Z(d$d%„ Z)d&d'„ Z*G d(d „ d ƒZ+dS ))z(
Different styles of persisted objects.
é    N)ÚStringIO)ÚDict)ÚlogÚreflect)Ú_PYPYÚ
oldModulesc                 C   s   t | j| j| jjffS )z3support function for copy_reg to pickle method refs)ÚunpickleMethodÚ__name__Ú__self__Ú	__class__)Úmethod© r   ú</tmp/pip-unpacked-wheel-audpp6lv/twisted/persisted/styles.pyÚpickleMethod   s    þr   c                 C   s   t | |ƒ}|S )a³  
    Retrieve the function object implementing a method name given the class
    it's on and a method name.

    @param classObject: A class to retrieve the method's function from.
    @type classObject: L{type}

    @param methodName: The name of the method whose function to retrieve.
    @type methodName: native L{str}

    @return: the function object corresponding to the given method name.
    @rtype: L{types.FunctionType}
    )Úgetattr)ZclassObjectÚ
methodNameZmethodObjectr   r   r   Ú_methodFunction"   s    
r   c              	   C   sŽ   |dkrt || ƒS zt|| ƒ}W nN tk
rn   t d| d|¡ |dk	sPtdƒ‚|j|kr\‚ t| ||jƒ Y S X d}tj	||f|žŽ }|S dS )aS  
    Support function for copy_reg to unpickle method refs.

    @param im_name: The name of the method.
    @type im_name: native L{str}

    @param im_self: The instance that the method was present on.
    @type im_self: L{object}

    @param im_class: The class where the method was declared.
    @type im_class: L{type} or L{None}
    NZMethodznot on classz'No recourse: no instance to guess from.r   )
r   r   ÚAttributeErrorr   ÚmsgÚAssertionErrorr   r   ÚtypesÚ
MethodType)Zim_nameZim_selfZim_classZmethodFunctionZ
maybeClassÚboundr   r   r   r   4   s    

r   c                 C   s4   | j dkrtd| › �ƒ‚ttd | j| jg¡gƒfS )a—  
    Reduce, in the sense of L{pickle}'s C{object.__reduce__} special method, a
    function object into its constituent parts.

    @param f: The function to reduce.
    @type f: L{types.FunctionType}

    @return: a 2-tuple of a reference to L{_unpickleFunction} and a tuple of
        its arguments, a 1-tuple of the function's fully qualified name.
    @rtype: 2-tuple of C{callable, native string}
    z<lambda>zCannot pickle lambda function: Ú.)r	   Ú_UniversalPicklingErrorÚ_unpickleFunctionÚtupleÚjoinÚ
__module__Ú__qualname__)Úfr   r   r   Ú_pickleFunctionW   s    
r!   c                 C   s   ddl m} || ƒS )aù  
    Convert a function name into a function by importing it.

    This is a synonym for L{twisted.python.reflect.namedAny}, but imported
    locally to avoid circular imports, and also to provide a persistent name
    that can be stored (and deprecated) independently of C{namedAny}.

    @param fullyQualifiedName: The fully qualified name of a function.
    @type fullyQualifiedName: native C{str}

    @return: A function object imported from the given location.
    @rtype: L{types.FunctionType}
    r   )ÚnamedAny)Ztwisted.python.reflectr"   )ZfullyQualifiedNamer"   r   r   r   r   h   s    r   c                 C   s   t | jffS )z3support function for copy_reg to pickle module refs)ÚunpickleModuler	   )Úmoduler   r   r   ÚpickleModule~   s    r%   c                 C   s6   | t kr(t d|  ¡ t |  } t | ¡ t| i i dƒS )z5support function for copy_reg to unpickle module refszModule has moved: %sÚx)r   r   r   Ú
__import__)Únamer   r   r   r#   ƒ   s
    
r#   c                 C   s   t |  ¡ |  ¡ ffS )zá
    Reduce the given cStringO.

    This is only called on Python 2, because the cStringIO module only exists
    on Python 2.

    @param stringo: The string output to pickle.
    @type stringo: C{cStringIO.OutputType}
    )ÚunpickleStringOÚgetvalueÚtell)Zstringor   r   r   ÚpickleStringO�   s    r,   c                 C   s   t ƒ }| | ¡ | |¡ |S )aÚ  
    Convert the output of L{pickleStringO} into an appropriate type for the
    current python version.  This may be called on Python 3 and will convert a
    cStringIO into an L{io.StringIO}.

    @param val: The content of the file.
    @type val: L{bytes}

    @param sek: The seek position of the file.
    @type sek: L{int}

    @return: a file-like object which you can write bytes to.
    @rtype: C{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3.
    )Ú
_cStringIOÚwriteÚseek©ÚvalZsekr&   r   r   r   r)   �   s    

r)   c                 C   s   t |  ¡ |  ¡ ffS )aQ  
    Reduce the given cStringI.

    This is only called on Python 2, because the cStringIO module only exists
    on Python 2.

    @param stringi: The string input to pickle.
    @type stringi: C{cStringIO.InputType}

    @return: a 2-tuple of (C{unpickleStringI}, (bytes, pointer))
    @rtype: 2-tuple of (function, (bytes, int))
    )ÚunpickleStringIr*   r+   )Zstringir   r   r   ÚpickleStringI²   s    r3   c                 C   s   t | ƒ}| |¡ |S )aß  
    Convert the output of L{pickleStringI} into an appropriate type for the
    current Python version.

    This may be called on Python 3 and will convert a cStringIO into an
    L{io.StringIO}.

    @param val: The content of the file.
    @type val: L{bytes}

    @param sek: The seek position of the file.
    @type sek: L{int}

    @return: a file-like object which you can read bytes from.
    @rtype: C{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3.
    )r-   r/   r0   r   r   r   r2   Â   s    
r2   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú	Ephemeralzh
    This type of object is never persisted; if possible, even references to it
    are eliminated.
    c                 C   s   t dfS )zu
        Serialize any subclass of L{Ephemeral} in a way which replaces it with
        L{Ephemeral} itself.
        r   )r4   )Úselfr   r   r   Ú
__reduce__Þ   s    zEphemeral.__reduce__c                 C   sJ   t  d|  ¡ tsFdd l}t|dd ƒrF| | ¡D ]}t  d|› �¡ q0d S )Nz!WARNING: serializing ephemeral %sr   Úget_referrersz referred to by )r   r   r   Úgcr   r7   )r5   r8   Úrr   r   r   Ú__getstate__å   s    zEphemeral.__getstate__c                 C   s   t  d| j ¡ t| _d S )Nz#WARNING: unserializing ephemeral %s)r   r   r   r4   ©r5   Ústater   r   r   Ú__setstate__ï   s    zEphemeral.__setstate__N)r	   r   r   Ú__doc__r6   r:   r=   r   r   r   r   r4   Ø   s   
r4   Ú	VersionedÚversionedsToUpgradec                  C   s&   t t ¡ ƒD ]} t| ƒ qi ai ad S ©N)Úlistr@   ÚvaluesÚrequireUpgradeÚupgraded)Z	versionedr   r   r   Ú	doUpgradeø   s    
rF   c                 C   s0   t | ƒ}|tkr,|tkr,dt|< |  ¡  | S dS )z?Require that a Versioned instance be upgraded completely first.é   N)Úidr@   rE   ÚversionUpgrade)ÚobjZobjIDr   r   r   rD      s
    rD   c                 C   s@   | t g}t | ¡D ] }||krt|t ƒr| |¡ q|dd… S )z±
    Get all of the parent classes of C{c}, not including C{c} itself, which are
    strict subclasses of L{Versioned}.

    @param c: a class
    @returns: list of classes
    é   N)r?   ÚinspectÚgetmroÚ
issubclassÚappend)ÚcÚlÚbr   r   r   Ú_aybabtu	  s
    
rS   c                   @   s2   e Zd ZdZdZdZdd„ Zddd„Zd	d
„ ZdS )r?   a²  
    This type of object is persisted with versioning information.

    I have a single class attribute, the int persistenceVersion.  After I am
    unserialized (and styles.doUpgrade() is called), self.upgradeToVersionX()
    will be called for each version upgrade I must undergo.

    For example, if I serialize an instance of a Foo(Versioned) at version 4
    and then unserialize it when the code is at version 9, the calls::

      self.upgradeToVersion5()
      self.upgradeToVersion6()
      self.upgradeToVersion7()
      self.upgradeToVersion8()
      self.upgradeToVersion9()

    will be made.  If any of these methods are undefined, a warning message
    will be printed.
    r   r   c                 C   s   | t t| ƒ< || _d S rA   )r@   rH   Ú__dict__r;   r   r   r   r=   3  s    zVersioned.__setstate__Nc                 C   s€   t   |p| j¡}t| jƒ}| ¡  | | j¡ |D ]H}d|jkrZ|jD ]}||krF||= qFd|jkr2|j|t 	|¡› d�< q2|S )z8Get state, adding a version number to it on its way out.ÚpersistenceForgetsÚpersistenceVersionz.persistenceVersion)
ÚcopyrT   rS   r   ÚreverserO   rU   rV   r   Úqual)r5   ÚdictÚdctÚbasesÚbaseZslotr   r   r   r:   7  s    



þÿzVersioned.__getstate__c           
      C   sV  t | jƒ}| ¡  | | j¡ d| jkr„| jd }| jd= d}d}|D ]$}d|jkrVqF|j|krF|}|j}qF|r„|| jdt |¡ < |D ]È}t|j	kr¢d|jkr¢qˆ|j}dt |¡ }| j 
|¡pÄd}|rÒ| j|= ||ksâtdƒ‚||k rˆ|d }|j 
d| d¡}	|	�r<t dt |¡t | j¡t| ƒ|f ¡ |	| ƒ qât d	 ||¡¡ qâqˆdS )
z (internal) Do a version upgrade.rV   r   Nz%s.persistenceVersionz"Sorry, can't go backwards in time.rG   zupgradeToVersion%sz'Upgrading %s (of %s @ %s) to version %sz(Warning: cannot upgrade {} to version {})rS   r   rX   rO   rT   rV   r   rY   r?   Ú	__bases__Úgetr   r   r   rH   Úformat)
r5   r\   ZpverZhighestVersionZhighestBaser]   ZcurrentVersZpverNameZpersistVersr   r   r   r   rI   H  sf    

	


þÿÿþ
üÿÿ	
 ÿÿzVersioned.versionUpgrade)N)	r	   r   r   r>   rV   rU   r=   r:   rI   r   r   r   r   r?     s   
),r>   rW   ÚcopyregÚcopy_regrL   Úpickler   Úior   r-   Útypingr   Ztwisted.pythonr   r   Ztwisted.python.compatr   r   ÚstrÚ
ModuleTypeÚ__annotations__ÚPicklingErrorr   r   r   r   r   r!   r   ÚFunctionTyper%   r#   r,   r)   r3   r2   r4   r@   ÚintrE   rF   rD   rS   r?   r   r   r   r   Ú<module>   s@    		