o
    ¯b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   ú:/usr/lib/python3/dist-packages/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)ÚclassObjectÚ
methodNameÚmethodObjectr   r   r   Ú_methodFunction"   s   
r   c              	   C   sŠ   |du r	t || ƒS zt|| ƒ}W n& ty6   t d| d|¡ |dus'J dƒ‚|j|u r-‚ t| ||jƒ Y S w d}tj||g|¢R Ž }|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}
    NÚMethodznot on classz'No recourse: no instance to guess from.r   )	r   r   ÚAttributeErrorr   Úmsgr   r   ÚtypesÚ
MethodType)Úim_nameÚim_selfÚim_classÚmethodFunctionÚ
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)Útwisted.python.reflectr)   )Ú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 v 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)Ústringor   r   r   ÚpickleStringO�   s   
r6   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©ÚvalÚsekr/   r   r   r   r2   �   s   

r2   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))
    )ÚunpickleStringIr3   r4   )Ústringir   r   r   ÚpickleStringI²   s   r?   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.
    )r7   r9   r:   r   r   r   r=   Â   s   
r=   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   )r@   )Úselfr   r   r   Ú
__reduce__Þ   s   zEphemeral.__reduce__c                 C   sJ   t  d|  ¡ ts#dd l}t|dd ƒr#| | ¡D ]
}t  d|› �¡ qd S )Nz!WARNING: serializing ephemeral %sr   Úget_referrersz referred to by )r   r   r   Úgcr   rC   )rA   rD   Ú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   r@   ©rA   Ústater   r   r   Ú__setstate__ï   s   
zEphemeral.__setstate__N)r	   r%   r&   Ú__doc__rB   rF   rI   r   r   r   r   r@   Ø   s
    
r@   Ú	VersionedÚversionedsToUpgradec                  C   s&   t t ¡ ƒD ]} t| ƒ qi ai ad S ©N)ÚlistrL   ÚvaluesÚrequireUpgradeÚupgraded)Ú	versionedr   r   r   Ú	doUpgradeø   s   
rS   c                 C   s4   t | ƒ}|tv r|tvrdt|< |  ¡  | S dS dS )z?Require that a Versioned instance be upgraded completely first.é   N)ÚidrL   rQ   ÚversionUpgrade)ÚobjÚobjIDr   r   r   rP      s   ýrP   c                 C   s@   | t g}t | ¡D ]}||v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)rK   ÚinspectÚgetmroÚ
issubclassÚappend)ÚcÚlÚbr   r   r   Ú_aybabtu	  s   

€ra   c                   @   s2   e Zd ZdZdZdZdd„ Zddd„Zd	d
„ ZdS )rK   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 rM   )rL   rU   Ú__dict__rG   r   r   r   rI   3  s   
zVersioned.__setstate__Nc                 C   s€   t   |p| j¡}t| jƒ}| ¡  | | j¡ |D ]$}d|jv r-|jD ]	}||v r,||= q#d|jv r=|j|t 	|¡› d�< q|S )z8Get state, adding a version number to it on its way out.ÚpersistenceForgetsÚpersistenceVersionz.persistenceVersion)
Úcopyrb   ra   r   Úreverser]   rc   rd   r   Úqual)rA   ÚdictÚdctÚbasesÚbaseÚslotr   r   r   rF   7  s"   


€
þÿ€zVersioned.__getstate__c           
      C   sZ  t | jƒ}| ¡  | | j¡ d| jv rB| jd }| jd= d}d}|D ]}d|jvr+q#|j|kr5|}|j}q#|rB|| jdt |¡ < |D ]f}t|j	vrQd|jvrQqD|j}dt |¡ }| j 
|¡pbd}|ri| j|= ||ksqJ dƒ‚||k rª|d }|j 
d| d¡}	|	r�t dt |¡t | j¡t| ƒ|f ¡ |	| ƒ n	t d	 ||¡¡ ||k suqDdS )
z (internal) Do a version upgrade.rd   r   Nz%s.persistenceVersionz"Sorry, can't go backwards in time.rT   zupgradeToVersion%sz'Upgrading %s (of %s @ %s) to version %sz(Warning: cannot upgrade {} to version {})ra   r   rf   r]   rb   rd   r   rg   rK   Ú	__bases__Úgetr   r   rU   Úformat)
rA   rj   ÚpverÚhighestVersionÚhighestBaserk   ÚcurrentVersÚpverNameÚpersistVersr   r   r   r   rV   H  sh   





€þÿ


üÿÿ
	ÿÿñ€ózVersioned.versionUpgraderM   )	r	   r%   r&   rJ   rd   rc   rI   rF   rV   r   r   r   r   rK     s    
),rJ   re   ÚcopyregÚcopy_regrZ   Úpickler   Úior   r7   Útypingr   Útwisted.pythonr   r   Útwisted.python.compatr   r   ÚstrÚ
ModuleTypeÚ__annotations__ÚPicklingErrorr!   r   r   r   r   r(   r"   ÚFunctionTyper.   r,   r6   r2   r?   r=   r@   rL   ÚintrQ   rS   rP   ra   rK   r   r   r   r   Ú<module>   sB    		