def verify_record(oid, data, debug=False): input_file = io.BytesIO(data) unpickler = PersistentUnpickler(None, persistent_load, input_file) class_info = "unknown" pos = None try: class_info = unpickler.load() pos = input_file.tell() unpickler.load() except Exception as e: input_file.seek(0) pickle = input_file.read() logger.info("\nCould not process {} record {} ({!r}):".format( class_info, oid_repr(oid), oid)) logger.info(repr(pickle)) logger.info(traceback.format_exc()) if debug and pos is not None: try: pickletools.dis(pickle[pos:]) except Exception: # ignore exceptions while disassembling the pickle since the # real issue is that it references a unavailable module pass finally: pdb.set_trace() elif debug and pos is None: pdb.set_trace() # The same issues should have the same msg msg = "{}: {}".format(e.__class__.__name__, str(e)) return False, msg return True, None
def main(): from sys import argv if '--pickle' in argv: soft = stackless.enable_softswitch( False) # no crash, if soft switching try: p = pickle_current_frame() finally: stackless.enable_softswitch(soft) p = pickletools.optimize(p) print('Pickle as bytes: ', repr(p)) else: if bytes is str: # pickle created with Stackless v2.7.6r3, hg commt id 67088aa2da77 p = b'\x80\x02c_stackless._wrap\nframe\nc_stackless._wrap\ncode\nq\x01(K\x00K\x01K\x03J\x03`\x03\x00U?g\x00\x00\x89\x00\x00\x87\x00\x00f\x01\x00d\x01\x00\x86\x00\x00}\x00\x00t\x00\x00j\x01\x00\x83\x00\x00j\x02\x00|\x00\x00t\x00\x00j\x03\x00f\x01\x00\x83\x02\x00j\x04\x00\x83\x00\x00\x01\x88\x00\x00d\x02\x00\x19SNh\x01(K\x01K\x01K\x04J\x13`\x03\x00U \x88\x00\x00j\x00\x00t\x01\x00j\x02\x00|\x00\x00j\x03\x00d\x01\x00\x83\x02\x00\x83\x01\x00\x01d\x00\x00SNJ\xff\xff\xff\xff\x86(U\x06appendU\x06pickleU\x05dumpsU\x05frametU\x07currentq\n\x85U)Stackless/test/unpickle_crash_ticket61.pyU\x04funcq\rK\x12U\x02\x00\x01U\x06resultq\x0f\x85)tR)bK\x00\x87(U\tstacklessU\x07taskletU\x04bindh\nU\x03runth\r\x85U)Stackless/test/unpickle_crash_ticket61.pyU\x14pickle_current_frameK\x0fU\x08\x00\x01\x06\x02\x0f\x02"\x01)h\x0f\x85tRq\x1f)b\x85R(h\x1fK\x00U\x10eval_frame_valuec__builtin__\ngetattr\nc__builtin__\n__import__\n(U\x08__main__))U\x00\x85tRU\x08__dict__\x86RK\x00}NNK3K\x0f)Ntb.' else: # pickle created with Stackless v3.3.5 p = b'\x80\x03c_stackless._wrap\nframe\nc_stackless._wrap\ncode\nq\x01(K\x00K\x00K\x01K\x03K\x03CBg\x00\x00\x89\x00\x00\x87\x00\x00f\x01\x00d\x01\x00d\x02\x00\x86\x00\x00}\x00\x00t\x00\x00j\x01\x00\x83\x00\x00j\x02\x00|\x00\x00t\x00\x00j\x03\x00f\x01\x00\x83\x02\x00j\x04\x00\x83\x00\x00\x01\x88\x00\x00d\x03\x00\x19S(Nh\x01(K\x01K\x00K\x01K\x04K\x13C \x88\x00\x00j\x00\x00t\x01\x00j\x02\x00|\x00\x00j\x03\x00d\x02\x00\x83\x02\x00\x83\x01\x00\x01d\x00\x00SNK\x01J\xff\xff\xff\xff\x87(X\x06\x00\x00\x00appendX\x06\x00\x00\x00pickleX\x05\x00\x00\x00dumpsX\x05\x00\x00\x00frametX\x07\x00\x00\x00currentq\n\x85X)\x00\x00\x00Stackless/test/unpickle_crash_ticket61.pyq\x0cX\x04\x00\x00\x00funcq\rK\x0fC\x02\x00\x01X\x06\x00\x00\x00resultq\x0f\x85)tR)bX"\x00\x00\x00pickle_current_frame.<locals>.funcK\x00t(X\t\x00\x00\x00stacklessX\x07\x00\x00\x00taskletX\x04\x00\x00\x00bindh\nX\x03\x00\x00\x00runth\r\x85h\x0cX\x14\x00\x00\x00pickle_current_frameK\x0cC\x08\x00\x01\x06\x02\x12\x02"\x01)h\x0f\x85tRq\x1f)b\x85R(h\x1fK\x00X\x10\x00\x00\x00eval_frame_valuecbuiltins\ngetattr\ncimportlib\nimport_module\nX\x08\x00\x00\x00__main__\x85RX\x08\x00\x00\x00__dict__\x86RK\x00}NNK6K\x0c)Ntb.' if '--dis' in argv: pickletools.dis(p) else: frame = pickle.loads(p) frame.f_locals # this line crashes Stackless print("No Crash, OK")
def DiffNamedPickles(named_pickles1, named_pickles2): """Diff two lists of (name, pickled_module).""" len1, len2 = len(named_pickles1), len(named_pickles2) if len1 != len2: return ["different number of pyi files: %d, %d" % (len1, len2)] diff = [] for (name1, pickle1), (name2, pickle2) in zip(named_pickles1, named_pickles2): if name1 != name2: diff.append("different ordering of pyi files: %s, %s" % (name1, name2)) elif pickle1 != pickle2: ast1, ast2 = cPickle.loads(pickle1), cPickle.loads(pickle2) if ASTeq(ast1.ast, ast2.ast): diff.append("asts match but pickles differ: %s" % name1) p1 = six.StringIO() p2 = six.StringIO() pickletools.dis(pickle1, out=p1) pickletools.dis(pickle2, out=p2) diff.extend(difflib.unified_diff( p1.getvalue().splitlines(), p2.getvalue().splitlines())) else: diff.append("asts differ: %s" % name1) diff.append("-" * 50) diff.extend(ASTdiff(ast1.ast, ast2.ast)) diff.append("-" * 50) return diff
def dont_test_disassembly(self): from pickletools import dis for proto, expected in (0, DATA0_DIS), (1, DATA1_DIS): s = self.dumps(self._testdata, proto) filelike = cStringIO.StringIO() dis(s, out=filelike) got = filelike.getvalue() self.assertEqual(expected, got)
def dumps(cls, obj, _rop, *args, **kwargs): buf = BytesIO() cls(buf, *args, _rop=_rop, **kwargs).dump(obj) if logger.getEffectiveLevel() == 1: buf.seek(0) dis_log = StringIO() dis(buf, dis_log) logger.debug(dis_log.getvalue()) return buf.getvalue()
def inspect(): import my_pickle as pickle import pickletools p1 = Person('Guido van Rossum') pickled = pickle.dumps(p1, protocol=0) pickled = pickletools.optimize(pickled) print(str(pickled)[2:-2].replace('\\n', '\n')) pickletools.dis(pickled) p2 = pickle.loads(pickled) return p2
def run(args): expr = eval(args.expression) protocol = int(args.protocol) if args.output: with open(args.output, 'wb') as fh: pickle.dump(expr, fh, protocol=protocol) else: print repr(pickle.dumps(expr, protocol=protocol)) if not args.output and args.disassembly: d = pickle.dumps(expr, protocol=protocol) pickletools.dis(d)
def serialize(x, fname): import pickle import pickletools print("Pickling: {} {}:".format(x, type(x))) # Create Instance and Pickle it: payload = pickle.dumps(x, protocol=0) # Lowest Protocol is Human-readable # Disassemble Pickle Payload: pickletools.dis(payload) with open(fname, "wb") as file: file.write(payload)
def log_insert(self): log2 = pickle.dumps(var_logging(self.window._MainWindow__modules), 0) log1 = pickle.dumps(var_logging(self.window.frame_map.aoi), 0) old_stdout = sys.stdout # This variable will store everything that is sent to the standard output result = StringIO() sys.stdout = result pickletools.dis(log2) pickletools.dis(log1) sys.stdout = old_stdout result_string = result.getvalue() self.log_box.delete("1.0", tkinter.END) self.log_box.insert(tkinter.INSERT, result_string)
def getDisassembledPickleData(self): self.debug_mark('- rendering pickle') pickle = BytesIO(self.state.pickledState) out = StringIO() memo = {} # 1st pickle: the class try: pickletools.dis(pickle, out, memo) except Exception as e: out.write(''.join(traceback.format_exception_only(type(e), e))) # 2st pickle: actual instance data out.write('\n') try: pickletools.dis(pickle, out, memo) except Exception as e: out.write(''.join(traceback.format_exception_only(type(e), e))) return out.getvalue()
def verify_record(oid, data, debug=False): input_file = io.BytesIO(data) unpickler = PersistentUnpickler(None, persistent_load, input_file) class_info = 'unknown' try: class_info = unpickler.load() pos = input_file.tell() unpickler.load() except Exception: input_file.seek(0) pickle = input_file.read() logger.info('\nCould not process {} record {}:'.format( class_info, repr(oid), )) logger.info(repr(pickle)) logger.info(traceback.format_exc()) if debug: try: pickletools.dis(pickle[pos:]) finally: pdb.set_trace() return False return True
def main(): from sys import argv if '--pickle' in argv: soft = stackless.enable_softswitch(False) # no crash, if soft switching try: p = pickle_current_frame() finally: stackless.enable_softswitch(soft) p = pickletools.optimize(p) print('Pickle as bytes: ', repr(p)) else: if bytes is str: # pickle created with Stackless v2.7.6r3, hg commt id 67088aa2da77 p = b'\x80\x02c_stackless._wrap\nframe\nc_stackless._wrap\ncode\nq\x01(K\x00K\x01K\x03J\x03`\x03\x00U?g\x00\x00\x89\x00\x00\x87\x00\x00f\x01\x00d\x01\x00\x86\x00\x00}\x00\x00t\x00\x00j\x01\x00\x83\x00\x00j\x02\x00|\x00\x00t\x00\x00j\x03\x00f\x01\x00\x83\x02\x00j\x04\x00\x83\x00\x00\x01\x88\x00\x00d\x02\x00\x19SNh\x01(K\x01K\x01K\x04J\x13`\x03\x00U \x88\x00\x00j\x00\x00t\x01\x00j\x02\x00|\x00\x00j\x03\x00d\x01\x00\x83\x02\x00\x83\x01\x00\x01d\x00\x00SNJ\xff\xff\xff\xff\x86(U\x06appendU\x06pickleU\x05dumpsU\x05frametU\x07currentq\n\x85U)Stackless/test/unpickle_crash_ticket61.pyU\x04funcq\rK\x12U\x02\x00\x01U\x06resultq\x0f\x85)tR)bK\x00\x87(U\tstacklessU\x07taskletU\x04bindh\nU\x03runth\r\x85U)Stackless/test/unpickle_crash_ticket61.pyU\x14pickle_current_frameK\x0fU\x08\x00\x01\x06\x02\x0f\x02"\x01)h\x0f\x85tRq\x1f)b\x85R(h\x1fK\x00U\x10eval_frame_valuec__builtin__\ngetattr\nc__builtin__\n__import__\n(U\x08__main__))U\x00\x85tRU\x08__dict__\x86RK\x00}NNK3K\x0f)Ntb.' else: # pickle created with Stackless v3.3.5 p = b'\x80\x03c_stackless._wrap\nframe\nc_stackless._wrap\ncode\nq\x01(K\x00K\x00K\x01K\x03K\x03CBg\x00\x00\x89\x00\x00\x87\x00\x00f\x01\x00d\x01\x00d\x02\x00\x86\x00\x00}\x00\x00t\x00\x00j\x01\x00\x83\x00\x00j\x02\x00|\x00\x00t\x00\x00j\x03\x00f\x01\x00\x83\x02\x00j\x04\x00\x83\x00\x00\x01\x88\x00\x00d\x03\x00\x19S(Nh\x01(K\x01K\x00K\x01K\x04K\x13C \x88\x00\x00j\x00\x00t\x01\x00j\x02\x00|\x00\x00j\x03\x00d\x02\x00\x83\x02\x00\x83\x01\x00\x01d\x00\x00SNK\x01J\xff\xff\xff\xff\x87(X\x06\x00\x00\x00appendX\x06\x00\x00\x00pickleX\x05\x00\x00\x00dumpsX\x05\x00\x00\x00frametX\x07\x00\x00\x00currentq\n\x85X)\x00\x00\x00Stackless/test/unpickle_crash_ticket61.pyq\x0cX\x04\x00\x00\x00funcq\rK\x0fC\x02\x00\x01X\x06\x00\x00\x00resultq\x0f\x85)tR)bX"\x00\x00\x00pickle_current_frame.<locals>.funcK\x00t(X\t\x00\x00\x00stacklessX\x07\x00\x00\x00taskletX\x04\x00\x00\x00bindh\nX\x03\x00\x00\x00runth\r\x85h\x0cX\x14\x00\x00\x00pickle_current_frameK\x0cC\x08\x00\x01\x06\x02\x12\x02"\x01)h\x0f\x85tRq\x1f)b\x85R(h\x1fK\x00X\x10\x00\x00\x00eval_frame_valuecbuiltins\ngetattr\ncimportlib\nimport_module\nX\x08\x00\x00\x00__main__\x85RX\x08\x00\x00\x00__dict__\x86RK\x00}NNK6K\x0c)Ntb.' if '--dis' in argv: pickletools.dis(p) else: frame = pickle.loads(p) frame.f_locals # this line crashes Stackless print("No Crash, OK")
import serialization import pickle #print (serialization.book_entry) #WITH clause ensures that the file gets closed when the block inside the WITH clause executes.It doesn’t require to call the close() method explicitly with open('book_entry.pickle', 'wb') as bin_file: pickle.dump(serialization.book_entry, bin_file) print('\n\033[1m{:s} file is serialzied successfully and wriiten in disk'. format(bin_file.name)) with open('book_entry.pickle', 'rb') as bin_file: book_entry = pickle.load(bin_file) print('\n\033[1m original python data structure is {}'.format(book_entry)) #print (book_entry['published_date']) #Examine pickle file #Example 2: #The most interesting piece of information in that disassembly is on the last line, because it includes the version of the pickle protocol with which this file was saved. There is no explicit version marker in the pickle protocol. import pickletools with open('book_entry.pickle', 'rb') as bin_file: pickletools.dis(bin_file)
with open('entry.pickle', 'rb') as f: entry1 = pickle.load(f) print(entry1) with open('entry.pickle', 'rb') as f: entry2 = pickle.load(f) print(entry == entry1 == entry2) print(entry is entry2) b = pickle.dumps(entry) print(type(b)) entry3 = pickle.loads(b) print(entry == entry3) import pickletools with open('entry.pickle', 'rb') as f: print(pickletools.dis(f)) basic_entry = {} basic_entry['id'] = 256 basic_entry['title'] = 'Dive into history, 2009 edition' basic_entry['tags'] = ('diveintopython', 'docbook', 'html') basic_entry['published'] = True basic_entry['comments_link'] = None import json with open('basic.json', mode='w', encoding='utf-8') as f: json.dump(basic_entry, f, indent=2) with open('basic.json', mode='r', encoding='utf-8') as f: basic = json.load(f) print(basic)
def update_event(self, inp=-1): self.set_output_val( 0, pickletools.dis(self.input(0), self.input(1), self.input(2), self.input(3), self.input(4)))
if len(sys.argv) != 3: print 'Missing arguments (load/save, <filename>)' sys.exit(0) if sys.argv[1] == 'load': loadFile = sys.argv[2] #loadFile += '.pickle' print 'Opening', loadFile elif sys.argv[1] == 'save': saveFile = sys.argv[2] #saveFile += '.pickle' print 'Saving to', saveFile elif sys.argv[1] == 'debug': print 'Debugging:', sys.argv[2] debug_file = file(sys.argv[2]) pickletools.dis(debug_file) sys.exit(0) else: print 'Unknown command. Arguments are (load/save, <filename>)' if loadFile != '': print 'Loading...' pickle_file = file(loadFile) meta = pickle.load(pickle_file) else: meta = Process('root') meta.AddSubProcess('Huvudprocess0', 'h') meta.AddSubProcess('Huvudprocess1', 'h')
pos = f.tell() self.assertEqual(unpickler.load(), data1) if f.seekable(): self.assertEqual(f.tell(), pos + len(pickled)) self.assertRaises(EOFError, unpickler.load) def test_multiple_unpicklings_seekable(self): self._check_multiple_unpicklings(io.BytesIO) def test_multiple_unpicklings_unseekable(self): self._check_multiple_unpicklings(UnseekableIO) if __name__ == "__main__": # Print some stuff that can be used to rewrite DATA{0,1,2} from pickletools import dis x = create_data() for i in range(3): p = pickle.dumps(x, i) print("DATA{0} = (".format(i)) for j in range(0, len(p), 20): b = bytes(p[j:j+20]) print(" {0!r}".format(b)) print(")") print() print("# Disassembly of DATA{0}".format(i)) print("DATA{0}_DIS = \"\"\"\\".format(i)) dis(p) print("\"\"\"") print()
#!/usr/bin/python import pickletools f = open('test.pickle', 'r') print pickletools.dis(f.read())
import pickletools from file_path_collect import output_pickle_path_dir as output with open(output + 'entry.pickle', 'rb') as f: pickletools.dis(f) """ 0: \x80 PROTO 3 2: } EMPTY_DICT 3: q BINPUT 0 5: ( MARK 6: X BINUNICODE 'title' 16: q BINPUT 1 18: X BINUNICODE 'Divve into history, 2009 edition' 55: q BINPUT 2 57: X BINUNICODE 'article_link' 74: q BINPUT 3 76: X BINUNICODE 'http://diveintomark.org/archives/2009/03/27/dive‐into‐history‐2009‐edition' 163: q BINPUT 4 165: X BINUNICODE 'comments_link' 183: q BINPUT 5 185: N NONE 186: X BINUNICODE 'internal_id' 202: q BINPUT 6 204: C SHORT_BINBYTES b'\xde\xd5\xb4\xf8' 210: q BINPUT 7 212: X BINUNICODE 'tags' 221: q BINPUT 8 223: X BINUNICODE 'diveintopython' 242: q BINPUT 9 244: X BINUNICODE 'docbook'
def _dump_file(self, f): f.seek(0, io.SEEK_SET) pickletools.dis(f)
""" Module to demonstrate pickle library usage in python. """ import pickle import pickletools GRADES = {"Alice": 89, "Bob": 72, "Charles": 87} # Use dumps to convert the object to a serialized string SERIAL_GRADES = pickle.dumps(GRADES) print(SERIAL_GRADES) pickletools.dis(SERIAL_GRADES) # Use loads to de-serialize an object RECEIVED_GRADES = pickle.loads(SERIAL_GRADES) assert GRADES == RECEIVED_GRADES SERIAL_GRADES = pickle.dumps(GRADES, protocol=pickle.HIGHEST_PROTOCOL) print(SERIAL_GRADES) # Use loads to de-serialize an object RECEIVED_GRADES = pickle.loads(SERIAL_GRADES) assert GRADES == RECEIVED_GRADES
# 此模块包含与 pickle 模块内部细节有关的多个常量,一些关于具体实现的详细注释,以及一些能够分析封存数据的有用函数。 # 此模块的内容对需要操作 pickle 的 Python 核心开发者来说很有用处; # https://docs.python.org/zh-cn/3/library/pickletools.html import pickle import pickletools class Foo: attr = 'A class attribute' picklestring = pickle.dumps(Foo) pickletools.dis(picklestring)
def zodbdump(stor, tidmin, tidmax, hashonly=False, pretty='raw', out=asbinstream(sys.stdout)): def badpretty(): raise ValueError("invalid pretty format %s" % pretty) for txn in stor.iterator(tidmin, tidmax): # XXX .status not covered by IStorageTransactionInformation # XXX but covered by BaseStorage.TransactionRecord out.write(b"txn %s %s\nuser %s\ndescription %s\n" % (ashex( txn.tid), qq(txn.status), qq(txn.user), qq(txn.description))) # extension is saved by ZODB as either empty or as pickle dump of an object rawext = txn_raw_extension(stor, txn) if pretty == 'raw': out.write(b"extension %s\n" % qq(rawext)) elif pretty == 'zpickledis': if len(rawext) == 0: out.write(b'extension ""\n') else: out.write(b"extension\n") extf = BytesIO(rawext) disf = BytesIO() pickletools.dis(extf, disf) out.write(indent(disf.getvalue(), " ")) extra = extf.read() if len(extra) > 0: out.write(b" + extra data %s\n" % qq(extra)) else: badpretty() objv = txnobjv(txn) for obj in objv: entry = b"obj %s " % ashex(obj.oid) write_data = False if obj.data is None: entry += b"delete" # was undo and data taken from obj.data_txn elif obj.data_txn is not None: entry += b"from %s" % ashex(obj.data_txn) else: # XXX sha1 is hardcoded for now. Dump format allows other hashes. entry += b"%i sha1:%s" % (len(obj.data), ashex(sha1(obj.data))) write_data = True out.write(b(entry)) if write_data: if hashonly: out.write(b" -") else: out.write(b"\n") if pretty == 'raw': out.write(obj.data) elif pretty == 'zpickledis': # https://github.com/zopefoundation/ZODB/blob/5.6.0-55-g1226c9d35/src/ZODB/serialize.py#L24-L29 dataf = BytesIO(obj.data) disf = BytesIO() pickletools.dis(dataf, disf) # class pickletools.dis(dataf, disf) # state out.write(indent(disf.getvalue(), " ")) extra = dataf.read() if len(extra) > 0: out.write(b" + extra data %s\n" % qq(extra)) else: badpretty() out.write(b"\n") out.write(b"\n")
now = int(time.time()) tuples = ([]) tuples.append( (unicode('mediaflux-1128.ping.size.bytes'), (now, unicode(sizeBytes)))) tuples.append( (unicode('mediaflux-1128.ping.speed.mbs'), (now, unicode(rate[0].value())))) tuples.append((unicode('mediaflux-1128.ping.read.' + readunits), (now, unicode(readtime)))) # Create package and send it to Carbon package = pickle.dumps(tuples, protocol=2) size = struct.pack('!L', len(package)) sock.sendall(size) sock.sendall(package) pickletools.dis(package) # asset.create # Create arguments for create asset test cAsset = mfclient.XmlStringWriter('args') cAsset.push("service", attributes={"name": "asset.create"}) cAsset.add("namespace", namespace) cAsset.add("action", "get-meta") cAsset.pop() cAsset.add("time", True) create = pingResults def createtest(): global create create = cxn.execute("service.execute", cAsset.doc_text(),
#!/usr/bin/env python import pickletools, sys pickletools.dis(open(sys.argv[1]))
p.dumps(decompiler, protocol), protocol), 9) with open(path.join(pack_folder, "un.rpyc"), "wb") as f: f.write(unrpyc) if args.debug: print("File length = {0}".format(len(unrpyc))) import pickletools data = zlib.decompress(unrpyc) with open(path.join(pack_folder, "un.dis"), "wb" if p.PY2 else "w") as f: pickletools.dis(data, f) for com, arg, _ in pickletools.genops(data): if arg and (isinstance(arg, str) or p.PY3 and isinstance(arg, bytes)) and len(arg) > 1000: if p.PY3 and isinstance(arg, str): arg = arg.encode("latin1") data = zlib.decompress(arg) break else: raise Exception("didn't find the gzipped blob inside") with open(path.join(pack_folder, "un.dis2"), "wb" if p.PY2 else "w") as f: pickletools.dis(data, f)
entry2 is entry entry2['internal_id'] ---- # shell 1 b = pickle.dumps(entry) type(b) entry3 = pickle.loads(b) entry3 == entry ---- # shell 1 import pickletools with open('entry.pickle', 'rb') as f: pickletools.dis(f) import pickletools def protocol_version(file_object): maxproto = -1 for opcode, arg, pos in pickletools.genops(file_object): maxproto = max(maxproto, opcode.proto) return maxproto import pickversion with open('entry.pickle', 'rb') as f: v = pickversion.protocol_version(f) v --- # to JSON
prevpos = None # set to pos if previous opcode was a PUT for opcode, arg, pos in genops(p): if prevpos is not None: puts.append((prevarg, prevpos, pos)) prevpos = None if 'PUT' in opcode.name: prevarg, prevpos = arg, pos elif 'GET' in opcode.name: gets.add(arg) # Copy the pickle string except for PUTS without a corresponding GET s = [] i = 0 for arg, start, stop in puts: j = stop if (arg in gets) else start s.append(p[i:j]) i = stop s.append(p[i:]) return ''.join(s) if __name__ == '__main__': from pickle import dumps from pickletools import dis p = dumps(['the', 'quick', 'brown', 'fox']) print 'Before:' dis(p) print '\nAfter:' dis(optimize(p))
pos = f.tell() self.assertEqual(unpickler.load(), data1) if f.seekable(): self.assertEqual(f.tell(), pos + len(pickled)) self.assertRaises(EOFError, unpickler.load) def test_multiple_unpicklings_seekable(self): self._check_multiple_unpicklings(io.BytesIO) def test_multiple_unpicklings_unseekable(self): self._check_multiple_unpicklings(UnseekableIO) if __name__ == "__main__": # Print some stuff that can be used to rewrite DATA{0,1,2} from pickletools import dis x = create_data() for i in range(3): p = pickle.dumps(x, i) print("DATA{0} = (".format(i)) for j in range(0, len(p), 20): b = bytes(p[j:j + 20]) print(" {0!r}".format(b)) print(")") print() print("# Disassembly of DATA{0}".format(i)) print("DATA{0}_DIS = \"\"\"\\".format(i)) dis(p) print("\"\"\"") print()
import pickletools class Animal: def __init__(self, name, category): self.name = name self.category = category def __repr__(self): return f'Animal(name={self.name!r}, category={self.category!r})' def __eq__(self, other): return type( other ) is Animal and self.name == other.name and self.category == other.category class IsTrue: def __eq__(self, other): return True def test(): return 233 x = pickle.dumps(Animal(favorite.name, favorite.category)) print(x) print(pickletools.dis(x)) print(base64.b64encode(x))
Module("unrpyc", path.join(pack_folder, "unrpyc-compile.py")))) unrpyc = zlib.compress(p.optimize(p.dumps(decompiler, protocol), protocol), 9) with open(path.join(pack_folder, "un.rpyc"), "wb") as f: f.write(unrpyc) if args.debug: print("File length = {0}".format(len(unrpyc))) import pickletools data = zlib.decompress(unrpyc) with open(path.join(pack_folder, "un.dis"), "wb" if p.PY2 else "w") as f: pickletools.dis(data, f) for com, arg, _ in pickletools.genops(data): if arg and (isinstance(arg, str) or p.PY3 and isinstance(arg, bytes)) and len(arg) > 1000: if p.PY3 and isinstance(arg, str): arg = arg.encode("latin1") data = zlib.decompress(arg) break else: raise Exception("didn't find the gzipped blob inside") with open(path.join(pack_folder, "un.dis2"), "wb" if p.PY2 else "w") as f: pickletools.dis(data, f)
import pickle import pickletools from datetime import datetime my_data = {"data": [ {"element": [datetime.now(), "aaa", 1.2, None]}, {"element": [datetime.now(), "ccc", 26, True]}, {"element": [datetime.now(), "bbb", 3.14, False]} ]} pickled_data = pickle.dumps(my_data) with open("class_wk8.pickle", "wb") as fh: pickle.dump(my_data, fh) with open("class_wk8.pickle", "rb") as fh: pickletools.dis(fh) print()
import pickle import pickletools class Foo: attr = "Some attribute" def __init__(self, name): self.name = name picklestring = pickle.dumps(Foo("boo")) print(picklestring) foo = pickle.loads(picklestring) print(foo.name) print(foo.attr) print(pickletools.dis(picklestring))