| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Usage:
 | 
| 4 | #   ./pickle.sh <function name>
 | 
| 5 | 
 | 
| 6 | set -o nounset
 | 
| 7 | set -o pipefail
 | 
| 8 | set -o errexit
 | 
| 9 | 
 | 
| 10 | #
 | 
| 11 | # Protocol TWO
 | 
| 12 | #
 | 
| 13 | 
 | 
| 14 | # Opcodes for pure data.  BIN is for"binary"
 | 
| 15 | 
 | 
| 16 | # PROTO 2  # Say what protocol we're using
 | 
| 17 | 
 | 
| 18 | # EMPTY_DICT
 | 
| 19 | # EMPTY_LIST
 | 
| 20 | # NEWTRUE
 | 
| 21 | # BIN FLOAT
 | 
| 22 | # BIN INT1
 | 
| 23 | # BIN UNICODE -- don't like this, but OK
 | 
| 24 | 
 | 
| 25 | # SET ITEMS   # Dict
 | 
| 26 | # APPENDS     # List
 | 
| 27 | 
 | 
| 28 | # MARK
 | 
| 29 | # BIN PUT index
 | 
| 30 | # BIN GET index
 | 
| 31 | 
 | 
| 32 | # STOP
 | 
| 33 | 
 | 
| 34 | 
 | 
| 35 | # Other:
 | 
| 36 | 
 | 
| 37 | # GLOBAL     -- push self.find_class(mod_name, class_name)
 | 
| 38 | # NEWOBJ     # build object by applying cls.__new__ to argtuple
 | 
| 39 | #            # OK this still relies on __new__ fundamentally, not sure I want
 | 
| 40 |              #  it.
 | 
| 41 | # BINPUT     # store stack top in memo; index is string arg
 | 
| 42 | # BINGET     # push item from memo on stack; index is string arg
 | 
| 43 | # BININT2  -- 10043
 | 
| 44 | # SETITEM  -- calls __dict__.update() or __setstate__()
 | 
| 45 | # BUILD
 | 
| 46 | 
 | 
| 47 | 
 | 
| 48 | # NOTE: There is no INST bytecode.
 | 
| 49 | run() {
 | 
| 50 |   local kind=$1
 | 
| 51 | 
 | 
| 52 |   local p=_tmp/$kind.pickle
 | 
| 53 | 
 | 
| 54 |   for version in 0 1 2 3 4; do
 | 
| 55 |     demo/cpython/pickle_instance.py $kind $version $p
 | 
| 56 | 
 | 
| 57 |     python3 -m pickletools $p
 | 
| 58 | 
 | 
| 59 |     # Protocol 0 is viewable as plain text, but protocol 2 isn't
 | 
| 60 |     echo
 | 
| 61 |     echo "--- $p with PICKLE version $version ---"
 | 
| 62 |     echo
 | 
| 63 | 
 | 
| 64 |     od -c $p
 | 
| 65 |     ls -l $p
 | 
| 66 | 
 | 
| 67 |     echo
 | 
| 68 |     echo ---
 | 
| 69 |     echo
 | 
| 70 | 
 | 
| 71 |   done
 | 
| 72 | }
 | 
| 73 | 
 | 
| 74 | instance() {
 | 
| 75 |   run instance
 | 
| 76 | }
 | 
| 77 | 
 | 
| 78 | pure-data() {
 | 
| 79 |   run pure-data
 | 
| 80 | }
 | 
| 81 | 
 | 
| 82 | #
 | 
| 83 | # Protocol ZERO
 | 
| 84 | #
 | 
| 85 | 
 | 
| 86 | # The pickle VM has a stack and a memo dictionary.
 | 
| 87 | #
 | 
| 88 | # Oh and it also uses copy_reg and _reconstructor
 | 
| 89 | # OK so what I'm missing is __new__!  I didn't know about that.
 | 
| 90 | # It creates an object without initializing fields.  OK.
 | 
| 91 | # Not sure I want that in OVM2.
 | 
| 92 | 
 | 
| 93 | # def _reconstructor(cls, base, state):
 | 
| 94 | #     if base is object:
 | 
| 95 | #         obj = object.__new__(cls)
 | 
| 96 | #     else:
 | 
| 97 | #         obj = base.__new__(cls, state)
 | 
| 98 | #         if base.__init__ != object.__init__:
 | 
| 99 | #             base.__init__(obj, state)
 | 
| 100 | #     return obj
 | 
| 101 | 
 | 
| 102 | 
 | 
| 103 | # opcodes used:
 | 
| 104 | # GLOBAL
 | 
| 105 | # PUT      # store stack top in memo
 | 
| 106 | # MARK     # set a mark
 | 
| 107 | # REDUCE   # apply a callable
 | 
| 108 | # SETITEM  # add key-value pair to dict
 | 
| 109 | # BUILD    # call __dict__.update()  (or __setstate__)
 | 
| 110 | 
 | 
| 111 | # TUPLE
 | 
| 112 | # DICT
 | 
| 113 | # INT
 | 
| 114 | # NONE
 | 
| 115 | # STRING
 | 
| 116 | 
 | 
| 117 | 
 | 
| 118 | "$@"
 |