Skip to content

cloudpickle cannot save bound classmethods for python2.7 #288

Closed

Description

@pierreglaser
In [1]: import cloudpickle
   ...: class A:
   ...:     @classmethod
   ...:     def f(cls):
   ...:         pass
   ...: cloudpickle.dumps(A.f)

gives

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-b09b8cb46375> in <module>()
      4     def f(cls):
      5         pass
----> 6 cloudpickle.dumps(A.f)

/home/pierreglaser/.virtualenvs/py27/local/lib/python2.7/site-packages/cloudpickle/cloudpickle.pyc in dumps(obj, protocol)
    893     try:
    894         cp = CloudPickler(file, protocol=protocol)
--> 895         cp.dump(obj)
    896         return file.getvalue()
    897     finally:

/home/pierreglaser/.virtualenvs/py27/local/lib/python2.7/site-packages/cloudpickle/cloudpickle.pyc in dump(self, obj)
    266         self.inject_addons()
    267         try:
--> 268             return Pickler.dump(self, obj)
    269         except RuntimeError as e:
    270             if 'recursion' in e.args[0]:

/usr/lib/python2.7/pickle.py in dump(self, obj)
    222         if self.proto >= 2:
    223             self.write(PROTO + chr(self.proto))
--> 224         self.save(obj)
    225         self.write(STOP)
    226 

/usr/lib/python2.7/pickle.py in save(self, obj)
    284         f = self.dispatch.get(t)
    285         if f:
--> 286             f(self, obj) # Call unbound method with explicit self
    287             return
    288 

/home/pierreglaser/.virtualenvs/py27/local/lib/python2.7/site-packages/cloudpickle/cloudpickle.pyc in save_instancemethod(self, obj)
    667                 self.save_reduce(types.MethodType, (obj.__func__, obj.__self__), obj=obj)
    668             else:
--> 669                 self.save_reduce(types.MethodType, (obj.__func__, obj.__self__, obj.__self__.__class__),
    670                                  obj=obj)
    671 

AttributeError: class A has no attribute '__class__'

This does not seem to be a regression. The bug is present for any version of cloudpickle.

Activity

  1. ogrisel commented on Jun 12, 2019

    @ogrisel
    Contributor

    As it's a Python 2 only bug I would not make it a high priority bug unless a user encounters it in a real life usage scenario.

  2. ogrisel commented on Jun 12, 2019

    @ogrisel
    Contributor

    Hum I wrote the above comment prior to seeing that you already had found a fix for it...

  3. 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions