Author serhiy.storchaka
Recipients gvanrossum, levkivskyi, serhiy.storchaka
Date 2018-02-19.10:46:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1519037205.03.0.467229070634.issue32873@psf.upfronthosting.co.za>
In-reply-to
Content
In 3.6 typing types are pickled by names:

>>> import pickle, pickletools, typing
>>> pickletools.optimize(pickle.dumps(typing.List))
b'\x80\x03ctyping\nList\n.'
>>> pickletools.dis(pickletools.optimize(pickle.dumps(typing.List)))
    0: \x80 PROTO      3
    2: c    GLOBAL     'typing List'
   15: .    STOP
highest protocol among opcodes = 2

The side effect of this is that they are considered atomic by the copy module.

In 3.7 the pickle data contains all private attributes.

>>> pickletools.optimize(pickle.dumps(typing.List))
b'\x80\x03ctyping\n_GenericAlias\n)\x81}(X\x05\x00\x00\x00_inst\x89X\x08\x00\x00\x00_special\x88X\x05\x00\x00\x00_nameX\x04\x00\x00\x00ListX\n\x00\x00\x00__origin__cbuiltins\nlist\nX\x08\x00\x00\x00__args__ctyping\nTypeVar\n)\x81q\x00}(X\x04\x00\x00\x00nameX\x01\x00\x00\x00TX\x05\x00\x00\x00boundNX\x0b\x00\x00\x00constraints)X\x02\x00\x00\x00co\x89X\x06\x00\x00\x00contra\x89ub\x85X\x0e\x00\x00\x00__parameters__h\x00\x85X\t\x00\x00\x00__slots__Nub.'
>>> pickletools.dis(pickletools.optimize(pickle.dumps(typing.List)))
    0: \x80 PROTO      3
    2: c    GLOBAL     'typing _GenericAlias'
   24: )    EMPTY_TUPLE
   25: \x81 NEWOBJ
   26: }    EMPTY_DICT
   27: (    MARK
   28: X        BINUNICODE '_inst'
   38: \x89     NEWFALSE
   39: X        BINUNICODE '_special'
   52: \x88     NEWTRUE
   53: X        BINUNICODE '_name'
   63: X        BINUNICODE 'List'
   72: X        BINUNICODE '__origin__'
   87: c        GLOBAL     'builtins list'
  102: X        BINUNICODE '__args__'
  115: c        GLOBAL     'typing TypeVar'
  131: )        EMPTY_TUPLE
  132: \x81     NEWOBJ
  133: q        BINPUT     0
  135: }        EMPTY_DICT
  136: (        MARK
  137: X            BINUNICODE 'name'
  146: X            BINUNICODE 'T'
  152: X            BINUNICODE 'bound'
  162: N            NONE
  163: X            BINUNICODE 'constraints'
  179: )            EMPTY_TUPLE
  180: X            BINUNICODE 'co'
  187: \x89         NEWFALSE
  188: X            BINUNICODE 'contra'
  199: \x89         NEWFALSE
  200: u            SETITEMS   (MARK at 136)
  201: b        BUILD
  202: \x85     TUPLE1
  203: X        BINUNICODE '__parameters__'
  222: h        BINGET     0
  224: \x85     TUPLE1
  225: X        BINUNICODE '__slots__'
  239: N        NONE
  240: u        SETITEMS   (MARK at 27)
  241: b    BUILD
  242: .    STOP
highest protocol among opcodes = 2

Unpickling it creates a new object. And I'm not sure all invariants are satisfied.

In additional to lesses efficiency and lost of preserving identity, such pickle can be incompatible with old Python versions and future Python versions if the internal representation of typing types will be changed.
History
Date User Action Args
2018-02-19 10:46:45serhiy.storchakasetrecipients: + serhiy.storchaka, gvanrossum, levkivskyi
2018-02-19 10:46:45serhiy.storchakasetmessageid: <1519037205.03.0.467229070634.issue32873@psf.upfronthosting.co.za>
2018-02-19 10:46:44serhiy.storchakalinkissue32873 messages
2018-02-19 10:46:44serhiy.storchakacreate