def encode(*args): # args: (msgid, flags, name, args) # (We used to have a global pickler, but that's not thread-safe. :-( ) # It's not thread safe if, in the couse of pickling, we call the # Python interpeter, which releases the GIL. # Note that args may contain very large binary pickles already; for # this reason, it's important to use proto 1 (or higher) pickles here # too. For a long time, this used proto 0 pickles, and that can # bloat our pickle to 4x the size (due to high-bit and control bytes # being represented by \xij escapes in proto 0). # Undocumented: cPickle.Pickler accepts a lone protocol argument; # pickle.py does not. if PY3: # XXX: Py3: Needs optimization. f = BytesIO() pickler = Pickler(f, 3) pickler.fast = 1 pickler.dump(args) res = f.getvalue() return res else: pickler = Pickler(1) pickler.fast = 1 return pickler.dump(args, 1)
def __str__(self): f = BytesIO() self.dump(f) return f.getvalue().decode()