Page 1 of 1

PicklingError on lambdas

Posted: Mon Feb 03, 2014 3:51 pm
by iichan_lolbot
It is imposible to save game if a value was set to lambda after game start.
Tested on RenPy 6.16.5, 6.15.7 and even 6.1.0c.

Here is the minimal test case:

Code: Select all

label start:
    "You can Save here."
    $ some_check = lambda: None
    "You cannot Save here."
Error text:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/_layout/screen_load_save.rpym", line 17, in script
  File "renpy/common/00action_file.rpy", line 241, in python
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "C:\renpy\renpy\execution.py", line 288, in run
    node.execute()
  File "C:\renpy\renpy\ast.py", line 720, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\renpy\renpy\python.py", line 1308, in py_exec_bytecode
    exec bytecode in globals, locals
  File "renpy/common/_layout/screen_load_save.rpym", line 17, in <module>
    $ ui.interact()
  File "C:\renpy\renpy\ui.py", line 237, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\renpy\renpy\display\core.py", line 1853, in interact
    repeat, rv = self.interact_core(preloads=preloads, **kwargs)
  File "C:\renpy\renpy\display\core.py", line 2406, in interact_core
    rv = root_widget.event(ev, x, y, 0)
  File "C:\renpy\renpy\display\layout.py", line 749, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\renpy\renpy\display\layout.py", line 749, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\renpy\renpy\display\layout.py", line 749, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\renpy\renpy\display\screen.py", line 319, in event
    rv = self.child.event(ev, x, y, st)
  File "C:\renpy\renpy\display\layout.py", line 749, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\renpy\renpy\display\layout.py", line 175, in event
    rv = d.event(ev, x - xo, y - yo, st)
  File "C:\renpy\renpy\display\layout.py", line 749, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\renpy\renpy\display\layout.py", line 175, in event
    rv = d.event(ev, x - xo, y - yo, st)
  File "C:\renpy\renpy\display\behavior.py", line 636, in event
    rv = run(self.clicked)
  File "C:\renpy\renpy\display\behavior.py", line 222, in run
    return var(*args, **kwargs)
  File "renpy/common/00action_file.rpy", line 241, in __call__
    renpy.save(fn, extra_info=save_name)
  File "C:\renpy\renpy\loadsave.py", line 272, in save
    dump((roots, renpy.game.log), logf)
  File "C:\renpy\renpy\loadsave.py", line 43, in dump
    cPickle.dump(o, f, cPickle.HIGHEST_PROTOCOL)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

Windows-post2008Server-6.2.9200
Ren'Py 6.16.5.525
test 0.0

Re: PicklingError on lambdas

Posted: Mon Feb 03, 2014 3:59 pm
by xela
Ren'Py is using Pythons pickle/cpickle for saving, just read it's documentation and you'll understand why.

Re: PicklingError on lambdas

Posted: Mon Feb 03, 2014 4:30 pm
by iichan_lolbot
Well, I understand the reasons why this does not work with cpickle. But are there are any plans to fix it in any future?
This of course is not a "batteries included" stuff, but this is surely possible to serialize a lambda and even to restore it's python source text.
Should things like this be considered as eternal "wontfix" or is there at least low-priority task to change it in some future, even years after?

Re: PicklingError on lambdas

Posted: Mon Feb 03, 2014 9:47 pm
by xela
iichan_lolbot wrote:Well, I understand the reasons why this does not work with cpickle. But are there are any plans to fix it in any future?
This of course is not a "batteries included" stuff, but this is surely possible to serialize a lambda and even to restore it's python source text.
Should things like this be considered as eternal "wontfix" or is there at least low-priority task to change it in some future, even years after?
No idea... I got this error once, figured out where it came from (I didn't know that lambda couldn't be pickled back then) and never used lambdas since (other than for sorting), can't say that it inconvenienced me in any way or form.

Re: PicklingError on lambdas

Posted: Mon Feb 03, 2014 9:58 pm
by PyTom
I don't think it's likely I'll add support for serializing lambdas.

Apart from my not knowing how to do it, I think it would have a lot of potential problems. Say you update a lambda in a new version of your game, but an old version is serialized in save data. That's a crash, at best.

So I'd put this in the wontfix bucket.