B t1`8@sBdZddlmZmZmZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZddl mZddlmZmZmZmZmZmZmZerddlZeeefZeeehZejeedheeBZ e j!e j"e j#e j$e j%hZ&e'e(ee)eee*e+he Be&BZ,dd Z-d d Z.d d Z/ddZ0ddZ1ddZ2ddZ3ddZ4ddZ5ddZ6ddZ7ddZ8d d!Z9d"d#Z:d$d%Z;d&d'Zd,d-Z?d.d/Z@d0d1ZAd2d3ZBd4d5ZCd6d7ZDd8d9ZEdUd;d<ZFdVd=d>ZGd?d@ZHdAdBZIdCdDZJdEdFZKdGdHZLdIdJZMdKdLZNdMdNZOdOdPZPeQdfdQdRZQdSdTZRdS)WzkHelper functions for pickling and unpickling. Most functions assist in determining the type of an object. )absolute_importdivisionunicode_literalsN)tags)compat) abc_iterator class_typesiterator_types numeric_typesPY2PY3PY3_ORDERED_DICTcCs t|tS)zReturns True is obj is a reference to a type. >>> is_type(1) False >>> is_type(object) True >>> class Klass: pass >>> is_type(Klass) True ) isinstancer )objr3/tmp/pip-unpacked-wheel-wy5q9vwy/jsonpickle/util.pyis_type>srcCst||sdSt||}t|tjr(dSt|tjtjfs>dSt|rJ|n|j}d}x*t |D]}t | |}|dk r`Pq`W|dkrdSt|t rdStrdnd}t||sdSt||}t|trt||St|t|S)NFT__self__Zim_self)hasattrgetattrrtypesBuiltinMethodType MethodType FunctionTyper __class__inspectgetmrovarsget staticmethodr classmethod issubclasstype)rnamefuncZ base_typeoriginalsubtypeZ self_attrZbound_torrr has_methodOs0         r(cCs t|tot|ttjtjf S)zReturns True is obj is a reference to an object instance. >>> is_object(1) True >>> is_object(object()) True >>> is_object(lambda x: 1) False )robjectr#rrBuiltinFunctionType)rrrr is_objects r+cCs t|tkS)aHelper method to see if the object is a basic data type. Unicode strings, integers, longs, floats, booleans, and None are considered primitive and will return True when passed into *is_primitive()* >>> is_primitive(3) True >>> is_primitive([4,4]) False )r# PRIMITIVES)rrrr is_primitives r-cCsdtjkot|tjdjS)zIs the object an enum?enum)sysmodulesrEnum)rrrris_enumsr2cCs t|tkS)zoHelper method for testing if the object is a dictionary. >>> is_dictionary({'key':'value'}) True )r#dict)rrrr is_dictionarysr4cCs t|tkS)zpHelper method to see if the object is a sequence (list, set, or tuple). >>> is_sequence([4]) True )r# SEQUENCES_SET)rrrr is_sequencesr6cCs t|tkS)zXHelper method to see if the object is a Python list. >>> is_list([4]) True )r#list)rrrris_listsr8cCs t|tkS)zXHelper method to see if the object is a Python set. >>> is_set(set()) True )r#set)rrrris_setsr:cCs t|tkS)z[Helper method to see if the object is a bytestring. >>> is_bytes(b'foo') True )r#bytes)rrrris_bytessr<cCst|tjkS)z6Helper method to see if the object is a unicode string)r#rustr)rrrr is_unicodesr>cCs t|tkS)z[Helper method to see if the object is a Python tuple. >>> is_tuple((1,)) True )r#tuple)rrrris_tuplesr@cCs"t|do t|jto t|tk S)zReturns True if *obj* is a subclass of the dict type. *obj* must be a subclass and not the actual builtin dict. >>> class Temp(dict): pass >>> is_dictionary_subclass(Temp()) True r)rr"rr3r#)rrrris_dictionary_subclasss  rAcCs(t|do&t|jtst|o&t| S)zReturns True if *obj* is a subclass of list, set or tuple. *obj* must be a subclass and not the actual builtin, such as list, set, tuple, etc.. >>> class Temp(list): pass >>> is_sequence_subclass(Temp()) True r)rr"r SEQUENCES is_list_liker6)rrrris_sequence_subclasss rDcCst|tjkrdSdS)zReturns True if *obj* is a special (weird) class, that is more complex than primitive data types, but is not a full object. Including: * :class:`~time.struct_time` TF)r#time struct_time)rrrr is_noncomplexsrGcCs t|tkS)zReturns true if passed a function >>> is_function(lambda x: 1) True >>> is_function(locals) True >>> def method(): pass >>> is_function(method) True >>> is_function(1) False )r#FUNCTION_TYPES)rrrr is_function srIcCs:t|do8t|tjtjfo8t|do8t|do8|jdkS)zReturn True if `obj` is a module-global function >>> import os >>> is_module_function(os.path.exists) True >>> is_module_function(lambda: None) False r __module____name__z)rrrrr*rK)rrrris_module_functions   rLcCs t|tjS)zWReturns True if passed a module >>> import os >>> is_module(os) True )rr ModuleType)rrrr is_module3srNcCs |tjkrdSt|pt| S)zReturn True if an object can be pickled >>> import os >>> is_picklable('os', os) True >>> def foo(): pass >>> is_picklable('foo', foo) True >>> is_picklable('foo', lambda: None) False F)rZRESERVEDrLrI)r$valuerrr is_picklable>s rPcCs&y t|dStk r dSXdS)zTests to see if ``module`` is available on the sys.path >>> is_installed('sys') True >>> is_installed('hopefullythisisnotarealmodule') False TFN) __import__ ImportError)modulerrr is_installedRs rTcCst|dot|dS)N __getitem__append)r)rrrrrCbsrCcCs.tot|tj}t|to,t|tj o,| S)N)r r __builtin__filerioIOBase)ris_filerrr is_iteratorfsr\cCs(yt|jdkStk r"dSXdS)N collectionsF)r#rJ Exception)rrrris_collectionsmsr_cCst|dot|jtS)Nr)rr"rrB)rrrris_reducible_sequence_subclasstsr`cCst|rt|tjsdSt|tkr(dS|tkr4dSt|r@dSt|tj rPdSt |r\dSt |rhdStt |ddt r~dSt|r|jdkrdSdS)zu Returns false if of a type which have special casing, and should not have their __reduce__ methods used TF __slots__Ndatetime)r_rr] defaultdictr#NON_REDUCIBLE_TYPESr)rCrrMrAr`rr rrJ)rrrr is_reduciblexs&  reFcCst|ddr||jkS|S)zt Returns true if key exists in obj.__dict__; false if not in. If obj.__dict__ is absent, return default __dict__N)rrf)rkeydefaultrrrin_dictsricCst|ddr||jkS|S)zv Returns true if key exists in obj.__slots__; false if not in. If obj.__slots__ is absent, return default raN)rra)rrgrhrrrin_slotssrjc Cst|rt|rdSt|r dSd}d}d}d}t||pBt||}t||pVt||}xFt|jD]8}t|r|p|t||}|pt||}|rd|rd||fSqdWt|}tt|}tt|}|st||d} | |k r| }|st||d} | |k r| }||fS)z Tests if __reduce__ or __reduce_ex__ exists in the object dict or in the class dicts of every class in the MRO *except object*. Returns a tuple of booleans (has_reduce, has_reduce_ex) )FF)FTF __reduce__ __reduce_ex__) rerrGrirjr#__mro__rr)) r has_reduceZ has_reduce_exREDUCEZ REDUCE_EXbaseclsZ object_reduceZobject_reduce_exZhas_reduce_clsZhas_reduce_ex_clsrrrrns8     rncCstddd}|||S)a Rename builtin modules to a consistent module name. Prefer the more modern naming. This is used so that references to Python's `builtins` module can be loaded in both Python 2 and 3. We remap to the "__builtin__" name and unmap it when importing. Map the Python2 `exceptions` module to `builtins` because `builtins` is a superset and contains everything that is available in `exceptions`, which makes the translation simpler. See untranslate_module_name() for the reverse operation. builtins)rW exceptions)r3r)rSlookuprrrtranslate_module_names rucCs&t|}trtddni}|||S)zRename module names mention in JSON to names that we can import This reverses the translation applied by translate_module_name() to a module name available to the current version of Python. rW)rr)_0_9_6_compat_untranslater r3r)rSrtrrruntranslate_module_namesrwcCstddd}|||S)zProvide compatibility for pickles created with jsonpickle 0.9.6 and earlier, remapping `exceptions` and `__builtin__` to `builtins`. rr)rWrs)r3r)rSrtrrrrvs rvcCs$t|d|j}t|j}d||S)a >>> class Example(object): ... pass >>> ex = Example() >>> importable_name(ex.__class__) == 'jsonpickle.util.Example' True >>> importable_name(type(25)) == 'builtins.int' True >>> importable_name(None.__class__) == 'builtins.NoneType' True >>> importable_name(False.__class__) == 'builtins.bool' True >>> importable_name(AttributeError) == 'builtins.AttributeError' True __qualname__z{}.{})rrKrurJformat)rqr$rSrrrimportable_names rzcCst|dS)zI Encode binary data to ascii text in base64. Data must be bytes. ascii)base64 b64encodedecode)datarrrr}sr}cCs t|S)z. Decode payload - must be ascii text. )r| b64decode)payloadrrrrsrcCstr tdt|dS)zI Encode binary data to ascii text in base85. Data must be bytes. z%Python 2 can't encode data in base85.r{)r NotImplementedErrorr| b85encoder~)rrrrr&srcCstr tdt|S)z. Decode payload - must be ascii text. z*Python 2 can't decode base85-encoded data.)r rr| b85decode)rrrrr/srcCst||S)N)rr=)rgetterrrr itemgetter8srccsRtr&xH|D]\}}||fVqWn(x&t|tdD]\}}||fVq8WdS)aIterate over dicts in a deterministic order Python2 does not guarantee dict ordering, so this function papers over the difference in behavior. Python3 does guarantee dict order, without use of OrderedDict, so no sorting is needed there. )rgN)ritemssortedr)rkvrrrr<s r)F)F)S__doc__ __future__rrrr|r]rYoperatorr/rErrrrrr r r r r rrWr7r9r?rBr5r=boolr#r,rr LambdaTyper*rrHintfloatr3r)r;rdrr(r+r-r2r4r6r8r:r<r>r@rArDrGrIrLrNrPrTrCr\r_r`rerirjrnrurwrvrzr}rrrrrrrrr sz  $   0           5