python / python/cpython

Can't pickle a bound method if `getattr(type(im_self), funcname)` fails

未關閉
#93,889 5 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

stdlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

MWE:

import pickle
import numpy as np
from scipy.interpolate import RectBivariateSpline

class Foo:
    def __init__(self):
        x = np.linspace(0,1,10)[:,None]
        y = np.linspace(2,3,9)[None,:]
        self.f = RectBivariateSpline(x,y,x*y)
        def f2(self,x,y):
            return self.f(x,y)
        self.f2 = f2.__get__(self)

foo = Foo()
pickled = pickle.dumps(foo)
unpickled = pickle.loads(pickled)

print(unpickled.f2(0.5, 0.6))

Expected output is to print [[1.]]. Actually gives an exception

Traceback (most recent call last):
  File ".../pickle-test.py", line 18, in <module>
    unpickled = pickle.loads(pickled)
AttributeError: 'Foo' object has no attribute 'f2'

This issue follows up from the identification an fixing of the same issue with dill, see
https://github.com/uqfoundation/dill/issues/510
https://github.com/uqfoundation/dill/pull/511
where @anivegesana commented

It seems to be a little bit more subtle than that. Because Python 3 implements its own reduce function for methods, I removed it from dill. It turns out that this is not a safe optimization in the case that the func is an inner function. Will be fixed soon.

Your environment

  • CPython versions tested on: 3.9.10
  • Operating system and architecture: Linux Mint 20.2 on x86-64

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先使用 pickle.dumps 和 pickle.loads 重現 MWE,重點關注指派給 Foo.f2 的 bound method 以及回報的 getattr(type(im_self), funcname) 失敗。追蹤該 method 的往返行為和現有的 pickle 處理;完成標準是範例能夠成功 unpickle 並列印 [[1.]]。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
tooling
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
需要釐清
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。