1

我正在尝试 dill 一个存储在字典中的类对象,但由于某种原因我不能这样做。

    local_env = {}
    global_env = {}
    exec(x, global_env, local_env)
    dill.dumps(local_env['Human'])

澄清一下,x 是一个字符串,它是一个普通/简单的类定义,“Human”是类的名称。运行上面的代码时出现以下错误。

------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in _getattribute(obj, name)
    267             parent = obj
--> 268             obj = getattr(obj, subpath)
    269         except AttributeError:

AttributeError: module 'builtins' has no attribute 'Human'

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in save_global(self, obj, name)
    906             module = sys.modules[module_name]
--> 907             obj2, parent = _getattribute(module, name)
    908         except (ImportError, KeyError, AttributeError):

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in _getattribute(obj, name)
    270             raise AttributeError("Can't get attribute {!r} on {!r}"
--> 271                                  .format(name, obj))
    272     return obj, parent

AttributeError: Can't get attribute 'Human' on <module 'builtins' (built-in)>

During handling of the above exception, another exception occurred:

PicklingError                             Traceback (most recent call last)
<ipython-input-27-6ce5592364a6> in <module>()
----> 1 dill.dumps(local_env['Human'])

/usr/local/lib/python3.5/site-packages/dill/dill.py in dumps(obj, protocol, byref, fmode, recurse)
    241     """pickle an object to a string"""
    242     file = StringIO()
--> 243     dump(obj, file, protocol, byref, fmode, recurse)#, strictio)
    244     return file.getvalue()
    245 

/usr/local/lib/python3.5/site-packages/dill/dill.py in dump(obj, file, protocol, byref, fmode, recurse)
    234             return
    235     # end hack
--> 236     pik.dump(obj)
    237     stack.clear()  # clear record of 'recursion-sensitive' pickled objects
    238     return

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in dump(self, obj)
    406         if self.proto >= 4:
    407             self.framer.start_framing()
--> 408         self.save(obj)
    409         self.write(STOP)
    410         self.framer.end_framing()

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in save(self, obj, save_persistent_id)
    473         f = self.dispatch.get(t)
    474         if f is not None:
--> 475             f(self, obj) # Call unbound method with explicit self
    476             return
    477 

/usr/local/lib/python3.5/site-packages/dill/dill.py in save_type(pickler, obj)
   1229        #print ("%s\n%s" % (type(obj), obj.__name__))
   1230        #print ("%s\n%s" % (obj.__bases__, obj.__dict__))
-> 1231         StockPickler.save_global(pickler, obj)
   1232         log.info("# T4")
   1233     return

/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pickle.py in save_global(self, obj, name)
    909             raise PicklingError(
    910                 "Can't pickle %r: it's not found as %s.%s" %
--> 911                 (obj, module_name, name))
    912         else:
    913             if obj2 is not obj:

PicklingError: Can't pickle <class 'Human'>: it's not found as builtins.Human
4

2 回答 2

1

我是dill作者。简短的回答是它不起作用,因为dill不知道如何。

>>> import dill
>>> local_env = {}
>>> global_env = {}
>>> x = "class Human(object):\n  pass"
>>> exec(x, global_env, local_env)
>>> local_env
{'Human': <class 'Human'>}
>>> dill.dumps(local_env['Human'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mmckerns/lib/python2.7/site-packages/dill-0.2.5.dev0-py2.7.egg/dill/dill.py", line 243, in dumps
    dump(obj, file, protocol, byref, fmode, recurse)#, strictio)
  File "/Users/mmckerns/lib/python2.7/site-packages/dill-0.2.5.dev0-py2.7.egg/dill/dill.py", line 236, in dump
    pik.dump(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 224, in dump
    self.save(obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self
  File "/Users/mmckerns/lib/python2.7/site-packages/dill-0.2.5.dev0-py2.7.egg/dill/dill.py", line 1231, in save_type
    StockPickler.save_global(pickler, obj)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 754, in save_global
    (obj, module, name))
pickle.PicklingError: Can't pickle <class 'Human'>: it's not found as __builtin__.Human

更长的答案是pickle只能序列化在文件内部定义的类,但不能在解释器中,或者更一般地在__main__. 但是,dill通常可以腌制这些动态构建的类,因为它__main__像文件一样对待,并解析 python 的内置缓冲区以获取类定义(以及其他内容)。将类定义隐藏在字符串中,然后用exec它来生成类……嗯,这种情况dill还不能处理。出于同样的原因,dill也不能腌制在其中定义的函数。exec然而,这两种情况都是有效的。

已经有一张功能票(https://github.com/uqfoundation/dill/issues/145)……所以我会在票上添加一个指向这个问题的链接。

于 2016-02-11T00:57:21.447 回答
0
import dill
import builtins
x = """
def test(x, y):
    return x + y
"""
env = {}
env['__builtins__'] = builtins
local_env = Environment()
exec(x, env, local_env)
exec("__builtins__.{0} = {0}".format('test'), env, local_env)
test = dill.loads(dill.dumps(local_env['test']))

使用 Mike 给出的答案作为灵感,我想出了一个解决方法。

于 2016-02-11T05:46:56.217 回答