Ϫf1UdZddlZddlZddlZddlZddlZddlmZ ddl m Z ddl m Z mZddlmZiZe eej&fed<ej*ZdZd Zd Zej ej4ed Zd Zej ej:ed ZdZej ej&edZ dZ!dZ"dZ#GddZ$ia%e e&dfed<ia'dZ(dZ)dZ*GddZ+y)z( Different styles of persisted objects. N)StringIO)Dict)logreflect)_PYPY oldModulescht|j|j|jjffS)z3support function for copy_reg to pickle method refs)unpickleMethod__name____self__ __class__)methods :/usr/lib/python3/dist-packages/twisted/persisted/styles.py pickleMethodrs-  &//6??+D+DE ct||}|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 methodObjects r_methodFunctionr"s; 3L rc| t||S t||}d}tj||g|}|S#t$rJt j d|d||Jd|j|urt|||jcYSwxYw)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} Methodz not on classz'No recourse: no instance to guess from.) rrtypes MethodTypeAttributeErrorrmsgr r )im_nameim_selfim_classmethodFunction maybeClassbounds rr r 4sx))(7;   F:F  C '>8<"M$MM"    ( gw0A0ABBCs 7AB  B c|jdk(rtd|ttdj |j |j ggfS)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} zzCannot pickle lambda function: .)r _UniversalPicklingError_unpickleFunctiontuplejoin __module__ __qualname__)fs r_pickleFunctionr.WsL zzZ%(Gs&KLL uchh ann/M&N%OP QQrcddlm}||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.reflectr0)fullyQualifiedNamer0s rr(r(hs0 & ''rc(t|jffS)z3support function for copy_reg to pickle module refs)unpickleModuler )modules r pickleModuler6~s FOO- --rc|tvr6tjd|zt|}tj|t|iidS)z5support function for copy_reg to unpickle module refszModule has moved: %sx)rrr __import__)names rr4r4sB z &-.$  dBC ((rcP t|j|jffS)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} )unpickleStringOgetvaluetell)stringos r pickleStringOr@s'C W--/@ @@rc^t}|j||j||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. ) _cStringIOwriteseekvalsekr8s rr<r<s&  AGGCLFF3K HrcNt|j|jffS)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>)stringis r pickleStringIrKs# W--/@ @@rc>t|}|j||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. )rBrDrEs rrIrIs" 3AFF3K Hrc"eZdZdZdZdZdZy) Ephemeralzh This type of object is never persisted; if possible, even references to it are eliminated. ctdfS)zu Serialize any subclass of L{Ephemeral} in a way which replaces it with L{Ephemeral} itself. r)rN)selfs r __reduce__zEphemeral.__reduce__s 2rctjd|zts?ddl}t |ddr.|j |D]}tjd|y)Nz!WARNING: serializing ephemeral %sr get_referrersz referred to by )rrrgcrrS)rPrTrs r __getstate__zEphemeral.__getstate__sW 3d:; r?D1))$/4AGG.qc234rc^tjd|jzt|_y)Nz#WARNING: unserializing ephemeral %s)rrr rNrPstates r __setstate__zEphemeral.__setstate__s 5FG"rN)r r+r,__doc__rQrVrZrrrrNrNs #rrN VersionedversionedsToUpgradecfttjD] }t|iaiayN)listr]valuesrequireUpgradeupgraded) versioneds r doUpgraderes3-4467" y!"Hrcrt|}|tvr$|tvrdt|<|j|Syy)z?Require that a Versioned instance be upgraded completely first.N)idr]rcversionUpgrade)objobjIDs rrbrbs= sGE ##X(=  )>#rc|tg}tj|D])}||vst|ts|j |+|ddS)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\inspectgetmro issubclassappend)clbs r_aybabturu sM IA ^^A  A:*Q 2 HHQK QR5Lrc,eZdZdZdZdZdZddZdZy) 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. rrc6|tt|<||_yr_)r]rh__dict__rXs rrZzVersioned.__setstate__3s(,BtH% rNctj|xs |j}t|j}|j |j |j|D]]}d|jvr|j D] }||vs||= d|jvs9|j|tj|d<_|S)z8Get state, adding a version number to it on its way out.persistenceForgetspersistenceVersionz.persistenceVersion) copyrxrur reverserqrzr{rqual)rPdictdctbasesbaseslots rrVzVersioned.__getstate__7sii- .(  T^^$ ,D#t}}4 33&Ds{I&$t}}4++||D)**=>  , rc t|j}|j|j|jd|jvr{|jd}|jd=d}d}|D]/}d|jvr|j |kDs"|}|j }1|r%||jdt j|z<|D]<}t|jvrd|jvr%|j }dt j|z}|jj|xsd}|r |j|=||ksJd||ks|dz}|jjd|zd} | rctjdt j|d t j|jd t|d || |n%tjd j||||kr?y) z (internal) Do a version upgrade.r{rNz%s.persistenceVersionz"Sorry, can't go backwards in time.rgzupgradeToVersion%sz Upgrading z (of z @ z ) to version z(Warning: cannot upgrade {} to version {})rur r}rqrxr{rr~r\ __bases__getrrrhformat) rPrpverhighestVersion highestBaser currentVerspverName persistVersrs rrizVersioned.versionUpgradeHs(   T^^$ 4== 0==!56D 23NK ='t}}<**^;"&K%)%<%rs   %''*, De&&& ',!..$@  ,/R"(&""O4. )  ,/ A * A  ,##8/1T#{*+0 $llr