U  W[4@sdZddlmZmZddlZddlZz ddlZWnek rLddlZYnXddl Z ddl Z ddl m Z m Z ddlmZddlmZiZz ddlZWnek rdZYnXedksejejkrejZnGdddejejZd d Zd d Zd dZeejeeddZddZeejeeddZddZeejeeddZddZ ddZ!ddZ"zddl#m$Z$m%Z%m&Z'Wn"ek rdd l(m&Z'YnXee%ee ee$e!e"Gd!d"d"Z)ia*ia+d#d$Z,d%d&Z-d'd(Z.Gd)d*d*Z/dS)+z( Different styles of persisted objects. )divisionabsolute_importN)_PY3_PYPY)log)reflectc@seZdZdZdS)_UniversalPicklingErrorzz A PicklingError catchable by both L{cPickle.PicklingError} and L{pickle.PicklingError} handlers. N)__name__ __module__ __qualname____doc__r r :/usr/lib/python3/dist-packages/twisted/persisted/styles.pyr&srcCs4trt|j|j|jjffSt|jj|j|jffSdS)z3support function for copy_reg to pickle method refsN)runpickleMethodr __self__ __class__im_funcim_selfim_class)methodr r r pickleMethod1srcCst||}tr|S|jS)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} or L{types.ClassType} @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} )getattrrr)Z classObjectZ methodNameZ methodObjectr r r_methodFunction>s rc Cs|dkrt||Szt||}WnNtk rntd|d||dk sPtd|j|kr\t|||jYSXtrzd}n t |g}t j ||f|}|SdS)ai 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{types.ClassType} or L{type} or L{None} NZMethodz not on classz'No recourse: no instance to guess from.r ) rrAttributeErrorrmsgAssertionErrorrrrtupletypes MethodType)Zim_namerrZmethodFunctionZ maybeClassZboundr r rrSs    rcCs4|jdkrtd|ttd|j|jggfS)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} zz!Cannot pickle lambda function: {}.)r rformat_unpickleFunctionrjoinr r )fr r r_pickleFunctionys r$cCsddlm}||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)Ztwisted.python.reflectr%)ZfullyQualifiedNamer%r r rr!s r!cCs t|jffS)z3support function for copy_reg to pickle module refs)unpickleModuler )moduler r r pickleModulesr(cCs6|tkr(td|t|}t|t|iidS)z5support function for copy_reg to unpickle module refszModule has moved: %sx) oldModulesrr __import__)namer r rr&s  r&cCst||ffS)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: L{cStringIO.OutputType} )unpickleStringOgetvaluetell)Zstringor r r pickleStringOs r0cCst}|||||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: L{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3. ) _cStringIOwriteseekvalZsekr)r r rr-s  r-cCst||ffS)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: L{cStringIO.InputType} @return: a 2-tuple of (C{unpickleStringI}, (bytes, pointer)) @rtype: 2-tuple of (function, (bytes, int)) )unpickleStringIr.r/)Zstringir r r pickleStringIs r7cCst|}|||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: L{cStringIO.OutputType} on Python 2, L{io.StringIO} on Python 3. )r1r3r4r r rr6s r6) InputType OutputTypeStringIO)r:c@s(eZdZdZddZddZddZdS) Ephemeralzh This type of object is never persisted; if possible, even references to it are eliminated. cCstdfS)zu Serialize any subclass of L{Ephemeral} in a way which replaces it with L{Ephemeral} itself. r )r;)selfr r r __reduce__szEphemeral.__reduce__cCsJtd|tsFddl}t|ddrF||D]}td|fq0dS)Nz!WARNING: serializing ephemeral %sr get_referrersz referred to by %s)rrrgcrr>)r<r?rr r r __getstate__s zEphemeral.__getstate__cCstd|jt|_dS)Nz#WARNING: unserializing ephemeral %s)rrrr;r<stater r r __setstate__"szEphemeral.__setstate__N)r r r r r=rArDr r r rr; s r;cCs&ttD] }t|q iaiadSN)listversionedsToUpgradevaluesrequireUpgradeupgraded)Z versionedr r r doUpgrade*s rKcCs0t|}|tkr,|tkr,dt|<||SdS)zDRequire that a Versioned instance be upgraded completely first. N)idrGrJversionUpgrade)objZobjIDr r rrI1s rIcCs@|tg}t|D] }||krt|tr||q|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) VersionedinspectZgetmro issubclassappend)clbr r r_aybabtu:s  rXc@s2eZdZdZdZdZddZd ddZd d ZdS) rQa 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. rr cCs|tt|<||_dSrE)rGrM__dict__rBr r rrDbs zVersioned.__setstate__NcCs~t|p |j}t|j}|||j|D]F}d|jkrZ|jD]}||krF||=qFd|jkr2|j|dt |<q2|S)zAGet state, adding a version number to it on its way out. persistenceForgetspersistenceVersion%s.persistenceVersion) copyrYrXrreverserTrZr[rqual)r<dictZdctbasesbaseZslotr r rrAfs     zVersioned.__getstate__c CsVt|j}|||jd|jkr|jd}|jd=d}d}|D]$}d|jkrVqF|j|krF|}|j}qF|r||jdt|<|D]}t|j krd|jkrq|j}dt|}|j |pd}|r|j|=||kst d||kr|d}|j d|d} | rsh       $