Skip to content

Failed to loads a dumped MetaClass object #302

Closed

Description

@yitso

Hello, I met a problem when i call cloudpickle.loads.

This is my code:

import cloudpickle
from scrapy.item import Item, Field

MyItem = type('MyItem', (Item,), dict({'id': Field()}))
item = MyItem()
print(cloudpickle.loads(cloudpickle.dumps(item))) # {}
print(isinstance(dict().values(), MyItem)) # False
print(cloudpickle.loads(cloudpickle.dumps(item))) # raise 

I guess the cache of MetaClass cause to this problem.

isinstance call __instancecheck__ of ABCMeta and finally call __subclasscheck__ of ABCMeta.
code: https://github.com/python/cpython/blob/3.5/Lib/abc.py#L210

And cloudpickle.dumps will dumps the _abc_cache and _abc_negative_cache which has some invalid object, so when I call cloudpickle.loads in second , it raise exception.

Activity

  1. pierreglaser commented on Aug 1, 2019

    @pierreglaser
    Member

    Hi, thanks for reporting. I can reproduce on python3.6, your example raises:

    Traceback (most recent call last):                                                                                                                                                                                 
      File "test_scrapy.py", line 8, in <module>                                                                                                                                                                       
        print(cloudpickle.loads(cloudpickle.dumps(item)))  # raise                                                                                                                                                     
      File "/home/pierreglaser/repos/cloudpickle/cloudpickle/cloudpickle.py", line 1308, in _rehydrate_skelet                                                                                                          
    on_class                                                                                                                                                                                                           
        setattr(skeleton_class, attrname, attr)                                                                                                                                                                        
    TypeError: can't set attributes of built-in/extension type 'dict_values'   

    I'll look into it.

  2. pierreglaser commented on Aug 1, 2019

    @pierreglaser
    Member

    Indeed, the error comes from the fact that the negative cache of MyItem got populated with an unpickleable class (dict_values) during

    isinstance(dict().values(), MyItem)

    In general, I would lean towards not pickling the abc caches of a specifc class: we don't need the objects in the cache to pickle the said class, and this actually may make the final pickle unnecessarily large.

  3. yitso commented on Aug 1, 2019

    @yitso
    Author

    Thanks for your reply.
    not pickling the abc caches is great idea~

  4. added a commit that references this issue on Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions