Skip to content

Classes fail to pickle in Python 3 when super is referenced when dumping session #300

@charlesccychen

Description

@charlesccychen

This is a problematic scenario on Python 3 (tested to be failing with Python 3.5 and 3.6). The following code dumps the main session:

import dill

class A(object):
	pass

class B(A):
	def __init__(self):
		super(B, self).__init__()

dill.dump_session('test4.dump')

When this is loaded, it causes an error:

root@c3690280664a:/# python -c 'import dill; dill.load_session("test4.dump")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/dill/_dill.py", line 410, in load_session
    module = unpickler.load()
  File "/usr/local/lib/python3.6/site-packages/dill/_dill.py", line 474, in find_class
    return StockUnpickler.find_class(self, module, name)
AttributeError: Can't get attribute 'B' on <module '__main__' (built-in)>

For reference, this is the disassembled pickle output:

root@c3690280664a:/# python -c 'import pickletools; pickletools.dis(open("test4.dump", "rb").read())'
    0: \x80 PROTO      3
    2: c    GLOBAL     'dill._dill _import_module'
   29: q    BINPUT     0
   31: X    BINUNICODE '__main__'
   44: q    BINPUT     1
   46: \x85 TUPLE1
   47: q    BINPUT     2
   49: R    REDUCE
   50: q    BINPUT     3
   52: }    EMPTY_DICT
   53: q    BINPUT     4
   55: (    MARK
   56: X        BINUNICODE '__name__'
   69: q        BINPUT     5
   71: h        BINGET     1
   73: X        BINUNICODE '__doc__'
   85: q        BINPUT     6
   87: N        NONE
   88: X        BINUNICODE '__package__'
  104: q        BINPUT     7
  106: N        NONE
  107: X        BINUNICODE '__spec__'
  120: q        BINPUT     8
  122: N        NONE
  123: X        BINUNICODE '__annotations__'
  143: q        BINPUT     9
  145: }        EMPTY_DICT
  146: q        BINPUT     10
  148: X        BINUNICODE '__file__'
  161: q        BINPUT     11
  163: X        BINUNICODE '1.py'
  172: q        BINPUT     12
  174: X        BINUNICODE '__cached__'
  189: q        BINPUT     13
  191: N        NONE
  192: X        BINUNICODE 'dill'
  201: q        BINPUT     14
  203: h        BINGET     0
  205: h        BINGET     14
  207: \x85     TUPLE1
  208: q        BINPUT     15
  210: R        REDUCE
  211: q        BINPUT     16
  213: X        BINUNICODE 'A'
  219: q        BINPUT     17
  221: c        GLOBAL     'dill._dill _create_type'
  246: q        BINPUT     18
  248: (        MARK
  249: c            GLOBAL     'dill._dill _load_type'
  272: q            BINPUT     19
  274: X            BINUNICODE 'type'
  283: q            BINPUT     20
  285: \x85         TUPLE1
  286: q            BINPUT     21
  288: R            REDUCE
  289: q            BINPUT     22
  291: h            BINGET     17
  293: h            BINGET     19
  295: X            BINUNICODE 'object'
  306: q            BINPUT     23
  308: \x85         TUPLE1
  309: q            BINPUT     24
  311: R            REDUCE
  312: q            BINPUT     25
  314: \x85         TUPLE1
  315: q            BINPUT     26
  317: }            EMPTY_DICT
  318: q            BINPUT     27
  320: (            MARK
  321: X                BINUNICODE '__module__'
  336: q                BINPUT     28
  338: h                BINGET     1
  340: h                BINGET     6
  342: N                NONE
  343: u                SETITEMS   (MARK at 320)
  344: t            TUPLE      (MARK at 248)
  345: q        BINPUT     29
  347: R        REDUCE
  348: q        BINPUT     30
  350: X        BINUNICODE 'B'
  356: q        BINPUT     31
  358: h        BINGET     18
  360: (        MARK
  361: h            BINGET     22
  363: h            BINGET     31
  365: h            BINGET     30
  367: \x85         TUPLE1
  368: q            BINPUT     32
  370: }            EMPTY_DICT
  371: q            BINPUT     33
  373: (            MARK
  374: h                BINGET     28
  376: h                BINGET     1
  378: X                BINUNICODE '__init__'
  391: q                BINPUT     34
  393: c                GLOBAL     'dill._dill _create_function'
  422: q                BINPUT     35
  424: (                MARK
  425: h                    BINGET     19
  427: X                    BINUNICODE 'CodeType'
  440: q                    BINPUT     36
  442: \x85                 TUPLE1
  443: q                    BINPUT     37
  445: R                    REDUCE
  446: q                    BINPUT     38
  448: (                    MARK
  449: K                        BININT1    1
  451: K                        BININT1    0
  453: K                        BININT1    1
  455: K                        BININT1    3
  457: K                        BININT1    3
  459: C                        SHORT_BINBYTES b't\x00t\x01|\x00\x83\x02j\x02\x83\x00\x01\x00d\x00S\x00'
  479: q                        BINPUT     39
  481: N                        NONE
  482: \x85                     TUPLE1
  483: q                        BINPUT     40
  485: X                        BINUNICODE 'super'
  495: q                        BINPUT     41
  497: h                        BINGET     31
  499: h                        BINGET     34
  501: \x87                     TUPLE3
  502: q                        BINPUT     42
  504: X                        BINUNICODE 'self'
  513: q                        BINPUT     43
  515: \x85                     TUPLE1
  516: q                        BINPUT     44
  518: X                        BINUNICODE '1.py'
  527: q                        BINPUT     45
  529: h                        BINGET     34
  531: K                        BININT1    6
  533: C                        SHORT_BINBYTES b'\x00\x01'
  537: q                        BINPUT     46
  539: X                        BINUNICODE '__class__'
  553: q                        BINPUT     47
  555: \x85                     TUPLE1
  556: q                        BINPUT     48
  558: )                        EMPTY_TUPLE
  559: t                        TUPLE      (MARK at 448)
  560: q                    BINPUT     49
  562: R                    REDUCE
  563: q                    BINPUT     50
  565: c                    GLOBAL     '__builtin__ __main__'
  587: h                    BINGET     34
  589: N                    NONE
  590: c                    GLOBAL     'dill._dill _create_cell'
  615: q                    BINPUT     51
  617: c                    GLOBAL     '__main__ B'
  629: q                    BINPUT     52
  631: \x85                 TUPLE1
  632: q                    BINPUT     53
  634: R                    REDUCE
  635: q                    BINPUT     54
  637: \x85                 TUPLE1
  638: q                    BINPUT     55
  640: }                    EMPTY_DICT
  641: q                    BINPUT     56
  643: t                    TUPLE      (MARK at 424)
  644: q                BINPUT     57
  646: R                REDUCE
  647: q                BINPUT     58
  649: h                BINGET     6
  651: N                NONE
  652: u                SETITEMS   (MARK at 373)
  653: t            TUPLE      (MARK at 360)
  654: q        BINPUT     59
  656: R        REDUCE
  657: 0        POP
  658: h        BINGET     52
  660: u        SETITEMS   (MARK at 55)
  661: b    BUILD
  662: .    STOP
highest protocol among opcodes = 3

CC: @aaltay, @tvalentyn, @markflyhigh

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions