How to use test_pickle method in avocado

Best Python code snippet using avocado_python

explain_pickle.py

Source:explain_pickle.py Github

copy

Full Screen

...529 def APPEND(self):530 r"""531 TESTS::532 sage: from sage.misc.explain_pickle import *533 sage: test_pickle(['a'])534 0: \x80 PROTO 2535 2: ] EMPTY_LIST536 3: q BINPUT 1537 5: U SHORT_BINSTRING 'a'538 8: a APPEND539 9: . STOP540 highest protocol among opcodes = 2541 explain_pickle in_current_sage=True/False:542 ['a']543 result: ['a']544 As shown above, we prefer to create a list literal. This is not545 possible if the list is recursive::546 sage: v = []547 sage: v.append(v)548 sage: test_pickle(v)549 0: \x80 PROTO 2550 2: ] EMPTY_LIST551 3: q BINPUT 1552 5: h BINGET 1553 7: a APPEND554 8: . STOP555 highest protocol among opcodes = 2556 explain_pickle in_current_sage=True/False:557 si = []558 list.append(si, si)559 si560 result: [[...]]561 """562 obj = self.pop()563 lst = self.pop()564 self._APPENDS_helper(lst, [obj])565 def APPENDS(self):566 r"""567 TESTS::568 sage: from sage.misc.explain_pickle import *569 sage: test_pickle(['a', 'b'])570 0: \x80 PROTO 2571 2: ] EMPTY_LIST572 3: q BINPUT 1573 5: ( MARK574 6: U SHORT_BINSTRING 'a'575 9: U SHORT_BINSTRING 'b'576 12: e APPENDS (MARK at 5)577 13: . STOP578 highest protocol among opcodes = 2579 explain_pickle in_current_sage=True/False:580 ['a', 'b']581 result: ['a', 'b']582 As shown above, we prefer to create a list literal. This is not583 possible if the list is recursive::584 sage: v = []585 sage: v.append(v)586 sage: v.append(v)587 sage: test_pickle(v)588 0: \x80 PROTO 2589 2: ] EMPTY_LIST590 3: q BINPUT 1591 5: ( MARK592 6: h BINGET 1593 8: h BINGET 1594 10: e APPENDS (MARK at 5)595 11: . STOP596 highest protocol among opcodes = 2597 explain_pickle in_current_sage=True/False:598 si = []599 list.extend(si, [si, si])600 si601 result: [[...], [...]]602 """603 slice = self.pop_to_mark()604 lst = self.pop()605 self._APPENDS_helper(lst, slice)606 def _APPENDS_helper(self, lst, slice):607 r"""608 TESTS::609 See the doctests for APPEND and APPENDS for some simple indirect610 tests of this method. Here we test some subtle behavior.611 For subtypes of list, we use list.append/list.extend instead of612 the append method of the object (TestAppendList.append raises613 an exception, so we can tell that cPickle doesn't call it either)::614 sage: from sage.misc.explain_pickle import *615 sage: test_pickle(TestAppendList((True,))) # indirect doctest616 0: \x80 PROTO 2617 2: c GLOBAL 'sage.misc.explain_pickle TestAppendList'618 43: q BINPUT 1619 45: ) EMPTY_TUPLE620 46: \x81 NEWOBJ621 47: q BINPUT 2622 49: \x88 NEWTRUE623 50: a APPEND624 51: } EMPTY_DICT625 52: q BINPUT 3626 54: b BUILD627 55: . STOP628 highest protocol among opcodes = 2629 explain_pickle in_current_sage=True:630 from sage.misc.explain_pickle import TestAppendList631 si = unpickle_newobj(TestAppendList, ())632 list.append(si, True)633 si634 explain_pickle in_current_sage=False:635 pg_TestAppendList = unpickle_global('sage.misc.explain_pickle', 'TestAppendList')636 si = unpickle_newobj(pg_TestAppendList, ())637 unpickle_appends(si, [True])638 unpickle_build(si, {})639 si640 result: [True]641 642 For values which are not subtypes of list, we use their own append643 method::644 sage: v = TestAppendNonlist()645 sage: v.list = [False, None]646 sage: test_pickle(v, verbose_eval=True)647 0: \x80 PROTO 2648 2: c GLOBAL 'sage.misc.explain_pickle TestAppendNonlist'649 46: q BINPUT 1650 48: ) EMPTY_TUPLE651 49: R REDUCE652 50: q BINPUT 2653 52: ( MARK654 53: \x89 NEWFALSE655 54: N NONE656 55: e APPENDS (MARK at 52)657 56: . STOP658 highest protocol among opcodes = 2659 explain_pickle in_current_sage=True:660 from sage.misc.explain_pickle import TestAppendNonlist661 si = TestAppendNonlist()662 si.append(False)663 si.append(None)664 si665 explain_pickle in_current_sage=False:666 pg_TestAppendNonlist = unpickle_global('sage.misc.explain_pickle', 'TestAppendNonlist')667 pg = unpickle_instantiate(pg_TestAppendNonlist, ())668 unpickle_appends(pg, [False, None])669 pg670 evaluating explain_pickle in_current_sage=True:671 Fetching append attribute672 Fetching append attribute673 evaluating explain_pickle in_current_sage=False:674 Fetching append attribute675 loading pickle with cPickle:676 Fetching append attribute677 result: [False, None]678 We see above that the in_current_sage=True code doesn't quite match679 the other cases, because it fetches the append attribute twice680 instead of once. If we set pedantic=True, then this is fixed.681 (We show only the changed parts of the output)::682 sage: test_pickle(v, verbose_eval=True, pedantic=True)683 0: \x80 PROTO 2684 ...685 explain_pickle in_current_sage=True:686 from sage.misc.explain_pickle import TestAppendNonlist687 si1 = TestAppendNonlist()688 si2 = si1.append689 si2(False)690 si2(None)691 si1692 ...693 evaluating explain_pickle in_current_sage=True:694 Fetching append attribute695 ...696 """697 # This has the side-effect of marking lst as immutable, if698 # slice happens to include lst.699 slice_exp = self.sib(slice)700 if self.is_mutable_pickle_object(lst) and isinstance(lst.value, list):701 lst.value.extend(slice)702 lst.expression = self.sib(lst.value)703 elif isinstance(lst, PickleObject) or self.default_assumptions:704 if isinstance(lst.value, list) or \705 (isinstance(lst.value, PickleInstance) and706 issubclass(lst.value.klass, list)) or \707 self.default_assumptions:708 if len(slice) > 1:709 self.sib.command(lst, self.sib.name('list').extend(lst, slice))710 else:711 for s in slice:712 self.sib.command(lst, self.sib.name('list').append(lst, self.sib(s)))713 else:714 if self.pedantic:715 app = self.sib(lst).append716 for s in slice:717 self.sib.command(lst, app(self.sib(s)))718 else:719 for s in slice:720 self.sib.command(lst, self.sib(lst).append(self.sib(s)))721 else:722 self.sib.command(lst, self.sib.name('unpickle_appends')(self.sib(lst), slice_exp))723 self.push(lst)724 725 def BINFLOAT(self, f):726 r"""727 TESTS::728 sage: from sage.misc.explain_pickle import *729 sage: test_pickle(float(pi))730 0: \x80 PROTO 2731 2: G BINFLOAT 3.141592653589793732 11: . STOP733 highest protocol among opcodes = 2734 explain_pickle in_current_sage=True/False:735 float(RR(3.1415926535897931))736 result: 3.141592653589793737 """738 self.push(self.sib(f))739 def BINGET(self, n):740 r"""741 TESTS::742 sage: from pickle import *743 sage: from sage.misc.explain_pickle import *744 sage: test_pickle(EMPTY_LIST + BINPUT + 'x' + POP + BINGET + 'x' + '.')745 0: ] EMPTY_LIST746 1: q BINPUT 120747 3: 0 POP748 4: h BINGET 120749 6: . STOP750 highest protocol among opcodes = 1751 explain_pickle in_current_sage=True/False:752 []753 result: [] 754 """755 self.push(self.memo[n])756 def BININT(self, n):757 r"""758 TESTS::759 sage: from sage.misc.explain_pickle import *760 sage: test_pickle(dumps(100000r, compress=False))761 0: \x80 PROTO 2762 2: J BININT 100000763 7: . STOP764 highest protocol among opcodes = 2765 explain_pickle in_current_sage=True/False:766 100000767 result: 100000768 """769 self.push_and_share(self.sib(n))770 def BININT1(self, n):771 r"""772 TESTS::773 sage: from sage.misc.explain_pickle import *774 sage: test_pickle(dumps(100r, compress=False))775 0: \x80 PROTO 2776 2: K BININT1 100777 4: . STOP778 highest protocol among opcodes = 2779 explain_pickle in_current_sage=True/False:780 100781 result: 100782 """783 self.push_and_share(self.sib(n))784 def BININT2(self, n):785 r"""786 TESTS::787 sage: from sage.misc.explain_pickle import *788 sage: test_pickle(dumps(1000r, compress=False))789 0: \x80 PROTO 2790 2: M BININT2 1000791 5: . STOP792 highest protocol among opcodes = 2793 explain_pickle in_current_sage=True/False:794 1000795 result: 1000796 """797 self.push_and_share(self.sib(n))798 def BINPUT(self, n):799 r"""800 TESTS::801 sage: from pickle import *802 sage: from sage.misc.explain_pickle import *803 sage: test_pickle(EMPTY_LIST + BINPUT + 'x' + POP + BINGET + 'x')804 0: ] EMPTY_LIST805 1: q BINPUT 120806 3: 0 POP807 4: h BINGET 120808 6: . STOP809 highest protocol among opcodes = 1810 explain_pickle in_current_sage=True/False:811 []812 result: [] 813 """814 v = self.pop()815 self.memo[n] = v816 self.push(v)817 def BINSTRING(self, s):818 r"""819 TESTS::820 sage: from sage.misc.explain_pickle import *821 sage: test_pickle('T\5\0\0\0hello.')822 0: T BINSTRING 'hello'823 10: . STOP824 highest protocol among opcodes = 1825 explain_pickle in_current_sage=True/False:826 'hello'827 result: 'hello'828 """829 self.push(PickleObject(s, self.share(self.sib(s))))830 def BINUNICODE(self, s):831 r"""832 TESTS::833 sage: from sage.misc.explain_pickle import *834 sage: test_pickle(u'hi\u1234\U00012345')835 0: \x80 PROTO 2836 2: X BINUNICODE u'hi\u1234\U00012345'837 16: q BINPUT 1838 18: . STOP839 highest protocol among opcodes = 2840 explain_pickle in_current_sage=True/False:841 u'hi\u1234\U00012345'842 result: u'hi\u1234\U00012345'843 """844 self.push_and_share(self.sib(s))845 def BUILD(self):846 r"""847 TESTS::848 sage: from sage.misc.explain_pickle import *849 sage: test_pickle(TestBuild())850 0: \x80 PROTO 2851 2: c GLOBAL 'sage.misc.explain_pickle TestBuild'852 38: q BINPUT 1853 40: ) EMPTY_TUPLE854 41: \x81 NEWOBJ855 42: q BINPUT 2856 44: } EMPTY_DICT857 45: q BINPUT 3858 47: U SHORT_BINSTRING 'x'859 50: K BININT1 3860 52: s SETITEM861 53: } EMPTY_DICT862 54: q BINPUT 4863 56: U SHORT_BINSTRING 'y'864 59: K BININT1 4865 61: s SETITEM866 62: \x86 TUPLE2867 63: b BUILD868 64: . STOP869 highest protocol among opcodes = 2870 explain_pickle in_current_sage=True:871 from sage.misc.explain_pickle import TestBuild872 si = unpickle_newobj(TestBuild, ())873 si.__dict__['x'] = 3874 si.y = 4875 si876 explain_pickle in_current_sage=False:877 pg_TestBuild = unpickle_global('sage.misc.explain_pickle', 'TestBuild')878 si = unpickle_newobj(pg_TestBuild, ())879 unpickle_build(si, ({'x':3}, {'y':4}))880 si881 result: TestBuild: x=3; y=4882 ::883 sage: test_pickle(TestBuildSetstate(), verbose_eval=True)884 0: \x80 PROTO 2885 2: c GLOBAL 'sage.misc.explain_pickle TestBuildSetstate'886 46: q BINPUT 1887 48: ) EMPTY_TUPLE888 49: \x81 NEWOBJ889 50: q BINPUT 2890 52: } EMPTY_DICT891 53: q BINPUT 3892 55: U SHORT_BINSTRING 'x'893 58: K BININT1 3894 60: s SETITEM895 61: } EMPTY_DICT896 62: q BINPUT 4897 64: U SHORT_BINSTRING 'y'898 67: K BININT1 4899 69: s SETITEM900 70: \x86 TUPLE2901 71: b BUILD902 72: . STOP903 highest protocol among opcodes = 2904 explain_pickle in_current_sage=True:905 from sage.misc.explain_pickle import TestBuildSetstate906 si = unpickle_newobj(TestBuildSetstate, ())907 si.__setstate__(({'x':3}, {'y':4}))908 si909 explain_pickle in_current_sage=False:910 pg_TestBuildSetstate = unpickle_global('sage.misc.explain_pickle', 'TestBuildSetstate')911 si = unpickle_newobj(pg_TestBuildSetstate, ())912 unpickle_build(si, ({'x':3}, {'y':4}))913 si914 evaluating explain_pickle in_current_sage=True:915 setting state from ({'x': 3}, {'y': 4})916 evaluating explain_pickle in_current_sage=False:917 setting state from ({'x': 3}, {'y': 4})918 loading pickle with cPickle:919 setting state from ({'x': 3}, {'y': 4})920 result: TestBuild: x=4; y=3921 """922 args = self.pop()923 obj = self.pop()924 use_setstate = False925 direct_set = False926 if self.default_assumptions:927 direct_set = True928 elif self.in_current_sage:929 if isinstance(obj, PickleObject) and isinstance(obj.value, PickleInstance):930 if hasattr(obj.value.klass, '__setstate__'):931 use_setstate = True932 else:933 direct_set = True934 can_handle_direct_set = False935 if direct_set:936 if isinstance(args, PickleObject):937 if isinstance(args.value, PickleDict):938 can_handle_direct_set = True939 if isinstance(args.value, tuple) and isinstance(args.value[0], PickleObject) and isinstance(args.value[0].value, PickleDict) and isinstance(args.value[1], PickleObject) and isinstance(args.value[1].value, PickleDict):940 can_handle_direct_set = True941 if not can_handle_direct_set:942 direct_set = False943 if use_setstate:944 self.sib.command(obj, self.sib.getattr(obj, '__setstate__')(args))945 elif direct_set:946 state = args.value947 slots = None948 if isinstance(state, tuple):949 slots = state[1].value950 state = state[0].value951 d = self.sib.getattr(obj, '__dict__')952 for k,v in state.items:953 self.sib.command(obj, self.sib.assign(d[k], v))954 if slots is not None:955 for k,v in slots.items:956 if isinstance(k, PickleObject) and isinstance(k.value, str):957 self.sib.command(obj, self.sib.assign(self.sib.getattr(obj, k.value), v))958 else:959 self.sib.command(obj, self.sib.name('setattr')(obj, k, v))960 else:961 self.sib.command(obj, self.sib.name('unpickle_build')(obj, args))962 self.push(obj)963 def DICT(self):964 r"""965 TESTS::966 sage: from pickle import *967 sage: from sage.misc.explain_pickle import *968 sage: test_pickle(DICT, args=('mark', 'a', 1, 2, 'b'))969 0: ( MARK970 1: P PERSID '1'971 4: P PERSID '2'972 7: P PERSID '3'973 10: P PERSID '4'974 13: d DICT (MARK at 0)975 14: . STOP976 highest protocol among opcodes = 0977 explain_pickle in_current_sage=True/False:978 {unpickle_persistent('1'):unpickle_persistent('2'), unpickle_persistent('3'):unpickle_persistent('4')}979 result: {'a': 1, 2: 'b'}980 """981 slice = self.pop_to_mark()982 self.EMPTY_DICT()983 self._SETITEMS_helper(slice)984 def DUP(self):985 r"""986 TESTS::987 sage: from pickle import *988 sage: from sage.misc.explain_pickle import *989 sage: test_pickle(EMPTY_LIST + DUP + TUPLE2 + STOP)990 0: ] EMPTY_LIST991 1: 2 DUP992 2: \x86 TUPLE2993 3: . STOP994 highest protocol among opcodes = 2995 explain_pickle in_current_sage=True/False:996 si = []997 (si, si)998 result: ([], [])999 """1000 v = self.pop()1001 self.push(v)1002 self.push(v)1003 def EMPTY_DICT(self):1004 r"""1005 TESTS::1006 sage: from pickle import *1007 sage: from sage.misc.explain_pickle import *1008 sage: test_pickle(EMPTY_DICT)1009 0: } EMPTY_DICT1010 1: . STOP1011 highest protocol among opcodes = 11012 explain_pickle in_current_sage=True/False:1013 {}1014 result: {}1015 """1016 self.push(PickleObject(PickleDict([]), self.sib({})))1017 def EMPTY_LIST(self):1018 r"""1019 TESTS::1020 sage: from pickle import *1021 sage: from sage.misc.explain_pickle import *1022 sage: test_pickle(EMPTY_LIST)1023 0: ] EMPTY_LIST1024 1: . STOP1025 highest protocol among opcodes = 11026 explain_pickle in_current_sage=True/False:1027 []1028 result: []1029 """1030 self.push(PickleObject([], self.sib([])))1031 def EMPTY_TUPLE(self):1032 r"""1033 TESTS::1034 sage: from pickle import *1035 sage: from sage.misc.explain_pickle import *1036 sage: test_pickle(EMPTY_TUPLE)1037 0: ) EMPTY_TUPLE1038 1: . STOP1039 highest protocol among opcodes = 11040 explain_pickle in_current_sage=True/False:1041 ()1042 result: ()1043 """1044 self.push(PickleObject((), self.sib(())))1045 def EXT1(self, n):1046 r"""1047 TESTS::1048 sage: from copy_reg import *1049 sage: from sage.misc.explain_pickle import *1050 sage: add_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 42)1051 sage: test_pickle(EmptyNewstyleClass())1052 0: \x80 PROTO 21053 2: \x82 EXT1 421054 4: ) EMPTY_TUPLE1055 5: \x81 NEWOBJ1056 6: q BINPUT 11057 8: } EMPTY_DICT1058 9: q BINPUT 21059 11: b BUILD1060 12: . STOP1061 highest protocol among opcodes = 21062 explain_pickle in_current_sage=True/False:1063 si = unpickle_newobj(unpickle_extension(42), ())1064 unpickle_build(si, {})1065 si1066 result: EmptyNewstyleClass1067 sage: remove_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 42)1068 """1069 self.push(self.sib.name('unpickle_extension')(n))1070 def EXT2(self, n):1071 r"""1072 TESTS::1073 sage: from copy_reg import *1074 sage: from sage.misc.explain_pickle import *1075 sage: add_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 31415)1076 sage: test_pickle(EmptyNewstyleClass())1077 0: \x80 PROTO 21078 2: \x83 EXT2 314151079 5: ) EMPTY_TUPLE1080 6: \x81 NEWOBJ1081 7: q BINPUT 11082 9: } EMPTY_DICT1083 10: q BINPUT 21084 12: b BUILD1085 13: . STOP1086 highest protocol among opcodes = 21087 explain_pickle in_current_sage=True/False:1088 si = unpickle_newobj(unpickle_extension(31415), ())1089 unpickle_build(si, {})1090 si1091 result: EmptyNewstyleClass1092 sage: remove_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 31415)1093 """1094 self.push(self.sib.name('unpickle_extension')(n))1095 def EXT4(self, n):1096 r"""1097 TESTS::1098 sage: from copy_reg import *1099 sage: from sage.misc.explain_pickle import *1100 sage: add_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 27182818)1101 sage: test_pickle(EmptyNewstyleClass())1102 0: \x80 PROTO 21103 2: \x84 EXT4 271828181104 7: ) EMPTY_TUPLE1105 8: \x81 NEWOBJ1106 9: q BINPUT 11107 11: } EMPTY_DICT1108 12: q BINPUT 21109 14: b BUILD1110 15: . STOP1111 highest protocol among opcodes = 21112 explain_pickle in_current_sage=True/False:1113 si = unpickle_newobj(unpickle_extension(27182818), ())1114 unpickle_build(si, {})1115 si1116 result: EmptyNewstyleClass1117 sage: remove_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 27182818)1118 """1119 self.push(self.sib.name('unpickle_extension')(n))1120 def FLOAT(self, f):1121 r"""1122 TESTS::1123 sage: from pickle import *1124 sage: from sage.misc.explain_pickle import *1125 sage: test_pickle(FLOAT + '2.71828\n')1126 0: F FLOAT 2.718281127 9: . STOP1128 highest protocol among opcodes = 01129 explain_pickle in_current_sage=True/False:1130 2.718281131 result: 2.718281132 """1133 self.push(self.sib(f))1134 def GET(self, n):1135 r"""1136 TESTS::1137 sage: from pickle import *1138 sage: from sage.misc.explain_pickle import *1139 sage: test_pickle(EMPTY_LIST + PUT + '1\n' + POP + GET + '1\n' + '.')1140 0: ] EMPTY_LIST1141 1: p PUT 11142 4: 0 POP1143 5: g GET 11144 8: . STOP1145 highest protocol among opcodes = 11146 explain_pickle in_current_sage=True/False:1147 []1148 result: []1149 """1150 self.push(self.memo[n])1151 def GLOBAL(self, name):1152 r"""1153 TESTS::1154 sage: from sage.misc.explain_pickle import *1155 We've used register_unpickle_override so that unpickle_global1156 will map TestGlobalOldName to TestGlobalNewName.1157 ::1158 sage: test_pickle(TestGlobalOldName())1159 0: \x80 PROTO 21160 2: c GLOBAL 'sage.misc.explain_pickle TestGlobalOldName'1161 46: q BINPUT 11162 48: ) EMPTY_TUPLE1163 49: \x81 NEWOBJ1164 50: q BINPUT 21165 52: } EMPTY_DICT1166 53: q BINPUT 31167 55: b BUILD1168 56: . STOP1169 highest protocol among opcodes = 21170 explain_pickle in_current_sage=True:1171 from sage.misc.explain_pickle import TestGlobalNewName1172 unpickle_newobj(TestGlobalNewName, ())1173 explain_pickle in_current_sage=False:1174 pg_TestGlobalOldName = unpickle_global('sage.misc.explain_pickle', 'TestGlobalOldName')1175 si = unpickle_newobj(pg_TestGlobalOldName, ())1176 unpickle_build(si, {})1177 si1178 result: TestGlobalNewName1179 Note that default_assumptions blithely assumes that it should1180 use the old name, giving code that doesn't actually work as1181 desired::1182 sage: explain_pickle(dumps(TestGlobalOldName()), default_assumptions=True)1183 from sage.misc.explain_pickle import TestGlobalOldName1184 unpickle_newobj(TestGlobalOldName, ())1185 A class name need not be a valid identifier::1186 sage: sage.misc.explain_pickle.__dict__['funny$name'] = TestGlobalFunnyName # see comment at end of file1187 sage: test_pickle((TestGlobalFunnyName(), TestGlobalFunnyName()))1188 0: \x80 PROTO 21189 2: c GLOBAL 'sage.misc.explain_pickle funny$name'1190 39: q BINPUT 11191 41: ) EMPTY_TUPLE1192 42: \x81 NEWOBJ1193 43: q BINPUT 21194 45: } EMPTY_DICT1195 46: q BINPUT 31196 48: b BUILD1197 49: h BINGET 11198 51: ) EMPTY_TUPLE1199 52: \x81 NEWOBJ1200 53: q BINPUT 41201 55: } EMPTY_DICT1202 56: q BINPUT 51203 58: b BUILD1204 59: \x86 TUPLE21205 60: q BINPUT 61206 62: . STOP1207 highest protocol among opcodes = 21208 explain_pickle in_current_sage=True/False:1209 si1 = unpickle_global('sage.misc.explain_pickle', 'funny$name')1210 si2 = unpickle_newobj(si1, ())1211 unpickle_build(si2, {})1212 si3 = unpickle_newobj(si1, ())1213 unpickle_build(si3, {})1214 (si2, si3)1215 result: (TestGlobalFunnyName, TestGlobalFunnyName)1216 """1217 module, func = name.split(' ')1218 if self.default_assumptions:1219 # Should the default assumption be that sage.all does, or1220 # does not, have a conflicting variable name?1221 # I'm going to go with "does not conflict".1222 self.push(self.sib.import_name(module, func))1223 return1224 name_ok = name_is_valid(func)1225 if self.in_current_sage and name_ok:1226 override = unpickle_override.get((module, func))1227 if override is None:1228 __import__(module)1229 f = getattr(sys.modules[module], func)1230 else:1231 f, new_mf = override1232 if new_mf is not None:1233 module, func = new_mf1234 if override is None or new_mf is not None:1235 # OK, we know what module and function name will actually1236 # be used, as well as the actual function.1237 # Is this already available at the command line?1238 cmdline_f = getattr(sage.all, func, None)1239 if cmdline_f is f:1240 self.push(PickleObject(f, self.sib.name(func)))1241 return1242 if cmdline_f is None:1243 # OK, we'll go ahead and import it under the original1244 # name.1245 self.push(PickleObject(f, self.sib.import_name(module, func)))1246 return1247 # The original name is in use.1248 self.push(PickleObject(f, self.sib.import_name(module, func, 'pg_' + func)))1249 return 1250 # We don't know the full name of the function that will1251 # actually be used (either we're being generic, or1252 # unpickle_override only has the function, not its name).1253 v = self.sib.name('unpickle_global')(module, func)1254 if name_ok:1255 self.sib.use_variable(v, 'pg_' + func)1256 self.push(v)1257 def INST(self, name):1258 r"""1259 TESTS::1260 sage: import pickle1261 sage: from sage.misc.explain_pickle import *1262 sage: test_pickle(pickle.dumps(EmptyOldstyleClass(), protocol=0))1263 0: ( MARK1264 1: i INST 'sage.misc.explain_pickle EmptyOldstyleClass' (MARK at 0)1265 46: p PUT 01266 49: ( MARK1267 50: d DICT (MARK at 49)1268 51: p PUT 11269 54: b BUILD1270 55: . STOP1271 highest protocol among opcodes = 01272 explain_pickle in_current_sage=True:1273 from types import InstanceType1274 from sage.misc.explain_pickle import EmptyOldstyleClass1275 InstanceType(EmptyOldstyleClass)1276 explain_pickle in_current_sage=False:1277 pg_EmptyOldstyleClass = unpickle_global('sage.misc.explain_pickle', 'EmptyOldstyleClass')1278 pg = unpickle_instantiate(pg_EmptyOldstyleClass, ())1279 unpickle_build(pg, {})1280 pg1281 result: EmptyOldstyleClass1282 """1283 self.TUPLE()1284 v = self.pop()1285 self.GLOBAL(name)1286 self.push(v)1287 self.REDUCE()1288 def INT(self, n):1289 r"""1290 TESTS::1291 sage: from pickle import *1292 sage: from sage.misc.explain_pickle import *1293 sage: test_pickle(INT + "-12345\n")1294 0: I INT -123451295 8: . STOP1296 highest protocol among opcodes = 01297 explain_pickle in_current_sage=True/False:1298 -123451299 result: -123451300 INT can also be used to record True and False::1301 sage: test_pickle(INT + "00\n")1302 0: I INT False1303 4: . STOP1304 highest protocol among opcodes = 01305 explain_pickle in_current_sage=True/False:1306 False1307 result: False1308 sage: test_pickle(INT + "01\n")1309 0: I INT True1310 4: . STOP1311 highest protocol among opcodes = 01312 explain_pickle in_current_sage=True/False:1313 True1314 result: True1315 """1316 self.push_and_share(self.sib(n))1317 def LIST(self):1318 r"""1319 TESTS::1320 sage: from pickle import *1321 sage: from sage.misc.explain_pickle import *1322 sage: test_pickle(MARK + NONE + NEWFALSE + LIST)1323 0: ( MARK1324 1: N NONE1325 2: \x89 NEWFALSE1326 3: l LIST (MARK at 0)1327 4: . STOP1328 highest protocol among opcodes = 21329 explain_pickle in_current_sage=True/False:1330 [None, False]1331 result: [None, False]1332 """1333 lst = self.pop_to_mark()1334 self.push(PickleObject(lst, self.sib(lst)))1335 def LONG(self, n):1336 r"""1337 TESTS::1338 sage: from pickle import *1339 sage: from sage.misc.explain_pickle import *1340 sage: test_pickle(LONG + "12345678909876543210123456789L\n")1341 0: L LONG 12345678909876543210123456789L1342 32: . STOP1343 highest protocol among opcodes = 01344 explain_pickle in_current_sage=True/False:1345 123456789098765432101234567891346 result: 12345678909876543210123456789L1347 """1348 self.push(self.sib(n))1349 def LONG1(self, n):1350 r"""1351 TESTS::1352 sage: from sage.misc.explain_pickle import *1353 sage: test_pickle(1L)1354 0: \x80 PROTO 21355 2: \x8a LONG1 1L1356 5: . STOP1357 highest protocol among opcodes = 21358 explain_pickle in_current_sage=True/False:1359 1L1360 result: 1L1361 """1362 self.push(self.sib(n))1363 def LONG4(self, n):1364 r"""1365 TESTS::1366 sage: from pickle import *1367 sage: from sage.misc.explain_pickle import *1368 sage: test_pickle(LONG4 + '\014\0\0\0' + 'hello, world')1369 0: \x8b LONG4 31079605376604435891501163880L1370 17: . STOP1371 highest protocol among opcodes = 21372 explain_pickle in_current_sage=True/False:1373 310796053766044358915011638801374 result: 31079605376604435891501163880L1375 """1376 self.push(self.sib(n))1377 def LONG_BINGET(self, n):1378 r"""1379 TESTS::1380 sage: from pickle import *1381 sage: from sage.misc.explain_pickle import *1382 sage: test_pickle(EMPTY_LIST + LONG_BINPUT + 'Sage' + POP + LONG_BINGET + 'Sage')1383 0: ] EMPTY_LIST1384 1: r LONG_BINPUT 17012739391385 6: 0 POP1386 7: j LONG_BINGET 17012739391387 12: . STOP1388 highest protocol among opcodes = 11389 explain_pickle in_current_sage=True/False:1390 []1391 result: [] 1392 """1393 self.push(self.memo[n])1394 def LONG_BINPUT(self, n):1395 r"""1396 TESTS::1397 sage: from pickle import *1398 sage: from sage.misc.explain_pickle import *1399 sage: test_pickle(EMPTY_LIST + LONG_BINPUT + 'Sage' + POP + LONG_BINGET + 'Sage')1400 0: ] EMPTY_LIST1401 1: r LONG_BINPUT 17012739391402 6: 0 POP1403 7: j LONG_BINGET 17012739391404 12: . STOP1405 highest protocol among opcodes = 11406 explain_pickle in_current_sage=True/False:1407 []1408 result: [] 1409 """1410 v = self.pop()1411 self.memo[n] = v1412 self.push(v)1413 def MARK(self):1414 r"""1415 TESTS::1416 sage: from pickle import *1417 sage: from sage.misc.explain_pickle import *1418 sage: test_pickle(MARK + TUPLE)1419 0: ( MARK1420 1: t TUPLE (MARK at 0)1421 2: . STOP1422 highest protocol among opcodes = 01423 explain_pickle in_current_sage=True/False:1424 ()1425 result: ()1426 """1427 self.push_mark()1428 def NEWFALSE(self):1429 r"""1430 TESTS::1431 sage: from pickle import *1432 sage: from sage.misc.explain_pickle import *1433 sage: test_pickle(NEWFALSE)1434 0: \x89 NEWFALSE1435 1: . STOP1436 highest protocol among opcodes = 21437 explain_pickle in_current_sage=True/False:1438 False1439 result: False1440 """1441 self.push(self.sib.name('False'))1442 def NEWTRUE(self):1443 r"""1444 TESTS::1445 sage: from pickle import *1446 sage: from sage.misc.explain_pickle import *1447 sage: test_pickle(NEWTRUE)1448 0: \x88 NEWTRUE1449 1: . STOP1450 highest protocol among opcodes = 21451 explain_pickle in_current_sage=True/False:1452 True1453 result: True1454 """1455 self.push(self.sib.name('True'))1456 def NEWOBJ(self):1457 r"""1458 TESTS::1459 sage: from sage.misc.explain_pickle import *1460 sage: test_pickle(EmptyNewstyleClass())1461 0: \x80 PROTO 21462 2: c GLOBAL 'sage.misc.explain_pickle EmptyNewstyleClass'1463 47: q BINPUT 11464 49: ) EMPTY_TUPLE1465 50: \x81 NEWOBJ1466 51: q BINPUT 21467 53: } EMPTY_DICT1468 54: q BINPUT 31469 56: b BUILD1470 57: . STOP1471 highest protocol among opcodes = 21472 explain_pickle in_current_sage=True:1473 from sage.misc.explain_pickle import EmptyNewstyleClass1474 unpickle_newobj(EmptyNewstyleClass, ())1475 explain_pickle in_current_sage=False:1476 pg_EmptyNewstyleClass = unpickle_global('sage.misc.explain_pickle', 'EmptyNewstyleClass')1477 si = unpickle_newobj(pg_EmptyNewstyleClass, ())1478 unpickle_build(si, {})1479 si1480 result: EmptyNewstyleClass1481 """1482 args = self.pop()1483 klass = self.pop()1484 obj = self.sib.name('unpickle_newobj')(klass, args)1485 if isinstance(klass, PickleObject):1486 self.push(PickleObject(PickleInstance(klass.value), obj))1487 else:1488 self.push(obj)1489 def NONE(self):1490 r"""1491 TESTS::1492 sage: from pickle import *1493 sage: from sage.misc.explain_pickle import *1494 sage: test_pickle(NONE)1495 0: N NONE1496 1: . STOP1497 highest protocol among opcodes = 01498 explain_pickle in_current_sage=True/False:1499 None1500 result: None1501 """1502 self.push(PickleObject(None, self.sib.name('None')))1503 def OBJ(self):1504 r"""1505 TESTS::1506 sage: from sage.misc.explain_pickle import *1507 sage: test_pickle(EmptyOldstyleClass())1508 0: \x80 PROTO 21509 2: ( MARK1510 3: c GLOBAL 'sage.misc.explain_pickle EmptyOldstyleClass'1511 48: q BINPUT 11512 50: o OBJ (MARK at 2)1513 51: q BINPUT 21514 53: } EMPTY_DICT1515 54: q BINPUT 31516 56: b BUILD1517 57: . STOP1518 highest protocol among opcodes = 21519 explain_pickle in_current_sage=True:1520 from types import InstanceType1521 from sage.misc.explain_pickle import EmptyOldstyleClass1522 InstanceType(EmptyOldstyleClass)1523 explain_pickle in_current_sage=False:1524 pg_EmptyOldstyleClass = unpickle_global('sage.misc.explain_pickle', 'EmptyOldstyleClass')1525 pg = unpickle_instantiate(pg_EmptyOldstyleClass, ())1526 unpickle_build(pg, {})1527 pg1528 result: EmptyOldstyleClass1529 """1530 klass_args = self.pop_to_mark()1531 klass = klass_args[0]1532 args = klass_args[1:]1533 self.push(klass)1534 self.push(PickleObject(tuple(args), self.sib(tuple(args))))1535 self.REDUCE()1536 1537 def PERSID(self, id):1538 r"""1539 TESTS::1540 sage: from pickle import *1541 sage: from sage.misc.explain_pickle import *1542 sage: test_pickle(PERSID + "0\n" + '.', args=('Yo!',))1543 0: P PERSID '0'1544 3: . STOP1545 highest protocol among opcodes = 01546 explain_pickle in_current_sage=True/False:1547 unpickle_persistent('0')1548 result: 'Yo!'1549 """1550 self.push(self.sib.name('unpickle_persistent')(id))1551 def BINPERSID(self):1552 r"""1553 TESTS::1554 sage: from pickle import *1555 sage: from sage.misc.explain_pickle import *1556 sage: test_pickle(INT + "0\n" + BINPERSID + '.', args=('Yo!',))1557 0: I INT 01558 3: Q BINPERSID1559 4: . STOP1560 highest protocol among opcodes = 11561 explain_pickle in_current_sage=True/False:1562 unpickle_persistent(0)1563 result: 'Yo!'1564 """1565 id = self.pop()1566 self.push(self.sib.name('unpickle_persistent')(id))1567 def POP(self):1568 r"""1569 TESTS::1570 sage: from pickle import *1571 sage: from sage.misc.explain_pickle import *1572 sage: test_pickle(INT + "0\n" + POP + INT + "42\n")1573 0: I INT 01574 3: 0 POP1575 4: I INT 421576 8: . STOP1577 highest protocol among opcodes = 01578 explain_pickle in_current_sage=True/False:1579 421580 result: 421581 """1582 v = self.stack.pop()1583 if v is not the_mark:1584 self.check_value(v)1585 def POP_MARK(self):1586 r"""1587 TESTS::1588 sage: from pickle import *1589 sage: from sage.misc.explain_pickle import *1590 sage: test_pickle(MARK + NONE + NEWFALSE + POP_MARK + NEWTRUE)1591 0: ( MARK1592 1: N NONE1593 2: \x89 NEWFALSE1594 3: 1 POP_MARK (MARK at 0)1595 4: \x88 NEWTRUE1596 5: . STOP1597 highest protocol among opcodes = 21598 explain_pickle in_current_sage=True/False:1599 True1600 result: True1601 """1602 self.pop_to_mark()1603 def PROTO(self, proto):1604 r"""1605 TESTS::1606 sage: from sage.misc.explain_pickle import *1607 sage: test_pickle(0r)1608 0: \x80 PROTO 21609 2: K BININT1 01610 4: . STOP1611 highest protocol among opcodes = 21612 explain_pickle in_current_sage=True/False:1613 01614 result: 01615 """1616 if not 0 <= proto <= 2:1617 raise ValueError, "unsupported pickle protocol: %d" % proto1618 def PUT(self, n):1619 r"""1620 TESTS::1621 sage: from pickle import *1622 sage: from sage.misc.explain_pickle import *1623 sage: test_pickle(EMPTY_LIST + PUT + '1\n' + POP + GET + '1\n' + '.')1624 0: ] EMPTY_LIST1625 1: p PUT 11626 4: 0 POP1627 5: g GET 11628 8: . STOP1629 highest protocol among opcodes = 11630 explain_pickle in_current_sage=True/False:1631 []1632 result: []1633 """1634 v = self.pop()1635 self.memo[n] = v1636 self.push(v)1637 def REDUCE(self):1638 r"""1639 TESTS::1640 sage: import pickle1641 sage: from sage.misc.explain_pickle import *1642 sage: test_pickle(pickle.dumps(EmptyNewstyleClass(), protocol=1))1643 0: c GLOBAL 'copy_reg _reconstructor'1644 25: q BINPUT 01645 27: ( MARK1646 28: c GLOBAL 'sage.misc.explain_pickle EmptyNewstyleClass'1647 73: q BINPUT 11648 75: c GLOBAL '__builtin__ object'1649 95: q BINPUT 21650 97: N NONE1651 98: t TUPLE (MARK at 27)1652 99: q BINPUT 31653 101: R REDUCE1654 102: q BINPUT 41655 104: . STOP1656 highest protocol among opcodes = 11657 explain_pickle in_current_sage=True:1658 from copy_reg import _reconstructor1659 from sage.misc.explain_pickle import EmptyNewstyleClass1660 from __builtin__ import object1661 _reconstructor(EmptyNewstyleClass, object, None)1662 explain_pickle in_current_sage=False:1663 pg__reconstructor = unpickle_global('copy_reg', '_reconstructor')1664 pg_EmptyNewstyleClass = unpickle_global('sage.misc.explain_pickle', 'EmptyNewstyleClass')1665 pg_object = unpickle_global('__builtin__', 'object')1666 pg__reconstructor(pg_EmptyNewstyleClass, pg_object, None)1667 result: EmptyNewstyleClass1668 ::1669 sage: test_pickle(TestReduceGetinitargs(), verbose_eval=True)1670 Running __init__ for TestReduceGetinitargs1671 0: \x80 PROTO 21672 2: ( MARK1673 3: c GLOBAL 'sage.misc.explain_pickle TestReduceGetinitargs'1674 51: q BINPUT 11675 53: o OBJ (MARK at 2)1676 54: q BINPUT 21677 56: } EMPTY_DICT1678 57: q BINPUT 31679 59: b BUILD1680 60: . STOP1681 highest protocol among opcodes = 21682 explain_pickle in_current_sage=True:1683 from sage.misc.explain_pickle import TestReduceGetinitargs1684 TestReduceGetinitargs()1685 explain_pickle in_current_sage=False:1686 pg_TestReduceGetinitargs = unpickle_global('sage.misc.explain_pickle', 'TestReduceGetinitargs')1687 pg = unpickle_instantiate(pg_TestReduceGetinitargs, ())1688 unpickle_build(pg, {})1689 pg1690 evaluating explain_pickle in_current_sage=True:1691 Running __init__ for TestReduceGetinitargs1692 evaluating explain_pickle in_current_sage=False:1693 Running __init__ for TestReduceGetinitargs1694 loading pickle with cPickle:1695 Running __init__ for TestReduceGetinitargs1696 result: TestReduceGetinitargs1697 ::1698 sage: test_pickle(TestReduceNoGetinitargs(), verbose_eval=True)1699 Running __init__ for TestReduceNoGetinitargs1700 0: \x80 PROTO 21701 2: ( MARK1702 3: c GLOBAL 'sage.misc.explain_pickle TestReduceNoGetinitargs'1703 53: q BINPUT 11704 55: o OBJ (MARK at 2)1705 56: q BINPUT 21706 58: } EMPTY_DICT1707 59: q BINPUT 31708 61: b BUILD1709 62: . STOP1710 highest protocol among opcodes = 21711 explain_pickle in_current_sage=True:1712 from types import InstanceType1713 from sage.misc.explain_pickle import TestReduceNoGetinitargs1714 InstanceType(TestReduceNoGetinitargs)1715 explain_pickle in_current_sage=False:1716 pg_TestReduceNoGetinitargs = unpickle_global('sage.misc.explain_pickle', 'TestReduceNoGetinitargs')1717 pg = unpickle_instantiate(pg_TestReduceNoGetinitargs, ())1718 unpickle_build(pg, {})1719 pg1720 evaluating explain_pickle in_current_sage=True:1721 evaluating explain_pickle in_current_sage=False:1722 loading pickle with cPickle:1723 result: TestReduceNoGetinitargs1724 """1725 # Reading cPickle.c (in the Instance_New function),1726 # I think that REDUCE is equivalent to a function call unless1727 # all three of the following conditions are met:1728 # obj is an old-style class1729 # obj defines __getinitargs__1730 # args is an empty tuple1731 # in which case it is equivalent to PyInstance_NewRaw(obj)1732 args = self.pop()1733 obj = self.pop()1734 simple_call = False1735 new_inst = False1736 if isinstance(args, PickleObject) and isinstance(args.value, tuple) \1737 and len(args.value) > 0:1738 simple_call = True1739 if self.default_assumptions:1740 simple_call = True1741 if self.in_current_sage:1742 if isinstance(obj, PickleObject):1743 if isinstance(obj.value, type):1744 simple_call = True1745 elif isinstance(obj.value, types.ClassType):1746 if hasattr(obj.value, '__getinitargs__'):1747 simple_call = True1748 else:1749 new_inst = True1750 1751 if simple_call:1752 v = self.sib(obj)(*args.value)1753 elif new_inst:1754 v = self.new_instance(obj)1755 else:1756 v = self.sib.name('unpickle_instantiate')(obj, args)1757 self.sib.use_variable(v, 'pg')1758 if isinstance(obj, PickleObject):1759 self.push(PickleObject(PickleInstance(obj.value), v))1760 else:1761 self.push(v)1762 def SETITEM(self):1763 r"""1764 TESTS::1765 sage: import pickle1766 sage: from sage.misc.explain_pickle import *1767 sage: test_pickle(pickle.dumps({'a': 'b'}))1768 0: ( MARK1769 1: d DICT (MARK at 0)1770 2: p PUT 01771 5: S STRING 'a'1772 10: p PUT 11773 13: S STRING 'b'1774 18: p PUT 21775 21: s SETITEM1776 22: . STOP1777 highest protocol among opcodes = 01778 explain_pickle in_current_sage=True/False:1779 {'a':'b'}1780 result: {'a': 'b'} 1781 We see above that we output the result as a dictionary literal, when1782 possible. This is impossible when a key or value is recursive. First1783 we test recursive values::1784 sage: value_rec = dict()1785 sage: value_rec['circular'] = value_rec1786 sage: test_pickle(pickle.dumps(value_rec))1787 0: ( MARK1788 1: d DICT (MARK at 0)1789 2: p PUT 01790 5: S STRING 'circular'1791 17: p PUT 11792 20: g GET 01793 23: s SETITEM1794 24: . STOP1795 highest protocol among opcodes = 01796 explain_pickle in_current_sage=True/False:1797 si = {}1798 si['circular'] = si1799 si1800 result: {'circular': {...}}1801 Then we test recursive keys::1802 sage: key_rec = dict()1803 sage: key = EmptyNewstyleClass()1804 sage: key.circular = key_rec1805 sage: key_rec[key] = 'circular'1806 sage: test_pickle(pickle.dumps(key_rec))1807 0: ( MARK1808 1: d DICT (MARK at 0)1809 2: p PUT 01810 5: c GLOBAL 'copy_reg _reconstructor'1811 30: p PUT 11812 33: ( MARK1813 34: c GLOBAL 'sage.misc.explain_pickle EmptyNewstyleClass'1814 79: p PUT 21815 82: c GLOBAL '__builtin__ object'1816 102: p PUT 31817 105: N NONE1818 106: t TUPLE (MARK at 33)1819 107: p PUT 41820 110: R REDUCE1821 111: p PUT 51822 114: ( MARK1823 115: d DICT (MARK at 114)1824 116: p PUT 61825 119: S STRING 'circular'1826 131: p PUT 71827 134: g GET 01828 137: s SETITEM1829 138: b BUILD1830 139: g GET 71831 142: s SETITEM1832 143: . STOP1833 highest protocol among opcodes = 01834 explain_pickle in_current_sage=True:1835 si1 = {}1836 from copy_reg import _reconstructor1837 from sage.misc.explain_pickle import EmptyNewstyleClass1838 from __builtin__ import object1839 si2 = _reconstructor(EmptyNewstyleClass, object, None)1840 si2.__dict__['circular'] = si11841 si1[si2] = 'circular'1842 si11843 explain_pickle in_current_sage=False:1844 si1 = {}1845 pg__reconstructor = unpickle_global('copy_reg', '_reconstructor')1846 pg_EmptyNewstyleClass = unpickle_global('sage.misc.explain_pickle', 'EmptyNewstyleClass')1847 pg_object = unpickle_global('__builtin__', 'object')1848 si2 = pg__reconstructor(pg_EmptyNewstyleClass, pg_object, None)1849 unpickle_build(si2, {'circular':si1})1850 si1[si2] = 'circular'1851 si11852 result: {EmptyNewstyleClass: 'circular'}1853 """1854 v = self.pop()1855 k = self.pop()1856 self._SETITEMS_helper([k, v])1857 def SETITEMS(self):1858 r"""1859 TESTS::1860 sage: import pickle1861 sage: from sage.misc.explain_pickle import *1862 sage: test_pickle(pickle.dumps({'a': 'b', 1r : 2r}, protocol=2))1863 0: \x80 PROTO 21864 2: } EMPTY_DICT1865 3: q BINPUT 01866 5: ( MARK1867 6: U SHORT_BINSTRING 'a'1868 9: q BINPUT 11869 11: U SHORT_BINSTRING 'b'1870 14: q BINPUT 21871 16: K BININT1 11872 18: K BININT1 21873 20: u SETITEMS (MARK at 5)1874 21: . STOP1875 highest protocol among opcodes = 21876 explain_pickle in_current_sage=True/False:1877 {'a':'b', 1:2}1878 result: {'a': 'b', 1: 2}1879 Similar to the tests for SETITEM, we test recursive keys and values::1880 sage: recdict = {}1881 sage: recdict['Circular value'] = recdict1882 sage: key = EmptyOldstyleClass()1883 sage: key.recdict = recdict1884 sage: recdict[key] = 'circular_key'1885 sage: test_pickle(pickle.dumps(recdict, protocol=2))1886 0: \x80 PROTO 21887 2: } EMPTY_DICT1888 3: q BINPUT 01889 5: ( MARK1890 6: ( MARK1891 7: c GLOBAL 'sage.misc.explain_pickle EmptyOldstyleClass'1892 52: q BINPUT 11893 54: o OBJ (MARK at 6)1894 55: q BINPUT 21895 57: } EMPTY_DICT1896 58: q BINPUT 31897 60: U SHORT_BINSTRING 'recdict'1898 69: q BINPUT 41899 71: h BINGET 01900 73: s SETITEM1901 74: b BUILD1902 75: U SHORT_BINSTRING 'circular_key'1903 89: q BINPUT 51904 91: U SHORT_BINSTRING 'Circular value'1905 107: q BINPUT 61906 109: h BINGET 01907 111: u SETITEMS (MARK at 5)1908 112: . STOP1909 highest protocol among opcodes = 21910 explain_pickle in_current_sage=True:1911 si1 = {}1912 from types import InstanceType1913 from sage.misc.explain_pickle import EmptyOldstyleClass1914 si2 = InstanceType(EmptyOldstyleClass)1915 si2.__dict__['recdict'] = si11916 si1[si2] = 'circular_key'1917 si1['Circular value'] = si11918 si11919 explain_pickle in_current_sage=False:1920 si = {}1921 pg_EmptyOldstyleClass = unpickle_global('sage.misc.explain_pickle', 'EmptyOldstyleClass')1922 pg = unpickle_instantiate(pg_EmptyOldstyleClass, ())1923 unpickle_build(pg, {'recdict':si})1924 si[pg] = 'circular_key'1925 si['Circular value'] = si1926 si1927 result: {EmptyOldstyleClass: 'circular_key', 'Circular value': {...}}1928 """1929 slice = self.pop_to_mark()1930 self._SETITEMS_helper(slice)1931 def _SETITEMS_helper(self, slice):1932 r"""1933 TESTS::1934 sage: import pickle1935 sage: from sage.misc.explain_pickle import *1936 sage: test_pickle(pickle.dumps({'a': 'b'})) # indirect doctest1937 0: ( MARK1938 1: d DICT (MARK at 0)1939 2: p PUT 01940 5: S STRING 'a'1941 10: p PUT 11942 13: S STRING 'b'1943 18: p PUT 21944 21: s SETITEM1945 22: . STOP1946 highest protocol among opcodes = 01947 explain_pickle in_current_sage=True/False:1948 {'a':'b'}1949 result: {'a': 'b'} 1950 """1951 updates = []1952 i = 01953 while i < len(slice):1954 k = slice[i]1955 v = slice[i+1]1956 # This marks d as immutable, if k or v happens to include d.1957 self.sib(k)1958 self.sib(v)1959 updates.append((k, v))1960 i += 21961 d = self.pop()1962 if self.is_mutable_pickle_object(d) and isinstance(d.value, PickleDict):1963 d.value = PickleDict(d.value.items + updates)1964 d.expression = self.sib.dict(d.value.items)1965 else:1966 d = self.sib(d)1967 for k, v in updates:1968 self.sib.command(d, self.sib.assign(d[k], v))1969 self.push(d)1970 def SHORT_BINSTRING(self, s):1971 r"""1972 TESTS::1973 sage: from sage.misc.explain_pickle import *1974 sage: test_pickle(dumps('hello', compress=False))1975 0: \x80 PROTO 21976 2: U SHORT_BINSTRING 'hello'1977 9: q BINPUT 11978 11: . STOP1979 highest protocol among opcodes = 21980 explain_pickle in_current_sage=True/False:1981 'hello'1982 result: 'hello'1983 """1984 self.push(PickleObject(s, self.share(self.sib(s))))1985 def STOP(self):1986 r"""1987 TESTS::1988 sage: from pickle import *1989 sage: from sage.misc.explain_pickle import *1990 sage: test_pickle(EMPTY_TUPLE)1991 0: ) EMPTY_TUPLE1992 1: . STOP1993 highest protocol among opcodes = 11994 explain_pickle in_current_sage=True/False:1995 ()1996 result: ()1997 """1998 self.stopped = True1999 def STRING(self, s):2000 r"""2001 TESTS::2002 sage: from sage.misc.explain_pickle import *2003 sage: test_pickle("S'Testing...'\n.")2004 0: S STRING 'Testing...'2005 14: . STOP2006 highest protocol among opcodes = 02007 explain_pickle in_current_sage=True/False:2008 'Testing...'2009 result: 'Testing...'2010 """2011 self.push(PickleObject(s, self.share(self.sib(s))))2012 def TUPLE(self):2013 r"""2014 TESTS::2015 sage: import pickle2016 sage: from sage.misc.explain_pickle import *2017 sage: test_pickle(pickle.dumps(('a',)))2018 0: ( MARK2019 1: S STRING 'a'2020 6: p PUT 02021 9: t TUPLE (MARK at 0)2022 10: p PUT 12023 13: . STOP2024 highest protocol among opcodes = 02025 explain_pickle in_current_sage=True/False:2026 ('a',)2027 result: ('a',)2028 We prefer to produce tuple literals, as above; but if the2029 tuple is recursive, we need a more complicated2030 construction. It used to be the case that the cPickle2031 unpickler couldn't handle this case, but that's no longer true2032 (see http://bugs.python.org/issue5794)::2033 sage: v = ([],)2034 sage: v[0].append(v)2035 sage: test_pickle(pickle.dumps(v))2036 0: ( MARK2037 1: ( MARK2038 2: l LIST (MARK at 1)2039 3: p PUT 02040 6: ( MARK2041 7: g GET 02042 10: t TUPLE (MARK at 6)2043 11: p PUT 12044 14: a APPEND2045 15: 0 POP2046 16: 0 POP (MARK at 0)2047 17: g GET 12048 20: . STOP2049 highest protocol among opcodes = 02050 explain_pickle in_current_sage=True/False:2051 si1 = []2052 si2 = (si1,)2053 list.append(si1, si2)2054 si22055 result: ([(...)],)2056 """2057 v = self.pop_to_mark()2058 self.push(PickleObject(tuple(v), self.sib(tuple(v))))2059 def TUPLE1(self):2060 r"""2061 TESTS::2062 sage: from sage.misc.explain_pickle import *2063 sage: test_pickle(('a',))2064 0: \x80 PROTO 22065 2: U SHORT_BINSTRING 'a'2066 5: \x85 TUPLE12067 6: q BINPUT 12068 8: . STOP2069 highest protocol among opcodes = 22070 explain_pickle in_current_sage=True/False:2071 ('a',)2072 result: ('a',)2073 """2074 v1 = self.pop()2075 self.push(PickleObject((v1,), self.sib((v1,))))2076 2077 def TUPLE2(self):2078 r"""2079 TESTS::2080 sage: from sage.misc.explain_pickle import *2081 sage: test_pickle(('a','b'))2082 0: \x80 PROTO 22083 2: U SHORT_BINSTRING 'a'2084 5: U SHORT_BINSTRING 'b'2085 8: \x86 TUPLE22086 9: q BINPUT 12087 11: . STOP2088 highest protocol among opcodes = 22089 explain_pickle in_current_sage=True/False:2090 ('a', 'b')2091 result: ('a', 'b')2092 """2093 v2 = self.pop()2094 v1 = self.pop()2095 self.push(PickleObject((v1, v2), self.sib((v1, v2))))2096 2097 def TUPLE3(self):2098 r"""2099 TESTS::2100 sage: from sage.misc.explain_pickle import *2101 sage: test_pickle(('a','b','c'))2102 0: \x80 PROTO 22103 2: U SHORT_BINSTRING 'a'2104 5: U SHORT_BINSTRING 'b'2105 8: U SHORT_BINSTRING 'c'2106 11: \x87 TUPLE32107 12: q BINPUT 12108 14: . STOP2109 highest protocol among opcodes = 22110 explain_pickle in_current_sage=True/False:2111 ('a', 'b', 'c')2112 result: ('a', 'b', 'c')2113 """2114 v3 = self.pop()2115 v2 = self.pop()2116 v1 = self.pop()2117 self.push(PickleObject((v1, v2, v3), self.sib((v1, v2, v3))))2118 def UNICODE(self, s):2119 r"""2120 TESTS::2121 sage: import pickle2122 sage: from sage.misc.explain_pickle import *2123 sage: test_pickle(pickle.dumps(u'hi\u1234\U00012345'))2124 0: V UNICODE u'hi\u1234\U00012345'2125 20: p PUT 02126 23: . STOP2127 highest protocol among opcodes = 02128 explain_pickle in_current_sage=True/False:2129 u'hi\u1234\U00012345'2130 result: u'hi\u1234\U00012345'2131 """2132 self.push_and_share(self.sib(s))2133# def explain_picklejar(dir, catch_exceptions=True):2134# r"""2135# Given a directory or a .tar.bz2 of pickles, we unpickle each of2136# them three times: once with cPickle, once with explain_pickle2137# with in_current_sage=True, and once with explain_pickle2138# with in_current_sage=False.2139# We verify that each of these gives the same answer (where we check2140# using repr(a)==repr(b), in case equality is not defined on all2141# of these objects).2142# INPUT::2143# dir -- string; a directory or name of a .tar.bz2 file that2144# decompresses to give a directory full of pickles.2145# catch_exceptions -- boolean; default True: if True, then2146# unpickling exceptions will be caught (and if all three2147# unpicklers raise an exception, then that will be counted2148# as a match). If False, then exceptions will not be caught,2149# and will terminate this function.2150# EXAMPLES:2151# sage: std = os.environ['SAGE_DATA'] + '/extcode/pickle_jar/pickle_jar.tar.bz2'2152# sage: from sage.misc.explain_pickle import *2153# sage: explain_picklejar(std) # long time (~45s on my computer)2154# doctest:...: DeprecationWarning: Your data is stored in an old format. Please use the save() function to store your data in a more recent format.2155# doctest:...: DeprecationWarning: Your data is stored in an old format. Please use the save() function to store your data in a more recent format.2156# 4... matches; 0 mismatches2157# """2158# # Largely copied from unpickle_all in sage_object.pyx2159# from sage.structure.sage_object import load2160# obj_at = re.compile('object at 0x[0-9a-f]+')2161# n_success = 02162# failed = []2163# if dir.endswith('.tar.bz2'):2164# # create a temporary directory2165# from sage.misc.all import tmp_dir2166# T = tmp_dir()2167# # extract tarball to it2168# os.system('cd "%s"; bunzip2 -c "%s" | tar fx - '%(T, os.path.abspath(dir)))2169# # Now use the directory in the tarball instead of dir2170# dir = T + "/" + os.listdir(T)[0]2171 2172# for A in os.listdir(dir):2173# if A.endswith('.sobj'):2174# fn = dir + '/' + A2175# try:2176# v1 = load(fn)2177# v1r = repr(v1)2178# except Exception, msg:2179# # if not catch_exceptions: raise2180# v1r = 'error'2181# try:2182# exp_current = explain_pickle(file=fn, in_current_sage=True)2183# import sys2184# sys.stderr.write(exp_current)2185# v2 = sage_eval(exp_current)2186# v2r = repr(v2)2187# except Exception, msg:2188# if not catch_exceptions: raise2189# v2r = 'error'2190# try:2191# exp_generic = explain_pickle(file=fn)2192# import sys2193# sys.stderr.write(exp_generic)2194# v3 = sage_eval(exp_generic)2195# v3r = repr(v3)2196# except Exception, msg:2197# if not catch_exceptions: raise2198# v3r = 'error'2199# v1r = obj_at.sub('object at 0x...', v1r)2200# v2r = obj_at.sub('object at 0x...', v2r)2201# v3r = obj_at.sub('object at 0x...', v3r)2202# if v1r == v2r == v3r:2203# n_success += 12204# else:2205# print "Mismatch on %s" % fn2206# print v1r, v2r, v3r2207# failed.append(A)2208# print "%d matches; %d mismatches" % (n_success, len(failed))2209# print '\n'.join(failed)2210# Helper routines for explain_pickle2211 2212def unpickle_newobj(klass, args):2213 r"""2214 Create a new object; this corresponds to the C code2215 klass->tp_new(klass, args, NULL). Used by ``explain_pickle``.2216 EXAMPLES:2217 sage: unpickle_newobj(tuple, ([1, 2, 3],))2218 (1, 2, 3)2219 """2220 # We need to call klass->tp_new(klass, args, NULL).2221 # This is almost but not quite the same as klass.__new__(klass, *args).2222 # (I don't know exactly what the difference is, but when you try2223 # to unpickle a Sequence, cPickle -- which uses the former -- works,2224 # and pickle.py -- which uses the latter -- fails, with2225 # TypeError: sage.structure.sage_object.SageObject.__new__(Sequence) is not safe, use list.__new__()2226 # )2227 # It seems unlikely that you can implement this from pure-Python code --2228 # somewhat disturbingly, it actually is possible. This shows how.2229 # (Using Cython would also work, of course; but this is cooler, and2230 # probably simpler.)2231 # This pickle is: load persistent object 0, load persistent object 1,2232 # NEWOBJ, STOP.2233 pickle = "P0\nP1\n\x81."2234 pers = [klass, args]2235 pers_load = lambda id: pers[int(id)]2236 from cStringIO import StringIO2237 import cPickle2238 unp = cPickle.Unpickler(StringIO(pickle))2239 unp.persistent_load = pers_load2240 return unp.load()2241 2242def unpickle_build(obj, state):2243 r"""2244 Set the state of an object. Used by ``explain_pickle``.2245 EXAMPLES::2246 sage: from sage.misc.explain_pickle import *2247 sage: v = EmptyNewstyleClass()2248 sage: unpickle_build(v, {'hello': 42})2249 sage: v.hello2250 422251 """2252 setstate = getattr(obj, '__setstate__', None)2253 if setstate is not None:2254 setstate(state)2255 return2256 if isinstance(state, tuple) and len(state) == 2:2257 state, slots = state2258 else:2259 slots = None2260 if state is not None:2261 assert(isinstance(state, dict))2262 d = obj.__dict__2263 for k,v in state.iteritems():2264 d[k] = v2265 if slots is not None:2266 assert(isinstance(slots, dict))2267 for k,v in slots.iteritems():2268 setattr(obj, k, v)2269def unpickle_instantiate(fn, args):2270 r"""2271 Instantiate a new object of class fn with arguments args. Almost always2272 equivalent to ``fn(*args)``. Used by ``explain_pickle``.2273 EXAMPLES::2274 sage: unpickle_instantiate(Integer, ('42',))2275 422276 """2277 if isinstance(fn, types.ClassType) and len(args) == 0 and not hasattr(fn, '__getinitargs__'):2278 return types.InstanceType(fn)2279 2280 return fn(*args)2281unpickle_persistent_loader = None2282def unpickle_persistent(s):2283 r"""2284 Takes an integer index and returns the persistent object with that2285 index; works by calling whatever callable is stored in2286 unpickle_persistent_loader. Used by ``explain_pickle``.2287 EXAMPLES::2288 sage: import sage.misc.explain_pickle2289 sage: sage.misc.explain_pickle.unpickle_persistent_loader = lambda n: n+72290 sage: unpickle_persistent(35)2291 422292 """2293 return unpickle_persistent_loader(s)2294def unpickle_extension(code):2295 r"""2296 Takes an integer index and returns the extension object with that2297 index. Used by ``explain_pickle``.2298 EXAMPLES::2299 sage: from copy_reg import *2300 sage: add_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 42)2301 sage: unpickle_extension(42)2302 <class 'sage.misc.explain_pickle.EmptyNewstyleClass'>2303 sage: remove_extension('sage.misc.explain_pickle', 'EmptyNewstyleClass', 42)2304 """2305 from copy_reg import _inverted_registry, _extension_cache2306 # copied from .get_extension() in pickle.py2307 nil = []2308 obj = _extension_cache.get(code, nil)2309 if obj is not nil:2310 return obj2311 key = _inverted_registry.get(code)2312 if not key:2313 raise ValueError("unregistered extension code %d" % code)2314 obj = unpickle_global(*key)2315 _extension_cache[code] = obj2316 return obj2317def unpickle_appends(lst, vals):2318 r"""2319 Given a list (or list-like object) and a sequence of values, appends the2320 values to the end of the list. This is careful to do so using the2321 exact same technique that cPickle would use. Used by ``explain_pickle``.2322 EXAMPLES::2323 sage: v = []2324 sage: unpickle_appends(v, (1, 2, 3))2325 sage: v2326 [1, 2, 3]2327 """2328 if isinstance(lst, list):2329 # If lst is a list (or a subtype of list)2330 list.extend(lst, vals)2331 else:2332 append = lst.append2333 for v in vals:2334 append(v)2335def test_pickle(p, verbose_eval=False, pedantic=False, args=()):2336 r"""2337 Tests explain_pickle on a given pickle p. p can be:2338 - a string containing an uncompressed pickle (which will always end 2339 with a '.')2340 - a string containing a pickle fragment (not ending with '.')2341 test_pickle will synthesize a pickle that will push args onto 2342 the stack (using persistent IDs), run the pickle fragment, and then2343 STOP (if the string 'mark' occurs in args, then a mark will be pushed)2344 - an arbitrary object; test_pickle will pickle the object2345 Once it has a pickle, test_pickle will print the pickle's2346 disassembly, run explain_pickle with in_current_sage=True and2347 False, print the results, evaluate the results, unpickle the2348 object with cPickle, and compare all three results.2349 If verbose_eval is True, then test_pickle will print messages2350 before evaluating the pickles; this is to allow for tests where2351 the unpickling prints messages (to verify that the same operations2352 occur in all cases).2353 EXAMPLES::2354 sage: from sage.misc.explain_pickle import *2355 sage: test_pickle(['a'])2356 0: \x80 PROTO 22357 2: ] EMPTY_LIST2358 3: q BINPUT 12359 5: U SHORT_BINSTRING 'a'2360 8: a APPEND2361 9: . STOP2362 highest protocol among opcodes = 22363 explain_pickle in_current_sage=True/False:2364 ['a']2365 result: ['a']2366 """2367 start = ''2368 for n in range(len(args)):2369 a = args[n]...

Full Screen

Full Screen

test_pickle_serializer.py

Source:test_pickle_serializer.py Github

copy

Full Screen

1import unittest2from os import path, curdir3from serializer.serializers.pickle_serializer import PickleSerializer4from test_data import data, get_answer_files, get_test_files5class PickleSerializerTestCase(unittest.TestCase):6 def setUp(self):7 self.test_pickle_ser = PickleSerializer()8 self.path_dir = path.join(9 path.abspath(curdir),10 'test_answers',11 'test_pickle'12 )13 self.data = data14 self.answer_files = get_answer_files('pickle')15 self.pickle_files = get_test_files('pickle')16 def test_dump(self):17 for value, test_pickle in zip(18 self.data,19 self.pickle_files):20 self.test_pickle_ser.dump(21 value, path.join(self.path_dir, test_pickle))22 self.assertIsNotNone(23 path.getsize(path.join(self.path_dir, test_pickle))24 )25 self.assertIsNone(26 self.test_pickle_ser.dump(None, self.data[:-1])27 )28 def test_dumps(self):29 for value, test_pickle in zip(self.data[:-4],30 self.pickle_files[:-4]):31 with open(path.join(32 self.path_dir, test_pickle), 'rb') as rf:33 answer = rf.read()34 self.assertEqual(35 self.test_pickle_ser.dumps(value), answer36 )37 self.assertIsNone(38 self.test_pickle_ser.dump(self.data[:-1], self.data[:-1])39 )40 def test_load(self):41 for value, test_pickle in zip(self.data[:-4],42 self.pickle_files[:-4]):43 self.assertEqual(self.test_pickle_ser.load(44 path.join(self.path_dir, test_pickle)), value)45 func = self.test_pickle_ser.load(path.join(46 self.path_dir, self.pickle_files[4]),47 convert=True)48 self.assertEqual(type(func), dict)49 simple_class = self.test_pickle_ser.load(path.join(50 self.path_dir, self.pickle_files[5]))51 self.assertEqual(type(simple_class), type)52 complex_class = self.test_pickle_ser.load(path.join(53 self.path_dir, self.pickle_files[6]))54 self.assertTrue(hasattr(complex_class, 'func_in_class'))55 complex_object = self.test_pickle_ser.load(path.join(56 self.path_dir, self.pickle_files[7]))57 self.assertEqual(complex_object.other.string, 'string')58 self.assertIsNone(self.test_pickle_ser.load('incorrect_path'))59 def test_loads(self):60 for value, test_pickle in zip(self.data[:-4],61 self.pickle_files[:-4]):62 with open(path.join(63 self.path_dir,64 test_pickle), 'rb') as rf:65 data_ = rf.read()66 self.assertEqual(67 self.test_pickle_ser.loads(data_), value68 )69 def get_data():70 for data_file in self.pickle_files[4:]:71 with open(path.join(72 self.path_dir,73 data_file), 'rb') as rf:74 data_ = rf.read()75 yield data_76 data_ = get_data()77 func = self.test_pickle_ser.loads(next(data_))78 self.assertEqual(func(12), 101)79 simple_class = self.test_pickle_ser.loads(next(data_))80 self.assertEqual(type(simple_class), type)81 complex_class = self.test_pickle_ser.loads(next(data_))82 self.assertTrue(hasattr(complex_class, 'func_in_class'))83 complex_object = self.test_pickle_ser.loads(next(data_))84 self.assertEqual(complex_object.other.string, 'string')85 self.assertIsNone(self.test_pickle_ser.loads('bad_data'))86if __name__ == "__main__":...

Full Screen

Full Screen

test_sva_pickle.py

Source:test_sva_pickle.py Github

copy

Full Screen

...16 e3.Vector3d.Random())17 rb = sva.RBInertiad(3., e3.Vector3d.Random(), e3.Matrix3d.Random())18 ab = sva.ABInertiad(e3.Matrix3d.Random(), e3.Matrix3d.Random(),19 e3.Matrix3d.Random())20 def test_pickle(v):21 pickled = pickle.dumps(v)22 v2 = pickle.loads(pickled)23 self.assertEqual(v, v2)24 test_pickle(mv)25 test_pickle(fv)26 test_pickle(pt)27 test_pickle(rb)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful